Skip to content

Commit d1189bb

Browse files
author
zhourenjian
committed
Improving default mousemove performance in browser (Or may cause 100% CPU)
1 parent 74ad272 commit d1189bb

File tree

8 files changed

+160
-142
lines changed

8 files changed

+160
-142
lines changed

sources/net.sf.j2s.java.org.eclipse.swt/src/org/eclipse/swt/package.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -418,8 +418,7 @@ if (!isDebugging) {
418418

419419
var wPath = ClazzLoader.getClasspathFor ("org.eclipse.swt.widgets.*");
420420
ClazzLoader.jarClasspath (wPath + "Shell.z.js", [
421-
"$wt.widgets.DesktopListener",
422-
"$.DesktopItem",
421+
"$wt.widgets.DesktopItem",
423422
"$.TaskBar",
424423
"$.MaximizedTitle",
425424
"$.QuickLaunch",

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import org.eclipse.swt.internal.xhtml.Element;
77
import org.eclipse.swt.internal.xhtml.window;
88

9-
public abstract class DesktopItem implements DesktopListener {
9+
public abstract class DesktopItem {
1010

1111
Display display;
1212

@@ -57,6 +57,8 @@ public void run() {
5757

5858
public abstract void updateLayout();
5959

60+
public abstract void handleLeaving();
61+
6062
public abstract void releaseWidget();
6163

6264
}

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

Lines changed: 0 additions & 15 deletions
This file was deleted.

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

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -388,8 +388,6 @@ public class Display extends Device {
388388
QuickLaunch shortcutBar;
389389
NotificationCorner trayCorner;
390390

391-
DesktopItem[] desktopItems;
392-
393391
static String bodyHeight, bodyOverflow, htmlOverflow;
394392
static int bodyScrollTop, bodyScrollLeft;
395393
static int htmlScrollTop, htmlScrollLeft;
@@ -2482,11 +2480,10 @@ void initializeDekstop() {
24822480
topBar = disp.topBar;
24832481
shortcutBar = disp.shortcutBar;
24842482
trayCorner = disp.trayCorner;
2485-
desktopItems = disp.desktopItems;
24862483
return;
24872484
}
24882485
}
2489-
if (desktopItems != null) return;
2486+
if (taskBar != null) return;
24902487

24912488
taskBar = new TaskBar(this);
24922489
topBar = new MaximizedTitle(this);
@@ -2500,29 +2497,34 @@ void initializeDekstop() {
25002497
} else {
25012498
trayCorner = new NotificationCorner(this);
25022499
}
2503-
2504-
desktopItems = new DesktopItem[] {
2505-
taskBar,
2506-
topBar,
2507-
shortcutBar,
2508-
trayCorner
2509-
};
2510-
for (int i = 0; i < desktopItems.length; i++) {
2511-
desktopItems[i].initialize();
2512-
}
2500+
2501+
taskBar.initialize();
2502+
topBar.initialize();
2503+
shortcutBar.initialize();
2504+
trayCorner.initialize();
25132505

25142506
mouseMoveListener = new RunnableCompatibility(){
25152507

25162508
public void run() {
25172509
HTMLEvent e = (HTMLEvent) getEvent();
2518-
for (int i = 0; i < desktopItems.length; i++) {
2519-
DesktopListener item = (DesktopListener) desktopItems[i];
2520-
if (item.isApproaching(e)) {
2521-
item.handleApproaching();
2522-
} else if (item.isLeaving(e)) {
2523-
item.handleLeaving();
2524-
}
2510+
int height = OS.getFixedBodyClientHeight();
2511+
// Hacks: Return as quickly as possible to avoid CPU 100%
2512+
int x = e.clientX;
2513+
int y = e.clientY;
2514+
if (x > 220 && y > 32 && y < height - 90) {
2515+
return;
25252516
}
2517+
2518+
long now = new Date().getTime();
2519+
boolean ctrlKey = e.ctrlKey;
2520+
boolean handled = taskBar.handleMouseMove(now, x, y, ctrlKey);
2521+
//if (handled) return;
2522+
handled = shortcutBar.handleMouseMove(now, x, y, ctrlKey);
2523+
//if (handled) return;
2524+
handled = trayCorner.handleMouseMove(now, x, y, ctrlKey);
2525+
//if (handled) return;
2526+
handled = topBar.handleMouseMove(now, x, y, ctrlKey);
2527+
if (handled) return;
25262528
}
25272529

25282530
};
@@ -3538,17 +3540,15 @@ void releaseDesktop () {
35383540
}
35393541
}
35403542

3541-
for (int i = 0; i < desktopItems.length; i++) {
3542-
if (trayRefs > 1 && desktopItems[i] == trayCorner) {
3543-
continue;
3544-
}
3545-
desktopItems[i].releaseWidget();
3546-
}
3547-
desktopItems = null;
3548-
trayCorner = null;
3543+
3544+
taskBar.releaseWidget();
35493545
taskBar = null;
3550-
shortcutBar = null;
3546+
topBar.releaseWidget();
35513547
topBar = null;
3548+
shortcutBar.releaseWidget();
3549+
shortcutBar = null;
3550+
if (trayRefs <= 1) trayCorner.releaseWidget();
3551+
trayCorner = null;
35523552

35533553
/**
35543554
* @j2sNative
@@ -4655,11 +4655,11 @@ static Shell getTopMaximizedShell() {
46554655
return lastShell;
46564656
}
46574657
public void updateLayout() {
4658-
if (desktopItems != null) {
4659-
for (int i = 0; i < desktopItems.length; i++) {
4660-
DesktopItem item = desktopItems[i];
4661-
item.updateLayout();
4662-
}
4658+
if (taskBar != null) {
4659+
taskBar.updateLayout();
4660+
topBar.updateLayout();
4661+
shortcutBar.updateLayout();
4662+
trayCorner.updateLayout();
46634663
}
46644664
}
46654665

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

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,9 @@
1111

1212
package org.eclipse.swt.widgets;
1313

14-
import java.util.Date;
15-
1614
import org.eclipse.swt.internal.browser.OS;
1715
import org.eclipse.swt.internal.xhtml.CSSStyle;
1816
import org.eclipse.swt.internal.xhtml.Element;
19-
import org.eclipse.swt.internal.xhtml.HTMLEvent;
2017
import org.eclipse.swt.internal.xhtml.document;
2118

2219
/**
@@ -37,7 +34,7 @@ public MaximizedTitle(Display display) {
3734
}
3835

3936
public void updateLayout() {
40-
Shell lastShell = Display.getTopMaximizedShell ();
37+
Shell lastShell = Display.getTopMaximizedShell();
4138
if (lastShell == null || lastShell.titleBar == null) return;
4239
this.lastMaximizedShell = lastShell;
4340

@@ -47,12 +44,12 @@ public void updateLayout() {
4744
els[i] = lastShell.titleBar.childNodes[i];
4845
}
4946
for (int i = 0; i < els.length; i++) {
50-
lastShell.titleBar.removeChild (els[i]);
51-
this.topbarEl.appendChild (els[i]);
47+
lastShell.titleBar.removeChild(els[i]);
48+
this.topbarEl.appendChild(els[i]);
5249
}
5350

5451
// update topbar
55-
this.handle.style.left = Math.round ((document.body.clientWidth - 320) / 2) + "px";
52+
this.handle.style.left = Math.round((document.body.clientWidth - 320) / 2) + "px";
5653
this.topbarEl.style.width = "316px";
5754
if (OS.isIE) {
5855
this.topbarEl.style.left = "1px";
@@ -61,7 +58,7 @@ public void updateLayout() {
6158
}
6259
this.topbarEl.style.top = "1px";
6360
this.handle.ondblclick = lastShell.titleBar.ondblclick;
64-
lastShell.updateShellTitle (320 + 4);
61+
lastShell.updateShellTitle(320 + 4);
6562
}
6663
public void returnTopMaximized(Shell shell) {
6764
Shell lastShell = this.lastMaximizedShell;
@@ -74,7 +71,7 @@ public void returnTopMaximized(Shell shell) {
7471
els[i] = this.topbarEl.childNodes[i];
7572
}
7673
for (int i = 0; i < els.length; i++) {
77-
lastShell.titleBar.appendChild (els[i]);
74+
lastShell.titleBar.appendChild(els[i]);
7875
}
7976
if (shell != null) {
8077
this.handle.style.display = "none";
@@ -83,9 +80,9 @@ public void returnTopMaximized(Shell shell) {
8380
public void initialize() {
8481
if (this.handle != null) return;
8582

86-
Element tbc = document.createElement ("DIV");
83+
Element tbc = document.createElement("DIV");
8784
tbc.className = "shell-manager-topbar-container";
88-
document.body.appendChild (tbc);
85+
document.body.appendChild(tbc);
8986
tbc.style.display = "none";
9087
tbc.style.width = "320px";
9188
tbc.style.zIndex = "3456";
@@ -100,20 +97,19 @@ public void initialize() {
10097
Decorations.createShadowHandles(handle);
10198
}
10299

103-
Element tb = document.createElement ("DIV");
100+
Element tb = document.createElement("DIV");
104101
tb.className = "shell-title-bar shell-maximized";
105-
this.handle.appendChild (tb);
102+
this.handle.appendChild(tb);
106103
this.topbarEl = tb;
107104
}
108105

109-
boolean isAround(int x, int y) {
110-
long now = new Date().getTime();
106+
boolean isAround(long now, int x, int y) {
111107
if (now - this.lastUpdated < 1000) {
112108
return true;
113109
}
114110
int barWidth = 320;
115-
int height = document.body.clientWidth;
116-
int offset = Math.round ((height - barWidth) / 2);
111+
int width = document.body.clientWidth;
112+
int offset = Math.round((width - barWidth) / 2);
117113
int x1 = offset - 72;
118114
int x2 = offset + barWidth + 72;
119115
return (x >= x1 && x <= x2);
@@ -131,10 +127,10 @@ public void handleApproaching() {
131127
Element topbar = handle;
132128
if (topbar == null) return;
133129
if (topbar.style.display != "block") {
134-
Shell lastShell = Display.getTopMaximizedShell ();
130+
Shell lastShell = Display.getTopMaximizedShell();
135131
if (lastShell != null && lastShell.titleBar != null) {
136132
topbar.style.display = "block";
137-
updateLayout ();
133+
updateLayout();
138134
}
139135
}
140136
}
@@ -144,24 +140,36 @@ public void handleLeaving() {
144140
if (topbar == null) return;
145141
if (topbar.style.display != "none") {
146142
topbar.style.display = "none";
147-
returnTopMaximized (null);
143+
returnTopMaximized(null);
148144
}
149145
}
150146

151-
public boolean isApproaching(HTMLEvent e) {
147+
public boolean isApproaching(long now, int x, int y, boolean ctrlKey) {
152148
mouseAlreadyMoved = true;
153-
return (e.clientY <= 8 && !e.ctrlKey) && isAround (e.clientX, e.clientY);
149+
return (y <= 8 && !ctrlKey) && isAround(now, x, y);
154150
}
155151

156-
public boolean isLeaving(HTMLEvent e) {
152+
public boolean isLeaving(long now, int x, int y, boolean ctrlKey) {
157153
mouseAlreadyMoved = true;
158-
long now = new Date().getTime();
159154
if (now - lastUpdated <= Display.AUTO_HIDE_DELAY) return false;
160-
Shell topShell = Display.getTopMaximizedShell ();
155+
Shell topShell = Display.getTopMaximizedShell();
161156
if (topShell == null) return false;
162-
return !isAround (e.clientX, e.clientY) || e.ctrlKey || e.clientY > 12 + ((topShell.titleBar != null) ? OS.getContainerHeight (topShell.titleBar) : 20);
157+
return !isAround(now, x, y) || ctrlKey
158+
|| y > 12 + ((topShell.titleBar != null) ?
159+
OS.getContainerHeight(topShell.titleBar) : 20);
163160
}
164-
161+
162+
public boolean handleMouseMove(long now, int x, int y, boolean ctrlKey) {
163+
if (this.isApproaching(now, x, y, ctrlKey)) {
164+
this.handleApproaching();
165+
return true;
166+
} else if (this.isLeaving(now, x, y, ctrlKey)) {
167+
this.handleLeaving();
168+
return true;
169+
}
170+
return false;
171+
}
172+
165173
public void bringToTop(String index) {
166174
// TODO Auto-generated method stub
167175

0 commit comments

Comments
 (0)