diff --git a/src/app.rs b/src/app.rs index 088ff691..14d1f461 100644 --- a/src/app.rs +++ b/src/app.rs @@ -173,6 +173,14 @@ impl App { ), &["b"], ), + ( + format!( + "{}.{}", + browser.window.action.id, + browser.window.action.find.simple_action.name() + ), + &["f"], + ), ( format!( "{}.{}", diff --git a/src/app/browser/window/action.rs b/src/app/browser/window/action.rs index 844f008c..2cd45a7b 100644 --- a/src/app/browser/window/action.rs +++ b/src/app/browser/window/action.rs @@ -2,6 +2,7 @@ mod append; mod bookmark; mod close; mod close_all; +mod find; mod history_back; mod history_forward; mod home; @@ -14,6 +15,7 @@ use append::Append; use bookmark::Bookmark; use close::Close; use close_all::CloseAll; +use find::Find; use history_back::HistoryBack; use history_forward::HistoryForward; use home::Home; @@ -38,6 +40,7 @@ pub struct Action { pub bookmark: Rc, pub close_all: Rc, pub close: Rc, + pub find: Rc, pub history_back: Rc, pub history_forward: Rc, pub home: Rc, @@ -60,6 +63,7 @@ impl Action { let bookmark = Rc::new(Bookmark::new()); let close = Rc::new(Close::new()); let close_all = Rc::new(CloseAll::new()); + let find = Rc::new(Find::new()); let history_back = Rc::new(HistoryBack::new()); let history_forward = Rc::new(HistoryForward::new()); let home = Rc::new(Home::new()); @@ -79,6 +83,7 @@ impl Action { simple_action_group.add_action(&bookmark.simple_action); simple_action_group.add_action(&close_all.simple_action); simple_action_group.add_action(&close.simple_action); + simple_action_group.add_action(&find.simple_action); simple_action_group.add_action(&history_back.simple_action); simple_action_group.add_action(&history_forward.simple_action); simple_action_group.add_action(&home.simple_action); @@ -93,6 +98,7 @@ impl Action { bookmark, close_all, close, + find, history_back, history_forward, home, diff --git a/src/app/browser/window/action/find.rs b/src/app/browser/window/action/find.rs new file mode 100644 index 00000000..15391959 --- /dev/null +++ b/src/app/browser/window/action/find.rs @@ -0,0 +1,64 @@ +use gtk::{ + gio::SimpleAction, + glib::uuid_string_random, + prelude::{ActionExt, ToVariant}, +}; + +// Defaults + +/// C-compatible variant type +const DEFAULT_STATE: i32 = -1; + +/// [SimpleAction](https://docs.gtk.org/gio/class.SimpleAction.html) wrapper for `Find` action of `Window` group +pub struct Find { + pub simple_action: SimpleAction, +} + +impl Find { + // Constructors + + /// Create new `Self` + pub fn new() -> Self { + Self { + simple_action: SimpleAction::new_stateful( + &uuid_string_random(), + None, + &DEFAULT_STATE.to_variant(), + ), + } + } + + // Actions + + /// Change action [state](https://docs.gtk.org/gio/method.SimpleAction.set_state.html) + /// * set `DEFAULT_STATE` on `None` + pub fn change_state(&self, state: Option) { + self.simple_action.change_state( + &match state { + Some(value) => value, + None => DEFAULT_STATE, + } + .to_variant(), + ) + } + + // Events + + /// Define callback function for + /// [SimpleAction::activate](https://docs.gtk.org/gio/signal.SimpleAction.activate.html) signal + pub fn connect_activate(&self, callback: impl Fn(Option) + 'static) { + self.simple_action.connect_activate(move |this, _| { + let state = this + .state() + .expect("State value required") + .get::() + .expect("Parameter type does not match `i32`"); + + callback(if state == DEFAULT_STATE { + None + } else { + Some(state) + }) + }); + } +} diff --git a/src/app/browser/window/header/bar/menu.rs b/src/app/browser/window/header/bar/menu.rs index 8ff2269b..d3836be1 100644 --- a/src/app/browser/window/header/bar/menu.rs +++ b/src/app/browser/window/header/bar/menu.rs @@ -36,6 +36,12 @@ impl Menu { window_action.reload.simple_action.name() ))); + main_page.append(Some("Find.."), Some(&format!( + "{}.{}", + window_action.id, + window_action.find.simple_action.name() + ))); + main_page.append(Some("Save as.."), Some(&format!( "{}.{}", window_action.id, diff --git a/src/app/browser/window/tab.rs b/src/app/browser/window/tab.rs index 28330fff..ad33550e 100644 --- a/src/app/browser/window/tab.rs +++ b/src/app/browser/window/tab.rs @@ -75,6 +75,7 @@ impl Tab { action.bookmark.change_state(state); action.close_all.change_state(state); action.close.change_state(state); + action.find.change_state(state); action.history_back.change_state(state); action.history_forward.change_state(state); action.home.change_state(state); diff --git a/src/app/browser/window/tab/menu.rs b/src/app/browser/window/tab/menu.rs index 4a134560..f1238499 100644 --- a/src/app/browser/window/tab/menu.rs +++ b/src/app/browser/window/tab/menu.rs @@ -25,6 +25,15 @@ impl Menu { )), ); + main.append( + Some("Find.."), + Some(&format!( + "{}.{}", + window_action.id, + window_action.find.simple_action.name() + )), + ); + main.append( Some("Save as.."), Some(&format!(