Skip to content

Commit

Permalink
feat: Floating open editors list for managing editors
Browse files Browse the repository at this point in the history
  • Loading branch information
tomlin7 committed Oct 7, 2024
1 parent 7f53242 commit 404235f
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/biscuit/common/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from .io import IO
from .menu import Checkable, Command, Dropdown, Menu
from .notifications import Notification, Notifications
from .open_editors import OpenEditors
from .palette import Palette
from .sysinfo import SysInfo
from .textutils import *
2 changes: 2 additions & 0 deletions src/biscuit/common/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ def _process_in(self) -> None:
self.p.stdin.write(chunk)
except queue.Empty:
continue
except OSError:
continue

try:
self.p.stdin.flush()
Expand Down
87 changes: 87 additions & 0 deletions src/biscuit/common/open_editors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import tkinter as tk

from biscuit.common.ui import Closable, Frame
from biscuit.common.ui.native import Label, Toplevel


class OpenEditors(Toplevel):
"""Toplevel view that displays the open editors in the editor tabs.
The OpenEditors view displays the open editors in the editor tabs.
- The user can switch between open editors, close the editors."""

def __init__(self, master, *args, **kwargs) -> None:
super().__init__(master, *args, **kwargs)
self.config(bg=self.base.theme.border)
self.overrideredirect(True)
self.nodes = {}
self.withdraw()

self.container = Frame(self)
self.container.pack(fill=tk.BOTH, expand=True, padx=1, pady=1)

Label(
self.container,
text="Opened Editors",
font=self.base.settings.uifont,
padx=10,
pady=5,
**self.base.theme.utils.label,
).pack(fill=tk.X, expand=True, anchor=tk.W)

self.bind("<FocusOut>", lambda _: self.hide())

def show(self, e: tk.Event) -> None:
btn = e.widget
x, y = (
btn.winfo_rootx() - self.winfo_width() + btn.winfo_width(),
btn.winfo_rooty() + btn.winfo_height(),
)
self.geometry(f"+{x}+{y}")
self.deiconify()
self.update()
self.focus_force()

def hide(self) -> None:
self.withdraw()

def add_item(self, editor):
temp = Closable(
self.container,
text=editor.filename or editor.path,
callback=lambda p=editor.path: self.openfile(p),
closefn=lambda p=editor.path: self.closefile(p),
)
try:
temp.text_label.config(anchor=tk.W)
except AttributeError:
pass

temp.pack(fill=tk.X, expand=True)
self.nodes[editor.path] = temp
self.update()

def remove_item(self, editor):
if not self.nodes:
return

e = self.nodes.pop(editor.path)
e.pack_forget()
e.destroy()
self.update()

def set_active(self, editor):
# TODO: set highlight, clear highlight on others
...

def clear(self):
for node in self.nodes.values():
node.destroy()
self.nodes = {}

def openfile(self, path) -> None:
self.base.editorsmanager.editorsbar.switch_tabs(path)

def closefile(self, path) -> None:
e = self.base.editorsmanager.close_editor_by_path(path)
self.base.editorsmanager.editorsbar.close_tab_helper(e)
8 changes: 5 additions & 3 deletions src/biscuit/common/ui/closables.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def __init__(
callback=lambda *_: None,
closefn=lambda *_: None,
iconside=tk.LEFT,
padx=5,
padx=1,
pady=1,
fg=None,
bg=None,
Expand Down Expand Up @@ -60,7 +60,7 @@ def __init__(
fg=self.fg,
font=("codicon", 14),
)
self.icon_label.pack(side=iconside, fill=tk.BOTH)
self.icon_label.pack(side=iconside, fill=tk.BOTH, padx=(10, 0))

if text:
self.text_label = tk.Label(
Expand All @@ -72,7 +72,9 @@ def __init__(
fg=self.fg,
font=self.base.settings.uifont,
)
self.text_label.pack(side=iconside, fill=tk.BOTH, expand=True)
self.text_label.pack(
side=iconside, fill=tk.BOTH, expand=True, padx=10 if not icon else 0
)

self.close_btn = IconButton(self, Icons.CLOSE, self.closefn)
self.close_btn.config(bg=self.bg, fg=self.fg)
Expand Down
1 change: 1 addition & 0 deletions src/biscuit/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ def setup_floating_widgets(self) -> None:
self.findreplace = FindReplace(self)
self.notifications = Notifications(self)

self.open_editors = OpenEditors(self)
self.autocomplete = AutoComplete(self)
self.peek = Peek(self)
self.rename = Rename(self)
Expand Down
1 change: 1 addition & 0 deletions src/biscuit/layout/editors/editorsbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ def __init__(self, master: EditorsManager, *args, **kwargs) -> None:
self.buttons: list[IconButton] = []
self.default_buttons = (
(Icons.ELLIPSIS, self.menu.show),
(Icons.CHEVRON_DOWN, self.base.open_editors.show),
(Icons.ADD, self.base.commands.open_empty_editor),
)

Expand Down
10 changes: 5 additions & 5 deletions src/biscuit/layout/editors/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def add_editor(self, editor: Union[Editor, BaseEditor]) -> Editor | BaseEditor:
if editor.content:
editor.content.create_buttons(self.editorsbar.action_container)
self.editorsbar.add_tab(editor)
self.base.explorer.open_editors.add_item(editor)
self.base.open_editors.add_item(editor)
self.refresh()
return editor

Expand All @@ -119,7 +119,7 @@ def delete_all_editors(self) -> None:

self.editorsbar.clear_all_tabs()
self.active_editors.clear()
self.base.explorer.open_editors.clear()
self.base.open_editors.clear()
self.refresh()

def reopen_active_editor(self, *_) -> None:
Expand Down Expand Up @@ -211,7 +211,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.base.open_editors.remove_item(editor)

def close_editor_by_path(self, path: str) -> None:
"""Closes the editor with the given path.
Expand Down Expand Up @@ -252,7 +252,7 @@ def delete_editor(self, editor: Editor) -> None:
self.closed_editors.pop(editor.path)

editor.destroy()
self.base.explorer.open_editors.remove_item(editor)
self.base.open_editors.remove_item(editor)
self.refresh()

def set_active_editor(self, editor: Editor) -> Editor:
Expand All @@ -264,7 +264,7 @@ def set_active_editor(self, editor: Editor) -> Editor:
for tab in self.editorsbar.active_tabs:
if tab.editor == editor:
self.editorsbar.set_active_tab(tab)
self.base.explorer.open_editors.set_active(editor)
self.base.open_editors.set_active(editor)

return editor

Expand Down

0 comments on commit 404235f

Please sign in to comment.