Skip to content

Commit

Permalink
add Escape key binding to qr import
Browse files Browse the repository at this point in the history
  • Loading branch information
kryptonbutterfly committed Jan 24, 2024
1 parent 06a0426 commit 12e3271
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
9 changes: 9 additions & 0 deletions src/kryptonbutterfly/totp/ui/qrimport/BL.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Map;
Expand All @@ -25,6 +27,7 @@
import kryptonbutterfly.totp.TinyTotp;
import kryptonbutterfly.totp.misc.ImageLuminanceSource;
import kryptonbutterfly.totp.misc.Utils;
import kryptonbutterfly.totp.ui.misc.KeyTypedAdapter;
import kryptonbutterfly.util.swing.Logic;
import kryptonbutterfly.util.swing.events.GuiCloseEvent;
import kryptonbutterfly.util.swing.events.GuiCloseEvent.Result;
Expand All @@ -36,6 +39,9 @@ final class BL extends Logic<QrGui, Void>
private boolean keepScanning = true;
private final Webcam webcam = Webcam.getDefault();

final KeyListener escapeScanListener = new KeyTypedAdapter(c -> abortScan(null), KeyEvent.VK_ESCAPE);
final KeyListener escapeMenuListener = new KeyTypedAdapter(c -> abort(null), KeyEvent.VK_ESCAPE);

BL(QrGui gui)
{
super(gui);
Expand All @@ -51,13 +57,16 @@ void abortScan(ActionEvent ae)
gui.if_(gui -> {
gui.cardLayout.show(gui.cardPanel, QrGui.MENU_CARD);
keepScanning = false;
gui.btnScanCamera.requestFocus();
});
}

void scanCamera(ActionEvent ae)
{
gui.if_(gui -> {
keepScanning = true;
gui.cardLayout.show(gui.cardPanel, QrGui.SCAN_CARD);
gui.btnAbortScan.requestFocus();
EventQueue.invokeLater(() -> scan(gui));
});
}
Expand Down
13 changes: 10 additions & 3 deletions src/kryptonbutterfly/totp/ui/qrimport/QrGui.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ public final class QrGui extends ObservableDialog<BL, String, Void>
final JPanel cardPanel = new JPanel(cardLayout);

private final JButton btnAbort = new JButton("abort");
private final JButton btnAbortScan = new JButton("abort");
final JButton btnAbortScan = new JButton("abort");

private final JButton btnScanFile = new JButton("open File");
private final JButton btnScanCamera = new JButton("scan Camera");
final JButton btnScanCamera = new JButton("scan Camera");

final JLabel cameraDisplay = new JLabel();

Expand All @@ -38,7 +38,7 @@ public QrGui(Window owner, ModalityType modality, Consumer<GuiCloseEvent<String>
super(owner, modality, closeListener);
TinyTotp.windowStates.qrScan.setBounds(this);
setTitle(title);
setLayout(new BorderLayout());
getContentPane().setLayout(new BorderLayout());

getContentPane().add(cardPanel, BorderLayout.CENTER);

Expand Down Expand Up @@ -83,9 +83,16 @@ protected BL createBusinessLogic(Void args)

private void init(BL bl)
{
btnAbort.addKeyListener(bl.escapeMenuListener);
btnAbort.addActionListener(bl::abort);

btnAbortScan.addKeyListener(bl.escapeScanListener);
btnAbortScan.addActionListener(bl::abortScan);

btnScanCamera.addKeyListener(bl.escapeMenuListener);
btnScanCamera.addActionListener(bl::scanCamera);

btnScanFile.addKeyListener(bl.escapeMenuListener);
btnScanFile.addActionListener(bl::scanFile);
}
}

0 comments on commit 12e3271

Please sign in to comment.