From 1b9dff5fb1145e11a2c70551422a547083f8cf68 Mon Sep 17 00:00:00 2001 From: thankyouverycool <66646555+thankyouverycool@users.noreply.github.com> Date: Mon, 4 Jul 2022 22:41:05 -0400 Subject: [PATCH] FontEditor: Convert preview window to GML and propagate its errors --- .../Applications/FontEditor/CMakeLists.txt | 2 + .../Applications/FontEditor/FontEditor.cpp | 72 +++++++------------ Userland/Applications/FontEditor/FontEditor.h | 8 ++- .../FontEditor/FontPreviewWindow.gml | 35 +++++++++ 4 files changed, 69 insertions(+), 48 deletions(-) create mode 100644 Userland/Applications/FontEditor/FontPreviewWindow.gml diff --git a/Userland/Applications/FontEditor/CMakeLists.txt b/Userland/Applications/FontEditor/CMakeLists.txt index 2e6339d61e..81174d221f 100644 --- a/Userland/Applications/FontEditor/CMakeLists.txt +++ b/Userland/Applications/FontEditor/CMakeLists.txt @@ -6,12 +6,14 @@ serenity_component( include_directories(${CMAKE_CURRENT_BINARY_DIR}) compile_gml(FontEditorWindow.gml FontEditorWindowGML.h font_editor_window_gml) +compile_gml(FontPreviewWindow.gml FontPreviewWindowGML.h font_preview_window_gml) compile_gml(NewFontDialogPage1.gml NewFontDialogPage1GML.h new_font_dialog_page_1_gml) compile_gml(NewFontDialogPage2.gml NewFontDialogPage2GML.h new_font_dialog_page_2_gml) set(SOURCES FontEditor.cpp FontEditorWindowGML.h + FontPreviewWindowGML.h GlyphEditorWidget.cpp main.cpp NewFontDialog.cpp diff --git a/Userland/Applications/FontEditor/FontEditor.cpp b/Userland/Applications/FontEditor/FontEditor.cpp index 383ec3905f..ffb7d4a7c6 100644 --- a/Userland/Applications/FontEditor/FontEditor.cpp +++ b/Userland/Applications/FontEditor/FontEditor.cpp @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -56,55 +57,33 @@ static constexpr Array pangrams = { "lazy dog" }; -static RefPtr create_font_preview_window(FontEditorWidget& editor) +ErrorOr> FontEditorWidget::create_preview_window() { - auto window = GUI::Window::construct(&editor); + auto window = TRY(GUI::Window::try_create(this)); window->set_window_type(GUI::WindowType::ToolWindow); window->set_title("Preview"); window->resize(400, 150); - window->set_minimum_size(200, 100); - window->center_within(*editor.window()); + window->center_within(*this->window()); - auto& main_widget = window->set_main_widget(); - main_widget.set_fill_with_background_color(true); - main_widget.set_layout(); - main_widget.layout()->set_margins(2); - main_widget.layout()->set_spacing(4); + auto main_widget = TRY(window->try_set_main_widget()); + main_widget->load_from_gml(font_preview_window_gml); - auto& preview_box = main_widget.add(); - preview_box.set_layout(); - preview_box.layout()->set_margins(8); + m_preview_label = find_descendant_of_type_named("preview_label"); + m_preview_label->set_font(edited_font()); - auto& preview_label = preview_box.add(); - preview_label.set_font(editor.edited_font()); - - editor.on_initialize = [&] { - preview_label.set_font(editor.edited_font()); + m_preview_textbox = find_descendant_of_type_named("preview_textbox"); + m_preview_textbox->on_change = [&] { + auto preview = String::formatted("{}\n{}", m_preview_textbox->text(), Unicode::to_unicode_uppercase_full(m_preview_textbox->text())); + m_preview_label->set_text(preview); }; + m_preview_textbox->set_text(pangrams[0]); - auto& textbox_button_container = main_widget.add(); - textbox_button_container.set_layout(); - textbox_button_container.set_fixed_height(22); - - auto& preview_textbox = textbox_button_container.add(); - preview_textbox.set_text(pangrams[0]); - preview_textbox.set_placeholder("Preview text"); - - preview_textbox.on_change = [&] { - auto preview = String::formatted("{}\n{}", - preview_textbox.text(), - Unicode::to_unicode_uppercase_full(preview_textbox.text())); - preview_label.set_text(preview); - }; - - auto& reload_button = textbox_button_container.add(); - reload_button.set_icon(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/reload.png").release_value_but_fixme_should_propagate_errors()); - reload_button.set_fixed_width(22); + auto& reload_button = *find_descendant_of_type_named("reload_button"); reload_button.on_click = [&](auto) { static size_t i = 1; if (i >= pangrams.size()) i = 0; - preview_textbox.set_text(pangrams[i]); + m_preview_textbox->set_text(pangrams[i]); i++; }; @@ -196,13 +175,16 @@ ErrorOr FontEditorWidget::create_actions() m_undo_selection->set_size(selection.size()); }); - m_open_preview_action = GUI::Action::create("&Preview Font", { Mod_Ctrl, Key_P }, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/find.png").release_value_but_fixme_should_propagate_errors(), [&](auto&) { - if (!m_font_preview_window) - m_font_preview_window = create_font_preview_window(*this); - m_font_preview_window->show(); - m_font_preview_window->move_to_front(); + m_open_preview_action = GUI::Action::create("&Preview Font", { Mod_Ctrl, Key_P }, TRY(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/find.png")), [&](auto&) { + if (!m_font_preview_window) { + if (auto maybe_window = create_preview_window(); maybe_window.is_error()) + warnln("Failed to create preview window: {}", maybe_window.error()); + else + m_font_preview_window = maybe_window.release_value(); + } + if (m_font_preview_window) + m_font_preview_window->show(); }); - m_open_preview_action->set_checked(false); m_open_preview_action->set_status_tip("Preview the current font"); bool show_metadata = Config::read_bool("FontEditor", "Layout", "ShowMetadata", true); @@ -582,6 +564,9 @@ ErrorOr FontEditorWidget::initialize(String const& path, RefPtrset_font(*m_edited_font); + m_glyph_map_widget->set_font(*m_edited_font); m_glyph_editor_widget->initialize(*m_edited_font); did_resize_glyph_editor(); @@ -636,9 +621,6 @@ ErrorOr FontEditorWidget::initialize(String const& path, RefPtr on_initialize; - private: FontEditorWidget(); @@ -58,6 +56,7 @@ private: ErrorOr create_models(); ErrorOr create_toolbars(); ErrorOr create_undo_stack(); + ErrorOr> create_preview_window(); virtual void drop_event(GUI::DropEvent&) override; @@ -124,7 +123,6 @@ private: RefPtr m_rotate_counterclockwise_action; RefPtr m_statusbar; - RefPtr m_font_preview_window; RefPtr m_left_column_container; RefPtr m_glyph_editor_container; RefPtr m_unicode_block_container; @@ -146,6 +144,10 @@ private: RefPtr m_filter_model; RefPtr m_context_menu; + RefPtr m_preview_label; + RefPtr m_preview_textbox; + RefPtr m_font_preview_window; + String m_path; Vector m_font_weight_list; Vector m_font_slope_list; diff --git a/Userland/Applications/FontEditor/FontPreviewWindow.gml b/Userland/Applications/FontEditor/FontPreviewWindow.gml new file mode 100644 index 0000000000..b91908d882 --- /dev/null +++ b/Userland/Applications/FontEditor/FontPreviewWindow.gml @@ -0,0 +1,35 @@ +@GUI::Widget { + fill_with_background_color: true + layout: @GUI::VerticalBoxLayout { + margins: [2] + } + + @GUI::Frame { + layout: @GUI::VerticalBoxLayout { + margins: [4] + } + shape: "Box" + shadow: "Sunken" + thickness: 2 + + @GUI::Label { + name: "preview_label" + } + } + + @GUI::Widget { + layout: @GUI::HorizontalBoxLayout {} + fixed_height: 22 + + @GUI::TextBox { + name: "preview_textbox" + placeholder: "Preview text" + } + + @GUI::Button { + name: "reload_button" + icon: "/res/icons/16x16/reload.png" + fixed_width: 22 + } + } +}