From: Kevin Lamonte Date: Mon, 17 Jul 2017 19:34:14 +0000 (-0400) Subject: #13 focusFollowsMouse X-Git-Url: https://git.nikiroo.be/?a=commitdiff_plain;h=72fca17b02059ad7131406f89890a3875d3ed88b;p=fanfix-jexer.git #13 focusFollowsMouse --- diff --git a/src/jexer/TApplication.java b/src/jexer/TApplication.java index b80e7a3..c326149 100644 --- a/src/jexer/TApplication.java +++ b/src/jexer/TApplication.java @@ -580,6 +580,32 @@ public class TApplication implements Runnable { return result; } + /** + * If true, focus follows mouse: windows automatically raised if the + * mouse passes over them. + */ + private boolean focusFollowsMouse = false; + + /** + * Get focusFollowsMouse flag. + * + * @return true if focus follows mouse: windows automatically raised if + * the mouse passes over them + */ + public boolean getFocusFollowsMouse() { + return focusFollowsMouse; + } + + /** + * Set focusFollowsMouse flag. + * + * @param focusFollowsMouse if true, focus follows mouse: windows + * automatically raised if the mouse passes over them + */ + public void setFocusFollowsMouse(final boolean focusFollowsMouse) { + this.focusFollowsMouse = focusFollowsMouse; + } + // ------------------------------------------------------------------------ // General behavior ------------------------------------------------------- // ------------------------------------------------------------------------ @@ -1962,55 +1988,64 @@ public class TApplication implements Runnable { return; } - // Only switch if there are multiple windows - if (windows.size() < 2) { + // If a menu is still active, don't switch windows + if (activeMenu != null) { return; } - // Switch on the upclick - if (mouse.getType() != TMouseEvent.Type.MOUSE_UP) { + // Only switch if there are multiple windows + if (windows.size() < 2) { return; } - synchronized (windows) { - Collections.sort(windows); - if (windows.get(0).isModal()) { - // Modal windows don't switch - return; - } + if (((focusFollowsMouse == true) + && (mouse.getType() == TMouseEvent.Type.MOUSE_MOTION)) + || (mouse.getType() == TMouseEvent.Type.MOUSE_UP) + ) { + synchronized (windows) { + Collections.sort(windows); + if (windows.get(0).isModal()) { + // Modal windows don't switch + return; + } - for (TWindow window: windows) { - assert (!window.isModal()); + for (TWindow window: windows) { + assert (!window.isModal()); - if (window.isHidden()) { - assert (!window.isActive()); - continue; - } + if (window.isHidden()) { + assert (!window.isActive()); + continue; + } - if (window.mouseWouldHit(mouse)) { - if (window == windows.get(0)) { - // Clicked on the same window, nothing to do - assert (window.isActive()); + if (window.mouseWouldHit(mouse)) { + if (window == windows.get(0)) { + // Clicked on the same window, nothing to do + assert (window.isActive()); + return; + } + + // We will be switching to another window + assert (windows.get(0).isActive()); + assert (windows.get(0) == activeWindow); + assert (!window.isActive()); + activeWindow.onUnfocus(); + activeWindow.setActive(false); + activeWindow.setZ(window.getZ()); + activeWindow = window; + window.setZ(0); + window.setActive(true); + window.onFocus(); return; } - - // We will be switching to another window - assert (windows.get(0).isActive()); - assert (windows.get(0) == activeWindow); - assert (!window.isActive()); - activeWindow.onUnfocus(); - activeWindow.setActive(false); - activeWindow.setZ(window.getZ()); - activeWindow = window; - window.setZ(0); - window.setActive(true); - window.onFocus(); - return; } } + + // Clicked on the background, nothing to do + return; } - // Clicked on the background, nothing to do + // Nothing to do: this isn't a mouse up, or focus isn't following + // mouse. return; } diff --git a/src/jexer/demos/DesktopDemoApplication.java b/src/jexer/demos/DesktopDemoApplication.java index 825c040..c546aac 100644 --- a/src/jexer/demos/DesktopDemoApplication.java +++ b/src/jexer/demos/DesktopDemoApplication.java @@ -172,6 +172,20 @@ public class DesktopDemoApplication extends TApplication { } ); + desktop.addButton("Enable focusFollowsMouse", 25, 18, + new TAction() { + public void DO() { + DesktopDemoApplication.this.setFocusFollowsMouse(true); + } + } + ); + desktop.addButton("Disable focusFollowsMouse", 25, 21, + new TAction() { + public void DO() { + DesktopDemoApplication.this.setFocusFollowsMouse(false); + } + } + ); }