Skip to content

Commit a377912

Browse files
committed
NemoAction: Add support for keyboard shortcuts.
- These are stored in actions-tree.json - Only the menubar menus will show the assigned shortcut next to the action. The context menus will not.
1 parent fa76d52 commit a377912

File tree

7 files changed

+98
-4
lines changed

7 files changed

+98
-4
lines changed

libnemo-private/nemo-action-manager.c

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -667,12 +667,14 @@ parse_item (ActionsIterData *idata,
667667
NULL,
668668
GTK_UI_MANAGER_SEPARATOR,
669669
path,
670+
NULL,
670671
idata->user_data);
671672
return TRUE;
672673
}
673674

674675
const gchar *user_label = NULL;
675676
const gchar *user_icon = NULL;
677+
const gchar *accelerator = NULL;
676678

677679
// user-label and user-icon are optional, no error if they're missing or null.
678680
if (json_reader_read_member (reader, "user-label") && !json_reader_get_null_value (reader)) {
@@ -685,6 +687,14 @@ parse_item (ActionsIterData *idata,
685687
}
686688
json_reader_end_member (reader);
687689

690+
if (json_reader_read_member (reader, "accelerator") && !json_reader_get_null_value (reader)) {
691+
accelerator = json_reader_get_string_value (reader);
692+
if (accelerator[0] == '\0') {
693+
accelerator = NULL;
694+
}
695+
}
696+
json_reader_end_member (reader);
697+
688698
if (g_strcmp0 (type, "action") == 0) {
689699
NemoAction *action;
690700
g_autofree gchar *lookup_uuid = nemo_make_action_uuid_for_path (uuid);
@@ -705,12 +715,17 @@ parse_item (ActionsIterData *idata,
705715
nemo_action_override_icon (action, user_icon);
706716
}
707717

718+
if (accelerator != NULL) {
719+
action->has_accel = TRUE;
720+
}
721+
708722
DEBUG ("Adding action '%s' to UI.", action->uuid);
709723

710724
idata->func (idata->action_manager,
711725
GTK_ACTION (action),
712726
GTK_UI_MANAGER_MENUITEM,
713727
path,
728+
accelerator,
714729
idata->user_data);
715730
g_object_unref (action);
716731

@@ -743,6 +758,7 @@ parse_item (ActionsIterData *idata,
743758
submenu,
744759
GTK_UI_MANAGER_MENU,
745760
path,
761+
NULL,
746762
idata->user_data);
747763

748764
if (!json_reader_read_member (reader, "children") || json_reader_get_null_value (reader)) {
@@ -890,6 +906,7 @@ nemo_action_manager_iterate_actions (NemoActionManager *action_ma
890906
GTK_ACTION (action),
891907
GTK_UI_MANAGER_MENUITEM,
892908
NULL,
909+
NULL,
893910
user_data);
894911
}
895912
}
@@ -902,6 +919,7 @@ nemo_action_manager_add_action_ui (NemoActionManager *manager,
902919
GtkUIManager *ui_manager,
903920
GtkAction *action,
904921
const gchar *action_path,
922+
const gchar *accelerator,
905923
GtkActionGroup *action_group,
906924
guint merge_id,
907925
const gchar **placeholder_paths,
@@ -920,7 +938,7 @@ nemo_action_manager_add_action_ui (NemoActionManager *manager,
920938
user_data);
921939
}
922940

923-
gtk_action_group_add_action (action_group, action);
941+
gtk_action_group_add_action_with_accel (action_group, action, accelerator ? accelerator : "");
924942
gtk_action_set_visible (action, FALSE);
925943
}
926944

@@ -960,10 +978,13 @@ nemo_action_manager_update_action_states (NemoActionManager *action_manager,
960978
GList *selection,
961979
NemoFile *parent,
962980
gboolean for_places,
981+
gboolean for_accelerators,
963982
GtkWindow *window)
964983
{
965984
GList *l, *actions;
966985

986+
DEBUG ("Updating action states: for accelerated actions only: %s", for_accelerators ? "yes" : "no");
987+
967988
actions = gtk_action_group_list_actions (action_group);
968989

969990
for (l = actions; l != NULL; l = l->next) {
@@ -973,6 +994,12 @@ nemo_action_manager_update_action_states (NemoActionManager *action_manager,
973994
continue;
974995
}
975996

997+
998+
if (for_accelerators && !NEMO_ACTION (l->data)->has_accel) {
999+
DEBUG ("Skipping '%s' (no accelerator assigned to this action)", NEMO_ACTION (l->data)->uuid);
1000+
continue;
1001+
}
1002+
9761003
nemo_action_update_display_state (NEMO_ACTION (l->data), selection, parent, for_places, window);
9771004
}
9781005

libnemo-private/nemo-action-manager.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ typedef void (* NemoActionManagerIterFunc) (NemoActionManager *manager,
3333
GtkAction *action,
3434
GtkUIManagerItemType type,
3535
const gchar *path,
36+
const gchar *accelerator,
3637
gpointer user_data);
3738

3839
NemoActionManager * nemo_action_manager_new (void);
@@ -50,12 +51,14 @@ void nemo_action_manager_update_action_states (NemoActionM
5051
GList *selection,
5152
NemoFile *parent,
5253
gboolean for_places,
54+
gboolean for_accelerators,
5355
GtkWindow *window);
5456

5557
void nemo_action_manager_add_action_ui (NemoActionManager *manager,
5658
GtkUIManager *ui_manager,
5759
GtkAction *action,
5860
const gchar *action_path,
61+
const gchar *accelerator,
5962
GtkActionGroup *action_group,
6063
guint merge_id,
6164
const gchar **placeholder_paths,

libnemo-private/nemo-action.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ struct _NemoAction {
3636
gchar *uuid; // basename of key_file_path
3737
gchar *key_file_path;
3838
gchar *parent_dir;
39+
gboolean has_accel;
3940
};
4041

4142
struct _NemoActionClass {

src/nemo-blank-desktop-window.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <config.h>
2323
#include "nemo-blank-desktop-window.h"
2424
#include "nemo-desktop-manager.h"
25+
#include "nemo-application.h"
2526

2627
#include <X11/Xatom.h>
2728
#include <gdk/gdk.h>
@@ -145,6 +146,7 @@ update_actions_visibility (NemoBlankDesktopWindow *window)
145146
NULL,
146147
NULL,
147148
FALSE,
149+
FALSE,
148150
GTK_WINDOW (window));
149151
}
150152

@@ -153,6 +155,7 @@ add_action_to_ui (NemoActionManager *manager,
153155
GtkAction *action,
154156
GtkUIManagerItemType type,
155157
const gchar *path,
158+
const gchar *accelerator,
156159
gpointer user_data)
157160
{
158161
NemoBlankDesktopWindow *window = NEMO_BLANK_DESKTOP_WINDOW (user_data);
@@ -166,6 +169,7 @@ add_action_to_ui (NemoActionManager *manager,
166169
window->details->ui_manager,
167170
action,
168171
path,
172+
accelerator,
169173
window->details->action_group,
170174
window->details->actions_merge_id,
171175
roots,
@@ -262,6 +266,7 @@ actions_changed_idle_cb (gpointer user_data)
262266
NemoBlankDesktopWindow *window = NEMO_BLANK_DESKTOP_WINDOW (user_data);
263267

264268
reset_popup_menu (window);
269+
update_actions_visibility (window);
265270

266271
window->details->actions_changed_idle_id = 0;
267272
return G_SOURCE_REMOVE;
@@ -276,14 +281,36 @@ actions_changed (gpointer user_data)
276281
window->details->actions_changed_idle_id = g_idle_add (actions_changed_idle_cb, window);
277282
}
278283

284+
/**
285+
* nemo_window_show:
286+
* @widget: GtkWidget
287+
*
288+
* Call parent and then show/hide window items
289+
* base on user prefs.
290+
*/
291+
static void
292+
show (GtkWidget *widget)
293+
{
294+
NemoBlankDesktopWindow *window;
295+
296+
window = NEMO_BLANK_DESKTOP_WINDOW (widget);
297+
298+
GTK_WIDGET_CLASS (nemo_blank_desktop_window_parent_class)->show (widget);
299+
gtk_ui_manager_ensure_update (window->details->ui_manager);
300+
}
301+
279302
static void
280303
nemo_blank_desktop_window_constructed (GObject *obj)
281304
{
282305
AtkObject *accessible;
306+
NemoApplication *application;
283307
NemoBlankDesktopWindow *window = NEMO_BLANK_DESKTOP_WINDOW (obj);
284308

285309
G_OBJECT_CLASS (nemo_blank_desktop_window_parent_class)->constructed (obj);
286310

311+
application = nemo_application_get_singleton ();
312+
gtk_window_set_application (GTK_WINDOW (window), GTK_APPLICATION (application));
313+
287314
/* Set the accessible name so that it doesn't inherit the cryptic desktop URI. */
288315
accessible = gtk_widget_get_accessible (GTK_WIDGET (window));
289316

@@ -292,6 +319,9 @@ nemo_blank_desktop_window_constructed (GObject *obj)
292319
}
293320

294321
window->details->ui_manager = gtk_ui_manager_new ();
322+
gtk_window_add_accel_group (GTK_WINDOW (window),
323+
gtk_ui_manager_get_accel_group (window->details->ui_manager));
324+
295325
window->details->action_manager = nemo_action_manager_new ();
296326

297327
if (window->details->actions_changed_id == 0) {
@@ -444,6 +474,7 @@ nemo_blank_desktop_window_class_init (NemoBlankDesktopWindowClass *klass)
444474
wclass->realize = realize;
445475
wclass->unrealize = unrealize;
446476
wclass->map = map;
477+
wclass->show = show;
447478
wclass->delete_event = nemo_blank_desktop_window_delete_event;
448479

449480
properties[PROP_MONITOR] =

src/nemo-places-sidebar.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2323,6 +2323,7 @@ update_menu_states (NemoPlacesSidebar *sidebar)
23232323
selection,
23242324
parent,
23252325
TRUE,
2326+
FALSE,
23262327
GTK_WINDOW (sidebar->window));
23272328
nemo_file_list_free (selection);
23282329
nemo_file_unref (parent);
@@ -3482,6 +3483,7 @@ add_action_to_ui (NemoActionManager *manager,
34823483
GtkAction *action,
34833484
GtkUIManagerItemType type,
34843485
const gchar *path,
3486+
const gchar *accelerator,
34853487
gpointer user_data)
34863488
{
34873489
NemoPlacesSidebar *sidebar = NEMO_PLACES_SIDEBAR (user_data);
@@ -3495,6 +3497,7 @@ add_action_to_ui (NemoActionManager *manager,
34953497
sidebar->ui_manager,
34963498
action,
34973499
path,
3500+
accelerator,
34983501
sidebar->action_action_group,
34993502
sidebar->action_action_group_merge_id,
35003503
roots,
@@ -3830,7 +3833,7 @@ trash_state_changed_cb (NemoTrashMonitor *trash_monitor,
38303833
/* The trash icon changed, update the sidebar */
38313834
update_places (sidebar);
38323835

3833-
// reset_menu (sidebar);
3836+
reset_menu (sidebar);
38343837
}
38353838

38363839
static void

src/nemo-tree-sidebar.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -738,6 +738,7 @@ update_menu_states (FMTreeView *view,
738738
selected,
739739
parent,
740740
FALSE,
741+
FALSE,
741742
GTK_WINDOW (view->details->window));
742743
nemo_file_list_free (selected);
743744
nemo_file_unref (parent);
@@ -1311,6 +1312,7 @@ add_action_to_ui (NemoActionManager *manager,
13111312
GtkAction *action,
13121313
GtkUIManagerItemType type,
13131314
const gchar *path,
1315+
const gchar *accelerator,
13141316
gpointer user_data)
13151317
{
13161318
FMTreeView *view = FM_TREE_VIEW (user_data);
@@ -1324,6 +1326,7 @@ add_action_to_ui (NemoActionManager *manager,
13241326
view->details->ui_manager,
13251327
action,
13261328
path,
1329+
accelerator,
13271330
view->details->action_action_group,
13281331
view->details->action_action_group_merge_id,
13291332
roots,

src/nemo-view.c

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,7 @@ static gboolean file_list_all_are_folders (GList *file_list
364364
static void unschedule_pop_up_location_context_menu (NemoView *view);
365365
static void disconnect_bookmark_signals (NemoView *view);
366366
static void run_action_callback (NemoAction *action, gpointer callback_data);
367+
static void update_accelerated_actions (NemoView *view);
367368

368369
G_DEFINE_TYPE (NemoView, nemo_view, GTK_TYPE_SCROLLED_WINDOW);
369370
#define parent_class nemo_view_parent_class
@@ -6331,7 +6332,9 @@ run_action_callback (NemoAction *action, gpointer callback_data)
63316332
}
63326333

63336334
static void
6334-
update_actions_visibility (NemoView *view, GList *selection)
6335+
update_actions_visibility (NemoView *view,
6336+
GList *selection,
6337+
gboolean for_accelerators)
63356338
{
63366339
NemoFile *parent = nemo_view_get_directory_as_file (view);
63376340

@@ -6340,6 +6343,7 @@ update_actions_visibility (NemoView *view, GList *selection)
63406343
selection,
63416344
parent,
63426345
FALSE,
6346+
for_accelerators,
63436347
GTK_WINDOW (view->details->window));
63446348
}
63456349

@@ -6348,6 +6352,7 @@ add_action_to_ui (NemoActionManager *manager,
63486352
GtkAction *action,
63496353
GtkUIManagerItemType type,
63506354
const gchar *path,
6355+
const gchar *accelerator,
63516356
gpointer user_data)
63526357
{
63536358
NemoView *view = NEMO_VIEW (user_data);
@@ -6363,6 +6368,7 @@ add_action_to_ui (NemoActionManager *manager,
63636368
nemo_window_get_ui_manager (view->details->window),
63646369
action,
63656370
path,
6371+
accelerator,
63666372
view->details->actions_action_group,
63676373
view->details->actions_merge_id,
63686374
roots,
@@ -6379,6 +6385,8 @@ update_actions (NemoView *view)
63796385
nemo_action_manager_iterate_actions (view->details->action_manager,
63806386
(NemoActionManagerIterFunc) add_action_to_ui,
63816387
view);
6388+
6389+
update_accelerated_actions (view);
63826390
}
63836391

63846392
static void
@@ -9931,6 +9939,8 @@ real_update_menus (NemoView *view)
99319939
update_templates_menu (view);
99329940
}
99339941

9942+
update_accelerated_actions (view);
9943+
99349944
next_pane_is_writable = has_writable_extra_pane (view);
99359945

99369946
/* next pane: works if file is copyable, and next pane is writable */
@@ -10049,13 +10059,29 @@ nemo_view_update_actions_and_extensions (NemoView *view)
1004910059

1005010060
selection = nemo_view_get_selection (view);
1005110061

10052-
update_actions_visibility (view, selection);
10062+
update_actions_visibility (view, selection, FALSE);
1005310063
reset_extension_actions_menu (view, selection);
1005410064
update_configurable_context_menu_items (view);
1005510065

1005610066
nemo_file_list_free (selection);
1005710067
}
1005810068

10069+
static void
10070+
update_accelerated_actions (NemoView *view)
10071+
{
10072+
GList *selection;
10073+
10074+
if (view->details->actions_invalid) {
10075+
update_actions_menu (view);
10076+
}
10077+
10078+
selection = nemo_view_get_selection (view);
10079+
10080+
update_actions_visibility (view, selection, TRUE);
10081+
10082+
nemo_file_list_free (selection);
10083+
}
10084+
1005910085
/**
1006010086
* nemo_view_pop_up_selection_context_menu
1006110087
*

0 commit comments

Comments
 (0)