From 5aa91d81ff7fcb386722bec0f344181f17c358f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=9F=B3=E5=8D=9A=E6=96=87?= Date: Sun, 7 Jan 2024 01:00:18 +0800 Subject: [PATCH] feat: Add popup menu --- viewer/src/main.rs | 30 ++++++++++++++++++++++++++++-- viewer/src/stc_viewer.rs | 9 +++++++++ 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/viewer/src/main.rs b/viewer/src/main.rs index b5b4e8c..7eb9e45 100644 --- a/viewer/src/main.rs +++ b/viewer/src/main.rs @@ -4,10 +4,12 @@ mod storage; use crate::stc_viewer::{StcViewerApp, UIMessages, STC_VIEWER_COLUMN_NAME}; use glib::MainContext; +use gtk4::gdk::ffi::GDK_BUTTON_SECONDARY; +use gtk4::gdk::Rectangle; use gtk4::prelude::*; use gtk4::{ - Application, ApplicationWindow, CellRendererText, Orientation, Paned, ScrolledWindow, - TreeViewColumn, WrapMode, + Application, ApplicationWindow, CellRendererText, EventSequenceState, GestureClick, + Orientation, Paned, PopoverMenu, ScrolledWindow, TreeViewColumn, WrapMode, }; use quick_xml::de::from_str; use stc::prelude::*; @@ -114,6 +116,30 @@ fn build_ui(app: &Application, mgr: UnitsManager) { } }); + // TreeView popup menus + let app_copy = stc_app.clone(); + let window_copy = window.clone(); + let gesture = GestureClick::new(); + gesture.set_button(GDK_BUTTON_SECONDARY as u32); + gesture.connect_pressed(move |g, _, x, y| { + g.set_state(EventSequenceState::Claimed); + + let app = app_copy.lock().unwrap(); + let menu_model = &app.popup_menu_model(); + if menu_model.n_items() == 0 { + return; + } + + let menu = PopoverMenu::builder() + .menu_model(menu_model) + .has_arrow(false) + .pointing_to(&Rectangle::new(x as i32, y as i32, 0, 0)) + .build(); + menu.set_parent(&window_copy); + menu.show(); + }); + app_lock.tree_view.add_controller(gesture); + // refresh UI when the window is shown let tx = app_lock.ui_tx.clone(); window.connect_show(move |_| { diff --git a/viewer/src/stc_viewer.rs b/viewer/src/stc_viewer.rs index af2564c..344aef5 100644 --- a/viewer/src/stc_viewer.rs +++ b/viewer/src/stc_viewer.rs @@ -3,6 +3,7 @@ use crate::column_object::ColumnObject; use async_channel::{Receiver, Sender}; use glib::subclass::prelude::ObjectSubclassIsExt; use glib::value::ValueTypeMismatchOrNoneError; +use gtk4::gio::Menu; use gtk4::glib::Type; use gtk4::prelude::*; use gtk4::{Button, SearchEntry, TextBuffer, TextView, TreeStore, TreeView, TreeViewColumn}; @@ -65,6 +66,14 @@ impl StcViewerApp { (r, rx) } + pub fn popup_menu_model(&self) -> Menu { + let m = Menu::new(); + m.append(Some("a"), None); + m.append(Some("b"), None); + + m + } + pub fn on_cursor_changed(&self) { let (path, _col) = TreeViewExt::cursor(&self.tree_view);