mod button;
mod context_menu;
mod disclosure;
mod dock;
mod inspector;
mod list;
mod number_entry;
mod outliner;
mod scrollbar;
mod select;
mod split;
mod tabs;
#[cfg(test)]
mod tests;
mod text_input;
mod value;
use std::borrow::Cow;
use std::panic::Location;
use std::time::Duration;
use glam::Vec2;
#[cfg(doc)]
use super::Response;
use super::Ui;
use super::store::{Interaction, NodeKey};
use super::style::LengthAuto;
use crate::style::NodeSelector;
pub use context_menu::{CONTEXT_ARROW, ContextItem, ContextMenuResponse};
pub use dock::{DockLayout, DockSide};
pub use inspector::{
AXES, FieldEdit, FieldRow, INSPECTOR_STEP, Inspection, InspectorOptions, Overrides, RowBuilder,
VARIANT_LABEL, VariantEdit, WHOLE_STEP,
};
pub use list::LIST_OVERSCAN;
pub use outliner::{
OUTLINER_INDENT, OUTLINER_ROW_HEIGHT, OutlinerBuilder, OutlinerId, OutlinerOptions,
OutlinerRow, OutlinerState, SelectMode,
};
pub use scrollbar::MIN_SCROLL_HANDLE;
pub use select::SELECT_CARET;
pub use split::{SPLIT_NAV_STEP, SplitAxis};
pub use text_input::{
ClipboardAnswer, ClipboardReply, ClipboardRequest, DOUBLE_CLICK_TIME, MASK, TextInput,
TextInputOptions,
};
pub(crate) use text_input::{EditState, TextFit};
pub use value::DragNumber;
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub(crate) enum WidgetState {
#[default]
None,
Header {
open: bool,
},
TreeItem {
open: bool,
branch: bool,
parent: Option<NodeKey>,
first_child: Option<NodeKey>,
item: Option<OutlinerId>,
},
Anchor(Option<f32>),
Drag {
anchor: Option<value::sealed::DragAnchor>,
last_press: Option<(Duration, Vec2)>,
},
TypedNumber(value::sealed::DragAnchor),
Split(Option<f32>),
Rows(Option<usize>),
OutlinerToggle(OutlinerId),
Tabs(u64),
TextInput,
MenuBranch,
}
impl WidgetState {
pub(crate) const fn types(self) -> bool {
matches!(self, Self::TextInput | Self::TypedNumber(_))
}
pub(crate) const fn is_typed_number(self) -> bool {
matches!(self, Self::TypedNumber(_))
}
pub(crate) const fn is_drag_value(self) -> bool {
matches!(self, Self::Drag { .. } | Self::TypedNumber(_))
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub(crate) enum WidgetKind {
#[default]
None,
Decoration,
Button,
Checkbox,
Slider,
DragValue,
TextInput {
masked: bool,
},
Select,
SelectList,
SelectOption,
Disclosure,
TreeRow,
Outliner,
OutlinerRow,
OutlinerToggle,
List,
ListRow,
TabStrip,
Tab,
TabPanel,
Splitter,
Pane,
ScrollBar,
Popup,
Menu,
MenuItem,
Tooltip,
Inspector,
}
pub(super) fn typed<'s>(kind: &'static str, selector: &'s str) -> Cow<'s, str> {
if selector.is_empty() {
Cow::Borrowed(kind)
} else if selector.starts_with(['#', '.']) {
Cow::Owned(format!("{kind}{selector}"))
} else {
Cow::Borrowed(selector)
}
}
fn clamp_to(value: f32, min: f32, max: f32) -> f32 {
value.max(min).min(max)
}
impl Ui {
fn widget_key(&mut self, selector: NodeSelector<'_>, location: &Location<'_>) -> NodeKey {
let key = self.selector_key(selector, location);
self.unique(key)
}
fn interaction_of(&self, key: NodeKey) -> Interaction {
self.store
.by_key(key)
.map(|node| node.interaction)
.unwrap_or_default()
}
fn widget_state(&self, key: NodeKey) -> WidgetState {
self.store
.by_key(key)
.map_or(WidgetState::None, |node| node.widget)
}
fn set_widget_state(&mut self, key: NodeKey, state: WidgetState) {
if let Some(slot) = self.store.find(key) {
self.store.get_mut(slot).widget = state;
}
}
pub(in crate::tree) fn mark(&mut self, key: NodeKey, kind: WidgetKind) {
if let Some(slot) = self.store.find(key) {
self.store.get_mut(slot).kind = kind;
}
}
const fn building_disabled(&self) -> bool {
self.disabled_depth > 0
}
pub fn enabled(&mut self, enabled: bool, build: impl FnOnce(&mut Self)) {
let disables = u32::from(!enabled);
self.disabled_depth += disables;
build(self);
self.disabled_depth -= disables;
}
#[must_use]
pub fn is_open(&self, key: NodeKey) -> bool {
matches!(
self.widget_state(key),
WidgetState::Header { open: true } | WidgetState::TreeItem { open: true, .. }
)
}
const fn clicked_key(&self) -> Option<NodeKey> {
self.clicked
}
fn row_window(&mut self, rows: usize, height: f32) -> RowWindow {
let index = *self.open.last().expect("called inside the block");
let block = &self.nodes[index];
let stored = self.store.get(block.slot);
let view = if block.fresh {
match block.style.height {
LengthAuto::Px(px) => px,
LengthAuto::Percent(_) | LengthAuto::Auto => self
.viewport
.map(|viewport| viewport.max.y - viewport.min.y)
.filter(|height| height.is_finite())
.unwrap_or(0.0),
}
} else {
let (start, end) = stored.content_box();
end.y - start.y
};
let reach = (rows as f32 * height - view).max(0.0);
let offset = stored.scroll_offset.y.max(0.0).min(reach);
let window = window_of(rows, height, offset, view, LIST_OVERSCAN);
self.built_rows.push(BuiltRows {
node: index,
rows,
height,
window,
});
window
}
pub(in crate::tree) fn check_built_rows(&mut self) {
self.rows_missed = self.built_rows.iter().any(|built| {
let stored = self.store.get(self.nodes[built.node].slot);
let (start, end) = stored.content_box();
let offset = stored.scroll_offset.y.max(0.0);
let shown = window_of(built.rows, built.height, offset, end.y - start.y, 0);
shown.first < shown.end
&& (shown.first < built.window.first || shown.end > built.window.end)
});
}
#[must_use]
pub const fn needs_rebuild(&self) -> bool {
self.rows_missed
}
fn kept_row(
&self,
key: NodeKey,
rows: usize,
row_key: impl Fn(&Self, usize) -> NodeKey,
) -> Option<usize> {
let kept = match self.widget_state(key) {
WidgetState::Rows(focused) => focused.filter(|&index| index < rows),
_ => None,
}?;
let expected = row_key(self, kept);
(self.focused() == Some(expected)).then_some(kept)
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) struct RowWindow {
pub first: usize,
pub end: usize,
}
fn window_of(rows: usize, height: f32, offset: f32, view: f32, overscan: usize) -> RowWindow {
let first = ((offset / height).floor() as usize).saturating_sub(overscan);
let last = ((offset + view.max(0.0)) / height).ceil() as usize;
let end = last.saturating_add(overscan).min(rows);
RowWindow {
first: first.min(end),
end,
}
}
#[derive(Clone, Copy, Debug)]
pub(in crate::tree) struct BuiltRows {
node: usize,
rows: usize,
height: f32,
window: RowWindow,
}