Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable AppFocus and AppBlur in terminal emulators #4265

Merged
merged 15 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/textual/_xterm_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
)
_re_bracketed_paste_start = re.compile(r"^\x1b\[200~$")
_re_bracketed_paste_end = re.compile(r"^\x1b\[201~$")
_re_focusin = re.compile(r"^\x1b\[I$")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't look like this needs to be a regex. Could it not be a simple string?

Ditto for the bracketed paste stuff. Unless I'm missing something, it looks like they could also just be strings.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was going to ask you that very question in the morning, oddly enough. Couldn't for the life of me see why the bracketed paste checking used a regex but followed the pattern with a mental note to ask why it was done that way (although looking at fe151a7 it seems I'd have been asking the wrong person anyway).

If there was no reason I'll make them simple strings; makes way more sense to me.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably just following a pattern. But yeah, let's go with strings. And if you wouldn't mind changing the bracketed paste ones while you are there...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do.

_re_focusout = re.compile(r"^\x1b\[O$")


class XTermParser(Parser[events.Event]):
Expand Down Expand Up @@ -202,6 +204,14 @@ def reissue_sequence_as_keys(reissue_sequence: str) -> None:

self.debug_log(f"sequence={sequence!r}")

if _re_focusin.match(sequence):
on_token(events.AppFocus())
break

if _re_focusout.match(sequence):
on_token(events.AppBlur())
break

bracketed_paste_start_match = _re_bracketed_paste_start.match(
sequence
)
Expand Down
2 changes: 2 additions & 0 deletions src/textual/drivers/linux_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ def on_terminal_resize(signum, stack) -> None:

self.write("\x1b[?25l") # Hide cursor
self.write("\033[?1003h\n")
self.write("\033[?1004h\n") # Enable FocusIn/FocusOut.
self.flush()
self._key_thread = Thread(target=self._run_input_thread)
send_size_event()
Expand Down Expand Up @@ -316,6 +317,7 @@ def stop_application_mode(self) -> None:

# Alt screen false, show cursor
self.write("\x1b[?1049l" + "\x1b[?25h")
self.write("\033[?1004l\n") # Disable FocusIn/FocusOut.
self.flush()

def close(self) -> None:
Expand Down
Loading