Quellcode durchsuchen

LibGUI: Add a way for GWidget subclasses to learn that the font changed

Use this in GTextEditor to update the vertical scrolling step size so
we always scroll one-line-at-a-time.
Andreas Kling vor 6 Jahren
Ursprung
Commit
3e2e086011

+ 6 - 0
Libraries/LibGUI/GTextEditor.cpp

@@ -1448,3 +1448,9 @@ void GTextEditor::add_custom_context_menu_action(GAction& action)
 {
     m_custom_context_menu_actions.append(action);
 }
+
+void GTextEditor::did_change_font()
+{
+    vertical_scrollbar().set_step(line_height());
+    GWidget::did_change_font();
+}

+ 3 - 0
Libraries/LibGUI/GTextEditor.h

@@ -165,6 +165,9 @@ public:
 
     void add_custom_context_menu_action(GAction&);
 
+protected:
+    virtual void did_change_font() override;
+
 private:
     virtual void paint_event(GPaintEvent&) override;
     virtual void mousedown_event(GMouseEvent&) override;

+ 8 - 3
Libraries/LibGUI/GWidget.cpp

@@ -14,8 +14,8 @@
 
 GWidget::GWidget(GWidget* parent)
     : CObject(parent, true)
+    , m_font(Font::default_font())
 {
-    set_font(nullptr);
     m_background_color = Color::WarmGray;
     m_foreground_color = Color::Black;
 }
@@ -372,12 +372,17 @@ void GWidget::set_focus(bool focus)
     }
 }
 
-void GWidget::set_font(Font* font)
+void GWidget::set_font(const Font* font)
 {
+    if (m_font.ptr() == font)
+        return;
+
     if (!font)
         m_font = Font::default_font();
     else
-        m_font = font;
+        m_font = *font;
+
+    did_change_font();
     update();
 }
 

+ 5 - 3
Libraries/LibGUI/GWidget.h

@@ -176,8 +176,8 @@ public:
     bool fill_with_background_color() const { return m_fill_with_background_color; }
 
     const Font& font() const { return *m_font; }
-    void set_font(Font*);
-    void set_font(Font& font) { set_font(&font); }
+    void set_font(const Font*);
+    void set_font(const Font& font) { set_font(&font); }
 
     void set_global_cursor_tracking(bool);
     bool global_cursor_tracking() const;
@@ -219,6 +219,8 @@ public:
 
     virtual void save_to(AK::JsonObject&) override;
 
+    virtual void did_change_font() {}
+
 private:
     void handle_paint_event(GPaintEvent&);
     void handle_resize_event(GResizeEvent&);
@@ -237,7 +239,7 @@ private:
     Rect m_relative_rect;
     Color m_background_color;
     Color m_foreground_color;
-    RefPtr<Font> m_font;
+    NonnullRefPtr<Font> m_font;
     String m_tooltip;
 
     SizePolicy m_horizontal_size_policy { SizePolicy::Fill };