Skip to content

Commit

Permalink
misc: port to new gtk-rs version
Browse files Browse the repository at this point in the history
  • Loading branch information
SeaDve committed Feb 5, 2024
1 parent 27444f4 commit e842654
Show file tree
Hide file tree
Showing 10 changed files with 260 additions and 263 deletions.
323 changes: 179 additions & 144 deletions Cargo.lock

Large diffs are not rendered by default.

9 changes: 5 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,20 @@ edition = "2021"
lto = true

[dependencies]
adw = { package = "libadwaita", version = "0.5", features = ["v1_4"] }
adw = { package = "libadwaita", version = "0.6", features = ["v1_5"] }
anyhow = "1.0.75"
async-lock = "3.2"
futures-util = { version = "0.3.29", features = ["io"] }
futures-channel = "0.3.29"
fuzzy-matcher = "0.3.7"
gettext-rs = { version = "0.7", features = ["gettext-system"] }
gtk = { version = "0.7", package = "gtk4", features = ["gnome_45"] }
gtk_source = { package = "sourceview5", version = "0.7", features = ["v5_10"] }
gtk = { version = "0.8", package = "gtk4", features = ["gnome_45"] }
gtk_source = { package = "sourceview5", version = "0.8", features = ["v5_10"] }
indexmap = "2.1"
once_cell = "1.19.0"
regex = "1.10.2"
serde = { version = "1", features = ["derive"] }
serde_json = "1.0.112"
tracing = "0.1.37"
tracing-subscriber = "0.3"
webkit = { package = "webkit6", version = "0.2.0", features = ["v2_42"] }
webkit = { package = "webkit6", version = "0.3.0", features = ["v2_42"] }
3 changes: 2 additions & 1 deletion src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ const FILE_SAVER_FLAGS: gtk_source::FileSaverFlags =
mod imp {
use std::{cell::Cell, marker::PhantomData};

use glib::{once_cell::sync::Lazy, subclass::Signal};
use glib::subclass::Signal;
use once_cell::sync::Lazy;

use super::*;

Expand Down
3 changes: 2 additions & 1 deletion src/graph_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ mod imp {
use std::{cell::Cell, marker::PhantomData};

use async_lock::OnceCell;
use glib::{once_cell::sync::Lazy, subclass::Signal};
use glib::subclass::Signal;
use once_cell::sync::Lazy;

use super::*;

Expand Down
6 changes: 2 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,8 @@ mod window;
use std::{fs, path::PathBuf};

use gettextrs::LocaleCategory;
use gtk::{
gio,
glib::{self, once_cell::sync::Lazy},
};
use gtk::{gio, glib};
use once_cell::sync::Lazy;

use self::{
application::Application,
Expand Down
10 changes: 2 additions & 8 deletions src/page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ use anyhow::{Context, Result};
use gettextrs::gettext;
use gtk::{
gdk, gdk_pixbuf, gio,
glib::{self, clone, closure, once_cell::sync::Lazy},
glib::{self, clone, closure},
subclass::prelude::*,
};
use gtk_source::prelude::*;
use once_cell::sync::Lazy;
use regex::Regex;

use crate::{
Expand Down Expand Up @@ -117,43 +118,36 @@ mod imp {
gdk::Key::plus,
gdk::ModifierType::CONTROL_MASK,
"page.zoom-graph-in",
None,
);
klass.add_binding_action(
gdk::Key::KP_Add,
gdk::ModifierType::CONTROL_MASK,
"page.zoom-graph-in",
None,
);
klass.add_binding_action(
gdk::Key::equal,
gdk::ModifierType::CONTROL_MASK,
"page.zoom-graph-in",
None,
);
klass.add_binding_action(
gdk::Key::minus,
gdk::ModifierType::CONTROL_MASK,
"page.zoom-graph-out",
None,
);
klass.add_binding_action(
gdk::Key::KP_Subtract,
gdk::ModifierType::CONTROL_MASK,
"page.zoom-graph-out",
None,
);
klass.add_binding_action(
gdk::Key::_0,
gdk::ModifierType::CONTROL_MASK,
"page.reset-graph-zoom",
None,
);
klass.add_binding_action(
gdk::Key::KP_0,
gdk::ModifierType::CONTROL_MASK,
"page.reset-graph-zoom",
None,
);
}

Expand Down
8 changes: 2 additions & 6 deletions src/recent_item.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
use fuzzy_matcher::{skim::SkimMatcherV2, FuzzyMatcher};
use gtk::{
gio,
glib::{self, once_cell::sync::Lazy},
prelude::*,
subclass::prelude::*,
};
use gtk::{gio, glib, prelude::*, subclass::prelude::*};
use once_cell::sync::Lazy;

static FUZZY_MATCHER: Lazy<SkimMatcherV2> = Lazy::new(SkimMatcherV2::default);

Expand Down
3 changes: 2 additions & 1 deletion src/recent_popover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ use crate::{
mod imp {
use std::cell::OnceCell;

use glib::{once_cell::sync::Lazy, subclass::Signal};
use glib::subclass::Signal;
use once_cell::sync::Lazy;

use super::*;

Expand Down
3 changes: 2 additions & 1 deletion src/recent_row.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ use crate::{i18n::ngettext_f, recent_item::RecentItem, utils};
mod imp {
use std::cell::OnceCell;

use glib::{once_cell::sync::Lazy, subclass::Signal};
use glib::subclass::Signal;
use once_cell::sync::Lazy;

use super::*;

Expand Down
155 changes: 62 additions & 93 deletions src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,38 +146,46 @@ mod imp {
},
);

klass.install_action_async("win.export-graph", Some("s"), |obj, _, arg| async move {
let raw_format = arg.unwrap().get::<String>().unwrap();

let format = match raw_format.as_str() {
"svg" => ExportFormat::Svg,
"png" => ExportFormat::Png,
"jpeg" => ExportFormat::Jpeg,
_ => unreachable!("unknown format `{}`", raw_format),
};

let page = obj.selected_page().unwrap();
debug_assert!(page.can_export_graph());
klass.install_action_async(
"win.export-graph",
Some(glib::VariantTy::STRING),
|obj, _, arg| async move {
let raw_format = arg.unwrap().get::<String>().unwrap();

let format = match raw_format.as_str() {
"svg" => ExportFormat::Svg,
"png" => ExportFormat::Png,
"jpeg" => ExportFormat::Jpeg,
_ => unreachable!("unknown format `{}`", raw_format),
};

if let Err(err) = page.export_graph(format).await {
if !err
.downcast_ref::<glib::Error>()
.is_some_and(|error| error.matches(gtk::DialogError::Dismissed))
{
tracing::error!("Failed to export graph: {:?}", err);
obj.add_message_toast(&gettext("Failed to export graph"));
let page = obj.selected_page().unwrap();
debug_assert!(page.can_export_graph());

if let Err(err) = page.export_graph(format).await {
if !err
.downcast_ref::<glib::Error>()
.is_some_and(|error| error.matches(gtk::DialogError::Dismissed))
{
tracing::error!("Failed to export graph: {:?}", err);
obj.add_message_toast(&gettext("Failed to export graph"));
}
}
}
});
},
);

klass.install_action("win.select-page", Some("i"), |obj, _, args| {
let index = args.unwrap().get::<i32>().unwrap();
klass.install_action(
"win.select-page",
Some(glib::VariantTy::INT32),
|obj, _, args| {
let index = args.unwrap().get::<i32>().unwrap();

if index < obj.n_pages() {
let page = obj.nth_page(index);
obj.set_selected_page(&page);
}
});
if index < obj.n_pages() {
let page = obj.nth_page(index);
obj.set_selected_page(&page);
}
},
);

klass.install_action("win.move-page-to-left", None, |obj, _, _| {
let imp = obj.imp();
Expand Down Expand Up @@ -239,124 +247,68 @@ mod imp {
gdk::Key::T,
gdk::ModifierType::CONTROL_MASK,
"win.new-document",
None,
);
klass.add_binding_action(
gdk::Key::O,
gdk::ModifierType::CONTROL_MASK,
"win.open-document",
None,
);
klass.add_binding_action(
gdk::Key::S,
gdk::ModifierType::CONTROL_MASK,
"win.save-document",
None,
);
klass.add_binding_action(
gdk::Key::S,
gdk::ModifierType::CONTROL_MASK | gdk::ModifierType::SHIFT_MASK,
"win.save-document-as",
None,
);

klass.add_binding_action(
gdk::Key::_1,
gdk::ModifierType::CONTROL_MASK,
"win.select-page",
Some(&0.into()),
);
klass.add_binding_action(
gdk::Key::_2,
gdk::ModifierType::CONTROL_MASK,
"win.select-page",
Some(&1.into()),
);
klass.add_binding_action(
gdk::Key::_3,
gdk::ModifierType::CONTROL_MASK,
"win.select-page",
Some(&2.into()),
);
klass.add_binding_action(
gdk::Key::_4,
gdk::ModifierType::CONTROL_MASK,
"win.select-page",
Some(&3.into()),
);
klass.add_binding_action(
gdk::Key::_5,
gdk::ModifierType::CONTROL_MASK,
"win.select-page",
Some(&4.into()),
);
klass.add_binding_action(
gdk::Key::_6,
gdk::ModifierType::CONTROL_MASK,
"win.select-page",
Some(&5.into()),
);
klass.add_binding_action(
gdk::Key::_7,
gdk::ModifierType::CONTROL_MASK,
"win.select-page",
Some(&6.into()),
);
klass.add_binding_action(
gdk::Key::_8,
gdk::ModifierType::CONTROL_MASK,
"win.select-page",
Some(&7.into()),
);
klass.add_binding_action(
gdk::Key::_9,
gdk::ModifierType::CONTROL_MASK,
"win.select-page",
Some(&8.into()),
);
add_select_page_action(klass, gdk::Key::_1, 0);
add_select_page_action(klass, gdk::Key::_2, 1);
add_select_page_action(klass, gdk::Key::_3, 2);
add_select_page_action(klass, gdk::Key::_4, 3);
add_select_page_action(klass, gdk::Key::_5, 4);
add_select_page_action(klass, gdk::Key::_6, 5);
add_select_page_action(klass, gdk::Key::_7, 6);
add_select_page_action(klass, gdk::Key::_8, 7);
add_select_page_action(klass, gdk::Key::_9, 8);

klass.add_binding_action(
gdk::Key::Page_Up,
gdk::ModifierType::CONTROL_MASK | gdk::ModifierType::SHIFT_MASK,
"win.move-page-to-left",
None,
);
klass.add_binding_action(
gdk::Key::KP_Page_Up,
gdk::ModifierType::CONTROL_MASK | gdk::ModifierType::SHIFT_MASK,
"win.move-page-to-left",
None,
);
klass.add_binding_action(
gdk::Key::Page_Down,
gdk::ModifierType::CONTROL_MASK | gdk::ModifierType::SHIFT_MASK,
"win.move-page-to-right",
None,
);
klass.add_binding_action(
gdk::Key::KP_Page_Down,
gdk::ModifierType::CONTROL_MASK | gdk::ModifierType::SHIFT_MASK,
"win.move-page-to-right",
None,
);
klass.add_binding_action(
gdk::Key::N,
gdk::ModifierType::CONTROL_MASK | gdk::ModifierType::SHIFT_MASK,
"win.move-page-to-new-window",
None,
);
klass.add_binding_action(
gdk::Key::W,
gdk::ModifierType::CONTROL_MASK,
"win.close-page-or-window",
None,
);

klass.add_binding_action(
gdk::Key::T,
gdk::ModifierType::CONTROL_MASK | gdk::ModifierType::SHIFT_MASK,
"win.undo-close-page",
None,
);
}

Expand Down Expand Up @@ -883,3 +835,20 @@ impl Window {
self.action_set_enabled("win.undo-close-page", !is_empty);
}
}

fn add_select_page_action(
klass: &mut <imp::Window as ObjectSubclass>::Class,
key: gdk::Key,
page_index: i32,
) {
klass.add_shortcut(
&gtk::Shortcut::builder()
.trigger(&gtk::KeyvalTrigger::new(
key,
gdk::ModifierType::CONTROL_MASK,
))
.action(&gtk::NamedAction::new("win.select-page"))
.arguments(&page_index.into())
.build(),
);
}

0 comments on commit e842654

Please sign in to comment.