Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New bookmark backend + UI #92

Draft
wants to merge 13 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions data/resources/icons/library-symbolic.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions data/resources/meson.build
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
blueprints = custom_target('blueprints',
input: files(
'ui/bookmarks.blp',
'ui/window.blp',
'ui/shortcuts.blp',
'ui/input_page.blp',
Expand Down
5 changes: 5 additions & 0 deletions data/resources/resources.gresource.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@
<gresource prefix="/com/ranfdev/Geopard/">
<!-- see https://gtk-rs.org/gtk4-rs/git/docs/gtk4/struct.Application.html#automatic-resources -->
<file compressed="true" preprocess="xml-stripblanks" alias="gtk/help-overlay.ui">ui/shortcuts.ui</file>
<file compressed="true" preprocess="xml-stripblanks">ui/bookmarks.ui</file>
<file compressed="true" preprocess="xml-stripblanks">ui/window.ui</file>
<file compressed="true" preprocess="xml-stripblanks">ui/input_page.ui</file>
<file compressed="true" preprocess="xml-stripblanks">ui/download_page.ui</file>
<file compressed="true" preprocess="xml-stripblanks">ui/tab.ui</file>
</gresource>

<gresource prefix="/com/ranfdev/Geopard/icons/scalable/actions/">
<file preprocess="xml-stripblanks" alias="library-symbolic.svg">icons/library-symbolic.svg</file>
</gresource>
</gresources>
77 changes: 77 additions & 0 deletions data/resources/ui/bookmarks_window.blp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
using Gtk 4.0;
using Adw 1;

template $GeopardBookmarksWindow : Adw.Window {
title: _("Bookmarks");
default-height: 400;
default-width: 500;
height-request: 290;
width-request: 360;
modal: true;

Adw.ToastOverlay toast_overlay {
Adw.ToolbarView {
[top]
Adw.HeaderBar {
centering-policy: strict;

[end]
Gtk.ToggleButton search_button {
visible: false; // TODO: Remove
icon-name: "edit-find-symbolic";
tooltip-text: _("Search");
}

[end]
Gtk.Button select_items_button {
visible: false; // TODO: Remove
icon-name: "selection-mode-symbolic";
tooltip-text: _("Select Items");
}
}

content: Gtk.Stack stack {
transition-type: crossfade;

Gtk.StackPage {
name: "no_bookmarks_page";

child: Adw.StatusPage {
icon-name: "starred-symbolic";
title: _("No Bookmarks");
description: _("Bookmarked sites will appear here");
};
}

Gtk.StackPage {
name: "bookmarks_page";

child: Gtk.ScrolledWindow {
child: Gtk.Box {
orientation: vertical;
margin-top: 6;
margin-bottom: 6;
margin-start: 6;
margin-end: 6;
hexpand: true;
vexpand: true;

Adw.Clamp {
maximum-size: 1024;

child: Gtk.ListBox bookmarks_list {
activate-on-single-click: true;
selection-mode: none;

styles [
"boxed-list"
]
};
}
};
};
}
};
}
}
}
15 changes: 15 additions & 0 deletions data/resources/ui/window.blp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ template $GeopardWindow: Adw.ApplicationWindow {
primary: true;
}

[end]
Gtk.Button bookmarks_button {
icon-name: "library-symbolic";
action-name: "win.show-bookmarks";
tooltip-text: _("Bookmarks");
}

[end]
Gtk.Button desktop_tab_overview_btn {
icon-name: "view-grid-symbolic";
Expand Down Expand Up @@ -171,6 +178,13 @@ template $GeopardWindow: Adw.ApplicationWindow {
action-name: "overview.open";
}

[end]
Gtk.Button {
icon-name: "library-symbolic";
action-name: "win.show-bookmarks";
tooltip-text: _("Bookmarks");
}

[end]
Gtk.Button {
icon-name: "system-search-symbolic";
Expand All @@ -191,6 +205,7 @@ template $GeopardWindow: Adw.ApplicationWindow {
next_box.visible: false;
refresh_btn.visible: false;
tab_new_btn.visible: false;
bookmarks_button.visible: false;
desktop_tab_overview_btn.visible: false;
toolbar_view.reveal-bottom-bars: true;
tab_bar_revealer.reveal-child: false;
Expand Down
156 changes: 156 additions & 0 deletions src/bookmarks.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
use std::collections::BTreeMap;
use std::path::Path;

use anyhow::{Context, Ok};
use once_cell::sync::Lazy;
use serde::{Deserialize, Serialize};
use toml;

// todo!(bookmarks): replace bookmarks.bookmarks.insert() with bookmarks.insert_bookmark()
pub static DEFAULT_BOOKMARKS: Lazy<Bookmarks> = Lazy::new(|| {
let mut bookmarks = Bookmarks::default();

bookmarks.bookmarks.insert(
1.to_string(),
BookmarkBuilder::new()
.title("Gemini Project")
.url("gemini://geminiprotocol.net")
.build(),
);

bookmarks.bookmarks.insert(
2.to_string(),
BookmarkBuilder::new()
.title("Spacewalk aggregator")
.url("gemini://rawtext.club:1965/~sloum/spacewalk.gmi")
.build(),
);

bookmarks.bookmarks.insert(
3.to_string(),
BookmarkBuilder::new()
.title("About Geopard + help")
.url("about:help")
.build(),
);

bookmarks
});

#[derive(Clone, Default, Serialize, Deserialize, Debug)]
pub struct Bookmark {
title: String,
description: Option<String>,
url: String,
}

#[derive(Clone, Default, Debug)]
pub struct BookmarkBuilder {
title: String,
description: Option<String>,
url: String,
}

#[derive(Clone, Default, Serialize, Deserialize, Debug)]
pub struct Bookmarks {
#[serde(rename = "bookmark")]
pub bookmarks: BTreeMap<String, Bookmark>,
}

impl BookmarkBuilder {
pub fn new() -> Self {
Self::default()
}

pub fn title(mut self, title: &str) -> Self {
self.title = String::from(title);
self
}

pub fn description(mut self, description: Option<&str>) -> Self {
match description {
Some(desc) => self.description = Some(String::from(desc)),
None => self.description = None,
}
self
}

pub fn url(mut self, url: &str) -> Self {
self.url = String::from(url);
self
}

pub fn build(self) -> Bookmark {
Bookmark {
title: self.title,
description: self.description,
url: self.url,
}
}
}

impl Bookmark {
pub fn title(&self) -> String {
self.title.clone()
}

pub fn set_title(&mut self, title: &str) {
self.title = String::from(title);
}

pub fn description(&self) -> Option<String> {
self.description.as_ref().cloned()
}

pub fn set_description(&mut self, description: &str) {
self.description = Some(String::from(description));
}

pub fn url(&self) -> String {
self.url.clone()
}

pub fn set_url(&mut self, url: &str) {
self.url = String::from(url);
}
}

//todo!(bookmarks): Add from_gmi() method for migrations
impl Bookmarks {
pub async fn from_file(&self, path: &Path) -> anyhow::Result<Self> {
let file_str = async_fs::read_to_string(path)
.await
.context("Reading bookmarks file")?;

let bookmarks = toml::from_str(&file_str)?;

Ok(bookmarks)
}

pub async fn to_file(&self, path: &Path) -> anyhow::Result<()> {
let toml = toml::to_string(self)?;

async_fs::write(path, toml)
.await
.context("Writting data to bookmarks file")?;

Ok(())
}

//todo!(bookmarks): key must be the biggest current key + 1
pub fn insert_bookmark(&mut self, bookmark: Bookmark) {
self.bookmarks.insert(1.to_string(), bookmark);
}

pub fn update_bookmark(&mut self, key: u32, new_bookmark: Bookmark) {
self.bookmarks.insert(key.to_string(), new_bookmark);
}

pub fn remove_bookmark(&mut self, key: u32) {
if self.bookmarks.is_empty() {
return;
}

self.bookmarks.remove(&key.to_string());
}
}
26 changes: 4 additions & 22 deletions src/common/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use gtk::glib;
use once_cell::sync::Lazy;
use url::Url;

pub static DOWNLOAD_PATH: Lazy<std::path::PathBuf> = Lazy::new(|| {
let mut download_path = glib::user_special_dir(glib::UserDirectory::Downloads)
Expand All @@ -23,37 +22,20 @@ pub static KNOWN_HOSTS_PATH: Lazy<std::path::PathBuf> =
pub static CONFIG_DIR_PATH: Lazy<std::path::PathBuf> =
Lazy::new(|| glib::user_config_dir().join("geopard"));

pub static BOOKMARK_FILE_PATH: Lazy<std::path::PathBuf> =
pub static OLD_BOOKMARK_FILE_PATH: Lazy<std::path::PathBuf> =
Lazy::new(|| DATA_DIR_PATH.join("bookmarks.gemini"));

pub static BOOKMARK_FILE_PATH: Lazy<std::path::PathBuf> =
Lazy::new(|| DATA_DIR_PATH.join("bookmarks.toml"));

pub static SETTINGS_FILE_PATH: Lazy<std::path::PathBuf> =
Lazy::new(|| CONFIG_DIR_PATH.join("config.toml"));

pub static HISTORY_FILE_PATH: Lazy<std::path::PathBuf> =
Lazy::new(|| DATA_DIR_PATH.join("history.gemini"));

pub static DEFAULT_BOOKMARKS: &str = r"# Bookmarks

This is a gemini file where you can put all your bookmarks.
You can even edit this file in a text editor. That's how you
should remove bookmarks.

## Default bookmarks

=> gemini://geminiprotocol.net Gemini project
=> gemini://rawtext.club:1965/~sloum/spacewalk.gmi Spacewalk aggregator
=> about:help About geopard + help

## Custom bookmarks

";

pub const STREAMABLE_EXTS: [&str; 8] = ["mp3", "mp4", "webm", "opus", "wav", "ogg", "mkv", "flac"];

pub fn bookmarks_url() -> Url {
Url::parse(&format!("file://{}", BOOKMARK_FILE_PATH.to_str().unwrap())).unwrap()
}

pub fn glibctx() -> glib::MainContext {
glib::MainContext::default()
}
Loading
Loading