diff options
-rw-r--r-- | Ladybird/BrowserWindow.cpp | 38 | ||||
-rw-r--r-- | Ladybird/BrowserWindow.h | 4 | ||||
-rw-r--r-- | Ladybird/WebContentView.cpp | 30 | ||||
-rw-r--r-- | Ladybird/WebContentView.h | 10 |
4 files changed, 82 insertions, 0 deletions
diff --git a/Ladybird/BrowserWindow.cpp b/Ladybird/BrowserWindow.cpp index 43733d525c..81e3689c5b 100644 --- a/Ladybird/BrowserWindow.cpp +++ b/Ladybird/BrowserWindow.cpp @@ -2,6 +2,7 @@ * Copyright (c) 2022, Andreas Kling <kling@serenityos.org> * Copyright (c) 2022, Matthew Costa <ucosty@gmail.com> * Copyright (c) 2022, Filiph Sandström <filiph.sandstrom@filfatstudios.com> + * Copyright (c) 2023, Linus Groh <linusg@serenityos.org> * * SPDX-License-Identifier: BSD-2-Clause */ @@ -65,6 +66,25 @@ BrowserWindow::BrowserWindow(Browser::CookieJar& cookie_jar, StringView webdrive view_menu->addSeparator(); + auto* zoom_menu = view_menu->addMenu("&Zoom"); + + auto* zoom_in_action = new QAction("Zoom &In", this); + zoom_in_action->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_Plus)); + zoom_menu->addAction(zoom_in_action); + QObject::connect(zoom_in_action, &QAction::triggered, this, &BrowserWindow::zoom_in); + + auto* zoom_out_action = new QAction("Zoom &Out", this); + zoom_out_action->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_Minus)); + zoom_menu->addAction(zoom_out_action); + QObject::connect(zoom_out_action, &QAction::triggered, this, &BrowserWindow::zoom_out); + + auto* reset_zoom_action = new QAction("&Reset Zoom", this); + reset_zoom_action->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_0)); + zoom_menu->addAction(reset_zoom_action); + QObject::connect(reset_zoom_action, &QAction::triggered, this, &BrowserWindow::reset_zoom); + + view_menu->addSeparator(); + auto* color_scheme_menu = view_menu->addMenu("&Color Scheme"); auto* color_scheme_group = new QActionGroup(this); @@ -416,3 +436,21 @@ void BrowserWindow::enable_dark_color_scheme() tab.view().set_color_scheme(ColorScheme::Dark); } } + +void BrowserWindow::zoom_in() +{ + if (m_current_tab) + m_current_tab->view().zoom_in(); +} + +void BrowserWindow::zoom_out() +{ + if (m_current_tab) + m_current_tab->view().zoom_out(); +} + +void BrowserWindow::reset_zoom() +{ + if (m_current_tab) + m_current_tab->view().reset_zoom(); +} diff --git a/Ladybird/BrowserWindow.h b/Ladybird/BrowserWindow.h index f1fb157120..4fb89cf198 100644 --- a/Ladybird/BrowserWindow.h +++ b/Ladybird/BrowserWindow.h @@ -1,5 +1,6 @@ /* * Copyright (c) 2022, Andreas Kling <kling@serenityos.org> + * Copyright (c) 2023, Linus Groh <linusg@serenityos.org> * * SPDX-License-Identifier: BSD-2-Clause */ @@ -42,6 +43,9 @@ public slots: void enable_auto_color_scheme(); void enable_light_color_scheme(); void enable_dark_color_scheme(); + void zoom_in(); + void zoom_out(); + void reset_zoom(); private: void debug_request(DeprecatedString const& request, DeprecatedString const& argument = ""); diff --git a/Ladybird/WebContentView.cpp b/Ladybird/WebContentView.cpp index 59be8b6737..5ff8f17704 100644 --- a/Ladybird/WebContentView.cpp +++ b/Ladybird/WebContentView.cpp @@ -1,5 +1,6 @@ /* * Copyright (c) 2022, Andreas Kling <kling@serenityos.org> + * Copyright (c) 2023, Linus Groh <linusg@serenityos.org> * * SPDX-License-Identifier: BSD-2-Clause */ @@ -575,6 +576,35 @@ void WebContentView::set_color_scheme(ColorScheme color_scheme) } } +void WebContentView::zoom_in() +{ + if (m_zoom_level >= ZOOM_MAX_LEVEL) + return; + m_zoom_level += ZOOM_STEP; + update_zoom(); +} + +void WebContentView::zoom_out() +{ + if (m_zoom_level <= ZOOM_MIN_LEVEL) + return; + m_zoom_level -= ZOOM_STEP; + update_zoom(); +} + +void WebContentView::reset_zoom() +{ + m_zoom_level = 1.0f; + update_zoom(); +} + +void WebContentView::update_zoom() +{ + client().async_set_device_pixels_per_css_pixel(m_device_pixel_ratio * m_zoom_level); + update_viewport_rect(); + request_repaint(); +} + void WebContentView::showEvent(QShowEvent* event) { QAbstractScrollArea::showEvent(event); diff --git a/Ladybird/WebContentView.h b/Ladybird/WebContentView.h index 85021cda87..d7a1615a20 100644 --- a/Ladybird/WebContentView.h +++ b/Ladybird/WebContentView.h @@ -113,6 +113,10 @@ public: void set_color_scheme(ColorScheme); + void zoom_in(); + void zoom_out(); + void reset_zoom(); + virtual void notify_server_did_layout(Badge<WebContentClient>, Gfx::IntSize content_size) override; virtual void notify_server_did_paint(Badge<WebContentClient>, i32 bitmap_id) override; virtual void notify_server_did_invalidate_content_rect(Badge<WebContentClient>, Gfx::IntRect const&) override; @@ -184,9 +188,14 @@ signals: Gfx::IntRect fullscreen_window(); private: + static constexpr auto ZOOM_MIN_LEVEL = 0.3f; + static constexpr auto ZOOM_MAX_LEVEL = 5.0f; + static constexpr auto ZOOM_STEP = 0.1f; + void request_repaint(); void update_viewport_rect(); void handle_resize(); + void update_zoom(); void ensure_js_console_widget(); void ensure_inspector_widget(); @@ -197,6 +206,7 @@ private: void close_sub_widgets(); ErrorOr<Ladybird::DOMNodeProperties> inspect_dom_node(i32 node_id, Optional<Web::CSS::Selector::PseudoElement> pseudo_element); + float m_zoom_level { 1.0 }; float m_device_pixel_ratio { 1.0 }; qreal m_inverse_pixel_scaling_ratio { 1.0 }; bool m_should_show_line_box_borders { false }; |