mirror of
https://github.com/ArthurSonzogni/FTXUI.git
synced 2025-07-21 19:11:12 +08:00
Added a callback on selection change
This commit is contained in:
parent
70a6a04e80
commit
dc70091203
@ -27,12 +27,18 @@ Element LoremIpsum() {
|
|||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
auto screen = ScreenInteractive::TerminalOutput();
|
auto screen = ScreenInteractive::TerminalOutput();
|
||||||
|
int counter = 0;
|
||||||
|
|
||||||
|
screen.onSelectionModified([&]{
|
||||||
|
counter++;
|
||||||
|
});
|
||||||
|
|
||||||
auto quit = Button("Quit", screen.ExitLoopClosure());
|
auto quit = Button("Quit", screen.ExitLoopClosure());
|
||||||
|
|
||||||
// The components:
|
// The components:
|
||||||
auto renderer = Renderer(quit, [&] {
|
auto renderer = Renderer(quit, [&] {
|
||||||
return vbox({
|
return vbox({
|
||||||
|
text("Select: " + std::to_string(counter)),
|
||||||
window(text("Horizontal split"), hbox({
|
window(text("Horizontal split"), hbox({
|
||||||
LoremIpsum(),
|
LoremIpsum(),
|
||||||
separator(),
|
separator(),
|
||||||
|
@ -70,6 +70,8 @@ class ScreenInteractive : public Screen {
|
|||||||
|
|
||||||
// Selection API.
|
// Selection API.
|
||||||
// TODO: Implement somethings here.
|
// TODO: Implement somethings here.
|
||||||
|
std::string GetSelectedContent(void);
|
||||||
|
void onSelectionModified(std::function<void(void)> callback);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void ExitNow();
|
void ExitNow();
|
||||||
@ -141,6 +143,8 @@ class ScreenInteractive : public Screen {
|
|||||||
int selection_start_y_ = 0;
|
int selection_start_y_ = 0;
|
||||||
int selection_end_x_ = 0;
|
int selection_end_x_ = 0;
|
||||||
int selection_end_y_ = 0;
|
int selection_end_y_ = 0;
|
||||||
|
bool selection_changed = false;
|
||||||
|
std::function<void(void)> selection_changed_callback_ = nullptr;
|
||||||
|
|
||||||
friend class Loop;
|
friend class Loop;
|
||||||
|
|
||||||
|
@ -576,6 +576,21 @@ void ScreenInteractive::ForceHandleCtrlZ(bool force) {
|
|||||||
force_handle_ctrl_z_ = 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.
|
/// @brief Return the currently active screen, or null if none.
|
||||||
// static
|
// static
|
||||||
ScreenInteractive* ScreenInteractive::Active() {
|
ScreenInteractive* ScreenInteractive::Active() {
|
||||||
@ -828,6 +843,8 @@ void ScreenInteractive::HandleTask(Component component, Task& task) {
|
|||||||
|
|
||||||
// private
|
// private
|
||||||
bool ScreenInteractive::HandleSelection(Event event) {
|
bool ScreenInteractive::HandleSelection(Event event) {
|
||||||
|
selection_changed = false;
|
||||||
|
|
||||||
if (!event.is_mouse()) {
|
if (!event.is_mouse()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -843,6 +860,8 @@ bool ScreenInteractive::HandleSelection(Event event) {
|
|||||||
selection_start_y_ = mouse.y;
|
selection_start_y_ = mouse.y;
|
||||||
selection_end_x_ = mouse.x;
|
selection_end_x_ = mouse.x;
|
||||||
selection_end_y_ = mouse.y;
|
selection_end_y_ = mouse.y;
|
||||||
|
|
||||||
|
selection_changed = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!selection_pending_) {
|
if (!selection_pending_) {
|
||||||
@ -850,8 +869,13 @@ bool ScreenInteractive::HandleSelection(Event event) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (mouse.motion == Mouse::Moved) {
|
if (mouse.motion == Mouse::Moved) {
|
||||||
|
if((mouse.x != selection_end_x_) || (mouse.y != selection_end_y_)) {
|
||||||
selection_end_x_ = mouse.x;
|
selection_end_x_ = mouse.x;
|
||||||
selection_end_y_ = mouse.y;
|
selection_end_y_ = mouse.y;
|
||||||
|
|
||||||
|
selection_changed = true;
|
||||||
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -859,6 +883,8 @@ bool ScreenInteractive::HandleSelection(Event event) {
|
|||||||
selection_pending_ = nullptr;
|
selection_pending_ = nullptr;
|
||||||
selection_end_x_ = mouse.x;
|
selection_end_x_ = mouse.x;
|
||||||
selection_end_y_ = mouse.y;
|
selection_end_y_ = mouse.y;
|
||||||
|
|
||||||
|
selection_changed = true;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -944,6 +970,11 @@ void ScreenInteractive::Draw(Component component) {
|
|||||||
selection_end_x_, selection_end_y_);
|
selection_end_x_, selection_end_y_);
|
||||||
Render(*this, document.get(), selection);
|
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.
|
// Set cursor position for user using tools to insert CJK characters.
|
||||||
{
|
{
|
||||||
const int dx = dimx_ - 1 - cursor_.x + int(dimx_ != terminal.dimx);
|
const int dx = dimx_ - 1 - cursor_.x + int(dimx_ != terminal.dimx);
|
||||||
|
Loading…
Reference in New Issue
Block a user