From 3f3c6ca8b51e4fbf7beab5a1da038500568d9b8a Mon Sep 17 00:00:00 2001 From: Billy Date: Tue, 19 Mar 2024 02:23:03 +0530 Subject: [PATCH] feat: Open-editors tree using native Treeview --- .../views/sidebar/explorer/__init__.py | 4 ++ .../views/sidebar/explorer/open_editors.py | 66 +++++++++++++++++++ .../components/views/sidebar/sidebarview.py | 2 +- .../layout/base/content/editors/__init__.py | 14 ++-- 4 files changed, 75 insertions(+), 11 deletions(-) create mode 100644 biscuit/core/components/views/sidebar/explorer/open_editors.py diff --git a/biscuit/core/components/views/sidebar/explorer/__init__.py b/biscuit/core/components/views/sidebar/explorer/__init__.py index 0c78a730..c5df02f0 100644 --- a/biscuit/core/components/views/sidebar/explorer/__init__.py +++ b/biscuit/core/components/views/sidebar/explorer/__init__.py @@ -1,10 +1,12 @@ import os +import tkinter as tk from biscuit.core.components.floating.palette import ActionSet from ..sidebarview import SidebarView from .directorytree import DirectoryTree from .menu import ExplorerMenu +from .open_editors import OpenEditors class Explorer(SidebarView): @@ -20,6 +22,8 @@ def __init__(self, master, *args, **kwargs) -> None: self.menu.add_item("Search", self.base.events.show_file_search_palette) self.add_button('ellipsis', self.menu.show) + self.open_editors = OpenEditors(self) + self.open_editors.pack(fill=tk.X) self.directory = DirectoryTree(self, observe_changes=True) self.add_widget(self.directory) diff --git a/biscuit/core/components/views/sidebar/explorer/open_editors.py b/biscuit/core/components/views/sidebar/explorer/open_editors.py new file mode 100644 index 00000000..46359769 --- /dev/null +++ b/biscuit/core/components/views/sidebar/explorer/open_editors.py @@ -0,0 +1,66 @@ +import os +import platform +import shutil +import subprocess +import threading +import tkinter as tk + +import pyperclip + +from biscuit.core.components.floating.palette.actionset import ActionSet +from biscuit.core.components.utils import Tree + +from ..item import SidebarViewItem + + +class OpenEditors(SidebarViewItem): + def __init__(self, master, startpath=None, itembar=True, *args, **kwargs) -> None: + self.title = 'Open Editors' + self.__buttons__ = (('new-file', lambda: self.base.palette.show('newfile:')),) + super().__init__(master, itembar, *args, **kwargs) + + self.tree = Tree(self.content, startpath, singleclick=self.openfile, *args, **kwargs) + self.tree.bind("", self.adjust_frame_size) + self.tree.grid(row=0, column=0, sticky=tk.NSEW) + self.tree.bind("<>", self.openfile) + + self.path = startpath + + def adjust_frame_size(self, *_): + if items := self.tree.get_children(): + frame_height = len(items) * 25 + 5 + else: + frame_height = 1 + self.tree.config(height=frame_height) + self.update() + + def add_item(self, editor): + self.tree.insert('', 'end', text=editor.filename, values=(editor.path, 'file')) + self.adjust_frame_size() + + def remove_item(self, editor): + for child in self.tree.get_children(): + if self.tree.item_fullpath(child) == editor.path: + self.tree.delete(child) + self.adjust_frame_size() + if not len(self.base.editorsmanager.active_editors): + self.disable() + return + + def set_active(self, editor): + for child in self.tree.get_children(): + if self.tree.item_fullpath(child) == editor.path: + self.tree.tree.selection_set(child) + self.adjust_frame_size() + return + + def clear(self): + self.tree.delete(*self.tree.get_children()) + self.adjust_frame_size() + + def openfile(self, _) -> None: + if self.tree.selected_type() != 'file': + return + + path = self.tree.selected_path() + self.base.open_editor(path) diff --git a/biscuit/core/components/views/sidebar/sidebarview.py b/biscuit/core/components/views/sidebar/sidebarview.py index d57a559d..f7179dc0 100644 --- a/biscuit/core/components/views/sidebar/sidebarview.py +++ b/biscuit/core/components/views/sidebar/sidebarview.py @@ -11,7 +11,7 @@ class SidebarView(View): def __init__(self, master, *args, **kwargs) -> None: super().__init__(master, *args, **kwargs) - self.__buttons__: list[tuple(str, typing.Callable)] = [] + self.__buttons__: list[tuple(str, typing.Callable)] = [] # type: ignore self.__icon__ = 'preview' self.name = "View" self.__name__ = self.__class__.__name__ diff --git a/biscuit/core/layout/base/content/editors/__init__.py b/biscuit/core/layout/base/content/editors/__init__.py index 7915b5a2..81a83c34 100644 --- a/biscuit/core/layout/base/content/editors/__init__.py +++ b/biscuit/core/layout/base/content/editors/__init__.py @@ -1,15 +1,5 @@ """ Holds the editors and provides an interface to manage the editor tabs. - -+---------------------------------+ -| File1.txt | File2.py | | -+---------------------------------+ -| \ \ \ \ \ \ \ | -| \ \ \ \ \ \ \| -| \ \ \ \ \ \ | -| \ \ \ \ \ \ | -|\ \ \ \ \ \ \ | -+---------------------------------+ """ from __future__ import annotations @@ -88,6 +78,7 @@ def add_editor(self, editor: Union[Editor,BaseEditor]) -> Editor | BaseEditor: if editor.content: editor.content.create_buttons(self.editorsbar.container) self.tabs.add_tab(editor) + self.base.explorer.open_editors.add_item(editor) self.refresh() return editor @@ -146,6 +137,7 @@ def close_editor(self, editor: Editor) -> None: self.closed_editors[editor.path] = editor else: editor.destroy() + self.base.explorer.open_editors.remove_item(editor) self.refresh() def get_editor(self, path: str) -> Editor: @@ -166,6 +158,7 @@ def delete_editor(self, editor: Editor) -> None: self.closed_editors.remove(editor) editor.destroy() + self.base.explorer.open_editors.remove_item(editor) self.refresh() def set_active_editor(self, editor: Editor) -> Editor: @@ -173,6 +166,7 @@ def set_active_editor(self, editor: Editor) -> Editor: for tab in self.tabs.tabs: if tab.editor == editor: self.tabs.set_active_tab(tab) + self.base.explorer.open_editors.set_active(editor) return editor