From bc0688816fd6c8a2b4c01f4b5ead3de2dbfcd217 Mon Sep 17 00:00:00 2001 From: Leopold Luley Date: Sun, 10 Nov 2024 18:09:03 +0100 Subject: [PATCH 1/8] Merge config mod and imp files. --- gnome/src/config.rs | 111 ++++++++++++++++++++++++++++++++++++++++ gnome/src/config/imp.rs | 92 --------------------------------- gnome/src/config/mod.rs | 19 ------- 3 files changed, 111 insertions(+), 111 deletions(-) create mode 100644 gnome/src/config.rs delete mode 100644 gnome/src/config/imp.rs delete mode 100644 gnome/src/config/mod.rs diff --git a/gnome/src/config.rs b/gnome/src/config.rs new file mode 100644 index 0000000..7800b8e --- /dev/null +++ b/gnome/src/config.rs @@ -0,0 +1,111 @@ +use gtk::glib; + +glib::wrapper! { + pub struct Config(ObjectSubclass); +} + +impl Config { + pub fn new() -> Config { + glib::Object::new() + } +} + +impl Default for Config { + fn default() -> Self { + Self::new() + } +} + +mod imp { + use cosmic_config::{ConfigGet, ConfigSet}; + use glib::prelude::*; + use gtk::{glib, subclass::prelude::*}; + use std::cell::{Cell, OnceCell}; + + use crate::APP_ID; + + #[derive(Default, glib::Properties)] + #[properties(wrapper_type = super::Config)] + pub struct Config { + #[property(get, set = Self::set_window_width, default = 1600)] + window_width: Cell, + #[property(get, set = Self::set_window_height, default = 900)] + window_height: Cell, + #[property(get, set = Self::set_window_maximized, default = false)] + window_maximized: Cell, + + #[property(get, set = Self::set_search_pdf, default = false)] + search_pdf: Cell, + #[property(get, set = Self::set_search_office, default = false)] + search_office: Cell, + + config: OnceCell, + } + + #[glib::object_subclass] + impl ObjectSubclass for Config { + const NAME: &'static str = "ClapgrepConfig"; + type Type = super::Config; + } + + #[glib::derived_properties] + impl ObjectImpl for Config { + fn constructed(&self) { + let config = cosmic_config::Config::new(APP_ID, 1).expect("failed to open config"); + + if let Ok(window_width) = config.get("window_width") { + self.window_width.set(window_width); + } + + if let Ok(window_height) = config.get("window_height") { + self.window_height.set(window_height); + } + + if let Ok(window_maximized) = config.get("window_maximized") { + self.window_maximized.set(window_maximized); + } + + if let Ok(search_pdf) = config.get("search_pdf") { + self.search_pdf.set(search_pdf); + } + + if let Ok(search_office) = config.get("search_office") { + self.search_office.set(search_office); + } + + let _ = self.config.set(config); + } + } + + impl Config { + fn set_window_width(&self, width: i32) { + let config = self.config.get().unwrap(); + let _ = config.set("window_width", width); + self.window_width.set(width); + } + + fn set_window_height(&self, height: i32) { + let config = self.config.get().unwrap(); + let _ = config.set("window_height", height); + self.window_height.set(height); + } + + fn set_window_maximized(&self, maximized: bool) { + let config = self.config.get().unwrap(); + let _ = config.set("window_maximized", maximized); + self.window_maximized.set(maximized); + } + + fn set_search_pdf(&self, search_pdf: bool) { + let config = self.config.get().unwrap(); + let _ = config.set("search_pdf", search_pdf); + self.search_pdf.set(search_pdf); + } + + fn set_search_office(&self, search_office: bool) { + let config = self.config.get().unwrap(); + let _ = config.set("search_office", search_office); + self.search_office.set(search_office); + } + } +} diff --git a/gnome/src/config/imp.rs b/gnome/src/config/imp.rs deleted file mode 100644 index 56fff69..0000000 --- a/gnome/src/config/imp.rs +++ /dev/null @@ -1,92 +0,0 @@ -use cosmic_config::{ConfigGet, ConfigSet}; -use glib::prelude::*; -use gtk::{glib, subclass::prelude::*}; -use std::cell::{Cell, OnceCell}; - -use crate::APP_ID; - -#[derive(Default, glib::Properties)] -#[properties(wrapper_type = super::Config)] -pub struct Config { - #[property(get, set = Self::set_window_width, default = 1600)] - window_width: Cell, - #[property(get, set = Self::set_window_height, default = 900)] - window_height: Cell, - #[property(get, set = Self::set_window_maximized, default = false)] - window_maximized: Cell, - - #[property(get, set = Self::set_search_pdf, default = false)] - search_pdf: Cell, - #[property(get, set = Self::set_search_office, default = false)] - search_office: Cell, - - config: OnceCell, -} - -impl Config { - fn set_window_width(&self, width: i32) { - let config = self.config.get().unwrap(); - let _ = config.set("window_width", width); - self.window_width.set(width); - } - - fn set_window_height(&self, height: i32) { - let config = self.config.get().unwrap(); - let _ = config.set("window_height", height); - self.window_height.set(height); - } - - fn set_window_maximized(&self, maximized: bool) { - let config = self.config.get().unwrap(); - let _ = config.set("window_maximized", maximized); - self.window_maximized.set(maximized); - } - - fn set_search_pdf(&self, search_pdf: bool) { - let config = self.config.get().unwrap(); - let _ = config.set("search_pdf", search_pdf); - self.search_pdf.set(search_pdf); - } - - fn set_search_office(&self, search_office: bool) { - let config = self.config.get().unwrap(); - let _ = config.set("search_office", search_office); - self.search_office.set(search_office); - } -} - -// Basic declaration of our type for the GObject type system -#[glib::object_subclass] -impl ObjectSubclass for Config { - const NAME: &'static str = "ClapgrepConfig"; - type Type = super::Config; -} - -#[glib::derived_properties] -impl ObjectImpl for Config { - fn constructed(&self) { - let config = cosmic_config::Config::new(APP_ID, 1).expect("failed to open config"); - - if let Ok(window_width) = config.get("window_width") { - self.window_width.set(window_width); - } - - if let Ok(window_height) = config.get("window_height") { - self.window_height.set(window_height); - } - - if let Ok(window_maximized) = config.get("window_maximized") { - self.window_maximized.set(window_maximized); - } - - if let Ok(search_pdf) = config.get("search_pdf") { - self.search_pdf.set(search_pdf); - } - - if let Ok(search_office) = config.get("search_office") { - self.search_office.set(search_office); - } - - let _ = self.config.set(config); - } -} diff --git a/gnome/src/config/mod.rs b/gnome/src/config/mod.rs deleted file mode 100644 index d0c4b4e..0000000 --- a/gnome/src/config/mod.rs +++ /dev/null @@ -1,19 +0,0 @@ -mod imp; - -use gtk::glib; - -glib::wrapper! { - pub struct Config(ObjectSubclass); -} - -impl Config { - pub fn new() -> Config { - glib::Object::new() - } -} - -impl Default for Config { - fn default() -> Self { - Self::new() - } -} From 096d936dd1a71dfacd7bdd270d30bef2c81bfab4 Mon Sep 17 00:00:00 2001 From: Leopold Luley Date: Sun, 10 Nov 2024 18:10:56 +0100 Subject: [PATCH 2/8] Merge search_match mod and imp files. --- gnome/src/search_match.rs | 38 +++++++++++++++++++++++++++++++++++ gnome/src/search_match/imp.rs | 23 --------------------- gnome/src/search_match/mod.rs | 16 --------------- 3 files changed, 38 insertions(+), 39 deletions(-) create mode 100644 gnome/src/search_match.rs delete mode 100644 gnome/src/search_match/imp.rs delete mode 100644 gnome/src/search_match/mod.rs diff --git a/gnome/src/search_match.rs b/gnome/src/search_match.rs new file mode 100644 index 0000000..e96cdce --- /dev/null +++ b/gnome/src/search_match.rs @@ -0,0 +1,38 @@ +use gtk::glib; + +glib::wrapper! { + pub struct SearchMatch(ObjectSubclass); +} + +impl SearchMatch { + pub fn new(start: u32, end: u32) -> SearchMatch { + glib::Object::builder() + .property("start", start) + .property("end", end) + .build() + } +} + +mod imp { + use glib::prelude::*; + use gtk::{glib, subclass::prelude::*}; + use std::cell::Cell; + + #[derive(Default, glib::Properties)] + #[properties(wrapper_type = super::SearchMatch)] + pub struct SearchMatch { + #[property(get, set)] + start: Cell, + #[property(get, set)] + end: Cell, + } + + #[glib::object_subclass] + impl ObjectSubclass for SearchMatch { + const NAME: &'static str = "ClapgrepSearchMatch"; + type Type = super::SearchMatch; + } + + #[glib::derived_properties] + impl ObjectImpl for SearchMatch {} +} diff --git a/gnome/src/search_match/imp.rs b/gnome/src/search_match/imp.rs deleted file mode 100644 index 583aab4..0000000 --- a/gnome/src/search_match/imp.rs +++ /dev/null @@ -1,23 +0,0 @@ -use std::cell::Cell; - -use glib::prelude::*; -use gtk::{glib, subclass::prelude::*}; - -#[derive(Default, glib::Properties)] -#[properties(wrapper_type = super::SearchMatch)] -pub struct SearchMatch { - #[property(get, set)] - start: Cell, - #[property(get, set)] - end: Cell, -} - -// Basic declaration of our type for the GObject type system -#[glib::object_subclass] -impl ObjectSubclass for SearchMatch { - const NAME: &'static str = "ClapgrepSearchMatch"; - type Type = super::SearchMatch; -} - -#[glib::derived_properties] -impl ObjectImpl for SearchMatch {} diff --git a/gnome/src/search_match/mod.rs b/gnome/src/search_match/mod.rs deleted file mode 100644 index f05adc6..0000000 --- a/gnome/src/search_match/mod.rs +++ /dev/null @@ -1,16 +0,0 @@ -mod imp; - -use gtk::glib; - -glib::wrapper! { - pub struct SearchMatch(ObjectSubclass); -} - -impl SearchMatch { - pub fn new(start: u32, end: u32) -> SearchMatch { - glib::Object::builder() - .property("start", start) - .property("end", end) - .build() - } -} From 271bc3cc7de4695a2b0ec1d8f591244a0372efc4 Mon Sep 17 00:00:00 2001 From: Leopold Luley Date: Sun, 10 Nov 2024 18:12:29 +0100 Subject: [PATCH 3/8] Merge search_result mod and imp files. --- .../mod.rs => search_result.rs} | 39 ++++++++++++++++++- gnome/src/search_result/imp.rs | 35 ----------------- 2 files changed, 37 insertions(+), 37 deletions(-) rename gnome/src/{search_result/mod.rs => search_result.rs} (71%) delete mode 100644 gnome/src/search_result/imp.rs diff --git a/gnome/src/search_result/mod.rs b/gnome/src/search_result.rs similarity index 71% rename from gnome/src/search_result/mod.rs rename to gnome/src/search_result.rs index f12e667..59fb458 100644 --- a/gnome/src/search_result/mod.rs +++ b/gnome/src/search_result.rs @@ -1,5 +1,3 @@ -mod imp; - use crate::search_match::SearchMatch; use clapgrep_core::{Location, Match}; use gtk::{ @@ -82,3 +80,40 @@ impl SearchResult { Vec::new() } } + +mod imp { + use gtk::{ + gio, + glib::{self, prelude::*}, + subclass::prelude::*, + }; + use std::{ + cell::{Cell, RefCell}, + path::PathBuf, + }; + + #[derive(Default, glib::Properties)] + #[properties(wrapper_type = super::SearchResult)] + pub struct SearchResult { + #[property(get, set)] + file: RefCell, + #[property(get, set)] + uri: RefCell, + #[property(get, set)] + line: Cell, + #[property(get, set)] + content: RefCell, + #[property(get, set, construct)] + matches: RefCell>, + } + + // Basic declaration of our type for the GObject type system + #[glib::object_subclass] + impl ObjectSubclass for SearchResult { + const NAME: &'static str = "ClapgrepSearchResult"; + type Type = super::SearchResult; + } + + #[glib::derived_properties] + impl ObjectImpl for SearchResult {} +} diff --git a/gnome/src/search_result/imp.rs b/gnome/src/search_result/imp.rs deleted file mode 100644 index 8fb8578..0000000 --- a/gnome/src/search_result/imp.rs +++ /dev/null @@ -1,35 +0,0 @@ -use std::{ - cell::{Cell, RefCell}, - path::PathBuf, -}; - -use gtk::{ - gio, - glib::{self, prelude::*}, - subclass::prelude::*, -}; - -#[derive(Default, glib::Properties)] -#[properties(wrapper_type = super::SearchResult)] -pub struct SearchResult { - #[property(get, set)] - file: RefCell, - #[property(get, set)] - uri: RefCell, - #[property(get, set)] - line: Cell, - #[property(get, set)] - content: RefCell, - #[property(get, set, construct)] - matches: RefCell>, -} - -// Basic declaration of our type for the GObject type system -#[glib::object_subclass] -impl ObjectSubclass for SearchResult { - const NAME: &'static str = "ClapgrepSearchResult"; - type Type = super::SearchResult; -} - -#[glib::derived_properties] -impl ObjectImpl for SearchResult {} From 0d2ad4ac8afb19c5c0f4e6b6453fbc581a848951 Mon Sep 17 00:00:00 2001 From: Leopold Luley Date: Sun, 10 Nov 2024 18:14:06 +0100 Subject: [PATCH 4/8] Merge search_model mod and imp files. --- .../{search_model/mod.rs => search_model.rs} | 74 +++++++++++++++++-- gnome/src/search_model/imp.rs | 63 ---------------- 2 files changed, 69 insertions(+), 68 deletions(-) rename gnome/src/{search_model/mod.rs => search_model.rs} (56%) delete mode 100644 gnome/src/search_model/imp.rs diff --git a/gnome/src/search_model/mod.rs b/gnome/src/search_model.rs similarity index 56% rename from gnome/src/search_model/mod.rs rename to gnome/src/search_model.rs index 03b27fc..b1b8aab 100644 --- a/gnome/src/search_model/mod.rs +++ b/gnome/src/search_model.rs @@ -1,5 +1,3 @@ -mod imp; - use crate::search_result::SearchResult; use gtk::{gio, glib, prelude::*, subclass::prelude::*}; use imp::Section; @@ -10,6 +8,12 @@ glib::wrapper! { @implements gio::ListModel, gtk::SectionModel; } +impl Default for SearchModel { + fn default() -> Self { + Self::new() + } +} + impl SearchModel { pub fn new() -> SearchModel { glib::Object::new() @@ -71,8 +75,68 @@ impl SearchModel { } } -impl Default for SearchModel { - fn default() -> Self { - Self::new() +mod imp { + use crate::search_result::SearchResult; + use gtk::{gio, glib, prelude::*, subclass::prelude::*}; + use std::{cell::RefCell, path::PathBuf}; + + #[derive(Debug, Default)] + pub struct SearchModel { + pub data: RefCell>, + pub sections: RefCell>, + pub base_path: RefCell, + } + + #[derive(Debug, Clone, Copy)] + pub struct Section { + pub start: u32, + pub end: u32, + } + + impl Section { + pub fn size(&self) -> u32 { + self.end - self.start + } + } + + #[glib::object_subclass] + impl ObjectSubclass for SearchModel { + const NAME: &'static str = "ClapgrepSearchModel"; + type Type = super::SearchModel; + type Interfaces = (gio::ListModel, gtk::SectionModel); + } + + impl ObjectImpl for SearchModel {} + + impl ListModelImpl for SearchModel { + fn item_type(&self) -> glib::Type { + SearchResult::static_type() + } + + fn n_items(&self) -> u32 { + self.data.borrow().len() as u32 + } + + fn item(&self, position: u32) -> Option { + self.data + .borrow() + .get(position as usize) + .map(|o| o.clone().upcast::()) + } + } + + impl SectionModelImpl for SearchModel { + fn section(&self, position: u32) -> (u32, u32) { + let mut total = 0; + for section in self.sections.borrow().iter() { + total += section.size(); + + if total > position { + return (section.start, section.end); + } + } + + panic!("missing section") + } } } diff --git a/gnome/src/search_model/imp.rs b/gnome/src/search_model/imp.rs deleted file mode 100644 index c4db06f..0000000 --- a/gnome/src/search_model/imp.rs +++ /dev/null @@ -1,63 +0,0 @@ -use crate::search_result::SearchResult; -use gtk::{gio, glib, prelude::*, subclass::prelude::*}; -use std::{cell::RefCell, path::PathBuf}; - -#[derive(Debug, Default)] -pub struct SearchModel { - pub data: RefCell>, - pub sections: RefCell>, - pub base_path: RefCell, -} - -#[derive(Debug, Clone, Copy)] -pub struct Section { - pub start: u32, - pub end: u32, -} - -impl Section { - pub fn size(&self) -> u32 { - self.end - self.start - } -} - -#[glib::object_subclass] -impl ObjectSubclass for SearchModel { - const NAME: &'static str = "ClapgrepSearchModel"; - type Type = super::SearchModel; - type Interfaces = (gio::ListModel, gtk::SectionModel); -} - -impl ObjectImpl for SearchModel {} - -impl ListModelImpl for SearchModel { - fn item_type(&self) -> glib::Type { - SearchResult::static_type() - } - - fn n_items(&self) -> u32 { - self.data.borrow().len() as u32 - } - - fn item(&self, position: u32) -> Option { - self.data - .borrow() - .get(position as usize) - .map(|o| o.clone().upcast::()) - } -} - -impl SectionModelImpl for SearchModel { - fn section(&self, position: u32) -> (u32, u32) { - let mut total = 0; - for section in self.sections.borrow().iter() { - total += section.size(); - - if total > position { - return (section.start, section.end); - } - } - - panic!("missing section") - } -} From 5c705508efcaaf8dd686f299476e0019f373f4a0 Mon Sep 17 00:00:00 2001 From: Leopold Luley Date: Sun, 10 Nov 2024 18:19:51 +0100 Subject: [PATCH 5/8] Move search_ modules into new search module. --- gnome/src/main.rs | 4 +--- gnome/src/search/mod.rs | 8 ++++++++ gnome/src/{search_model.rs => search/model.rs} | 4 ++-- gnome/src/{search_match.rs => search/range.rs} | 0 gnome/src/{search_result.rs => search/result.rs} | 2 +- gnome/src/search_window/imp.rs | 2 +- 6 files changed, 13 insertions(+), 7 deletions(-) create mode 100644 gnome/src/search/mod.rs rename gnome/src/{search_model.rs => search/model.rs} (97%) rename gnome/src/{search_match.rs => search/range.rs} (100%) rename gnome/src/{search_result.rs => search/result.rs} (98%) diff --git a/gnome/src/main.rs b/gnome/src/main.rs index 4d4a956..9b54282 100644 --- a/gnome/src/main.rs +++ b/gnome/src/main.rs @@ -9,9 +9,7 @@ use std::path::PathBuf; mod about; mod config; mod error_window; -mod search_match; -mod search_model; -mod search_result; +mod search; mod search_window; const APP_ID: &str = env!("APP_ID"); diff --git a/gnome/src/search/mod.rs b/gnome/src/search/mod.rs new file mode 100644 index 0000000..fc0c097 --- /dev/null +++ b/gnome/src/search/mod.rs @@ -0,0 +1,8 @@ +mod model; +pub use model::SearchModel; + +mod range; +pub use range::SearchMatch; + +mod result; +pub use result::SearchResult; diff --git a/gnome/src/search_model.rs b/gnome/src/search/model.rs similarity index 97% rename from gnome/src/search_model.rs rename to gnome/src/search/model.rs index b1b8aab..d1904d2 100644 --- a/gnome/src/search_model.rs +++ b/gnome/src/search/model.rs @@ -1,4 +1,4 @@ -use crate::search_result::SearchResult; +use crate::search::SearchResult; use gtk::{gio, glib, prelude::*, subclass::prelude::*}; use imp::Section; use std::path::PathBuf; @@ -76,7 +76,7 @@ impl SearchModel { } mod imp { - use crate::search_result::SearchResult; + use crate::search::SearchResult; use gtk::{gio, glib, prelude::*, subclass::prelude::*}; use std::{cell::RefCell, path::PathBuf}; diff --git a/gnome/src/search_match.rs b/gnome/src/search/range.rs similarity index 100% rename from gnome/src/search_match.rs rename to gnome/src/search/range.rs diff --git a/gnome/src/search_result.rs b/gnome/src/search/result.rs similarity index 98% rename from gnome/src/search_result.rs rename to gnome/src/search/result.rs index 59fb458..ffa0e8c 100644 --- a/gnome/src/search_result.rs +++ b/gnome/src/search/result.rs @@ -1,4 +1,4 @@ -use crate::search_match::SearchMatch; +use crate::search::SearchMatch; use clapgrep_core::{Location, Match}; use gtk::{ gio, glib, pango, diff --git a/gnome/src/search_window/imp.rs b/gnome/src/search_window/imp.rs index 3629e11..688c197 100644 --- a/gnome/src/search_window/imp.rs +++ b/gnome/src/search_window/imp.rs @@ -1,4 +1,4 @@ -use crate::{config::Config, error_window::ErrorWindow, search_model::SearchModel}; +use crate::{config::Config, error_window::ErrorWindow, search::SearchModel}; use adw::subclass::prelude::*; use clapgrep_core::{SearchEngine, SearchFlags, SearchMessage, SearchParameters}; use glib::subclass::InitializingObject; From e20e5dd03c4cda08d167e7f13032fab14c9ca5fe Mon Sep 17 00:00:00 2001 From: Leopold Luley Date: Sun, 10 Nov 2024 18:24:35 +0100 Subject: [PATCH 6/8] Move Application code into app.rs file. --- gnome/src/app.rs | 73 +++++++++++++++++++++++++++++++++++++++++++ gnome/src/main.rs | 79 ++++------------------------------------------- 2 files changed, 79 insertions(+), 73 deletions(-) create mode 100644 gnome/src/app.rs diff --git a/gnome/src/app.rs b/gnome/src/app.rs new file mode 100644 index 0000000..544a6d3 --- /dev/null +++ b/gnome/src/app.rs @@ -0,0 +1,73 @@ +use adw::prelude::*; +use gtk::gio::SimpleAction; +use gtk::gio::{self}; +use gtk::glib::{self, clone}; +use gtk::{gdk, STYLE_PROVIDER_PRIORITY_APPLICATION}; +use gtk_blueprint::include_blp; + +use crate::about; +use crate::search_window::SearchWindow; + +pub fn start(app: &adw::Application, files: &[gio::File]) { + let app = app.downcast_ref::().unwrap(); + + let style_provider = gtk::CssProvider::new(); + style_provider.load_from_string(include_str!("styles.css")); + gtk::style_context_add_provider_for_display( + &gdk::Display::default().unwrap(), + &style_provider, + STYLE_PROVIDER_PRIORITY_APPLICATION, + ); + + let window = SearchWindow::new(app); + + if let Some(dir) = files.first() { + if let Some(path) = dir.path() { + if path.is_dir() { + window.set_search_path(path); + } + } + } + + let about_action = SimpleAction::new("about", None); + about_action.connect_activate(clone!( + #[weak] + window, + move |_, _| about::dialog().present(Some(&window)) + )); + app.add_action(&about_action); + + let shortcuts_action = SimpleAction::new("shortcuts", None); + shortcuts_action.connect_activate(clone!( + #[weak] + window, + move |_, _| { + let blueprint = include_blp!("gnome/src/shortcuts.blp"); + let builder = gtk::Builder::from_string(blueprint); + let help_overlay = builder + .object::("help-overlay") + .unwrap(); + help_overlay.set_transient_for(Some(&window)); + help_overlay.set_application(window.application().as_ref()); + help_overlay.present(); + } + )); + app.add_action(&shortcuts_action); + + let quit_action = SimpleAction::new("quit", None); + quit_action.connect_activate(clone!( + #[weak] + window, + move |_, _| { + window.close(); + } + )); + app.add_action(&quit_action); + + app.set_accels_for_action("app.quit", &["q"]); + app.set_accels_for_action("app.shortcuts", &["h"]); + app.set_accels_for_action("win.start-search", &["Return"]); + app.set_accels_for_action("app.stop-search", &["s"]); + + window.present(); +} diff --git a/gnome/src/main.rs b/gnome/src/main.rs index 9b54282..160418f 100644 --- a/gnome/src/main.rs +++ b/gnome/src/main.rs @@ -1,12 +1,9 @@ use adw::prelude::*; -use gtk::gio::SimpleAction; -use gtk::gio::{self, ApplicationFlags}; -use gtk::glib::{self, clone}; -use gtk::{gdk, STYLE_PROVIDER_PRIORITY_APPLICATION}; -use gtk_blueprint::include_blp; +use gtk::gio::ApplicationFlags; use std::path::PathBuf; mod about; +mod app; mod config; mod error_window; mod search; @@ -32,77 +29,13 @@ fn setup_gettext() { fn main() { setup_gettext(); - let app = adw::Application::builder() + let application = adw::Application::builder() .application_id(APP_ID) .flags(ApplicationFlags::HANDLES_OPEN) .build(); - app.connect_open(|app, files, _| start(app, files)); - app.connect_activate(|app| start(app, &[])); + application.connect_open(|a, files, _| app::start(a, files)); + application.connect_activate(|a| app::start(a, &[])); - app.run(); -} - -fn start(app: &adw::Application, files: &[gio::File]) { - let app = app.downcast_ref::().unwrap(); - - let style_provider = gtk::CssProvider::new(); - style_provider.load_from_string(include_str!("styles.css")); - gtk::style_context_add_provider_for_display( - &gdk::Display::default().unwrap(), - &style_provider, - STYLE_PROVIDER_PRIORITY_APPLICATION, - ); - - let window = search_window::SearchWindow::new(app); - - if let Some(dir) = files.first() { - if let Some(path) = dir.path() { - if path.is_dir() { - window.set_search_path(path); - } - } - } - - let about_action = SimpleAction::new("about", None); - about_action.connect_activate(clone!( - #[weak] - window, - move |_, _| about::dialog().present(Some(&window)) - )); - app.add_action(&about_action); - - let shortcuts_action = SimpleAction::new("shortcuts", None); - shortcuts_action.connect_activate(clone!( - #[weak] - window, - move |_, _| { - let blueprint = include_blp!("gnome/src/shortcuts.blp"); - let builder = gtk::Builder::from_string(blueprint); - let help_overlay = builder - .object::("help-overlay") - .unwrap(); - help_overlay.set_transient_for(Some(&window)); - help_overlay.set_application(window.application().as_ref()); - help_overlay.present(); - } - )); - app.add_action(&shortcuts_action); - - let quit_action = SimpleAction::new("quit", None); - quit_action.connect_activate(clone!( - #[weak] - window, - move |_, _| { - window.close(); - } - )); - app.add_action(&quit_action); - - app.set_accels_for_action("app.quit", &["q"]); - app.set_accels_for_action("app.shortcuts", &["h"]); - app.set_accels_for_action("win.start-search", &["Return"]); - app.set_accels_for_action("app.stop-search", &["s"]); - - window.present(); + application.run(); } From ae854b50f16c3a3a815e2f11949d60fad6811391 Mon Sep 17 00:00:00 2001 From: Leopold Luley Date: Sun, 10 Nov 2024 18:28:50 +0100 Subject: [PATCH 7/8] Move code for shortcuts helper into shortcuts.rs file. --- gnome/src/app.rs | 24 ++++++++---------------- gnome/src/main.rs | 1 + gnome/src/shortcuts.rs | 15 +++++++++++++++ 3 files changed, 24 insertions(+), 16 deletions(-) create mode 100644 gnome/src/shortcuts.rs diff --git a/gnome/src/app.rs b/gnome/src/app.rs index 544a6d3..149feb1 100644 --- a/gnome/src/app.rs +++ b/gnome/src/app.rs @@ -1,12 +1,11 @@ +use crate::{about, search_window::SearchWindow, shortcuts}; use adw::prelude::*; -use gtk::gio::SimpleAction; -use gtk::gio::{self}; -use gtk::glib::{self, clone}; -use gtk::{gdk, STYLE_PROVIDER_PRIORITY_APPLICATION}; -use gtk_blueprint::include_blp; - -use crate::about; -use crate::search_window::SearchWindow; +use gtk::{ + gdk, + gio::{self, SimpleAction}, + glib::{self, clone}, + STYLE_PROVIDER_PRIORITY_APPLICATION, +}; pub fn start(app: &adw::Application, files: &[gio::File]) { let app = app.downcast_ref::().unwrap(); @@ -42,14 +41,7 @@ pub fn start(app: &adw::Application, files: &[gio::File]) { #[weak] window, move |_, _| { - let blueprint = include_blp!("gnome/src/shortcuts.blp"); - let builder = gtk::Builder::from_string(blueprint); - let help_overlay = builder - .object::("help-overlay") - .unwrap(); - help_overlay.set_transient_for(Some(&window)); - help_overlay.set_application(window.application().as_ref()); - help_overlay.present(); + shortcuts::show_shortcuts(&window); } )); app.add_action(&shortcuts_action); diff --git a/gnome/src/main.rs b/gnome/src/main.rs index 160418f..3402038 100644 --- a/gnome/src/main.rs +++ b/gnome/src/main.rs @@ -8,6 +8,7 @@ mod config; mod error_window; mod search; mod search_window; +mod shortcuts; const APP_ID: &str = env!("APP_ID"); diff --git a/gnome/src/shortcuts.rs b/gnome/src/shortcuts.rs new file mode 100644 index 0000000..72491cb --- /dev/null +++ b/gnome/src/shortcuts.rs @@ -0,0 +1,15 @@ +use gtk::prelude::*; +use gtk_blueprint::include_blp; + +use crate::search_window::SearchWindow; + +pub fn show_shortcuts(window: &SearchWindow) { + let blueprint = include_blp!("gnome/src/shortcuts.blp"); + let builder = gtk::Builder::from_string(blueprint); + let help_overlay = builder + .object::("help-overlay") + .unwrap(); + help_overlay.set_transient_for(Some(window)); + help_overlay.set_application(window.application().as_ref()); + help_overlay.present(); +} From 75336ebe98201345ec9ef53e0b1df54062dec881 Mon Sep 17 00:00:00 2001 From: Leopold Luley Date: Sun, 10 Nov 2024 18:46:18 +0100 Subject: [PATCH 8/8] Move ui related code into ui module. --- gnome/src/app.rs | 8 ++++---- gnome/src/main.rs | 5 +---- gnome/src/{ => ui}/about.rs | 2 +- gnome/src/{ => ui}/error_window/error_window.blp | 0 gnome/src/{ => ui}/error_window/imp.rs | 4 ++-- gnome/src/{ => ui}/error_window/mod.rs | 2 +- gnome/src/ui/mod.rs | 11 +++++++++++ gnome/src/{ => ui}/search_window/imp.rs | 4 ++-- gnome/src/{ => ui}/search_window/mod.rs | 0 gnome/src/{ => ui}/search_window/search_window.blp | 0 gnome/src/{shortcuts.rs => ui/shortcuts/mod.rs} | 4 ++-- gnome/src/{ => ui/shortcuts}/shortcuts.blp | 0 12 files changed, 24 insertions(+), 16 deletions(-) rename gnome/src/{ => ui}/about.rs (94%) rename gnome/src/{ => ui}/error_window/error_window.blp (100%) rename gnome/src/{ => ui}/error_window/imp.rs (91%) rename gnome/src/{ => ui}/error_window/mod.rs (94%) create mode 100644 gnome/src/ui/mod.rs rename gnome/src/{ => ui}/search_window/imp.rs (98%) rename gnome/src/{ => ui}/search_window/mod.rs (100%) rename gnome/src/{ => ui}/search_window/search_window.blp (100%) rename gnome/src/{shortcuts.rs => ui/shortcuts/mod.rs} (79%) rename gnome/src/{ => ui/shortcuts}/shortcuts.blp (100%) diff --git a/gnome/src/app.rs b/gnome/src/app.rs index 149feb1..4421882 100644 --- a/gnome/src/app.rs +++ b/gnome/src/app.rs @@ -1,4 +1,4 @@ -use crate::{about, search_window::SearchWindow, shortcuts}; +use crate::ui; use adw::prelude::*; use gtk::{ gdk, @@ -18,7 +18,7 @@ pub fn start(app: &adw::Application, files: &[gio::File]) { STYLE_PROVIDER_PRIORITY_APPLICATION, ); - let window = SearchWindow::new(app); + let window = ui::SearchWindow::new(app); if let Some(dir) = files.first() { if let Some(path) = dir.path() { @@ -32,7 +32,7 @@ pub fn start(app: &adw::Application, files: &[gio::File]) { about_action.connect_activate(clone!( #[weak] window, - move |_, _| about::dialog().present(Some(&window)) + move |_, _| ui::about_dialog().present(Some(&window)) )); app.add_action(&about_action); @@ -41,7 +41,7 @@ pub fn start(app: &adw::Application, files: &[gio::File]) { #[weak] window, move |_, _| { - shortcuts::show_shortcuts(&window); + ui::show_shortcuts(&window); } )); app.add_action(&shortcuts_action); diff --git a/gnome/src/main.rs b/gnome/src/main.rs index 3402038..c191911 100644 --- a/gnome/src/main.rs +++ b/gnome/src/main.rs @@ -2,13 +2,10 @@ use adw::prelude::*; use gtk::gio::ApplicationFlags; use std::path::PathBuf; -mod about; mod app; mod config; -mod error_window; mod search; -mod search_window; -mod shortcuts; +mod ui; const APP_ID: &str = env!("APP_ID"); diff --git a/gnome/src/about.rs b/gnome/src/ui/about.rs similarity index 94% rename from gnome/src/about.rs rename to gnome/src/ui/about.rs index d32fc0b..36e2a9f 100644 --- a/gnome/src/about.rs +++ b/gnome/src/ui/about.rs @@ -8,7 +8,7 @@ static RELEASE_NOTES: &str = r#" "#; -pub fn dialog() -> adw::AboutDialog { +pub fn about_dialog() -> adw::AboutDialog { adw::AboutDialog::builder() .application_name("Clapgrep") .version("1.2") diff --git a/gnome/src/error_window/error_window.blp b/gnome/src/ui/error_window/error_window.blp similarity index 100% rename from gnome/src/error_window/error_window.blp rename to gnome/src/ui/error_window/error_window.blp diff --git a/gnome/src/error_window/imp.rs b/gnome/src/ui/error_window/imp.rs similarity index 91% rename from gnome/src/error_window/imp.rs rename to gnome/src/ui/error_window/imp.rs index 469ece4..2bb2aa8 100644 --- a/gnome/src/error_window/imp.rs +++ b/gnome/src/ui/error_window/imp.rs @@ -4,10 +4,10 @@ use glib::subclass::InitializingObject; use gtk::{glib, CompositeTemplate}; use std::cell::RefCell; -use crate::search_window::SearchWindow; +use crate::ui::SearchWindow; #[derive(CompositeTemplate, glib::Properties, Default)] -#[template(file = "src/error_window/error_window.blp")] +#[template(file = "src/ui/error_window/error_window.blp")] #[properties(wrapper_type = super::ErrorWindow)] pub struct ErrorWindow { #[property(get, set)] diff --git a/gnome/src/error_window/mod.rs b/gnome/src/ui/error_window/mod.rs similarity index 94% rename from gnome/src/error_window/mod.rs rename to gnome/src/ui/error_window/mod.rs index 8661392..93fcde0 100644 --- a/gnome/src/error_window/mod.rs +++ b/gnome/src/ui/error_window/mod.rs @@ -3,7 +3,7 @@ mod imp; use glib::Object; use gtk::{gio, glib, prelude::*}; -use crate::search_window::SearchWindow; +use crate::ui::SearchWindow; glib::wrapper! { pub struct ErrorWindow(ObjectSubclass) diff --git a/gnome/src/ui/mod.rs b/gnome/src/ui/mod.rs new file mode 100644 index 0000000..d8d84f7 --- /dev/null +++ b/gnome/src/ui/mod.rs @@ -0,0 +1,11 @@ +mod about; +pub use about::about_dialog; + +mod shortcuts; +pub use shortcuts::show_shortcuts; + +mod error_window; +pub use error_window::ErrorWindow; + +mod search_window; +pub use search_window::SearchWindow; diff --git a/gnome/src/search_window/imp.rs b/gnome/src/ui/search_window/imp.rs similarity index 98% rename from gnome/src/search_window/imp.rs rename to gnome/src/ui/search_window/imp.rs index 688c197..7a529e9 100644 --- a/gnome/src/search_window/imp.rs +++ b/gnome/src/ui/search_window/imp.rs @@ -1,4 +1,4 @@ -use crate::{config::Config, error_window::ErrorWindow, search::SearchModel}; +use crate::{config::Config, search::SearchModel, ui::ErrorWindow}; use adw::subclass::prelude::*; use clapgrep_core::{SearchEngine, SearchFlags, SearchMessage, SearchParameters}; use glib::subclass::InitializingObject; @@ -15,7 +15,7 @@ use std::{ }; #[derive(CompositeTemplate, glib::Properties, Default)] -#[template(file = "src/search_window/search_window.blp")] +#[template(file = "src/ui/search_window/search_window.blp")] #[properties(wrapper_type = super::SearchWindow)] pub struct SearchWindow { #[property(get, set)] diff --git a/gnome/src/search_window/mod.rs b/gnome/src/ui/search_window/mod.rs similarity index 100% rename from gnome/src/search_window/mod.rs rename to gnome/src/ui/search_window/mod.rs diff --git a/gnome/src/search_window/search_window.blp b/gnome/src/ui/search_window/search_window.blp similarity index 100% rename from gnome/src/search_window/search_window.blp rename to gnome/src/ui/search_window/search_window.blp diff --git a/gnome/src/shortcuts.rs b/gnome/src/ui/shortcuts/mod.rs similarity index 79% rename from gnome/src/shortcuts.rs rename to gnome/src/ui/shortcuts/mod.rs index 72491cb..1a185cb 100644 --- a/gnome/src/shortcuts.rs +++ b/gnome/src/ui/shortcuts/mod.rs @@ -1,10 +1,10 @@ use gtk::prelude::*; use gtk_blueprint::include_blp; -use crate::search_window::SearchWindow; +use crate::ui::SearchWindow; pub fn show_shortcuts(window: &SearchWindow) { - let blueprint = include_blp!("gnome/src/shortcuts.blp"); + let blueprint = include_blp!("gnome/src/ui/shortcuts/shortcuts.blp"); let builder = gtk::Builder::from_string(blueprint); let help_overlay = builder .object::("help-overlay") diff --git a/gnome/src/shortcuts.blp b/gnome/src/ui/shortcuts/shortcuts.blp similarity index 100% rename from gnome/src/shortcuts.blp rename to gnome/src/ui/shortcuts/shortcuts.blp