Skip to content

Commit

Permalink
WIP - Custom launcher management
Browse files Browse the repository at this point in the history
Minor readability improvements (questionable)

Editing launchers requires a restart.
  • Loading branch information
BGazotti committed Mar 1, 2024
1 parent cb12191 commit 8b477e7
Showing 1 changed file with 44 additions and 53 deletions.
97 changes: 44 additions & 53 deletions Mopy/bash/basher/settings_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -1560,24 +1560,21 @@ def __init__(self, parent, page_desc):

self._is_creating_launcher = False

# List of existing launchers, plus <New> 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...'),
Expand All @@ -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),
Expand All @@ -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)
Expand All @@ -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(
Expand All @@ -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):
Expand All @@ -1706,29 +1700,26 @@ 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
if not popups.askYes(parent=self, message=_('Are you sure?'), title=_('Remove Launcher'), default_is_yes=False):
# 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 ------------------------------------------------------------
Expand Down

0 comments on commit 8b477e7

Please sign in to comment.