Skip to content

Commit 9f3e1a5

Browse files
author
zhourenjian
committed
Improving default mousemove performance in browser (Or may cause 100% CPU)
1 parent 39c70c7 commit 9f3e1a5

File tree

8 files changed

+88
-108
lines changed

8 files changed

+88
-108
lines changed

sources/net.sf.j2s.java.org.eclipse.swt/src/org/eclipse/swt/widgets/ColorDialog.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ protected void releaseHandle() {
413413
colorMap[colors[i4]] = arr;
414414
map[colors[i4]] = arr.join (',');
415415
}
416-
*/ {}
416+
*/ { map.toString(); colors.toString(); }
417417

418418
final String[] colorMatrix = new String[] { "salmon", "pale yellow",
419419
"pale green", "spring green", "pale turquoise",
@@ -599,7 +599,7 @@ public void run() {
599599
/**
600600
* @j2sNative
601601
* rgbColor = colorMap[matrix[i]];
602-
*/ {}
602+
*/ { map.toString(); matrix.toString(); }
603603
HSL hsl = new HSL(0, 0, 0);
604604
hsl.fromRGB(new RGB(rgbColor[0], rgbColor[1], rgbColor[2]));
605605
updateFromHSL(hsl.h, hsl.s, hsl.l);

sources/net.sf.j2s.java.org.eclipse.swt/src/org/eclipse/swt/widgets/Decorations.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,8 @@ void bringToTop () {
210210
}
211211

212212
void bringToTop (boolean parentShell, boolean childShells) {
213+
Display.topMaxShell = null;
214+
Display.topMaxShellNeedUpdated = true;
213215
if (parentShell && childShells) {
214216
Display.deactivateAllTitleBarShells();
215217
}
@@ -1731,6 +1733,8 @@ public void setMaximized (boolean maximized) {
17311733
OS.UpdateWindow (handle);
17321734
}
17331735
*/
1736+
Display.topMaxShell = null;
1737+
Display.topMaxShellNeedUpdated = true;
17341738
this.maximized = maximized;
17351739
String key = "shell-maximized";
17361740
Element b = document.body;
@@ -1805,7 +1809,8 @@ public void setMaximized (boolean maximized) {
18051809
if (titleBar != null) {
18061810
OS.addCSSClass(titleBar, key);
18071811
}
1808-
handle.style.zIndex = Display.getNextZIndex(true);
1812+
window.currentTopZIndex++;
1813+
handle.style.zIndex = window.currentTopZIndex;
18091814
if (contentHandle != null) {
18101815
window.setTimeout(Clazz.makeFunction(new Runnable() {
18111816

@@ -1965,6 +1970,8 @@ public void setMinimized (boolean minimized) {
19651970
OS.ShowWindow (handle, flags);
19661971
OS.UpdateWindow (handle);
19671972
*/
1973+
Display.topMaxShell = null;
1974+
Display.topMaxShellNeedUpdated = true;
19681975
if (!minimized) {
19691976
if (this.maximized) {
19701977
this.minimized = minimized;
@@ -2264,8 +2271,8 @@ public void run() {
22642271
}
22652272
};
22662273

2267-
Display.getNextZIndex(true);
2268-
handle.style.zIndex = Display.getNextZIndex(true);
2274+
window.currentTopZIndex += 2;
2275+
handle.style.zIndex = window.currentTopZIndex;
22692276
}
22702277

22712278
/**

sources/net.sf.j2s.java.org.eclipse.swt/src/org/eclipse/swt/widgets/DesktopItem.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public abstract class DesktopItem {
2626
private Runnable leaving;
2727

2828
private int leavingTimeoutHandle = 0;
29-
29+
3030
public void updateLastModified() {
3131
this.lastUpdated = new Date().getTime();
3232
mouseAlreadyMoved = false;

sources/net.sf.j2s.java.org.eclipse.swt/src/org/eclipse/swt/widgets/Display.java

Lines changed: 48 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,10 @@ public class Display extends Device {
393393
static int bodyScrollTop, bodyScrollLeft;
394394
static int htmlScrollTop, htmlScrollLeft;
395395

396-
static int AUTO_HIDE_DELAY = 2000;
396+
static final int AUTO_HIDE_DELAY = 2000;
397+
398+
static boolean topMaxShellNeedUpdated = true;
399+
static Shell topMaxShell = null;
397400

398401
/* Private SWT Window Messages */
399402
/*
@@ -2512,20 +2515,49 @@ public void run() {
25122515
// Hacks: Return as quickly as possible to avoid CPU 100%
25132516
int x = e.clientX;
25142517
int y = e.clientY;
2515-
if (x > 220 && y > 32 && y < height - 90) {
2518+
if (x > 264 && y >= 100 && y <= height - 128) {
25162519
return;
25172520
}
25182521

25192522
long now = new Date().getTime();
25202523
boolean ctrlKey = e.ctrlKey;
2521-
boolean handled = taskBar.handleMouseMove(now, x, y, ctrlKey);
2522-
//if (handled) return;
2523-
handled = shortcutBar.handleMouseMove(now, x, y, ctrlKey);
2524-
//if (handled) return;
2525-
handled = trayCorner.handleMouseMove(now, x, y, ctrlKey);
2526-
//if (handled) return;
2527-
handled = topBar.handleMouseMove(now, x, y, ctrlKey);
2528-
if (handled) return;
2524+
taskBar.mouseAlreadyMoved = true;
2525+
shortcutBar.mouseAlreadyMoved = true;
2526+
trayCorner.mouseAlreadyMoved = true;
2527+
topBar.mouseAlreadyMoved = true;
2528+
boolean inDelay = (now - taskBar.lastUpdated <= Display.AUTO_HIDE_DELAY);
2529+
2530+
if (taskBar.barEl != null && x < 264) {
2531+
if (taskBar.isApproaching(now, x, y, ctrlKey)) {
2532+
taskBar.handleApproaching();
2533+
} else if (!inDelay && taskBar.isLeaving(now, x, y, ctrlKey)) {
2534+
taskBar.handleLeaving();
2535+
}
2536+
}
2537+
2538+
if (y > height - 128) {
2539+
if (shortcutBar.isApproaching(now, x, y, ctrlKey)) {
2540+
shortcutBar.handleApproaching();
2541+
} else if (!inDelay && shortcutBar.isLeaving(now, x, y, ctrlKey)) {
2542+
shortcutBar.handleLeaving();
2543+
}
2544+
}
2545+
2546+
if (x + y < 200) {
2547+
if (trayCorner.isApproaching(now, x, y, ctrlKey)) {
2548+
trayCorner.handleApproaching();
2549+
} else if (!inDelay && trayCorner.isLeaving(now, x, y, ctrlKey)) {
2550+
trayCorner.handleLeaving();
2551+
}
2552+
}
2553+
2554+
if (y < 100) {
2555+
if (topBar.isApproaching(now, x, y, ctrlKey)) {
2556+
topBar.handleApproaching();
2557+
} else if (!inDelay && topBar.isLeaving(now, x, y, ctrlKey)) {
2558+
topBar.handleLeaving();
2559+
}
2560+
}
25292561
}
25302562

25312563
};
@@ -4555,14 +4587,6 @@ static int wcsToMbcs (char ch) {
45554587
}
45564588
*/
45574589

4558-
static int getNextZIndex(boolean increase) {
4559-
int zIndex = window.currentTopZIndex + 1;
4560-
if (increase) {
4561-
window.currentTopZIndex = zIndex;
4562-
}
4563-
return zIndex;
4564-
}
4565-
45664590
static Tray getTray() {
45674591
Tray tray = null;
45684592
if (Default != null) {
@@ -4624,6 +4648,9 @@ static void deactivateAllTitleBarShells() {
46244648
}
46254649

46264650
static Shell getTopMaximizedShell() {
4651+
if (!topMaxShellNeedUpdated) {
4652+
return topMaxShell; // Cached shell to avoid mousemove's CPU 100% bug
4653+
}
46274654
// find the top maximized shell
46284655
Shell lastShell = null;
46294656
int lastMaxZIndex = 0;
@@ -4648,6 +4675,8 @@ static Shell getTopMaximizedShell() {
46484675
}
46494676
}
46504677
}
4678+
topMaxShell = lastShell;
4679+
topMaxShellNeedUpdated = false;
46514680
return lastShell;
46524681
}
46534682
public void updateLayout() {
@@ -4732,6 +4761,7 @@ static void releaseAllDisplays() {
47324761
Displays = null;
47334762
}
47344763
Default = null; // Default will be disposed in the above for loop
4764+
topMaxShell = null;
47354765
}
47364766

47374767

sources/net.sf.j2s.java.org.eclipse.swt/src/org/eclipse/swt/widgets/MaximizedTitle.java

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,6 @@ void hide() {
118118
if (smStyle.display == "block") {
119119
smStyle.display = "none";
120120
}
121-
122121
}
123122

124123
public void handleApproaching() {
@@ -143,31 +142,17 @@ public void handleLeaving() {
143142
}
144143

145144
public boolean isApproaching(long now, int x, int y, boolean ctrlKey) {
146-
mouseAlreadyMoved = true;
147145
return (y <= 8 && !ctrlKey) && isAround(now, x, y);
148146
}
149147

150148
public boolean isLeaving(long now, int x, int y, boolean ctrlKey) {
151-
mouseAlreadyMoved = true;
152-
if (now - lastUpdated <= Display.AUTO_HIDE_DELAY) return false;
153149
Shell topShell = Display.getTopMaximizedShell();
154150
if (topShell == null) return false;
155151
return !isAround(now, x, y) || ctrlKey
156152
|| y > 12 + ((topShell.titleBar != null) ?
157153
OS.getContainerHeight(topShell.titleBar) : 20);
158154
}
159155

160-
public boolean handleMouseMove(long now, int x, int y, boolean ctrlKey) {
161-
if (this.isApproaching(now, x, y, ctrlKey)) {
162-
this.handleApproaching();
163-
return true;
164-
} else if (this.isLeaving(now, x, y, ctrlKey)) {
165-
this.handleLeaving();
166-
return true;
167-
}
168-
return false;
169-
}
170-
171156
public void bringToTop(int index) {
172157

173158
}

sources/net.sf.j2s.java.org.eclipse.swt/src/org/eclipse/swt/widgets/NotificationCorner.java

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ public class NotificationCorner extends DesktopItem {
2121

2222
private boolean alreadyInitialized;
2323

24+
private int currentZIndex;
25+
2426
public NotificationCorner(Display display) {
2527
super();
2628
this.display = display;
@@ -129,7 +131,7 @@ public void run() {
129131
if (isAutoHide) {
130132
setMinimized(false);
131133
}
132-
int zIndex = Display.getNextZIndex(false);
134+
int zIndex = window.currentTopZIndex + 1;
133135
if (handle.style.zIndex != zIndex) {
134136
layerZIndex = handle.style.zIndex;
135137
bringToTop(zIndex);
@@ -203,7 +205,7 @@ public void handleApproaching() {
203205
if (handle == null) {
204206
return;
205207
}
206-
int zIndex = Display.getNextZIndex(false);
208+
int zIndex = window.currentTopZIndex + 1;
207209
if (handle.style.zIndex != zIndex) {
208210
layerZIndex = handle.style.zIndex;
209211
if (!isAutoHide) {
@@ -227,18 +229,15 @@ public void handleLeaving() {
227229
}
228230

229231
public boolean isApproaching(long now, int x, int y, boolean ctrlKey) {
230-
mouseAlreadyMoved = true;
231232
return !ctrlKey && isAround(x, y);
232233
}
233234

234235
public boolean isLeaving(long now, int x, int y, boolean ctrlKey) {
235-
mouseAlreadyMoved = true;
236-
if (now - lastUpdated <= Display.AUTO_HIDE_DELAY) return false;
237236
return !isAroundCorner(x, y);
238237
}
239238
boolean isAround(int x, int y) {
240239
int range = 32;
241-
if (x < range && y < range && x + y < range) {
240+
if (x + y < range) {
242241
return true;
243242
}
244243
return false;
@@ -252,24 +251,17 @@ boolean isAroundCorner(int x, int y) {
252251
if (tray != null) {
253252
range = getRange() + 16;
254253
}
255-
if (x < range && y < range && x + y < range) {
256-
return true;
257-
}
258-
return false;
259-
}
260-
261-
public boolean handleMouseMove(long now, int x, int y, boolean ctrlKey) {
262-
if (this.isApproaching(now, x, y, ctrlKey)) {
263-
this.handleApproaching();
264-
return true;
265-
} else if (this.isLeaving(now, x, y, ctrlKey)) {
266-
this.handleLeaving();
254+
if (x + y < range) {
267255
return true;
268256
}
269257
return false;
270258
}
271259

272260
void setZIndex(int zIdx) {
261+
if (currentZIndex == zIdx) {
262+
return;
263+
}
264+
currentZIndex = zIdx;
273265
if (zIdx == -1 && !OS.isIE)
274266
/**
275267
* @j2sNative
@@ -348,13 +340,14 @@ int getRange() {
348340
}
349341

350342
public void bringToTop(int zIdx) {
351-
Tray tray = Display.getTray();
343+
//Tray tray = Display.getTray();
352344
if (tray == null) {
353345
return;
354346
}
355347
int zIndex = -1;
356348
if (zIdx == -1) {
357-
zIndex = Display.getNextZIndex(true);
349+
window.currentTopZIndex++;
350+
zIndex = window.currentTopZIndex;
358351
if (Display.getTopMaximizedShell() == null) {
359352
layerZIndex = zIndex;
360353
}

0 commit comments

Comments
 (0)