| 1 |
#include "adw-demo-page-avatar.h" |
| 2 |
|
| 3 |
#include <glib/gi18n.h> |
| 4 |
|
| 5 |
struct _AdwDemoPageAvatar |
| 6 |
{ |
| 7 |
AdwBin parent_instance; |
| 8 |
|
| 9 |
AdwAvatar *avatar; |
| 10 |
AdwEntryRow *text; |
| 11 |
GtkLabel *file_chooser_label; |
| 12 |
GtkListBox *contacts; |
| 13 |
}; |
| 14 |
|
| 15 |
G_DEFINE_FINAL_TYPE (AdwDemoPageAvatar, adw_demo_page_avatar, ADW_TYPE_BIN) |
| 16 |
|
| 17 |
static char * |
| 18 |
create_random_name (void) |
| 19 |
{ |
| 20 |
static const char *first_names[] = { |
| 21 |
"Adam", |
| 22 |
"Adrian", |
| 23 |
"Anna", |
| 24 |
"Charlotte", |
| 25 |
"Frédérique", |
| 26 |
"Ilaria", |
| 27 |
"Jakub", |
| 28 |
"Jennyfer", |
| 29 |
"Julia", |
| 30 |
"Justin", |
| 31 |
"Mario", |
| 32 |
"Miriam", |
| 33 |
"Mohamed", |
| 34 |
"Nourimane", |
| 35 |
"Owen", |
| 36 |
"Peter", |
| 37 |
"Petra", |
| 38 |
"Rachid", |
| 39 |
"Rebecca", |
| 40 |
"Sarah", |
| 41 |
"Thibault", |
| 42 |
"Wolfgang", |
| 43 |
}; |
| 44 |
static const char *last_names[] = { |
| 45 |
"Bailey", |
| 46 |
"Berat", |
| 47 |
"Chen", |
| 48 |
"Farquharson", |
| 49 |
"Ferber", |
| 50 |
"Franco", |
| 51 |
"Galinier", |
| 52 |
"Han", |
| 53 |
"Lawrence", |
| 54 |
"Lepied", |
| 55 |
"Lopez", |
| 56 |
"Mariotti", |
| 57 |
"Rossi", |
| 58 |
"Urasawa", |
| 59 |
"Zwickelman", |
| 60 |
}; |
| 61 |
|
| 62 |
return g_strdup_printf ("%s %s", |
| 63 |
first_names[g_random_int_range (0, G_N_ELEMENTS (first_names))], |
| 64 |
last_names[g_random_int_range (0, G_N_ELEMENTS (last_names))]); |
| 65 |
} |
| 66 |
|
| 67 |
static void |
| 68 |
populate_contacts (AdwDemoPageAvatar *self) |
| 69 |
{ |
| 70 |
for (int i = 0; i < 30; i++) { |
| 71 |
char *name = create_random_name (); |
| 72 |
GtkWidget *contact = adw_action_row_new (); |
| 73 |
GtkWidget *avatar = adw_avatar_new (40, name, TRUE); |
| 74 |
|
| 75 |
gtk_widget_set_margin_top (avatar, 12); |
| 76 |
gtk_widget_set_margin_bottom (avatar, 12); |
| 77 |
|
| 78 |
adw_preferences_row_set_title (ADW_PREFERENCES_ROW (contact), name); |
| 79 |
adw_action_row_add_prefix (ADW_ACTION_ROW (contact), avatar); |
| 80 |
gtk_list_box_append (self->contacts, contact); |
| 81 |
|
| 82 |
g_free (name); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
static void |
| 87 |
avatar_open_dialog_cb (GtkFileDialog *dialog, |
| 88 |
GAsyncResult *result, |
| 89 |
AdwDemoPageAvatar *self) |
| 90 |
{ |
| 91 |
GFile *file = gtk_file_dialog_open_finish (dialog, result, NULL); |
| 92 |
|
| 93 |
if (file) { |
| 94 |
GFileInfo *info; |
| 95 |
GdkTexture *texture; |
| 96 |
GError *error = NULL; |
| 97 |
|
| 98 |
info = g_file_query_info (file, |
| 99 |
G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME, |
| 100 |
G_FILE_QUERY_INFO_NONE, |
| 101 |
NULL, |
| 102 |
NULL); |
| 103 |
|
| 104 |
if (info) |
| 105 |
gtk_label_set_label (self->file_chooser_label, |
| 106 |
g_file_info_get_display_name (info)); |
| 107 |
|
| 108 |
gtk_widget_action_set_enabled (GTK_WIDGET (self), "avatar.remove", TRUE); |
| 109 |
|
| 110 |
texture = gdk_texture_new_from_file (file, &error); |
| 111 |
if (error) { |
| 112 |
g_critical ("Failed to create texture from file: %s", error->message); |
| 113 |
g_clear_error (&error); |
| 114 |
} |
| 115 |
|
| 116 |
adw_avatar_set_custom_image (self->avatar, texture ? GDK_PAINTABLE (texture) : NULL); |
| 117 |
|
| 118 |
g_clear_object (&info); |
| 119 |
g_clear_object (&texture); |
| 120 |
g_object_unref (file); |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
static void |
| 125 |
avatar_open_cb (AdwDemoPageAvatar *self) |
| 126 |
{ |
| 127 |
GtkRoot *root = gtk_widget_get_root (GTK_WIDGET (self)); |
| 128 |
GtkFileDialog *dialog = gtk_file_dialog_new (); |
| 129 |
|
| 130 |
gtk_file_dialog_set_title (dialog, _("Select an Avatar")); |
| 131 |
|
| 132 |
gtk_file_dialog_open (dialog, |
| 133 |
GTK_WINDOW (root), |
| 134 |
NULL, |
| 135 |
(GAsyncReadyCallback) avatar_open_dialog_cb, |
| 136 |
self); |
| 137 |
|
| 138 |
g_object_unref (dialog); |
| 139 |
} |
| 140 |
|
| 141 |
static void |
| 142 |
avatar_remove_cb (AdwDemoPageAvatar *self) |
| 143 |
{ |
| 144 |
gtk_label_set_label (self->file_chooser_label, _("(None)")); |
| 145 |
gtk_widget_action_set_enabled (GTK_WIDGET (self), "avatar.remove", FALSE); |
| 146 |
adw_avatar_set_custom_image (self->avatar, NULL); |
| 147 |
} |
| 148 |
|
| 149 |
static void |
| 150 |
avatar_save_dialog_cb (GtkFileDialog *dialog, |
| 151 |
GAsyncResult *result, |
| 152 |
AdwDemoPageAvatar *self) |
| 153 |
{ |
| 154 |
GFile *file = gtk_file_dialog_save_finish (dialog, result, NULL); |
| 155 |
|
| 156 |
if (file) { |
| 157 |
GdkTexture *texture = |
| 158 |
adw_avatar_draw_to_texture (self->avatar, |
| 159 |
gtk_widget_get_scale_factor (GTK_WIDGET (self))); |
| 160 |
|
| 161 |
gdk_texture_save_to_png (texture, g_file_peek_path (file)); |
| 162 |
|
| 163 |
g_object_unref (texture); |
| 164 |
g_object_unref (file); |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
static void |
| 169 |
avatar_save_cb (AdwDemoPageAvatar *self) |
| 170 |
{ |
| 171 |
GtkRoot *root = gtk_widget_get_root (GTK_WIDGET (self)); |
| 172 |
GtkFileDialog *dialog = gtk_file_dialog_new (); |
| 173 |
|
| 174 |
gtk_file_dialog_set_title (dialog, _("Save Avatar")); |
| 175 |
|
| 176 |
gtk_file_dialog_save (dialog, |
| 177 |
GTK_WINDOW (root), |
| 178 |
NULL, |
| 179 |
(GAsyncReadyCallback) avatar_save_dialog_cb, |
| 180 |
self); |
| 181 |
|
| 182 |
g_object_unref (dialog); |
| 183 |
} |
| 184 |
|
| 185 |
static void |
| 186 |
adw_demo_page_avatar_class_init (AdwDemoPageAvatarClass *klass) |
| 187 |
{ |
| 188 |
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass); |
| 189 |
|
| 190 |
gtk_widget_class_set_template_from_resource (widget_class, "/org/gnome/Adwaita1/Demo/ui/pages/avatar/adw-demo-page-avatar.ui"); |
| 191 |
gtk_widget_class_bind_template_child (widget_class, AdwDemoPageAvatar, avatar); |
| 192 |
gtk_widget_class_bind_template_child (widget_class, AdwDemoPageAvatar, text); |
| 193 |
gtk_widget_class_bind_template_child (widget_class, AdwDemoPageAvatar, file_chooser_label); |
| 194 |
gtk_widget_class_bind_template_child (widget_class, AdwDemoPageAvatar, contacts); |
| 195 |
|
| 196 |
gtk_widget_class_install_action (widget_class, "avatar.open", NULL, (GtkWidgetActionActivateFunc) avatar_open_cb); |
| 197 |
gtk_widget_class_install_action (widget_class, "avatar.remove", NULL, (GtkWidgetActionActivateFunc) avatar_remove_cb); |
| 198 |
gtk_widget_class_install_action (widget_class, "avatar.save", NULL, (GtkWidgetActionActivateFunc) avatar_save_cb); |
| 199 |
} |
| 200 |
|
| 201 |
static void |
| 202 |
adw_demo_page_avatar_init (AdwDemoPageAvatar *self) |
| 203 |
{ |
| 204 |
char *name; |
| 205 |
|
| 206 |
gtk_widget_init_template (GTK_WIDGET (self)); |
| 207 |
|
| 208 |
name = create_random_name (); |
| 209 |
gtk_editable_set_text (GTK_EDITABLE (self->text), name); |
| 210 |
|
| 211 |
populate_contacts (self); |
| 212 |
avatar_remove_cb (self); |
| 213 |
|
| 214 |
g_free (name); |
| 215 |
} |