Skip to content

Commit

Permalink
Add caps lock indicator
Browse files Browse the repository at this point in the history
  • Loading branch information
RetGal committed Jan 5, 2025
1 parent 945705f commit a1b8575
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public boolean selfTest(String publicIp) {
}
Log.info("Port " + configuration.getPort() + " is reachable from the outside");
} catch (IOException e) {
Log.error("Port " + configuration.getPort() + " is not reachable from the outside");
Log.warn("Port " + configuration.getPort() + " is not reachable from the outside");
return false;
}
}
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/mpo/dayon/assisted/gui/Assisted.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.util.concurrent.ExecutionException;
import java.util.concurrent.atomic.AtomicBoolean;

import static java.awt.event.KeyEvent.VK_CAPS_LOCK;
import static java.lang.String.format;
import static java.lang.String.valueOf;
import static mpo.dayon.common.babylon.Babylon.translate;
Expand Down Expand Up @@ -332,6 +333,25 @@ private static NetworkAssistedEngineConfiguration extractConfiguration(String co
return null;
}

private void capsOff() {
if (Toolkit.getDefaultToolkit().getLockingKeyState(VK_CAPS_LOCK)) {
Log.info("Caps Lock is on, turning it off");
try {
Toolkit.getDefaultToolkit().setLockingKeyState(VK_CAPS_LOCK, false);
} catch (UnsupportedOperationException e) {
final Robot robot;
try {
robot = new Robot();
} catch (AWTException ex) {
throw new IllegalStateException("Could not initialize the AWT robot!", ex);
}
robot.keyPress(VK_CAPS_LOCK);
robot.delay(10);
robot.keyRelease(VK_CAPS_LOCK);
}
}
}

@Override
public void lostOwnership(Clipboard clipboard, Transferable transferable) {
Log.debug("Lost clipboard ownership");
Expand Down Expand Up @@ -422,6 +442,7 @@ private class MyNetworkAssistedEngineListener implements NetworkAssistedEngineLi

@Override
public void onConnecting(String serverName, int serverPort) {
capsOff();
frame.onConnecting(serverName, serverPort);
}

Expand Down
14 changes: 13 additions & 1 deletion src/main/java/mpo/dayon/common/gui/common/BaseFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import mpo.dayon.common.version.Version;

import static java.awt.GridBagConstraints.HORIZONTAL;
import static java.awt.event.KeyEvent.VK_CAPS_LOCK;
import static java.lang.String.format;
import static mpo.dayon.common.babylon.Babylon.translate;
import static mpo.dayon.common.configuration.Configuration.DEFAULT_TOKEN_SERVER_URL;
Expand Down Expand Up @@ -138,7 +139,11 @@ protected void setupStatusBar(StatusBar statusBar) {
add(statusBar, BorderLayout.SOUTH);
this.statusBar = statusBar;
updateInputLocale();
new Timer(5000, e -> updateInputLocale()).start();
updateCapsLockState();
new Timer(3000, e -> {
updateInputLocale();
updateCapsLockState();
}).start();
}

private void updateInputLocale() {
Expand All @@ -148,6 +153,13 @@ private void updateInputLocale() {
}
}

private void updateCapsLockState() {
boolean currentCapsLockState = Toolkit.getDefaultToolkit().getLockingKeyState(VK_CAPS_LOCK);
if (currentCapsLockState != statusBar.isCapsLockOn()) {
statusBar.setCapsLockIndicator(currentCapsLockState);
}
}

protected static JButton createButton(Action action) {
return createButton(action, true);
}
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/mpo/dayon/common/gui/statusbar/StatusBar.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class StatusBar extends JPanel {
private final JLabel message = new JLabel();
private final JLabel sessionDuration = new JLabel("00:00:00");
private final JLabel keyboardLayout = new JLabel();
private final JLabel capsLockIndicator = new JLabel();

public StatusBar() {
setLayout(new BoxLayout(this, LINE_AXIS));
Expand All @@ -27,6 +28,7 @@ public StatusBar() {
add(Box.createHorizontalGlue());
addSeparator();
addKeyboardLayout();
addCapsLockIndicator();
}

public void clearMessage() {
Expand Down Expand Up @@ -54,6 +56,19 @@ private void addKeyboardLayout() {
add(keyboardLayout);
}

private void addCapsLockIndicator() {
add(capsLockIndicator);
}

public boolean isCapsLockOn() {
return !capsLockIndicator.getText().isBlank();
}

public void setCapsLockIndicator(boolean isCapsLockOn) {
capsLockIndicator.setText(isCapsLockOn ? " ⛰ " : "");
capsLockIndicator.setToolTipText("Caps Lock");
}

public <T> void addCounter(Counter<T> counter, int width) {
JLabel label = createLabel(counter.getUid(), width);
label.setToolTipText(counter.getShortDescription());
Expand Down

0 comments on commit a1b8575

Please sign in to comment.