#13 focusFollowsMouse
authorKevin Lamonte <kevin.lamonte@gmail.com>
Mon, 17 Jul 2017 19:34:14 +0000 (15:34 -0400)
committerKevin Lamonte <kevin.lamonte@gmail.com>
Mon, 17 Jul 2017 19:34:14 +0000 (15:34 -0400)
src/jexer/TApplication.java
src/jexer/demos/DesktopDemoApplication.java

index b80e7a30dd483bebfd6fc130da3b091810463b5e..c32614914334ee7002d429c50df35aa98a7b9033 100644 (file)
@@ -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;
     }
 
index 825c040e6edb076fe17fa1ad0b71dd6e8661e519..c546aacaadaa63e6b6447be3b0d0563305afadd0 100644 (file)
@@ -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);
+                }
+            }
+        );
 
     }