remove unnecessary reflows when adding widgets

This commit is contained in:
hippoz 2022-07-10 04:30:42 +03:00
parent 45e322f947
commit 4f35de353e
No known key found for this signature in database
GPG key ID: 7C52899193467641
4 changed files with 23 additions and 19 deletions

View file

@ -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() {

View file

@ -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() {

View file

@ -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<Widget> 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;
}

View file

@ -44,7 +44,7 @@ public:
std::function<void(Event&)> 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) {}