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

View file

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

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 super::WindowAction;
use gtk::glib::GString;
use gtk::{glib::GString, Button};
use std::{cell::RefCell, rc::Rc};
struct Memory {
@ -16,9 +16,6 @@ struct Memory {
}
pub struct History {
// Components
back: Rc<Back>,
forward: Rc<Forward>,
// Extras
memory: RefCell<Vec<Memory>>,
index: RefCell<Option<usize>>,
@ -31,12 +28,11 @@ impl History {
/// Build new `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
let widget = Rc::new(Widget::build(&back.button, &forward.button));
let widget = Rc::new(Widget::build(
&Button::back(action),
&Button::forward(action),
));
// Init memory
let memory = RefCell::new(Vec::new());
@ -45,8 +41,6 @@ impl History {
let index = RefCell::new(None);
Self {
back,
forward,
memory,
index,
widget,
@ -110,16 +104,4 @@ impl History {
}
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 gtk::{
prelude::{ButtonExt, WidgetExt},
Button,
};
use gtk::{prelude::ActionExt, Button};
use std::rc::Rc;
pub struct Back {
action: Rc<WindowAction>,
pub button: Button,
pub trait Back {
fn back(action: &Rc<WindowAction>) -> Self;
}
impl Back {
// Constructors
/// Build new `Self`
pub fn build(action: &Rc<WindowAction>) -> Self {
// Init gobject
let button = Button::builder()
impl Back for Button {
fn back(action: &Rc<WindowAction>) -> Self {
Button::builder()
.action_name(format!(
"{}.{}",
action.id,
action.history_back.simple_action.name()
)) // @TODO
.icon_name("go-previous-symbolic")
.tooltip_text("Back")
.sensitive(false)
.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);
.build()
}
}

View file

@ -1,48 +1,21 @@
use gtk::{
prelude::{ButtonExt, WidgetExt},
Button,
};
use super::WindowAction;
use gtk::{prelude::ActionExt, Button};
use std::rc::Rc;
pub struct Forward {
action: Rc<WindowAction>,
pub button: Button,
pub trait Forward {
fn forward(action: &Rc<WindowAction>) -> Self;
}
impl Forward {
// Constructors
/// Build new `Self`
pub fn build(action: &Rc<WindowAction>) -> Self {
// Init gobject
let button = Button::builder()
impl Forward for Button {
fn forward(action: &Rc<WindowAction>) -> Self {
Button::builder()
.action_name(format!(
"{}.{}",
action.id,
action.history_back.simple_action.name()
)) // @TODO
.icon_name("go-next-symbolic")
.tooltip_text("Forward")
.sensitive(false)
.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);
.build()
}
}

View file

@ -1,41 +1,21 @@
use super::WindowAction;
use gtk::{
prelude::{ButtonExt, WidgetExt},
Button,
};
use gtk::{prelude::ActionExt, Button};
use std::rc::Rc;
pub struct Home {
action: Rc<WindowAction>,
pub button: Button,
pub trait Home {
fn home(action: &Rc<WindowAction>) -> Self;
}
impl Home {
// Construct
pub fn build(action: &Rc<WindowAction>) -> Self {
// Init gobject
let button = Button::builder()
impl Home for Button {
fn home(action: &Rc<WindowAction>) -> Self {
Button::builder()
.action_name(format!(
"{}.{}",
action.id,
action.home.simple_action.name()
)) // @TODO
.icon_name("go-home-symbolic")
.tooltip_text("Home")
.sensitive(false)
.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);
.build()
}
}

View file

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

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);
}
}