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

Input and TextArea have cursor blinking even though terminal window is not focused #4263

Closed
willmcgugan opened this issue Mar 6, 2024 Discussed in #4259 · 8 comments · Fixed by #4265
Closed

Input and TextArea have cursor blinking even though terminal window is not focused #4263

willmcgugan opened this issue Mar 6, 2024 Discussed in #4259 · 8 comments · Fixed by #4265
Assignees
Labels
enhancement New feature or request Task

Comments

@willmcgugan
Copy link
Collaborator

Discussed in #4259

Originally posted by villekr March 5, 2024
Following simple app has Input and TextArea widgets. Focus transitions between them as expected. However, when I focus some other window than iTerm2 running textual app then cursor still keeps on blinking on the previously focused widget. I would expect that by default focus is removed alltogether from app when other window gets focus. Or is this something app needs to be specifically configured?

I added also _on_app_focus annd _on_app_blur event handlers for debugging, but they are not invoked. Maybe these are only relevant for textual-web based on comments (# Required by textual-web to manage focus in a web page.)

As this might be platform/terminal related here's the configuration I have.

macOS Sonoma: Version 14.3
iTerm2: Build 3.4.23
Python 3.12
Textual 0.52.1

from textual import events
from textual.app import App, ComposeResult
from textual.containers import Vertical
from textual.widgets import Input, TextArea


class AppFocus(App):

    def compose(self) -> ComposeResult:
        with Vertical():
            yield TextArea()
            yield Input()


if __name__ == "__main__":
    AppFocus().run()

```</div>
@willmcgugan
Copy link
Collaborator Author

We should implement these ANSI codes. Should be straightforward in theory. Enable the escape sequences, and convert them to AppFocus and AppBlur.

Talk to @willmcgugan if more clarification is needed.

@davep davep self-assigned this Mar 6, 2024
@davep davep added enhancement New feature or request Task labels Mar 6, 2024
@davep
Copy link
Contributor

davep commented Mar 6, 2024

With an eye on the motivation for this: it would seem that there's benefit to the widgets in the focus chain knowing when AppFocus and AppBlur happens; I wonder if perhaps those events should be posted to the focused widget and allowed to bubble?

@willmcgugan
Copy link
Collaborator Author

What is the benefit?

@davep
Copy link
Contributor

davep commented Mar 6, 2024

Just thinking about if there's utility for it when it comes to a random developer wanting to implement functionality on a widget that's related to having focus or not, and wanting to take into account if there emulator window has focus (while I recognise that it's not done with Focus and Blur with Input and TextArea, I'm thinking about any situations we've not considered yet that might be akin to the original motivation for the question that resulted in this issue).

@willmcgugan
Copy link
Collaborator Author

The behaviour (in Textual-web) is that when the window loses focus, the focused widget is blurred. When the window regains focus, the previously blurred widget is focused again.

This feels like an extension of the regular focus mechanism. I can't think why a developer would want to do something different. You can always check if the windows as focus with self.app.app_focus.

@davep
Copy link
Contributor

davep commented Mar 6, 2024

The behaviour (in Textual-web) is that when the window loses focus, the focused widget is blurred. When the window regains focus, the previously blurred widget is focused again.

Ahh, cool, so it seems this is already taken care of anyway? Nice.

@davep
Copy link
Contributor

davep commented Mar 6, 2024

Something to consider, perhaps as part of this, or as a fresh issue: it seems that the current implementation of AppBlur and AppFocus, as seen in textual-web, causes focus to be fully lost. For example, while testing with this code:

from pathlib import Path
from textual.app import App, ComposeResult
from textual.widgets import Digits, Input, TextArea

class AppFocusBlurApp(App[None]):

    def compose(self) -> ComposeResult:
        yield Digits("")
        yield Input(placeholder="Here's an input to help test")
        yield TextArea(Path(__file__).read_text(), language="python")

    def update_digits(self) -> None:
        self.query_one(Digits).update(
            ("1" if self.app_focus else "0") * (self.size.width // 3)
        )

    def on_mount(self) -> None:
        self.update_digits()

    def watch_app_focus(self) -> None:
        self.update_digits()

if __name__ == "__main__":
    AppFocusBlurApp().run()

if I focus the Input, Cmd-tab away to another window, then Cmd-tab back, I've lost my place in the form. It would be normal to expect focus to settle back where it was before once the app is focused again.

davep added a commit to davep/textual-sandbox that referenced this issue Mar 6, 2024
@davep davep linked a pull request Mar 7, 2024 that will close this issue
@davep davep assigned willmcgugan and davep and unassigned davep and willmcgugan Mar 11, 2024
Copy link

Don't forget to star the repository!

Follow @textualizeio for Textual updates.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request Task
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants