[geary/wip/778025-conversation-height: 1/3] Adjust conversation height dynamically when zooming



commit b107cd23538933618e4d2f74cea8df213055e156
Author: Michael Gratton <mike vee net>
Date:   Sun Oct 14 11:14:31 2018 +1100

    Adjust conversation height dynamically when zooming
    
    Follow-up to commit 14bb8161, this ensures the current conversation
    viewer's height is updated when zooming in/out/reset by keeping the web
    content's height around and recalculating and queuing a resize when the
    zoom level is changed.

 src/client/components/client-web-view.vala         | 43 +++++++++++++---------
 .../conversation-viewer/conversation-list-box.vala |  2 +-
 .../conversation-viewer/conversation-web-view.vala |  2 +
 3 files changed, 28 insertions(+), 19 deletions(-)
---
diff --git a/src/client/components/client-web-view.vala b/src/client/components/client-web-view.vala
index 898ae1f1..3b375d98 100644
--- a/src/client/components/client-web-view.vala
+++ b/src/client/components/client-web-view.vala
@@ -202,8 +202,14 @@ public class ClientWebView : WebKit.WebView, Geary.BaseInterface {
     /** Determines if the view has started rendering the HTML */
     public bool has_valid_height { get; private set; default = false; }
 
-    /** The HTML content's current preferred height. */
-    public int preferred_height { get; private set; default = 0; }
+    /** The HTML content's current preferred height in window pixels. */
+    public int preferred_height {
+        get {
+            return (int) GLib.Math.ceil(
+                this.webkit_reported_height * this.zoom_level
+            );
+        }
+    }
 
     public string document_font {
         get {
@@ -243,6 +249,8 @@ public class ClientWebView : WebKit.WebView, Geary.BaseInterface {
     private Gee.List<ulong> registered_message_handlers =
         new Gee.LinkedList<ulong>();
 
+    private double webkit_reported_height = 0;
+
 
     /**
      * Emitted when the view's content has finished loaded.
@@ -408,8 +416,11 @@ public class ClientWebView : WebKit.WebView, Geary.BaseInterface {
         execute_editing_command(WebKit.EDITING_COMMAND_COPY);
     }
 
-    public void reset_zoom() {
-        this.zoom_level == ZOOM_DEFAULT;
+    public void zoom_reset() {
+        this.zoom_level = ZOOM_DEFAULT;
+        // Notify the preferred height has changed since it depends on
+        // the zoom level. Same for zoom in and out below.
+        notify_property("preferred-height");
     }
 
     public void zoom_in() {
@@ -418,6 +429,7 @@ public class ClientWebView : WebKit.WebView, Geary.BaseInterface {
             new_zoom = ZOOM_MAX;
         }
         this.zoom_level = new_zoom;
+        notify_property("preferred-height");
     }
 
     public void zoom_out() {
@@ -426,6 +438,7 @@ public class ClientWebView : WebKit.WebView, Geary.BaseInterface {
             new_zoom = ZOOM_MIN;
         }
         this.zoom_level = new_zoom;
+        notify_property("preferred-height");
     }
 
     /**
@@ -555,24 +568,18 @@ public class ClientWebView : WebKit.WebView, Geary.BaseInterface {
     }
 
     private void on_preferred_height_changed(WebKit.JavascriptResult result) {
+        double height = this.webkit_reported_height;
         try {
-            int height = (int) GLib.Math.ceil(WebKitUtil.to_number(result) * this.zoom_level);
-            // Avoid notifying if the values have not changed
-            if (this.preferred_height != height) {
-                // value has changed
-                this.preferred_height = height;
-                if (height >= 1) {
-                    // value is valid
-                    if (!this.has_valid_height) {
-                        // validity has changed
-                        this.has_valid_height = true;
-                    }
-                    queue_resize();
-                }
-            }
+            height = WebKitUtil.to_number(result);
+            this.has_valid_height = true;
         } catch (Geary.JS.Error err) {
             debug("Could not get preferred height: %s", err.message);
         }
+
+        if (this.webkit_reported_height != height) {
+            this.webkit_reported_height = height;
+            notify_property("preferred-height");
+        }
     }
 
     private void on_remote_image_load_blocked(WebKit.JavascriptResult result) {
diff --git a/src/client/conversation-viewer/conversation-list-box.vala 
b/src/client/conversation-viewer/conversation-list-box.vala
index 6ee9059f..70365506 100644
--- a/src/client/conversation-viewer/conversation-list-box.vala
+++ b/src/client/conversation-viewer/conversation-list-box.vala
@@ -725,7 +725,7 @@ public class ConversationListBox : Gtk.ListBox, Geary.BaseInterface {
      */
     public void zoom_reset() {
         message_view_iterator().foreach((msg_view) => {
-                msg_view.web_view.zoom_level = 1.0f;
+                msg_view.web_view.zoom_reset();
                 return true;
             });
     }
diff --git a/src/client/conversation-viewer/conversation-web-view.vala 
b/src/client/conversation-viewer/conversation-web-view.vala
index 217d0a64..85cafa54 100644
--- a/src/client/conversation-viewer/conversation-web-view.vala
+++ b/src/client/conversation-viewer/conversation-web-view.vala
@@ -72,6 +72,8 @@ public class ConversationWebView : ClientWebView {
         register_message_handler(
             DECEPTIVE_LINK_CLICKED, on_deceptive_link_clicked
         );
+
+        this.notify["preferred-height"].connect(() => queue_resize());
     }
 
     /**


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