[gnome-builder] libide/tweaks: add IdeTweaksRadio



commit 0aefcfe1c7a7b523486857131185db05c5910df2
Author: Christian Hergert <chergert redhat com>
Date:   Wed Aug 10 13:41:59 2022 -0700

    libide/tweaks: add IdeTweaksRadio
    
    This is similar to IdeTweaksSwitch but draws a checkbutton instead. We
    could potentially do this another way w/ a "role" attribute, but this
    seemed a bit easier to manage long term and is roughly the same amount
    of effort for people to switch between to test things.

 src/libide/tweaks/ide-tweaks-init.c  |   1 +
 src/libide/tweaks/ide-tweaks-radio.c | 274 +++++++++++++++++++++++++++++++++++
 src/libide/tweaks/ide-tweaks-radio.h |  55 +++++++
 src/libide/tweaks/libide-tweaks.h    |   1 +
 src/libide/tweaks/meson.build        |   2 +
 5 files changed, 333 insertions(+)
---
diff --git a/src/libide/tweaks/ide-tweaks-init.c b/src/libide/tweaks/ide-tweaks-init.c
index 802f00665..e5bee1bb2 100644
--- a/src/libide/tweaks/ide-tweaks-init.c
+++ b/src/libide/tweaks/ide-tweaks-init.c
@@ -37,6 +37,7 @@ _ide_tweaks_init (void)
   g_type_ensure (IDE_TYPE_TWEAKS_GROUP);
   g_type_ensure (IDE_TYPE_TWEAKS_ITEM);
   g_type_ensure (IDE_TYPE_TWEAKS_PAGE);
+  g_type_ensure (IDE_TYPE_TWEAKS_RADIO);
   g_type_ensure (IDE_TYPE_TWEAKS_SECTION);
   g_type_ensure (IDE_TYPE_TWEAKS_SETTINGS);
   g_type_ensure (IDE_TYPE_TWEAKS_SWITCH);
diff --git a/src/libide/tweaks/ide-tweaks-radio.c b/src/libide/tweaks/ide-tweaks-radio.c
new file mode 100644
index 000000000..c1e5ed7dc
--- /dev/null
+++ b/src/libide/tweaks/ide-tweaks-radio.c
@@ -0,0 +1,274 @@
+/* ide-tweaks-radio.c
+ *
+ * Copyright 2022 Christian Hergert <chergert redhat com>
+ *
+ * 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 3 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/>.
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+#define G_LOG_DOMAIN "ide-tweaks-radio"
+
+#include "config.h"
+
+#include <adwaita.h>
+
+#include "ide-tweaks-radio.h"
+
+struct _IdeTweaksRadio
+{
+  IdeTweaksWidget parent_instance;
+  char *title;
+  char *subtitle;
+  char *action_name;
+  GVariant *action_target;
+};
+
+enum {
+  PROP_0,
+  PROP_TITLE,
+  PROP_SUBTITLE,
+  PROP_ACTION_NAME,
+  PROP_ACTION_TARGET,
+  N_PROPS
+};
+
+G_DEFINE_FINAL_TYPE (IdeTweaksRadio, ide_tweaks_radio, IDE_TYPE_TWEAKS_WIDGET)
+
+static GParamSpec *properties [N_PROPS];
+
+static GtkWidget *
+ide_tweaks_radio_create (IdeTweaksWidget *widget)
+{
+  IdeTweaksRadio *self = (IdeTweaksRadio *)widget;
+  AdwActionRow *row;
+  GtkWidget *radio;
+
+  g_assert (IDE_IS_TWEAKS_WIDGET (widget));
+
+  radio = g_object_new (GTK_TYPE_CHECK_BUTTON,
+                        "action-name", self->action_name,
+                        "action-target", self->action_target,
+                        "can-target", FALSE,
+                        "valign", GTK_ALIGN_CENTER,
+                        NULL);
+  gtk_widget_add_css_class (radio, "checkimage");
+
+  row = g_object_new (ADW_TYPE_ACTION_ROW,
+                      "title", self->title,
+                      "subtitle", self->subtitle,
+                      "activatable-widget", radio,
+                      NULL);
+  adw_action_row_add_suffix (row, radio);
+
+  return GTK_WIDGET (row);
+}
+
+static void
+ide_tweaks_radio_dispose (GObject *object)
+{
+  IdeTweaksRadio *self = (IdeTweaksRadio *)object;
+
+  g_clear_pointer (&self->title, g_free);
+  g_clear_pointer (&self->subtitle, g_free);
+  g_clear_pointer (&self->action_name, g_free);
+  g_clear_pointer (&self->action_target, g_variant_unref);
+
+  G_OBJECT_CLASS (ide_tweaks_radio_parent_class)->dispose (object);
+}
+
+static void
+ide_tweaks_radio_get_property (GObject    *object,
+                               guint       prop_id,
+                               GValue     *value,
+                               GParamSpec *pspec)
+{
+  IdeTweaksRadio *self = IDE_TWEAKS_RADIO (object);
+
+  switch (prop_id)
+    {
+    case PROP_ACTION_NAME:
+      g_value_set_string (value, ide_tweaks_radio_get_action_name (self));
+      break;
+
+    case PROP_ACTION_TARGET:
+      g_value_set_variant (value, ide_tweaks_radio_get_action_target (self));
+      break;
+
+    case PROP_SUBTITLE:
+      g_value_set_string (value, ide_tweaks_radio_get_subtitle (self));
+      break;
+
+    case PROP_TITLE:
+      g_value_set_string (value, ide_tweaks_radio_get_title (self));
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+    }
+}
+
+static void
+ide_tweaks_radio_set_property (GObject      *object,
+                               guint         prop_id,
+                               const GValue *value,
+                               GParamSpec   *pspec)
+{
+  IdeTweaksRadio *self = IDE_TWEAKS_RADIO (object);
+
+  switch (prop_id)
+    {
+    case PROP_ACTION_NAME:
+      ide_tweaks_radio_set_action_name (self, g_value_get_string (value));
+      break;
+
+    case PROP_ACTION_TARGET:
+      ide_tweaks_radio_set_action_target (self, g_value_get_variant (value));
+      break;
+
+    case PROP_SUBTITLE:
+      ide_tweaks_radio_set_subtitle (self, g_value_get_string (value));
+      break;
+
+    case PROP_TITLE:
+      ide_tweaks_radio_set_title (self, g_value_get_string (value));
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+    }
+}
+
+static void
+ide_tweaks_radio_class_init (IdeTweaksRadioClass *klass)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+  IdeTweaksWidgetClass *widget_class = IDE_TWEAKS_WIDGET_CLASS (klass);
+
+  object_class->dispose = ide_tweaks_radio_dispose;
+  object_class->get_property = ide_tweaks_radio_get_property;
+  object_class->set_property = ide_tweaks_radio_set_property;
+
+  widget_class->create = ide_tweaks_radio_create;
+
+  properties[PROP_ACTION_NAME] =
+    g_param_spec_string ("action-name", NULL, NULL,
+                         NULL,
+                         (G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS));
+
+  properties[PROP_ACTION_TARGET] =
+    g_param_spec_variant ("action-target", NULL, NULL,
+                          G_VARIANT_TYPE_ANY,
+                          NULL,
+                          (G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS));
+
+  properties[PROP_SUBTITLE] =
+    g_param_spec_string ("subtitle", NULL, NULL,
+                         NULL,
+                         (G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS));
+
+  properties[PROP_TITLE] =
+    g_param_spec_string ("title", NULL, NULL,
+                         NULL,
+                         (G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS));
+
+  g_object_class_install_properties (object_class, N_PROPS, properties);
+}
+
+static void
+ide_tweaks_radio_init (IdeTweaksRadio *self)
+{
+}
+
+const char *
+ide_tweaks_radio_get_action_name (IdeTweaksRadio *self)
+{
+  g_return_val_if_fail (IDE_IS_TWEAKS_RADIO (self), NULL);
+
+  return self->action_name;
+}
+
+/**
+ * ide_tweaks_radio_get_action_target:
+ * @self: a #IdeTweaksRadio
+ *
+ * Returns: (transfer none) (nullable): a #GVariant or %NULL
+ */
+GVariant *
+ide_tweaks_radio_get_action_target (IdeTweaksRadio *self)
+{
+  g_return_val_if_fail (IDE_IS_TWEAKS_RADIO (self), NULL);
+
+  return self->action_target;
+}
+
+const char *
+ide_tweaks_radio_get_subtitle (IdeTweaksRadio *self)
+{
+  g_return_val_if_fail (IDE_IS_TWEAKS_RADIO (self), NULL);
+
+  return self->subtitle;
+}
+
+const char *
+ide_tweaks_radio_get_title (IdeTweaksRadio *self)
+{
+  g_return_val_if_fail (IDE_IS_TWEAKS_RADIO (self), NULL);
+
+  return self->title;
+}
+
+void
+ide_tweaks_radio_set_action_name (IdeTweaksRadio *self,
+                                  const char     *action_name)
+{
+  g_return_if_fail (IDE_IS_TWEAKS_RADIO (self));
+
+  if (ide_set_string (&self->action_name, action_name))
+    g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_ACTION_NAME]);
+}
+
+void
+ide_tweaks_radio_set_action_target (IdeTweaksRadio *self,
+                                    GVariant       *action_target)
+{
+  g_return_if_fail (IDE_IS_TWEAKS_RADIO (self));
+
+  if (action_target == self->action_target)
+    return;
+
+  g_clear_pointer (&self->action_target, g_variant_unref);
+  self->action_target = action_target ? g_variant_ref_sink (action_target) : NULL;
+  g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_ACTION_TARGET]);
+}
+
+void
+ide_tweaks_radio_set_subtitle (IdeTweaksRadio *self,
+                               const char     *subtitle)
+{
+  g_return_if_fail (IDE_IS_TWEAKS_RADIO (self));
+
+  if (ide_set_string (&self->subtitle, subtitle))
+    g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_SUBTITLE]);
+}
+
+void
+ide_tweaks_radio_set_title (IdeTweaksRadio *self,
+                            const char     *title)
+{
+  g_return_if_fail (IDE_IS_TWEAKS_RADIO (self));
+
+  if (ide_set_string (&self->title, title))
+    g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_TITLE]);
+}
diff --git a/src/libide/tweaks/ide-tweaks-radio.h b/src/libide/tweaks/ide-tweaks-radio.h
new file mode 100644
index 000000000..41531ecf6
--- /dev/null
+++ b/src/libide/tweaks/ide-tweaks-radio.h
@@ -0,0 +1,55 @@
+/* ide-tweaks-radio.h
+ *
+ * Copyright 2022 Christian Hergert <chergert redhat com>
+ *
+ * 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 3 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/>.
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+#pragma once
+
+#include "ide-tweaks-widget.h"
+
+G_BEGIN_DECLS
+
+#define IDE_TYPE_TWEAKS_RADIO (ide_tweaks_radio_get_type())
+
+IDE_AVAILABLE_IN_ALL
+G_DECLARE_FINAL_TYPE (IdeTweaksRadio, ide_tweaks_radio, IDE, TWEAKS_RADIO, IdeTweaksWidget)
+
+IDE_AVAILABLE_IN_ALL
+IdeTweaksRadio *ide_tweaks_radio_new               (void);
+IDE_AVAILABLE_IN_ALL
+const char     *ide_tweaks_radio_get_title         (IdeTweaksRadio *self);
+IDE_AVAILABLE_IN_ALL
+void            ide_tweaks_radio_set_title         (IdeTweaksRadio *self,
+                                                    const char     *title);
+IDE_AVAILABLE_IN_ALL
+const char     *ide_tweaks_radio_get_subtitle      (IdeTweaksRadio *self);
+IDE_AVAILABLE_IN_ALL
+void            ide_tweaks_radio_set_subtitle      (IdeTweaksRadio *self,
+                                                    const char     *subtitle);
+IDE_AVAILABLE_IN_ALL
+const char     *ide_tweaks_radio_get_action_name   (IdeTweaksRadio *self);
+IDE_AVAILABLE_IN_ALL
+void            ide_tweaks_radio_set_action_name   (IdeTweaksRadio *self,
+                                                    const char     *action_name);
+IDE_AVAILABLE_IN_ALL
+GVariant       *ide_tweaks_radio_get_action_target (IdeTweaksRadio *self);
+IDE_AVAILABLE_IN_ALL
+void            ide_tweaks_radio_set_action_target (IdeTweaksRadio *self,
+                                                    GVariant       *action_target);
+
+G_END_DECLS
diff --git a/src/libide/tweaks/libide-tweaks.h b/src/libide/tweaks/libide-tweaks.h
index 3e9c73959..1e50a9032 100644
--- a/src/libide/tweaks/libide-tweaks.h
+++ b/src/libide/tweaks/libide-tweaks.h
@@ -26,6 +26,7 @@
 # include "ide-tweaks-group.h"
 # include "ide-tweaks-item.h"
 # include "ide-tweaks-page.h"
+# include "ide-tweaks-radio.h"
 # include "ide-tweaks-section.h"
 # include "ide-tweaks-settings.h"
 # include "ide-tweaks-switch.h"
diff --git a/src/libide/tweaks/meson.build b/src/libide/tweaks/meson.build
index cfce90a7c..34cffcaf9 100644
--- a/src/libide/tweaks/meson.build
+++ b/src/libide/tweaks/meson.build
@@ -13,6 +13,7 @@ libide_tweaks_public_headers = [
   'ide-tweaks-group.h',
   'ide-tweaks-item.h',
   'ide-tweaks-page.h',
+  'ide-tweaks-radio.h',
   'ide-tweaks-section.h',
   'ide-tweaks-settings.h',
   'ide-tweaks-switch.h',
@@ -33,6 +34,7 @@ libide_tweaks_public_sources = [
   'ide-tweaks-factory.c',
   'ide-tweaks-group.c',
   'ide-tweaks-page.c',
+  'ide-tweaks-radio.c',
   'ide-tweaks-section.c',
   'ide-tweaks-settings.c',
   'ide-tweaks-switch.c',


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]