TextEditor: Include extension during SaveAs

When we save-as in the text editor we now auto-populate GFilePicker /w
the current name & extension.
This commit is contained in:
rhin123 2019-07-28 23:45:50 -05:00 committed by Andreas Kling
parent 80cb833594
commit a175e76948
6 changed files with 34 additions and 20 deletions

View file

@ -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('/');

View file

@ -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 };
};

View file

@ -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));
}

View file

@ -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;

View file

@ -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();
}

View file

@ -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; }