implement recent bookmarks menu item

This commit is contained in:
yggverse 2025-01-12 00:38:22 +02:00
parent c335207b28
commit 9e86e9b29f
13 changed files with 155 additions and 60 deletions

View file

@ -26,11 +26,11 @@ impl Bar {
) -> Self {
let control = Control::new();
let tab = Tab::new(window_action, view);
let menu = Menu::new((browser_action, window_action), profile);
let menu = Rc::new(Menu::new((browser_action, window_action), profile));
Self {
widget: Rc::new(Widget::new(
&control.widget.gobject,
&menu.widget.gobject,
&control.window_controls,
&menu.menu_button,
&tab.widget.tab_bar,
)),
}

View file

@ -1,17 +1,19 @@
mod widget;
use widget::Widget;
use gtk::{PackType, WindowControls};
use std::rc::Rc;
const MARGIN: i32 = 4;
pub struct Control {
pub widget: Rc<Widget>,
pub window_controls: WindowControls,
}
impl Control {
// Construct
pub fn new() -> Self {
Self {
widget: Rc::new(Widget::new()),
window_controls: WindowControls::builder()
.margin_end(MARGIN)
.side(PackType::End)
.build(),
}
}
}

View file

@ -1,17 +0,0 @@
use gtk::{PackType, WindowControls};
pub struct Widget {
pub gobject: WindowControls,
}
impl Widget {
// Construct
pub fn new() -> Self {
Self {
gobject: WindowControls::builder()
.side(PackType::End)
.margin_end(4)
.build(),
}
}
}

View file

@ -1,22 +1,24 @@
mod widget;
use widget::Widget;
use super::{BrowserAction, Profile, WindowAction};
use gtk::{
gio::{self},
prelude::ActionExt,
prelude::{ActionExt, ToVariant},
Align, MenuButton,
};
use std::rc::Rc;
// Config options
const RECENT_BOOKMARKS: usize = 50;
pub struct Menu {
pub widget: Rc<Widget>,
pub menu_button: MenuButton,
}
#[rustfmt::skip] // @TODO template builder?
impl Menu {
pub fn new(
(browser_action, window_action): (&Rc<BrowserAction>, &Rc<WindowAction>),
_profile: &Rc<Profile>,
profile: &Rc<Profile>,
) -> Self {
// Main
let main = gio::Menu::new();
@ -122,6 +124,12 @@ impl Menu {
main.append_submenu(Some("Page"), &main_page);
// Main > Bookmark
// * menu items dynamically generated using profile memory pool and `set_create_popup_func`
let main_bookmarks = gio::Menu::new();
main.append_submenu(Some("Bookmarks"), &main_bookmarks);
// Main > Tool
let main_tool = gio::Menu::new();
@ -152,7 +160,42 @@ impl Menu {
browser_action.close.simple_action.name()
)));
// Init main widget
let menu_button = MenuButton::builder()
.css_classes(["flat"])
.icon_name("open-menu-symbolic")
.menu_model(&main)
.tooltip_text("Menu")
.valign(Align::Center)
.build();
// Generate dynamical menu items
menu_button.set_create_popup_func({
let profile = profile.clone();
let main_bookmarks = main_bookmarks.clone();
let window_action = window_action.clone();
move |_| {
main_bookmarks.remove_all();
for bookmark in profile.bookmark.memory.recent(RECENT_BOOKMARKS) {
let menu_item = gio::MenuItem::new(Some(&bookmark), None);
menu_item.set_action_and_target_value(Some(&format!(
"{}.{}",
window_action.id,
window_action.open.simple_action.name()
)), Some(&bookmark.to_variant()));
main_bookmarks.append_item(&menu_item);
}
// Show all bookmarks menu item
// if profile.bookmark.memory.total() > RECENT_BOOKMARKS {
// @TODO
// }
}
});
// Result
Self { widget:Rc::new(Widget::new(&main)) }
Self {
menu_button
}
}
}

View file

@ -1,20 +0,0 @@
use gtk::{gio::Menu, Align, MenuButton};
pub struct Widget {
pub gobject: MenuButton,
}
impl Widget {
// Construct
pub fn new(model: &Menu) -> Self {
Self {
gobject: MenuButton::builder()
.css_classes(["flat"])
.icon_name("open-menu-symbolic")
.menu_model(model)
.tooltip_text("Menu")
.valign(Align::Center)
.build(),
}
}
}