class CollapseFrame(Frame): def __init__(self, master, **cnf): super().__init__(master, **cnf) self.config(**self.style.dark) self._label_frame = Frame(self, **self.style.bright, height=20) self._label_frame.pack(side="top", fill="x", padx=2) self._label_frame.pack_propagate(0) self._label = Label(self._label_frame, **self.style.bright, **self.style.text_bright) self._label.pack(side="left") self._collapse_btn = Button(self._label_frame, width=20, **self.style.bright, **self.style.text_bright) self._collapse_btn.config(text=get_icon("triangle_up")) self._collapse_btn.pack(side="right", fill="y") self._collapse_btn.on_click(self.toggle) self.body = Frame(self, **self.style.dark) self.body.pack(side="top", fill="both", pady=2) self.__ref = Frame(self.body, height=0, width=0, **self.style.dark) self.__ref.pack(side="top") self._collapsed = False def update_state(self): self.__ref.pack(side="top") def collapse(self, *_): if not self._collapsed: self.body.pack_forget() self._collapse_btn.config(text=get_icon("triangle_down")) self.pack_propagate(0) self.config(height=20) self._collapsed = True def clear_children(self): self.body.clear_children() def expand(self, *_): if self._collapsed: self.body.pack(side="top", fill="both") self.pack_propagate(1) self._collapse_btn.config(text=get_icon("triangle_up")) self._collapsed = False def toggle(self, *_): if self._collapsed: self.expand() else: self.collapse() @property def label(self): return self._label["text"] @label.setter def label(self, value): self._label.config(text=value)
class StylePane(BaseFeature): name = "Style pane" icon = "edit" _defaults = { **BaseFeature._defaults, "side": "right", } def __init__(self, master, studio, **cnf): super().__init__(master, studio, **cnf) self.body = ScrolledFrame(self, **self.style.dark) self.body.pack(side="top", fill="both", expand=True) self._toggle_btn = Button(self._header, image=get_icon_image("chevron_down", 15, 15), **self.style.dark_button, width=25, height=25) self._toggle_btn.pack(side="right") self._toggle_btn.on_click(self._toggle) self._search_btn = Button(self._header, image=get_icon_image("search", 15, 15), width=25, height=25, **self.style.dark_button) self._search_btn.pack(side="right") self._search_btn.on_click(self.start_search) self.groups = [] self._identity_group = self.add_group(IdentityGroup) self._layout_group = self.add_group(LayoutGroup) self._attribute_group = self.add_group(AttributeGroup) self._empty_frame = Frame(self.body) self.show_empty() self._current = None self._expanded = False def create_menu(self): return (("command", "Search", get_icon_image("search", 14, 14), self.start_search, {}), ("command", "Expand all", get_icon_image("chevron_down", 14, 14), self.expand_all, {}), ("command", "Collapse all", get_icon_image("chevron_up", 14, 14), self.collapse_all, {})) def add_group(self, group_class) -> StyleGroup: if not issubclass(group_class, StyleGroup): raise ValueError('type required.') group = group_class(self.body.body) self.groups.append(group) group.pack(side='top', fill='x', pady=4) return group def show_empty(self): self.remove_empty() self._empty_frame.place(x=0, y=0, relheight=1, relwidth=1) Label(self._empty_frame, text="You have not selected any item", **self.style.dark_text_passive).place(x=0, y=0, relheight=1, relwidth=1) def remove_empty(self): self._empty_frame.clear_children() self._empty_frame.place_forget() def show_loading(self): self.remove_empty() self._empty_frame.place(x=0, y=0, relheight=1, relwidth=1) Label(self._empty_frame, text="Loading...", **self.style.dark_text_passive).place(x=0, y=0, relheight=1, relwidth=1) def styles_for(self, widget): self._current = widget if widget is None: self.show_empty() return self.show_loading() for group in self.groups: group.on_widget_change(widget) self.remove_empty() self.body.update_idletasks() def layout_for(self, widget): self._layout_group.on_widget_change(widget) def on_select(self, widget): self.styles_for(widget) def on_widget_change(self, old_widget, new_widget=None): self.styles_for(new_widget) def on_widget_layout_change(self, widget): self.layout_for(widget) def expand_all(self): for group in self.groups: group.expand() self._expanded = True self._toggle_btn.config(image=get_icon_image("chevron_up", 15, 15)) def clear_all(self): for group in self.groups: group.clear_children() def collapse_all(self): for group in self.groups: group.collapse() self._expanded = False self._toggle_btn.config(image=get_icon_image("chevron_down", 15, 15)) def _toggle(self, *_): if not self._expanded: self.expand_all() else: self.collapse_all() def __update_frames(self): for group in self.groups: group.update_state() def start_search(self, *_): if self._current: super().start_search() self.body.scroll_to_start() def on_search_query(self, query): for group in self.groups: group.on_search_query(query) self.__update_frames() def on_search_clear(self): for group in self.groups: group.on_search_clear() # The search bar is being closed and we need to bring everything back super().on_search_clear()
class CollapseFrame(Frame): __icons_loaded = False EXPAND = None COLLAPSE = None def __init__(self, master, **cnf): super().__init__(master, **cnf) self._load_icons() self.config(**self.style.surface) self._label_frame = Frame(self, **self.style.bright, height=20) self._label_frame.pack(side="top", fill="x", padx=2) self._label_frame.pack_propagate(0) self._label = Label(self._label_frame, **self.style.bright, **self.style.text_bright) self._label.pack(side="left") self._collapse_btn = Button(self._label_frame, width=20, **self.style.bright, **self.style.text_bright) self._collapse_btn.config(image=self.COLLAPSE) self._collapse_btn.pack(side="right", fill="y") self._collapse_btn.on_click(self.toggle) self.body = Frame(self, **self.style.surface) self.body.pack(side="top", fill="both", pady=2) self.__ref = Frame(self.body, height=0, width=0, **self.style.surface) self.__ref.pack(side="top") self._collapsed = False @classmethod def _load_icons(cls): if cls.__icons_loaded: return cls.EXPAND = get_icon_image("triangle_down", 14, 14) cls.COLLAPSE = get_icon_image("triangle_up", 14, 14) def update_state(self): self.__ref.pack(side="top") def collapse(self, *_): if not self._collapsed: self.body.pack_forget() self._collapse_btn.config(image=self.EXPAND) self.pack_propagate(0) self.config(height=20) self._collapsed = True def clear_children(self): self.body.clear_children() def expand(self, *_): if self._collapsed: self.body.pack(side="top", fill="both") self.pack_propagate(1) self._collapse_btn.config(image=self.COLLAPSE) self._collapsed = False def toggle(self, *_): if self._collapsed: self.expand() else: self.collapse() @property def label(self): return self._label["text"] @label.setter def label(self, value): self._label.config(text=value)
class StylePaneFramework: def setup_style_pane(self): self.body = ScrolledFrame(self, **self.style.surface) self.body.pack(side="top", fill="both", expand=True) self._toggle_btn = Button(self.get_header(), image=get_icon_image("chevron_down", 15, 15), **self.style.button, width=25, height=25) self._toggle_btn.pack(side="right") self._toggle_btn.on_click(self._toggle) self._search_btn = Button(self.get_header(), image=get_icon_image("search", 15, 15), width=25, height=25, **self.style.button) self._search_btn.pack(side="right") self._search_btn.on_click(self.start_search) self.groups = [] self._empty_frame = Frame(self.body) self.show_empty() self._current = None self._expanded = False self._is_loading = False self._search_query = None def get_header(self): raise NotImplementedError() @property def supported_groups(self): return [ group for group in self.groups if group.supports_widget(self._current) ] def create_menu(self): return (("command", "Search", get_icon_image("search", 14, 14), self.start_search, {}), ("command", "Expand all", get_icon_image("chevron_down", 14, 14), self.expand_all, {}), ("command", "Collapse all", get_icon_image("chevron_up", 14, 14), self.collapse_all, {})) def extern_apply(self, group_class, prop, value, widget=None, silent=False): for group in self.groups: if group.__class__ == group_class: group.apply(prop, value, widget, silent) return raise ValueError(f"Class {group_class.__class__.__name__} not found") def last_action(self): raise NotImplementedError() def new_action(self, action): raise NotImplementedError() def widget_modified(self, widget): raise NotImplementedError() def add_group(self, group_class, **kwargs) -> StyleGroup: if not issubclass(group_class, StyleGroup): raise ValueError('type required.') group = group_class(self.body.body, self, **kwargs) self.groups.append(group) self.show_group(group) return group def add_group_instance(self, group_instance, show=False): if not isinstance(group_instance, StyleGroup): raise ValueError('Expected object of type StyleGroup.') self.groups.append(group_instance) if show: self.show_group(group_instance) def hide_group(self, group): if group.self_positioned: group._hide_group() return group.pack_forget() def show_group(self, group): if group.self_positioned: group._show_group() return group.pack(side='top', fill='x', pady=12) def show_empty(self): self.remove_empty() self._empty_frame.place(x=0, y=0, relheight=1, relwidth=1) Label(self._empty_frame, text="You have not selected any item", **self.style.text_passive).place(x=0, y=0, relheight=1, relwidth=1) def remove_empty(self): self._empty_frame.clear_children() self._empty_frame.place_forget() def show_loading(self): if platform_is(LINUX) or self._is_loading: # render transitions in linux are very fast and glitch free # for other platforms or at least for windows we need to hide the glitching return self.remove_empty() self._empty_frame.place(x=0, y=0, relheight=1, relwidth=1) Label(self._empty_frame, text="Loading...", **self.style.text_passive).place(x=0, y=0, relheight=1, relwidth=1) self._is_loading = True def remove_loading(self): self.remove_empty() self._is_loading = False def styles_for(self, widget): self._current = widget if widget is None: self.show_empty() return for group in self.groups: if group.supports_widget(widget): self.show_group(group) group.on_widget_change(widget) else: self.hide_group(group) self.remove_loading() self.body.update_idletasks() def layout_for(self, widget): for group in self.groups: if group.handles_layout: group.on_widget_change(widget) self.remove_loading() def on_select(self, widget): self.styles_for(widget) def on_widget_change(self, old_widget, new_widget=None): if new_widget is None: new_widget = old_widget self.styles_for(new_widget) def on_widget_layout_change(self, widget): self.layout_for(widget) def expand_all(self): for group in self.groups: group.expand() self._expanded = True self._toggle_btn.config(image=get_icon_image("chevron_up", 15, 15)) def clear_all(self): for group in self.groups: group.clear_children() def collapse_all(self): for group in self.groups: group.collapse() self._expanded = False self._toggle_btn.config(image=get_icon_image("chevron_down", 15, 15)) def _toggle(self, *_): if not self._expanded: self.expand_all() else: self.collapse_all() def __update_frames(self): for group in self.groups: group.update_state() def start_search(self, *_): if self._current: super().start_search() self.body.scroll_to_start() def on_search_query(self, query): for group in self.groups: group.on_search_query(query) self.__update_frames() self.body.scroll_to_start() self._search_query = query def on_search_clear(self): for group in self.groups: group.on_search_clear() # The search bar is being closed and we need to bring everything back super().on_search_clear() self._search_query = None