Skip to content

Commit 472c567

Browse files
committed
Indicate dirty notebooks with * in tab
If a notebook is dirty (meaning that it has been changed since the last save), then add an asterix `*` to the file name in the title of the tab. This mirrors the behaviour of the editor.
1 parent 5f66f24 commit 472c567

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

spyder_notebook/widgets/client.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,16 @@ class NotebookClient(QFrame):
322322

323323
CONF_SECTION = CONF_SECTION
324324

325+
sig_dirty_changed = Signal(bool)
326+
"""
327+
This signal is emitted when the notebook becomes dirty or non-dirty.
328+
329+
Parameters
330+
----------
331+
new_value : bool
332+
Whether the notebook is now dirty.
333+
"""
334+
325335
def __init__(self, parent, filename, actions=None, ini_message=None):
326336
"""
327337
Constructor.
@@ -349,6 +359,7 @@ def __init__(self, parent, filename, actions=None, ini_message=None):
349359
self.file_url = None
350360
self.server_url = None
351361
self.path = None
362+
self.dirty = False
352363

353364
self.notebookwidget = NotebookWidget(self, actions)
354365
if ini_message:
@@ -358,6 +369,8 @@ def __init__(self, parent, filename, actions=None, ini_message=None):
358369
self.notebookwidget.show_blank()
359370
self.static = False
360371

372+
self.notebookwidget.sig_dirty_changed.connect(
373+
self._handle_dirty_changed)
361374
self.notebookwidget.sig_focus_in_event.connect(
362375
lambda: self._apply_stylesheet(focus=True))
363376
self.notebookwidget.sig_focus_out_event.connect(
@@ -527,6 +540,14 @@ def _apply_stylesheet(self, focus=False):
527540

528541
self.setStyleSheet(css.toString())
529542

543+
def _handle_dirty_changed(self, new_value: bool) -> None:
544+
"""
545+
Handle signal that a notebook became dirty or not.
546+
547+
Store the new value and emit the signal again.
548+
"""
549+
self.dirty = new_value
550+
self.sig_dirty_changed.emit(new_value)
530551

531552
# -----------------------------------------------------------------------------
532553
# Tests

spyder_notebook/widgets/notebooktabwidget.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ def create_new_client(self, filename=None):
207207

208208
client = NotebookClient(self, filename, self.actions)
209209
self.add_tab(client)
210+
client.sig_dirty_changed.connect(self.handle_dirty_changed)
210211
interpreter = self.get_interpreter()
211212
server_info = self.server_manager.get_server(
212213
filename, interpreter, start=True)
@@ -490,6 +491,27 @@ def add_tab(self, widget):
490491
self.setCurrentIndex(index)
491492
self.setTabToolTip(index, widget.get_filename())
492493

494+
def handle_dirty_changed(self, new_value: bool) -> None:
495+
"""
496+
Handle signal that a notebook became dirty or not.
497+
498+
Append a `*` to the filename of the notebook in the tab title if the
499+
notebook is dirty.
500+
501+
Parameters
502+
----------
503+
new_value : bool
504+
Whether the notebook is now dirty.
505+
"""
506+
notebook_client = self.sender()
507+
index = self.indexOf(notebook_client)
508+
if index == -1:
509+
logger.warn('handle_dirty_changed: Client not found!')
510+
else:
511+
suffix = '*' if new_value else ''
512+
self.setTabText(index, notebook_client.get_short_name() + suffix)
513+
self.setTabToolTip(index, notebook_client.get_filename() + suffix)
514+
493515
def handle_server_started(self, process):
494516
"""
495517
Handle signal that a notebook server has started.

0 commit comments

Comments
 (0)