Skip to content

Commit

Permalink
Move keyboard layout indicator to status bar
Browse files Browse the repository at this point in the history
  • Loading branch information
RetGal committed Feb 14, 2024
1 parent 8263a4c commit 45cd4a7
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 14 deletions.
23 changes: 10 additions & 13 deletions src/main/java/mpo/dayon/common/gui/common/BaseFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Locale;

import javax.swing.*;

Expand Down Expand Up @@ -43,8 +42,6 @@ public abstract class BaseFrame extends JFrame {

private StatusBar statusBar;

private Locale currentLocale;

protected BaseFrame() {
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
setIconImage(ImageUtilities.getOrCreateIcon(ImageNames.APP).getImage());
Expand Down Expand Up @@ -74,16 +71,7 @@ protected void setFrameType(FrameType frameType) {
this.dimension = new Dimension(Math.max(configuration.getWidth(), frameType.getMinWidth()),
Math.max(configuration.getHeight(), frameType.getMinHeight()));
this.setSize(dimension.width, dimension.height);
setTitle();
new Timer(5000, e -> setTitle()).start();
}

private void setTitle() {
Locale newLocale = InputContext.getInstance().getLocale();
if (newLocale != currentLocale) {
currentLocale = newLocale;
setTitle(format("Dayon! (%s) %s %s", translate(frameType.getPrefix()), Version.get(), currentLocale != null ? currentLocale.toString() : ""));
}
setTitle(format("Dayon! (%s) %s", translate(frameType.getPrefix()), Version.get()));
}

protected void setupToolBar(ToolBar toolBar) {
Expand All @@ -107,6 +95,15 @@ protected void setupStatusBar(StatusBar statusBar) {
statusBar.add(Box.createHorizontalStrut(10));
add(statusBar, BorderLayout.SOUTH);
this.statusBar = statusBar;
updateInputLocale();
new Timer(5000, e -> updateInputLocale()).start();
}

private void updateInputLocale() {
String currentKeyboardLayout = InputContext.getInstance().getLocale().toString();
if (!currentKeyboardLayout.equals(statusBar.getKeyboardLayout())) {
statusBar.setKeyboardLayout(currentKeyboardLayout);
}
}

protected JButton createButton(Action action) {
Expand Down
21 changes: 21 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 @@ -9,6 +9,7 @@
import java.awt.*;
import java.util.TimerTask;

import static java.lang.String.format;
import static javax.swing.BoxLayout.LINE_AXIS;
import static javax.swing.SwingConstants.*;
import static mpo.dayon.common.babylon.Babylon.translate;
Expand All @@ -18,12 +19,15 @@ public class StatusBar extends JPanel {
private static final int HEIGHT = 5;
private final JLabel message = new JLabel();
private final JLabel sessionDuration = new JLabel("00:00:00");
private final JLabel keyboardLayout = new JLabel();

public StatusBar() {
setLayout(new BoxLayout(this, LINE_AXIS));
add(Box.createHorizontalStrut(10));
add(message);
add(Box.createHorizontalGlue());
addSeparator();
addKeyboardLayout();
}

public void clearMessage() {
Expand All @@ -38,6 +42,23 @@ public void setSessionDuration(String sessionDuration) {
this.sessionDuration.setText(sessionDuration);
}

public void setKeyboardLayout(String keyboardLayout) {
this.keyboardLayout.setText(keyboardLayout);
this.keyboardLayout.setToolTipText(format("\u2328 %s", keyboardLayout));
}

public String getKeyboardLayout() {
return keyboardLayout.getText();
}

private void addKeyboardLayout() {
final Dimension dimension = new Dimension(60, HEIGHT);
keyboardLayout.setHorizontalAlignment(CENTER);
keyboardLayout.setSize(dimension);
keyboardLayout.setPreferredSize(dimension);
add(keyboardLayout);
}

public void addCounter(Counter<?> counter, int width) {
final JLabel lbl = new JLabel(counter.getUid());
final Dimension dimension = new Dimension(width, HEIGHT);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/mpo/dayon/common/utils/UnitUtilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public static String toElapsedTime(long millis) {
return format("%dh%02dm%02ds", toHours(secs), toMinutes(secs) % 60, round(secs) % 60);
}
// noinspection NumericCastThatLosesPrecision
return format("%dd%02dh%02dm%02ds", floor(toHours(secs) / 24.0), toHours(secs) % 24,
return format("%dd%02dh%02dm%02ds", (int) floor(toHours(secs) / 24.0), toHours(secs) % 24,
toMinutes(secs) % 60, round(secs) % 60);
}

Expand Down

0 comments on commit 45cd4a7

Please sign in to comment.