init context menu for page tabs

This commit is contained in:
yggverse 2024-11-04 04:52:33 +02:00
parent 51f543143d
commit b443d1c58e
6 changed files with 93 additions and 27 deletions

View file

@ -92,8 +92,8 @@ impl Window {
self.tab.page_navigation_history_forward();
}
pub fn tab_page_navigation_reload(&self) {
self.tab.page_navigation_reload();
pub fn tab_page_navigation_reload(&self, page_position: i32) {
self.tab.page_navigation_reload(page_position);
}
pub fn tab_close(&self) {

View file

@ -8,9 +8,9 @@ use widget::Widget;
use adw::TabView;
use gtk::{
gio::SimpleAction,
glib::{uuid_string_random, GString, Propagation},
prelude::StaticVariantType,
gio::{Menu, SimpleAction},
glib::{gformat, uuid_string_random, GString, Propagation},
prelude::{ActionExt, StaticVariantType, ToVariant},
};
use sqlite::Transaction;
use std::{cell::RefCell, collections::HashMap, sync::Arc};
@ -48,10 +48,51 @@ 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())),
); // @TODO resolve namespace outside
// Init widget
let widget = Arc::new(Widget::new(action_tab_open.clone()));
let widget = Arc::new(Widget::new(&menu));
// Init events
// Setup actions for context menu
widget.gobject().connect_setup_menu({
let action_page_reload = action_page_reload.clone();
move |tab_view, tab_page| {
// Enable actions by default
action_page_reload.set_enabled(true);
match tab_page {
// Menu opened:
// setup actions to operate with page selected only
Some(this) => {
let position = tab_view.page_position(this).to_variant();
action_page_reload.change_state(&position);
}
// Menu closed:
// return actions to default values
None => match tab_view.selected_page() {
Some(selected) => {
// Get position of page selected
let position = tab_view.page_position(&selected).to_variant();
// Update related actions
action_page_reload.change_state(&position);
}
// No selected page found, disable related actions
None => action_page_reload.set_enabled(false),
},
}
}
});
widget.gobject().connect_close_page({
let index = index.clone();
move |_, item| {
@ -201,8 +242,8 @@ impl Tab {
}
}
pub fn page_navigation_reload(&self) {
if let Some(id) = self.widget.current_page_keyword() {
pub fn page_navigation_reload(&self, page_position: i32) {
if let Some(id) = self.widget.gobject().nth_page(page_position).keyword() {
if let Some(item) = self.index.borrow().get(&id) {
item.page_navigation_reload();
}

View file

@ -1,36 +1,37 @@
use adw::TabView;
use gtk::{
gio::{Icon, SimpleAction, SimpleActionGroup},
glib::{uuid_string_random, GString},
prelude::{ActionMapExt, WidgetExt},
gio::{Icon, MenuModel},
glib::GString,
prelude::IsA,
};
/// Currently used as the indicator for pinned tabs
const DEFAULT_TAB_ICON: &str = "view-pin-symbolic";
/// Wrapper for [TabView](https://gnome.pages.gitlab.gnome.org/libadwaita/doc/main/class.TabView.html) GObject
pub struct Widget {
gobject: TabView,
}
impl Widget {
// Construct
pub fn new(action_page_new: SimpleAction) -> Self {
// Init additional action group
let action_group = SimpleActionGroup::new();
action_group.add_action(&action_page_new);
pub fn new(menu_model: &impl IsA<MenuModel>) -> Self {
// Init gobject
let gobject = TabView::new();
let gobject = TabView::builder().menu_model(menu_model).build();
// Change default icon visible for tabs pinned
if let Ok(default_icon) = Icon::for_string("view-pin-symbolic") {
// Change default icon (if available in the system icon set)
// * visible for pinned tabs only
// * @TODO not default GTK behavior, make this feature optional
if let Ok(default_icon) = Icon::for_string(DEFAULT_TAB_ICON) {
gobject.set_default_icon(&default_icon);
}
// Create new group for actions
gobject.insert_action_group(&uuid_string_random(), Some(&action_group));
// Done
Self { gobject }
}
// Actions
pub fn close(&self) {
if let Some(selected_page) = self.gobject.selected_page() {
self.gobject.close_page(&selected_page);
@ -46,6 +47,7 @@ impl Widget {
}
// Getters
pub fn current_page_keyword(&self) -> Option<GString> {
let page = self.gobject.selected_page()?;
let id = page.keyword()?;