Skip to content

Commit

Permalink
actions: Refactor some code to reduce duplication.
Browse files Browse the repository at this point in the history
  • Loading branch information
mtwebster committed Apr 8, 2024
1 parent c5b7694 commit 2eafca4
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 172 deletions.
57 changes: 57 additions & 0 deletions libnemo-private/nemo-action-manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -880,6 +880,63 @@ nemo_action_manager_iterate_actions (NemoActionManager *action_ma
}
}

void
nemo_action_manager_add_action_ui (NemoActionManager *manager,
GtkUIManager *ui_manager,
GtkAction *action,
const gchar *action_path,
GtkActionGroup *action_group,
guint merge_id,
const gchar **placeholder_paths,
GtkUIManagerItemType type,
GCallback activate_callback,
gpointer user_data)
{
if (type != GTK_UI_MANAGER_SEPARATOR) {
if (type == GTK_UI_MANAGER_MENUITEM) {
g_signal_handlers_disconnect_by_func (action,
activate_callback,
user_data);

g_signal_connect (action, "activate",
G_CALLBACK (activate_callback),
user_data);
}

gtk_action_group_add_action (action_group, action);
gtk_action_set_visible (action, FALSE);
}

gint i = 0;
while (placeholder_paths[i] != NULL) {
g_autofree gchar *full_path = NULL;
const gchar *name;

if (action_path != NULL) {
full_path = g_strdup_printf ("%s/%s", placeholder_paths[i], action_path);
}
else {
full_path = g_strdup (placeholder_paths[i]);
}

if (type == GTK_UI_MANAGER_SEPARATOR) {
name = NULL;
}
else {
name = gtk_action_get_name (action);
}

gtk_ui_manager_add_ui (ui_manager,
merge_id,
full_path,
name,
name,
type,
FALSE);
i++;
}
}

void
nemo_action_manager_update_action_states (NemoActionManager *action_manager,
GtkActionGroup *action_group,
Expand Down
11 changes: 11 additions & 0 deletions libnemo-private/nemo-action-manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,16 @@ void nemo_action_manager_update_action_states (NemoActionM
gboolean for_places,
GtkWindow *window);

void nemo_action_manager_add_action_ui (NemoActionManager *manager,
GtkUIManager *ui_manager,
GtkAction *action,
const gchar *action_path,
GtkActionGroup *action_group,
guint merge_id,
const gchar **placeholder_paths,
GtkUIManagerItemType type,
GCallback activate_callback,
gpointer user_data);

#endif /* NEMO_ACTION_MANAGER_H */

58 changes: 15 additions & 43 deletions src/nemo-blank-desktop-window.c
Original file line number Diff line number Diff line change
Expand Up @@ -157,49 +157,21 @@ add_action_to_ui (NemoActionManager *manager,
{
NemoBlankDesktopWindow *window = NEMO_BLANK_DESKTOP_WINDOW (user_data);

if (type != GTK_UI_MANAGER_SEPARATOR) {
if (type == GTK_UI_MANAGER_MENUITEM) {
g_signal_handlers_disconnect_by_func (action,
run_action_callback,
window);

g_signal_connect (action, "activate",
G_CALLBACK (run_action_callback),
window);
}

gtk_action_group_add_action (window->details->action_group,
action);
gtk_action_set_visible (GTK_ACTION (action), FALSE);
}

const gchar *placeholder = "/background/BlankDesktopActionsPlaceholder";

g_autofree gchar *full_path = NULL;
const gchar *name;

if (path != NULL) {
full_path = g_strdup_printf ("%s/%s", placeholder, path);
}
else {
full_path = g_strdup (placeholder);
}


if (type == GTK_UI_MANAGER_SEPARATOR) {
name = NULL;
}
else {
name = gtk_action_get_name (action);
}

gtk_ui_manager_add_ui (window->details->ui_manager,
window->details->actions_merge_id,
full_path,
name,
name,
type,
FALSE);
const gchar *roots[] = {
"/background/BlankDesktopActionsPlaceholder",
NULL
};

nemo_action_manager_add_action_ui (manager,
window->details->ui_manager,
action,
path,
window->details->action_group,
window->details->actions_merge_id,
roots,
type,
G_CALLBACK (run_action_callback),
window);
}

static void
Expand Down
57 changes: 15 additions & 42 deletions src/nemo-places-sidebar.c
Original file line number Diff line number Diff line change
Expand Up @@ -3486,48 +3486,21 @@ add_action_to_ui (NemoActionManager *manager,
{
NemoPlacesSidebar *sidebar = NEMO_PLACES_SIDEBAR (user_data);

if (type != GTK_UI_MANAGER_SEPARATOR) {
if (type == GTK_UI_MANAGER_MENUITEM) {
g_signal_handlers_disconnect_by_func (action,
run_action_callback,
sidebar);

g_signal_connect (action, "activate",
G_CALLBACK (run_action_callback),
sidebar);
}

gtk_action_group_add_action (sidebar->action_action_group,
action);
gtk_action_set_visible (GTK_ACTION (action), FALSE);
}

const gchar *placeholder = "/selection/PlacesSidebarActionsPlaceholder";

g_autofree gchar *full_path = NULL;
const gchar *name;

if (path != NULL) {
full_path = g_strdup_printf ("%s/%s", placeholder, path);
}
else {
full_path = g_strdup (placeholder);
}

if (type == GTK_UI_MANAGER_SEPARATOR) {
name = NULL;
}
else {
name = gtk_action_get_name (action);
}

gtk_ui_manager_add_ui (sidebar->ui_manager,
sidebar->action_action_group_merge_id,
full_path,
name,
name,
type,
FALSE);
static const gchar *roots[] = {
"/selection/PlacesSidebarActionsPlaceholder",
NULL,
};

nemo_action_manager_add_action_ui (manager,
sidebar->ui_manager,
action,
path,
sidebar->action_action_group,
sidebar->action_action_group_merge_id,
roots,
type,
G_CALLBACK (run_action_callback),
sidebar);
}

static void
Expand Down
57 changes: 15 additions & 42 deletions src/nemo-tree-sidebar.c
Original file line number Diff line number Diff line change
Expand Up @@ -1315,48 +1315,21 @@ add_action_to_ui (NemoActionManager *manager,
{
FMTreeView *view = FM_TREE_VIEW (user_data);

if (type != GTK_UI_MANAGER_SEPARATOR) {
if (type == GTK_UI_MANAGER_MENUITEM) {
g_signal_handlers_disconnect_by_func (action,
run_action_callback,
view);

g_signal_connect (action, "activate",
G_CALLBACK (run_action_callback),
view);
}

gtk_action_group_add_action (view->details->action_action_group,
action);
gtk_action_set_visible (GTK_ACTION (action), FALSE);
}

const gchar *placeholder = "/selection/TreeSidebarActionsPlaceholder";

g_autofree gchar *full_path = NULL;
const gchar *name;

if (path != NULL) {
full_path = g_strdup_printf ("%s/%s", placeholder, path);
}
else {
full_path = g_strdup (placeholder);
}

if (type == GTK_UI_MANAGER_SEPARATOR) {
name = NULL;
}
else {
name = gtk_action_get_name (action);
}

gtk_ui_manager_add_ui (view->details->ui_manager,
view->details->action_action_group_merge_id,
full_path,
name,
name,
type,
FALSE);
const gchar *roots[] = {
"/selection/TreeSidebarActionsPlaceholder",
NULL
};

nemo_action_manager_add_action_ui (manager,
view->details->ui_manager,
action,
path,
view->details->action_action_group,
view->details->action_action_group_merge_id,
roots,
type,
G_CALLBACK (run_action_callback),
view);
}

static void
Expand Down
55 changes: 10 additions & 45 deletions src/nemo-view.c
Original file line number Diff line number Diff line change
Expand Up @@ -6352,58 +6352,23 @@ add_action_to_ui (NemoActionManager *manager,
{
NemoView *view = NEMO_VIEW (user_data);

if (type != GTK_UI_MANAGER_SEPARATOR) {
if (type == GTK_UI_MANAGER_MENUITEM) {
g_signal_handlers_disconnect_by_func (action,
run_action_callback,
view);

g_signal_connect (action, "activate",
G_CALLBACK (run_action_callback),
view);
}

gtk_action_group_add_action (view->details->actions_action_group,
action);
gtk_action_set_visible (GTK_ACTION (action), FALSE);
}

static const gchar *roots[] = {
NEMO_VIEW_MENU_PATH_ACTIONS_PLACEHOLDER,
NEMO_VIEW_POPUP_PATH_ACTIONS_PLACEHOLDER,
NEMO_VIEW_POPUP_PATH_BACKGROUND_ACTIONS_PLACEHOLDER,
NULL
};

gint i = 0;
while (roots[i] != NULL) {
g_autofree gchar *full_path = NULL;
const gchar *name;

if (path != NULL) {
full_path = g_strdup_printf ("%s/%s", roots[i], path);
}
else {
full_path = g_strdup (roots[i]);
}


if (type == GTK_UI_MANAGER_SEPARATOR) {
name = NULL;
}
else {
name = gtk_action_get_name (action);
}

gtk_ui_manager_add_ui (nemo_window_get_ui_manager (view->details->window),
view->details->actions_merge_id,
full_path,
name,
name,
type,
FALSE);
i++;
}
nemo_action_manager_add_action_ui (manager,
nemo_window_get_ui_manager (view->details->window),
action,
path,
view->details->actions_action_group,
view->details->actions_merge_id,
roots,
type,
G_CALLBACK (run_action_callback),
view);
}

static void
Expand Down

0 comments on commit 2eafca4

Please sign in to comment.