Skip to content

Commit

Permalink
feat: Open-editors tree using native Treeview
Browse files Browse the repository at this point in the history
  • Loading branch information
tomlin7 committed Mar 18, 2024
1 parent 7f4ebfa commit 3f3c6ca
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 11 deletions.
4 changes: 4 additions & 0 deletions biscuit/core/components/views/sidebar/explorer/__init__.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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)

Expand Down
66 changes: 66 additions & 0 deletions biscuit/core/components/views/sidebar/explorer/open_editors.py
Original file line number Diff line number Diff line change
@@ -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("<Configure>", self.adjust_frame_size)
self.tree.grid(row=0, column=0, sticky=tk.NSEW)
self.tree.bind("<<Open>>", 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)
2 changes: 1 addition & 1 deletion biscuit/core/components/views/sidebar/sidebarview.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__
Expand Down
14 changes: 4 additions & 10 deletions biscuit/core/layout/base/content/editors/__init__.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
"""
Holds the editors and provides an interface to manage the editor tabs.
+---------------------------------+
| File1.txt | File2.py | |
+---------------------------------+
| \ \ \ \ \ \ \ |
| \ \ \ \ \ \ \|
| \ \ \ \ \ \ |
| \ \ \ \ \ \ |
|\ \ \ \ \ \ \ |
+---------------------------------+
"""
from __future__ import annotations

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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:
Expand All @@ -166,13 +158,15 @@ 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:
"set an existing editor to currently shown one"
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

Expand Down

0 comments on commit 3f3c6ca

Please sign in to comment.