diff --git a/installer/gui.py b/installer/gui.py index 2c89ec78..9c0dc1fd 100644 --- a/installer/gui.py +++ b/installer/gui.py @@ -82,6 +82,11 @@ class App: self.bound = [] # (widget, option, id, params) retranslated on a language switch self.vars = {n: tk.StringVar() for n in ('base', 'update', 'keys', 'firmware', 'dest')} self.vars['lang'] = tk.StringVar() + self.recorded = {} # base/update paths prefilled from the recorded ones, by field + self.touched = set() # fields the user picked or typed + self.programmatic = False + for name in ('base', 'update'): + self.vars[name].trace_add('write', lambda *_, n=name: self.touched.add(n) if not self.programmatic else None) self.lang_code = default_game_language(ui_lang) self.screen = None self.fitted = None # the (width, height) fit last gave the window @@ -133,9 +138,10 @@ class App: # recorded language). In update mode filled files are harmless: Extract.current hashes one only when its # size or mtime differ from the recorded ones. last = load_recent() + self.recorded = {n: last[n] for n in ('base', 'update') if n in last and not self.vars[n].get().strip()} for name in ('base', 'update', 'keys', 'firmware', 'dest'): if name in last and not self.vars[name].get().strip(): - self.vars[name].set(last[name]) + self.set_prefill(name, last[name]) # ---- text ----------------------------------------------------------------------------------------------------- @@ -340,9 +346,35 @@ class App: firmware=self.vars['firmware'].get(), lang=self.selected_lang(), dest=self.vars['dest'].get(), dlss=self.dlss_var.get()) + def set_prefill(self, name, value): + self.programmatic = True + try: + self.vars[name].set(value) + finally: + self.programmatic = False + + def match_prefill(self, state): + """Prefilled base/update stay only while the chosen install recorded the same file (name and size, one stat); + another install would otherwise extract again from them. Fields the user touched are left alone.""" + for name, value in self.recorded.items(): + if name in self.touched: + continue + keep = True + if state is not None: + known = (state.get('inputs') or {}).get(name) + try: + keep = (isinstance(known, dict) and Path(value).name == known.get('name') + and Path(value).stat().st_size == known.get('size')) + except (OSError, ValueError): + keep = False + want = value if keep else '' + if self.vars[name].get() != want: + self.set_prefill(name, want) + def refresh_form(self): form = self.form() state = form.installed_state() + self.match_prefill(state) if state is not None: self.update_hint.configure(text=self.cat('ui.update_mode')) recorded = ini.canonical_language(state.get('lang')) diff --git a/tests/installer_gui_test.py b/tests/installer_gui_test.py index 3d2a370d..b8ba833b 100644 --- a/tests/installer_gui_test.py +++ b/tests/installer_gui_test.py @@ -517,6 +517,73 @@ class RecentTest(unittest.TestCase): self.assertEqual(gui.load_recent(), {'dest': 'x'}) +class FakeVar: + def __init__(self, value=''): + self.value = value + + def get(self): + return self.value + + def set(self, value): + self.value = value + + +@unittest.skipIf(gui is None, 'no tkinter') +class PrefillTest(unittest.TestCase): + """App.match_prefill on a stand-in object: no window needed.""" + + def setUp(self): + self.tmp = Path(tempfile.mkdtemp()) + self.addCleanup(shutil.rmtree, self.tmp, True) + self.update = self.tmp / 'totk-1.4.3.nsp' + self.update.write_bytes(b'x' * 10) + self.base = self.tmp / 'totk.xci' + self.base.write_bytes(b'y' * 20) + self.app = SimpleNamespace(vars={n: FakeVar() for n in ('base', 'update')}, touched=set(), + recorded={'base': str(self.base), 'update': str(self.update)}) + self.app.set_prefill = lambda name, value: self.app.vars[name].set(value) + for name, value in self.app.recorded.items(): + self.app.vars[name].set(value) + + def state(self, update_name, update_size, base_size=20): + return {'inputs': {'base': {'name': 'totk.xci', 'size': base_size}, + 'update': {'name': update_name, 'size': update_size}}} + + def shown(self): + return {n: v.get() for n, v in self.app.vars.items()} + + def test_same_install_keeps(self): + gui.App.match_prefill(self.app, self.state('totk-1.4.3.nsp', 10)) + self.assertEqual(self.shown(), {'base': str(self.base), 'update': str(self.update)}) + + def test_other_install_clears_and_restores(self): + gui.App.match_prefill(self.app, self.state('totk-1.2.1.nsp', 10)) + self.assertEqual(self.shown(), {'base': str(self.base), 'update': ''}) + gui.App.match_prefill(self.app, self.state('totk-1.4.3.nsp', 99)) # same name, other size + self.assertEqual(self.shown()['update'], '') + gui.App.match_prefill(self.app, self.state('totk-1.4.3.nsp', 10)) + self.assertEqual(self.shown()['update'], str(self.update)) + + def test_install_without_recorded_inputs_clears(self): + gui.App.match_prefill(self.app, {'inputs': {}}) + self.assertEqual(self.shown(), {'base': '', 'update': ''}) + + def test_user_picked_field_survives(self): + self.app.vars['update'].set('C:/mine/other.nsp') + self.app.touched.add('update') + gui.App.match_prefill(self.app, self.state('totk-1.2.1.nsp', 10, base_size=1)) + self.assertEqual(self.shown(), {'base': '', 'update': 'C:/mine/other.nsp'}) + + def test_fresh_install_keeps(self): + gui.App.match_prefill(self.app, None) + self.assertEqual(self.shown(), {'base': str(self.base), 'update': str(self.update)}) + + def test_vanished_file_clears(self): + self.update.unlink() + gui.App.match_prefill(self.app, self.state('totk-1.4.3.nsp', 10)) + self.assertEqual(self.shown()['update'], '') + + class SmokeTest(unittest.TestCase): def test_every_screen_in_both_languages(self): try: