keyboard: Use GTK widget types

This commit is contained in:
Robert Ancell 2019-11-20 12:59:46 +13:00 committed by Robert Ancell
parent f00a5e20cb
commit ee4d08ca1d

View file

@ -28,24 +28,24 @@ struct _CcKeyboardShortcutEditor
{ {
GtkDialog parent; GtkDialog parent;
GtkWidget *add_button; GtkButton *add_button;
GtkWidget *cancel_button; GtkButton *cancel_button;
GtkWidget *command_entry; GtkEntry *command_entry;
GtkWidget *custom_shortcut_accel_label; GtkShortcutLabel *custom_shortcut_accel_label;
GtkWidget *custom_shortcut_stack; GtkStack *custom_shortcut_stack;
GtkWidget *headerbar; GtkHeaderBar *headerbar;
GtkWidget *name_entry; GtkEntry *name_entry;
GtkWidget *new_shortcut_conflict_label; GtkLabel *new_shortcut_conflict_label;
GtkWidget *remove_button; GtkButton *remove_button;
GtkWidget *replace_button; GtkButton *replace_button;
GtkWidget *reset_button; GtkButton *reset_button;
GtkWidget *reset_custom_button; GtkButton *reset_custom_button;
GtkWidget *set_button; GtkButton *set_button;
GtkWidget *shortcut_accel_label; GtkShortcutLabel *shortcut_accel_label;
GtkWidget *shortcut_conflict_label; GtkLabel *shortcut_conflict_label;
GtkWidget *standard_shortcut_stack; GtkStack *standard_shortcut_stack;
GtkWidget *stack; GtkStack *stack;
GtkWidget *top_info_label; GtkLabel *top_info_label;
CcShortcutEditorMode mode; CcShortcutEditorMode mode;
@ -101,14 +101,14 @@ static GParamSpec *properties [N_PROPS] = { NULL, };
static ShortcutEditorPage static ShortcutEditorPage
get_shortcut_editor_page (CcKeyboardShortcutEditor *self) get_shortcut_editor_page (CcKeyboardShortcutEditor *self)
{ {
if (g_str_equal (gtk_stack_get_visible_child_name (GTK_STACK (self->stack)), "change-shortcut")) if (g_str_equal (gtk_stack_get_visible_child_name (self->stack), "change-shortcut"))
return PAGE_CUSTOM_EDIT; return PAGE_CUSTOM_EDIT;
if (g_str_equal (gtk_stack_get_visible_child_name (GTK_STACK (self->stack)), "custom")) if (g_str_equal (gtk_stack_get_visible_child_name (self->stack), "custom"))
return PAGE_CUSTOM; return PAGE_CUSTOM;
if (g_str_equal (gtk_stack_get_visible_child_name (GTK_STACK (self->stack)), "edit") && if (g_str_equal (gtk_stack_get_visible_child_name (self->stack), "edit") &&
g_str_equal (gtk_stack_get_visible_child_name (GTK_STACK (self->standard_shortcut_stack)), "change-shortcut")) g_str_equal (gtk_stack_get_visible_child_name (self->standard_shortcut_stack), "change-shortcut"))
{ {
return PAGE_STANDARD_EDIT; return PAGE_STANDARD_EDIT;
} }
@ -123,21 +123,21 @@ set_shortcut_editor_page (CcKeyboardShortcutEditor *self,
switch (page) switch (page)
{ {
case PAGE_CUSTOM: case PAGE_CUSTOM:
gtk_stack_set_visible_child_name (GTK_STACK (self->stack), "custom"); gtk_stack_set_visible_child_name (self->stack, "custom");
break; break;
case PAGE_CUSTOM_EDIT: case PAGE_CUSTOM_EDIT:
gtk_stack_set_visible_child_name (GTK_STACK (self->stack), "change-shortcut"); gtk_stack_set_visible_child_name (self->stack, "change-shortcut");
break; break;
case PAGE_STANDARD: case PAGE_STANDARD:
gtk_stack_set_visible_child_name (GTK_STACK (self->stack), "edit"); gtk_stack_set_visible_child_name (self->stack, "edit");
gtk_stack_set_visible_child_name (GTK_STACK (self->standard_shortcut_stack), "main"); gtk_stack_set_visible_child_name (self->standard_shortcut_stack, "main");
break; break;
case PAGE_STANDARD_EDIT: case PAGE_STANDARD_EDIT:
gtk_stack_set_visible_child_name (GTK_STACK (self->stack), "edit"); gtk_stack_set_visible_child_name (self->stack, "edit");
gtk_stack_set_visible_child_name (GTK_STACK (self->standard_shortcut_stack), "change-shortcut"); gtk_stack_set_visible_child_name (self->standard_shortcut_stack, "change-shortcut");
break; break;
default: default:
@ -173,8 +173,8 @@ apply_custom_item_fields (CcKeyboardShortcutEditor *self,
/* Set the keyboard shortcut name and command for custom entries */ /* Set the keyboard shortcut name and command for custom entries */
if (cc_keyboard_item_get_item_type (item) == CC_KEYBOARD_ITEM_TYPE_GSETTINGS_PATH) if (cc_keyboard_item_get_item_type (item) == CC_KEYBOARD_ITEM_TYPE_GSETTINGS_PATH)
{ {
g_settings_set_string (cc_keyboard_item_get_settings (item), "name", gtk_entry_get_text (GTK_ENTRY (self->name_entry))); g_settings_set_string (cc_keyboard_item_get_settings (item), "name", gtk_entry_get_text (self->name_entry));
g_settings_set_string (cc_keyboard_item_get_settings (item), "command", gtk_entry_get_text (GTK_ENTRY (self->command_entry))); g_settings_set_string (cc_keyboard_item_get_settings (item), "command", gtk_entry_get_text (self->command_entry));
} }
} }
@ -184,12 +184,12 @@ clear_custom_entries (CcKeyboardShortcutEditor *self)
g_signal_handlers_block_by_func (self->command_entry, command_entry_changed_cb, self); g_signal_handlers_block_by_func (self->command_entry, command_entry_changed_cb, self);
g_signal_handlers_block_by_func (self->name_entry, name_entry_changed_cb, self); g_signal_handlers_block_by_func (self->name_entry, name_entry_changed_cb, self);
gtk_entry_set_text (GTK_ENTRY (self->name_entry), ""); gtk_entry_set_text (self->name_entry, "");
gtk_entry_set_text (GTK_ENTRY (self->command_entry), ""); gtk_entry_set_text (self->command_entry, "");
gtk_shortcut_label_set_accelerator (GTK_SHORTCUT_LABEL (self->custom_shortcut_accel_label), ""); gtk_shortcut_label_set_accelerator (GTK_SHORTCUT_LABEL (self->custom_shortcut_accel_label), "");
gtk_label_set_label (GTK_LABEL (self->new_shortcut_conflict_label), ""); gtk_label_set_label (self->new_shortcut_conflict_label, "");
gtk_label_set_label (GTK_LABEL (self->shortcut_conflict_label), ""); gtk_label_set_label (self->shortcut_conflict_label, "");
memset (self->custom_combo, 0, sizeof (CcKeyCombo)); memset (self->custom_combo, 0, sizeof (CcKeyCombo));
self->custom_is_modifier = TRUE; self->custom_is_modifier = TRUE;
@ -213,7 +213,7 @@ cancel_editing (CcKeyboardShortcutEditor *self)
static gboolean static gboolean
is_custom_shortcut (CcKeyboardShortcutEditor *self) is_custom_shortcut (CcKeyboardShortcutEditor *self)
{ {
return !g_str_equal (gtk_stack_get_visible_child_name (GTK_STACK (self->stack)), "edit"); return !g_str_equal (gtk_stack_get_visible_child_name (self->stack), "edit");
} }
static void static void
@ -293,14 +293,14 @@ static void
set_header_mode (CcKeyboardShortcutEditor *self, set_header_mode (CcKeyboardShortcutEditor *self,
HeaderMode mode) HeaderMode mode)
{ {
gtk_header_bar_set_show_close_button (GTK_HEADER_BAR (self->headerbar), mode == HEADER_MODE_CUSTOM_EDIT); gtk_header_bar_set_show_close_button (self->headerbar, mode == HEADER_MODE_CUSTOM_EDIT);
gtk_widget_set_visible (self->add_button, mode == HEADER_MODE_ADD); gtk_widget_set_visible (GTK_WIDGET (self->add_button), mode == HEADER_MODE_ADD);
gtk_widget_set_visible (self->cancel_button, mode != HEADER_MODE_NONE && gtk_widget_set_visible (GTK_WIDGET (self->cancel_button), mode != HEADER_MODE_NONE &&
mode != HEADER_MODE_CUSTOM_EDIT); mode != HEADER_MODE_CUSTOM_EDIT);
gtk_widget_set_visible (self->replace_button, mode == HEADER_MODE_REPLACE); gtk_widget_set_visible (GTK_WIDGET (self->replace_button), mode == HEADER_MODE_REPLACE);
gtk_widget_set_visible (self->set_button, mode == HEADER_MODE_SET); gtk_widget_set_visible (GTK_WIDGET (self->set_button), mode == HEADER_MODE_SET);
gtk_widget_set_visible (self->remove_button, mode == HEADER_MODE_CUSTOM_EDIT); gtk_widget_set_visible (GTK_WIDGET (self->remove_button), mode == HEADER_MODE_CUSTOM_EDIT);
/* By setting the default response, the action button gets the 'suggested-action' applied */ /* By setting the default response, the action button gets the 'suggested-action' applied */
switch (mode) switch (mode)
@ -352,18 +352,18 @@ setup_custom_shortcut (CcKeyboardShortcutEditor *self)
/* We have to check if the current accelerator is empty in order to /* We have to check if the current accelerator is empty in order to
* decide if we show the "Set Shortcut" button or the accelerator label */ * decide if we show the "Set Shortcut" button or the accelerator label */
gtk_stack_set_visible_child_name (GTK_STACK (self->custom_shortcut_stack), gtk_stack_set_visible_child_name (self->custom_shortcut_stack,
is_accel_empty ? "button" : "label"); is_accel_empty ? "button" : "label");
gtk_widget_set_visible (self->reset_custom_button, !is_accel_empty); gtk_widget_set_visible (GTK_WIDGET (self->reset_custom_button), !is_accel_empty);
} }
valid = accel_valid && valid = accel_valid &&
gtk_entry_get_text_length (GTK_ENTRY (self->name_entry)) > 0 && gtk_entry_get_text_length (self->name_entry) > 0 &&
gtk_entry_get_text_length (GTK_ENTRY (self->command_entry)) > 0; gtk_entry_get_text_length (self->command_entry) > 0;
} }
gtk_widget_set_sensitive (self->replace_button, valid); gtk_widget_set_sensitive (GTK_WIDGET (self->replace_button), valid);
gtk_widget_set_sensitive (self->add_button, valid); gtk_widget_set_sensitive (GTK_WIDGET (self->add_button), valid);
if (valid) if (valid)
set_header_mode (self, HEADER_MODE_ADD); set_header_mode (self, HEADER_MODE_ADD);
else else
@ -398,11 +398,11 @@ setup_custom_shortcut (CcKeyboardShortcutEditor *self)
* must warn the user and let it be very clear that adding this * must warn the user and let it be very clear that adding this
* shortcut will disable the other. * shortcut will disable the other.
*/ */
gtk_widget_set_visible (self->new_shortcut_conflict_label, collision_item != NULL); gtk_widget_set_visible (GTK_WIDGET (self->new_shortcut_conflict_label), collision_item != NULL);
if (collision_item) if (collision_item)
{ {
GtkWidget *label; GtkLabel *label;
g_autofree gchar *friendly_accelerator = NULL; g_autofree gchar *friendly_accelerator = NULL;
g_autofree gchar *accelerator_text = NULL; g_autofree gchar *accelerator_text = NULL;
g_autofree gchar *collision_text = NULL; g_autofree gchar *collision_text = NULL;
@ -418,7 +418,7 @@ setup_custom_shortcut (CcKeyboardShortcutEditor *self)
label = is_custom_shortcut (self) ? self->new_shortcut_conflict_label : self->shortcut_conflict_label; label = is_custom_shortcut (self) ? self->new_shortcut_conflict_label : self->shortcut_conflict_label;
gtk_label_set_markup (GTK_LABEL (label), collision_text); gtk_label_set_markup (label, collision_text);
} }
/* /*
@ -525,8 +525,8 @@ reset_custom_clicked_cb (CcKeyboardShortcutEditor *self)
if (self->item) if (self->item)
cc_keyboard_manager_reset_shortcut (self->manager, self->item); cc_keyboard_manager_reset_shortcut (self->manager, self->item);
gtk_stack_set_visible_child_name (GTK_STACK (self->custom_shortcut_stack), "button"); gtk_stack_set_visible_child_name (self->custom_shortcut_stack, "button");
gtk_widget_hide (self->reset_custom_button); gtk_widget_hide (GTK_WIDGET (self->reset_custom_button));
} }
static void static void
@ -577,25 +577,25 @@ setup_keyboard_item (CcKeyboardShortcutEditor *self,
self->custom_combo->mask = combo->mask; self->custom_combo->mask = combo->mask;
/* Headerbar */ /* Headerbar */
gtk_header_bar_set_title (GTK_HEADER_BAR (self->headerbar), gtk_header_bar_set_title (self->headerbar,
is_custom ? _("Set Custom Shortcut") : _("Set Shortcut")); is_custom ? _("Set Custom Shortcut") : _("Set Shortcut"));
set_header_mode (self, is_custom ? HEADER_MODE_CUSTOM_EDIT : HEADER_MODE_NONE); set_header_mode (self, is_custom ? HEADER_MODE_CUSTOM_EDIT : HEADER_MODE_NONE);
gtk_widget_hide (self->add_button); gtk_widget_hide (GTK_WIDGET (self->add_button));
gtk_widget_hide (self->cancel_button); gtk_widget_hide (GTK_WIDGET (self->cancel_button));
gtk_widget_hide (self->replace_button); gtk_widget_hide (GTK_WIDGET (self->replace_button));
/* Setup the top label */ /* Setup the top label */
description_text = g_strdup_printf ("<b>%s</b>", cc_keyboard_item_get_description (item)); description_text = g_strdup_printf ("<b>%s</b>", cc_keyboard_item_get_description (item));
/* TRANSLATORS: %s is replaced with a description of the keyboard shortcut */ /* TRANSLATORS: %s is replaced with a description of the keyboard shortcut */
text = g_strdup_printf (_("Enter new shortcut to change %s."), description_text); text = g_strdup_printf (_("Enter new shortcut to change %s."), description_text);
gtk_label_set_markup (GTK_LABEL (self->top_info_label), text); gtk_label_set_markup (self->top_info_label, text);
/* Accelerator labels */ /* Accelerator labels */
gtk_shortcut_label_set_accelerator (GTK_SHORTCUT_LABEL (self->shortcut_accel_label), accel); gtk_shortcut_label_set_accelerator (self->shortcut_accel_label, accel);
gtk_shortcut_label_set_accelerator (GTK_SHORTCUT_LABEL (self->custom_shortcut_accel_label), accel); gtk_shortcut_label_set_accelerator (self->custom_shortcut_accel_label, accel);
g_clear_pointer (&self->reset_item_binding, g_binding_unbind); g_clear_pointer (&self->reset_item_binding, g_binding_unbind);
self->reset_item_binding = g_object_bind_property (item, self->reset_item_binding = g_object_bind_property (item,
@ -613,20 +613,20 @@ setup_keyboard_item (CcKeyboardShortcutEditor *self,
g_signal_handlers_block_by_func (self->name_entry, name_entry_changed_cb, self); g_signal_handlers_block_by_func (self->name_entry, name_entry_changed_cb, self);
/* Name entry */ /* Name entry */
gtk_entry_set_text (GTK_ENTRY (self->name_entry), cc_keyboard_item_get_description (item)); gtk_entry_set_text (self->name_entry, cc_keyboard_item_get_description (item));
gtk_widget_set_sensitive (self->name_entry, cc_keyboard_item_get_desc_editable (item)); gtk_widget_set_sensitive (GTK_WIDGET (self->name_entry), cc_keyboard_item_get_desc_editable (item));
/* Command entry */ /* Command entry */
gtk_entry_set_text (GTK_ENTRY (self->command_entry), cc_keyboard_item_get_command (item)); gtk_entry_set_text (self->command_entry, cc_keyboard_item_get_command (item));
gtk_widget_set_sensitive (self->command_entry, cc_keyboard_item_get_cmd_editable (item)); gtk_widget_set_sensitive (GTK_WIDGET (self->command_entry), cc_keyboard_item_get_cmd_editable (item));
/* If there is no accelerator set for this custom shortcut, show the "Set Shortcut" button. */ /* If there is no accelerator set for this custom shortcut, show the "Set Shortcut" button. */
is_accel_empty = !accel || accel[0] == '\0'; is_accel_empty = !accel || accel[0] == '\0';
gtk_stack_set_visible_child_name (GTK_STACK (self->custom_shortcut_stack), gtk_stack_set_visible_child_name (self->custom_shortcut_stack,
is_accel_empty ? "button" : "label"); is_accel_empty ? "button" : "label");
gtk_widget_set_visible (self->reset_custom_button, !is_accel_empty); gtk_widget_set_visible (GTK_WIDGET (self->reset_custom_button), !is_accel_empty);
g_signal_handlers_unblock_by_func (self->command_entry, command_entry_changed_cb, self); g_signal_handlers_unblock_by_func (self->command_entry, command_entry_changed_cb, self);
g_signal_handlers_unblock_by_func (self->name_entry, name_entry_changed_cb, self); g_signal_handlers_unblock_by_func (self->name_entry, name_entry_changed_cb, self);
@ -926,8 +926,8 @@ cc_keyboard_shortcut_editor_init (CcKeyboardShortcutEditor *self)
self->custom_is_modifier = TRUE; self->custom_is_modifier = TRUE;
self->custom_combo = g_new0 (CcKeyCombo, 1); self->custom_combo = g_new0 (CcKeyCombo, 1);
gtk_widget_set_direction (self->custom_shortcut_accel_label, GTK_TEXT_DIR_LTR); gtk_widget_set_direction (GTK_WIDGET (self->custom_shortcut_accel_label), GTK_TEXT_DIR_LTR);
gtk_widget_set_direction (self->shortcut_accel_label, GTK_TEXT_DIR_LTR); gtk_widget_set_direction (GTK_WIDGET (self->shortcut_accel_label), GTK_TEXT_DIR_LTR);
} }
/** /**
@ -1002,8 +1002,8 @@ cc_keyboard_shortcut_editor_set_mode (CcKeyboardShortcutEditor *self,
self->mode = mode; self->mode = mode;
is_create_mode = mode == CC_SHORTCUT_EDITOR_CREATE; is_create_mode = mode == CC_SHORTCUT_EDITOR_CREATE;
gtk_widget_set_visible (self->new_shortcut_conflict_label, is_create_mode); gtk_widget_set_visible (GTK_WIDGET (self->new_shortcut_conflict_label), is_create_mode);
gtk_stack_set_visible_child_name (GTK_STACK (self->custom_shortcut_stack), gtk_stack_set_visible_child_name (self->custom_shortcut_stack,
is_create_mode ? "button" : "label"); is_create_mode ? "button" : "label");
if (mode == CC_SHORTCUT_EDITOR_CREATE) if (mode == CC_SHORTCUT_EDITOR_CREATE)
@ -1013,12 +1013,12 @@ cc_keyboard_shortcut_editor_set_mode (CcKeyboardShortcutEditor *self,
set_header_mode (self, HEADER_MODE_ADD); set_header_mode (self, HEADER_MODE_ADD);
set_shortcut_editor_page (self, PAGE_CUSTOM); set_shortcut_editor_page (self, PAGE_CUSTOM);
gtk_header_bar_set_title (GTK_HEADER_BAR (self->headerbar), _("Add Custom Shortcut")); gtk_header_bar_set_title (self->headerbar, _("Add Custom Shortcut"));
gtk_widget_set_sensitive (self->command_entry, TRUE); gtk_widget_set_sensitive (GTK_WIDGET (self->command_entry), TRUE);
gtk_widget_set_sensitive (self->name_entry, TRUE); gtk_widget_set_sensitive (GTK_WIDGET (self->name_entry), TRUE);
gtk_widget_set_sensitive (self->add_button, FALSE); gtk_widget_set_sensitive (GTK_WIDGET (self->add_button), FALSE);
gtk_widget_hide (self->reset_custom_button); gtk_widget_hide (GTK_WIDGET (self->reset_custom_button));
} }
} }