diff --git a/src/Button.cpp b/src/Button.cpp index e559250..992154d 100644 --- a/src/Button.cpp +++ b/src/Button.cpp @@ -8,26 +8,18 @@ namespace Raven { -void Button::recompute_text_size() { - window()->painter().set_pango_font_description(styles()->controls_font_description()); - auto size = window()->painter().compute_text_size(current_geometry(), m_text); - if (!resize(size)) { - wants_full_relayout(); - } -} - void Button::set_text(std::string text) { m_text = text; if (window()) { - recompute_text_size(); + fit_text(text); } } void Button::on_init() { set_background_fill_color(styles()->button_normal_color()); set_do_background_fill(true); - recompute_text_size(); + fit_text(m_text); set_did_init(true); } diff --git a/src/Label.cpp b/src/Label.cpp index 91e537a..d336837 100644 --- a/src/Label.cpp +++ b/src/Label.cpp @@ -5,12 +5,17 @@ namespace Raven { -void Label::on_init() { - // the label will inherit the background color properties from the parent - if (parent()) { - set_do_background_fill(parent()->do_background_fill()); - set_background_fill_color(parent()->background_fill_color()); +void Label::set_text(std::string text) { + m_text = text; + + if (window()) { + fit_text(text); } +} + +void Label::on_init() { + fit_text(m_text); + set_did_init(true); } @@ -19,14 +24,11 @@ void Label::on_paint() { auto painter = window()->painter(); auto text_color = styles()->label_text_color(); - auto text_geometry = current_geometry().max_geometry(); - bool set_size = current_geometry().max_width() != -1 && current_geometry().max_height() != -1; painter.source_rgb(text_color); painter.set_pango_font_description(styles()->controls_font_description()); - auto point = painter.text(text_geometry, m_text, PaintTextAlign::Left, PANGO_ELLIPSIZE_NONE, set_size); - resize(point); + painter.text(current_geometry(), m_text, PaintTextAlign::Left, PANGO_ELLIPSIZE_NONE, true); painter.fill(); } -} \ No newline at end of file +} diff --git a/src/Label.hpp b/src/Label.hpp index 4665a9f..c2486d7 100644 --- a/src/Label.hpp +++ b/src/Label.hpp @@ -16,7 +16,7 @@ public: ~Label() {} std::string &text() { return m_text; } - void set_text(std::string text) { m_text = text; wants_repaint(); } + void set_text(std::string text); protected: void on_paint(); void on_init(); diff --git a/src/Widget.cpp b/src/Widget.cpp index 7bc692d..60b43ad 100644 --- a/src/Widget.cpp +++ b/src/Widget.cpp @@ -9,6 +9,17 @@ namespace Raven { +void Widget::fit_text(std::string &text) { + if (!window()) + return; + + window()->painter().set_pango_font_description(styles()->controls_font_description()); + auto size = window()->painter().compute_text_size(current_geometry(), text); + if (!resize(size)) { + wants_full_relayout(); + } +} + bool Widget::resize(double width, double height) { if (m_current_geometry.width() == width && m_current_geometry.height() == height) return false; diff --git a/src/Widget.hpp b/src/Widget.hpp index f0fe571..98fd363 100644 --- a/src/Widget.hpp +++ b/src/Widget.hpp @@ -62,6 +62,8 @@ public: void set_current_geometry(Box current_geometry) { m_current_geometry = current_geometry; wants_full_relayout(); } void set_current_geometry(Box current_geometry, bool is_pure) { m_current_geometry = current_geometry; if (!is_pure) { wants_full_relayout(); } } + void fit_text(std::string &text); + void move_to(double x, double y); bool resize(double width, double height); bool resize(Point &point) { return resize(point.x(), point.y()); } diff --git a/src/main.cpp b/src/main.cpp index 3d41aa6..82e7e40 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -20,7 +20,8 @@ int main() { int number = 0; for (int i = 0; i < 500; i++) { - auto button = main_widget->add("0"); + auto button = main_widget->add("text "); + /* button->on_click = [&]() { number++; @@ -33,6 +34,7 @@ int main() { } window.end_batch(); }; + */ } window.run(true);