Skip to content

Commit

Permalink
connect actions directly, use trait implementation for navigation but…
Browse files Browse the repository at this point in the history
…tons
  • Loading branch information
yggverse committed Jan 23, 2025
1 parent 10c73f4 commit 5145a53
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 266 deletions.
23 changes: 8 additions & 15 deletions src/app/browser/window/tab/item/page/navigation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ mod request;
mod widget;

use bookmark::Bookmark;
use gtk::Button;
use history::History;
use home::Home;
use reload::Reload;
Expand All @@ -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>,
}
Expand All @@ -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,
}
Expand All @@ -68,6 +60,7 @@ impl Navigation {
// Actions

pub fn update(&self) {
/* @TODO
// init shared request value
let request = self.request.strip_prefix();
Expand All @@ -86,7 +79,7 @@ impl Navigation {
self.request
.home()
.is_some_and(|home| home.to_string() != request),
);
);*/
}

pub fn clean(
Expand Down
42 changes: 24 additions & 18 deletions src/app/browser/window/tab/item/page/navigation/bookmark.rs
Original file line number Diff line number Diff line change
@@ -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";

impl Bookmark {
// Constructors
pub trait Bookmark {
fn bookmark(action: &Rc<WindowAction>) -> Self;
fn _update(&self, has_bookmark: bool); // @TODO
}

/// 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 });
}
}
40 changes: 0 additions & 40 deletions src/app/browser/window/tab/item/page/navigation/bookmark/widget.rs

This file was deleted.

28 changes: 5 additions & 23 deletions src/app/browser/window/tab/item/page/navigation/history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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>>,
Expand All @@ -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());
Expand All @@ -45,8 +41,6 @@ impl History {
let index = RefCell::new(None);

Self {
back,
forward,
memory,
index,
widget,
Expand Down Expand Up @@ -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),
};
}
}
47 changes: 12 additions & 35 deletions src/app/browser/window/tab/item/page/navigation/history/back.rs
Original file line number Diff line number Diff line change
@@ -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()
}
}
51 changes: 12 additions & 39 deletions src/app/browser/window/tab/item/page/navigation/history/forward.rs
Original file line number Diff line number Diff line change
@@ -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()
}
}
44 changes: 12 additions & 32 deletions src/app/browser/window/tab/item/page/navigation/home.rs
Original file line number Diff line number Diff line change
@@ -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()
}
}
Loading

0 comments on commit 5145a53

Please sign in to comment.