Skip to content

Commit e521590

Browse files
committed
enh: UiSettings.mnemonic_hidden
1 parent 2688f35 commit e521590

File tree

5 files changed

+31
-4
lines changed

5 files changed

+31
-4
lines changed

mininterface/_lib/showcase.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
from tyro.conf import Positional
77

8+
from ..tag.select_tag import SelectTag
9+
810
from ..exceptions import ValidationFail
911
from ..cli import Command, SubcommandPlaceholder
1012
from ..tag.secret_tag import SecretTag
@@ -85,6 +87,9 @@ class Env:
8587
my_choice: Annotated[str, Options("one", "two", "three")] = "two"
8688
""" Choose between values """
8789

90+
my_multiple: Annotated[str, SelectTag(options=("one", "two", "three"), multiple=True)] = "two"
91+
""" Choose values """
92+
8893

8994
def showcase(case: int):
9095
kw = {"args": []}

mininterface/_mininterface/adaptor.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,10 @@ def _determine_mnemonic(self, form: TagDict, also_nones=False):
4848
# Determine mnemonic
4949
used_mnemonic = set()
5050
to_be_determined: list[Tag] = []
51-
for tag in flatten(form):
51+
tags = list(flatten(form))
52+
if len(tags) <= 1: # do not use mnemonic for single field which is focused by default
53+
return
54+
for tag in tags:
5255
if tag.mnemonic is False:
5356
continue
5457
if isinstance(tag.mnemonic, str):

mininterface/_tk_interface/select_input.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ def __init__(self, master, tag: SelectTag, grid_info, widget: Widget, adaptor: "
5959
self.options: OptionsReturnType = tag._get_options()
6060
self.variable = Variable()
6161
self.widget = widget
62+
self.taking_focus = widget
6263

6364
self.frame = nested_frame = Frame(master)
6465
nested_frame.grid(row=grid_info['row'], column=grid_info['column'], sticky='w')
@@ -92,6 +93,8 @@ def checkboxes(self, bg):
9293

9394
vw = self.variable_wrapper
9495

96+
taken = False
97+
9598
for i, (choice_label, choice_val, tip, tupled_key) in enumerate(options):
9699
var = BooleanVar(value=choice_val in tag.val)
97100

@@ -113,6 +116,10 @@ def on_toggle(val=choice_val, var=var):
113116

114117
button.pack(anchor="w")
115118

119+
if not taken:
120+
taken = True
121+
self.taking_focus = button # NOTE should be better, to the first one
122+
116123
def radio(self, bg):
117124
options = self.options
118125
tag = self.tag
@@ -128,6 +135,8 @@ def radio(self, bg):
128135
value=choice_label,
129136
style="Highlight.TRadiobutton" if tip else "",
130137
takefocus=is_selected)
138+
if is_selected:
139+
self.taking_focus = rb
131140
if adaptor.settings.radio_select_on_focus:
132141
rb.bind("<FocusIn>",
133142
lambda _, var=self.variable, val=choice_label: self.select_on_focus(var, val),

mininterface/_tk_interface/utils.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ def prevent_submit(event):
169169
variable = field_form.variable
170170
master = widget.master
171171
widget.pack_forget()
172+
taking_focus = widget
172173
process_change_handler = True
173174
""" If False, you process _last_ui_val and launch _on_change_trigger. """
174175
select_tag = False
@@ -193,6 +194,7 @@ def prevent_submit(event):
193194
replace_variable(variable)
194195
widget.grid_forget()
195196
widget = wrapper.widget
197+
taking_focus = wrapper.taking_focus
196198
if tag.multiple:
197199
process_change_handler = False
198200

@@ -270,11 +272,16 @@ def inner(tag: Tag):
270272
underline = -1
271273
if char:
272274
try:
273-
underline = label.index(char)
275+
underline = label.lower().index(char)
274276
except ValueError:
275277
label += f" ({char})"
276-
underline = label.index(char)
277-
adaptor.bind_shortcut(f'<Alt-{char}>', widget)
278+
underline = label.lower().index(char)
279+
# NOTE in case of a radio, put here
280+
adaptor.bind_shortcut(f'<Alt-{char}>', taking_focus)
281+
282+
if adaptor.settings.mnemonic_hidden:
283+
label = tag.label
284+
underline = -1
278285

279286
# Change label name as the field name might have changed (ex. highlighted by an asterisk)
280287
# But we cannot change the dict key itself

mininterface/settings.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ class UiSettings:
3434
* `None`: All Tags with `Tag(mnemonic=char|True)` will have a mnemonic enabled.
3535
"""
3636

37+
mnemonic_hidden: bool = False
38+
""" If True, the field label is not underlined to mark the mnemonic. """
39+
3740

3841
@dataclass
3942
class GuiSettings(UiSettings):

0 commit comments

Comments
 (0)