瀏覽代碼

TextEditor: Add a "document dirty" flag and show it in the window title

This lets you know if the document has been modified since last save.
Andreas Kling 5 年之前
父節點
當前提交
ecbedda34c
共有 2 個文件被更改,包括 24 次插入2 次删除
  1. 21 2
      Applications/TextEditor/TextEditorWidget.cpp
  2. 3 0
      Applications/TextEditor/TextEditorWidget.h

+ 21 - 2
Applications/TextEditor/TextEditorWidget.cpp

@@ -25,6 +25,13 @@ TextEditorWidget::TextEditorWidget()
     m_editor->set_automatic_indentation_enabled(true);
     m_editor->set_line_wrapping_enabled(true);
 
+    m_editor->on_change = [this] {
+        bool was_dirty = m_document_dirty;
+        m_document_dirty = true;
+        if (!was_dirty)
+            update_title();
+    };
+
     m_find_widget = new GWidget(this);
     m_find_widget->set_fill_with_background_color(true);
     m_find_widget->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
@@ -131,14 +138,19 @@ TextEditorWidget::TextEditorWidget()
             return;
         }
 
+        m_document_dirty = false;
         set_path(FileSystemPath(save_path.value()));
         dbg() << "Wrote document to " << save_path.value();
     });
 
     m_save_action = GAction::create("Save", { Mod_Ctrl, Key_S }, GraphicsBitmap::load_from_file("/res/icons/16x16/save.png"), [&](const GAction&) {
         if (!m_path.is_empty()) {
-            if (!m_editor->write_to_file(m_path))
+            if (!m_editor->write_to_file(m_path)) {
                 GMessageBox::show("Unable to save file.\n", "Error", GMessageBox::Type::Error, GMessageBox::InputType::OK, window());
+            } else {
+                m_document_dirty = false;
+                update_title();
+            }
             return;
         }
 
@@ -229,9 +241,16 @@ void TextEditorWidget::set_path(const FileSystemPath& file)
     m_path = file.string();
     m_name = file.title();
     m_extension = file.extension();
+    update_title();
+}
+
+void TextEditorWidget::update_title()
+{
     StringBuilder builder;
     builder.append("Text Editor: ");
-    builder.append(file.string());
+    builder.append(m_path);
+    if (m_document_dirty)
+        builder.append(" (*)");
     window()->set_title(builder.to_string());
 }
 

+ 3 - 0
Applications/TextEditor/TextEditorWidget.h

@@ -19,6 +19,7 @@ public:
 
 private:
     void set_path(const FileSystemPath& file);
+    void update_title();
 
     GTextEditor* m_editor { nullptr };
     String m_path;
@@ -37,4 +38,6 @@ private:
     GButton* m_find_previous_button { nullptr };
     GButton* m_find_next_button { nullptr };
     GWidget* m_find_widget { nullptr };
+
+    bool m_document_dirty { false };
 };