diff --git a/libs/ui/include/psemek/ui/edit.hpp b/libs/ui/include/psemek/ui/edit.hpp index 59e1573e..0a2bdae5 100644 --- a/libs/ui/include/psemek/ui/edit.hpp +++ b/libs/ui/include/psemek/ui/edit.hpp @@ -53,6 +53,7 @@ namespace psemek::ui virtual void on_text_entered(callback_type callback, bool signal = true); virtual void on_start_input(std::function callback); + virtual void on_text_changed(callback_type callback); bool on_event(mouse_move const & e) override; bool on_event(mouse_click const & e) override; @@ -100,6 +101,7 @@ namespace psemek::ui callback_type on_text_entered_; std::function on_start_input_; + callback_type on_text_changed_; struct cached_state { @@ -114,6 +116,7 @@ namespace psemek::ui void reset_caret(); void post_text_entered() const; void post_start_input() const; + void post_text_changed() const; }; } diff --git a/libs/ui/source/edit.cpp b/libs/ui/source/edit.cpp index 179373f2..d74d55c7 100644 --- a/libs/ui/source/edit.cpp +++ b/libs/ui/source/edit.cpp @@ -21,6 +21,7 @@ namespace psemek::ui text_ = std::move(text); caret_ = std::min(caret_, text_.size()); on_state_changed(); + post_text_changed(); if (signal) post_text_entered(); @@ -93,6 +94,11 @@ namespace psemek::ui on_start_input_ = std::move(callback); } + void edit::on_text_changed(callback_type callback) + { + on_text_changed_ = std::move(callback); + } + bool edit::on_event(mouse_move const & e) { auto m = geom::cast(e.position); @@ -438,4 +444,15 @@ namespace psemek::ui post(on_start_input_); } + void edit::post_text_changed() const + { + if (on_text_changed_) + { + post([weak_self = weak_from_this(), cb = on_text_changed_]{ + if (auto self = weak_self.lock()) + cb(static_cast(self.get())->text_); + }); + } + } + }