summaryrefslogtreecommitdiff
path: root/Applications/TextEditor
diff options
context:
space:
mode:
authorAndreas Kling <awesomekling@gmail.com>2019-08-27 20:18:19 +0200
committerAndreas Kling <awesomekling@gmail.com>2019-08-27 20:18:19 +0200
commitecbedda34cb102c595bbe0a69979864d2ba092cc (patch)
tree1e507deba2ab99708ffefca4b869c14a5a891ff9 /Applications/TextEditor
parentfcf85d8feffa4f838446f674e27065dd828b1d26 (diff)
downloadserenity-ecbedda34cb102c595bbe0a69979864d2ba092cc.zip
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.
Diffstat (limited to 'Applications/TextEditor')
-rw-r--r--Applications/TextEditor/TextEditorWidget.cpp23
-rw-r--r--Applications/TextEditor/TextEditorWidget.h3
2 files changed, 24 insertions, 2 deletions
diff --git a/Applications/TextEditor/TextEditorWidget.cpp b/Applications/TextEditor/TextEditorWidget.cpp
index 2b3cd6d294..ffda29672e 100644
--- a/Applications/TextEditor/TextEditorWidget.cpp
+++ b/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());
}
diff --git a/Applications/TextEditor/TextEditorWidget.h b/Applications/TextEditor/TextEditorWidget.h
index 5ea4e77ae2..3b44a667c1 100644
--- a/Applications/TextEditor/TextEditorWidget.h
+++ b/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 };
};