forward/backward word, hide mouse when typing
[fanfix.git] / src / jexer / TEditorWindow.java
index d474db2fa3e623e155c90a93183ac898b0a4b530..85bb91a273169da8f4fdd0e4749c521c4f6b5968 100644 (file)
@@ -3,7 +3,7 @@
  *
  * The MIT License (MIT)
  *
- * Copyright (C) 2017 Kevin Lamonte
+ * Copyright (C) 2019 Kevin Lamonte
  *
  * Permission is hereby granted, free of charge, to any person obtaining a
  * copy of this software and associated documentation files (the "Software"),
@@ -59,6 +59,10 @@ public class TEditorWindow extends TScrollableWindow {
      */
     private static final ResourceBundle i18n = ResourceBundle.getBundle(TEditorWindow.class.getName());
 
+    // ------------------------------------------------------------------------
+    // Variables --------------------------------------------------------------
+    // ------------------------------------------------------------------------
+
     /**
      * Hang onto my TEditor so I can resize it with the window.
      */
@@ -70,28 +74,19 @@ public class TEditorWindow extends TScrollableWindow {
     private String filename = "";
 
     /**
-     * Setup other fields after the editor is created.
+     * If true, hide the mouse after typing a keystroke.
      */
-    private void setupAfterEditor() {
-        hScroller = new THScroller(this, 17, getHeight() - 2, getWidth() - 20);
-        vScroller = new TVScroller(this, getWidth() - 2, 0, getHeight() - 2);
-        setMinimumWindowWidth(25);
-        setMinimumWindowHeight(10);
-        setTopValue(1);
-        setBottomValue(editField.getMaximumRowNumber());
-        setLeftValue(1);
-        setRightValue(editField.getMaximumColumnNumber());
+    private boolean hideMouseWhenTyping = true;
 
-        statusBar = newStatusBar(i18n.getString("statusBar"));
-        statusBar.addShortcutKeypress(kbF1, cmHelp,
-            i18n.getString("statusBarHelp"));
-        statusBar.addShortcutKeypress(kbF2, cmSave,
-            i18n.getString("statusBarSave"));
-        statusBar.addShortcutKeypress(kbF3, cmOpen,
-            i18n.getString("statusBarOpen"));
-        statusBar.addShortcutKeypress(kbF10, cmMenu,
-            i18n.getString("statusBarMenu"));
-    }
+    /**
+     * If true, the mouse should not be displayed because a keystroke was
+     * typed.
+     */
+    private boolean typingHidMouse = false;
+
+    // ------------------------------------------------------------------------
+    // Constructors -----------------------------------------------------------
+    // ------------------------------------------------------------------------
 
     /**
      * Public constructor sets window title.
@@ -154,38 +149,9 @@ public class TEditorWindow extends TScrollableWindow {
         this(parent, i18n.getString("newTextDocument"));
     }
 
-    /**
-     * Read file data into a string.
-     *
-     * @param file the file to open
-     * @return the file contents
-     * @throws IOException if a java.io operation throws
-     */
-    private String readFileData(final File file) throws IOException {
-        StringBuilder fileContents = new StringBuilder();
-        Scanner scanner = new Scanner(file);
-        String EOL = System.getProperty("line.separator");
-
-        try {
-            while (scanner.hasNextLine()) {
-                fileContents.append(scanner.nextLine() + EOL);
-            }
-            return fileContents.toString();
-        } finally {
-            scanner.close();
-        }
-    }
-
-    /**
-     * Read file data into a string.
-     *
-     * @param filename the file to open
-     * @return the file contents
-     * @throws IOException if a java.io operation throws
-     */
-    private String readFileData(final String filename) throws IOException {
-        return readFileData(new File(filename));
-    }
+    // ------------------------------------------------------------------------
+    // TWindow ----------------------------------------------------------------
+    // ------------------------------------------------------------------------
 
     /**
      * Draw the window.
@@ -208,24 +174,6 @@ public class TEditorWindow extends TScrollableWindow {
         }
     }
 
-    /**
-     * Check if a mouse press/release/motion event coordinate is over the
-     * editor.
-     *
-     * @param mouse a mouse-based event
-     * @return whether or not the mouse is on the editor
-     */
-    private final boolean mouseOnEditor(final TMouseEvent mouse) {
-        if ((mouse.getAbsoluteX() >= getAbsoluteX() + 1)
-            && (mouse.getAbsoluteX() <  getAbsoluteX() + getWidth() - 1)
-            && (mouse.getAbsoluteY() >= getAbsoluteY() + 1)
-            && (mouse.getAbsoluteY() <  getAbsoluteY() + getHeight() - 1)
-        ) {
-            return true;
-        }
-        return false;
-    }
-
     /**
      * Handle mouse press events.
      *
@@ -236,16 +184,20 @@ public class TEditorWindow extends TScrollableWindow {
         // Use TWidget's code to pass the event to the children.
         super.onMouseDown(mouse);
 
+        if (hideMouseWhenTyping) {
+            typingHidMouse = false;
+        }
+
         if (mouseOnEditor(mouse)) {
             // The editor might have changed, update the scollbars.
             setBottomValue(editField.getMaximumRowNumber());
-            setVerticalValue(editField.getEditingRowNumber());
+            setVerticalValue(editField.getVisibleRowNumber());
             setRightValue(editField.getMaximumColumnNumber());
             setHorizontalValue(editField.getEditingColumnNumber());
         } else {
             if (mouse.isMouseWheelUp() || mouse.isMouseWheelDown()) {
                 // Vertical scrollbar actions
-                editField.setEditingRowNumber(getVerticalValue());
+                editField.setVisibleRowNumber(getVerticalValue());
             }
         }
     }
@@ -260,9 +212,13 @@ public class TEditorWindow extends TScrollableWindow {
         // Use TWidget's code to pass the event to the children.
         super.onMouseUp(mouse);
 
+        if (hideMouseWhenTyping) {
+            typingHidMouse = false;
+        }
+
         if (mouse.isMouse1() && mouseOnVerticalScroller(mouse)) {
             // Clicked on vertical scrollbar
-            editField.setEditingRowNumber(getVerticalValue());
+            editField.setVisibleRowNumber(getVerticalValue());
         }
 
         // TODO: horizontal scrolling
@@ -278,16 +234,20 @@ public class TEditorWindow extends TScrollableWindow {
         // Use TWidget's code to pass the event to the children.
         super.onMouseMotion(mouse);
 
+        if (hideMouseWhenTyping) {
+            typingHidMouse = false;
+        }
+
         if (mouseOnEditor(mouse) && mouse.isMouse1()) {
             // The editor might have changed, update the scollbars.
             setBottomValue(editField.getMaximumRowNumber());
-            setVerticalValue(editField.getEditingRowNumber());
+            setVerticalValue(editField.getVisibleRowNumber());
             setRightValue(editField.getMaximumColumnNumber());
             setHorizontalValue(editField.getEditingColumnNumber());
         } else {
             if (mouse.isMouse1() && mouseOnVerticalScroller(mouse)) {
                 // Clicked/dragged on vertical scrollbar
-                editField.setEditingRowNumber(getVerticalValue());
+                editField.setVisibleRowNumber(getVerticalValue());
             }
 
             // TODO: horizontal scrolling
@@ -302,12 +262,16 @@ public class TEditorWindow extends TScrollableWindow {
      */
     @Override
     public void onKeypress(final TKeypressEvent keypress) {
+        if (hideMouseWhenTyping) {
+            typingHidMouse = true;
+        }
+
         // Use TWidget's code to pass the event to the children.
         super.onKeypress(keypress);
 
         // The editor might have changed, update the scollbars.
         setBottomValue(editField.getMaximumRowNumber());
-        setVerticalValue(editField.getEditingRowNumber());
+        setVerticalValue(editField.getVisibleRowNumber());
         setRightValue(editField.getMaximumColumnNumber());
         setHorizontalValue(editField.getEditingColumnNumber());
     }
@@ -381,4 +345,102 @@ public class TEditorWindow extends TScrollableWindow {
         super.onCommand(command);
     }
 
+    /**
+     * Returns true if this window does not want the application-wide mouse
+     * cursor drawn over it.
+     *
+     * @return true if this window does not want the application-wide mouse
+     * cursor drawn over it
+     */
+    @Override
+    public boolean hasHiddenMouse() {
+        return (super.hasHiddenMouse() || typingHidMouse);
+    }
+
+    // ------------------------------------------------------------------------
+    // TEditorWindow ----------------------------------------------------------
+    // ------------------------------------------------------------------------
+
+    /**
+     * Setup other fields after the editor is created.
+     */
+    private void setupAfterEditor() {
+        hScroller = new THScroller(this, 17, getHeight() - 2, getWidth() - 20);
+        vScroller = new TVScroller(this, getWidth() - 2, 0, getHeight() - 2);
+        setMinimumWindowWidth(25);
+        setMinimumWindowHeight(10);
+        setTopValue(1);
+        setBottomValue(editField.getMaximumRowNumber());
+        setLeftValue(1);
+        setRightValue(editField.getMaximumColumnNumber());
+
+        statusBar = newStatusBar(i18n.getString("statusBar"));
+        statusBar.addShortcutKeypress(kbF1, cmHelp,
+            i18n.getString("statusBarHelp"));
+        statusBar.addShortcutKeypress(kbF2, cmSave,
+            i18n.getString("statusBarSave"));
+        statusBar.addShortcutKeypress(kbF3, cmOpen,
+            i18n.getString("statusBarOpen"));
+        statusBar.addShortcutKeypress(kbF10, cmMenu,
+            i18n.getString("statusBarMenu"));
+
+        // Hide mouse when typing option
+        if (System.getProperty("jexer.TEditor.hideMouseWhenTyping",
+                "true").equals("false")) {
+
+            hideMouseWhenTyping = false;
+        }
+    }
+
+    /**
+     * Read file data into a string.
+     *
+     * @param file the file to open
+     * @return the file contents
+     * @throws IOException if a java.io operation throws
+     */
+    private String readFileData(final File file) throws IOException {
+        StringBuilder fileContents = new StringBuilder();
+        Scanner scanner = new Scanner(file);
+        String EOL = System.getProperty("line.separator");
+
+        try {
+            while (scanner.hasNextLine()) {
+                fileContents.append(scanner.nextLine() + EOL);
+            }
+            return fileContents.toString();
+        } finally {
+            scanner.close();
+        }
+    }
+
+    /**
+     * Read file data into a string.
+     *
+     * @param filename the file to open
+     * @return the file contents
+     * @throws IOException if a java.io operation throws
+     */
+    private String readFileData(final String filename) throws IOException {
+        return readFileData(new File(filename));
+    }
+
+    /**
+     * Check if a mouse press/release/motion event coordinate is over the
+     * editor.
+     *
+     * @param mouse a mouse-based event
+     * @return whether or not the mouse is on the editor
+     */
+    private boolean mouseOnEditor(final TMouseEvent mouse) {
+        if ((mouse.getAbsoluteX() >= getAbsoluteX() + 1)
+            && (mouse.getAbsoluteX() <  getAbsoluteX() + getWidth() - 1)
+            && (mouse.getAbsoluteY() >= getAbsoluteY() + 1)
+            && (mouse.getAbsoluteY() <  getAbsoluteY() + getHeight() - 1)
+        ) {
+            return true;
+        }
+        return false;
+    }
+
 }