diff --git a/examples/ui.cpp b/examples/ui.cpp index 94a79d61..5c767c89 100644 --- a/examples/ui.cpp +++ b/examples/ui.cpp @@ -19,6 +19,16 @@ using namespace psemek; static std::string const lorem_ipsum = R"(Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.)"; +static std::string const to_be = +R"(To be, or not to be, that is the question: +Whether 'tis nobler in the mind to suffer +The slings and arrows of outrageous fortune, +Or to take Arms against a Sea of troubles, +And by opposing end them: to die, to sleep; +No more; and by a sleep, to say we end +The heart-ache, and the thousand natural shocks +That Flesh is heir to? 'Tis a consummation)"; + struct ui_example : app::app { @@ -42,18 +52,18 @@ struct ui_example // auto text = element_factory.make_label(lorem_ipsum); // screen->add(text, ui::screen::x_policy::center, ui::screen::y_policy::center); - auto text = element_factory.make_label(lorem_ipsum); + auto text = element_factory.make_label(to_be); text->set_overflow(ui::label::overflow_mode::ellipsis); text->set_multiline(ui::label::multiline_mode::minimize_lines); auto frame = element_factory.make_frame(); frame->set_child(text); - frame->set_fixed_size({200.f, 200.f}); + frame->set_fixed_size({200.f, 300.f}); screen->add(frame, ui::screen::x_policy::center, ui::screen::y_policy::center); auto updater = util::recursive([this, frame, i = 0](auto && self) mutable -> void { - frame->set_fixed_size({200.f + i * 2.f, 200.f}); + frame->set_fixed_size({200.f + i * 2.f, 300.f}); ++i; - loop.post_at(async::clock::now() + std::chrono::milliseconds{20}, self); + loop.post_at(async::clock::now() + std::chrono::milliseconds{10}, self); }); updater(); diff --git a/libs/ui/source/label.cpp b/libs/ui/source/label.cpp index aeabc534..0758aabe 100644 --- a/libs/ui/source/label.cpp +++ b/libs/ui/source/label.cpp @@ -75,6 +75,11 @@ namespace psemek::ui post_reshape(); } + static bool is_newline(char32_t c) + { + return (c == '\n') || (c == '\r'); + } + void label::update_cached_state() const { cached_state_ = cached_state{}; @@ -126,6 +131,9 @@ namespace psemek::ui geom::interval x_range; while (line_end < glyphs.size()) { + if (is_newline(glyphs[line_end].character)) + break; + x_range |= glyphs[line_end].position[0]; if (x_range.length() > shape_.box[0].length())