warn user when closing tabs with unsaved changes

This commit is contained in:
2026-07-28 16:26:04 +10:00
parent 9e0f81b7a0
commit 575fb31cd5
2 changed files with 20 additions and 2 deletions

View File

@@ -9,6 +9,7 @@ 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 from widgets.context_menu import ContextMenu
from widgets.prompt import Prompt
import os, pathlib import os, pathlib
@@ -99,9 +100,26 @@ class FileTabs(TabbedContent):
return False return False
return True return True
async def close_tab(self, tab: FileTab):
if tab.saved:
self.remove_pane(tab.id)
return
def callback(should_close: bool):
if should_close:
self.remove_pane(tab.id)
await self.app.push_screen(Prompt(
f"{tab.file_name} has unsaved changes, are you sure you want to close it?",
prompt_type="confirm"
), callback=callback)
async def close_open_tab(self): async def close_open_tab(self):
if self.active_pane: if self.active_pane:
self.remove_pane(self.active) if not isinstance(self.active_pane, FileTab):
self.remove_pane(self.active)
else:
await self.close_tab(self.active_pane)
async def new_file(self): async def new_file(self):
new_tab = FileTab(None) new_tab = FileTab(None)

View File

@@ -61,7 +61,7 @@ class Prompt(ModalScreen):
elif event.button.id == "confirm-list": elif event.button.id == "confirm-list":
self.dismiss(self.query_one("#select").value) self.dismiss(self.query_one("#select").value)
def __init__(self, question: str, prompt_type: Literal["confirm", "string"], title: str = "Confirm", **kwargs): def __init__(self, question: str, prompt_type: Literal["confirm", "string", "list"], title: str = "Confirm", **kwargs):
super().__init__() super().__init__()
self.question = question self.question = question
self.window_title = title self.window_title = title