use separated mod for context menu model

This commit is contained in:
yggverse 2024-11-04 23:48:30 +02:00
parent 2d81cf7954
commit cf002cebd8
2 changed files with 95 additions and 51 deletions

View File

@ -1,15 +1,17 @@
mod database;
mod item;
mod menu;
mod widget;
use database::Database;
use item::Item;
use menu::Menu;
use widget::Widget;
use adw::TabView;
use gtk::{
gio::{Menu, SimpleAction},
glib::{gformat, uuid_string_random, GString, Propagation},
gio::SimpleAction,
glib::{uuid_string_random, GString, Propagation},
prelude::{ActionExt, StaticVariantType, ToVariant},
};
use sqlite::Transaction;
@ -50,58 +52,19 @@ impl Tab {
// Init empty HashMap index as no tabs appended yet
let index = Arc::new(RefCell::new(HashMap::new()));
// @TODO move to mod
let menu = Menu::new();
menu.append(
Some("Reload"),
Some(&gformat!("win.{}", action_page_reload.name())),
// Init context menu
let menu = Menu::new(
action_page_close_all.clone(),
action_page_close.clone(),
action_page_history_back.clone(),
action_page_history_forward.clone(),
action_page_home.clone(),
action_page_pin.clone(),
action_page_reload.clone(),
);
menu.append(
Some("Pin"),
Some(&gformat!("win.{}", action_page_pin.name())),
);
let navigation = Menu::new();
navigation.append(
Some("Home"),
Some(&gformat!("win.{}", action_page_home.name())),
);
menu.append_section(None, &navigation);
let history = Menu::new();
history.append(
Some("Back"),
Some(&gformat!("win.{}", action_page_history_back.name())),
);
history.append(
Some("Forward"),
Some(&gformat!("win.{}", action_page_history_forward.name())),
);
menu.append_submenu(Some("History"), &history);
let close = Menu::new();
close.append(
Some("Current"),
Some(&gformat!("win.{}", action_page_close.name())),
);
close.append(
Some("All"),
Some(&gformat!("win.{}", action_page_close_all.name())),
);
menu.append_submenu(Some("Close"), &close);
// Init widget
let widget = Arc::new(Widget::new(&menu));
let widget = Arc::new(Widget::new(menu.gobject()));
// Init events

View File

@ -0,0 +1,81 @@
use gtk::{gio::SimpleAction, prelude::ActionExt};
/// Context menu wrapper
///
/// https://gnome.pages.gitlab.gnome.org/libadwaita/doc/main/method.TabView.get_menu_model.html
pub struct Menu {
gobject: gtk::gio::Menu,
}
impl Menu {
// Constructors
/// Create new `Self`
pub fn new(
action_page_close_all: SimpleAction,
action_page_close: SimpleAction,
action_page_history_back: SimpleAction,
action_page_history_forward: SimpleAction,
action_page_home: SimpleAction,
action_page_pin: SimpleAction,
action_page_reload: SimpleAction,
) -> Self {
let main = gtk::gio::Menu::new();
main.append(
Some("Reload"),
Some(&detailed_action_name(action_page_reload)),
);
main.append(Some("Pin"), Some(&detailed_action_name(action_page_pin)));
let navigation = gtk::gio::Menu::new();
navigation.append(Some("Home"), Some(&detailed_action_name(action_page_home)));
main.append_section(None, &navigation);
let history = gtk::gio::Menu::new();
history.append(
Some("Back"),
Some(&detailed_action_name(action_page_history_back)),
);
history.append(
Some("Forward"),
Some(&detailed_action_name(action_page_history_forward)),
);
main.append_submenu(Some("History"), &history);
let close = gtk::gio::Menu::new();
close.append(
Some("Current"),
Some(&detailed_action_name(action_page_close)),
);
close.append(
Some("All"),
Some(&detailed_action_name(action_page_close_all)),
);
main.append_submenu(Some("Close"), &close);
Self { gobject: main }
}
/// Get reference to [Menu](https://docs.gtk.org/gio/class.Menu.html) `GObject`
pub fn gobject(&self) -> &gtk::gio::Menu {
&self.gobject
}
}
// Private helpers
fn detailed_action_name(action: SimpleAction) -> String {
format!("win.{}", action.name()) // @TODO find the way to ident parent group
// without application-wide dependencies import
// see also src/app/action.rs
}