//! The shadow tree: what the view asked for per node (tag, classes, //! attributes, properties, states, children), the entity behind each, and //! the pass that turns the cascade's computed style into `bevy_ui` //! components. use std::collections::{HashMap, HashSet}; use bevy::input_focus::tab_navigation::TabIndex; use bevy::input_focus::InputFocus; use bevy::picking::events::PointerDrag; use bevy::prelude::*; use bevy::text::{EditableText, LineHeight}; use bevy::ui::ScrollPosition; use bevy::ui::{ BoxShadow, GlobalZIndex, GridTrack, Overflow, OverflowAxis, RepeatedGridTrack, ShadowStyle, UiTargetCamera, UiTransform, Val2, }; use bevy::ui_widgets::{ ScrollArea, Slider, SliderRange, SliderStep, SliderThumb, SliderValue, TextInput, TrackClick, }; use mithril::style::cascade::{ Chain, Computed, Display, Element, Length, Overflow as Ov, Position, Rgba, States, TextAlign, }; use crate::{with_host, NodeId, Op, UiCamera, ROOT}; /// The fonts the renderer sets: the app maps the stylesheet's families and /// weights onto its font assets. #[derive(Resource, Clone)] pub struct FontMap { pub regular: Handle, pub semibold: Handle, pub mono: Handle, /// An icon font, for a family naming "Symbols" or "icons" /// (`font-family: 'Material Symbols Rounded'`); `None` falls back to /// the regular face. pub icons: Option>, /// The icon face drawn this much larger than its `font-size`; 1 for none. pub icon_scale: f32, /// A face every text face falls back to for a character it lacks — an /// emoji font, so a 😮 in a chat is a face and not an empty box. pub emoji: Option>, } impl FontMap { fn is_icons(&self, family: &str) -> bool { self.icons.is_some() && (family.contains("Symbols") || family.contains("icons")) } /// The size `px` of `family` is drawn at. fn size(&self, family: &str, px: f32) -> f32 { if self.is_icons(family) { px * self.icon_scale } else { px } } fn pick(&self, family: &str, weight: u16) -> bevy::text::FontSource { if family.contains("Symbols") || family.contains("icons") { if let Some(icons) = &self.icons { return icons.clone().into(); } } let face = if family.contains("monospace") || family.contains("mono") { self.mono.clone() } else if weight >= 550 { self.semibold.clone() } else { self.regular.clone() }; match &self.emoji { Some(emoji) => bevy::text::FontSource::list([face, emoji.clone()]), None => face.into(), } } } pub(crate) enum NodeKind { Element { tag: String, classes: Vec, attrs: Vec<(String, String)>, props: HashMap, states: States, }, Text(String), Anchor, } pub(crate) struct ShadowNode { pub entity: Option, pub kind: NodeKind, pub parent: Option, pub children: Vec, pub computed: Option, pub opacity: f32, /// What the view said about this node changed: style it and write its /// components. pub dirty: bool, /// Something its selectors may read changed (an ancestor's class, a /// hover, the focus, the window): style it, and write only if the style, /// opacity or states come out different. A focus change marks the whole /// tree this way, and rewriting every node respawned every decoration /// and made Bevy lay out and shape every text again. pub restyle: bool, /// The states the components were last written with. pub written_states: Option, /// The drawn pseudo-elements, replaced on restyle. pub decor: Vec, /// For `