Merge pull request #1367 head

This commit is contained in:
Valiantsin Dzerakh committed 2026-11-22 15:50:35 +03:00
commit f8eabdad46
2 files changed
+199 -17

No files matched your search

+109 -17
View File
@@ -114,6 +114,73 @@ def use_sun_valley(root, ui_font):
return True
class ScrollArea(ttk.Frame):
"""A screen's content scrolls while its action bar stays outside the viewport."""
def __init__(self, parent):
super().__init__(parent)
background = ttk.Style(parent).lookup('TFrame', 'background') or parent.winfo_toplevel().cget('background')
self.canvas = tk.Canvas(self, width=1, height=1, borderwidth=0, highlightthickness=0, background=background)
self.scrollbar = ttk.Scrollbar(self, orient='vertical', command=self.canvas.yview)
self.canvas.configure(yscrollcommand=self.scrollbar.set)
self.canvas.pack(side='left', fill='both', expand=True)
self.content = ttk.Frame(self.canvas)
self.window = self.canvas.create_window((0, 0), window=self.content, anchor='nw')
self.content.bind('<Configure>', lambda _e: self.canvas.configure(scrollregion=self.canvas.bbox('all')))
self.canvas.bind('<Configure>', lambda e: self.canvas.itemconfigure(self.window, width=e.width))
root = self.winfo_toplevel()
root.bind('<FocusIn>', self.reveal_focus, add='+')
for sequence in ('<MouseWheel>',) + (('<Button-4>', '<Button-5>') if sys.platform.startswith('linux') else ()):
root.bind(sequence, self.wheel, add='+')
def contains(self, widget):
return widget is self.canvas or str(widget).startswith(str(self.content) + '.') or widget is self.content
def natural(self):
width = self.content.winfo_reqwidth()
if int(self.canvas.cget('width')) == width:
return False
self.canvas.configure(width=width)
return True
def limit_height(self, height):
height = max(1, height)
scroll = height < self.content.winfo_reqheight()
changed = bool(self.scrollbar.winfo_manager()) != scroll
if changed:
if scroll:
self.scrollbar.pack(side='right', fill='y', before=self.canvas)
else:
self.scrollbar.pack_forget()
if int(self.canvas.cget('height')) != height:
self.canvas.configure(height=height)
changed = True
return changed
def reveal_focus(self, event):
if not self.contains(event.widget) or event.widget is self.canvas:
return
top = event.widget.winfo_rooty() - self.content.winfo_rooty()
bottom = top + event.widget.winfo_height()
visible = self.canvas.canvasy(0)
height = self.canvas.winfo_height()
if top < visible:
self.canvas.yview_moveto(top / max(1, self.content.winfo_height()))
elif bottom > visible + height:
self.canvas.yview_moveto((bottom - height) / max(1, self.content.winfo_height()))
def wheel(self, event):
if not self.contains(event.widget) or event.widget.winfo_class() in ('Text', 'Listbox', 'TCombobox'):
return
if self.canvas.yview() == (0.0, 1.0):
return
units = -1 if event.num == 4 else 1 if event.num == 5 else -int(event.delta / 120)
if not units and event.delta:
units = -1 if event.delta > 0 else 1
self.canvas.yview_scroll(units, 'units')
return 'break'
class App:
def __init__(self, root, ui_lang, layout, run_args=None, rebuild=None):
self.root, self.layout = root, layout
@@ -180,6 +247,7 @@ class App:
self.ui_box.pack(side='left')
self.body = ttk.Frame(root, padding=PAD)
self.body.pack(fill='both', expand=True)
self.scroll_areas = {}
self.frames = {'form': self.build_form(), 'run': self.build_run(), 'finish': self.build_finish()}
self.vars['lang'].set(self.lang_label(self.lang_code))
self.vars['dest'].trace_add('write', lambda *_: self.refresh_form())
@@ -267,13 +335,28 @@ class App:
for), so a screen or language switch does not move the buttons sideways, and the height of the shown screen,
so no screen carries the empty band of a taller one. Every text change that can add lines calls it again, or
pack takes the missing height from the button bars. Without `shrink` the window only grows and a size the user
dragged it to stays; a screen or language switch and closing the details or the log shrink it to fit."""
dragged it to stays; a screen or language switch and closing the details or the log shrink it to fit. A screen
taller than the display scrolls its content while its action bar remains outside the viewport."""
self.root.update_idletasks()
widths_changed = [area.natural() for area in self.scroll_areas.values()]
if any(widths_changed):
self.root.update_idletasks()
widths = [f.winfo_reqwidth() for f in self.frames.values()]
shown = self.frames[self.screen] if self.screen else max(self.frames.values(), key=lambda f: f.winfo_reqheight())
chrome = self.root.winfo_reqheight() - self.body.winfo_reqheight() # the title row above the body
w = max(self.root.winfo_reqwidth(), max(widths) + 2 * PAD)
h = chrome + shown.winfo_reqheight() + 2 * PAD
area = self.scroll_areas.get(self.screen)
if area:
h += area.content.winfo_reqheight() - area.canvas.winfo_reqheight()
# Leave room for window decorations and the desktop's panels. Content scrolls; the footer stays visible.
limit = max(1, self.root.winfo_screenheight() - dpi.px(80))
if area:
changed = area.limit_height(area.content.winfo_reqheight() - max(0, h - limit))
h = min(h, limit)
if changed:
self.root.update_idletasks()
w = max(w, shown.winfo_reqwidth() + 2 * PAD)
# refresh_run calls it on every poll: leave the window alone while nothing grew
if (w, h) == self.fitted and (not shrink or self.root.winfo_height() == h):
return
@@ -281,6 +364,12 @@ class App:
self.root.minsize(w, h)
self.root.geometry(f'{max(w, self.root.winfo_width())}x{h if shrink else max(h, self.root.winfo_height())}')
def scroll_content(self, parent, name):
area = ScrollArea(parent)
area.pack(fill='both', expand=True)
self.scroll_areas[name] = area
return area.content
def notice(self, parent):
w = ttk.Label(parent, style='Notice.TLabel', wraplength=WRAP, justify='left')
self.bind_text(w, 'text', 'ui.notice')
@@ -310,9 +399,10 @@ class App:
self.form_error.pack(side='bottom', anchor='w', pady=(dpi.px(4), 0))
self.update_hint = ttk.Label(f, style='Status.TLabel', wraplength=WRAP, justify='left')
self.update_hint.pack(side='bottom', anchor='w', pady=(dpi.px(8), 0))
self.form_intro = self.label(f, 'ui.intro', wraplength=WRAP, justify='left')
content = self.scroll_content(f, 'form')
self.form_intro = self.label(content, 'ui.intro', wraplength=WRAP, justify='left')
self.form_intro.pack(anchor='w', pady=(0, dpi.px(4)))
grid = ttk.Frame(f)
grid = ttk.Frame(content)
grid.pack(fill='x')
grid.columnconfigure(1, weight=1)
self.section(grid, 0, 'ui.section_files', dpi.px(6))
@@ -344,16 +434,16 @@ class App:
# On by default and named with its source and licence: the player sees what is downloaded and can untick it.
# The source and licence line is the first one under More details.
self.dlss_var = tk.BooleanVar(value=True)
dlss = ttk.Checkbutton(f, variable=self.dlss_var)
dlss = ttk.Checkbutton(content, variable=self.dlss_var)
self.bind_text(dlss, 'text', 'ui.dlss')
dlss.pack(anchor='w', pady=(dpi.px(12), 0))
# The explanations stay out of the way until asked for: a link opens them under it
self.details_toggle = ttk.Label(f, style='Link.TLabel', cursor='hand2', takefocus=True)
self.details_toggle = ttk.Label(content, style='Link.TLabel', cursor='hand2', takefocus=True)
self.bind_text(self.details_toggle, 'text', 'ui.more_details')
for seq in ('<Button-1>', '<Return>', '<space>'):
self.details_toggle.bind(seq, lambda _e: self.toggle_details())
self.details_toggle.pack(anchor='w', pady=(dpi.px(8), 0))
self.details = ttk.Frame(f) # packed after the link when open; no holder frame, as an emptied Tk frame keeps
self.details = ttk.Frame(content) # packed after the link when open; no holder frame, as an emptied Tk frame keeps
# the height its last child gave it
for id in ('ui.dlss_help', 'ui.formats_help', 'ui.compressed_help', 'ui.firmware_help', 'ui.keys_private'):
self.label(self.details, id, style='Muted.TLabel', wraplength=WRAP - dpi.px(16),
@@ -379,14 +469,15 @@ class App:
self.cancel_button.pack(side='right')
self.log_toggle = self.button(bar, 'ui.show_log', self.toggle_log)
self.log_toggle.pack(side='left')
self.run_header = ttk.Label(f, font=(self.ui_font, 11, 'bold'))
content = self.scroll_content(f, 'run')
self.run_header = ttk.Label(content, font=(self.ui_font, 11, 'bold'))
self.run_header.pack(anchor='w')
self.step_rows = {}
self.steps_box = ttk.Frame(f)
self.steps_box = ttk.Frame(content)
self.steps_box.pack(fill='x', pady=dpi.px(8))
self.phase_label = ttk.Label(f, wraplength=WRAP, justify='left')
self.phase_label = ttk.Label(content, wraplength=WRAP, justify='left')
self.phase_label.pack(anchor='w')
bar_holder = ttk.Frame(f)
bar_holder = ttk.Frame(content)
bar_holder.pack(fill='x', pady=(dpi.px(4), dpi.px(6)))
self.bar = ttk.Progressbar(bar_holder, mode='determinate', maximum=1000)
self.bar.pack(fill='both', expand=True)
@@ -396,11 +487,11 @@ class App:
# holder follows it.
bar_holder.pack_propagate(False)
bar_holder.configure(height=dpi.px(22))
self.space_label = ttk.Label(f, wraplength=WRAP, justify='left')
self.space_label = ttk.Label(content, wraplength=WRAP, justify='left')
self.space_label.pack(anchor='w')
self.warn_label = ttk.Label(f, wraplength=WRAP, justify='left', foreground='#9a6700')
self.warn_label = ttk.Label(content, wraplength=WRAP, justify='left', foreground='#9a6700')
self.warn_label.pack(anchor='w')
self.log_frame = ttk.Frame(f)
self.log_frame = ttk.Frame(content)
self.log = tk.Text(self.log_frame, height=10, wrap='word', state='disabled', font=(self.mono_font, 9))
scroll = ttk.Scrollbar(self.log_frame, command=self.log.yview)
self.log.configure(yscrollcommand=scroll.set)
@@ -420,13 +511,14 @@ class App:
self.back_button.pack(side='left')
if self.rebuild: # a failed or cancelled rebuild: the game starts with the code it has
self.button(bar, 'ui.start_game', self.start_game).pack(side='right', padx=(0, dpi.px(8)))
self.finish_title = ttk.Label(f, style='Title.TLabel', wraplength=WRAP, justify='left')
content = self.scroll_content(f, 'finish')
self.finish_title = ttk.Label(content, style='Title.TLabel', wraplength=WRAP, justify='left')
self.finish_title.pack(anchor='w')
self.finish_text = ttk.Label(f, wraplength=WRAP, justify='left')
self.finish_text = ttk.Label(content, wraplength=WRAP, justify='left')
self.finish_text.pack(anchor='w', pady=dpi.px(8))
self.finish_steps = ttk.Frame(f)
self.finish_steps = ttk.Frame(content)
self.finish_steps.pack(fill='x', pady=(0, dpi.px(8)))
self.finish_notice = self.notice(f)
self.finish_notice = self.notice(content)
self.finish_notice.pack(fill='x', pady=(dpi.px(8), 0))
return f
+90
View File
@@ -539,6 +539,96 @@ class RecentTest(unittest.TestCase):
self.assertEqual(gui.load_recent(), {'dest': 'x'})
@unittest.skipIf(gui is None, 'no tkinter')
class SmallScreenTest(unittest.TestCase):
def setUp(self):
self.tmp = tempfile.TemporaryDirectory()
self.addCleanup(self.tmp.cleanup)
env = unittest.mock.patch.dict(os.environ, {recent.ENV: str(Path(self.tmp.name) / 'recent.json')})
env.start()
self.addCleanup(env.stop)
gui.fix_tcl_paths()
self.root = gui.tk.Tk()
self.addCleanup(self.root.destroy)
self.root.tk.call('tk', 'scaling', 2.0)
for name, value in (('winfo_screenheight', 700), ('winfo_screenwidth', 1200)):
mock = unittest.mock.patch.object(self.root, name, return_value=value)
mock.start()
self.addCleanup(mock.stop)
self.app = gui.App(self.root, 'en', layouts.dev_layout(ROOT))
self.root.update()
def assert_start_visible(self):
self.assertLessEqual(self.root.winfo_height(), 700)
bottom = self.app.start_button.winfo_rooty() + self.app.start_button.winfo_height()
self.assertLessEqual(bottom, self.root.winfo_rooty() + self.root.winfo_height())
def test_form_fits_a_small_screen_with_large_fonts(self):
self.assert_start_visible()
self.app.toggle_details()
self.root.update()
self.assert_start_visible()
def test_expanded_details_are_reachable_without_moving_start(self):
self.app.toggle_details()
self.root.update()
self.assert_start_visible()
start_y = self.app.start_button.winfo_rooty()
canvas = self.app.scroll_areas['form'].canvas
canvas.yview_moveto(1)
self.root.update()
bottom = self.app.details.winfo_rooty() + self.app.details.winfo_height()
self.assertLessEqual(bottom, canvas.winfo_rooty() + canvas.winfo_height() + 1)
self.assertEqual(self.app.start_button.winfo_rooty(), start_y)
view = canvas.yview()
self.app.fit() # progress polling must not reset the user's scroll position
self.root.update()
self.assertEqual(canvas.yview(), view)
self.app.lang_box.focus_force()
self.root.update()
self.assertGreaterEqual(self.app.lang_box.winfo_rooty(), canvas.winfo_rooty())
self.assertLessEqual(self.app.lang_box.winfo_rooty() + self.app.lang_box.winfo_height(),
canvas.winfo_rooty() + canvas.winfo_height())
def test_mouse_wheel_and_linux_buttons_scroll_the_content(self):
self.app.toggle_details()
self.root.update()
canvas = self.app.scroll_areas['form'].canvas
for event, params in (('<MouseWheel>', {'delta': -120}), ('<Button-5>', {})):
with self.subTest(event=event):
canvas.yview_moveto(0)
if event == '<Button-5>' and not sys.platform.startswith('linux'):
self.app.scroll_areas['form'].wheel(SimpleNamespace(widget=canvas, num=5, delta=0))
else:
canvas.event_generate(event, **params)
self.root.update()
self.assertGreater(canvas.yview()[0], 0)
self.assert_start_visible()
def test_content_that_fits_needs_no_scrollbar(self):
with unittest.mock.patch.object(self.root, 'winfo_screenheight', return_value=2000):
self.app.fit(shrink=True)
self.root.update()
area = self.app.scroll_areas['form']
self.assertFalse(area.scrollbar.winfo_manager())
self.assertEqual(area.canvas.yview(), (0.0, 1.0))
def test_progress_and_finish_actions_stay_visible_with_long_content(self):
self.app.phase_label.configure(text='Long progress detail\n' * 40)
self.app.show('run')
self.root.update()
self.assertLessEqual(self.root.winfo_height(), 700)
self.assertLessEqual(self.app.cancel_button.winfo_rooty() + self.app.cancel_button.winfo_height(),
self.root.winfo_rooty() + self.root.winfo_height())
self.app.finish_text.configure(text='Long finish detail\n' * 40)
self.app.show('finish')
self.root.update()
close = next(w for w, _, id, _ in self.app.bound if id == 'ui.close')
self.assertLessEqual(self.root.winfo_height(), 700)
self.assertLessEqual(close.winfo_rooty() + close.winfo_height(),
self.root.winfo_rooty() + self.root.winfo_height())
class FakeVar:
def __init__(self, value=''):
self.value = value