[gnome-boxes] vnc-display: Keep aspect ratio when scaling display



commit 8016cef3fd595666fe8d467e6f8e67a29df3d2e8
Author: Felipe Borges <felipeborges gnome org>
Date:   Mon Mar 13 18:11:02 2017 +0100

    vnc-display: Keep aspect ratio when scaling display
    
    A scaled VNC display is barely readable in portrait devices (with
    reverse aspect ratios, such as 3:4, 5:8, 9:16).
    
    There is not benefit on scaling a display without preserving its
    aspect ratio.
    
    The logic behind the scaling is:
    
    Given a display ratio D = display_width / display_height and a
    available_space ratio A = available_width / available_height.
    
    if (D > A)
      scaled_size = (available_width * D, available_height)
    else
      scaled_size = (available_width, available_width * D)
    
    The resulting allocation structure is also centered (scaled.[x/y])
    
    https://bugzilla.gnome.org/show_bug.cgi?id=779991

 src/vnc-display.vala |   24 ++++++++++++++++++++++++
 1 files changed, 24 insertions(+), 0 deletions(-)
---
diff --git a/src/vnc-display.vala b/src/vnc-display.vala
index abf9b02..fb55103 100644
--- a/src/vnc-display.vala
+++ b/src/vnc-display.vala
@@ -69,6 +69,8 @@ private class Boxes.VncDisplay: Boxes.Display {
 
             display.close ();
         });
+
+        display.size_allocate.connect (scale);
     }
 
     public VncDisplay (BoxConfig config, string host, int port) {
@@ -142,4 +144,26 @@ private class Boxes.VncDisplay: Boxes.Display {
     public override void send_keys (uint[] keyvals) {
         display.send_keys (keyvals);
     }
+
+    public void scale () {
+        if (!display.is_open ())
+            return;
+
+        // Get the allocated size of the parent container
+        Gtk.Allocation alloc;
+        display.get_parent ().get_allocation (out alloc);
+
+        double parent_aspect = (double) alloc.width / (double) alloc.height;
+        double display_aspect = (double) display.get_width () / (double) display.get_height ();
+        Gtk.Allocation scaled = alloc;
+        if (parent_aspect > display_aspect) {
+            scaled.width = (int) (alloc.height * display_aspect);
+            scaled.x += (alloc.width - scaled.width) / 2;
+        } else {
+            scaled.height = (int) (alloc.width / display_aspect);
+            scaled.y += (alloc.height - scaled.height) / 2;
+        }
+
+        display.size_allocate (scaled);
+    }
 }


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