Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[GTK4 Prep] Replace menu items with menu model #1428

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
96 changes: 69 additions & 27 deletions plugins/fuzzy-search/fuzzy-search.vala
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,24 @@ public class Scratch.Plugins.FuzzySearch: Peas.ExtensionBase, Peas.Activatable {
private MainWindow window = null;
private Scratch.Services.Interface plugins;
private Gtk.EventControllerKey key_controller;
private Gtk.MenuItem fuzzy_menuitem;
private GLib.MenuItem fuzzy_menuitem;
private GLib.Cancellable cancellable;

private const string ACTION_GROUP = "fuzzysearch";
private const string ACTION_PREFIX = ACTION_GROUP + ".";
private const string ACTION_SHOW = "fuzzysearch_action_show";
private SimpleActionGroup actions;
private static Gee.MultiMap<string, string> action_accelerators = new Gee.HashMultiMap<string, string> ();
private GLib.Settings folder_settings;

private const ActionEntry[] ACTION_ENTRIES = {
{ACTION_SHOW, fuzzy_find }
};

static construct {
action_accelerators.set (ACTION_SHOW, @"<Alt>$(Gdk.keyval_name (ACCEL_KEY))");
}

public void update_state () {

}
Expand All @@ -38,21 +53,14 @@ public class Scratch.Plugins.FuzzySearch: Peas.ExtensionBase, Peas.Activatable {
});

window = w;

key_controller = new Gtk.EventControllerKey (window) {
propagation_phase = BUBBLE
};

key_controller.key_pressed.connect (on_window_key_press_event);

fuzzy_menuitem = new Gtk.MenuItem.with_label (_("Find Project Files"));
var child = ((Gtk.Bin)fuzzy_menuitem).get_child ();
if (child is Gtk.AccelLabel) {
((Gtk.AccelLabel)child).set_accel (ACCEL_KEY, ACCEL_MODTYPE);
}

fuzzy_menuitem.activate.connect (fuzzy_find);
fuzzy_menuitem.show ();
window.sidebar.project_menu.append (fuzzy_menuitem);
folder_settings = new GLib.Settings ("io.elementary.code.folder-manager");
add_actions ();
folder_settings.changed["opened-folders"].connect (handle_opened_projects_change);
});

plugins.hook_folder_item_change.connect ((src, dest, event) => {
Expand All @@ -64,21 +72,49 @@ public class Scratch.Plugins.FuzzySearch: Peas.ExtensionBase, Peas.Activatable {
});
}

bool on_window_key_press_event (uint keyval, uint keycode, Gdk.ModifierType state) {
/* <Alt>f shows fuzzy search dialog */
switch (Gdk.keyval_to_upper (keyval)) {
case ACCEL_KEY:
if (state == ACCEL_MODTYPE) {
fuzzy_find ();
return true;
}

break;
default:
return false;
private void add_actions () {
if (actions == null) {
actions = new SimpleActionGroup ();
actions.add_action_entries (ACTION_ENTRIES, this);
}

window.insert_action_group (ACTION_GROUP, actions);

var application = (Gtk.Application) GLib.Application.get_default ();
var app = (Scratch.Application) application;

foreach (var action in action_accelerators.get_keys ()) {
var accels_array = action_accelerators[action].to_array ();
accels_array += null;

app.set_accels_for_action (ACTION_PREFIX + action, accels_array);
}

handle_opened_projects_change ();

fuzzy_menuitem = new GLib.MenuItem (_("Find Project Files"), ACTION_PREFIX + ACTION_SHOW );

var menu = window.sidebar.project_menu_model as GLib.Menu;
menu.append_item (fuzzy_menuitem);
}

private void remove_actions () {
var sidebar_menu = window.sidebar.project_menu_model as GLib.Menu;
int length = sidebar_menu.get_n_items ();
for (var i = length - 1; i >= 0; i--) {
var action_name = sidebar_menu.get_item_attribute_value (i, GLib.Menu.ATTRIBUTE_ACTION, GLib.VariantType.STRING).get_string ();
if (action_name.has_prefix (ACTION_PREFIX)) {
sidebar_menu.remove (i);
}
}

var application = (Gtk.Application) GLib.Application.get_default ();
var app = (Scratch.Application) application;
foreach (var action in action_accelerators.get_keys ()) {
app.set_accels_for_action (ACTION_PREFIX + action, {});
}

return false;
window.insert_action_group (ACTION_GROUP, null);
}

private void fuzzy_find () {
Expand All @@ -103,12 +139,18 @@ public class Scratch.Plugins.FuzzySearch: Peas.ExtensionBase, Peas.Activatable {
}

public void deactivate () {
key_controller.key_pressed.disconnect (on_window_key_press_event);
window.sidebar.project_menu.remove (fuzzy_menuitem);
folder_settings.changed["opened-folders"].disconnect (handle_opened_projects_change);
remove_actions ();
if (cancellable != null) {
cancellable.cancel ();
}
}

private void handle_opened_projects_change () {
var show_action = Utils.action_from_group (ACTION_SHOW, actions);
string[] opened_folders = folder_settings.get_strv ("opened-folders");
show_action.set_enabled (opened_folders != null && opened_folders.length > 0);
}
}

[ModuleInit]
Expand Down
57 changes: 46 additions & 11 deletions plugins/pastebin/pastebin.vala
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,23 @@ namespace Scratch.Services {
}

public class Scratch.Plugins.Pastebin : Peas.ExtensionBase, Peas.Activatable {
Gtk.MenuItem? menuitem = null;

public Object object { owned get; construct; }

GLib.MenuItem? menuitem = null;
GLib.Menu? share_menu = null;
Scratch.Services.Document? doc = null;
Scratch.Services.Interface plugins;

const string ACTION_GROUP = "pastebin";
const string ACTION_PREFIX = ACTION_GROUP + ".";
const string ACTION_SHOW = "pastebin_action_show";
SimpleActionGroup actions;

const ActionEntry[] ACTION_ENTRIES = {
{ACTION_SHOW, show_paste_bin_upload_dialog }
};


public void update_state () {
}

Expand All @@ -84,26 +94,51 @@ public class Scratch.Plugins.Pastebin : Peas.ExtensionBase, Peas.Activatable {

plugins.hook_document.connect ((doc) => {
this.doc = doc;

});

plugins.hook_share_menu.connect (on_hook_share_menu);
}

void on_hook_share_menu (Gtk.Menu menu) {
void on_hook_share_menu (GLib.MenuModel menu) {
if (menuitem != null)
return;

menuitem = new Gtk.MenuItem.with_label (_("Upload to Pastebin"));
menuitem.activate.connect (() => {
MainWindow window = plugins.manager.window;
new Dialogs.PasteBinDialog (window, doc);
});
menu.append (menuitem);
menuitem.show_all ();
add_actions (menu);

}

void add_actions (GLib.MenuModel menu) {
if (actions == null) {
actions = new SimpleActionGroup ();
actions.add_action_entries (ACTION_ENTRIES, this);
}

plugins.manager.window.insert_action_group (ACTION_GROUP, actions);
share_menu = (GLib.Menu) menu;
menuitem = new GLib.MenuItem (_("Upload to Pastebin"), ACTION_PREFIX + ACTION_SHOW);
share_menu.append_item (menuitem);
}

void remove_actions () {
int length = share_menu.get_n_items ();
for (var i = length - 1; i >= 0; i--) {
var action_name = share_menu.get_item_attribute_value (i, GLib.Menu.ATTRIBUTE_ACTION, GLib.VariantType.STRING).get_string ();
if (action_name.has_prefix (ACTION_PREFIX)) {
share_menu.remove (i);
}
}

plugins.manager.window.insert_action_group (ACTION_GROUP, null);
}

void show_paste_bin_upload_dialog () {
MainWindow window = plugins.manager.window;
new Dialogs.PasteBinDialog (window, doc);
}

public void deactivate () {
menuitem.destroy ();
remove_actions ();
}
}

Expand Down
39 changes: 0 additions & 39 deletions src/FolderManager/ContractMenuItem.vala

This file was deleted.