[libadwaita] preferences-row: Add use-markup property
- From: Alexander Mikhaylenko <alexm src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [libadwaita] preferences-row: Add use-markup property
- Date: Mon, 4 Apr 2022 22:34:21 +0000 (UTC)
commit 63d94d0c906479d469bcdd5c58f306537aedd61f
Author: Sophie Herold <sophie hemio de>
Date: Mon Apr 4 22:34:20 2022 +0000
preferences-row: Add use-markup property
Fixes https://gitlab.gnome.org/GNOME/libadwaita/-/issues/439
src/adw-action-row.c | 3 ++
src/adw-action-row.ui | 4 +--
src/adw-combo-row.c | 4 +++
src/adw-expander-row.c | 3 ++
src/adw-expander-row.ui | 1 +
src/adw-preferences-row.c | 82 +++++++++++++++++++++++++++++++++++++++++++-
src/adw-preferences-row.h | 7 ++++
src/adw-preferences-window.c | 40 ++++++++++++++-------
tests/test-action-row.c | 4 +++
tests/test-expander-row.c | 4 +++
tests/test-preferences-row.c | 4 +++
11 files changed, 141 insertions(+), 15 deletions(-)
---
diff --git a/src/adw-action-row.c b/src/adw-action-row.c
index 2954b17f..a5de7ec9 100644
--- a/src/adw-action-row.c
+++ b/src/adw-action-row.c
@@ -423,6 +423,9 @@ adw_action_row_get_subtitle (AdwActionRow *self)
*
* Sets the subtitle for @self.
*
+ * The subtitle is interpreted as Pango markup unless
+ * [property@PreferencesRow:use-markup] is set to `FALSE`.
+ *
* Since: 1.0
*/
void
diff --git a/src/adw-action-row.ui b/src/adw-action-row.ui
index 32fbe9ec..3efdad57 100644
--- a/src/adw-action-row.ui
+++ b/src/adw-action-row.ui
@@ -58,7 +58,7 @@
<property name="wrap">True</property>
<property name="wrap-mode">word-char</property>
<property name="xalign">0</property>
- <property name="use-markup">True</property>
+ <property name="use-markup" bind-source="AdwActionRow" bind-property="use-markup"
bind-flags="sync-create"/>
<style>
<class name="title"/>
</style>
@@ -76,7 +76,7 @@
<property name="wrap">True</property>
<property name="wrap-mode">word-char</property>
<property name="xalign">0</property>
- <property name="use-markup">True</property>
+ <property name="use-markup" bind-source="AdwActionRow" bind-property="use-markup"
bind-flags="sync-create"/>
<style>
<class name="subtitle"/>
</style>
diff --git a/src/adw-combo-row.c b/src/adw-combo-row.c
index a1d46aee..2d04b724 100644
--- a/src/adw-combo-row.c
+++ b/src/adw-combo-row.c
@@ -521,6 +521,9 @@ adw_combo_row_class_init (AdwComboRowClass *klass)
*
* If `TRUE`, you should not access [property@ActionRow:subtitle].
*
+ * The subtitle is interpreted as Pango markup if
+ * [property@PreferencesRow:use-markup] is set to `TRUE`.
+ *
* Since: 1.0
*/
props[PROP_USE_SUBTITLE] =
@@ -549,6 +552,7 @@ adw_combo_row_init (AdwComboRow *self)
{
gtk_widget_init_template (GTK_WIDGET (self));
+ adw_preferences_row_set_use_markup (ADW_PREFERENCES_ROW (self), FALSE);
set_default_factory (self);
model_changed (self);
}
diff --git a/src/adw-expander-row.c b/src/adw-expander-row.c
index 0f53ad06..18cc8398 100644
--- a/src/adw-expander-row.c
+++ b/src/adw-expander-row.c
@@ -358,6 +358,9 @@ adw_expander_row_get_subtitle (AdwExpanderRow *self)
*
* Sets the subtitle for @self.
*
+ * The subtitle is interpreted as Pango markup unless
+ * [property@PreferencesRow:use-markup] is set to `FALSE`.
+ *
* Since: 1.0
*/
void
diff --git a/src/adw-expander-row.ui b/src/adw-expander-row.ui
index c4cbebdc..21e832c3 100644
--- a/src/adw-expander-row.ui
+++ b/src/adw-expander-row.ui
@@ -30,6 +30,7 @@
<property name="title" bind-source="AdwExpanderRow" bind-property="title"
bind-flags="sync-create"/>
<property name="use-underline" bind-source="AdwExpanderRow" bind-property="use-underline"
bind-flags="sync-create"/>
<property name="selectable" bind-source="AdwExpanderRow" bind-property="title-selectable"
bind-flags="sync-create"/>
+ <property name="use-markup" bind-source="AdwExpanderRow" bind-property="use-markup"
bind-flags="sync-create"/>
<style>
<class name="header"/>
</style>
diff --git a/src/adw-preferences-row.c b/src/adw-preferences-row.c
index d963781f..e1d705b1 100644
--- a/src/adw-preferences-row.c
+++ b/src/adw-preferences-row.c
@@ -30,6 +30,7 @@ typedef struct
gboolean use_underline;
gboolean title_selectable;
+ gboolean use_markup;
} AdwPreferencesRowPrivate;
G_DEFINE_TYPE_WITH_PRIVATE (AdwPreferencesRow, adw_preferences_row, GTK_TYPE_LIST_BOX_ROW)
@@ -39,6 +40,7 @@ enum {
PROP_TITLE,
PROP_USE_UNDERLINE,
PROP_TITLE_SELECTABLE,
+ PROP_USE_MARKUP,
LAST_PROP,
};
@@ -51,7 +53,6 @@ adw_preferences_row_get_property (GObject *object,
GParamSpec *pspec)
{
AdwPreferencesRow *self = ADW_PREFERENCES_ROW (object);
-
switch (prop_id) {
case PROP_TITLE:
g_value_set_string (value, adw_preferences_row_get_title (self));
@@ -62,6 +63,9 @@ adw_preferences_row_get_property (GObject *object,
case PROP_TITLE_SELECTABLE:
g_value_set_boolean (value, adw_preferences_row_get_title_selectable (self));
break;
+ case PROP_USE_MARKUP:
+ g_value_set_boolean (value, adw_preferences_row_get_use_markup (self));
+ break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
@@ -85,6 +89,9 @@ adw_preferences_row_set_property (GObject *object,
case PROP_TITLE_SELECTABLE:
adw_preferences_row_set_title_selectable (self, g_value_get_boolean (value));
break;
+ case PROP_USE_MARKUP:
+ adw_preferences_row_set_use_markup (self, g_value_get_boolean (value));
+ break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
@@ -154,6 +161,24 @@ adw_preferences_row_class_init (AdwPreferencesRowClass *klass)
FALSE,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY);
+ /**
+ * AdwPreferencesRow:use-markup: (attributes org.gtk.Property.get=adw_preferences_row_get_use_markup
org.gtk.Property.set=adw_preferences_row_set_use_markup)
+ *
+ * Whether to use Pango markup for the title label.
+ *
+ * Subclasses may also use it for other labels, such as subtitle.
+ *
+ * See also [func@Pango.parse_markup].
+ *
+ * Since: 1.2
+ */
+ props[PROP_USE_MARKUP] =
+ g_param_spec_boolean ("use-markup",
+ "Use markup",
+ "Whether to use Pango markup for the title label",
+ TRUE,
+ G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY);
+
g_object_class_install_properties (object_class, LAST_PROP, props);
}
@@ -162,6 +187,7 @@ adw_preferences_row_init (AdwPreferencesRow *self)
{
AdwPreferencesRowPrivate *priv = adw_preferences_row_get_instance_private (self);
priv->title = g_strdup ("");
+ priv->use_markup = TRUE;
}
/**
@@ -208,6 +234,9 @@ adw_preferences_row_get_title (AdwPreferencesRow *self)
*
* Sets the title of the preference represented by @self.
*
+ * The title is interpreted as Pango markup unless
+ * [property@PreferencesRow:use-markup] is set to `FALSE`.
+ *
* Since: 1.0
*/
void
@@ -326,3 +355,54 @@ adw_preferences_row_set_title_selectable (AdwPreferencesRow *self,
g_object_notify_by_pspec (G_OBJECT (self), props[PROP_TITLE_SELECTABLE]);
}
+
+/**
+ * adw_preferences_row_get_use_markup: (attributes org.gtk.Method.get_property=use-markup)
+ * @self: a preferences row
+ *
+ * Gets whether to use Pango markup for the title label.
+ *
+ * Returns: whether to use markup
+ *
+ * Since: 1.2
+ */
+gboolean
+adw_preferences_row_get_use_markup (AdwPreferencesRow *self)
+{
+ AdwPreferencesRowPrivate *priv;
+
+ g_return_val_if_fail (ADW_IS_PREFERENCES_ROW (self), FALSE);
+
+ priv = adw_preferences_row_get_instance_private (self);
+
+ return priv->use_markup;
+}
+
+/**
+ * adw_preferences_row_set_use_markup: (attributes org.gtk.Method.set_property=use-markup)
+ * @self: a preferences row
+ * @use_markup: whether to use markup
+ *
+ * Sets whether to use Pango markup for the title label.
+ *
+ * Since: 1.2
+ */
+void
+adw_preferences_row_set_use_markup (AdwPreferencesRow *self,
+ gboolean use_markup)
+{
+ AdwPreferencesRowPrivate *priv;
+
+ g_return_if_fail (ADW_IS_PREFERENCES_ROW (self));
+
+ priv = adw_preferences_row_get_instance_private (self);
+
+ use_markup = !!use_markup;
+
+ if (priv->use_markup == use_markup)
+ return;
+
+ priv->use_markup = use_markup;
+
+ g_object_notify_by_pspec (G_OBJECT (self), props[PROP_USE_MARKUP]);
+}
diff --git a/src/adw-preferences-row.h b/src/adw-preferences-row.h
index 8b469dad..f1d40066 100644
--- a/src/adw-preferences-row.h
+++ b/src/adw-preferences-row.h
@@ -54,4 +54,11 @@ ADW_AVAILABLE_IN_ALL
void adw_preferences_row_set_title_selectable (AdwPreferencesRow *self,
gboolean title_selectable);
+
+ADW_AVAILABLE_IN_ALL
+gboolean adw_preferences_row_get_use_markup (AdwPreferencesRow *self);
+ADW_AVAILABLE_IN_ALL
+void adw_preferences_row_set_use_markup (AdwPreferencesRow *self,
+ gboolean use_markup);
+
G_END_DECLS
diff --git a/src/adw-preferences-window.c b/src/adw-preferences-window.c
index 0a8bba78..a1afa6c4 100644
--- a/src/adw-preferences-window.c
+++ b/src/adw-preferences-window.c
@@ -129,6 +129,32 @@ strip_mnemonic (const char *src)
return new_str;
}
+static char *
+make_comparable (const char *src,
+ AdwPreferencesRow *row,
+ gboolean allow_underline)
+{
+ char *plaintext = g_utf8_casefold (src, -1);
+ GError *error = NULL;
+
+ if (adw_preferences_row_get_use_markup (row)) {
+ pango_parse_markup (plaintext, -1, 0, NULL, &plaintext, NULL, &error);
+
+ if (error) {
+ g_critical ("Couldn't parse markup: %s", error->message);
+ g_clear_error (&error);
+ }
+ }
+
+ if (allow_underline && adw_preferences_row_get_use_underline (row)) {
+ char *comparable = strip_mnemonic (plaintext);
+ g_free (plaintext);
+ return comparable;
+ }
+
+ return plaintext;
+}
+
static gboolean
filter_search_results (AdwPreferencesRow *row,
AdwPreferencesWindow *self)
@@ -140,22 +166,12 @@ filter_search_results (AdwPreferencesRow *row,
g_assert (ADW_IS_PREFERENCES_ROW (row));
terms = g_utf8_casefold (gtk_editable_get_text (GTK_EDITABLE (priv->search_entry)), -1);
- title = g_utf8_casefold (adw_preferences_row_get_title (row), -1);
-
- if (adw_preferences_row_get_use_underline (ADW_PREFERENCES_ROW (row))) {
- char *stripped_title = strip_mnemonic (title);
-
- if (stripped_title) {
- g_free (title);
-
- title = stripped_title;
- }
- }
+ title = make_comparable (adw_preferences_row_get_title (row), row, TRUE);
if (!!strstr (title, terms)) {
result = TRUE;
} else if (ADW_IS_ACTION_ROW (row)) {
- char *subtitle = g_utf8_casefold (adw_action_row_get_subtitle (ADW_ACTION_ROW (row)), -1);
+ char *subtitle = make_comparable (adw_action_row_get_subtitle (ADW_ACTION_ROW(row)), row, FALSE);
if (!!strstr (subtitle, terms))
result = TRUE;
diff --git a/tests/test-action-row.c b/tests/test-action-row.c
index eac0aaa2..5059379d 100644
--- a/tests/test-action-row.c
+++ b/tests/test-action-row.c
@@ -50,6 +50,10 @@ test_adw_action_row_subtitle (void)
adw_action_row_set_subtitle (row, "Dummy subtitle");
g_assert_cmpstr (adw_action_row_get_subtitle (row), ==, "Dummy subtitle");
+ adw_preferences_row_set_use_markup (ADW_PREFERENCES_ROW (row), FALSE);
+ adw_action_row_set_subtitle (row, "Invalid <b>markup");
+ g_assert_cmpstr (adw_action_row_get_subtitle (row), ==, "Invalid <b>markup");
+
g_assert_finalize_object (row);
}
diff --git a/tests/test-expander-row.c b/tests/test-expander-row.c
index 730a25c8..90444f2f 100644
--- a/tests/test-expander-row.c
+++ b/tests/test-expander-row.c
@@ -37,6 +37,10 @@ test_adw_expander_row_subtitle (void)
adw_expander_row_set_subtitle (row, "Dummy subtitle");
g_assert_cmpstr (adw_expander_row_get_subtitle (row), ==, "Dummy subtitle");
+ adw_preferences_row_set_use_markup (ADW_PREFERENCES_ROW (row), FALSE);
+ adw_expander_row_set_subtitle (row, "Invalid <b>markup");
+ g_assert_cmpstr (adw_expander_row_get_subtitle (row), ==, "Invalid <b>markup");
+
g_assert_finalize_object (row);
}
diff --git a/tests/test-preferences-row.c b/tests/test-preferences-row.c
index fdf47105..3e5f4cb8 100644
--- a/tests/test-preferences-row.c
+++ b/tests/test-preferences-row.c
@@ -22,6 +22,10 @@ test_adw_preferences_row_title (void)
adw_preferences_row_set_title (row, NULL);
g_assert_cmpstr (adw_preferences_row_get_title (row), ==, "");
+ adw_preferences_row_set_use_markup (row, FALSE);
+ adw_preferences_row_set_title (row, "Invalid <b>markup");
+ g_assert_cmpstr (adw_preferences_row_get_title (row), ==, "Invalid <b>markup");
+
g_assert_finalize_object (row);
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]