From a83fea2bae838f4b9bbf59ce3832e0e67be41378 Mon Sep 17 00:00:00 2001 From: Kevin Lamonte Date: Thu, 19 Mar 2015 07:06:03 -0400 Subject: [PATCH] misc cleanup --- README.md | 5 +-- src/jexer/TApplication.java | 22 +++++++--- src/jexer/TCheckbox.java | 6 +-- src/jexer/TField.java | 8 +--- src/jexer/THScroller.java | 7 +--- src/jexer/TLabel.java | 6 +-- src/jexer/TProgressBar.java | 6 +-- src/jexer/TRadioButton.java | 6 +-- src/jexer/TRadioGroup.java | 6 +-- src/jexer/TText.java | 6 +-- src/jexer/TVScroller.java | 7 +--- src/jexer/TWidget.java | 42 ++++++++++++++++++- src/jexer/TWindow.java | 16 ++++++-- src/jexer/backend/ECMA48Backend.java | 15 ++----- src/jexer/io/ECMA48Terminal.java | 60 ++++++++++++++++++---------- 15 files changed, 124 insertions(+), 94 deletions(-) diff --git a/README.md b/README.md index a58e024c..c2946c69 100644 --- a/README.md +++ b/README.md @@ -82,9 +82,9 @@ Many tasks remain before calling this version 1.0: 0.0.2: +- Fix clipping errors - AWT: - Blinking cursor -- Clean up TWidget constuctors (everyone is doing setX() / setY() / set...) - ECMA48Backend running on socket - TTreeView - TDirectoryList @@ -99,10 +99,9 @@ Many tasks remain before calling this version 1.0: 0.0.4: - Bugs - - Bare ESC isn't being returned immediately - TTimer is jittery with I/O - TSubMenu keyboard mnemonic not working - - kbDel and use by TMenu (MID_CLEAR) + - Making TMenu keyboard accelerators active/inactive - TDirectoryList cannot be navigated only with keyboard - TTreeView cannot be navigated only with keyboard - RangeViolation after dragging scrollbar up/down diff --git a/src/jexer/TApplication.java b/src/jexer/TApplication.java index 4aaab1d0..9d845be1 100644 --- a/src/jexer/TApplication.java +++ b/src/jexer/TApplication.java @@ -69,6 +69,11 @@ public class TApplication { */ private static final boolean debugThreads = false; + /** + * If true, emit events being processed to System.err. + */ + private static final boolean debugEvents = false; + /** * WidgetEventHandler is the main event consumer loop. There are at most * two such threads in existence: the primary for normal case and a @@ -644,10 +649,10 @@ public class TApplication { */ private void metaHandleEvent(final TInputEvent event) { - /* - System.err.printf(String.format("metaHandleEvents event: %s\n", - event)); System.err.flush(); - */ + if (debugEvents) { + System.err.printf(String.format("metaHandleEvents event: %s\n", + event)); System.err.flush(); + } if (quit) { // Do no more processing if the application is already trying @@ -711,7 +716,9 @@ public class TApplication { */ private void primaryHandleEvent(final TInputEvent event) { - // System.err.printf("Handle event: %s\n", event); + if (debugEvents) { + System.err.printf("Handle event: %s\n", event); + } // Special application-wide events ----------------------------------- @@ -804,7 +811,10 @@ public class TApplication { mouse.setX(mouse.getX() - window.getX()); mouse.setY(mouse.getY() - window.getY()); } - // System.err("TApplication dispatch event: %s\n", event); + if (debugEvents) { + System.err.printf("TApplication dispatch event: %s\n", + event); + } window.handleEvent(event); break; } diff --git a/src/jexer/TCheckbox.java b/src/jexer/TCheckbox.java index 939f3fed..b33d1f59 100644 --- a/src/jexer/TCheckbox.java +++ b/src/jexer/TCheckbox.java @@ -64,13 +64,9 @@ public final class TCheckbox extends TWidget { final String label, final boolean checked) { // Set parent and window - super(parent); + super(parent, x, y, label.length() + 4, 1); - setX(x); - setY(y); - setHeight(1); this.label = label; - setWidth(label.length() + 4); this.checked = checked; setHasCursor(true); diff --git a/src/jexer/TField.java b/src/jexer/TField.java index 406c5756..083efdda 100644 --- a/src/jexer/TField.java +++ b/src/jexer/TField.java @@ -139,13 +139,9 @@ public class TField extends TWidget { final TAction enterAction, final TAction updateAction) { // Set parent and window - super(parent); - setX(x); - setY(y); - setHeight(1); - setWidth(width); - setHasCursor(true); + super(parent, x, y, width, 1); + setHasCursor(true); this.fixed = fixed; this.text = text; this.enterAction = enterAction; diff --git a/src/jexer/THScroller.java b/src/jexer/THScroller.java index a3599a9c..3539ff4e 100644 --- a/src/jexer/THScroller.java +++ b/src/jexer/THScroller.java @@ -158,12 +158,7 @@ public final class THScroller extends TWidget { final int width) { // Set parent and window - super(parent); - - setX(x); - setY(y); - setHeight(1); - setWidth(width); + super(parent, x, y, width, 1); } /** diff --git a/src/jexer/TLabel.java b/src/jexer/TLabel.java index d56be67f..e57ad9b1 100644 --- a/src/jexer/TLabel.java +++ b/src/jexer/TLabel.java @@ -92,13 +92,9 @@ public final class TLabel extends TWidget { final int y, final String colorKey) { // Set parent and window - super(parent, false); + super(parent, false, x, y, text.length(), 1); this.text = text; - setX(x); - setY(y); - setHeight(1); - setWidth(text.length()); this.colorKey = colorKey; } diff --git a/src/jexer/TProgressBar.java b/src/jexer/TProgressBar.java index 1cd950c9..51a88532 100644 --- a/src/jexer/TProgressBar.java +++ b/src/jexer/TProgressBar.java @@ -120,12 +120,8 @@ public final class TProgressBar extends TWidget { final int width, final int value) { // Set parent and window - super(parent, false); + super(parent, false, x, y, width, 1); - setX(x); - setY(y); - setHeight(1); - setWidth(width); this.value = value; } diff --git a/src/jexer/TRadioButton.java b/src/jexer/TRadioButton.java index 23663bb2..685f1f48 100644 --- a/src/jexer/TRadioButton.java +++ b/src/jexer/TRadioButton.java @@ -101,13 +101,9 @@ public final class TRadioButton extends TWidget { final String label, final int id) { // Set parent and window - super(parent); + super(parent, x, y, label.length() + 4, 1); - setX(x); - setY(y); - setHeight(1); this.label = label; - setWidth(label.length() + 4); this.id = id; setHasCursor(true); diff --git a/src/jexer/TRadioGroup.java b/src/jexer/TRadioGroup.java index c1b65235..e2e24a33 100644 --- a/src/jexer/TRadioGroup.java +++ b/src/jexer/TRadioGroup.java @@ -84,13 +84,9 @@ public final class TRadioGroup extends TWidget { final String label) { // Set parent and window - super(parent); + super(parent, x, y, label.length() + 4, 2); - setX(x); - setY(y); - setHeight(2); this.label = label; - setWidth(label.length() + 4); } /** diff --git a/src/jexer/TText.java b/src/jexer/TText.java index 1f1359fb..6d2fe27f 100644 --- a/src/jexer/TText.java +++ b/src/jexer/TText.java @@ -243,12 +243,8 @@ public final class TText extends TWidget { final int y, final int width, final int height, final String colorKey) { // Set parent and window - super(parent); + super(parent, x, y, width, height); - setX(x); - setY(y); - setWidth(width); - setHeight(height); this.text = text; this.colorKey = colorKey; diff --git a/src/jexer/TVScroller.java b/src/jexer/TVScroller.java index d9d5ec85..51fcd851 100644 --- a/src/jexer/TVScroller.java +++ b/src/jexer/TVScroller.java @@ -158,12 +158,7 @@ public final class TVScroller extends TWidget { final int height) { // Set parent and window - super(parent); - - setX(x); - setY(y); - setHeight(height); - setWidth(1); + super(parent, x, y, 1, height); } /** diff --git a/src/jexer/TWidget.java b/src/jexer/TWidget.java index 2a92d09d..d6864db7 100644 --- a/src/jexer/TWidget.java +++ b/src/jexer/TWidget.java @@ -547,6 +547,21 @@ public abstract class TWidget implements Comparable { this(parent, true); } + /** + * Protected constructor. + * + * @param parent parent widget + * @param x column relative to parent + * @param y row relative to parent + * @param width width of widget + * @param height height of widget + */ + protected TWidget(final TWidget parent, final int x, final int y, + final int width, final int height) { + + this(parent, true, x, y, width, height); + } + /** * Protected constructor used by subclasses that are disabled by default. * @@ -561,6 +576,31 @@ public abstract class TWidget implements Comparable { parent.addChild(this); } + /** + * Protected constructor used by subclasses that are disabled by default. + * + * @param parent parent widget + * @param enabled if true assume enabled + * @param x column relative to parent + * @param y row relative to parent + * @param width width of widget + * @param height height of widget + */ + protected TWidget(final TWidget parent, final boolean enabled, + final int x, final int y, final int width, final int height) { + + this.enabled = enabled; + this.parent = parent; + this.window = parent.window; + children = new LinkedList(); + parent.addChild(this); + + this.x = x; + this.y = y; + this.width = width; + this.height = height; + } + /** * Add a child widget to my list of children. We set its tabOrder to 0 * and increment the tabOrder of all other children. @@ -723,7 +763,7 @@ public abstract class TWidget implements Comparable { if ((children.size() == 0) // TODO // || (cast(TTreeView)this) - // || (cast(TText)this) + || (this instanceof TText) ) { // Defaults: diff --git a/src/jexer/TWindow.java b/src/jexer/TWindow.java index 2fc4aa5d..65eaa483 100644 --- a/src/jexer/TWindow.java +++ b/src/jexer/TWindow.java @@ -751,22 +751,30 @@ public class TWindow extends TWidget { } } if (keypress.equals(kbShiftLeft)) { - if (getWidth() > minimumWindowWidth) { + if ((getWidth() > minimumWindowWidth) + || (minimumWindowWidth <= 0) + ) { setWidth(getWidth() - 1); } } if (keypress.equals(kbShiftRight)) { - if (getWidth() < maximumWindowWidth) { + if ((getWidth() < maximumWindowWidth) + || (maximumWindowWidth <= 0) + ) { setWidth(getWidth() + 1); } } if (keypress.equals(kbShiftUp)) { - if (getHeight() > minimumWindowHeight) { + if ((getHeight() > minimumWindowHeight) + || (minimumWindowHeight <= 0) + ) { setHeight(getHeight() - 1); } } if (keypress.equals(kbShiftDown)) { - if (getHeight() < maximumWindowHeight) { + if ((getHeight() < maximumWindowHeight) + || (maximumWindowHeight <= 0) + ) { setHeight(getHeight() + 1); } } diff --git a/src/jexer/backend/ECMA48Backend.java b/src/jexer/backend/ECMA48Backend.java index 99c02003..da9c9c35 100644 --- a/src/jexer/backend/ECMA48Backend.java +++ b/src/jexer/backend/ECMA48Backend.java @@ -103,20 +103,13 @@ public final class ECMA48Backend extends Backend { synchronized (terminal) { try { terminal.wait(timeout); - if (terminal.hasEvents()) { - // System.err.println("getEvents()"); - terminal.getEvents(queue); - } else { - // If I got here, then I timed out. Call - // terminal.getIdleEvents() to pick up stragglers - // like bare resize. - // System.err.println("getIdleEvents()"); - terminal.getIdleEvents(queue); - } } catch (InterruptedException e) { // Spurious interrupt, pretend it was like a timeout. // System.err.println("[interrupt] getEvents()"); - terminal.getIdleEvents(queue); + } + if (terminal.hasEvents()) { + // System.err.println("getEvents()"); + terminal.getEvents(queue); } } } else { diff --git a/src/jexer/io/ECMA48Terminal.java b/src/jexer/io/ECMA48Terminal.java index f6bc0b3a..d466646d 100644 --- a/src/jexer/io/ECMA48Terminal.java +++ b/src/jexer/io/ECMA48Terminal.java @@ -127,6 +127,12 @@ public final class ECMA48Terminal implements Runnable { */ private long escapeTime; + /** + * The time we last checked the window size. We try not to spawn stty + * more than once per second. + */ + private long windowSizeTime; + /** * true if mouse1 was down. Used to report mouse1 on the release event. */ @@ -647,30 +653,34 @@ public final class ECMA48Terminal implements Runnable { * * @param queue list to append new events to */ - public void getIdleEvents(final List queue) { + private void getIdleEvents(final List queue) { + Date now = new Date(); // Check for new window size - sessionInfo.queryWindowSize(); - int newWidth = sessionInfo.getWindowWidth(); - int newHeight = sessionInfo.getWindowHeight(); - if ((newWidth != windowResize.getWidth()) - || (newHeight != windowResize.getHeight()) - ) { - TResizeEvent event = new TResizeEvent(TResizeEvent.Type.SCREEN, - newWidth, newHeight); - windowResize = new TResizeEvent(TResizeEvent.Type.SCREEN, - newWidth, newHeight); - synchronized (eventQueue) { - eventQueue.add(event); + long windowSizeDelay = now.getTime() - windowSizeTime; + if (windowSizeDelay > 1000) { + sessionInfo.queryWindowSize(); + int newWidth = sessionInfo.getWindowWidth(); + int newHeight = sessionInfo.getWindowHeight(); + if ((newWidth != windowResize.getWidth()) + || (newHeight != windowResize.getHeight()) + ) { + TResizeEvent event = new TResizeEvent(TResizeEvent.Type.SCREEN, + newWidth, newHeight); + windowResize = new TResizeEvent(TResizeEvent.Type.SCREEN, + newWidth, newHeight); + queue.add(event); } + windowSizeTime = now.getTime(); } - synchronized (eventQueue) { - if (eventQueue.size() > 0) { - synchronized (queue) { - queue.addAll(eventQueue); - } - eventQueue.clear(); + // ESCDELAY type timeout + if (state == ParseState.ESCAPE) { + long escDelay = now.getTime() - escapeTime; + if (escDelay > 100) { + // After 0.1 seconds, assume a true escape character + queue.add(controlChar((char)0x1B, false)); + reset(); } } } @@ -796,7 +806,7 @@ public final class ECMA48Terminal implements Runnable { // Parameter separator if (ch == ';') { paramI++; - params.set(paramI, ""); + params.add(""); return; } @@ -904,7 +914,7 @@ public final class ECMA48Terminal implements Runnable { // Parameter separator if (ch == ';') { paramI++; - params.set(paramI, ""); + params.add(paramI, ""); return; } @@ -1449,6 +1459,14 @@ public final class ECMA48Terminal implements Runnable { } } } else { + getIdleEvents(events); + if (events.size() > 0) { + synchronized (eventQueue) { + eventQueue.addAll(events); + } + events.clear(); + } + // Wait 10 millis for more data Thread.sleep(10); } -- 2.27.0