Skip to main content

egui_charts/styles/
focus.rs

1//! Focus Ring Indicators
2//!
3//! Provides visible focus indicators for keyboard navigation accessibility.
4//! Focus rings appear when UI elements receive keyboard focus.
5
6use egui::{Color32, Response, Stroke, Ui, epaint::StrokeKind};
7
8use crate::tokens::DESIGN_TOKENS;
9
10/// The focus ring colour: the applied theme's `ui.focus`, or the selection stroke when no theme
11/// has been applied.
12pub fn focus_ring_color(ui: &Ui) -> Color32 {
13    crate::theme::applied_theme(ui.ctx())
14        .map_or(ui.visuals().selection.stroke.color, |theme| theme.ui.focus)
15}
16
17/// Draw a focus ring around a response if it has keyboard focus.
18///
19/// Pass the control's corner radius so the 2 px outer ring stays concentric.
20pub fn draw_focus_ring(ui: &Ui, response: &Response, radius: impl Into<egui::CornerRadius>) {
21    if response.has_focus() {
22        let ring_color = focus_ring_color(ui);
23        ui.painter().rect_stroke(
24            response.rect.expand(DESIGN_TOKENS.spacing.xs),
25            radius.into() + DESIGN_TOKENS.spacing.xs as u8,
26            Stroke::new(DESIGN_TOKENS.stroke.thick, ring_color),
27            // Inside the expanded rect, so a control filling a tight strip keeps all four sides.
28            StrokeKind::Inside,
29        );
30    }
31}