summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrhin123 <ryanrhin@gmail.com>2019-07-28 23:45:50 -0500
committerAndreas Kling <awesomekling@gmail.com>2019-07-29 20:46:31 +0200
commita175e76948679a241745302d4b10637eecd8f275 (patch)
tree5b55bc8225248a5937f929fa8165b17c057c0d82
parent80cb833594587750b78e449de461d84961ed7b13 (diff)
downloadserenity-a175e76948679a241745302d4b10637eecd8f275.zip
TextEditor: Include extension during SaveAs
When we save-as in the text editor we now auto-populate GFilePicker /w the current name & extension.
-rw-r--r--AK/FileSystemPath.cpp5
-rw-r--r--AK/FileSystemPath.h4
-rw-r--r--Applications/TextEditor/TextEditorWidget.cpp26
-rw-r--r--Applications/TextEditor/TextEditorWidget.h5
-rw-r--r--Libraries/LibGUI/GFilePicker.cpp10
-rw-r--r--Libraries/LibGUI/GFilePicker.h4
6 files changed, 34 insertions, 20 deletions
diff --git a/AK/FileSystemPath.cpp b/AK/FileSystemPath.cpp
index 51a72302ad..72c7d1ff0b 100644
--- a/AK/FileSystemPath.cpp
+++ b/AK/FileSystemPath.cpp
@@ -37,6 +37,11 @@ void FileSystemPath::canonicalize()
}
m_basename = canonical_parts.last();
+ auto name_parts = m_basename.split('.');
+ m_title = name_parts[0];
+ if (name_parts.size() > 1)
+ m_extension = name_parts[1];
+
StringBuilder builder(approximate_canonical_length);
for (auto& cpart : canonical_parts) {
builder.append('/');
diff --git a/AK/FileSystemPath.h b/AK/FileSystemPath.h
index 4e6952d52e..e93622aa43 100644
--- a/AK/FileSystemPath.h
+++ b/AK/FileSystemPath.h
@@ -13,6 +13,8 @@ public:
const String& string() const { return m_string; }
const String& basename() const { return m_basename; }
+ const String& title() const { return m_title; }
+ const String& extension() const { return m_extension; }
const Vector<String>& parts() const { return m_parts; }
@@ -24,6 +26,8 @@ private:
Vector<String> m_parts;
String m_string;
String m_basename;
+ String m_title;
+ String m_extension;
bool m_is_valid { false };
};
diff --git a/Applications/TextEditor/TextEditorWidget.cpp b/Applications/TextEditor/TextEditorWidget.cpp
index 91f25f6384..537959135b 100644
--- a/Applications/TextEditor/TextEditorWidget.cpp
+++ b/Applications/TextEditor/TextEditorWidget.cpp
@@ -34,26 +34,26 @@ TextEditorWidget::TextEditorWidget()
});
m_open_action = GAction::create("Open...", { Mod_Ctrl, Key_O }, GraphicsBitmap::load_from_file("/res/icons/16x16/open.png"), [this](const GAction&) {
- Optional<String> open_name = GFilePicker::get_open_filepath();
+ Optional<String> open_path = GFilePicker::get_open_filepath();
- if (!open_name.has_value())
+ if (!open_path.has_value())
return;
- open_sesame(open_name.value());
+ open_sesame(open_path.value());
});
m_save_as_action = GAction::create("Save as...", { Mod_None, Key_F12 }, GraphicsBitmap::load_from_file("/res/icons/16x16/save.png"), [this](const GAction&) {
- Optional<String> save_name = GFilePicker::get_save_filepath();
- if (!save_name.has_value())
+ Optional<String> save_path = GFilePicker::get_save_filepath(m_name.is_null() ? "Untitled" : m_name, m_extension.is_null() ? "txt" : m_extension);
+ if (!save_path.has_value())
return;
- if (!m_editor->write_to_file(save_name.value())) {
+ if (!m_editor->write_to_file(save_path.value())) {
GMessageBox::show("Unable to save file.\n", "Error", GMessageBox::Type::Error, GMessageBox::InputType::OK, window());
return;
}
- set_path(save_name.value());
- dbg() << "Wrote document to " << save_name.value();
+ 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&) {
@@ -131,12 +131,14 @@ TextEditorWidget::~TextEditorWidget()
{
}
-void TextEditorWidget::set_path(const StringView& path)
+void TextEditorWidget::set_path(const FileSystemPath& file)
{
- m_path = path;
+ m_path = file.string();
+ m_name = file.title();
+ m_extension = file.extension();
StringBuilder builder;
builder.append("Text Editor: ");
- builder.append(path);
+ builder.append(file.string());
window()->set_title(builder.to_string());
}
@@ -150,5 +152,5 @@ void TextEditorWidget::open_sesame(const String& path)
}
m_editor->set_text(String::copy(file.read_all()));
- set_path(path);
+ set_path(FileSystemPath(path));
}
diff --git a/Applications/TextEditor/TextEditorWidget.h b/Applications/TextEditor/TextEditorWidget.h
index 7515a984bb..7b4a1f5b2e 100644
--- a/Applications/TextEditor/TextEditorWidget.h
+++ b/Applications/TextEditor/TextEditorWidget.h
@@ -1,5 +1,6 @@
#pragma once
+#include <AK/FileSystemPath.h>
#include <AK/Function.h>
#include <LibGUI/GApplication.h>
#include <LibGUI/GTextEditor.h>
@@ -15,10 +16,12 @@ public:
void open_sesame(const String& path);
private:
- void set_path(const StringView&);
+ void set_path(const FileSystemPath& file);
GTextEditor* m_editor { nullptr };
String m_path;
+ String m_name;
+ String m_extension;
RefPtr<GAction> m_new_action;
RefPtr<GAction> m_open_action;
RefPtr<GAction> m_save_action;
diff --git a/Libraries/LibGUI/GFilePicker.cpp b/Libraries/LibGUI/GFilePicker.cpp
index 323b357106..1e9a44fea7 100644
--- a/Libraries/LibGUI/GFilePicker.cpp
+++ b/Libraries/LibGUI/GFilePicker.cpp
@@ -1,5 +1,6 @@
#include <AK/FileSystemPath.h>
#include <AK/Function.h>
+#include <LibDraw/PNGLoader.h>
#include <LibGUI/GAction.h>
#include <LibGUI/GBoxLayout.h>
#include <LibGUI/GButton.h>
@@ -11,7 +12,6 @@
#include <LibGUI/GSortingProxyModel.h>
#include <LibGUI/GTextBox.h>
#include <LibGUI/GToolBar.h>
-#include <LibDraw/PNGLoader.h>
Optional<String> GFilePicker::get_open_filepath()
{
@@ -28,9 +28,9 @@ Optional<String> GFilePicker::get_open_filepath()
return {};
}
-Optional<String> GFilePicker::get_save_filepath()
+Optional<String> GFilePicker::get_save_filepath(const String& title, const String& extension)
{
- GFilePicker picker(Mode::Save);
+ GFilePicker picker(Mode::Save, String::format("%s.%s", title.characters(), extension.characters()));
if (picker.exec() == GDialog::ExecOK) {
String file_path = picker.selected_file().string();
@@ -43,7 +43,7 @@ Optional<String> GFilePicker::get_save_filepath()
return {};
}
-GFilePicker::GFilePicker(Mode mode, const StringView& path, CObject* parent)
+GFilePicker::GFilePicker(Mode mode, const StringView& file_name, const StringView& path, CObject* parent)
: GDialog(parent)
, m_model(GDirectoryModel::create())
, m_mode(mode)
@@ -134,7 +134,7 @@ GFilePicker::GFilePicker(Mode mode, const StringView& path, CObject* parent)
filename_label->set_preferred_size(60, 0);
auto* filename_textbox = new GTextBox(filename_container);
if (m_mode == Mode::Save) {
- filename_textbox->set_text("Untitled.txt"); //TODO: replace .txt with a preferred extension
+ filename_textbox->set_text(file_name);
filename_textbox->set_focus(true);
filename_textbox->select_all();
}
diff --git a/Libraries/LibGUI/GFilePicker.h b/Libraries/LibGUI/GFilePicker.h
index 55d394c895..1316ccd290 100644
--- a/Libraries/LibGUI/GFilePicker.h
+++ b/Libraries/LibGUI/GFilePicker.h
@@ -16,10 +16,10 @@ public:
};
static Optional<String> get_open_filepath();
- static Optional<String> get_save_filepath();
+ static Optional<String> get_save_filepath(const String& title, const String& extension);
static bool file_exists(const StringView& path);
- GFilePicker(Mode type = Mode::Open, const StringView& path = String(get_current_user_home_path()), CObject* parent = nullptr);
+ GFilePicker(Mode type = Mode::Open, const StringView& file_name = "Untitled", const StringView& path = String(get_current_user_home_path()), CObject* parent = nullptr);
virtual ~GFilePicker() override;
FileSystemPath selected_file() const { return m_selected_file; }