Skip to content

Commit

Permalink
Add selection, view menus and callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
tomlin7 committed Oct 16, 2023
1 parent 8ca28b4 commit 9dae4f2
Show file tree
Hide file tree
Showing 6 changed files with 178 additions and 24 deletions.
56 changes: 47 additions & 9 deletions biscuit/core/components/editors/texteditor/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,14 @@ def config_bindings(self):

self.bind("<Control-f>", self.open_find_replace)
self.bind("<Control-d>", self.multi_selection)
self.bind("<Control-Left>", lambda e: self.handle_ctrl_hmovement())
self.bind("<Control-Right>", lambda e: self.handle_ctrl_hmovement(True))
self.bind("<Control-g>", lambda _: self.base.palette.show_prompt(':'))
self.bind("<Control-Left>", lambda _: self.handle_ctrl_hmovement())
self.bind("<Control-Right>", lambda _: self.handle_ctrl_hmovement(True))

self.bind("<Shift-Alt-Up>", self.copy_line_up)
self.bind("<Shift-Alt-Down>", self.copy_line_down)
self.bind("<Alt-Up>", self.move_line_up)
self.bind("<Alt-Down>", self.move_line_down)

self.bind("<Return>", self.enter_key_events)
self.bind("<Tab>", self.tab_key_events)
Expand All @@ -124,13 +130,6 @@ def config_bindings(self):
self.bind("<Up>", self.auto_completion.move_up)
self.bind("<Down>", self.auto_completion.move_down)

# self.bind("<braceleft>", lambda e: self.complete_pair("}"))
# self.bind("<bracketleft>", lambda e: self.complete_pair("]"))
# self.bind("<parenleft>", lambda e: self.complete_pair(")"))

# self.bind("<apostrophe>", lambda e: self.surrounding_selection("\'"))
# self.bind("<quotedbl>", lambda e: self.surrounding_selection("\""))

def key_release_events(self, event):
self._user_edit = True

Expand Down Expand Up @@ -484,6 +483,45 @@ def goto(self, line):
line = f"{line}.0"
self.move_cursor(line)
self.see(line)

def copy_line_up(self, *_) -> None:
"copies the line cursor is in below"
line = self.line
next_line = str(int(line) + 1)
self.insert(f"{next_line}.0", self.get(f"{line}.0", f"{line}.end"))
return "break"

def copy_line_down(self, *_) -> None:
"copies the line cursor is in above"
line = self.line
prev_line = str(int(line) - 1)
self.insert(f"{prev_line}.end", self.get(f"{line}.0", f"{line}.end"))
return "break"

def delete_line(self, *_) -> None:
"deletes the line cursor is in"
line = self.line
self.delete(f"{line}.0", f"{line}.end")

def move_line_up(self, *_) -> None:
"moves the line cursor is in below"
line = self.line
next_line = str(int(line) + 1)
self.insert(f"{next_line}.0", self.get(f"{line}.0", f"{line}.end"))
self.delete(f"{line}.0", f"{line}.end")
return "break"

def move_line_down(self, *_) -> None:
"moves the line cursor is in above"
line = self.line
prev_line = str(int(line) - 1)
self.insert(f"{prev_line}.end", self.get(f"{line}.0", f"{line}.end"))
self.delete(f"{line}.0", f"{line}.end")
return "break"

def duplicate_selection(self, *_) -> None:
"duplicates the current selection"
self.insert(tk.INSERT, self.get_selected_text())

def write(self, text, *args):
self.insert(tk.END, text, *args)
Expand Down
6 changes: 5 additions & 1 deletion biscuit/core/components/utils/text.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import tkinter as tk
import typing

if typing.TYPE_CHECKING:
from biscuit.core import App


class Text(tk.Text):
Expand All @@ -8,4 +12,4 @@ class Text(tk.Text):
def __init__(self, master, *args, **kwargs) -> None:
super().__init__(master, *args, **kwargs)
self.master = master
self.base = master.base
self.base: App = master.base
10 changes: 10 additions & 0 deletions biscuit/core/layout/base/content/panel/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,16 @@ def logger(self) -> Logs:
@property
def terminal(self) -> Terminal:
return self.default_views[1]

def show_terminal(self) -> None:
"shows the terminal if its hidden/minimized"
self.set_active_view(self.terminal)
self.show_panel()

def show_logs(self) -> None:
"shows the logs if its hidden/minimized"
self.set_active_view(self.logger)
self.show_panel()

def toggle_panel(self) -> None:
"toggles the current visible state of panel"
Expand Down
20 changes: 20 additions & 0 deletions biscuit/core/layout/base/sidebar/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,23 @@ def source_control(self) -> SourceControl:
def extensions(self) -> Extensions:
"Get source control view."
return self.default_views[3]

def show_view(self, view: SidebarView) -> None:
"Show a view."
self.slots.set_active_slot(view)

def show_explorer(self) -> None:
"Show explorer view."
self.show_view(self.default_views[0])

def show_search(self) -> None:
"Show search view."
self.show_view(self.default_views[1])

def show_source_control(self) -> None:
"Show source control view."
self.show_view(self.default_views[2])

def show_extensions(self) -> None:
"Show extensions view."
self.show_view(self.default_views[3])
42 changes: 28 additions & 14 deletions biscuit/core/layout/menubar/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ def set_title(self, title: str=None) -> None:
self.reposition_title()

def reposition_title(self) -> None:
"Reposition the title label to the center of the menubar"
x = self.winfo_x() + (self.winfo_width() - self.title_lbl.winfo_width())/2
y = self.winfo_y() + (self.winfo_height() - self.title_lbl.winfo_height())/2

Expand Down Expand Up @@ -88,14 +89,12 @@ def add_menu(self, text: str) -> Menu:
return new_menu.menu

def add_menus(self) -> None:
#TODO menu shall support check items
self.add_file_menu()
self.add_edit_menu()
self.add_selection_menu()
self.add_view_menu

#TODO implement View menu events
#TODO menu shall support check items
#self.add_view_menu()

# TODO: Implement events for the menu items
def add_file_menu(self) -> None:
events = self.events

Expand Down Expand Up @@ -128,21 +127,36 @@ def add_edit_menu(self) -> None:
edit_menu.add_item("Copy", events.copy)
edit_menu.add_item("Paste", events.paste)
edit_menu.add_separator()
edit_menu.add_item("Find",)
edit_menu.add_item("Replace",)
edit_menu.add_item("Find", events.find)
edit_menu.add_item("Replace", events.replace)

def add_selection_menu(self) -> None:
events = self.events

selection_menu = self.add_menu("Selection")
selection_menu.add_item("Select All", events.select_all)
selection_menu.add_item("Select Line", events.select_line)
selection_menu.add_item("Delete Line", events.delete_line)
selection_menu.add_separator()
selection_menu.add_item("Copy Line Up", events.copy_line_up)
selection_menu.add_item("Copy Line Down", events.copy_line_down)
selection_menu.add_item("Move Line Up", events.move_line_up)
selection_menu.add_item("Move Line Down", events.move_line_down)
selection_menu.add_item("Duplicate Selection", events.duplicate_selection)


def add_view_menu(self) -> None:
events = self.events

view_menu = self.add_menu("View")
view_menu.add_item("Side Bar",)
view_menu.add_item("Console",)
view_menu.add_item("Status Bar",)
view_menu.add_item("Menu",)
view_menu.add_item("Command Palette...", lambda: self.base.palette.show_prompt(">"))
view_menu.add_item("Explorer", events.show_explorer)
view_menu.add_item("Search", events.show_search)
view_menu.add_item("Source Control", events.show_source_control)
view_menu.add_item("Extensions", events.show_extensions)
view_menu.add_separator()
view_menu.add_item("Syntax",)
view_menu.add_item("Indentation",)
view_menu.add_item("Line Endings",)
view_menu.add_item("Terminal", events.show_terminal)
view_menu.add_item("Log", events.show_logs)

def close_all_menus(self, *_) -> None:
for menu in self.menus:
Expand Down
68 changes: 68 additions & 0 deletions biscuit/core/utils/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from ... import App

import platform
import tkinter as tk
import tkinter.filedialog as filedialog
from tkinter.filedialog import asksaveasfilename

Expand Down Expand Up @@ -132,3 +133,70 @@ def paste(self, *_) -> None:
if editor := self.base.editorsmanager.active_editor:
if editor.content and editor.content.editable:
editor.content.paste()
def find(self, *_) -> None:
if editor := self.base.editorsmanager.active_editor:
if editor.content and editor.content.editable:
editor.content.text.open_find_replace()

def replace(self, *_) -> None:
if editor := self.base.editorsmanager.active_editor:
if editor.content and editor.content.editable:
editor.content.text.open_find_replace()

def select_all(self, *_) -> None:
if editor := self.base.editorsmanager.active_editor:
if editor.content and editor.content.editable:
editor.content.tag_add("sel", "1.0", "end")

def select_line(self, *_) -> None:
if editor := self.base.editorsmanager.active_editor:
if editor.content and editor.content.editable:
editor.content.text.select_line(editor.content.text.index(tk.INSERT))

def delete_line(self, *_) -> None:
if editor := self.base.editorsmanager.active_editor:
if editor.content and editor.content.editable:
editor.content.text.delete_line()

def copy_line_up(self, *_) -> None:
if editor := self.base.editorsmanager.active_editor:
if editor.content and editor.content.editable:
editor.content.text.copy_line_up()

def copy_line_down(self, *_) -> None:
if editor := self.base.editorsmanager.active_editor:
if editor.content and editor.content.editable:
editor.content.text.copy_line_down()

def move_line_up(self, *_) -> None:
if editor := self.base.editorsmanager.active_editor:
if editor.content and editor.content.editable:
editor.content.text.move_line_up()

def move_line_down(self, *_) -> None:
if editor := self.base.editorsmanager.active_editor:
if editor.content and editor.content.editable:
editor.content.text.move_line_down()

def duplicate_selection(self, *_) -> None:
if editor := self.base.editorsmanager.active_editor:
if editor.content and editor.content.editable:
editor.content.text.duplicate_selection()

def show_explorer(self, *_) -> None:
self.base.sidebar.show_explorer()

def show_search(self, *_) -> None:
self.base.sidebar.show_search()

def show_source_control(self, *_) -> None:
self.base.sidebar.show_source_control()

def show_extensions(self, *_) -> None:
self.base.sidebar.show_extensions()

def show_terminal(self, *_) -> None:
self.base.panel.show_terminal()

def show_logs(self, *_) -> None:
self.base.panel.show_logs()

0 comments on commit 9dae4f2

Please sign in to comment.