Added a callback on selection change

This commit is contained in:
Clement Roblot 2024-12-02 15:40:50 +07:00 committed by ArthurSonzogni
parent 70a6a04e80
commit dc70091203
No known key found for this signature in database
GPG Key ID: 41D98248C074CD6C
3 changed files with 43 additions and 2 deletions

View File

@ -27,12 +27,18 @@ Element LoremIpsum() {
int main() {
auto screen = ScreenInteractive::TerminalOutput();
int counter = 0;
screen.onSelectionModified([&]{
counter++;
});
auto quit = Button("Quit", screen.ExitLoopClosure());
// The components:
auto renderer = Renderer(quit, [&] {
return vbox({
text("Select: " + std::to_string(counter)),
window(text("Horizontal split"), hbox({
LoremIpsum(),
separator(),

View File

@ -70,6 +70,8 @@ class ScreenInteractive : public Screen {
// Selection API.
// TODO: Implement somethings here.
std::string GetSelectedContent(void);
void onSelectionModified(std::function<void(void)> callback);
private:
void ExitNow();
@ -141,6 +143,8 @@ class ScreenInteractive : public Screen {
int selection_start_y_ = 0;
int selection_end_x_ = 0;
int selection_end_y_ = 0;
bool selection_changed = false;
std::function<void(void)> selection_changed_callback_ = nullptr;
friend class Loop;

View File

@ -576,6 +576,21 @@ void ScreenInteractive::ForceHandleCtrlZ(bool force) {
force_handle_ctrl_z_ = force;
}
/// @brief Returns the content of the current selection
std::string ScreenInteractive::GetSelectedContent(void)
{
}
/// @brief Sets a callback on modifications of the selection
/// This callback is called when the start of end of the selection moved.
/// Not when the content/characters inside of the selection change.
/// @param callback The function to callback on modifications of the selection.
void ScreenInteractive::onSelectionModified(std::function<void(void)> callback)
{
selection_changed_callback_ = std::move(callback);
}
/// @brief Return the currently active screen, or null if none.
// static
ScreenInteractive* ScreenInteractive::Active() {
@ -828,6 +843,8 @@ void ScreenInteractive::HandleTask(Component component, Task& task) {
// private
bool ScreenInteractive::HandleSelection(Event event) {
selection_changed = false;
if (!event.is_mouse()) {
return false;
}
@ -843,6 +860,8 @@ bool ScreenInteractive::HandleSelection(Event event) {
selection_start_y_ = mouse.y;
selection_end_x_ = mouse.x;
selection_end_y_ = mouse.y;
selection_changed = true;
}
if (!selection_pending_) {
@ -850,8 +869,13 @@ bool ScreenInteractive::HandleSelection(Event event) {
}
if (mouse.motion == Mouse::Moved) {
if((mouse.x != selection_end_x_) || (mouse.y != selection_end_y_)) {
selection_end_x_ = mouse.x;
selection_end_y_ = mouse.y;
selection_changed = true;
}
return true;
}
@ -859,6 +883,8 @@ bool ScreenInteractive::HandleSelection(Event event) {
selection_pending_ = nullptr;
selection_end_x_ = mouse.x;
selection_end_y_ = mouse.y;
selection_changed = true;
return true;
}
@ -944,6 +970,11 @@ void ScreenInteractive::Draw(Component component) {
selection_end_x_, selection_end_y_);
Render(*this, document.get(), selection);
if((selection_changed == true) && (selection_changed_callback_ != nullptr))
{
selection_changed_callback_();
}
// Set cursor position for user using tools to insert CJK characters.
{
const int dx = dimx_ - 1 - cursor_.x + int(dimx_ != terminal.dimx);