make fitting widgets to text a generic function and add it to label

This commit is contained in:
hippoz 2022-05-14 13:05:52 +03:00
parent 8251532938
commit fbece08638
No known key found for this signature in database
GPG key ID: 7C52899193467641
6 changed files with 31 additions and 22 deletions

View file

@ -8,26 +8,18 @@
namespace Raven { 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) { void Button::set_text(std::string text) {
m_text = text; m_text = text;
if (window()) { if (window()) {
recompute_text_size(); fit_text(text);
} }
} }
void Button::on_init() { void Button::on_init() {
set_background_fill_color(styles()->button_normal_color()); set_background_fill_color(styles()->button_normal_color());
set_do_background_fill(true); set_do_background_fill(true);
recompute_text_size(); fit_text(m_text);
set_did_init(true); set_did_init(true);
} }

View file

@ -5,12 +5,17 @@
namespace Raven { namespace Raven {
void Label::on_init() { void Label::set_text(std::string text) {
// the label will inherit the background color properties from the parent m_text = text;
if (parent()) {
set_do_background_fill(parent()->do_background_fill()); if (window()) {
set_background_fill_color(parent()->background_fill_color()); fit_text(text);
} }
}
void Label::on_init() {
fit_text(m_text);
set_did_init(true); set_did_init(true);
} }
@ -19,13 +24,10 @@ void Label::on_paint() {
auto painter = window()->painter(); auto painter = window()->painter();
auto text_color = styles()->label_text_color(); 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.source_rgb(text_color);
painter.set_pango_font_description(styles()->controls_font_description()); painter.set_pango_font_description(styles()->controls_font_description());
auto point = painter.text(text_geometry, m_text, PaintTextAlign::Left, PANGO_ELLIPSIZE_NONE, set_size); painter.text(current_geometry(), m_text, PaintTextAlign::Left, PANGO_ELLIPSIZE_NONE, true);
resize(point);
painter.fill(); painter.fill();
} }

View file

@ -16,7 +16,7 @@ public:
~Label() {} ~Label() {}
std::string &text() { return m_text; } std::string &text() { return m_text; }
void set_text(std::string text) { m_text = text; wants_repaint(); } void set_text(std::string text);
protected: protected:
void on_paint(); void on_paint();
void on_init(); void on_init();

View file

@ -9,6 +9,17 @@
namespace Raven { 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) { bool Widget::resize(double width, double height) {
if (m_current_geometry.width() == width && m_current_geometry.height() == height) if (m_current_geometry.width() == width && m_current_geometry.height() == height)
return false; return false;

View file

@ -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) { 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 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); void move_to(double x, double y);
bool resize(double width, double height); bool resize(double width, double height);
bool resize(Point &point) { return resize(point.x(), point.y()); } bool resize(Point &point) { return resize(point.x(), point.y()); }

View file

@ -20,7 +20,8 @@ int main() {
int number = 0; int number = 0;
for (int i = 0; i < 500; i++) { for (int i = 0; i < 500; i++) {
auto button = main_widget->add<Raven::Button>("0"); auto button = main_widget->add<Raven::Label>("text ");
/*
button->on_click = [&]() { button->on_click = [&]() {
number++; number++;
@ -33,6 +34,7 @@ int main() {
} }
window.end_batch(); window.end_batch();
}; };
*/
} }
window.run(true); window.run(true);