Skip to content

Commit

Permalink
Add context menu to the editor tab bar.
Browse files Browse the repository at this point in the history
  • Loading branch information
Bzero committed Jul 6, 2024
1 parent 50f5ce1 commit 5da57ce
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 1 deletion.
63 changes: 63 additions & 0 deletions typstwriter/editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,24 @@ def __init__(self):
self.TabWidget.currentChanged.connect(self.tab_changed)
state.working_directory.Signal.connect(self.update_tab_names)

self.TabWidget.tabBar().setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
self.TabWidget.tabBar().customContextMenuRequested.connect(self.tab_bar_rightclicked)

self.recentFiles = util.RecentFiles()

self.welcome()

def tab_bar_rightclicked(self, event):
"""Handle right click on the tab bar."""
tab_index = self.TabWidget.tabBar().tabAt(event)
tab = self.TabWidget.widget(tab_index)
if isinstance(tab, EditorPage):
cm = EditorPageBarContextMenu(self, tab_index)
cm.popup(QtGui.QCursor.pos())
elif isinstance(tab, WelcomePage):
cm = WelcomePageBarContextMenu(self, tab_index)
cm.popup(QtGui.QCursor.pos())

def new_file(self):
"""Open a new, empty file."""
editorpage = EditorPage()
Expand Down Expand Up @@ -212,6 +226,55 @@ def tab_changed(self, i):
self.active_file_changed.emit(page.path)


class EditorPageBarContextMenu(QtWidgets.QMenu):
"""ContextMenu for EditorPage tabbar."""

def __init__(self, parent, tab_index):
"""Declare all actions."""
QtWidgets.QMenu.__init__(self, parent)

self.tab_index = tab_index

self.action_close = QtWidgets.QAction("Close file", triggered=self.handle_close)
self.action_use_as_main = QtWidgets.QAction("Use as main file", triggered=self.handle_use_as_main)
self.action_use_as_working_directory = QtWidgets.QAction(
"Use containing folder as working directory", triggered=self.handle_use_as_working_directory
)

self.addAction(self.action_close)
self.addAction(self.action_use_as_main)
self.addAction(self.action_use_as_working_directory)

def handle_close(self):
"""Trigger closing the tab."""
self.parent().close_tab(self.tab_index)

def handle_use_as_main(self):
"""Trigger using this file as main file."""
state.main_file.Value = self.parent().TabWidget.widget(self.tab_index).path

def handle_use_as_working_directory(self):
"""Trigger using the folder containing this file as working directory."""
state.working_directory.Value = os.path.dirname(self.parent().TabWidget.widget(self.tab_index).path)


class WelcomePageBarContextMenu(QtWidgets.QMenu):
"""ContextMenu for WelcomePage tabbar."""

def __init__(self, parent, tab_index):
"""Declare all actions."""
QtWidgets.QMenu.__init__(self, parent)

self.tab_index = tab_index

self.action_close = QtWidgets.QAction("Close Tab", triggered=self.handle_close)
self.addAction(self.action_close)

def handle_close(self):
"""Trigger closing the tab."""
self.parent().close_tab(self.tab_index)


class EditorPage(QtWidgets.QFrame):
"""A single editor page."""

Expand Down
7 changes: 6 additions & 1 deletion typstwriter/fs_explorer.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,12 +188,17 @@ def open_directory_dialog(self):

def set_root(self, root):
"""Set the root directory of the FSExplorer."""
state.working_directory.Value = root
self.root_changed(root)

@QtCore.Slot(str)
def root_changed(self, root):
"""Update root directory."""
self.root = root
self.filesystem_model.setRootPath(root)
self.tree_view.setRootIndex(self.filesystem_model.index(root))
self.completer_filesystem_model.setRootPath(root)
self.pathBar.setText(root)
state.working_directory.Value = root
logger.debug("Change Working Directory to {!r}.", root)
# self.directoryChanged.emit(root)

Expand Down
1 change: 1 addition & 0 deletions typstwriter/mainwindow.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ def __init__(self):
self.actions.open_config.triggered.connect(self.open_config)

self.FSExplorer.open_file.connect(self.editor.open_file)
state.working_directory.Signal.connect(self.FSExplorer.root_changed)
self.editor.text_changed.connect(self.CompilerConnector.source_changed)
self.CompilerConnector.document_changed.connect(self.PDFWidget.reload)
state.main_file.Signal.connect(lambda s: self.CompilerConnector.stop())
Expand Down

0 comments on commit 5da57ce

Please # to comment.