From fc2baf7845d73c3e6e0d80b6aa689e36eaa51fee Mon Sep 17 00:00:00 2001 From: yggverse Date: Thu, 14 Nov 2024 16:15:13 +0200 Subject: [PATCH] draft auth action --- src/app/browser/window/tab/item.rs | 4 ++ src/app/browser/window/tab/item/action.rs | 11 ++++- .../browser/window/tab/item/action/auth.rs | 43 +++++++++++++++++++ .../window/tab/item/page/navigation.rs | 2 +- .../window/tab/item/page/navigation/auth.rs | 3 +- .../tab/item/page/navigation/auth/widget.rs | 4 +- 6 files changed, 61 insertions(+), 6 deletions(-) create mode 100644 src/app/browser/window/tab/item/action/auth.rs diff --git a/src/app/browser/window/tab/item.rs b/src/app/browser/window/tab/item.rs index de2945a8..b51c2ea8 100644 --- a/src/app/browser/window/tab/item.rs +++ b/src/app/browser/window/tab/item.rs @@ -79,6 +79,10 @@ impl Item { } } + action.auth().connect_activate(|request| { + // @TODO + }); + action.load().connect_activate({ let page = page.clone(); move |request, is_history| { diff --git a/src/app/browser/window/tab/item/action.rs b/src/app/browser/window/tab/item/action.rs index cf276d93..7d418357 100644 --- a/src/app/browser/window/tab/item/action.rs +++ b/src/app/browser/window/tab/item/action.rs @@ -1,11 +1,14 @@ +mod auth; mod load; + +use auth::Auth; use load::Load; use std::rc::Rc; /// [SimpleActionGroup](https://docs.gtk.org/gio/class.SimpleActionGroup.html) wrapper for `Browser` actions pub struct Action { - // Actions + auth: Rc, load: Rc, } @@ -15,12 +18,18 @@ impl Action { /// Create new `Self` pub fn new() -> Self { Self { + auth: Rc::new(Auth::new()), load: Rc::new(Load::new()), } } // Getters + /// Get reference to `Auth` action + pub fn auth(&self) -> &Rc { + &self.auth + } + /// Get reference to `Load` action pub fn load(&self) -> &Rc { &self.load diff --git a/src/app/browser/window/tab/item/action/auth.rs b/src/app/browser/window/tab/item/action/auth.rs new file mode 100644 index 00000000..ebca6b2c --- /dev/null +++ b/src/app/browser/window/tab/item/action/auth.rs @@ -0,0 +1,43 @@ +use gtk::{ + gio::SimpleAction, + glib::uuid_string_random, + prelude::{ActionExt, StaticVariantType, ToVariant}, +}; + +/// [SimpleAction](https://docs.gtk.org/gio/class.SimpleAction.html) wrapper for `Load` action of `Item` group +pub struct Auth { + gobject: SimpleAction, +} + +impl Auth { + // Constructors + + /// Create new `Self` + pub fn new() -> Self { + Self { + gobject: SimpleAction::new(&uuid_string_random(), Some(&String::static_variant_type())), + } + } + + // Actions + + /// Emit [activate](https://docs.gtk.org/gio/signal.SimpleAction.activate.html) signal + /// with formatted for this action [Variant](https://docs.gtk.org/glib/struct.Variant.html) value + pub fn activate(&self, request: &str) { + self.gobject.activate(Some(&request.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(String) + 'static) { + self.gobject.connect_activate(move |_, this| { + callback( + this.expect("Expected variant value") + .get::() + .expect("Parameter type does not match `String` type"), + ) + }); + } +} diff --git a/src/app/browser/window/tab/item/page/navigation.rs b/src/app/browser/window/tab/item/page/navigation.rs index a36792ee..210d0a82 100644 --- a/src/app/browser/window/tab/item/page/navigation.rs +++ b/src/app/browser/window/tab/item/page/navigation.rs @@ -40,7 +40,7 @@ impl Navigation { action: (Rc, Rc, Rc), ) -> Self { // Init components - let auth = Rc::new(Auth::new(action.1.clone())); + let auth = Rc::new(Auth::new(action.2.clone())); let home = Rc::new(Home::new(action.1.clone())); let history = Rc::new(History::new(action.1.clone())); let reload = Rc::new(Reload::new(action.1.clone())); diff --git a/src/app/browser/window/tab/item/page/navigation/auth.rs b/src/app/browser/window/tab/item/page/navigation/auth.rs index 6b8f5505..2d84fd37 100644 --- a/src/app/browser/window/tab/item/page/navigation/auth.rs +++ b/src/app/browser/window/tab/item/page/navigation/auth.rs @@ -1,8 +1,7 @@ mod widget; - use widget::Widget; -use crate::app::browser::window::Action; +use crate::app::browser::window::tab::item::Action; use std::rc::Rc; pub struct Auth { diff --git a/src/app/browser/window/tab/item/page/navigation/auth/widget.rs b/src/app/browser/window/tab/item/page/navigation/auth/widget.rs index 3df630b5..225ea59b 100644 --- a/src/app/browser/window/tab/item/page/navigation/auth/widget.rs +++ b/src/app/browser/window/tab/item/page/navigation/auth/widget.rs @@ -1,4 +1,4 @@ -use crate::app::browser::window::Action; +use crate::app::browser::window::tab::item::Action; use gtk::{ prelude::{ButtonExt, WidgetExt}, Button, @@ -19,7 +19,7 @@ impl Widget { .sensitive(false) .build(); - // Init events @TODO + // Init events @TODO dialog window required // gobject.connect_clicked(move |_| action.auth().activate()); // Return activated `Self`