Skip to content

Focus first empty field in credentials dialog#43

Merged
grzegorz-gutowski merged 2 commits into
OpenVPN:mainfrom
sobitoks:focus-first-empty-credential-field
Jul 23, 2026
Merged

Focus first empty field in credentials dialog#43
grzegorz-gutowski merged 2 commits into
OpenVPN:mainfrom
sobitoks:focus-first-empty-credential-field

Conversation

@sobitoks

Copy link
Copy Markdown
Contributor

When some credentials are already stored, put the keyboard focus on the first empty field so that e.g. a saved username and password let the user type the OTP code straight away without clicking. A field counts as empty both when it was never stored (None) and when it was stored blank ('').

When some credentials are already stored, put the keyboard focus on the
first empty field so that e.g. a saved username and password let the user
type the OTP code straight away without clicking. A field counts as empty
both when it was never stored (None) and when it was stored blank ('').

@grzegorz-gutowski grzegorz-gutowski left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! This is a very nice idea and a clean solution!

if user_input.value is not None:
entry.set_text(user_input.value)
default_store = True
if not entry.get_text() and focus_entry is None:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this would read better:

if user_input.value is not None:
    ...
elif focus_entry is None:
    focus_entry = entry

@sobitoks sobitoks Jul 22, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your feedback! I tried the elif but it actually breaks one case, so I ended up not using it. Here is why.

The tricky case is a field that was saved empty. It does not come back as None, it comes back as an empty string ''.
With the elif that field goes into the first branch, because '' is not None evaluates to True, so the elif never runs for it and the focus skips right over it. That was the problem when only the username or only the password was saved.

There are two ways to make the elif work. One is to change that first condition from is not None to a plain truthiness check like if user_input.value:, so the empty string also falls through into the elif. The catch is that same first line also sets default_store, so changing its condition changes that behaviour too, and there are no tests around it.

So to avoid touching that existing line, I went with a separate if instead of an elif. A separate if always runs no matter what the first one did, so it still catches the empty field, and the is not None line stays exactly as it was.

If you would rather keep the proposed elif I am happy to do that, I would just switch the first condition to the truthiness check and accept that small change to default_store. Either way the test covers the four cases (nothing saved, only user, only password, both saved) and they all pass.

Happy to go with whatever you think fits best.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see the point. My gut tells me: we shouldn't be storing empty values. And if an empty value comes back from the safe storage it should be treated as if there was nothing there.
If you don't mind, as we are already here, I'd like to change the logic to the one suggested by you:

if user_input.value:
    entry.set_text(user_input.value)
    default_store = True
elif focus_entry is None:
    focus_entry = entry

@sobitoks sobitoks Jul 23, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, pushed the elif version. And you are right that empty values shouldn't really be
stored in the first place, this at least makes sure an empty one is treated as empty.

Reads cleaner than two separate ifs, and `if user_input.value:` treats
both None and an empty string '' as empty, so a field stored blank still
counts as empty when picking which one to focus.
@grzegorz-gutowski
grzegorz-gutowski merged commit cce59df into OpenVPN:main Jul 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants