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
+20
View File
@@ -1034,6 +1034,18 @@ pub trait InteractiveElement: Sized {
self.interactivity().in_focus_style = Some(Box::new(f(StyleRefinement::default())));
self
}
/// Set the given styles to be applied when this element is focused via keyboard navigation.
/// This is similar to CSS's `:focus-visible` pseudo-class - it only applies when the element
/// is focused AND the user is navigating via keyboard (not mouse clicks).
/// Requires that the element is focusable. Elements can be made focusable using [`InteractiveElement::track_focus`].
fn focus_visible(mut self, f: impl FnOnce(StyleRefinement) -> StyleRefinement) -> Self
where
Self: Sized,
{
self.interactivity().focus_visible_style = Some(Box::new(f(StyleRefinement::default())));
self
}
}
/// A trait for elements that want to use the standard GPUI interactivity features
@@ -1497,6 +1509,7 @@ pub struct Interactivity {
pub base_style: Box<StyleRefinement>,
pub(crate) focus_style: Option<Box<StyleRefinement>>,
pub(crate) in_focus_style: Option<Box<StyleRefinement>>,
pub(crate) focus_visible_style: Option<Box<StyleRefinement>>,
pub(crate) hover_style: Option<Box<StyleRefinement>>,
pub(crate) group_hover_style: Option<GroupStyle>,
pub(crate) active_style: Option<Box<StyleRefinement>>,
@@ -2492,6 +2505,13 @@ impl Interactivity {
{
style.refine(focus_style);
}
if let Some(focus_visible_style) = self.focus_visible_style.as_ref()
&& focus_handle.is_focused(window)
&& window.last_input_was_keyboard()
{
style.refine(focus_visible_style);
}
}
if let Some(hitbox) = hitbox {
+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`.