nsh-core 0.8.1

Platform-independent core: shadow tree, styles, layout and mount instructions.
Documentation
//! A focus ring an ancestor would clip is drawn inside its view.
//!
//! A focus ring drawn outward (`outline` with a non-negative offset, or a
//! `ring-*` shadow: a spread with no blur and no offset) takes no layout
//! space, so an ancestor that clips its content cuts it wherever the view
//! sits against that ancestor's edge: a view with `overflow: hidden`
//! (often set only to round its corners), a scroll view or a list. A field
//! as wide as a scroll view, or a row filling a sidebar, then shows its
//! ring on two sides or none, and the keyboard focus is hard to see.
//!
//! While a view is styled by a focus state (`focus:`, `focus-visible:`,
//! `focus-within:`), the core checks that its outward ring fits in every
//! clipping ancestor up to its layout root. A scroll view clips to its
//! content, not its viewport: a view scrolled partly out of sight shows its
//! ring again once scrolled back, while a ring past the content's edge
//! never shows. When the ring does not fit, the mounted style draws it
//! inside the view's border box instead (the outline's offset becomes
//! minus its width, ring shadows become inset), so every platform shows it
//! whole.

use super::{Core, Key, Kind, NO_KEY};
use crate::{effects::Outline, style::ViewStyle, tailwind::state, text::TextEngine};

/// The states a focus ring is drawn in.
const FOCUS_STATES: u16 = state::FOCUSED | state::FOCUS_VISIBLE | state::FOCUS_WITHIN;

/// How far a ring may reach past a clip, in points, still counted as
/// fitting (rounding of the frames).
const TOLERANCE: f32 = 0.5;

/// How far the outward focus ring of `style` reaches past its border box
/// (0: none).
pub(super) fn outward_ring(style: &ViewStyle) -> f32 {
    let mut reach = 0.0f32;
    if outline_is_outward(&style.outline) {
        reach = (style.outline.width.0 + style.outline.offset.0).max(0.0);
    }
    if let Some(effects) = &style.effects {
        for s in effects.box_shadows.iter().filter(|s| is_ring(s)) {
            reach = reach.max(s.spread.0);
        }
    }
    reach
}

fn outline_is_outward(o: &Outline) -> bool {
    o.is_visible() && o.offset.0 >= 0.0
}

/// A `ring-*` shadow: a hard edge around the box (Tailwind's
/// `0 0 0 <width>`), not a blurred or offset shadow.
fn is_ring(s: &crate::effects::BoxShadow) -> bool {
    !s.inset
        && s.spread.0 > 0.0
        && s.blur.0 == 0.0
        && s.offset_x.0 == 0.0
        && s.offset_y.0 == 0.0
        && !s.color.is_transparent()
}

/// Draws the outward focus ring of `style` inside its border box.
pub(super) fn draw_inside(style: &mut ViewStyle) {
    if outline_is_outward(&style.outline) {
        style.outline.offset = crate::Points(-style.outline.width.0);
    }
    if let Some(effects) = &mut style.effects
        && effects.box_shadows.iter().any(is_ring)
    {
        let effects = std::sync::Arc::make_mut(effects);
        for s in effects.box_shadows.iter_mut().filter(|s| is_ring(s)) {
            s.inset = true;
        }
    }
}

impl<M: TextEngine> Core<M> {
    /// Whether `key` is styled by a focus state with an outward ring that
    /// a clipping ancestor would cut.
    pub(super) fn focus_ring_clipped(&self, key: Key, style: &ViewStyle) -> bool {
        if self.styled_states(key) & FOCUS_STATES == 0 {
            return false;
        }
        let reach = outward_ring(style);
        if reach <= 0.0 {
            return false;
        }
        let node = &self.nodes[key as usize];
        if node.is_layout_root() || node.is_overlay() || node.inline {
            return false;
        }
        let f = node.frame;
        if f.origin.x.0.is_nan() || f.size.width.0.is_nan() {
            return false;
        }
        // The ring's rectangle in the coordinates of the view `at` is in.
        let (mut x0, mut y0) = (f.origin.x.0 - reach, f.origin.y.0 - reach);
        let (mut x1, mut y1) = (
            f.origin.x.0 + f.size.width.0 + reach,
            f.origin.y.0 + f.size.height.0 + reach,
        );
        let mut at = node.parent;
        while at != NO_KEY {
            let a = &self.nodes[at as usize];
            let af = a.frame;
            if af.size.width.0.is_nan() {
                return false;
            }
            let (w, h) = (af.size.width.0, af.size.height.0);
            let clip = match (a.kind, a.scroll.as_deref()) {
                // Its children are placed in its content, which the
                // viewport scrolls over: only the content's edges are
                // edges for good.
                (Kind::Scroll | Kind::List, Some(s)) => {
                    let c = s.content.unwrap_or_default();
                    Some((c.width.0.max(w), c.height.0.max(h)))
                }
                (Kind::Scroll | Kind::List, None) => Some((w, h)),
                _ if a.view_style.overflow_hidden => Some((w, h)),
                _ => None,
            };
            if let Some((cw, ch)) = clip
                && (x0 < -TOLERANCE
                    || y0 < -TOLERANCE
                    || x1 > cw + TOLERANCE
                    || y1 > ch + TOLERANCE)
            {
                return true;
            }
            // Laid out on its own (a screen, an overlay, a popup): what
            // is around it does not place it.
            if a.is_layout_root() || a.is_overlay() || a.inline {
                return false;
            }
            let (dx, dy) = (af.origin.x.0, af.origin.y.0);
            (x0, y0, x1, y1) = (x0 + dx, y0 + dy, x1 + dx, y1 + dy);
            at = a.parent;
        }
        false
    }

    /// After `key` was laid out: mounts its style again when whether its
    /// focus ring fits changed (`focus_ring_clipped`).
    pub(super) fn sync_focus_ring(&mut self, key: Key) {
        let node = &self.nodes[key as usize];
        if !node.created && !node.ring_inside {
            return;
        }
        let inside = self.styled_states(key) & FOCUS_STATES != 0
            && outward_ring(&node.view_style) > 0.0
            && self.focus_ring_clipped(key, &node.view_style);
        let node = &mut self.nodes[key as usize];
        if node.ring_inside == inside {
            return;
        }
        node.ring_inside = inside;
        if node.created {
            let id = node.id;
            let style = self.mounted_view_style(key);
            self.mount.push(crate::MountItem::ViewStyle { id, style });
        }
    }
}