diff options
author | rhin123 <ryanrhin@gmail.com> | 2019-07-29 15:50:06 -0500 |
---|---|---|
committer | Andreas Kling <awesomekling@gmail.com> | 2019-07-30 09:19:14 +0200 |
commit | c869d62e168aff3d37f55219c8fa41ecd5625c74 (patch) | |
tree | b1a94f51c76525be24612ce873fbc5d60f5ecd25 | |
parent | 973c2d9bfdb16343d2456e71496ade3f647ab936 (diff) | |
download | serenity-c869d62e168aff3d37f55219c8fa41ecd5625c74.zip |
GFilePicker: Edit file name on opening window
Overlooked that you can't have two GWidgets
selected at the same time, whoops!
-rw-r--r-- | Libraries/LibGUI/GFilePicker.cpp | 44 | ||||
-rw-r--r-- | Libraries/LibGUI/GFilePicker.h | 3 |
2 files changed, 28 insertions, 19 deletions
diff --git a/Libraries/LibGUI/GFilePicker.cpp b/Libraries/LibGUI/GFilePicker.cpp index 1e9a44fea7..5b5cf864b0 100644 --- a/Libraries/LibGUI/GFilePicker.cpp +++ b/Libraries/LibGUI/GFilePicker.cpp @@ -132,14 +132,17 @@ GFilePicker::GFilePicker(Mode mode, const StringView& file_name, const StringVie filename_label->set_text_alignment(TextAlignment::CenterLeft); filename_label->set_size_policy(SizePolicy::Fixed, SizePolicy::Fill); filename_label->set_preferred_size(60, 0); - auto* filename_textbox = new GTextBox(filename_container); + m_filename_textbox = new GTextBox(filename_container); if (m_mode == Mode::Save) { - filename_textbox->set_text(file_name); - filename_textbox->set_focus(true); - filename_textbox->select_all(); + m_filename_textbox->set_text(file_name); + m_filename_textbox->set_focus(true); + m_filename_textbox->select_all(); } + m_filename_textbox->on_return_pressed = [&] { + on_file_return(); + }; - m_view->on_activation = [this, filename_textbox](auto& index) { + m_view->on_activation = [this](auto& index) { auto& filter_model = (GSortingProxyModel&)*m_view->model(); auto local_index = filter_model.map_to_target(index); const GDirectoryModel::Entry& entry = m_model->entry(local_index.row()); @@ -151,7 +154,7 @@ GFilePicker::GFilePicker(Mode mode, const StringView& file_name, const StringVie m_model->open(path.string()); // NOTE: 'entry' is invalid from here on } else { - filename_textbox->set_text(entry.name); + m_filename_textbox->set_text(entry.name); set_preview(path); } }; @@ -175,19 +178,8 @@ GFilePicker::GFilePicker(Mode mode, const StringView& file_name, const StringVie ok_button->set_size_policy(SizePolicy::Fixed, SizePolicy::Fill); ok_button->set_preferred_size(80, 0); ok_button->set_text(ok_button_name(m_mode)); - if (m_mode == Mode::Save) - ok_button->set_focus(true); - ok_button->on_click = [this, filename_textbox](auto&) { - FileSystemPath path(String::format("%s/%s", m_model->path().characters(), filename_textbox->text().characters())); - - if (GFilePicker::file_exists(path.string()) && m_mode == Mode::Save) { - GMessageBox box("File already exists, overwrite?", "Existing File", GMessageBox::Type::Warning, GMessageBox::InputType::OKCancel); - if (box.exec() == GMessageBox::ExecCancel) - return; - } - - m_selected_file = path; - done(ExecOK); + ok_button->on_click = [this](auto&) { + on_file_return(); }; auto* preview_container = new GFrame(horizontal_container); @@ -241,6 +233,20 @@ void GFilePicker::clear_preview() m_preview_geometry_label->set_text(String::empty()); } +void GFilePicker::on_file_return() +{ + FileSystemPath path(String::format("%s/%s", m_model->path().characters(), m_filename_textbox->text().characters())); + + if (GFilePicker::file_exists(path.string()) && m_mode == Mode::Save) { + GMessageBox box("File already exists, overwrite?", "Existing File", GMessageBox::Type::Warning, GMessageBox::InputType::OKCancel); + if (box.exec() == GMessageBox::ExecCancel) + return; + } + + m_selected_file = path; + done(ExecOK); +} + bool GFilePicker::file_exists(const StringView& path) { struct stat st; diff --git a/Libraries/LibGUI/GFilePicker.h b/Libraries/LibGUI/GFilePicker.h index 1316ccd290..49546c4f61 100644 --- a/Libraries/LibGUI/GFilePicker.h +++ b/Libraries/LibGUI/GFilePicker.h @@ -6,6 +6,7 @@ class GDirectoryModel; class GLabel; +class GTextBox; class GFilePicker final : public GDialog { C_OBJECT(GFilePicker) @@ -27,6 +28,7 @@ public: private: void set_preview(const FileSystemPath&); void clear_preview(); + void on_file_return(); static String ok_button_name(Mode mode) { @@ -44,6 +46,7 @@ private: NonnullRefPtr<GDirectoryModel> m_model; FileSystemPath m_selected_file; + GTextBox* m_filename_textbox { nullptr }; GLabel* m_preview_image_label { nullptr }; GLabel* m_preview_name_label { nullptr }; GLabel* m_preview_geometry_label { nullptr }; |