Skip to content

Commit

Permalink
Define terminal menu items using GLib.MenuModel (#1438)
Browse files Browse the repository at this point in the history
  • Loading branch information
colinkiama authored Jun 18, 2024
1 parent c7482c9 commit fa30c67
Showing 1 changed file with 34 additions and 12 deletions.
46 changes: 34 additions & 12 deletions src/Widgets/Terminal.vala
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,21 @@
*/

public class Code.Terminal : Gtk.Box {
public const string ACTION_GROUP = "term";
public const string ACTION_PREFIX = ACTION_GROUP + ".";
public const string ACTION_COPY = "action_copy";
public const string ACTION_PASTE = "action_paste";

private const double MAX_SCALE = 5.0;
private const double MIN_SCALE = 0.2;
private const string LEGACY_SETTINGS_SCHEMA = "org.pantheon.terminal.settings";
private const string SETTINGS_SCHEMA = "io.elementary.terminal.settings";

private GLib.Pid child_pid;
public Vte.Terminal terminal { get; construct; }
public SimpleActionGroup actions { get; construct; }

private GLib.Pid child_pid;
private Gtk.Clipboard current_clipboard;

construct {
terminal = new Vte.Terminal () {
Expand All @@ -36,29 +44,43 @@ public class Code.Terminal : Gtk.Box {
GLib.Application.get_default ().activate_action (Scratch.MainWindow.ACTION_PREFIX + Scratch.MainWindow.ACTION_TOGGLE_TERMINAL, null);
});

var copy = new Gtk.MenuItem.with_label (_("Copy"));
copy.activate.connect (() => {
terminal.copy_clipboard ();
});
var copy_action = new SimpleAction (ACTION_COPY, null);
copy_action.set_enabled (false);
copy_action.activate.connect (() => terminal.copy_clipboard ());

var paste = new Gtk.MenuItem.with_label (_("Paste"));
paste.activate.connect (() => {
terminal.paste_clipboard ();
});
var paste_action = new SimpleAction (ACTION_PASTE, null);
paste_action.activate.connect (() => terminal.paste_clipboard ());

var menu = new Gtk.Menu ();
menu.append (copy);
menu.append (paste);
actions = new SimpleActionGroup ();
actions.add_action (copy_action);
actions.add_action (paste_action);

var menu_model = new GLib.Menu ();
menu_model.append (_("Copy"), ACTION_PREFIX + ACTION_COPY);
menu_model.append (_("Paste"), ACTION_PREFIX + ACTION_PASTE);

var menu = new Gtk.Menu.from_model (menu_model);
menu.insert_action_group (ACTION_GROUP, actions);
menu.show_all ();

terminal.button_press_event.connect ((event) => {
if (event.button == 3) {
paste_action.set_enabled (current_clipboard.wait_is_text_available ());
menu.select_first (false);
menu.popup_at_pointer (event);
}
return false;
});

realize.connect (() => {
current_clipboard = terminal.get_clipboard (Gdk.SELECTION_CLIPBOARD);
copy_action.set_enabled (terminal.get_has_selection ());
});

terminal.selection_changed.connect (() => {
copy_action.set_enabled (terminal.get_has_selection ());
});

var settings = new Settings (Constants.PROJECT_NAME + ".saved-state");
spawn_shell (settings.get_string ("last-opened-path"));

Expand Down

0 comments on commit fa30c67

Please sign in to comment.