[gnome-contacts] Extract LinkSuggestionGrid into a class.



commit 74afe146d7356d6a63e74e182045b0e4029bc841
Author: Niels De Graef <nielsdegraef gmail com>
Date:   Sat Dec 23 18:29:47 2017 +0100

    Extract LinkSuggestionGrid into a class.

 src/contacts-contact-pane.vala         |   81 ++++++--------------------------
 src/contacts-link-suggestion-grid.vala |   80 +++++++++++++++++++++++++++++++
 src/meson.build                        |    1 +
 3 files changed, 95 insertions(+), 67 deletions(-)
---
diff --git a/src/contacts-contact-pane.vala b/src/contacts-contact-pane.vala
index 5ab541d..94bb4c7 100644
--- a/src/contacts-contact-pane.vala
+++ b/src/contacts-contact-pane.vala
@@ -62,7 +62,7 @@ public class Contacts.ContactPane : Stack {
   };
 
   public bool on_edit_mode;
-  public Grid suggestion_grid;
+  private LinkSuggestionGrid suggestion_grid;
 
   /* Signals */
   public signal void contacts_linked (string? main_contact, string linked_contact, LinkOperation operation);
@@ -99,76 +99,23 @@ public class Contacts.ContactPane : Stack {
       suggestion_grid = null;
     }
 
-    suggestion_grid = new Grid ();
-    suggestion_grid.set_valign (Align.END);
-    parent_overlay.add_overlay (suggestion_grid);
-
-    suggestion_grid.get_style_context ().add_class ("contacts-suggestion");
-    suggestion_grid.set_redraw_on_allocate (true);
-    suggestion_grid.draw.connect ( (cr) => {
-       Allocation allocation;
-       suggestion_grid.get_allocation (out allocation);
-
-       var context = suggestion_grid.get_style_context ();
-       context.render_background (cr,
-                                  0, 0,
-                                  allocation.width, allocation.height);
-       return false;
-      });
-
-    var image_frame = new ContactFrame (Contact.SMALL_AVATAR_SIZE);
-    image_frame.set_hexpand (false);
-    image_frame.margin = 24;
-    image_frame.margin_end = 12;
-    c.keep_widget_uptodate (image_frame,  (w) => {
-       (w as ContactFrame).set_image (c.individual, c);
-      });
-
-    suggestion_grid.attach (image_frame, 0, 0, 1, 2);
+    this.suggestion_grid = new LinkSuggestionGrid (c);
+    parent_overlay.add_overlay (this.suggestion_grid);
 
-    var label = new Label ("");
-    if (contact.is_main)
-      label.set_markup (Markup.printf_escaped (_("Does %s from %s belong here?"), c.display_name, 
c.format_persona_stores ()));
-    else
-      label.set_markup (Markup.printf_escaped (_("Do these details belong to %s?"), c.display_name));
-    label.set_valign (Align.START);
-    label.set_halign (Align.START);
-    label.set_line_wrap (true);
-    label.width_chars = 20;
-    label.set_line_wrap_mode (Pango.WrapMode.WORD_CHAR);
-    label.set_hexpand (true);
-    label.margin_top = 24;
-    label.margin_bottom = 24;
-    suggestion_grid.attach (label, 1, 0, 1, 2);
-
-    var bbox = new ButtonBox (Orientation.HORIZONTAL);
-    var yes = new Button.with_label (_("Yes"));
-    var no = new Button.with_label (_("No"));
-
-    yes.clicked.connect ( () => {
-      var linked_contact = c.display_name;
-      link_contacts.begin (contact, c, this.store, (obj, result) => {
-       var operation = link_contacts.end (result);
-       this.contacts_linked (null, linked_contact, operation);
+    this.suggestion_grid.suggestion_accepted.connect ( () => {
+        var linked_contact = c.display_name;
+        link_contacts.begin (contact, c, this.store, (obj, result) => {
+            var operation = link_contacts.end (result);
+            this.contacts_linked (null, linked_contact, operation);
+          });
+        this.suggestion_grid.destroy ();
       });
-      suggestion_grid.destroy ();
-    });
 
-    no.clicked.connect ( () => {
-       store.add_no_suggest_link (contact, c);
-       /* TODO: Add undo */
-       suggestion_grid.destroy ();
+    this.suggestion_grid.suggestion_rejected.connect ( () => {
+        store.add_no_suggest_link (contact, c);
+        /* TODO: Add undo */
+        this.suggestion_grid.destroy ();
       });
-
-    bbox.add (yes);
-    bbox.add (no);
-    bbox.set_spacing (8);
-    bbox.set_halign (Align.END);
-    bbox.set_hexpand (true);
-    bbox.margin = 24;
-    bbox.margin_start = 12;
-    suggestion_grid.attach (bbox, 2, 0, 1, 2);
-    suggestion_grid.show_all ();
   }
 
   public void show_contact (Contact? new_contact, bool show_matches = true) {
diff --git a/src/contacts-link-suggestion-grid.vala b/src/contacts-link-suggestion-grid.vala
new file mode 100644
index 0000000..d49b0cb
--- /dev/null
+++ b/src/contacts-link-suggestion-grid.vala
@@ -0,0 +1,80 @@
+/*
+ * Copyright (C) 2011 Alexander Larsson <alexl 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 2 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/>.
+ */
+
+using Gtk;
+using Folks;
+using Gee;
+
+/**
+ * The LinkSuggestionGrid is show at the bottom of the ContactPane.
+ * It offers the user the sugugestion of linking the currently shown contact
+ * and another (hopefully) similar contact.
+ */
+public class Contacts.LinkSuggestionGrid : Grid {
+
+  public signal void suggestion_accepted ();
+  public signal void suggestion_rejected ();
+
+  public LinkSuggestionGrid (Contact contact) {
+    this.valign = Align.END;
+
+    get_style_context ().add_class ("contacts-suggestion");
+    set_redraw_on_allocate (true);
+
+    var image_frame = new ContactFrame (Contact.SMALL_AVATAR_SIZE);
+    image_frame.hexpand = false;
+    image_frame.margin = 24;
+    image_frame.margin_end = 12;
+    contact.keep_widget_uptodate (image_frame,  (w) => {
+        (w as ContactFrame).set_image (contact.individual, contact);
+      });
+
+    attach (image_frame, 0, 0);
+
+    var label = new Label ("");
+    if (contact.is_main)
+      label.set_markup (Markup.printf_escaped (_("Does %s from %s belong here?"), contact.display_name, 
contact.format_persona_stores ()));
+    else
+      label.set_markup (Markup.printf_escaped (_("Do these details belong to %s?"), contact.display_name));
+    label.valign = Align.START;
+    label.halign = Align.START;
+    label.width_chars = 20;
+    label.wrap = true;
+    label.wrap_mode = Pango.WrapMode.WORD_CHAR;
+    label.hexpand = true;
+    label.margin_top = 24;
+    label.margin_bottom = 24;
+    attach (label, 1, 0);
+
+    var bbox = new ButtonBox (Orientation.HORIZONTAL);
+    var yes = new Button.with_label (_("Yes"));
+    var no = new Button.with_label (_("No"));
+
+    yes.clicked.connect ( () => suggestion_accepted ());
+    no.clicked.connect ( () => suggestion_rejected ());
+
+    bbox.add (yes);
+    bbox.add (no);
+    bbox.set_spacing (8);
+    bbox.set_halign (Align.END);
+    bbox.set_hexpand (true);
+    bbox.margin = 24;
+    bbox.margin_start = 12;
+    attach (bbox, 2, 0);
+    show_all ();
+  }
+}
diff --git a/src/meson.build b/src/meson.build
index 0bb6638..708c8d7 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -16,6 +16,7 @@ contacts_vala_sources = [
   'contacts-contact-sheet.vala',
   'contacts-contact.vala',
   'contacts-in-app-notification.vala',
+  'contacts-link-suggestion-grid.vala',
   'contacts-linked-accounts-dialog.vala',
   'contacts-linking.vala',
   'contacts-list-pane.vala',


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