Add "user" tab to themes kitten

Add new tab named "user" to the built-in themes kitten - new tab
filters and displays only user-defined color themes, identified by
adding "is_user_defined" property to "Theme" class which defaults
to False, and is set True when a theme is fetched using the
"load_from_dir" method.
This commit is contained in:
James Reid 2022-05-30 19:25:09 +01:00
parent 1dc6b49e02
commit 0b79cbdb3b
No known key found for this signature in database
GPG Key ID: 4359023131208302
2 changed files with 10 additions and 3 deletions

View File

@ -494,6 +494,7 @@ class Theme:
is_dark: bool = False is_dark: bool = False
blurb: str = '' blurb: str = ''
num_settings: int = 0 num_settings: int = 0
is_user_defined: bool = False
def apply_dict(self, d: Dict[str, Any]) -> None: def apply_dict(self, d: Dict[str, Any]) -> None:
self.name = str(d['name']) self.name = str(d['name'])
@ -501,7 +502,7 @@ class Theme:
a = d.get(x) a = d.get(x)
if isinstance(a, str): if isinstance(a, str):
setattr(self, x, a) setattr(self, x, a)
for x in ('is_dark', 'num_settings'): for x in ('is_dark', 'num_settings', 'is_user_defined'):
a = d.get(x) a = d.get(x)
if isinstance(a, int): if isinstance(a, int):
setattr(self, x, a) setattr(self, x, a)
@ -594,6 +595,7 @@ class Themes:
d = parse_theme(name, raw) d = parse_theme(name, raw)
except (Exception, SystemExit): except (Exception, SystemExit):
continue continue
d['is_user_defined'] = True
t = Theme(raw.__str__) t = Theme(raw.__str__)
t.apply_dict(d) t.apply_dict(d)
if t.name: if t.name:

View File

@ -69,6 +69,10 @@ def create_recent_filter(names: Iterable[str]) -> Callable[[Theme], bool]:
return recent_filter return recent_filter
def user_filter(q: Theme) -> bool:
return q.is_user_defined
def mark_shortcut(text: str, acc: str) -> str: def mark_shortcut(text: str, acc: str) -> str:
acc_idx = text.lower().index(acc.lower()) acc_idx = text.lower().index(acc.lower())
return text[:acc_idx] + styled(text[acc_idx], underline='straight', bold=True, fg_intense=True) + text[acc_idx+1:] return text[:acc_idx] + styled(text[acc_idx], underline='straight', bold=True, fg_intense=True) + text[acc_idx+1:]
@ -153,12 +157,13 @@ class ThemesHandler(Handler):
self.report_traceback_on_exit: Optional[str] = None self.report_traceback_on_exit: Optional[str] = None
self.filter_map: Dict[str, Callable[[Theme], bool]] = { self.filter_map: Dict[str, Callable[[Theme], bool]] = {
'dark': dark_filter, 'light': light_filter, 'all': all_filter, 'dark': dark_filter, 'light': light_filter, 'all': all_filter,
'recent': create_recent_filter(self.cached_values.get('recent', ())) 'recent': create_recent_filter(self.cached_values.get('recent', ())),
'user': user_filter
} }
self.themes_list = ThemesList() self.themes_list = ThemesList()
self.colors_set_once = False self.colors_set_once = False
self.line_edit = LineEdit() self.line_edit = LineEdit()
self.tabs = tuple('all dark light recent'.split()) self.tabs = tuple('all dark light recent user'.split())
self.quit_on_next_key_release = -1 self.quit_on_next_key_release = -1
def update_recent(self) -> None: def update_recent(self) -> None: