Skip to content

Commit

Permalink
Select current username/password values on tab (#103)
Browse files Browse the repository at this point in the history
Improves UX for password managers. See #97.
  • Loading branch information
iaik-jheher authored Nov 17, 2023
1 parent ee22130 commit 2a0c8c9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ public MobileBKUEnterNumberComposite(Composite parent, int style, State state) {
this.txt_number = new Text(containerComposite, SWT.SINGLE | SWT.NATIVE | SWT.BORDER);
SWTUtils.anchor(txt_number).bottom(50, -10).left(50, 10).right(100, -20);
this.txt_number.setEditable(true);
SWTUtils.addFocusGainedListener(txt_number, () -> { txt_number.selectAll(); });

this.lbl_number = new Label(containerComposite, SWT.NATIVE);
this.lbl_number.setAlignment(SWT.RIGHT);
Expand All @@ -186,6 +187,7 @@ public MobileBKUEnterNumberComposite(Composite parent, int style, State state) {
this.txt_password = new Text(containerComposite, SWT.SINGLE | SWT.PASSWORD | SWT.BORDER | SWT.NATIVE);
SWTUtils.anchor(txt_password).top(50, 10).left(50, 10).right(100, -20);
this.txt_password.setEditable(true);
SWTUtils.addFocusGainedListener(txt_password, () -> { txt_password.selectAll(); });

this.lbl_password = new Label(containerComposite, SWT.NATIVE);
SWTUtils.anchor(lbl_password).top(50, 10).right(50, -10);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.FocusAdapter;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.FocusListener;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
Expand Down Expand Up @@ -200,7 +203,7 @@ public static void addMouseDownListener(Object swtObj, Consumer<MouseEvent> call
} catch (NoSuchMethodException | IllegalAccessException e) {
log.error("Attempted to pass object of type {} to addMouseDownListener; object does not have an accessible addMouseListener method", swtObj.getClass().getSimpleName(), e);
} catch (InvocationTargetException e) {
log.error("Failed to add selection listeer on object of type {}", swtObj.getClass().getSimpleName(), e);
log.error("Failed to add mouse-down listener on object of type {}", swtObj.getClass().getSimpleName(), e);
}
}

Expand All @@ -210,6 +213,29 @@ public static void addMouseDownListener(Object swtObj, Consumer<MouseEvent> call
public static void addMouseDownListener(Object swtObj, Runnable callback) {
addMouseDownListener(swtObj, (e) -> { callback.run(); });
}

/**
* functional-interface wrapper around swtObj.addFocusListener
* @param swtObj SWT widget supporting addFocusListener
* @param callback focusGained method
*/
public static void addFocusGainedListener(Object swtObj, Consumer<FocusEvent> callback) {
try {
Method m = swtObj.getClass().getMethod("addFocusListener", FocusListener.class);
m.invoke(swtObj, new FocusAdapter() { @Override public void focusGained(FocusEvent e) { callback.accept(e); } });
} catch (NoSuchMethodException | IllegalAccessException e) {
log.error("Attempted to pass object of type {} to addFocusGainedListener; object does not have an accessible addFocusListener method", swtObj.getClass().getSimpleName(), e);
} catch (InvocationTargetException e) {
log.error("Failed to add focus gained listener on object of type {}", swtObj.getClass().getSimpleName(), e);
}
}

/**
* @see SWTUtils#addFocusGainedListener(Object, Consumer)
*/
public static void addFocusGainedListener(Object swtObj, Runnable callback) {
addFocusGainedListener(swtObj, (e) -> { callback.run(); });
}

public static void openURL(@Nullable URI uri) {
try {
Expand Down

0 comments on commit 2a0c8c9

Please sign in to comment.