mirror of
https://github.com/Textualize/textual.git
synced 2025-10-17 02:38:12 +03:00
Add auto_focus attribute to screens.
This commit is contained in:
@@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file.
|
||||
The format is based on [Keep a Changelog](http://keepachangelog.com/)
|
||||
and this project adheres to [Semantic Versioning](http://semver.org/).
|
||||
|
||||
|
||||
## Unreleased
|
||||
|
||||
### Added
|
||||
|
||||
- Attribute `auto_focus` to screens https://github.com/Textualize/textual/issues/2457
|
||||
|
||||
## [0.24.1] - 2023-05-08
|
||||
|
||||
### Fixed
|
||||
|
||||
@@ -30,7 +30,7 @@ from ._types import CallbackType
|
||||
from .binding import Binding
|
||||
from .css.match import match
|
||||
from .css.parse import parse_selectors
|
||||
from .css.query import QueryType
|
||||
from .css.query import NoMatches, QueryType
|
||||
from .dom import DOMNode
|
||||
from .geometry import Offset, Region, Size
|
||||
from .reactive import Reactive
|
||||
@@ -101,6 +101,12 @@ class Screen(Generic[ScreenResultType], Widget):
|
||||
}
|
||||
"""
|
||||
|
||||
auto_focus: str | None = "*"
|
||||
"""A selector to determine what to focus automatically when the screen is activated.
|
||||
|
||||
The widget focused is the first that matches the given [CSS selector](/guide/queries/#query-selectors).
|
||||
Set to `None` to disable auto focus.
|
||||
"""
|
||||
focused: Reactive[Widget | None] = Reactive(None)
|
||||
"""The focused [widget][textual.widget.Widget] or `None` for no focus."""
|
||||
stack_updates: Reactive[int] = Reactive(0, repaint=False)
|
||||
@@ -659,6 +665,13 @@ class Screen(Generic[ScreenResultType], Widget):
|
||||
"""Screen has resumed."""
|
||||
self.stack_updates += 1
|
||||
size = self.app.size
|
||||
if self.auto_focus is not None and self.focused is None:
|
||||
try:
|
||||
to_focus = self.query(self.auto_focus).first()
|
||||
except NoMatches:
|
||||
pass
|
||||
else:
|
||||
self.set_focus(to_focus)
|
||||
self._refresh_layout(size, full=True)
|
||||
self.refresh()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user