[gnome-screenshot/wip/exalm/cleanups: 13/16] interactive-dialog: Replace callback with a signal



commit fe7769ef0e8f4e02cb216956b7816a6593f3f741
Author: Alexander Mikhaylenko <alexm gnome org>
Date:   Fri Apr 3 07:08:38 2020 +0500

    interactive-dialog: Replace callback with a signal
    
    This allows to make the dialog a completely normal widget.

 src/screenshot-application.c        | 16 +++++++++++-
 src/screenshot-interactive-dialog.c | 52 ++++++++++++++++++-------------------
 src/screenshot-interactive-dialog.h |  4 +--
 3 files changed, 42 insertions(+), 30 deletions(-)
---
diff --git a/src/screenshot-application.c b/src/screenshot-application.c
index ef520f5..60690db 100644
--- a/src/screenshot-application.c
+++ b/src/screenshot-application.c
@@ -681,10 +681,24 @@ screenshot_application_command_line (GApplication            *app,
   return exit_status;
 }
 
+static void
+capture_clicked_cb (ScreenshotInteractiveDialog *dialog,
+                    ScreenshotApplication       *self)
+{
+  gtk_widget_destroy (GTK_WIDGET (dialog));
+  screenshot_start (self);
+}
+
 static void
 screenshot_show_interactive_dialog (ScreenshotApplication *self)
 {
-  screenshot_interactive_dialog_new ((CaptureClickedCallback) screenshot_start, self);
+  GtkWidget *dialog;
+
+  dialog = screenshot_interactive_dialog_new (GTK_APPLICATION (self));
+
+  g_signal_connect (dialog, "capture", G_CALLBACK (capture_clicked_cb), self);
+
+  gtk_widget_show (GTK_WIDGET (dialog));
 }
 
 static void
diff --git a/src/screenshot-interactive-dialog.c b/src/screenshot-interactive-dialog.c
index f2753ad..4e0a9d7 100644
--- a/src/screenshot-interactive-dialog.c
+++ b/src/screenshot-interactive-dialog.c
@@ -50,13 +50,17 @@ struct _ScreenshotInteractiveDialog
   GtkWidget *screen;
   GtkWidget *window;
   GtkWidget *selection;
-
-  CaptureClickedCallback callback;
-  gpointer user_data;
 };
 
 G_DEFINE_TYPE (ScreenshotInteractiveDialog, screenshot_interactive_dialog, GTK_TYPE_APPLICATION_WINDOW)
 
+enum {
+  SIGNAL_CAPTURE,
+  N_SIGNALS,
+};
+
+static guint signals[N_SIGNALS];
+
 static void
 set_mode (ScreenshotInteractiveDialog *self,
           ScreenshotMode               mode)
@@ -113,11 +117,7 @@ static void
 capture_button_clicked_cb (GtkButton                   *button,
                            ScreenshotInteractiveDialog *self)
 {
-  CaptureClickedCallback callback = self->callback;
-  gpointer user_data = self->user_data;
-
-  gtk_widget_destroy (GTK_WIDGET (self));
-  callback (user_data);
+  g_signal_emit (self, signals[SIGNAL_CAPTURE], 0);
 }
 
 static void
@@ -147,6 +147,15 @@ screenshot_interactive_dialog_class_init (ScreenshotInteractiveDialogClass *klas
 {
   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
 
+  signals[SIGNAL_CAPTURE] =
+    g_signal_new ("capture",
+                  G_TYPE_FROM_CLASS (klass),
+                  G_SIGNAL_RUN_LAST,
+                  0,
+                  NULL, NULL, NULL,
+                  G_TYPE_NONE,
+                  0);
+
   gtk_widget_class_set_template_from_resource (widget_class,
                                                "/org/gnome/Screenshot/ui/screenshot-interactive-dialog.ui");
   gtk_widget_class_bind_template_child (widget_class, ScreenshotInteractiveDialog, capture_button);
@@ -175,23 +184,6 @@ screenshot_interactive_dialog_init (ScreenshotInteractiveDialog *self)
                                 (GtkListBoxUpdateHeaderFunc) header_func,
                                 self,
                                 NULL);
-}
-
-GtkWidget *
-screenshot_interactive_dialog_new (CaptureClickedCallback f,
-                                   gpointer               user_data)
-{
-  ScreenshotApplication *app = user_data;
-  ScreenshotInteractiveDialog *self;
-
-  self = g_object_new (SCREENSHOT_TYPE_INTERACTIVE_DIALOG,
-                       "application", app,
-                       NULL);
-
-  self->callback = f;
-  self->user_data = user_data;
-
-  gtk_widget_show_all (GTK_WIDGET (self));
 
   if (screenshot_config->take_window_shot)
     gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (self->window), TRUE);
@@ -203,6 +195,14 @@ screenshot_interactive_dialog_new (CaptureClickedCallback f,
   gtk_switch_set_active (GTK_SWITCH (self->pointer), screenshot_config->include_pointer);
 
   gtk_adjustment_set_value (self->delay_adjustment, (gdouble) screenshot_config->delay);
+}
 
-  return GTK_WIDGET (self);
+GtkWidget *
+screenshot_interactive_dialog_new (GtkApplication *app)
+{
+  g_return_val_if_fail (GTK_IS_APPLICATION (app), NULL);
+
+  return g_object_new (SCREENSHOT_TYPE_INTERACTIVE_DIALOG,
+                       "application", app,
+                       NULL);
 }
diff --git a/src/screenshot-interactive-dialog.h b/src/screenshot-interactive-dialog.h
index 022b662..e73842c 100644
--- a/src/screenshot-interactive-dialog.h
+++ b/src/screenshot-interactive-dialog.h
@@ -30,8 +30,6 @@ G_BEGIN_DECLS
 
 G_DECLARE_FINAL_TYPE (ScreenshotInteractiveDialog, screenshot_interactive_dialog, SCREENSHOT, 
INTERACTIVE_DIALOG, GtkApplicationWindow)
 
-typedef void (*CaptureClickedCallback) (gpointer *user_data);
-
-GtkWidget *screenshot_interactive_dialog_new (CaptureClickedCallback f, gpointer user_data);
+GtkWidget *screenshot_interactive_dialog_new (GtkApplication *app);
 
 G_END_DECLS


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