Browse Source

Ladybird: Implement zoom :^)

Linus Groh 2 years ago
parent
commit
0cc151bc1c
4 changed files with 82 additions and 0 deletions
  1. 38 0
      Ladybird/BrowserWindow.cpp
  2. 4 0
      Ladybird/BrowserWindow.h
  3. 30 0
      Ladybird/WebContentView.cpp
  4. 10 0
      Ladybird/WebContentView.h

+ 38 - 0
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();
+}

+ 4 - 0
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 = "");

+ 30 - 0
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);

+ 10 - 0
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 };