diff options
author | Marcus Nilsson <brainbomb@gmail.com> | 2022-01-02 21:58:51 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2022-01-06 21:25:02 +0100 |
commit | 3af71c406d1e2d75cc36e2642e37479f12d3ab6d (patch) | |
tree | 41593facf8a92cb7ff193656b13a44cce99ac9b0 /Userland | |
parent | 45085122eea5cd43c88cfb7e542d6b22e69168b3 (diff) | |
download | serenity-3af71c406d1e2d75cc36e2642e37479f12d3ab6d.zip |
PDFViewer: Add zoom in/out/reset menu actions
Make PDFViewer::zoom_in() & ::zoom_out() public and add menu and toolbar
actions. Also add an action for zoom reset.
Diffstat (limited to 'Userland')
-rw-r--r-- | Userland/Applications/PDFViewer/PDFViewer.cpp | 17 | ||||
-rw-r--r-- | Userland/Applications/PDFViewer/PDFViewer.h | 7 | ||||
-rw-r--r-- | Userland/Applications/PDFViewer/PDFViewerWidget.cpp | 30 | ||||
-rw-r--r-- | Userland/Applications/PDFViewer/PDFViewerWidget.h | 4 |
4 files changed, 50 insertions, 8 deletions
diff --git a/Userland/Applications/PDFViewer/PDFViewer.cpp b/Userland/Applications/PDFViewer/PDFViewer.cpp index f377466ca8..05b744b649 100644 --- a/Userland/Applications/PDFViewer/PDFViewer.cpp +++ b/Userland/Applications/PDFViewer/PDFViewer.cpp @@ -109,9 +109,8 @@ void PDFViewer::mousewheel_event(GUI::MouseEvent& event) scrollbar.decrease_slider_by(20); } } + update(); } - - update(); } void PDFViewer::timer_event(Core::TimerEvent&) @@ -125,14 +124,24 @@ void PDFViewer::timer_event(Core::TimerEvent&) void PDFViewer::zoom_in() { - if (m_zoom_level < number_of_zoom_levels - 1) + if (m_zoom_level < number_of_zoom_levels - 1) { m_zoom_level++; + update(); + } } void PDFViewer::zoom_out() { - if (m_zoom_level > 0) + if (m_zoom_level > 0) { m_zoom_level--; + update(); + } +} + +void PDFViewer::reset_zoom() +{ + m_zoom_level = initial_zoom_level; + update(); } RefPtr<Gfx::Bitmap> PDFViewer::render_page(const PDF::Page& page) diff --git a/Userland/Applications/PDFViewer/PDFViewer.h b/Userland/Applications/PDFViewer/PDFViewer.h index 3b0a04eef8..ab6690b8fd 100644 --- a/Userland/Applications/PDFViewer/PDFViewer.h +++ b/Userland/Applications/PDFViewer/PDFViewer.h @@ -49,6 +49,10 @@ public: Function<void(u32 new_page)> on_page_change; + void zoom_in(); + void zoom_out(); + void reset_zoom(); + protected: PDFViewer(); @@ -60,9 +64,6 @@ private: RefPtr<Gfx::Bitmap> get_rendered_page(u32 index); RefPtr<Gfx::Bitmap> render_page(const PDF::Page&); - void zoom_in(); - void zoom_out(); - RefPtr<PDF::Document> m_document; u32 m_current_page_index { 0 }; Vector<HashMap<u32, RefPtr<Gfx::Bitmap>>> m_rendered_page_list; diff --git a/Userland/Applications/PDFViewer/PDFViewerWidget.cpp b/Userland/Applications/PDFViewer/PDFViewerWidget.cpp index aac25ba043..618dfd63b2 100644 --- a/Userland/Applications/PDFViewer/PDFViewerWidget.cpp +++ b/Userland/Applications/PDFViewer/PDFViewerWidget.cpp @@ -58,6 +58,10 @@ void PDFViewerWidget::initialize_menubar(GUI::Window& window) auto& view_menu = window.add_menu("&View"); view_menu.add_action(*m_toggle_sidebar_action); + view_menu.add_separator(); + view_menu.add_action(*m_zoom_in_action); + view_menu.add_action(*m_zoom_out_action); + view_menu.add_action(*m_reset_zoom_action); auto& help_menu = window.add_menu("&Help"); help_menu.add_action(GUI::CommonActions::make_about_action("PDF Viewer", GUI::Icon::default_icon("app-pdf-viewer"), &window)); @@ -111,6 +115,28 @@ void PDFViewerWidget::create_toolbar() }; m_total_page_label = toolbar.add<GUI::Label>(); + m_total_page_label->set_fixed_width(30); + toolbar.add_separator(); + + m_zoom_in_action = GUI::CommonActions::make_zoom_in_action([&](auto&) { + m_viewer->zoom_in(); + }); + + m_zoom_out_action = GUI::CommonActions::make_zoom_out_action([&](auto&) { + m_viewer->zoom_out(); + }); + + m_reset_zoom_action = GUI::CommonActions::make_reset_zoom_action([&](auto&) { + m_viewer->reset_zoom(); + }); + + m_zoom_in_action->set_enabled(false); + m_zoom_out_action->set_enabled(false); + m_reset_zoom_action->set_enabled(false); + + toolbar.add_action(*m_zoom_in_action); + toolbar.add_action(*m_zoom_out_action); + toolbar.add_action(*m_reset_zoom_action); } void PDFViewerWidget::open_file(int fd, String const& path) @@ -131,7 +157,6 @@ void PDFViewerWidget::open_file(int fd, String const& path) m_viewer->set_document(document); m_total_page_label->set_text(String::formatted("of {}", document->get_page_count())); - m_total_page_label->set_fixed_width(30); m_page_text_box->set_enabled(true); m_page_text_box->set_current_number(1, false); @@ -139,6 +164,9 @@ void PDFViewerWidget::open_file(int fd, String const& path) m_go_to_prev_page_action->set_enabled(false); m_go_to_next_page_action->set_enabled(document->get_page_count() > 1); m_toggle_sidebar_action->set_enabled(true); + m_zoom_in_action->set_enabled(true); + m_zoom_out_action->set_enabled(true); + m_reset_zoom_action->set_enabled(true); if (document->outline()) { auto outline = document->outline(); diff --git a/Userland/Applications/PDFViewer/PDFViewerWidget.h b/Userland/Applications/PDFViewer/PDFViewerWidget.h index 6fff990536..92840d3852 100644 --- a/Userland/Applications/PDFViewer/PDFViewerWidget.h +++ b/Userland/Applications/PDFViewer/PDFViewerWidget.h @@ -35,6 +35,10 @@ private: RefPtr<GUI::Action> m_go_to_prev_page_action; RefPtr<GUI::Action> m_go_to_next_page_action; RefPtr<GUI::Action> m_toggle_sidebar_action; + RefPtr<GUI::Action> m_zoom_in_action; + RefPtr<GUI::Action> m_zoom_out_action; + RefPtr<GUI::Action> m_reset_zoom_action; + bool m_sidebar_open { false }; ByteBuffer m_buffer; }; |