add closing tabs

This commit is contained in:
2026-07-28 16:20:19 +10:00
parent 66ce2fed82
commit 9e0f81b7a0
2 changed files with 25 additions and 4 deletions

View File

@@ -29,7 +29,8 @@ class Berry(App):
"save-as": "ctrl+shift+s", "save-as": "ctrl+shift+s",
"settings": "ctrl+f1", "settings": "ctrl+f1",
"home": "ctrl+h", "home": "ctrl+h",
"find": "ctrl+f" "find": "ctrl+f",
"close-tab": "ctrl+w"
} }
@@ -44,9 +45,10 @@ class Berry(App):
Binding(self.bind_keys['open'], "open_file", "Open file"), Binding(self.bind_keys['open'], "open_file", "Open file"),
Binding(self.bind_keys['open-folder'], "open_folder", "Open directory"), Binding(self.bind_keys['open-folder'], "open_folder", "Open directory"),
Binding(self.bind_keys['save'], "save", "Save"), Binding(self.bind_keys['save'], "save", "Save"),
Binding(self.bind_keys['save-as'], "save_as", "Save as...", priority=True), Binding(self.bind_keys['save-as'], "save_as", "Save as..."),
Binding(self.bind_keys['home'], "home", "Home"), Binding(self.bind_keys['home'], "home", "Home"),
Binding(self.bind_keys['find'], "find", "Find and replace") Binding(self.bind_keys['find'], "find", "Find and replace"),
Binding(self.bind_keys['close-tab'], "close_tab", "Close tab", priority=True),
) )
for bind in self.BINDINGS: for bind in self.BINDINGS:
@@ -64,6 +66,12 @@ class Berry(App):
yield Footer() yield Footer()
async def action_home(self):
await self.file_tabs.open_home()
async def action_close_tab(self):
await self.file_tabs.close_open_tab()
async def action_save(self): async def action_save(self):
await self.file_tabs.save() await self.file_tabs.save()

View File

@@ -2,11 +2,13 @@ from textual.widgets import TabbedContent, Tab, TabPane
from textual.widget import Widget from textual.widget import Widget
from textual.widgets._tabbed_content import ContentTabs from textual.widgets._tabbed_content import ContentTabs
from textual.reactive import reactive from textual.reactive import reactive
from textual.events import MouseDown
from textual_fspicker import FileOpen, FileSave, SelectDirectory from textual_fspicker import FileOpen, FileSave, SelectDirectory
from widgets.text_editor import TextEditor from widgets.text_editor import TextEditor
from widgets.home_page import HomePage from widgets.home_page import HomePage
from widgets.context_menu import ContextMenu
import os, pathlib import os, pathlib
@@ -33,6 +35,7 @@ class FileTab(TabPane):
self.path = path self.path = path
self.last_saved_value = True self.last_saved_value = True
def watch_saved(self, saved_value: bool): def watch_saved(self, saved_value: bool):
if self.last_saved_value != saved_value: if self.last_saved_value != saved_value:
self.changed = True self.changed = True
@@ -96,6 +99,10 @@ class FileTabs(TabbedContent):
return False return False
return True return True
async def close_open_tab(self):
if self.active_pane:
self.remove_pane(self.active)
async def new_file(self): async def new_file(self):
new_tab = FileTab(None) new_tab = FileTab(None)
@@ -163,7 +170,11 @@ class FileTabs(TabbedContent):
await self.app.push_screen(FileOpen(), callback=callback) await self.app.push_screen(FileOpen(), callback=callback)
async def open_home(self) -> None: async def open_home(self) -> None:
await self.add_pane(HomeTab()) new_home_tab = HomeTab()
await self.add_pane(new_home_tab)
self.active = new_home_tab.id
async def open_file(self, path: str) -> None: async def open_file(self, path: str) -> None:
opened_new_tab = await self.clean_up_tabs(path) opened_new_tab = await self.clean_up_tabs(path)
@@ -193,3 +204,5 @@ class FileTabs(TabbedContent):
self.active = new_pane.id self.active = new_pane.id
text_editor.focus()