Skip to content

Commit 91e6d78

Browse files
authored
Gtk4 Prep: User EventController and PopoverMenu for Terminal context menu (#1733)
1 parent 01308d8 commit 91e6d78

1 file changed

Lines changed: 21 additions & 14 deletions

File tree

src/Widgets/Terminal.vala

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
public class Code.Terminal : Gtk.Box {
88
public const string ACTION_GROUP = "term";
9-
public const string ACTION_PREFIX = ACTION_GROUP + ".";
109
public const string ACTION_COPY = "action-copy";
1110
public const string ACTION_PASTE = "action-paste";
1211

@@ -26,7 +25,7 @@ public class Code.Terminal : Gtk.Box {
2625

2726
public Vte.Terminal terminal { get; construct; }
2827
private Gtk.EventControllerKey key_controller;
29-
28+
private Gtk.GestureMultiPress button_controller;
3029
private Settings? terminal_settings = null;
3130
private Settings? gnome_interface_settings = null;
3231
private Settings? gnome_wm_settings = null;
@@ -35,6 +34,7 @@ public class Code.Terminal : Gtk.Box {
3534

3635
private GLib.Pid child_pid;
3736
private Gtk.Clipboard current_clipboard;
37+
private Menu menu_model;
3838

3939
private Scratch.Application application;
4040

@@ -128,14 +128,18 @@ public class Code.Terminal : Gtk.Box {
128128
actions = new SimpleActionGroup ();
129129
actions.add_action (copy_action);
130130
actions.add_action (paste_action);
131+
terminal.insert_action_group (ACTION_GROUP, actions);
131132

132-
var menu_model = new GLib.Menu ();
133-
menu_model.append (_("Copy"), ACTION_PREFIX + ACTION_COPY);
134-
menu_model.append (_("Paste"), ACTION_PREFIX + ACTION_PASTE);
133+
menu_model = new GLib.Menu ();
134+
menu_model.append (_("Copy"), ACTION_COPY);
135+
menu_model.append (_("Paste"), ACTION_PASTE);
135136

136-
var menu = new Gtk.Menu.from_model (menu_model);
137-
menu.insert_action_group (ACTION_GROUP, actions);
138-
menu.show_all ();
137+
var menu = new Gtk.PopoverMenu () {
138+
modal = true,
139+
relative_to = terminal,
140+
position = RIGHT
141+
};
142+
menu.bind_model (menu_model, ACTION_GROUP);
139143

140144
key_controller = new Gtk.EventControllerKey (terminal) {
141145
propagation_phase = BUBBLE
@@ -146,17 +150,20 @@ public class Code.Terminal : Gtk.Box {
146150
terminal.enter_notify_event.connect (() => {
147151
if (!terminal.has_focus) {
148152
terminal.grab_focus ();
149-
150153
}
151154
});
152155

153-
terminal.button_press_event.connect ((event) => {
154-
if (event.button == 3) {
156+
button_controller = new Gtk.GestureMultiPress (terminal) {
157+
propagation_phase = CAPTURE,
158+
button = 0
159+
};
160+
button_controller.pressed.connect ((n, x, y) => {
161+
var event = button_controller.get_last_event (null);
162+
if (event.triggers_context_menu ()) {
155163
paste_action.set_enabled (current_clipboard.wait_is_text_available ());
156-
menu.select_first (false);
157-
menu.popup_at_pointer (event);
164+
menu.pointing_to = Gdk.Rectangle () {x = (int)x, y = (int)y, height = 1, width = 1};
165+
menu.popup ();
158166
}
159-
return false;
160167
});
161168

162169
realize.connect (() => {

0 commit comments

Comments
 (0)