gpui: Add focus-visible selector support (#40940)

Release Notes:

- N/A

---------

Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
Mikayla Maki
2025-10-22 23:36:18 +00:00
committed by GitHub
co-authored by Claude
parent 044701e3a5
commit 4fd4cbbfb7
3 changed files with 251 additions and 0 deletions
+17
View File
@@ -863,6 +863,7 @@ pub struct Window {
hovered: Rc<Cell<bool>>,
pub(crate) needs_present: Rc<Cell<bool>>,
pub(crate) last_input_timestamp: Rc<Cell<Instant>>,
last_input_was_keyboard: bool,
pub(crate) refreshing: bool,
pub(crate) activation_observers: SubscriberSet<(), AnyObserver>,
pub(crate) focus: Option<FocusId>,
@@ -1246,6 +1247,7 @@ impl Window {
hovered,
needs_present,
last_input_timestamp,
last_input_was_keyboard: false,
refreshing: false,
activation_observers: SubscriberSet::new(),
focus: None,
@@ -1899,6 +1901,12 @@ impl Window {
self.modifiers
}
/// Returns true if the last input event was keyboard-based (key press, tab navigation, etc.)
/// This is used for focus-visible styling to show focus indicators only for keyboard navigation.
pub fn last_input_was_keyboard(&self) -> bool {
self.last_input_was_keyboard
}
/// The current state of the keyboard's capslock
pub fn capslock(&self) -> Capslock {
self.capslock
@@ -3580,6 +3588,15 @@ impl Window {
#[profiling::function]
pub fn dispatch_event(&mut self, event: PlatformInput, cx: &mut App) -> DispatchEventResult {
self.last_input_timestamp.set(Instant::now());
// Track whether this input was keyboard-based for focus-visible styling
self.last_input_was_keyboard = matches!(
event,
PlatformInput::KeyDown(_)
| PlatformInput::KeyUp(_)
| PlatformInput::ModifiersChanged(_)
);
// Handlers may set this to false by calling `stop_propagation`.
cx.propagate_event = true;
// Handlers may set this to true by calling `prevent_default`.