1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-06-01 07:28:11 +00:00

LibGUI+FontEditor: Add context menu for editor actions

GlyphMapWidget now reports context menu requests when secondary
clicking the map. This also adds a new Select All action and updates
the Copy Character action to work on multi-glyph selections. Glyph
navigation actions have been moved to a separate Go menu, as is
common in other apps.
This commit is contained in:
thankyouverycool 2022-03-19 13:29:55 -04:00 committed by Andreas Kling
parent 42284a1550
commit b8cc18896f
4 changed files with 59 additions and 9 deletions

View file

@ -203,6 +203,15 @@ FontEditorWidget::FontEditorWidget()
m_redo_action = GUI::CommonActions::make_redo_action([&](auto&) {
redo();
});
m_select_all_action = GUI::CommonActions::make_select_all_action([this](auto&) {
m_glyph_map_widget->set_selection(m_range.first, m_range.last - m_range.first + 1);
m_glyph_map_widget->update();
auto selection = m_glyph_map_widget->selection().normalized();
m_undo_selection->set_start(selection.start());
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);
@ -326,11 +335,17 @@ FontEditorWidget::FontEditorWidget()
m_glyph_editor_widget->flip_vertically();
});
m_copy_character_action = GUI::Action::create("Cop&y as Character", { Mod_Ctrl | Mod_Shift, Key_C }, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/edit-copy.png").release_value_but_fixme_should_propagate_errors(), [&](auto&) {
StringBuilder glyph_builder;
glyph_builder.append_code_point(m_glyph_editor_widget->glyph());
GUI::Clipboard::the().set_plain_text(glyph_builder.build());
m_copy_text_action = GUI::Action::create("Copy as Te&xt", { Mod_Ctrl, Key_T }, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/edit-copy.png").release_value_but_fixme_should_propagate_errors(), [&](auto&) {
StringBuilder builder;
auto selection = m_glyph_map_widget->selection().normalized();
for (auto code_point = selection.start(); code_point < selection.start() + selection.size(); ++code_point) {
if (!m_glyph_map_widget->font().contains_glyph(code_point))
continue;
builder.append_code_point(code_point);
}
GUI::Clipboard::the().set_plain_text(builder.to_string());
});
m_copy_text_action->set_status_tip("Copy to clipboard as text");
glyph_transform_toolbar.add_action(*m_flip_horizontal_action);
glyph_transform_toolbar.add_action(*m_flip_vertical_action);
@ -367,6 +382,21 @@ FontEditorWidget::FontEditorWidget()
update_statusbar();
};
m_glyph_map_widget->on_context_menu_request = [this](auto& event) {
if (!m_context_menu) {
m_context_menu = GUI::Menu::construct();
m_context_menu->add_action(*m_cut_action);
m_context_menu->add_action(*m_copy_action);
m_context_menu->add_action(*m_paste_action);
m_context_menu->add_action(*m_delete_action);
m_context_menu->add_separator();
m_context_menu->add_action(*m_select_all_action);
m_context_menu->add_separator();
m_context_menu->add_action(*m_copy_text_action);
}
m_context_menu->popup(event.screen_position());
};
m_name_textbox->on_change = [&] {
m_edited_font->set_name(m_name_textbox->text());
did_modify_font();
@ -602,11 +632,14 @@ void FontEditorWidget::initialize_menubar(GUI::Window& window)
edit_menu.add_action(*m_paste_action);
edit_menu.add_action(*m_delete_action);
edit_menu.add_separator();
edit_menu.add_action(*m_copy_character_action);
edit_menu.add_action(*m_select_all_action);
edit_menu.add_separator();
edit_menu.add_action(*m_previous_glyph_action);
edit_menu.add_action(*m_next_glyph_action);
edit_menu.add_action(*m_go_to_glyph_action);
edit_menu.add_action(*m_copy_text_action);
auto& go_menu = window.add_menu("&Go");
go_menu.add_action(*m_previous_glyph_action);
go_menu.add_action(*m_next_glyph_action);
go_menu.add_action(*m_go_to_glyph_action);
auto& view_menu = window.add_menu("&View");
view_menu.add_action(*m_open_preview_action);

View file

@ -77,6 +77,9 @@ private:
RefPtr<GUI::Action> m_paste_action;
RefPtr<GUI::Action> m_delete_action;
RefPtr<GUI::Action> m_copy_text_action;
RefPtr<GUI::Action> m_select_all_action;
RefPtr<GUI::Action> m_undo_action;
RefPtr<GUI::Action> m_redo_action;
RefPtr<UndoSelection> m_undo_selection;
@ -103,7 +106,6 @@ private:
RefPtr<GUI::Action> m_flip_vertical_action;
RefPtr<GUI::Action> m_rotate_clockwise_action;
RefPtr<GUI::Action> m_rotate_counterclockwise_action;
RefPtr<GUI::Action> m_copy_character_action;
RefPtr<GUI::Statusbar> m_statusbar;
RefPtr<GUI::Window> m_font_preview_window;
@ -126,6 +128,7 @@ private:
RefPtr<GUI::ListView> m_unicode_block_listview;
RefPtr<GUI::Model> m_unicode_block_model;
RefPtr<GUI::FilteringProxyModel> m_filter_model;
RefPtr<GUI::Menu> m_context_menu;
String m_path;
Vector<String> m_font_weight_list;

View file

@ -182,8 +182,17 @@ int GlyphMapWidget::glyph_at_position_clamped(Gfx::IntPoint position) const
return glyph;
}
void GlyphMapWidget::context_menu_event(GUI::ContextMenuEvent& event)
{
if (on_context_menu_request)
on_context_menu_request(event);
}
void GlyphMapWidget::mousedown_event(MouseEvent& event)
{
if (event.button() == MouseButton::Secondary)
return;
if (auto maybe_glyph = glyph_at_position(event.position()); maybe_glyph.has_value()) {
auto glyph = maybe_glyph.value();
if (event.shift())
@ -196,6 +205,9 @@ void GlyphMapWidget::mousedown_event(MouseEvent& event)
void GlyphMapWidget::mouseup_event(GUI::MouseEvent& event)
{
if (event.button() == MouseButton::Secondary)
return;
if (!m_in_drag_select)
return;
auto constrained = event.position().constrained(widget_inner_rect());

View file

@ -68,6 +68,7 @@ public:
Function<void(int)> on_active_glyph_changed;
Function<void(int)> on_glyph_double_clicked;
Function<void(ContextMenuEvent&)> on_context_menu_request;
private:
GlyphMapWidget();
@ -79,6 +80,7 @@ private:
virtual void keydown_event(KeyEvent&) override;
virtual void resize_event(ResizeEvent&) override;
virtual void did_change_font() override;
virtual void context_menu_event(ContextMenuEvent&) override;
Gfx::IntRect get_outer_rect(int glyph) const;
Optional<int> glyph_at_position(Gfx::IntPoint) const;