mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 06:47:35 +00:00
FontEditor: Warn on unsaved changes
Standardizes saving conventions: Editor now warns on close, new, and open if there are unsaved changes, and new files prompt to Save As.
This commit is contained in:
parent
67f81adc55
commit
8febfb169d
3 changed files with 102 additions and 11 deletions
|
@ -151,6 +151,16 @@ FontEditorWidget::FontEditorWidget(const String& path, RefPtr<Gfx::BitmapFont>&&
|
||||||
};
|
};
|
||||||
|
|
||||||
m_new_action = GUI::Action::create("&New Font...", { Mod_Ctrl, Key_N }, Gfx::Bitmap::load_from_file("/res/icons/16x16/filetype-font.png"), [&](auto&) {
|
m_new_action = GUI::Action::create("&New Font...", { Mod_Ctrl, Key_N }, Gfx::Bitmap::load_from_file("/res/icons/16x16/filetype-font.png"), [&](auto&) {
|
||||||
|
if (m_font_modified) {
|
||||||
|
auto result = GUI::MessageBox::show(window(), "Save changes to the current font?", "Unsaved changes", GUI::MessageBox::Type::Warning, GUI::MessageBox::InputType::YesNoCancel);
|
||||||
|
if (result == GUI::Dialog::ExecResult::ExecYes) {
|
||||||
|
m_save_action->activate();
|
||||||
|
if (m_font_modified)
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (result == GUI::Dialog::ExecResult::ExecCancel)
|
||||||
|
return;
|
||||||
|
}
|
||||||
auto new_font_wizard = NewFontDialog::construct(window());
|
auto new_font_wizard = NewFontDialog::construct(window());
|
||||||
if (new_font_wizard->exec() == GUI::Dialog::ExecOK) {
|
if (new_font_wizard->exec() == GUI::Dialog::ExecOK) {
|
||||||
auto metadata = new_font_wizard->new_font_metadata();
|
auto metadata = new_font_wizard->new_font_metadata();
|
||||||
|
@ -167,7 +177,7 @@ FontEditorWidget::FontEditorWidget(const String& path, RefPtr<Gfx::BitmapFont>&&
|
||||||
weight = String::formatted("{}{}", parts[0], parts[1]);
|
weight = String::formatted("{}{}", parts[0], parts[1]);
|
||||||
|
|
||||||
RefPtr<Gfx::BitmapFont> new_font = Gfx::BitmapFont::create(metadata.glyph_height, metadata.glyph_width, metadata.is_fixed_width, metadata.type);
|
RefPtr<Gfx::BitmapFont> new_font = Gfx::BitmapFont::create(metadata.glyph_height, metadata.glyph_width, metadata.is_fixed_width, metadata.type);
|
||||||
String path = String::formatted("/tmp/{}{}{}.font", name, weight, metadata.presentation_size);
|
String path = String::formatted("{}{}{}.font", name, weight, metadata.presentation_size);
|
||||||
if (!new_font) {
|
if (!new_font) {
|
||||||
String message = String::formatted("Failed to create new font: {}\n", path);
|
String message = String::formatted("Failed to create new font: {}\n", path);
|
||||||
GUI::MessageBox::show(window(), message, "Font Editor", GUI::MessageBox::Type::Error);
|
GUI::MessageBox::show(window(), message, "Font Editor", GUI::MessageBox::Type::Error);
|
||||||
|
@ -180,13 +190,22 @@ FontEditorWidget::FontEditorWidget(const String& path, RefPtr<Gfx::BitmapFont>&&
|
||||||
new_font->set_weight(metadata.weight);
|
new_font->set_weight(metadata.weight);
|
||||||
new_font->set_baseline(metadata.baseline);
|
new_font->set_baseline(metadata.baseline);
|
||||||
new_font->set_mean_line(metadata.mean_line);
|
new_font->set_mean_line(metadata.mean_line);
|
||||||
|
m_font_modified = true;
|
||||||
window()->set_title(String::formatted("{} - Font Editor", path));
|
|
||||||
initialize(path, move(new_font));
|
initialize(path, move(new_font));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
m_new_action->set_status_tip("Create a new font");
|
m_new_action->set_status_tip("Create a new font");
|
||||||
m_open_action = GUI::CommonActions::make_open_action([&](auto&) {
|
m_open_action = GUI::CommonActions::make_open_action([&](auto&) {
|
||||||
|
if (m_font_modified) {
|
||||||
|
auto result = GUI::MessageBox::show(window(), "Save changes to the current font?", "Unsaved changes", GUI::MessageBox::Type::Warning, GUI::MessageBox::InputType::YesNoCancel);
|
||||||
|
if (result == GUI::Dialog::ExecResult::ExecYes) {
|
||||||
|
m_save_action->activate();
|
||||||
|
if (m_font_modified)
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (result == GUI::Dialog::ExecResult::ExecCancel)
|
||||||
|
return;
|
||||||
|
}
|
||||||
Optional<String> open_path = GUI::FilePicker::get_open_filepath(window(), {}, "/res/fonts/");
|
Optional<String> open_path = GUI::FilePicker::get_open_filepath(window(), {}, "/res/fonts/");
|
||||||
if (!open_path.has_value())
|
if (!open_path.has_value())
|
||||||
return;
|
return;
|
||||||
|
@ -203,20 +222,22 @@ FontEditorWidget::FontEditorWidget(const String& path, RefPtr<Gfx::BitmapFont>&&
|
||||||
GUI::MessageBox::show(window(), message, "Font Editor", GUI::MessageBox::Type::Error);
|
GUI::MessageBox::show(window(), message, "Font Editor", GUI::MessageBox::Type::Error);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
window()->set_title(String::formatted("{} - Font Editor", open_path.value()));
|
m_font_newly_opened = true;
|
||||||
initialize(open_path.value(), move(new_font));
|
initialize(open_path.value(), move(new_font));
|
||||||
});
|
});
|
||||||
m_save_action = GUI::CommonActions::make_save_action([&](auto&) {
|
m_save_action = GUI::CommonActions::make_save_action([&](auto&) {
|
||||||
save_as(m_path);
|
LexicalPath lexical_path(m_path);
|
||||||
|
if (!lexical_path.dirname().is_empty())
|
||||||
|
save_as(m_path);
|
||||||
|
else
|
||||||
|
m_save_as_action->activate();
|
||||||
});
|
});
|
||||||
m_save_as_action = GUI::CommonActions::make_save_as_action([&](auto&) {
|
m_save_as_action = GUI::CommonActions::make_save_as_action([&](auto&) {
|
||||||
LexicalPath lexical_path(m_path);
|
LexicalPath lexical_path(m_path);
|
||||||
Optional<String> save_path = GUI::FilePicker::get_save_filepath(window(), lexical_path.title(), lexical_path.extension());
|
Optional<String> save_path = GUI::FilePicker::get_save_filepath(window(), lexical_path.title(), lexical_path.extension());
|
||||||
if (!save_path.has_value())
|
if (!save_path.has_value())
|
||||||
return;
|
return;
|
||||||
|
save_as(save_path.value());
|
||||||
if (save_as(save_path.value()))
|
|
||||||
window()->set_title(String::formatted("{} - Font Editor", save_path.value()));
|
|
||||||
});
|
});
|
||||||
m_cut_action = GUI::CommonActions::make_cut_action([&](auto&) {
|
m_cut_action = GUI::CommonActions::make_cut_action([&](auto&) {
|
||||||
m_glyph_editor_widget->cut_glyph();
|
m_glyph_editor_widget->cut_glyph();
|
||||||
|
@ -316,6 +337,7 @@ FontEditorWidget::FontEditorWidget(const String& path, RefPtr<Gfx::BitmapFont>&&
|
||||||
m_glyph_editor_widget->on_glyph_altered = [this, update_demo](int glyph) {
|
m_glyph_editor_widget->on_glyph_altered = [this, update_demo](int glyph) {
|
||||||
m_glyph_map_widget->update_glyph(glyph);
|
m_glyph_map_widget->update_glyph(glyph);
|
||||||
update_demo();
|
update_demo();
|
||||||
|
did_modify_font();
|
||||||
};
|
};
|
||||||
|
|
||||||
m_glyph_editor_widget->on_undo_event = [this](bool finalize) {
|
m_glyph_editor_widget->on_undo_event = [this](bool finalize) {
|
||||||
|
@ -337,10 +359,12 @@ FontEditorWidget::FontEditorWidget(const String& path, RefPtr<Gfx::BitmapFont>&&
|
||||||
|
|
||||||
m_name_textbox->on_change = [&] {
|
m_name_textbox->on_change = [&] {
|
||||||
m_edited_font->set_name(m_name_textbox->text());
|
m_edited_font->set_name(m_name_textbox->text());
|
||||||
|
did_modify_font();
|
||||||
};
|
};
|
||||||
|
|
||||||
m_family_textbox->on_change = [&] {
|
m_family_textbox->on_change = [&] {
|
||||||
m_edited_font->set_family(m_family_textbox->text());
|
m_edited_font->set_family(m_family_textbox->text());
|
||||||
|
did_modify_font();
|
||||||
};
|
};
|
||||||
|
|
||||||
m_fixed_width_checkbox->on_checked = [&, update_demo](bool checked) {
|
m_fixed_width_checkbox->on_checked = [&, update_demo](bool checked) {
|
||||||
|
@ -352,55 +376,68 @@ FontEditorWidget::FontEditorWidget(const String& path, RefPtr<Gfx::BitmapFont>&&
|
||||||
m_glyph_editor_present_checkbox->set_checked(glyph_width > 0);
|
m_glyph_editor_present_checkbox->set_checked(glyph_width > 0);
|
||||||
m_glyph_editor_widget->update();
|
m_glyph_editor_widget->update();
|
||||||
update_demo();
|
update_demo();
|
||||||
|
did_modify_font();
|
||||||
};
|
};
|
||||||
|
|
||||||
m_glyph_editor_width_spinbox->on_change = [this, update_demo, update_statusbar](int value) {
|
m_glyph_editor_width_spinbox->on_change = [this, update_demo, update_statusbar](int value) {
|
||||||
|
if (m_edited_font->raw_glyph_width(m_glyph_map_widget->selected_glyph()) == value)
|
||||||
|
return;
|
||||||
m_edited_font->set_glyph_width(m_glyph_map_widget->selected_glyph(), value);
|
m_edited_font->set_glyph_width(m_glyph_map_widget->selected_glyph(), value);
|
||||||
m_glyph_editor_widget->update();
|
m_glyph_editor_widget->update();
|
||||||
m_glyph_map_widget->update_glyph(m_glyph_map_widget->selected_glyph());
|
m_glyph_map_widget->update_glyph(m_glyph_map_widget->selected_glyph());
|
||||||
update_demo();
|
update_demo();
|
||||||
update_statusbar();
|
update_statusbar();
|
||||||
|
did_modify_font();
|
||||||
};
|
};
|
||||||
|
|
||||||
m_glyph_editor_present_checkbox->on_checked = [this, update_demo, update_statusbar](bool checked) {
|
m_glyph_editor_present_checkbox->on_checked = [this, update_demo, update_statusbar](bool checked) {
|
||||||
if (!m_edited_font->is_fixed_width())
|
if (!m_edited_font->is_fixed_width()
|
||||||
|
|| m_edited_font->raw_glyph_width(m_glyph_map_widget->selected_glyph()) == checked
|
||||||
|
|| (m_edited_font->raw_glyph_width(m_glyph_map_widget->selected_glyph()) && checked))
|
||||||
return;
|
return;
|
||||||
m_edited_font->set_glyph_width(m_glyph_map_widget->selected_glyph(), checked ? m_edited_font->glyph_fixed_width() : 0);
|
m_edited_font->set_glyph_width(m_glyph_map_widget->selected_glyph(), checked ? m_edited_font->glyph_fixed_width() : 0);
|
||||||
m_glyph_editor_widget->update();
|
m_glyph_editor_widget->update();
|
||||||
m_glyph_map_widget->update_glyph(m_glyph_map_widget->selected_glyph());
|
m_glyph_map_widget->update_glyph(m_glyph_map_widget->selected_glyph());
|
||||||
update_demo();
|
update_demo();
|
||||||
update_statusbar();
|
update_statusbar();
|
||||||
|
did_modify_font();
|
||||||
};
|
};
|
||||||
|
|
||||||
m_weight_combobox->on_change = [this]() {
|
m_weight_combobox->on_change = [this]() {
|
||||||
m_edited_font->set_weight(GUI::name_to_weight(m_weight_combobox->text()));
|
m_edited_font->set_weight(GUI::name_to_weight(m_weight_combobox->text()));
|
||||||
|
did_modify_font();
|
||||||
};
|
};
|
||||||
|
|
||||||
m_type_combobox->on_change = [this](auto&, const auto& index) {
|
m_type_combobox->on_change = [this](auto&, const auto& index) {
|
||||||
m_edited_font->set_type(static_cast<Gfx::FontTypes>(index.row()));
|
m_edited_font->set_type(static_cast<Gfx::FontTypes>(index.row()));
|
||||||
m_glyph_map_widget->reprobe_font();
|
m_glyph_map_widget->reprobe_font();
|
||||||
|
did_modify_font();
|
||||||
};
|
};
|
||||||
|
|
||||||
m_presentation_spinbox->on_change = [this, update_demo](int value) {
|
m_presentation_spinbox->on_change = [this, update_demo](int value) {
|
||||||
m_edited_font->set_presentation_size(value);
|
m_edited_font->set_presentation_size(value);
|
||||||
update_demo();
|
update_demo();
|
||||||
|
did_modify_font();
|
||||||
};
|
};
|
||||||
|
|
||||||
m_spacing_spinbox->on_change = [this, update_demo](int value) {
|
m_spacing_spinbox->on_change = [this, update_demo](int value) {
|
||||||
m_edited_font->set_glyph_spacing(value);
|
m_edited_font->set_glyph_spacing(value);
|
||||||
update_demo();
|
update_demo();
|
||||||
|
did_modify_font();
|
||||||
};
|
};
|
||||||
|
|
||||||
m_baseline_spinbox->on_change = [this, update_demo](int value) {
|
m_baseline_spinbox->on_change = [this, update_demo](int value) {
|
||||||
m_edited_font->set_baseline(value);
|
m_edited_font->set_baseline(value);
|
||||||
m_glyph_editor_widget->update();
|
m_glyph_editor_widget->update();
|
||||||
update_demo();
|
update_demo();
|
||||||
|
did_modify_font();
|
||||||
};
|
};
|
||||||
|
|
||||||
m_mean_line_spinbox->on_change = [this, update_demo](int value) {
|
m_mean_line_spinbox->on_change = [this, update_demo](int value) {
|
||||||
m_edited_font->set_mean_line(value);
|
m_edited_font->set_mean_line(value);
|
||||||
m_glyph_editor_widget->update();
|
m_glyph_editor_widget->update();
|
||||||
update_demo();
|
update_demo();
|
||||||
|
did_modify_font();
|
||||||
};
|
};
|
||||||
|
|
||||||
GUI::Application::the()->on_action_enter = [&statusbar](GUI::Action& action) {
|
GUI::Application::the()->on_action_enter = [&statusbar](GUI::Action& action) {
|
||||||
|
@ -478,6 +515,10 @@ void FontEditorWidget::initialize(const String& path, RefPtr<Gfx::BitmapFont>&&
|
||||||
deferred_invoke([this](auto&) {
|
deferred_invoke([this](auto&) {
|
||||||
m_glyph_map_widget->set_focus(true);
|
m_glyph_map_widget->set_focus(true);
|
||||||
m_glyph_map_widget->scroll_to_glyph(m_glyph_map_widget->selected_glyph());
|
m_glyph_map_widget->scroll_to_glyph(m_glyph_map_widget->selected_glyph());
|
||||||
|
if (m_font_newly_opened)
|
||||||
|
m_font_modified = false;
|
||||||
|
update_title();
|
||||||
|
m_font_newly_opened = false;
|
||||||
});
|
});
|
||||||
|
|
||||||
m_undo_stack = make<GUI::UndoStack>();
|
m_undo_stack = make<GUI::UndoStack>();
|
||||||
|
@ -497,6 +538,8 @@ void FontEditorWidget::initialize_menubar(GUI::Menubar& menubar)
|
||||||
app_menu.add_action(*m_save_as_action);
|
app_menu.add_action(*m_save_as_action);
|
||||||
app_menu.add_separator();
|
app_menu.add_separator();
|
||||||
app_menu.add_action(GUI::CommonActions::make_quit_action([this](auto&) {
|
app_menu.add_action(GUI::CommonActions::make_quit_action([this](auto&) {
|
||||||
|
if (!request_close())
|
||||||
|
return;
|
||||||
GUI::Application::the()->quit();
|
GUI::Application::the()->quit();
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
@ -534,6 +577,8 @@ bool FontEditorWidget::save_as(const String& path)
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
m_path = path;
|
m_path = path;
|
||||||
|
m_font_modified = false;
|
||||||
|
update_title();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -575,4 +620,39 @@ void FontEditorWidget::did_change_undo_stack()
|
||||||
{
|
{
|
||||||
m_undo_action->set_enabled(m_undo_stack->can_undo());
|
m_undo_action->set_enabled(m_undo_stack->can_undo());
|
||||||
m_redo_action->set_enabled(m_undo_stack->can_redo());
|
m_redo_action->set_enabled(m_undo_stack->can_redo());
|
||||||
|
did_modify_font();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool FontEditorWidget::request_close()
|
||||||
|
{
|
||||||
|
if (!m_font_modified)
|
||||||
|
return true;
|
||||||
|
auto result = GUI::MessageBox::show(window(), "Save changes to the current font?", "Unsaved changes", GUI::MessageBox::Type::Warning, GUI::MessageBox::InputType::YesNoCancel);
|
||||||
|
if (result == GUI::MessageBox::ExecYes) {
|
||||||
|
m_save_action->activate();
|
||||||
|
if (!m_font_modified)
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (result == GUI::MessageBox::ExecNo)
|
||||||
|
return true;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
void FontEditorWidget::update_title()
|
||||||
|
{
|
||||||
|
StringBuilder title;
|
||||||
|
title.append(m_path);
|
||||||
|
if (m_font_modified && !m_font_newly_opened)
|
||||||
|
title.append(" (*)");
|
||||||
|
title.append(" - Font Editor");
|
||||||
|
if (window())
|
||||||
|
window()->set_title(title.to_string());
|
||||||
|
}
|
||||||
|
|
||||||
|
void FontEditorWidget::did_modify_font()
|
||||||
|
{
|
||||||
|
if (m_font_modified)
|
||||||
|
return;
|
||||||
|
m_font_modified = true;
|
||||||
|
update_title();
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,6 +21,8 @@ public:
|
||||||
virtual ~FontEditorWidget() override;
|
virtual ~FontEditorWidget() override;
|
||||||
|
|
||||||
bool save_as(const String&);
|
bool save_as(const String&);
|
||||||
|
bool request_close();
|
||||||
|
void update_title();
|
||||||
|
|
||||||
const String& path() { return m_path; }
|
const String& path() { return m_path; }
|
||||||
const Gfx::BitmapFont& edited_font() { return *m_edited_font; }
|
const Gfx::BitmapFont& edited_font() { return *m_edited_font; }
|
||||||
|
@ -38,6 +40,7 @@ private:
|
||||||
void undo();
|
void undo();
|
||||||
void redo();
|
void redo();
|
||||||
void did_change_undo_stack();
|
void did_change_undo_stack();
|
||||||
|
void did_modify_font();
|
||||||
|
|
||||||
RefPtr<Gfx::BitmapFont> m_edited_font;
|
RefPtr<Gfx::BitmapFont> m_edited_font;
|
||||||
|
|
||||||
|
@ -87,4 +90,6 @@ private:
|
||||||
Vector<String> m_font_weight_list;
|
Vector<String> m_font_weight_list;
|
||||||
Vector<String> m_font_type_list;
|
Vector<String> m_font_type_list;
|
||||||
bool m_font_metadata { true };
|
bool m_font_metadata { true };
|
||||||
|
bool m_font_modified { false };
|
||||||
|
bool m_font_newly_opened { true };
|
||||||
};
|
};
|
||||||
|
|
|
@ -52,7 +52,7 @@ int main(int argc, char** argv)
|
||||||
|
|
||||||
RefPtr<Gfx::BitmapFont> edited_font;
|
RefPtr<Gfx::BitmapFont> edited_font;
|
||||||
if (path == nullptr) {
|
if (path == nullptr) {
|
||||||
path = "/tmp/saved.font";
|
path = "Untitled.font";
|
||||||
edited_font = static_ptr_cast<Gfx::BitmapFont>(Gfx::FontDatabase::default_font().clone());
|
edited_font = static_ptr_cast<Gfx::BitmapFont>(Gfx::FontDatabase::default_font().clone());
|
||||||
} else {
|
} else {
|
||||||
auto bitmap_font = Gfx::BitmapFont::load_from_file(path);
|
auto bitmap_font = Gfx::BitmapFont::load_from_file(path);
|
||||||
|
@ -74,14 +74,20 @@ int main(int argc, char** argv)
|
||||||
auto window = GUI::Window::construct();
|
auto window = GUI::Window::construct();
|
||||||
window->set_icon(app_icon.bitmap_for_size(16));
|
window->set_icon(app_icon.bitmap_for_size(16));
|
||||||
window->resize(440, 470);
|
window->resize(440, 470);
|
||||||
window->set_title(String::formatted("{} - Font Editor", path));
|
|
||||||
|
|
||||||
auto& font_editor = window->set_main_widget<FontEditorWidget>(path, move(edited_font));
|
auto& font_editor = window->set_main_widget<FontEditorWidget>(path, move(edited_font));
|
||||||
|
font_editor.update_title();
|
||||||
|
|
||||||
auto menubar = GUI::Menubar::construct();
|
auto menubar = GUI::Menubar::construct();
|
||||||
font_editor.initialize_menubar(menubar);
|
font_editor.initialize_menubar(menubar);
|
||||||
window->set_menubar(move(menubar));
|
window->set_menubar(move(menubar));
|
||||||
|
|
||||||
|
window->on_close_request = [&]() -> GUI::Window::CloseRequestDecision {
|
||||||
|
if (font_editor.request_close())
|
||||||
|
return GUI::Window::CloseRequestDecision::Close;
|
||||||
|
return GUI::Window::CloseRequestDecision::StayOpen;
|
||||||
|
};
|
||||||
|
|
||||||
window->show();
|
window->show();
|
||||||
|
|
||||||
return app->exec();
|
return app->exec();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue