From 4f35de353e258f462b713613d8a23fd5fd08f183 Mon Sep 17 00:00:00 2001 From: hippoz <10706925-hippoz@users.noreply.gitlab.com> Date: Sun, 10 Jul 2022 04:30:42 +0300 Subject: [PATCH] remove unnecessary reflows when adding widgets --- src/Button.cpp | 11 +++++++---- src/Label.cpp | 11 +++++++---- src/Widget.cpp | 13 +++++-------- src/Widget.hpp | 7 ++++--- 4 files changed, 23 insertions(+), 19 deletions(-) diff --git a/src/Button.cpp b/src/Button.cpp index 6eec3c4..fe675e2 100644 --- a/src/Button.cpp +++ b/src/Button.cpp @@ -12,15 +12,18 @@ namespace Raven { void Button::set_text(std::string text) { m_text = text; - if (window()) { - fit_text(text); + if (!fit_text(text)) { + repaint(); } } void Button::on_init() { - set_style(&default_button_style); - fit_text(m_text); + set_style_pure(&default_button_style); + set_did_init(true); + if (!fit_text(m_text)) { + reflow(); + } } void Button::on_paint() { diff --git a/src/Label.cpp b/src/Label.cpp index 49887dc..3e6ba63 100644 --- a/src/Label.cpp +++ b/src/Label.cpp @@ -9,15 +9,18 @@ namespace Raven { void Label::set_text(std::string text) { m_text = text; - if (window()) { - fit_text(text); + if (!fit_text(text)) { + repaint(); } } void Label::on_init() { - set_style(&default_label_style); - fit_text(m_text); + set_style_pure(&default_label_style); + set_did_init(true); + if (!fit_text(m_text)) { + reflow(); + } } void Label::on_paint() { diff --git a/src/Widget.cpp b/src/Widget.cpp index 42eec1c..0854689 100644 --- a/src/Widget.cpp +++ b/src/Widget.cpp @@ -17,15 +17,12 @@ Point Widget::compute_window_relative() { return point; } -void Widget::fit_text(std::string &text) { +bool Widget::fit_text(std::string &text) { if (!window()) - return; + return false; auto size = window()->painter().compute_text_size(rect(), text, style()->font_description()); - // always repaint, even if resize doesn't reflow - if (!resize(size)) { - repaint(); - } + return resize(size); } bool Widget::resize(double width, double height) { @@ -89,10 +86,10 @@ bool Widget::add_child(std::shared_ptr child) { child->set_parent(this); // children inherit the window from the parent - // TODO?: what happens when the parent changes its window? + // NOTE: we're no longer calling reflow() in this function, as we'll rely on the child widget to reflow + // after we call set_window() on it. child->set_window(m_window); - reflow(); return true; } diff --git a/src/Widget.hpp b/src/Widget.hpp index 669560d..3e882c6 100644 --- a/src/Widget.hpp +++ b/src/Widget.hpp @@ -44,7 +44,7 @@ public: std::function on_event { [](Event&){} }; - void fit_text(std::string &text); + bool fit_text(std::string &text); void move_to(double x, double y); bool resize(double width, double height); @@ -57,7 +57,7 @@ public: Box &rect() { return m_rect; } void set_rect(Box rect) { m_rect = rect; reflow(); } - void set_rect(Box rect, bool is_pure) { m_rect = rect; if (!is_pure) { reflow(); } } + void set_rect_pure(Box rect) { m_rect = rect; } Point &window_relative() { return m_window_relative; }; @@ -72,6 +72,7 @@ public: GenericStyle *style() { return m_style; } void set_style(GenericStyle *style) { m_style = style; reflow(); } + void set_style_pure(GenericStyle *style) { m_style = style; } bool did_init() { return m_did_init; } bool is_focused() { return m_is_focused; } @@ -107,7 +108,7 @@ public: protected: WidgetType m_type { WidgetType::Widget }; - virtual void on_init() { set_did_init(true); } + virtual void on_init() { set_did_init(true); reflow(); } virtual void on_mouse_button(MouseButtonEvent &event) {} virtual void on_mouse_move(MouseMoveEvent &event) {} virtual void on_focus_update(FocusUpdateEvent &event) {}