Skip to content

Commit

Permalink
Add a DisableMouseEvents() on ScreenInteractive to disable mouse events
Browse files Browse the repository at this point in the history
When mouse events are enabled, it is not possible to select text
in the terminal and copy it somewhere else. This could be usefull
for some applications if they don't need to handle mouse events.

Add a function on the ScreenInteractive class to disable grabbing
of mouse events so that it is e.g. possible to select text in the
user interface. The function needs to be called on the screen object
before starting the application loop if such a behaviour is desired.
  • Loading branch information
StefanRvO committed Aug 16, 2023
1 parent acbdb50 commit a9890af
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
7 changes: 7 additions & 0 deletions include/ftxui/component/screen_interactive.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ class ScreenInteractive : public Screen {
// temporarily uninstalled.
Closure WithRestoredIO(Closure);

// Disable all mouse events. This will make it possible to select
// parts of the screen in a terminal, and e.g. copy and paste it.
// It has to be called before loop construction.
void DisableMouseEvents();

private:
void ExitNow();

Expand Down Expand Up @@ -104,6 +109,8 @@ class ScreenInteractive : public Screen {

bool frame_valid_ = false;

bool mouse_events_enabled = true;

friend class Loop;

public:
Expand Down
14 changes: 10 additions & 4 deletions src/ftxui/component/screen_interactive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,10 @@ Closure ScreenInteractive::WithRestoredIO(Closure fn) { // NOLINT
};
}

void ScreenInteractive::DisableMouseEvents() {
mouse_events_enabled = false;
}

// static
ScreenInteractive* ScreenInteractive::Active() {
return g_active_screen;
Expand Down Expand Up @@ -580,10 +584,12 @@ void ScreenInteractive::Install() {
DECMode::kLineWrap,
});

enable({DECMode::kMouseVt200});
enable({DECMode::kMouseAnyEvent});
enable({DECMode::kMouseUrxvtMode});
enable({DECMode::kMouseSgrExtMode});
if (mouse_events_enabled) {
enable({DECMode::kMouseVt200});
enable({DECMode::kMouseAnyEvent});
enable({DECMode::kMouseUrxvtMode});
enable({DECMode::kMouseSgrExtMode});
}

// After installing the new configuration, flush it to the terminal to
// ensure it is fully applied:
Expand Down

0 comments on commit a9890af

Please sign in to comment.