FontEditor: Simplify start-up

Previusly a cloned or newly loaded font was moved twice from main to
the constructor and then from constructor to an init routine where it
was finally used. The unmasked font is now moved only once, directly
to initialization, and redundant error checking is discarded.
This commit is contained in:
thankyouverycool 2021-11-29 09:07:47 -05:00 committed by Andreas Kling
parent 56a8a2b493
commit 922e80e72b
Notes: sideshowbarker 2024-07-17 23:21:17 +09:00
4 changed files with 20 additions and 34 deletions

View File

@ -106,7 +106,7 @@ static RefPtr<GUI::Window> create_font_preview_window(FontEditorWidget& editor)
return window;
}
FontEditorWidget::FontEditorWidget(const String& path, RefPtr<Gfx::BitmapFont>&& edited_font)
FontEditorWidget::FontEditorWidget()
{
load_from_gml(font_editor_window_gml);
@ -475,8 +475,6 @@ FontEditorWidget::FontEditorWidget(const String& path, RefPtr<Gfx::BitmapFont>&&
GUI::Application::the()->on_action_leave = [this](GUI::Action&) {
m_statusbar->set_override_text({});
};
initialize(path, move(edited_font));
}
FontEditorWidget::~FontEditorWidget()
@ -619,22 +617,18 @@ void FontEditorWidget::set_show_font_metadata(bool show)
m_font_metadata_groupbox->set_visible(m_font_metadata);
}
void FontEditorWidget::open_file(String const& path)
bool FontEditorWidget::open_file(String const& path)
{
auto bitmap_font = Gfx::BitmapFont::load_from_file(path);
if (!bitmap_font) {
String message = String::formatted("Couldn't load font: {}\n", path);
GUI::MessageBox::show(window(), message, "Font Editor", GUI::MessageBox::Type::Error);
return;
}
RefPtr<Gfx::BitmapFont> new_font = static_ptr_cast<Gfx::BitmapFont>(bitmap_font->unmasked_character_set());
if (!new_font) {
String message = String::formatted("Couldn't load font: {}\n", path);
GUI::MessageBox::show(window(), message, "Font Editor", GUI::MessageBox::Type::Error);
return;
return false;
}
auto new_font = bitmap_font->unmasked_character_set();
window()->set_modified(false);
initialize(path, move(new_font));
return true;
}
void FontEditorWidget::undo()

View File

@ -20,6 +20,7 @@ class FontEditorWidget final : public GUI::Widget {
public:
virtual ~FontEditorWidget() override;
bool open_file(String const&);
bool save_as(const String&);
bool request_close();
void update_title();
@ -35,11 +36,10 @@ public:
Function<void()> on_initialize;
private:
FontEditorWidget(const String& path, RefPtr<Gfx::BitmapFont>&&);
FontEditorWidget();
virtual void drop_event(GUI::DropEvent&) override;
void open_file(String const&);
void undo();
void redo();
void did_modify_font();

View File

@ -31,6 +31,9 @@ void GlyphMapWidget::initialize(Gfx::BitmapFont& mutable_font)
void GlyphMapWidget::resize_event(GUI::ResizeEvent& event)
{
if (!m_font)
return;
int event_width = event.size().width() - this->vertical_scrollbar().width() - (frame_thickness() * 2) - m_horizontal_spacing;
int event_height = event.size().height() - (frame_thickness() * 2);
m_visible_glyphs = (event_width * event_height) / (font().max_glyph_width() * font().glyph_height());

View File

@ -34,35 +34,24 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
args_parser.add_positional_argument(path, "The font file for editing.", "file", Core::ArgsParser::Required::No);
args_parser.parse(arguments);
RefPtr<Gfx::BitmapFont> edited_font;
if (path == nullptr) {
auto bitmap_font = static_ptr_cast<Gfx::BitmapFont>(Gfx::FontDatabase::default_font().clone());
edited_font = static_ptr_cast<Gfx::BitmapFont>(bitmap_font->unmasked_character_set());
} else {
auto bitmap_font = Gfx::BitmapFont::load_from_file(path);
if (!bitmap_font) {
String message = String::formatted("Couldn't load font: {}\n", path);
GUI::MessageBox::show(nullptr, message, "Font Editor", GUI::MessageBox::Type::Error);
return 1;
}
edited_font = static_ptr_cast<Gfx::BitmapFont>(bitmap_font->unmasked_character_set());
if (!edited_font) {
String message = String::formatted("Couldn't load font: {}\n", path);
GUI::MessageBox::show(nullptr, message, "Font Editor", GUI::MessageBox::Type::Error);
return 1;
}
}
auto app_icon = GUI::Icon::default_icon("app-font-editor");
auto window = TRY(GUI::Window::try_create());
window->set_icon(app_icon.bitmap_for_size(16));
window->resize(440, 470);
auto& font_editor = window->set_main_widget<FontEditorWidget>(path, move(edited_font));
auto& font_editor = window->set_main_widget<FontEditorWidget>();
font_editor.initialize_menubar(*window);
if (path) {
auto success = font_editor.open_file(path);
if (!success)
return 1;
} else {
auto mutable_font = static_ptr_cast<Gfx::BitmapFont>(Gfx::FontDatabase::default_font().clone())->unmasked_character_set();
font_editor.initialize({}, move(mutable_font));
}
window->on_close_request = [&]() -> GUI::Window::CloseRequestDecision {
if (font_editor.request_close())
return GUI::Window::CloseRequestDecision::Close;