Ticket #9268: tb.resize.txt

File tb.resize.txt, 5.6 KB (added by cypherpunks, 6 years ago)

Resize window till everything rounded and fits.

Line 
1--- torbutton.js
2+++ torbutton.js.modified
3@@ -2869,6 +2869,68 @@
4   { /*torbutton_eclog(1, 'called linkIcon'); */ return 0; }
5 }
6 
7+/* Call it to resize window. Later is better, then it counts
8+   all appeared bars to proper round inner sizes */
9+function torbutton_resize_window(win) {
10+    var diff_height = window.outerHeight - win.innerHeight;
11+    var diff_width = window.outerWidth - win.innerWidth;
12+
13+    /* Re: f2b3ddc18d
14+       window.screen not lies about sizes,
15+       it returns maximum outer size available.
16+       We no need screen resolution for desktop here,
17+       which can't to count DPI settings, taskbar or something else  */
18+
19+    var maxHeight = window.screen.availHeight - diff_height;
20+    var maxWidth = window.screen.availWidth - diff_width ;
21+
22+    var width = 0;
23+    var height = 0;
24+
25+    /* Resize window till everything rounded and fits.
26+       Limit number of tries to prevent infinite loop.
27+       (If no random changes then 1 or 2 times should be
28+        enough, lets be ready for something unknown yet) */
29+    for (var i = 0; i < 5; i++) {
30+
31+      torbutton_log(3, "About to resize window: " +
32+                        window.outerWidth + "x" + window.outerHeight +
33+                       " inner: " + win.innerWidth + "x" + win.innerHeight +
34+                       " in state " + window.windowState +
35+                       " Available: " + window.screen.availWidth + "x" +
36+                       window.screen.availHeight);
37+
38+
39+      if (maxWidth > 1000) {
40+        width = 1000;
41+      } else {
42+        width = Math.floor(maxWidth/200.0)*200;
43+      }
44+
45+      height = Math.floor(maxHeight/100.0)*100;
46
47+      torbutton_log(3, "Max dimensions for resize: " + maxWidth + "x" +
48+                       maxHeight + " Requested: " + width + "x" + height);
49+      win.resizeBy(width - win.innerWidth, height - win.innerHeight);
50+      torbutton_log(3, "After resize, inner size: " + win.innerWidth +
51+                       "x" + win.innerHeight);
52+
53+      /* everything ok, rounded and fits */
54+      if (win.innerHeight == height && win.innerWidth == width)
55+        break;
56+
57+      /* if result is higher then something wrong. */
58+      if (win.innerHeight > height || win.innerWidth > width) {
59+        torbutton_log(5, "Result of resize is wrong." +
60+                         " Got: " + maxWidth + "x" + maxHeight +
61+                         " Requested: " + width + "x" + height);
62+        break;
63+      }
64+
65+      maxHeight = win.innerHeight;
66+      maxWidth = win.innerWidth;
67+    }
68+}
69 // Bug 1506 P1/P3: Setting a fixed window size is important, but
70 // probably not for android.
71 var torbutton_resizelistener =
72@@ -2894,10 +2956,6 @@
73         progress.removeProgressListener(this);
74         return;
75       }
76-      var screenMan = Components.classes["@mozilla.org/gfx/screenmanager;1"].
77-        getService(Components.interfaces.nsIScreenManager);
78-      var junk = {}, availWidth = {}, availHeight = {};
79-      screenMan.primaryScreen.GetRect(junk, junk, availWidth, availHeight);
80 
81       // We need to set the inner width to an initial value because it has none
82       // at this point... Choosing "300" as this works even on Windows
83@@ -2905,32 +2963,12 @@
84       win.innerWidth = 300;
85       win.innerHeight = 300;
86 
87-      torbutton_log(3, "About to resize new window: " + window.outerWidth +
88-        "x" + window.outerHeight + " inner: " + win.innerWidth + "x" + win.
89-        innerHeight + " in state " + window.windowState + " Have " +
90-        availWidth.value + "x" + availHeight.value);
91-
92-      var maxHeight = availHeight.value -
93-        (window.outerHeight - win.innerHeight) - 51;
94-      var maxWidth = availWidth.value - (window.outerWidth - win.innerWidth);
95-      torbutton_log(3, "Got max dimensions: " + maxWidth + "x" + maxHeight);
96-
97-      var width;
98-      var height;
99-
100-      if (maxWidth > 1000) {
101-        width = 1000;
102-      } else {
103-        width = Math.floor(maxWidth/200.0)*200;
104-      }
105-
106-      height = Math.floor(maxHeight/100.0)*100;
107 
108       var handler = function() {
109         if (window.windowState === 1) {
110           window.addEventListener("resize",
111             function() {
112-              win.resizeBy(width - win.innerWidth, height - win.innerHeight);
113+              torbutton_resize_window(win);
114               var calling_function = arguments.callee;
115               setTimeout(function() {
116                            torbutton_log(3, "Removing resize listener..");
117@@ -2969,10 +3007,7 @@
118       // This is fun. any attempt to directly set the inner window actually
119       // resizes the outer width to that value instead. Must use resizeBy()
120       // instead of assignment or resizeTo()
121-      win.resizeBy(width - win.innerWidth, height - win.innerHeight);
122-      torbutton_log(3, "Resized new window from: " + win.innerWidth + "x" +
123-        win.innerHeight + " to " + width + "x" + height + " in state " +
124-        window.windowState);
125+      torbutton_resize_window(win);
126 
127       // Resizing within this progress listener does not always work as overlays
128       // of other extensions might still influence the height/width of the
129@@ -2989,8 +3024,7 @@
130               torbutton_log(3, "Mutation observer: Window dimensions are: " +
131                 win.innerWidth + " x " + win.innerHeight);
132               setTimeout(function() {
133-                           win.resizeBy(width - win.innerWidth,
134-                             height - win.innerHeight);
135+                           torbutton_resize_window(win);
136                            torbutton_log(3, "Mutation observer: Window " +
137                              "dimensions are (after resizing again): " + win.
138                              innerWidth + " x " + win.innerHeight);