From 8b477e7fd36473162ceb89b85b88ab6607025bbc Mon Sep 17 00:00:00 2001 From: Bruno Date: Fri, 1 Mar 2024 15:16:01 -0300 Subject: [PATCH] WIP - Custom launcher management Minor readability improvements (questionable) Editing launchers requires a restart. --- Mopy/bash/basher/settings_dialog.py | 97 +++++++++++++---------------- 1 file changed, 44 insertions(+), 53 deletions(-) diff --git a/Mopy/bash/basher/settings_dialog.py b/Mopy/bash/basher/settings_dialog.py index aa94774cb7..52628236d0 100644 --- a/Mopy/bash/basher/settings_dialog.py +++ b/Mopy/bash/basher/settings_dialog.py @@ -1560,24 +1560,21 @@ def __init__(self, parent, page_desc): self._is_creating_launcher = False - # List of existing launchers, plus item # display name is just head of launcher path - #TODO swap to that listbox that can display icons + # TODO swap to that listbox that can display icons self._launcher_listbox = ListBox(self, isSort=True, isHScroll=True, - onSelect=self._handle_launcher_selected) + onSelect=self._handle_launcher_selected) # 3 Text boxes: name, path and args + self._launcher_name_txt = TextField(self) + self._launcher_name_txt.tooltip = _('Shortcut name') + self._launcher_path_txt = TextField(self) self._launcher_path_txt.tooltip = _('Path to application') self._launcher_args_txt = TextField(self) self._launcher_args_txt.tooltip = _('Command line arguments') - self._launcher_name_txt = TextField(self) - self._launcher_name_txt.tooltip = _('Shortcut name') - - self._clear_textfields() - # 4 buttons: save, remove, new, and pick file self._pick_launcher_file_btn = OpenButton(self, btn_label=_(u'Select...'), @@ -1601,7 +1598,10 @@ def __init__(self, parent, page_desc): self._new_launcher_mode) # that's it, time to build the list + self._cleanup() self._populate_launcher_listbox() + + # finish drawing page VLayout(border=6, spacing=4, item_expand=True, items=[ self._page_desc_label, HorizontalLine(self), @@ -1619,8 +1619,23 @@ def __init__(self, parent, page_desc): self._new_launcher_btn ])]).apply_to(self) + def _cleanup(self): + # clear text fields + self._clear_textfields() + self._save_launcher_btn.button_label = _('Save') + self._save_launcher_btn.tooltip = _('Save launcher') + self._remove_launcher_btn.button_label = _('Remove') + self._remove_launcher_btn.tooltip = _('Remove launcher') + self._new_launcher_btn.button_label = _('New') + self._new_launcher_btn.tooltip = _('New launcher') + + def _clear_textfields(self): + for txt_field in (self._launcher_args_txt, self._launcher_path_txt, + self._launcher_name_txt): + txt_field.text_content = '' + def _populate_launcher_listbox(self): - items = self.___filter() + items = list() for custom_launcher in bass.settings['bash.launchers']: items.append(custom_launcher) self._launcher_listbox.lb_set_items(items) @@ -1637,25 +1652,14 @@ def _update_file_txt_field(self): self._launcher_path_txt.text_content = str(picked_file) def _handle_launcher_selected(self, index: int, selected_str): - # if selected launcher is one of the preconfigured ones: - # make name and args not editable; - # change remove to reset which will restore the default path - - if selected_str not in bass.settings['bash.launchers']: #TODO diff stock/custom - self._launcher_name_txt.editable = False - self._launcher_args_txt.editable = False - self._remove_launcher_btn.button_label = _('Reset') - self._remove_launcher_btn.tooltip = _('Restore default settings') - self._remove_launcher_btn.enabled = True #FIXME might not need to - # enable if the path wasn't changed, but here it is - selected_launcher = BashStatusBar.all_sb_links[selected_str] - else: - selected_launcher = bass.settings['bash.launchers'][selected_str] + selected_launcher = bass.settings['bash.launchers'][selected_str] + self._remove_launcher_btn.enabled = True + self._save_launcher_btn.enabled = True # copy launcher details to textfields self._launcher_name_txt.text_content = selected_str # FIXME unify launchers - if path_obj := getattr(selected_launcher, 'app_path',None): + if path_obj := getattr(selected_launcher, 'app_path', None): # has a Path(TM) self._launcher_path_txt.text_content = path_obj.s self._launcher_args_txt.text_content = shlex.join( @@ -1669,33 +1673,23 @@ def _reset_textfields(self): self._launcher_args_txt.text_content =_('Command line arguments') self._launcher_name_txt.text_content = _('Shortcut name') - def _clear_textfields(self): - #self._remove_launcher_btn.enabled = False - for txt_field in (self._launcher_args_txt, self._launcher_path_txt, - self._launcher_name_txt): - txt_field.text_content = u'' - - def _new_launcher_mode(self, disable: bool = False): + def _new_launcher_mode(self, enable: bool = True): """'New Launcher Mode' is basically disabling the listbox, the 'New' button and changing the Remove button into a Cancel button. - Pass disable = True to return to the normal view (i.e. when calling it + Pass enable = False to return to the normal view (i.e. when calling it from Cancel button)""" # Add text/tooltip to textboxes - if not disable: + if enable: self._reset_textfields() for comp in self._launcher_listbox, self._new_launcher_btn: - comp.enabled = disable - self._remove_launcher_btn.button_label = _('Remove') if disable else _('Cancel') + comp.enabled = not enable + self._remove_launcher_btn.button_label = _('Cancel') if enable else _('Remove') # enable cancel button - self._remove_launcher_btn.enabled = not disable + self._remove_launcher_btn.enabled = enable # enable editing textboxes if they're disabled for comp in self._launcher_args_txt, self._launcher_name_txt, self._launcher_path_txt: - comp.editable = not disable - self._is_creating_launcher = not disable - - # Apologies for the low readability of the above code with all that not - # disable nonsense. It is, however, much more succinct like this. Just - # know we're toggling that alternate text mode. + comp.editable = enable + self._is_creating_launcher = enable ## TODO validation/error handling def _save_launcher(self): @@ -1706,17 +1700,16 @@ def _save_launcher(self): launcher_path = self._launcher_path_txt.text_content launcher_args = self._launcher_args_txt.text_content - if not launcher_name in BashStatusBar.all_sb_links: - bass.settings['bash.launchers'][launcher_name] = [launcher_path, + bass.settings['bash.launchers'][launcher_name] = [launcher_path, launcher_args,] - self._new_launcher_mode(True) - self._clear_textfields() - self._populate_launcher_listbox() + self._new_launcher_mode(False) + self._cleanup() + self._populate_launcher_listbox() def _remove_or_cancel(self): from ..gui import popups if self._is_creating_launcher: # cancel - self._new_launcher_mode(disable=True) + self._new_launcher_mode(False) self._clear_textfields() return # remove @@ -1724,11 +1717,9 @@ def _remove_or_cancel(self): # user canceled in confirm dialog return launcher_name = self._launcher_name_txt.text_content - try: - del bass.settings['bash.launchers'][launcher_name] - except KeyError: - pass # FIXME exception-oriented crap - self._clear_textfields() + if launcher_name in bass.settings['bash.launchers']: + del bass.settings['bash.launchers'][launcher_name] + self._cleanup() self._populate_launcher_listbox() # Page Definitions ------------------------------------------------------------