[gnome-contacts/wip/nielsdg/avatar-no-button] Avatar: don't use a button.



commit be21b8feaa212b441f456439ba8b2564b0985c04
Author: Niels De Graef <nielsdegraef gmail com>
Date:   Thu Jan 18 23:54:37 2018 +0100

    Avatar: don't use a button.
    
    Leave all the button logic out of it and just be a simple DrawingArea.
    
    NOTE: we temporarily lose our border because of this, but with the new
    avatars, this should no longer be a problem.

 data/ui/style.css                |    9 +-----
 src/contacts-avatar-dialog.vala  |   47 ++++++++++++++++++++-----------------
 src/contacts-avatar.vala         |   34 ++++-----------------------
 src/contacts-contact-editor.vala |   15 ++++++-----
 4 files changed, 40 insertions(+), 65 deletions(-)
---
diff --git a/data/ui/style.css b/data/ui/style.css
index ed45e38..ea89ca8 100644
--- a/data/ui/style.css
+++ b/data/ui/style.css
@@ -52,9 +52,8 @@ row.contact-data-row {
   border-top-left-radius: 0;
 }
 
-/* Draw a border around the avatar */
-.contacts-avatar border {
-  border-radius: 5px;
+/* An avatar */
+.contacts-avatar {
 }
 
 /* Give the avatar in the ContactSheet some margin,
@@ -85,10 +84,6 @@ ContactsWindow .primary-toolbar.toolbar {
  border-width: 1px;
 }
 
-.button.contacts-square {
- padding: 0px;
-}
-
 /* The style for the background "watermark" image and text.
  * (copied from dim-label) */
 .contacts-watermark {
diff --git a/src/contacts-avatar-dialog.vala b/src/contacts-avatar-dialog.vala
index 293a3f8..d8990a1 100644
--- a/src/contacts-avatar-dialog.vala
+++ b/src/contacts-avatar-dialog.vala
@@ -162,41 +162,44 @@ public class Contacts.AvatarDialog : Dialog {
     return pixbuf.scale_simple (w, h, Gdk.InterpType.HYPER);
   }
 
-  private Avatar create_frame (Gdk.Pixbuf source_pixbuf) {
-    var avatar = new Avatar (ICONS_SIZE, true);
+  private Button create_thumbnail (Gdk.Pixbuf source_pixbuf) {
+    var avatar = new Avatar (ICONS_SIZE);
     var pixbuf = source_pixbuf.scale_simple (ICONS_SIZE, ICONS_SIZE, Gdk.InterpType.HYPER);
     avatar.set_pixbuf (pixbuf);
-    var avatar_pixbuf = scale_pixbuf_for_avatar_use (source_pixbuf);
-    avatar.clicked.connect ( () => {
-        selected_pixbuf (avatar_pixbuf);
+
+    var button = new Button ();
+    button.get_style_context ().add_class ("flat");
+    button.image = avatar;
+    button.clicked.connect ( () => {
+        selected_pixbuf (scale_pixbuf_for_avatar_use (source_pixbuf));
       });
-    return avatar;
+
+    return button;
   }
 
-  private Avatar? frame_for_persona (Persona persona) {
+  private Button? thumbnail_for_persona (Persona persona) {
     var details = persona as AvatarDetails;
     if (details == null || details.avatar == null)
       return null;
 
     try {
       var stream = details.avatar.load (128, null);
-      var pixbuf = new Gdk.Pixbuf.from_stream (stream);
-      return create_frame (pixbuf);
-    }
-    catch {
+      return create_thumbnail (new Gdk.Pixbuf.from_stream (stream));
+    } catch {
+      debug ("Couldn't create frame for persona \"%s\".", persona.display_id);
     }
 
     return null;
   }
 
-  private Avatar? frame_for_filename (string filename) {
-    Avatar? avatar = null;
+  private Button? thumbnail_for_filename (string filename) {
     try {
-      var pixbuf = new Gdk.Pixbuf.from_file (filename);
-      return create_frame (pixbuf);
+      return create_thumbnail (new Gdk.Pixbuf.from_file (filename));
     } catch {
+      debug ("Couldn't create frame for file \"%s\".", filename);
     }
-    return avatar;
+
+    return null;
   }
 
   private void selected_pixbuf (Gdk.Pixbuf pixbuf) {
@@ -210,18 +213,18 @@ public class Contacts.AvatarDialog : Dialog {
   private void update_thumbnail_grids () {
     if (this.contact != null) {
       foreach (var p in contact.individual.personas) {
-        Avatar? frame = frame_for_persona (p);
-        if (frame != null)
-          this.personas_thumbnail_grid.add (frame);
+        var button = thumbnail_for_persona (p);
+        if (button != null)
+          this.personas_thumbnail_grid.add (button);
       }
     }
     this.personas_thumbnail_grid.show_all ();
 
     var stock_files = Utils.get_stock_avatars ();
     foreach (var file_name in stock_files) {
-      Avatar? frame = frame_for_filename (file_name);
-      if (frame != null)
-        this.stock_thumbnail_grid.add (frame);
+      var button = thumbnail_for_filename (file_name);
+      if (button != null)
+        this.stock_thumbnail_grid.add (button);
     }
     this.stock_thumbnail_grid.show_all ();
   }
diff --git a/src/contacts-avatar.vala b/src/contacts-avatar.vala
index 23ca495..5b0d1d6 100644
--- a/src/contacts-avatar.vala
+++ b/src/contacts-avatar.vala
@@ -23,42 +23,18 @@ using Gee;
  * The Avatar of a Contact is responsible for showing an {@link Individual}'s
  * avatar, or a fallback if it's not available.
  */
-public class Contacts.Avatar : Frame {
+public class Contacts.Avatar : DrawingArea {
   private int size;
   private Gdk.Pixbuf? pixbuf;
   private Pango.Layout? layout;
 
-  public signal void clicked ();
-
-  public Avatar (int size, bool with_button = false) {
+  public Avatar (int size) {
     this.size = size;
+    set_size_request (size, size);
 
     get_style_context ().add_class ("contacts-avatar");
 
-    var image = new Image ();
-    image.set_size_request (size, size);
-
-    if (with_button) {
-      var button = new Button ();
-      button.get_accessible ().set_name (_("Change avatar"));
-      button.get_style_context ().add_class ("contacts-square");
-      button.set_relief (ReliefStyle.NONE);
-      button.set_focus_on_click (false);
-      button.add (image);
-
-      button.clicked.connect ( () => {
-         this.clicked ();
-       });
-
-      this.add (button);
-      this.shadow_type = ShadowType.NONE;
-    } else {
-      this.add (image);
-    }
-
-    image.show ();
-    image.draw.connect (draw_image);
-
+    show ();
   }
 
   public void set_pixbuf (Gdk.Pixbuf a_pixbuf) {
@@ -84,7 +60,7 @@ public class Contacts.Avatar : Frame {
     set_pixbuf (a_pixbuf);
   }
 
-  public bool draw_image (Cairo.Context cr) {
+  public override bool draw (Cairo.Context cr) {
     cr.save ();
 
     if (pixbuf != null) {
diff --git a/src/contacts-contact-editor.vala b/src/contacts-contact-editor.vala
index 26130b1..922ee7e 100644
--- a/src/contacts-contact-editor.vala
+++ b/src/contacts-contact-editor.vala
@@ -942,11 +942,12 @@ public class Contacts.ContactEditor : Grid {
 
   // Creates the contact's current avatar in a big button on top of the Editor
   private void create_avatar_button () {
-    this.avatar = new Avatar (PROFILE_SIZE, true);
-    this.avatar.vexpand = false;
-    this.avatar.valign = Align.START;
-    (this.avatar.get_child () as Button).relief = ReliefStyle.NORMAL;
-    this.avatar.clicked.connect (on_avatar_frame_clicked);
+    this.avatar = new Avatar (PROFILE_SIZE);
+
+    var button = new Button ();
+    button.get_accessible ().set_name (_("Change avatar"));
+    button.image = this.avatar;
+    button.clicked.connect (on_avatar_button_clicked);
 
     if (this.contact != null) {
       this.contact.keep_widget_uptodate (this.avatar,  (w) => {
@@ -956,11 +957,11 @@ public class Contacts.ContactEditor : Grid {
       this.avatar.set_image (null, null);
     }
 
-    this.container_grid.attach (this.avatar, 0, 0, 1, 3);
+    this.container_grid.attach (button, 0, 0, 1, 3);
   }
 
   // Show the avatar dialog when the avatar is clicked
-  private void on_avatar_frame_clicked () {
+  private void on_avatar_button_clicked (Button avatar_button) {
     var dialog = new AvatarDialog ((Window) get_toplevel (), this.contact);
     dialog.set_avatar.connect ( (icon) =>  {
         this.avatar.set_data ("value", icon);


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