connect actions directly, use trait implementation for navigation buttons

This commit is contained in:
yggverse 2025-01-23 23:17:48 +02:00
parent 10c73f4e3d
commit 5145a53bfa
9 changed files with 87 additions and 266 deletions

View file

@ -7,6 +7,7 @@ mod request;
mod widget; mod widget;
use bookmark::Bookmark; use bookmark::Bookmark;
use gtk::Button;
use history::History; use history::History;
use home::Home; use home::Home;
use reload::Reload; use reload::Reload;
@ -18,11 +19,8 @@ use sqlite::Transaction;
use std::rc::Rc; use std::rc::Rc;
pub struct Navigation { pub struct Navigation {
pub bookmark: Rc<Bookmark>,
pub history: Rc<History>, pub history: Rc<History>,
pub home: Rc<Home>,
pub profile: Rc<Profile>, pub profile: Rc<Profile>,
pub reload: Rc<Reload>,
pub request: Rc<Request>, pub request: Rc<Request>,
pub widget: Rc<Widget>, pub widget: Rc<Widget>,
} }
@ -39,27 +37,21 @@ impl Navigation {
// init children components // init children components
let history = Rc::new(History::build(window_action)); let history = Rc::new(History::build(window_action));
let reload = Rc::new(Reload::build(window_action));
let request = Rc::new(Request::build((browser_action, tab_action))); let request = Rc::new(Request::build((browser_action, tab_action)));
let bookmark = Rc::new(Bookmark::build(window_action));
let home = Rc::new(Home::build(window_action));
// init main widget // init main widget
let widget = Rc::new(Widget::build( let widget = Rc::new(Widget::build(
&home.button, &Button::home(window_action),
&history.widget.g_box, &history.widget.g_box, // @TODO
&reload.widget.button, &Button::reload(window_action),
&request.widget.entry, &request.widget.entry, // @TODO
&bookmark.widget.button, &Button::bookmark(window_action),
)); ));
// done // done
Self { Self {
bookmark,
history, history,
home,
profile: profile.clone(), profile: profile.clone(),
reload,
request, request,
widget, widget,
} }
@ -68,6 +60,7 @@ impl Navigation {
// Actions // Actions
pub fn update(&self) { pub fn update(&self) {
/* @TODO
// init shared request value // init shared request value
let request = self.request.strip_prefix(); let request = self.request.strip_prefix();
@ -86,7 +79,7 @@ impl Navigation {
self.request self.request
.home() .home()
.is_some_and(|home| home.to_string() != request), .is_some_and(|home| home.to_string() != request),
); );*/
} }
pub fn clean( pub fn clean(

View file

@ -1,26 +1,32 @@
mod widget; use super::WindowAction;
use gtk::{
use widget::Widget; prelude::{ActionExt, ButtonExt},
Button,
use crate::app::browser::window::action::Action as WindowAction; };
use std::rc::Rc; use std::rc::Rc;
pub struct Bookmark { const ICON_YES: &str = "starred-symbolic";
pub widget: Rc<Widget>, const ICON_NON: &str = "non-starred-symbolic";
pub trait Bookmark {
fn bookmark(action: &Rc<WindowAction>) -> Self;
fn _update(&self, has_bookmark: bool); // @TODO
} }
impl Bookmark { impl Bookmark for Button {
// Constructors fn bookmark(action: &Rc<WindowAction>) -> Self {
Button::builder()
/// Build new `Self` .action_name(format!(
pub fn build(action: &Rc<WindowAction>) -> Self { "{}.{}",
Self { action.id,
widget: Rc::new(Widget::build(action)), action.bookmark.simple_action.name()
} )) // @TODO
.icon_name(ICON_NON)
.tooltip_text("Bookmark")
.build()
} }
// Actions fn _update(&self, has_bookmark: bool) {
pub fn update(&self, has_bookmark: bool) { self.set_icon_name(if has_bookmark { ICON_YES } else { ICON_NON });
self.widget.update(has_bookmark);
} }
} }

View file

@ -1,40 +0,0 @@
use gtk::{prelude::ButtonExt, Button};
use super::WindowAction;
use std::rc::Rc;
const ICON_YES: &str = "starred-symbolic";
const ICON_NON: &str = "non-starred-symbolic";
pub struct Widget {
pub button: Button,
}
impl Widget {
// Constructors
/// Build new `Self`
pub fn build(action: &Rc<WindowAction>) -> Self {
// Init gobject
let button = Button::builder()
.icon_name(ICON_NON)
.tooltip_text("Bookmark")
.build();
// Init events
button.connect_clicked({
let action = action.clone();
move |_| action.bookmark.activate()
});
// Return activated `Self`
Self { button }
}
// Actions
pub fn update(&self, has_bookmark: bool) {
self.button
.set_icon_name(if has_bookmark { ICON_YES } else { ICON_NON });
}
}

View file

@ -7,7 +7,7 @@ use forward::Forward;
use widget::Widget; use widget::Widget;
use super::WindowAction; use super::WindowAction;
use gtk::glib::GString; use gtk::{glib::GString, Button};
use std::{cell::RefCell, rc::Rc}; use std::{cell::RefCell, rc::Rc};
struct Memory { struct Memory {
@ -16,9 +16,6 @@ struct Memory {
} }
pub struct History { pub struct History {
// Components
back: Rc<Back>,
forward: Rc<Forward>,
// Extras // Extras
memory: RefCell<Vec<Memory>>, memory: RefCell<Vec<Memory>>,
index: RefCell<Option<usize>>, index: RefCell<Option<usize>>,
@ -31,12 +28,11 @@ impl History {
/// Build new `Self` /// Build new `Self`
pub fn build(action: &Rc<WindowAction>) -> Self { pub fn build(action: &Rc<WindowAction>) -> Self {
// init components
let back = Rc::new(Back::build(action));
let forward = Rc::new(Forward::build(action));
// Init widget // Init widget
let widget = Rc::new(Widget::build(&back.button, &forward.button)); let widget = Rc::new(Widget::build(
&Button::back(action),
&Button::forward(action),
));
// Init memory // Init memory
let memory = RefCell::new(Vec::new()); let memory = RefCell::new(Vec::new());
@ -45,8 +41,6 @@ impl History {
let index = RefCell::new(None); let index = RefCell::new(None);
Self { Self {
back,
forward,
memory, memory,
index, index,
widget, widget,
@ -110,16 +104,4 @@ impl History {
} }
None None
} }
pub fn update(&self) {
match self.back(false) {
Some(_) => self.back.update(true),
None => self.back.update(false),
};
match self.forward(false) {
Some(_) => self.forward.update(true),
None => self.forward.update(false),
};
}
} }

View file

@ -1,44 +1,21 @@
use super::WindowAction; use super::WindowAction;
use gtk::{ use gtk::{prelude::ActionExt, Button};
prelude::{ButtonExt, WidgetExt},
Button,
};
use std::rc::Rc; use std::rc::Rc;
pub struct Back { pub trait Back {
action: Rc<WindowAction>, fn back(action: &Rc<WindowAction>) -> Self;
pub button: Button,
} }
impl Back { impl Back for Button {
// Constructors fn back(action: &Rc<WindowAction>) -> Self {
Button::builder()
/// Build new `Self` .action_name(format!(
pub fn build(action: &Rc<WindowAction>) -> Self { "{}.{}",
// Init gobject action.id,
let button = Button::builder() action.history_back.simple_action.name()
)) // @TODO
.icon_name("go-previous-symbolic") .icon_name("go-previous-symbolic")
.tooltip_text("Back") .tooltip_text("Back")
.sensitive(false) .build()
.build();
// Init events
button.connect_clicked({
let action = action.clone();
move |_| action.history_back.activate()
});
// Return activated `Self`
Self {
action: action.clone(),
button,
}
}
// Actions
pub fn update(&self, status: bool) {
self.action.history_back.simple_action.set_enabled(status);
self.button.set_sensitive(status);
} }
} }

View file

@ -1,48 +1,21 @@
use gtk::{
prelude::{ButtonExt, WidgetExt},
Button,
};
use super::WindowAction; use super::WindowAction;
use gtk::{prelude::ActionExt, Button};
use std::rc::Rc; use std::rc::Rc;
pub struct Forward { pub trait Forward {
action: Rc<WindowAction>, fn forward(action: &Rc<WindowAction>) -> Self;
pub button: Button,
} }
impl Forward { impl Forward for Button {
// Constructors fn forward(action: &Rc<WindowAction>) -> Self {
Button::builder()
/// Build new `Self` .action_name(format!(
pub fn build(action: &Rc<WindowAction>) -> Self { "{}.{}",
// Init gobject action.id,
let button = Button::builder() action.history_back.simple_action.name()
)) // @TODO
.icon_name("go-next-symbolic") .icon_name("go-next-symbolic")
.tooltip_text("Forward") .tooltip_text("Forward")
.sensitive(false) .build()
.build();
// Init events
button.connect_clicked({
let action = action.clone();
move |_| action.history_forward.activate()
});
// Return activated `Self`
Self {
action: action.clone(),
button,
}
}
// Actions
pub fn update(&self, status: bool) {
self.action
.history_forward
.simple_action
.set_enabled(status);
self.button.set_sensitive(status);
} }
} }

View file

@ -1,41 +1,21 @@
use super::WindowAction; use super::WindowAction;
use gtk::{ use gtk::{prelude::ActionExt, Button};
prelude::{ButtonExt, WidgetExt},
Button,
};
use std::rc::Rc; use std::rc::Rc;
pub struct Home { pub trait Home {
action: Rc<WindowAction>, fn home(action: &Rc<WindowAction>) -> Self;
pub button: Button,
} }
impl Home { impl Home for Button {
// Construct fn home(action: &Rc<WindowAction>) -> Self {
pub fn build(action: &Rc<WindowAction>) -> Self { Button::builder()
// Init gobject .action_name(format!(
let button = Button::builder() "{}.{}",
action.id,
action.home.simple_action.name()
)) // @TODO
.icon_name("go-home-symbolic") .icon_name("go-home-symbolic")
.tooltip_text("Home") .tooltip_text("Home")
.sensitive(false) .build()
.build();
// Init events
button.connect_clicked({
let action = action.clone();
move |_| action.home.activate()
});
// Return activated `Self`
Self {
action: action.clone(),
button,
}
}
// Actions
pub fn update(&self, has_home: bool) {
self.action.home.simple_action.set_enabled(has_home);
self.button.set_sensitive(has_home);
} }
} }

View file

@ -1,33 +1,21 @@
mod widget;
use widget::Widget;
use super::WindowAction; use super::WindowAction;
use gtk::{prelude::ActionExt, Button};
use std::rc::Rc; use std::rc::Rc;
pub struct Reload { pub trait Reload {
action: Rc<WindowAction>, fn reload(action: &Rc<WindowAction>) -> Self;
pub widget: Rc<Widget>,
} }
impl Reload { impl Reload for Button {
// Constructors fn reload(action: &Rc<WindowAction>) -> Self {
Button::builder()
/// Build new `Self` .action_name(format!(
pub fn build(action: &Rc<WindowAction>) -> Self { "{}.{}",
Self { action.id,
action: action.clone(), action.reload.simple_action.name()
widget: Rc::new(Widget::build(action)), )) // @TODO
} .icon_name("view-refresh-symbolic")
} .tooltip_text("Reload")
.build()
// Actions
pub fn update(&self, is_enabled: bool) {
// Update actions
self.action.reload.simple_action.set_enabled(is_enabled);
// Update child components
self.widget.update(is_enabled);
} }
} }

View file

@ -1,38 +0,0 @@
use super::WindowAction;
use gtk::{
prelude::{ButtonExt, WidgetExt},
Button,
};
use std::rc::Rc;
pub struct Widget {
pub button: Button,
}
impl Widget {
// Constructors
/// Build new `Self`
pub fn build(action: &Rc<WindowAction>) -> Self {
// Init gobject
let button = Button::builder()
.icon_name("view-refresh-symbolic")
.tooltip_text("Reload")
.sensitive(false)
.build();
// Init events
button.connect_clicked({
let action = action.clone();
move |_| action.reload.activate()
});
// Return activated `Self`
Self { button }
}
// Actions
pub fn update(&self, is_sensitive: bool) {
self.button.set_sensitive(is_sensitive);
}
}