Skip to content

Commit

Permalink
feat: Add popup menu
Browse files Browse the repository at this point in the history
  • Loading branch information
sbwtw committed Jan 6, 2024
1 parent 8b83923 commit 5aa91d8
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
30 changes: 28 additions & 2 deletions viewer/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand Down Expand Up @@ -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 |_| {
Expand Down
9 changes: 9 additions & 0 deletions viewer/src/stc_viewer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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);

Expand Down

0 comments on commit 5aa91d8

Please sign in to comment.