keyboard: unify standard_edit_box and custom_edit_box

The redundant design here seemed awkward when I was thinking about how
to implement support for displaying multiple conflicts as needed by
https://gitlab.gnome.org/GNOME/gnome-control-center/-/merge_requests/776
and https://gitlab.gnome.org/GNOME/gnome-control-center/-/issues/1041.

This should be more maintainable.
This commit is contained in:
Ian Douglas Scott 2020-07-01 10:23:04 -07:00
parent 107eb008a0
commit 7244bd997d
2 changed files with 88 additions and 153 deletions

View file

@ -32,7 +32,6 @@ struct _CcKeyboardShortcutEditor
GtkButton *cancel_button; GtkButton *cancel_button;
GtkButton *change_custom_shortcut_button; GtkButton *change_custom_shortcut_button;
GtkEntry *command_entry; GtkEntry *command_entry;
GtkBox *custom_edit_box;
GtkGrid *custom_grid; GtkGrid *custom_grid;
GtkShortcutLabel *custom_shortcut_accel_label; GtkShortcutLabel *custom_shortcut_accel_label;
GtkStack *custom_shortcut_stack; GtkStack *custom_shortcut_stack;
@ -48,8 +47,6 @@ struct _CcKeyboardShortcutEditor
GtkShortcutLabel *shortcut_accel_label; GtkShortcutLabel *shortcut_accel_label;
GtkLabel *shortcut_conflict_label; GtkLabel *shortcut_conflict_label;
GtkBox *standard_box; GtkBox *standard_box;
GtkBox *standard_edit_box;
GtkStack *standard_shortcut_stack;
GtkStack *stack; GtkStack *stack;
GtkLabel *top_info_label; GtkLabel *top_info_label;
@ -96,9 +93,8 @@ typedef enum
typedef enum typedef enum
{ {
PAGE_CUSTOM, PAGE_CUSTOM,
PAGE_CUSTOM_EDIT, PAGE_EDIT,
PAGE_STANDARD, PAGE_STANDARD,
PAGE_STANDARD_EDIT
} ShortcutEditorPage; } ShortcutEditorPage;
static GParamSpec *properties [N_PROPS] = { NULL, }; static GParamSpec *properties [N_PROPS] = { NULL, };
@ -107,18 +103,12 @@ static GParamSpec *properties [N_PROPS] = { NULL, };
static ShortcutEditorPage static ShortcutEditorPage
get_shortcut_editor_page (CcKeyboardShortcutEditor *self) get_shortcut_editor_page (CcKeyboardShortcutEditor *self)
{ {
if (gtk_stack_get_visible_child (self->stack) == GTK_WIDGET (self->custom_edit_box)) if (gtk_stack_get_visible_child (self->stack) == GTK_WIDGET (self->edit_box))
return PAGE_CUSTOM_EDIT; return PAGE_EDIT;
if (gtk_stack_get_visible_child (self->stack) == GTK_WIDGET (self->custom_grid)) if (gtk_stack_get_visible_child (self->stack) == GTK_WIDGET (self->custom_grid))
return PAGE_CUSTOM; return PAGE_CUSTOM;
if (gtk_stack_get_visible_child (self->stack) == GTK_WIDGET (self->edit_box) &&
gtk_stack_get_visible_child (self->standard_shortcut_stack) == GTK_WIDGET (self->standard_edit_box))
{
return PAGE_STANDARD_EDIT;
}
return PAGE_STANDARD; return PAGE_STANDARD;
} }
@ -132,23 +122,19 @@ set_shortcut_editor_page (CcKeyboardShortcutEditor *self,
gtk_stack_set_visible_child (self->stack, GTK_WIDGET (self->custom_grid)); gtk_stack_set_visible_child (self->stack, GTK_WIDGET (self->custom_grid));
break; break;
case PAGE_CUSTOM_EDIT: case PAGE_EDIT:
gtk_stack_set_visible_child (self->stack, GTK_WIDGET (self->custom_edit_box)); gtk_stack_set_visible_child (self->stack, GTK_WIDGET (self->edit_box));
break; break;
case PAGE_STANDARD: case PAGE_STANDARD:
gtk_stack_set_visible_child (self->stack, GTK_WIDGET (self->edit_box)); gtk_stack_set_visible_child (self->stack, GTK_WIDGET (self->standard_box));
gtk_stack_set_visible_child (self->standard_shortcut_stack, GTK_WIDGET (self->standard_box));
break;
case PAGE_STANDARD_EDIT:
gtk_stack_set_visible_child (self->stack, GTK_WIDGET (self->edit_box));
gtk_stack_set_visible_child (self->standard_shortcut_stack, GTK_WIDGET (self->standard_edit_box));
break; break;
default: default:
g_assert_not_reached (); g_assert_not_reached ();
} }
gtk_widget_set_visible (GTK_WIDGET (self->top_info_label), page != PAGE_CUSTOM);
} }
static void static void
@ -217,9 +203,8 @@ cancel_editing (CcKeyboardShortcutEditor *self)
} }
static gboolean static gboolean
is_custom_shortcut (CcKeyboardShortcutEditor *self) is_custom_shortcut (CcKeyboardShortcutEditor *self) {
{ return self->item == NULL || cc_keyboard_item_get_item_type (self->item) == CC_KEYBOARD_ITEM_TYPE_GSETTINGS_PATH;
return gtk_stack_get_visible_child (self->stack) != GTK_WIDGET (self->edit_box);
} }
static void static void
@ -483,7 +468,7 @@ static void
change_custom_shortcut_button_clicked_cb (CcKeyboardShortcutEditor *self) change_custom_shortcut_button_clicked_cb (CcKeyboardShortcutEditor *self)
{ {
grab_seat (self); grab_seat (self);
set_shortcut_editor_page (self, PAGE_CUSTOM_EDIT); set_shortcut_editor_page (self, PAGE_EDIT);
set_header_mode (self, HEADER_MODE_NONE); set_header_mode (self, HEADER_MODE_NONE);
} }
@ -558,8 +543,10 @@ setup_keyboard_item (CcKeyboardShortcutEditor *self,
g_autofree gchar *description_text = NULL; g_autofree gchar *description_text = NULL;
g_autofree gchar *text = NULL; g_autofree gchar *text = NULL;
if (!item) if (!item) {
gtk_label_set_text (self->top_info_label, _("Enter the new shortcut"));
return; return;
}
combo = cc_keyboard_item_get_primary_combo (item); combo = cc_keyboard_item_get_primary_combo (item);
is_custom = cc_keyboard_item_get_item_type (item) == CC_KEYBOARD_ITEM_TYPE_GSETTINGS_PATH; is_custom = cc_keyboard_item_get_item_type (item) == CC_KEYBOARD_ITEM_TYPE_GSETTINGS_PATH;
@ -631,7 +618,7 @@ setup_keyboard_item (CcKeyboardShortcutEditor *self,
} }
/* Show the apropriate view */ /* Show the apropriate view */
set_shortcut_editor_page (self, is_custom ? PAGE_CUSTOM : PAGE_STANDARD_EDIT); set_shortcut_editor_page (self, is_custom ? PAGE_CUSTOM : PAGE_EDIT);
} }
static void static void
@ -707,8 +694,7 @@ cc_keyboard_shortcut_editor_key_press_event (GtkWidget *widget,
/* Being in the "change-shortcut" page is the only check we must /* Being in the "change-shortcut" page is the only check we must
* perform to decide if we're editing a shortcut. */ * perform to decide if we're editing a shortcut. */
editing = get_shortcut_editor_page (self) == PAGE_CUSTOM_EDIT || editing = get_shortcut_editor_page (self) == PAGE_EDIT;
get_shortcut_editor_page (self) == PAGE_STANDARD_EDIT;
if (!editing) if (!editing)
return GTK_WIDGET_CLASS (cc_keyboard_shortcut_editor_parent_class)->key_press_event (widget, event); return GTK_WIDGET_CLASS (cc_keyboard_shortcut_editor_parent_class)->key_press_event (widget, event);
@ -887,7 +873,6 @@ cc_keyboard_shortcut_editor_class_init (CcKeyboardShortcutEditorClass *klass)
gtk_widget_class_bind_template_child (widget_class, CcKeyboardShortcutEditor, cancel_button); gtk_widget_class_bind_template_child (widget_class, CcKeyboardShortcutEditor, cancel_button);
gtk_widget_class_bind_template_child (widget_class, CcKeyboardShortcutEditor, change_custom_shortcut_button); gtk_widget_class_bind_template_child (widget_class, CcKeyboardShortcutEditor, change_custom_shortcut_button);
gtk_widget_class_bind_template_child (widget_class, CcKeyboardShortcutEditor, command_entry); gtk_widget_class_bind_template_child (widget_class, CcKeyboardShortcutEditor, command_entry);
gtk_widget_class_bind_template_child (widget_class, CcKeyboardShortcutEditor, custom_edit_box);
gtk_widget_class_bind_template_child (widget_class, CcKeyboardShortcutEditor, custom_grid); gtk_widget_class_bind_template_child (widget_class, CcKeyboardShortcutEditor, custom_grid);
gtk_widget_class_bind_template_child (widget_class, CcKeyboardShortcutEditor, custom_shortcut_accel_label); gtk_widget_class_bind_template_child (widget_class, CcKeyboardShortcutEditor, custom_shortcut_accel_label);
gtk_widget_class_bind_template_child (widget_class, CcKeyboardShortcutEditor, custom_shortcut_stack); gtk_widget_class_bind_template_child (widget_class, CcKeyboardShortcutEditor, custom_shortcut_stack);
@ -903,8 +888,6 @@ cc_keyboard_shortcut_editor_class_init (CcKeyboardShortcutEditorClass *klass)
gtk_widget_class_bind_template_child (widget_class, CcKeyboardShortcutEditor, shortcut_accel_label); gtk_widget_class_bind_template_child (widget_class, CcKeyboardShortcutEditor, shortcut_accel_label);
gtk_widget_class_bind_template_child (widget_class, CcKeyboardShortcutEditor, shortcut_conflict_label); gtk_widget_class_bind_template_child (widget_class, CcKeyboardShortcutEditor, shortcut_conflict_label);
gtk_widget_class_bind_template_child (widget_class, CcKeyboardShortcutEditor, standard_box); gtk_widget_class_bind_template_child (widget_class, CcKeyboardShortcutEditor, standard_box);
gtk_widget_class_bind_template_child (widget_class, CcKeyboardShortcutEditor, standard_edit_box);
gtk_widget_class_bind_template_child (widget_class, CcKeyboardShortcutEditor, standard_shortcut_stack);
gtk_widget_class_bind_template_child (widget_class, CcKeyboardShortcutEditor, stack); gtk_widget_class_bind_template_child (widget_class, CcKeyboardShortcutEditor, stack);
gtk_widget_class_bind_template_child (widget_class, CcKeyboardShortcutEditor, top_info_label); gtk_widget_class_bind_template_child (widget_class, CcKeyboardShortcutEditor, top_info_label);
@ -978,11 +961,11 @@ cc_keyboard_shortcut_editor_set_item (CcKeyboardShortcutEditor *self,
{ {
g_return_if_fail (CC_IS_KEYBOARD_SHORTCUT_EDITOR (self)); g_return_if_fail (CC_IS_KEYBOARD_SHORTCUT_EDITOR (self));
setup_keyboard_item (self, item);
if (!g_set_object (&self->item, item)) if (!g_set_object (&self->item, item))
return; return;
setup_keyboard_item (self, item);
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_KEYBOARD_ITEM]); g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_KEYBOARD_ITEM]);
} }

View file

@ -13,21 +13,10 @@
<signal name="delete-event" handler="gtk_widget_hide_on_delete" object="CcKeyboardShortcutEditor" swapped="yes"/> <signal name="delete-event" handler="gtk_widget_hide_on_delete" object="CcKeyboardShortcutEditor" swapped="yes"/>
<child internal-child="vbox"> <child internal-child="vbox">
<object class="GtkBox"> <object class="GtkBox">
<property name="can_focus">False</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkStack" id="stack">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="hexpand">True</property>
<property name="vexpand">True</property>
<property name="border_width">12</property>
<child>
<object class="GtkBox" id="edit_box">
<property name="visible">True</property>
<property name="can_focus">False</property> <property name="can_focus">False</property>
<property name="orientation">vertical</property> <property name="orientation">vertical</property>
<property name="spacing">18</property> <property name="spacing">18</property>
<property name="border_width">12</property>
<child> <child>
<object class="GtkLabel" id="top_info_label"> <object class="GtkLabel" id="top_info_label">
<property name="visible">True</property> <property name="visible">True</property>
@ -37,15 +26,18 @@
<property name="width_chars">15</property> <property name="width_chars">15</property>
<property name="max_width_chars">20</property> <property name="max_width_chars">20</property>
</object> </object>
<packing>
<property name="position">0</property>
</packing>
</child> </child>
<child> <child>
<object class="GtkStack" id="standard_shortcut_stack"> <object class="GtkStack" id="stack">
<property name="visible">True</property> <property name="visible">True</property>
<property name="can_focus">False</property> <property name="can_focus">False</property>
<property name="expand">True</property> <property name="hexpand">True</property>
<property name="transition_type">crossfade</property> <property name="vexpand">True</property>
<child> <child>
<object class="GtkBox" id="standard_edit_box"> <object class="GtkBox" id="edit_box">
<property name="visible">True</property> <property name="visible">True</property>
<property name="can_focus">False</property> <property name="can_focus">False</property>
<property name="orientation">vertical</property> <property name="orientation">vertical</property>
@ -128,10 +120,6 @@
</child> </child>
</object> </object>
</child> </child>
</object>
</child>
</object>
</child>
<child> <child>
<object class="GtkGrid" id="custom_grid"> <object class="GtkGrid" id="custom_grid">
<property name="visible">True</property> <property name="visible">True</property>
@ -268,42 +256,6 @@
</child> </child>
</object> </object>
</child> </child>
<child>
<object class="GtkBox" id="custom_edit_box">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="orientation">vertical</property>
<property name="spacing">18</property>
<property name="expand">True</property>
<property name="halign">center</property>
<property name="valign">center</property>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Enter the new shortcut</property>
</object>
</child>
<child>
<object class="GtkImage">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="resource">/org/gnome/control-center/keyboard/enter-keyboard-shortcut.svg</property>
</object>
</child>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="wrap">True</property>
<property name="label" translatable="yes">Press Esc to cancel or Backspace to disable the keyboard shortcut.</property>
<style>
<class name="dim-label" />
</style>
</object>
</child>
</object>
</child>
</object> </object>
<packing> <packing>
<property name="expand">False</property> <property name="expand">False</property>