Skip to content

Commit

Permalink
Update sidebar project menu to use GLib.MenuModel
Browse files Browse the repository at this point in the history
  • Loading branch information
colinkiama authored and danirabbit committed Jun 24, 2024
1 parent ec189a6 commit 0acf880
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 43 deletions.
105 changes: 74 additions & 31 deletions plugins/fuzzy-search/fuzzy-search.vala
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,25 @@ public class Scratch.Plugins.FuzzySearch: Peas.ExtensionBase, Peas.Activatable {
private Scratch.Services.FuzzySearchIndexer indexer;
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 = "action_show";
private const ActionEntry[] ACTION_ENTRIES = {
{ACTION_SHOW, fuzzy_find }
};

private SimpleActionGroup actions;
private GLib.Settings folder_settings;

private static Gee.MultiMap<string, string> action_accelerators = new Gee.HashMultiMap<string, string> ();

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

public void update_state () {

}
Expand All @@ -38,21 +53,10 @@ 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 +68,53 @@ 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) == 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);
}

return false;
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, {});
}

window.insert_action_group (ACTION_GROUP, null);
}

private void fuzzy_find () {
Expand All @@ -103,12 +139,19 @@ 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
23 changes: 11 additions & 12 deletions src/Widgets/Sidebar.vala
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class Code.Sidebar : Gtk.Grid {
public Gtk.Stack stack { get; private set; }
public Code.ChooseProjectButton choose_project_button { get; private set; }
public Hdy.HeaderBar headerbar { get; private set; }
public Gtk.Menu project_menu { get; construct; }
public GLib.MenuModel project_menu_model { get; construct; }

private Gtk.StackSwitcher stack_switcher;

Expand Down Expand Up @@ -63,22 +63,21 @@ public class Code.Sidebar : Gtk.Grid {
label = _("Open Folder…")
};

var collapse_all_menu_item = new Gtk.MenuItem.with_label (_("Collapse All"));
collapse_all_menu_item.action_name = Scratch.MainWindow.ACTION_PREFIX +
Scratch.MainWindow.ACTION_COLLAPSE_ALL_FOLDERS;
var collapse_all_menu_item = new GLib.MenuItem (_("Collapse All"), Scratch.MainWindow.ACTION_PREFIX
+ Scratch.MainWindow.ACTION_COLLAPSE_ALL_FOLDERS);

var order_projects_menu_item = new Gtk.MenuItem.with_label (_("Alphabetize"));
order_projects_menu_item.action_name = Scratch.MainWindow.ACTION_PREFIX +
Scratch.MainWindow.ACTION_ORDER_FOLDERS;
var order_projects_menu_item = new GLib.MenuItem (_("Alphabetize"), Scratch.MainWindow.ACTION_PREFIX
+ Scratch.MainWindow.ACTION_ORDER_FOLDERS);

project_menu = new Gtk.Menu ();
project_menu.append (collapse_all_menu_item);
project_menu.append (order_projects_menu_item);
project_menu.show_all ();
var project_menu = new GLib.Menu ();
project_menu.append_item (collapse_all_menu_item);
project_menu.append_item (order_projects_menu_item);
project_menu_model = project_menu;

var project_more_button = new Gtk.MenuButton ();
project_more_button.image = new Gtk.Image.from_icon_name ("view-more-symbolic", Gtk.IconSize.SMALL_TOOLBAR);
project_more_button.popup = project_menu;
project_more_button.use_popover = false;
project_more_button.menu_model = project_menu_model;
project_more_button.tooltip_text = _("Manage project folders");

actionbar.add (add_folder_button);
Expand Down

0 comments on commit 0acf880

Please sign in to comment.