input-chooser: Port to AdwWindow

This commit is contained in:
Maximiliano Sandoval R 2023-11-02 21:50:24 +01:00 committed by Maximiliano Sandoval R.
parent a729de232e
commit 769287a59b
No known key found for this signature in database
GPG key ID: D64A8D747F6FE706
4 changed files with 119 additions and 83 deletions

View file

@ -15,6 +15,7 @@
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#include <adwaita.h>
#include <config.h>
#include <locale.h>
#include <glib/gi18n.h>
@ -54,7 +55,7 @@ typedef enum
struct _CcInputChooser
{
GtkDialog parent_instance;
AdwWindow parent_instance;
GtkButton *add_button;
GtkSearchEntry *filter_entry;
@ -75,7 +76,15 @@ struct _CcInputChooser
gboolean is_login;
};
G_DEFINE_TYPE (CcInputChooser, cc_input_chooser, GTK_TYPE_DIALOG)
G_DEFINE_TYPE (CcInputChooser, cc_input_chooser, ADW_TYPE_WINDOW)
enum
{
SIGNAL_SOURCE_SELECTED,
SIGNAL_LAST
};
static guint signals[SIGNAL_LAST] = { 0, };
typedef struct
{
@ -305,6 +314,15 @@ add_input_source_rows_for_locale (CcInputChooser *self,
gtk_list_box_append (self->input_sources_listbox, row);
}
static void
cc_input_chooser_emit_source_selected (CcInputChooser *self)
{
g_signal_emit (self, signals[SIGNAL_SOURCE_SELECTED], 0,
cc_input_chooser_get_source (self));
gtk_window_close (GTK_WINDOW (self));
}
static void
on_back_row_click_released_cb (CcInputChooser *self,
int n_press,
@ -618,6 +636,12 @@ show_more (CcInputChooser *self)
gtk_list_box_invalidate_filter (self->input_sources_listbox);
}
static void
on_add_button_clicked_cb (CcInputChooser *self)
{
cc_input_chooser_emit_source_selected (self);
}
static void
on_input_sources_listbox_row_activated_cb (CcInputChooser *self, GtkListBoxRow *row)
{
@ -643,9 +667,7 @@ on_input_sources_listbox_row_activated_cb (CcInputChooser *self, GtkListBoxRow
if (data)
{
if (gtk_widget_is_sensitive (GTK_WIDGET (self->add_button)))
gtk_dialog_response (GTK_DIALOG (self),
gtk_dialog_get_response_for_widget (GTK_DIALOG (self),
GTK_WIDGET (self->add_button)));
cc_input_chooser_emit_source_selected (self);
return;
}
@ -678,6 +700,18 @@ on_input_sources_listbox_selected_rows_changed_cb (CcInputChooser *self)
gtk_widget_set_sensitive (GTK_WIDGET (self->add_button), sensitive);
}
static void
on_stop_search_cb (CcInputChooser *self)
{
const char *search_text;
search_text = gtk_editable_get_text (GTK_EDITABLE (self->filter_entry));
if (search_text && g_strcmp0 (search_text, "") != 0)
gtk_editable_set_text (GTK_EDITABLE (self->filter_entry), "");
else
gtk_window_close (GTK_WINDOW (self));
}
static void
add_default_row (CcInputChooser *self,
LocaleInfo *info,
@ -1052,8 +1086,18 @@ cc_input_chooser_class_init (CcInputChooserClass *klass)
object_class->dispose = cc_input_chooser_dispose;
gtk_widget_class_add_binding_action (widget_class, GDK_KEY_Escape, 0, "window.close", NULL);
gtk_widget_class_set_template_from_resource (widget_class, "/org/gnome/control-center/keyboard/cc-input-chooser.ui");
signals[SIGNAL_SOURCE_SELECTED] = g_signal_new ("source-selected",
CC_TYPE_INPUT_CHOOSER,
G_SIGNAL_RUN_LAST,
0, NULL, NULL, NULL,
G_TYPE_NONE,
1,
CC_TYPE_INPUT_SOURCE);
gtk_widget_class_bind_template_child (widget_class, CcInputChooser, add_button);
gtk_widget_class_bind_template_child (widget_class, CcInputChooser, filter_entry);
gtk_widget_class_bind_template_child (widget_class, CcInputChooser, input_sources_listbox);
@ -1063,6 +1107,8 @@ cc_input_chooser_class_init (CcInputChooserClass *klass)
gtk_widget_class_bind_template_callback (widget_class, on_input_sources_listbox_row_activated_cb);
gtk_widget_class_bind_template_callback (widget_class, on_input_sources_listbox_selected_rows_changed_cb);
gtk_widget_class_bind_template_callback (widget_class, on_filter_entry_search_changed_cb);
gtk_widget_class_bind_template_callback (widget_class, on_add_button_clicked_cb);
gtk_widget_class_bind_template_callback (widget_class, on_stop_search_cb);
//gtk_widget_class_bind_template_callback (widget_class, on_filter_entry_key_release_event_cb);
}
@ -1081,9 +1127,7 @@ cc_input_chooser_new (gboolean is_login,
{
CcInputChooser *self;
self = g_object_new (CC_TYPE_INPUT_CHOOSER,
"use-header-bar", 1,
NULL);
self = g_object_new (CC_TYPE_INPUT_CHOOSER, NULL);
self->is_login = is_login;
self->xkb_info = g_object_ref (xkb_info);

View file

@ -17,6 +17,7 @@
#pragma once
#include <adwaita.h>
#include <gtk/gtk.h>
#include "cc-input-source.h"
@ -27,7 +28,7 @@
G_BEGIN_DECLS
#define CC_TYPE_INPUT_CHOOSER (cc_input_chooser_get_type ())
G_DECLARE_FINAL_TYPE (CcInputChooser, cc_input_chooser, CC, INPUT_CHOOSER, GtkDialog)
G_DECLARE_FINAL_TYPE (CcInputChooser, cc_input_chooser, CC, INPUT_CHOOSER, AdwWindow)
CcInputChooser *cc_input_chooser_new (gboolean is_login,
GnomeXkbInfo *xkb_info,

View file

@ -1,69 +1,43 @@
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<!-- interface-requires gtk+ 3.0 -->
<template class="CcInputChooser" parent="GtkDialog">
<template class="CcInputChooser" parent="AdwWindow">
<property name="title" translatable="yes">Add an Input Source</property>
<property name="modal">True</property>
<property name="destroy_with_parent">True</property>
<property name="resizable">True</property>
<child type="action">
<object class="GtkButton" id="cancel_button">
<property name="label" translatable="yes">_Cancel</property>
<property name="use_underline">True</property>
<property name="valign">center</property>
</object>
</child>
<child type="action">
<object class="GtkButton" id="add_button">
<property name="label" translatable="yes">_Add</property>
<property name="sensitive">False</property>
<property name="use_underline">True</property>
<property name="valign">center</property>
</object>
</child>
<child>
<object class="GtkBox">
<property name="orientation">vertical</property>
<property name="spacing">0</property>
<child>
<object class="GtkScrolledWindow">
<property name="vexpand">True</property>
<property name="hscrollbar-policy">never</property>
<property name="propagate-natural-height">True</property>
<property name="min-content-height">300</property>
<property name="vadjustment">scroll_adjustment</property>
<property name="child">
<object class="GtkViewport">
<property name="scroll-to-focus">True</property>
<property name="child">
<object class="AdwClamp">
<property name="child">
<object class="GtkListBox" id="input_sources_listbox">
<property name="halign">fill</property>
<property name="valign">start</property>
<property name="margin-top">24</property>
<property name="margin-bottom">24</property>
<property name="margin-start">12</property>
<property name="margin-end">12</property>
<signal name="row-activated" handler="on_input_sources_listbox_row_activated_cb" object="CcInputChooser" swapped="yes" />
<signal name="selected-rows-changed" handler="on_input_sources_listbox_selected_rows_changed_cb" object="CcInputChooser" swapped="yes" />
<style>
<class name="boxed-list" />
</style>
</object>
</property>
</object>
</property>
<property name="width-request">360</property>
<property name="height-request">294</property>
<property name="default-height">430</property>
<property name="default-width">600</property>
<property name="content">
<object class="AdwToolbarView">
<child type="top">
<object class="AdwHeaderBar">
<property name="show-start-title-buttons">False</property>
<property name="show-end-title-buttons">False</property>
<child>
<object class="GtkButton" id="cancel_button">
<property name="label" translatable="yes">_Cancel</property>
<property name="use_underline">True</property>
<property name="valign">center</property>
<property name="action-name">window.close</property>
</object>
</property>
</child>
<child type="end">
<object class="GtkButton" id="add_button">
<property name="label" translatable="yes">_Add</property>
<property name="sensitive">False</property>
<property name="use_underline">True</property>
<property name="valign">center</property>
<signal name="clicked" handler="on_add_button_clicked_cb" object="CcInputChooser" swapped="yes" />
<style>
<class name="suggested-action"/>
</style>
</object>
</child>
</object>
</child>
<child>
<object class="GtkSeparator">
<property name="visible" bind-source="filter_entry" bind-property="visible" bind-flags="sync-create"/>
</object>
</child>
<child>
<child type="top">
<object class="GtkSearchEntry" id="filter_entry">
<property name="placeholder_text" translatable="yes">Language or country</property>
<property name="visible">False</property>
@ -76,9 +50,10 @@
<property name="label" translatable="yes">Search</property>
</accessibility>
<signal name="search-changed" handler="on_filter_entry_search_changed_cb" object="CcInputChooser" swapped="yes" />
<signal name="stop-search" handler="on_stop_search_cb" object="CcInputChooser" swapped="yes" />
</object>
</child>
<child>
<child type="top">
<object class="GtkLabel" id="login_label">
<property name="visible">False</property>
<property name="wrap">True</property>
@ -91,13 +66,35 @@
</style>
</object>
</child>
<child>
<object class="GtkScrolledWindow">
<property name="hscrollbar-policy">never</property>
<property name="propagate-natural-height">True</property>
<property name="vadjustment">
<object class="GtkAdjustment" id="scroll_adjustment"/>
</property>
<property name="child">
<object class="AdwClamp">
<property name="child">
<object class="GtkListBox" id="input_sources_listbox">
<property name="halign">fill</property>
<property name="valign">start</property>
<property name="margin-top">24</property>
<property name="margin-bottom">24</property>
<property name="margin-start">12</property>
<property name="margin-end">12</property>
<signal name="row-activated" handler="on_input_sources_listbox_row_activated_cb" object="CcInputChooser" swapped="yes" />
<signal name="selected-rows-changed" handler="on_input_sources_listbox_selected_rows_changed_cb" object="CcInputChooser" swapped="yes" />
<style>
<class name="boxed-list" />
</style>
</object>
</property>
</object>
</property>
</object>
</child>
</object>
</child>
<action-widgets>
<action-widget response="-5" default="true">add_button</action-widget>
<action-widget response="-6">cancel_button</action-widget>
</action-widgets>
</property>
</template>
<object class="GtkAdjustment" id="scroll_adjustment">
</object>
</interface>

View file

@ -482,20 +482,14 @@ update_input (CcInputListBox *self)
static void
on_chooser_response_cb (CcInputListBox *self,
gint response,
GtkDialog *dialog)
CcInputSource *source)
{
if (response == GTK_RESPONSE_OK) {
CcInputSource *source;
source = cc_input_chooser_get_source (CC_INPUT_CHOOSER (dialog));
if (source) {
if (source != NULL && get_row_by_source (self, source) == NULL) {
add_input_row (self, source);
update_input (self);
}
}
gtk_window_destroy (GTK_WINDOW (dialog));
}
static void
@ -513,7 +507,7 @@ show_input_chooser (CcInputListBox *self)
);
gtk_window_set_transient_for (GTK_WINDOW (chooser),
GTK_WINDOW (gtk_widget_get_native (GTK_WIDGET (self))));
g_signal_connect_swapped (chooser, "response", G_CALLBACK (on_chooser_response_cb), self);
g_signal_connect_swapped (chooser, "source-selected", G_CALLBACK (on_chooser_response_cb), self);
gtk_window_present (GTK_WINDOW (chooser));
}