mirror of
https://github.com/RGBCube/serenity
synced 2025-05-22 19:45:08 +00:00
GFilePicker: Edit file name on opening window
Overlooked that you can't have two GWidgets selected at the same time, whoops!
This commit is contained in:
parent
973c2d9bfd
commit
c869d62e16
2 changed files with 28 additions and 19 deletions
|
@ -132,14 +132,17 @@ GFilePicker::GFilePicker(Mode mode, const StringView& file_name, const StringVie
|
||||||
filename_label->set_text_alignment(TextAlignment::CenterLeft);
|
filename_label->set_text_alignment(TextAlignment::CenterLeft);
|
||||||
filename_label->set_size_policy(SizePolicy::Fixed, SizePolicy::Fill);
|
filename_label->set_size_policy(SizePolicy::Fixed, SizePolicy::Fill);
|
||||||
filename_label->set_preferred_size(60, 0);
|
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) {
|
if (m_mode == Mode::Save) {
|
||||||
filename_textbox->set_text(file_name);
|
m_filename_textbox->set_text(file_name);
|
||||||
filename_textbox->set_focus(true);
|
m_filename_textbox->set_focus(true);
|
||||||
filename_textbox->select_all();
|
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& filter_model = (GSortingProxyModel&)*m_view->model();
|
||||||
auto local_index = filter_model.map_to_target(index);
|
auto local_index = filter_model.map_to_target(index);
|
||||||
const GDirectoryModel::Entry& entry = m_model->entry(local_index.row());
|
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());
|
m_model->open(path.string());
|
||||||
// NOTE: 'entry' is invalid from here on
|
// NOTE: 'entry' is invalid from here on
|
||||||
} else {
|
} else {
|
||||||
filename_textbox->set_text(entry.name);
|
m_filename_textbox->set_text(entry.name);
|
||||||
set_preview(path);
|
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_size_policy(SizePolicy::Fixed, SizePolicy::Fill);
|
||||||
ok_button->set_preferred_size(80, 0);
|
ok_button->set_preferred_size(80, 0);
|
||||||
ok_button->set_text(ok_button_name(m_mode));
|
ok_button->set_text(ok_button_name(m_mode));
|
||||||
if (m_mode == Mode::Save)
|
ok_button->on_click = [this](auto&) {
|
||||||
ok_button->set_focus(true);
|
on_file_return();
|
||||||
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);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
auto* preview_container = new GFrame(horizontal_container);
|
auto* preview_container = new GFrame(horizontal_container);
|
||||||
|
@ -241,6 +233,20 @@ void GFilePicker::clear_preview()
|
||||||
m_preview_geometry_label->set_text(String::empty());
|
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)
|
bool GFilePicker::file_exists(const StringView& path)
|
||||||
{
|
{
|
||||||
struct stat st;
|
struct stat st;
|
||||||
|
|
|
@ -6,6 +6,7 @@
|
||||||
|
|
||||||
class GDirectoryModel;
|
class GDirectoryModel;
|
||||||
class GLabel;
|
class GLabel;
|
||||||
|
class GTextBox;
|
||||||
|
|
||||||
class GFilePicker final : public GDialog {
|
class GFilePicker final : public GDialog {
|
||||||
C_OBJECT(GFilePicker)
|
C_OBJECT(GFilePicker)
|
||||||
|
@ -27,6 +28,7 @@ public:
|
||||||
private:
|
private:
|
||||||
void set_preview(const FileSystemPath&);
|
void set_preview(const FileSystemPath&);
|
||||||
void clear_preview();
|
void clear_preview();
|
||||||
|
void on_file_return();
|
||||||
|
|
||||||
static String ok_button_name(Mode mode)
|
static String ok_button_name(Mode mode)
|
||||||
{
|
{
|
||||||
|
@ -44,6 +46,7 @@ private:
|
||||||
NonnullRefPtr<GDirectoryModel> m_model;
|
NonnullRefPtr<GDirectoryModel> m_model;
|
||||||
FileSystemPath m_selected_file;
|
FileSystemPath m_selected_file;
|
||||||
|
|
||||||
|
GTextBox* m_filename_textbox { nullptr };
|
||||||
GLabel* m_preview_image_label { nullptr };
|
GLabel* m_preview_image_label { nullptr };
|
||||||
GLabel* m_preview_name_label { nullptr };
|
GLabel* m_preview_name_label { nullptr };
|
||||||
GLabel* m_preview_geometry_label { nullptr };
|
GLabel* m_preview_geometry_label { nullptr };
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue