Debian Codegraph @

demo/pages/alerts/adw-demo-page-alerts.c

raw
1 #include "adw-demo-page-alerts.h"
2
3 #include <glib/gi18n.h>
4
5 struct _AdwDemoPageAlerts
6 {
7 AdwBin parent_instance;
8
9 AdwToast *last_toast;
10 };
11
12 G_DEFINE_FINAL_TYPE (AdwDemoPageAlerts, adw_demo_page_alerts, ADW_TYPE_BIN)
13
14 static void
15 toast_dismissed_cb (AdwToast *toast,
16 AdwDemoPageAlerts *self)
17 {
18 if (toast == self->last_toast)
19 self->last_toast = NULL;
20 }
21
22 static void
23 alert_cb (AdwAlertDialog *dialog,
24 GAsyncResult *result,
25 AdwDemoPageAlerts *self)
26 {
27 GtkWidget *toast_overlay = gtk_widget_get_ancestor (GTK_WIDGET (self), ADW_TYPE_TOAST_OVERLAY);
28 const char *response = adw_alert_dialog_choose_finish (dialog, result);
29 AdwToast *toast = adw_toast_new_format (_("Dialog response: %s"), response);
30 g_signal_connect_object (toast, "dismissed", G_CALLBACK (toast_dismissed_cb), self, 0);
31
32 g_clear_pointer (&self->last_toast, adw_toast_dismiss);
33 self->last_toast = toast;
34
35 adw_toast_overlay_add_toast (ADW_TOAST_OVERLAY (toast_overlay), toast);
36 }
37
38 static void
39 demo_alert_dialog_cb (AdwDemoPageAlerts *self)
40 {
41 AdwDialog *dialog;
42
43 dialog = adw_alert_dialog_new (_("Save Changes?"),
44 _("Open document contains unsaved changes. Changes which are not saved will be permanently lost."));
45
46 adw_alert_dialog_add_responses (ADW_ALERT_DIALOG (dialog),
47 "cancel", _("_Cancel"),
48 "discard", _("_Discard"),
49 "save", _("_Save"),
50 NULL);
51
52 adw_alert_dialog_set_response_appearance (ADW_ALERT_DIALOG (dialog),
53 "discard",
54 ADW_RESPONSE_DESTRUCTIVE);
55 adw_alert_dialog_set_response_appearance (ADW_ALERT_DIALOG (dialog),
56 "save",
57 ADW_RESPONSE_SUGGESTED);
58
59 adw_alert_dialog_set_default_response (ADW_ALERT_DIALOG (dialog), "save");
60 adw_alert_dialog_set_close_response (ADW_ALERT_DIALOG (dialog), "cancel");
61
62 adw_alert_dialog_choose (ADW_ALERT_DIALOG (dialog), GTK_WIDGET (self), NULL,
63 (GAsyncReadyCallback) alert_cb, self);
64 }
65
66 static void
67 adw_demo_page_alerts_class_init (AdwDemoPageAlertsClass *klass)
68 {
69 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
70
71 gtk_widget_class_set_template_from_resource (widget_class, "/org/gnome/Adwaita1/Demo/ui/pages/alerts/adw-demo-page-alerts.ui");
72
73 gtk_widget_class_install_action (widget_class, "demo.alert-dialog", NULL, (GtkWidgetActionActivateFunc) demo_alert_dialog_cb);
74 }
75
76 static void
77 adw_demo_page_alerts_init (AdwDemoPageAlerts *self)
78 {
79 gtk_widget_init_template (GTK_WIDGET (self));
80 }