Commit a86cf1ec authored by Robert Ancell's avatar Robert Ancell Committed by Georges Basile Stavracas Neto

region: Use a GtkPopover instead of GtkDialog for input options

As per design:
https://wiki.gnome.org/Design/SystemSettings/RegionAndLanguage
parent 6ae351ff
Pipeline #30652 passed with stages
in 3 minutes and 20 seconds
/*
* Copyright (C) 2013, 2015 Red Hat, Inc.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*
* Written by:
* Matthias Clasen
*/
#include <config.h>
#include <glib/gi18n.h>
#define GNOME_DESKTOP_USE_UNSTABLE_API
#include <libgnome-desktop/gnome-xkb-info.h>
#include "cc-input-options.h"
struct _CcInputOptions {
GtkDialog parent_instance;
GtkWidget *same_source;
GtkWidget *per_window_source;
GtkWidget *previous_source;
GtkWidget *previous_source_label;
GtkWidget *next_source;
GtkWidget *next_source_label;
GtkWidget *alt_next_source;
GtkWidget *alt_next_source_label;
GSettings *settings;
};
G_DEFINE_TYPE (CcInputOptions, cc_input_options, GTK_TYPE_DIALOG);
static void
update_shortcut_label (GtkWidget *widget,
const gchar *value)
{
g_autofree gchar *text = NULL;
guint accel_key;
g_autofree guint *keycode = NULL;
GdkModifierType mods;
if (value == NULL || *value == '\0') {
gtk_widget_hide (widget);
return;
}
gtk_accelerator_parse_with_keycode (value, &accel_key, &keycode, &mods);
if (accel_key == 0 && keycode == NULL && mods == 0) {
g_warning ("Failed to parse keyboard shortcut: '%s'", value);
gtk_widget_hide (widget);
return;
}
text = gtk_accelerator_get_label_with_keycode (gtk_widget_get_display (widget), accel_key, *keycode, mods);
gtk_label_set_text (GTK_LABEL (widget), text);
}
static void
update_shortcuts (CcInputOptions *self)
{
g_auto(GStrv) previous = NULL;
g_auto(GStrv) next = NULL;
g_autofree gchar *previous_shortcut = NULL;
g_autoptr(GSettings) settings = NULL;
settings = g_settings_new ("org.gnome.desktop.wm.keybindings");
previous = g_settings_get_strv (settings, "switch-input-source-backward");
next = g_settings_get_strv (settings, "switch-input-source");
previous_shortcut = g_strdup (previous[0]);
update_shortcut_label (self->previous_source, previous_shortcut);
update_shortcut_label (self->next_source, next[0]);
}
static void
update_modifiers_shortcut (CcInputOptions *self)
{
g_auto(GStrv) options = NULL;
gchar **p;
g_autoptr(GSettings) settings = NULL;
g_autoptr(GnomeXkbInfo) xkb_info = NULL;
const gchar *text;
xkb_info = gnome_xkb_info_new ();
settings = g_settings_new ("org.gnome.desktop.input-sources");
options = g_settings_get_strv (settings, "xkb-options");
for (p = options; p && *p; ++p)
if (g_str_has_prefix (*p, "grp:"))
break;
if (p && *p) {
text = gnome_xkb_info_description_for_option (xkb_info, "grp", *p);
gtk_label_set_text (GTK_LABEL (self->alt_next_source), text);
} else {
gtk_widget_hide (self->alt_next_source);
}
}
static void
cc_input_options_finalize (GObject *object)
{
CcInputOptions *self = CC_INPUT_OPTIONS (object);
g_object_unref (self->settings);
G_OBJECT_CLASS (cc_input_options_parent_class)->finalize (object);
}
static void
cc_input_options_init (CcInputOptions *self)
{
gtk_widget_init_template (GTK_WIDGET (self));
g_object_bind_property (self->previous_source, "visible",
self->previous_source_label, "visible",
G_BINDING_DEFAULT);
g_object_bind_property (self->next_source, "visible",
self->next_source_label, "visible",
G_BINDING_DEFAULT);
g_object_bind_property (self->alt_next_source, "visible",
self->alt_next_source_label, "visible",
G_BINDING_DEFAULT);
self->settings = g_settings_new ("org.gnome.desktop.input-sources");
g_settings_bind (self->settings, "per-window",
self->per_window_source, "active",
G_SETTINGS_BIND_DEFAULT);
g_settings_bind (self->settings, "per-window",
self->same_source, "active",
G_SETTINGS_BIND_DEFAULT | G_SETTINGS_BIND_INVERT_BOOLEAN);
update_shortcuts (self);
update_modifiers_shortcut (self);
}
static void
cc_input_options_class_init (CcInputOptionsClass *klass)
{
GObjectClass *object_klass = G_OBJECT_CLASS (klass);
GtkWidgetClass *widget_klass = GTK_WIDGET_CLASS (klass);
object_klass->finalize = cc_input_options_finalize;
gtk_widget_class_set_template_from_resource (widget_klass,
"/org/gnome/control-center/region/input-options.ui");
gtk_widget_class_bind_template_child (widget_klass, CcInputOptions, same_source);
gtk_widget_class_bind_template_child (widget_klass, CcInputOptions, per_window_source);
gtk_widget_class_bind_template_child (widget_klass, CcInputOptions, previous_source);
gtk_widget_class_bind_template_child (widget_klass, CcInputOptions, previous_source_label);
gtk_widget_class_bind_template_child (widget_klass, CcInputOptions, next_source);
gtk_widget_class_bind_template_child (widget_klass, CcInputOptions, next_source_label);
gtk_widget_class_bind_template_child (widget_klass, CcInputOptions, alt_next_source);
gtk_widget_class_bind_template_child (widget_klass, CcInputOptions, alt_next_source_label);
}
GtkWidget *
cc_input_options_new (GtkWidget *parent)
{
return g_object_new (CC_TYPE_INPUT_OPTIONS, "transient-for", parent, "use-header-bar", TRUE, NULL);
}
/*
* Copyright (C) 2013, 2015 Red Hat, Inc
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*
* Written by:
* Matthias Clasen
*/
#ifndef __CC_INPUT_OPTIONS_H__
#define __CC_INPUT_OPTIONS_H__
#include <gtk/gtk.h>
#include <glib-object.h>
G_BEGIN_DECLS
#define CC_TYPE_INPUT_OPTIONS (cc_input_options_get_type ())
G_DECLARE_FINAL_TYPE (CcInputOptions, cc_input_options, CC, INPUT_OPTIONS, GtkDialog)
GtkWidget *cc_input_options_new (GtkWidget *parent);
G_END_DECLS
#endif /* __CC_FORMAT_CHOOSER_H__ */
......@@ -32,7 +32,6 @@
#include "cc-language-chooser.h"
#include "cc-format-chooser.h"
#include "cc-input-chooser.h"
#include "cc-input-options.h"
#include "cc-input-row.h"
#include "cc-common-language.h"
......@@ -109,6 +108,14 @@ struct _CcRegionPanel {
GtkWidget *show_layout;
GtkWidget *restart_button;
GtkWidget *language_list;
GtkWidget *same_source;
GtkWidget *per_window_source;
GtkWidget *previous_source;
GtkWidget *previous_source_label;
GtkWidget *next_source;
GtkWidget *next_source_label;
GtkWidget *alt_next_source;
GtkWidget *alt_next_source_label;
GSettings *input_settings;
GnomeXkbInfo *xkb_info;
......@@ -1241,25 +1248,72 @@ show_selected_layout (CcRegionPanel *self)
}
static void
options_response (GtkDialog *options,
gint response_id,
CcRegionPanel *self)
update_shortcut_label (GtkWidget *widget,
const gchar *value)
{
gtk_widget_destroy (GTK_WIDGET (options));
g_autofree gchar *text = NULL;
guint accel_key;
g_autofree guint *keycode = NULL;
GdkModifierType mods;
if (value == NULL || *value == '\0') {
gtk_widget_hide (widget);
return;
}
gtk_accelerator_parse_with_keycode (value, &accel_key, &keycode, &mods);
if (accel_key == 0 && keycode == NULL && mods == 0) {
g_warning ("Failed to parse keyboard shortcut: '%s'", value);
gtk_widget_hide (widget);
return;
}
text = gtk_accelerator_get_label_with_keycode (gtk_widget_get_display (widget), accel_key, *keycode, mods);
gtk_label_set_text (GTK_LABEL (widget), text);
}
static void
update_shortcuts (CcRegionPanel *self)
{
g_auto(GStrv) previous = NULL;
g_auto(GStrv) next = NULL;
g_autofree gchar *previous_shortcut = NULL;
g_autoptr(GSettings) settings = NULL;
settings = g_settings_new ("org.gnome.desktop.wm.keybindings");
previous = g_settings_get_strv (settings, "switch-input-source-backward");
next = g_settings_get_strv (settings, "switch-input-source");
previous_shortcut = g_strdup (previous[0]);
update_shortcut_label (self->previous_source, previous_shortcut);
update_shortcut_label (self->next_source, next[0]);
}
static void
show_input_options (CcRegionPanel *self)
update_modifiers_shortcut (CcRegionPanel *self)
{
GtkWidget *toplevel;
GtkWidget *options;
g_auto(GStrv) options = NULL;
gchar **p;
g_autoptr(GSettings) settings = NULL;
g_autoptr(GnomeXkbInfo) xkb_info = NULL;
const gchar *text;
xkb_info = gnome_xkb_info_new ();
settings = g_settings_new ("org.gnome.desktop.input-sources");
options = g_settings_get_strv (settings, "xkb-options");
for (p = options; p && *p; ++p)
if (g_str_has_prefix (*p, "grp:"))
break;
toplevel = gtk_widget_get_toplevel (GTK_WIDGET (self));
options = cc_input_options_new (toplevel);
g_signal_connect (options, "response",
G_CALLBACK (options_response), self);
gtk_window_present (GTK_WINDOW (options));
if (p && *p) {
text = gnome_xkb_info_description_for_option (xkb_info, "grp", *p);
gtk_label_set_text (GTK_LABEL (self->alt_next_source), text);
} else {
gtk_widget_hide (self->alt_next_source);
}
}
static void
......@@ -1298,6 +1352,23 @@ setup_input_section (CcRegionPanel *self)
add_input_sources_from_settings (self);
update_buttons (self);
g_object_bind_property (self->previous_source, "visible",
self->previous_source_label, "visible",
G_BINDING_DEFAULT);
g_object_bind_property (self->next_source, "visible",
self->next_source_label, "visible",
G_BINDING_DEFAULT);
g_settings_bind (self->input_settings, "per-window",
self->per_window_source, "active",
G_SETTINGS_BIND_DEFAULT);
g_settings_bind (self->input_settings, "per-window",
self->same_source, "active",
G_SETTINGS_BIND_DEFAULT | G_SETTINGS_BIND_INVERT_BOOLEAN);
update_shortcuts (self);
update_modifiers_shortcut (self);
}
static void
......@@ -1627,9 +1698,15 @@ cc_region_panel_class_init (CcRegionPanelClass * klass)
gtk_widget_class_bind_template_child (widget_class, CcRegionPanel, restart_button);
gtk_widget_class_bind_template_child (widget_class, CcRegionPanel, login_label);
gtk_widget_class_bind_template_child (widget_class, CcRegionPanel, language_list);
gtk_widget_class_bind_template_child (widget_class, CcRegionPanel, same_source);
gtk_widget_class_bind_template_child (widget_class, CcRegionPanel, per_window_source);
gtk_widget_class_bind_template_child (widget_class, CcRegionPanel, previous_source);
gtk_widget_class_bind_template_child (widget_class, CcRegionPanel, previous_source_label);
gtk_widget_class_bind_template_child (widget_class, CcRegionPanel, next_source);
gtk_widget_class_bind_template_child (widget_class, CcRegionPanel, next_source_label);
gtk_widget_class_bind_template_child (widget_class, CcRegionPanel, alt_next_source);
gtk_widget_class_bind_template_callback (widget_class, restart_now);
gtk_widget_class_bind_template_callback (widget_class, show_input_options);
gtk_widget_class_bind_template_callback (widget_class, add_input);
gtk_widget_class_bind_template_callback (widget_class, remove_selected_input);
gtk_widget_class_bind_template_callback (widget_class, move_selected_input_up);
......
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<!-- interface-requires gtk+ 3.10 -->
<template class="CcInputOptions" parent="GtkDialog">
<property name="can_focus">False</property>
<property name="border_width">5</property>
<property name="title" translatable="yes">Input Source Options</property>
<property name="type_hint">dialog</property>
<property name="modal">True</property>
<child internal-child="vbox">
<object class="GtkBox" id="dialog-vbox1">
<property name="can_focus">False</property>
<property name="orientation">vertical</property>
<property name="spacing">2</property>
<child>
<object class="GtkGrid" id="grid1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_start">6</property>
<property name="margin_end">6</property>
<property name="margin_top">6</property>
<property name="margin_bottom">6</property>
<property name="row_spacing">6</property>
<property name="column_spacing">6</property>
<child>
<object class="GtkRadioButton" id="same_source">
<property name="label" translatable="yes">Use the _same source for all windows</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">False</property>
<property name="use_underline">True</property>
<property name="xalign">0</property>
<property name="active">True</property>
<property name="draw_indicator">True</property>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">0</property>
<property name="width">2</property>
<property name="height">1</property>
</packing>
</child>
<child>
<object class="GtkRadioButton" id="per_window_source">
<property name="label" translatable="yes">Allow _different sources for each window</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">False</property>
<property name="use_underline">True</property>
<property name="xalign">0</property>
<property name="active">True</property>
<property name="draw_indicator">True</property>
<property name="group">same_source</property>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">1</property>
<property name="width">2</property>
<property name="height">1</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="label1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_top">6</property>
<property name="xalign">0</property>
<property name="label" translatable="yes">Keyboard Shortcuts</property>
<attributes>
<attribute name="weight" value="bold"/>
</attributes>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">2</property>
<property name="width">2</property>
<property name="height">1</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="previous_source_label">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="xalign">1</property>
<property name="label" translatable="yes">Switch to previous source</property>
<style>
<class name="dim-label"/>
</style>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">3</property>
<property name="width">1</property>
<property name="height">1</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="previous_source">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="xalign">0</property>
<property name="label" translatable="yes">Super+Shift+Space</property>
</object>
<packing>
<property name="left_attach">1</property>
<property name="top_attach">3</property>
<property name="width">1</property>
<property name="height">1</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="next_source_label">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="xalign">1</property>
<property name="label" translatable="yes">Switch to next source</property>
<style>
<class name="dim-label"/>
</style>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">4</property>
<property name="width">1</property>
<property name="height">1</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="next_source">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="xalign">0</property>
<property name="label" translatable="yes">Super+Space</property>
</object>
<packing>
<property name="left_attach">1</property>
<property name="top_attach">4</property>
<property name="width">1</property>
<property name="height">1</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="label3">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_top">6</property>
<property name="label" translatable="yes">You can change these shortcuts in the keyboard settings</property>
<style>
<class name="dim-label"/>
</style>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">6</property>
<property name="width">2</property>
<property name="height">1</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="alt_next_source_label">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="xalign">1</property>
<property name="label" translatable="yes">Alternative switch to next source</property>
<style>
<class name="dim-label"/>
</style>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">5</property>
<property name="width">1</property>
<property name="height">1</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="alt_next_source">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="xalign">0</property>
<property name="label" translatable="yes">Left+Right Alt</property>
</object>
<packing>
<property name="left_attach">1</property>
<property name="top_attach">5</property>
<property name="width">1</property>
<property name="height">1</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object>
</child>
</template>
</interface>
......@@ -22,14 +22,12 @@ sources = files(
'cc-format-chooser.c',
'cc-ibus-utils.c',
'cc-input-chooser.c',
'cc-input-options.c',
'cc-input-row.c',
)
resource_data = files(
'cc-format-chooser.ui',
'input-chooser.ui',
'input-options.ui',
'region.ui'
)
......
......@@ -4,7 +4,6 @@
<file preprocess="xml-stripblanks">region.ui</file>
<file preprocess="xml-stripblanks">cc-format-chooser.ui</file>
<file preprocess="xml-stripblanks">cc-input-row.ui</file>
<file preprocess="xml-stripblanks">input-options.ui</file>
<file preprocess="xml-stripblanks">input-chooser.ui</file>
</gresource>
</gresources>
......@@ -244,13 +244,13 @@
</packing>
</child>
<child>
<object class="GtkButton" id="options_button">
<object class="GtkMenuButton" id="options_button">
<property name="label" translatable="yes">_Options</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="use_underline">True</property>
<signal name="clicked" handler="show_input_options" object="CcRegionPanel" swapped="yes"/>
<property name="popover">options_popover</property>
</object>
<packing>
<property name="expand">False</property>
......@@ -549,4 +549,207 @@
</object>
</child>
</template>
<object class="GtkPopover" id="options_popover">
<property name="can_focus">False</property>
<property name="border_width">12</property>
<child>
<object class="GtkBox">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="orientation">vertical</property>
<property name="spacing">2</property>
<child>
<object class="GtkGrid">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_start">6</property>
<property name="margin_end">6</property>
<property name="margin_top">6</property>
<property name="margin_bottom">6</property>
<property name="row_spacing">6</property>
<property name="column_spacing">6</property>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_top">6</property>
<property name="label" translatable="yes">Input Source Options</property>
<property name="margin_bottom">6</property>
<attributes>
<attribute name="weight" value="bold"/>
</attributes>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">0</property>
<property name="width">2</property>
<property name="height">1</property>
</packing>
</child>
<child>
<object class="GtkRadioButton" id="same_source">
<property name="label" translatable="yes">Use the _same source for all windows</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">False</property>
<property name="use_underline">True</property>
<property name="xalign">0</property>
<property name="active">True</property>
<property name="draw_indicator">True</property>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">1</property>
<property name="width">2</property>
<property name="height">1</property>
</packing>
</child>
<child>
<object class="GtkRadioButton" id="per_window_source">
<property name="label" translatable="yes">Allow _different sources for each window</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">False</property>
<property name="use_underline">True</property>
<property name="xalign">0</property>
<property name="active">True</property>
<property name="draw_indicator">True</property>
<property name="group">same_source</property>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">2</property>
<property name="width">2</property>
<property name="height">1</property>
</packing>
</child>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_top">12</property>
<property name="margin_bottom">6</property>
<property name="xalign">0</property>
<property name="label" translatable="yes">Keyboard Shortcuts</property>
<attributes>
<attribute name="weight" value="bold"/>
</attributes>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">3</property>
<property name="width">2</property>
<property name="height">1</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="previous_source_label">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="xalign">0</property>
<property name="halign">start</property>
<property name="label" translatable="yes">Previous source</property>
<style>
<class name="dim-label"/>
</style>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">4</property>
<property name="width">1</property>
<property name="height">1</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="previous_source">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="xalign">0</property>
<property name="hexpand">True</property>