Dave Jarvis' Repositories

git clone https://repo.autonoma.ca/repo/kmcaster.git

Extract user settings into own entity

Author DaveJarvis <email>
Date 2020-07-31 20:12:50 GMT-0700
Commit 919ce1193de211bb5efe993d0fb506c3bde3f4b3
Parent 1a0c880
Delta 160 lines added, 82 lines removed, 78-line increase
src/main/com/whitemagicsoftware/kmcaster/EventHandler.java
public EventHandler(
- final HardwareImages hardwareImages,
- final int regularDelay,
- final int modifierDelay ) {
+ final HardwareImages hardwareImages, final UserSettings userSettings ) {
mHardwareImages = hardwareImages;
);
}
+
+ final var modifierDelay = userSettings.getDelayKeyModifier();
+ final var regularDelay = userSettings.getDelayKeyRegular();
for( final var key : HardwareSwitch.keyboardSwitches() ) {
src/main/com/whitemagicsoftware/kmcaster/HardwareImages.java
> mSwitches = new HashMap<>();
- public HardwareImages( final Dimension appDimensions ) {
- mAppDimensions = appDimensions;
+ public HardwareImages( final UserSettings userSettings ) {
+ mAppDimensions = userSettings.createAppDimensions();
final var mouseStates = createHardwareComponent();
src/main/com/whitemagicsoftware/kmcaster/KmCaster.java
import org.jnativehook.NativeHookException;
import picocli.CommandLine;
-import picocli.CommandLine.Command;
import picocli.CommandLine.Help.Ansi.Style;
import javax.swing.*;
-import java.awt.*;
import java.beans.PropertyChangeListener;
import java.io.IOException;
import java.net.URISyntaxException;
-import java.util.concurrent.Callable;
import static com.whitemagicsoftware.kmcaster.ui.Constants.TRANSLUCENT;
import static com.whitemagicsoftware.kmcaster.ui.FontLoader.initFonts;
import static java.util.logging.Level.OFF;
import static java.util.logging.Logger.getLogger;
import static javax.swing.SwingUtilities.invokeLater;
import static org.jnativehook.GlobalScreen.*;
import static picocli.CommandLine.Help.ColorScheme;
-import static picocli.CommandLine.Option;
/**
* </ol>
*/
-@Command(
- name = "KmCaster",
- mixinStandardHelpOptions = true,
- description = "Displays key presses and mouse clicks on the screen."
-)
-@SuppressWarnings("FieldMayBeFinal")
-public final class KmCaster extends JFrame implements Callable<Integer> {
-
- /**
- * Application height in pixels. Images are scaled to this height, maintaining
- * aspect ratio. The height constrains the width, so as long as the width
- * is large enough, the application's window will adjust to fit.
- */
- @Option(
- names = {"-s", "--size"},
- description = "Application size (${DEFAULT-VALUE} pixels)",
- paramLabel = "height",
- defaultValue = "100"
- )
- private int mHeight = 100;
-
- /**
- * Milliseconds to wait before releasing (clearing) the regular key.
- */
- @Option(
- names = {"-a", "--delay-alphanum"},
- description = "Delay for releasing non-modifier keys (${DEFAULT-VALUE} " +
- "milliseconds)",
- paramLabel = "delay",
- defaultValue = "250"
- )
- private int mDelayKeyRegular = 250;
-
- /**
- * Milliseconds to wait before releasing (clearing) any modifier key.
- */
- @Option(
- names = {"-m", "--delay-modifier"},
- description = "Delay for releasing modifier keys (${DEFAULT-VALUE} " +
- "milliseconds)",
- paramLabel = "delay",
- defaultValue = "150"
- )
- private int mDelayKeyModifier = 150;
+public final class KmCaster extends JFrame {
- /**
- * Milliseconds to wait before releasing (clearing) any mouse button.
- */
- @Option(
- names = {"-c", "--delay-click"},
- description = "Delay for releasing mouse buttons (${DEFAULT-VALUE} " +
- "milliseconds)",
- paramLabel = "delay",
- defaultValue = "200"
- )
- private int mDelayMouseClick = 200;
+ private final UserSettings mUserSettings = new UserSettings( this );
/**
- * Empty constructor so that command line arguments may be parsed.
+ * Constructs a window with the class name for its frame title.
*/
public KmCaster() {
super( KmCaster.class.getSimpleName() );
}
- private void init() {
- final var appDimension = new Dimension( 1024 + mHeight, mHeight );
- final var hardwareImages = new HardwareImages( appDimension );
- final var eventHandler = new EventHandler(
- hardwareImages, mDelayKeyRegular, mDelayKeyModifier );
+ public void init() {
+ final var hardwareImages = new HardwareImages( mUserSettings );
+ final var eventHandler = new EventHandler( hardwareImages, mUserSettings );
initWindowFrame();
keyboardListener.addPropertyChangeListener( listener );
keyboardListener.initModifiers();
+ }
+
+ private UserSettings getUserSettings() {
+ return mUserSettings;
}
.stackTraces( Style.italic )
.build();
- }
-
- /**
- * Invoked after the command-line arguments are parsed.
- *
- * @return Exit level zero.
- */
- @Override
- public Integer call() {
- init();
- return 0;
}
final var kc = new KmCaster();
- final var parser = new CommandLine( kc );
+ final var parser = new CommandLine( kc.getUserSettings() );
parser.setColorScheme( createColourScheme() );
src/main/com/whitemagicsoftware/kmcaster/UserSettings.java
+/*
+ * Copyright 2020 White Magic Software, Ltd.
+ *
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * o Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * o Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+package com.whitemagicsoftware.kmcaster;
+
+import picocli.CommandLine;
+
+import java.awt.*;
+import java.util.concurrent.Callable;
+
+@CommandLine.Command(
+ name = "KmCaster",
+ mixinStandardHelpOptions = true,
+ description = "Displays key presses and mouse clicks on the screen."
+)
+@SuppressWarnings("FieldMayBeFinal")
+public final class UserSettings implements Callable<Integer> {
+ /**
+ * Minimum application height, in pixels.
+ */
+ private static final int MIN_HEIGHT_PX = 20;
+
+ /**
+ * Executable class.
+ */
+ private final KmCaster mKmCaster;
+
+ /**
+ * Application height in pixels. Images are scaled to this height, maintaining
+ * aspect ratio. The height constrains the width, so as long as the width
+ * is large enough, the application's window will adjust to fit.
+ */
+ @CommandLine.Option(
+ names = {"-s", "--size"},
+ description = "Application size (${DEFAULT-VALUE} pixels)",
+ paramLabel = "height",
+ defaultValue = "100"
+ )
+ private int mHeight = 100;
+
+ /**
+ * Milliseconds to wait before releasing (clearing) the regular key.
+ */
+ @CommandLine.Option(
+ names = {"-a", "--delay-alphanum"},
+ description = "Delay for releasing non-modifier keys (${DEFAULT-VALUE} " +
+ "milliseconds)",
+ paramLabel = "delay",
+ defaultValue = "250"
+ )
+ private int mDelayKeyRegular = 250;
+
+ /**
+ * Milliseconds to wait before releasing (clearing) any modifier key.
+ */
+ @CommandLine.Option(
+ names = {"-m", "--delay-modifier"},
+ description = "Delay for releasing modifier keys (${DEFAULT-VALUE} " +
+ "milliseconds)",
+ paramLabel = "delay",
+ defaultValue = "150"
+ )
+ private int mDelayKeyModifier = 150;
+
+ /**
+ * Milliseconds to wait before releasing (clearing) any mouse button.
+ */
+ @CommandLine.Option(
+ names = {"-b", "--delay-button"},
+ description = "Delay for releasing mouse buttons (${DEFAULT-VALUE} " +
+ "milliseconds)",
+ paramLabel = "delay",
+ defaultValue = "200"
+ )
+ private int mDelayMouseButton = 200;
+
+ public UserSettings( final KmCaster kmCaster ) {
+ assert kmCaster != null;
+
+ mKmCaster = kmCaster;
+ }
+
+ /**
+ * Invoked after the command-line arguments are parsed to launch the
+ * application.
+ *
+ * @return Exit level zero.
+ */
+ @Override
+ public Integer call() {
+ mKmCaster.init();
+ return 0;
+ }
+
+ /**
+ * This will return the user-specified height
+ *
+ * @return The application height, in pixels.
+ */
+ public int getHeight() {
+ return mHeight < MIN_HEIGHT_PX ? MIN_HEIGHT_PX : mHeight;
+ }
+
+ public int getDelayKeyRegular() {
+ return mDelayKeyRegular;
+ }
+
+ public int getDelayKeyModifier() {
+ return mDelayKeyModifier;
+ }
+
+ public int getDelayMouseButton() {
+ return mDelayMouseButton;
+ }
+
+ public Dimension createAppDimensions() {
+ return new Dimension( 1024 + getHeight(), getHeight() );
+ }
+}