use crate::align::{Align, Anchors};
use crate::component::{AnchorRef, ComponentId, ControlBase};
use crate::event::EventArgs;
use crate::geometry::{PointF, RectF, SizeF};
use crate::ui::Ui;
const DOCK_ORDER: [Align; 8] = [
Align::MostTop,
Align::MostBottom,
Align::MostLeft,
Align::MostRight,
Align::Top,
Align::Bottom,
Align::Left,
Align::Right,
];
impl Ui {
pub fn update_layout(&mut self) {
if self.layout_dirty {
self.layout();
} else if !self.dirty_forms.is_empty() {
let _profile = crate::profile::scope(crate::profile::Lane::Layout, || "layout de form".into());
if !self.views.is_empty() {
self.apply_views();
}
if self.layout_dirty {
self.layout();
return;
}
let forms: Vec<ComponentId> = self.roots().iter().copied().filter(|r| self.dirty_forms.contains(r)).collect();
self.dirty_forms.clear();
let before: Vec<(ComponentId, Option<RectF>, bool)> = forms.iter().flat_map(|&f| self.placed(f)).collect();
self.layout_roots(&forms);
for (id, rect, visible) in before {
if self.absolute_rect(id) != rect || self.cb(id).is_some_and(|cb| cb.visible()) != visible {
self.damage(id);
}
}
}
}
fn placed(&self, root: ComponentId) -> Vec<(ComponentId, Option<RectF>, bool)> {
let mut out = Vec::new();
let mut stack = self.children(root).to_vec();
while let Some(id) = stack.pop() {
out.push((id, self.absolute_rect(id), self.cb(id).is_some_and(|cb| cb.visible())));
stack.extend(self.children(id));
}
out
}
pub fn layout(&mut self) {
let _profile = crate::profile::scope(crate::profile::Lane::Layout, || "layout".into());
if !self.views.is_empty() {
self.apply_views();
}
self.layout_dirty = false;
self.dirty_forms.clear();
let roots = self.roots().to_vec();
self.layout_roots(&roots);
self.request_repaint();
}
fn layout_roots(&mut self, roots: &[ComponentId]) {
let mut resized = Vec::new();
for &root in roots {
let (text, spacing) = self.view_scales(root);
if let Some(cb) = self.cb_mut(root) {
cb.text_scale = text;
cb.spacing_scale = spacing;
}
if let Some(cb) = self.cb_mut(root) {
let old = cb.bounds.size();
cb.bounds = RectF::new(0.0, 0.0, cb.width().max(cb.min_width()), cb.height().max(cb.min_height()));
if cb.bounds.size() != old {
resized.push(root);
}
}
self.realign(root, &mut resized);
self.place_followers(root);
}
for &id in &resized {
self.with_component(id, |c, ctx| c.resized(ctx));
}
for id in resized {
self.queue_event(id, "on_resize", EventArgs::None);
}
self.process_events();
}
fn realign(&mut self, id: ComponentId, resized: &mut Vec<ComponentId>) {
let Some(cb) = self.cb(id) else { return };
let size = cb.bounds.size();
let full = RectF::from_size(size);
let mut area = full.deflate(cb.padding()).deflate(cb.safe_area);
let scales = (cb.text_scale, cb.spacing_scale);
let children = self.children(id).to_vec();
for &child in &children {
let own = if self.views.is_empty() { scales } else { self.own_view_scales(child).unwrap_or(scales) };
if let Some(cb) = self.cb_mut(child) {
if (cb.text_scale, cb.spacing_scale) != own {
cb.text_scale = own.0;
cb.spacing_scale = own.1;
}
}
}
let old_sizes: Vec<Option<SizeF>> =
children.iter().map(|&c| self.cb(c).map(|b| b.bounds.size())).collect();
for &child in &children {
let Some(cb) = self.cb(child).filter(|cb| cb.visible() && cb.auto_size()) else { continue };
let width = match cb.align() {
Align::Top | Align::Bottom => cb.band_width((area.width - cb.margins().horizontal()).max(0.0)),
Align::None => cb.clamp_width(cb.width()),
_ => continue,
};
if let Some(h) = self.auto_height_of(child, width) {
if let Some(cb) = self.cb_mut(child) {
cb.set_height(h);
}
}
}
for pass in DOCK_ORDER {
for &child in &children {
if let Some(cb) = self.cb_mut(child) {
if cb.visible() && cb.align() == pass {
dock(cb, &mut area);
}
}
}
}
let free: Vec<crate::component::ArrangeItem> = children
.iter()
.filter_map(|&c| {
let cb = self.cb(c).filter(|cb| cb.visible() && cb.align() == Align::None)?;
Some(crate::component::ArrangeItem::of(c, cb))
})
.collect();
let arranged = if free.is_empty() { None } else { self.component(id).and_then(|c| c.arrange(area, &free)) };
if let Some(rects) = &arranged {
for (item, rect) in free.iter().zip(rects) {
if let Some(cb) = self.cb_mut(item.id) {
cb.bounds = *rect;
}
}
}
for &child in &children {
if arranged.is_some() && free.iter().any(|f| f.id == child) {
continue;
}
if let Some(cb) = self.cb_mut(child) {
if cb.visible() && !cb.align().is_dock() {
place(cb, area, full);
}
}
}
for &child in &children {
let Some((target, fx, fy)) = self.component(child).and_then(|c| c.attach()) else { continue };
if self.parent(target) != Some(id) || target == child {
continue;
}
let Some(t) = self.cb(target).map(|cb| cb.bounds) else { continue };
if let Some(cb) = self.cb_mut(child) {
let (w, h) = (cb.width(), cb.height());
cb.bounds = RectF::new(t.x + t.width * fx - w / 2.0, t.y + t.height * fy - h / 2.0, w, h);
}
}
for &child in &children {
let Some(c) = self.component(child).and_then(|c| c.platform_hooks()).and_then(|h| h.connection()) else { continue };
if self.parent(c.from) != Some(id) || self.parent(c.to) != Some(id) || c.from == child || c.to == child {
continue;
}
let (Some(a), Some(b)) = (self.cb(c.from).map(|cb| cb.bounds), self.cb(c.to).map(|cb| cb.bounds)) else { continue };
let points = c.route(a, b);
let (mut x0, mut y0, mut x1, mut y1) = (f32::MAX, f32::MAX, f32::MIN, f32::MIN);
for p in &points {
(x0, y0, x1, y1) = (x0.min(p.x), y0.min(p.y), x1.max(p.x), y1.max(p.y));
}
let pad = 12.0;
if let Some(cb) = self.cb_mut(child) {
cb.bounds = RectF::new(x0 - pad, y0 - pad, x1 - x0 + 2.0 * pad, y1 - y0 + 2.0 * pad);
}
}
let (padding, offset) = match self.cb(id) {
Some(cb) => (cb.padding(), cb.scroll_offset()),
None => return,
};
let mut extent = SizeF::new(padding.horizontal(), padding.vertical());
for &child in &children {
if let Some(cb) = self.cb(child).filter(|cb| cb.visible()) {
let m = cb.margins();
extent.width = extent.width.max(cb.bounds.right() + m.right + padding.right);
extent.height = extent.height.max(cb.bounds.bottom() + m.bottom + padding.bottom);
}
}
if let Some(cb) = self.cb_mut(id) {
cb.content_size = extent;
}
if self.is_mirrored(id) {
for &child in &children {
if let Some(cb) = self.cb_mut(child) {
let b = cb.bounds;
cb.bounds = RectF::new(size.width - b.right(), b.y, b.width, b.height);
}
}
}
let offset = if offset.y > 0.0 && self.parent(id).is_none() {
let y = offset.y.min(self.form_scroll_max(id));
if let Some(cb) = self.cb_mut(id) {
cb.set_scroll_offset(crate::geometry::PointF::new(offset.x, y));
}
crate::geometry::PointF::new(offset.x, y)
} else if offset != crate::geometry::PointF::ZERO && self.parent(id).is_some() {
let size = self.cb(id).map_or(SizeF::new(0.0, 0.0), |cb| cb.bounds.size());
let clamped = crate::geometry::PointF::new(
offset.x.min((extent.width - size.width).max(0.0)).max(0.0),
offset.y.min((extent.height - size.height).max(0.0)).max(0.0),
);
if clamped != offset {
if let Some(cb) = self.cb_mut(id) {
cb.set_scroll_offset(clamped);
}
}
clamped
} else {
offset
};
if let Some(cb) = self.cb_mut(id) {
cb.laid_out_scroll = offset;
}
if offset != crate::geometry::PointF::ZERO {
for &child in &children {
if let Some(cb) = self.cb_mut(child) {
cb.bounds = cb.bounds.offset(-offset.x, -offset.y);
}
}
}
for (&child, old) in children.iter().zip(old_sizes) {
if !self.cb(child).is_some_and(|cb| cb.visible()) {
continue;
}
if let (Some(old), Some(cb)) = (old, self.cb(child)) {
if cb.bounds.size() != old {
resized.push(child);
}
}
self.realign(child, resized);
}
}
}
impl Ui {
fn auto_height_of(&self, id: ComponentId, width: f32) -> Option<f32> {
if let Some(h) = self.component(id).and_then(|c| c.auto_height(self, id, width)) {
return Some(h.ceil());
}
let cb = self.cb(id)?;
let padding = cb.padding();
let inner = width - padding.horizontal();
let mut total = padding.vertical();
for &child in self.children(id) {
let Some(c) = self.cb(child).filter(|c| c.visible() && matches!(c.align(), Align::Top | Align::Bottom)) else { continue };
let m = c.margins();
let h = if c.auto_size() { self.auto_height_of(child, c.band_width((inner - m.horizontal()).max(0.0))).unwrap_or(c.height()) } else { c.height() };
let h = c.clamp_height(h);
total += h + m.vertical();
}
let free: Vec<crate::component::ArrangeItem> = self
.children(id)
.iter()
.filter_map(|&c| {
let cb = self.cb(c).filter(|cb| cb.visible() && cb.align() == Align::None)?;
Some(crate::component::ArrangeItem::of(c, cb))
})
.collect();
if !free.is_empty() {
let area = RectF::new(0.0, 0.0, inner.max(0.0), 0.0);
if let Some(rects) = self.component(id).and_then(|c| c.arrange(area, &free)) {
let bottom = rects.iter().zip(&free).map(|(r, f)| r.bottom() + f.margins.bottom).fold(0.0f32, f32::max);
total += bottom;
}
}
Some(total)
}
}
fn band(cb: &ControlBase, area: &RectF) -> (f32, f32) {
let m = cb.margins();
let available = (area.width - m.horizontal()).max(0.0);
let w = cb.band_width(available);
(area.x + m.left + cb.band_offset(available, w), w)
}
fn column(cb: &ControlBase, area: &RectF) -> (f32, f32) {
let m = cb.margins();
let available = (area.height - m.vertical()).max(0.0);
let h = cb.band_height(available);
(area.y + m.top + cb.band_offset(available, h), h)
}
fn dock(cb: &mut ControlBase, area: &mut RectF) {
let m = cb.margins();
match cb.align() {
Align::Top | Align::MostTop => {
let (x, w) = band(cb, area);
let h = ratio_height(cb, w);
cb.bounds = RectF::new(x, area.y + m.top, w, h);
let used = h + m.vertical();
area.y += used;
area.height = (area.height - used).max(0.0);
}
Align::Bottom | Align::MostBottom => {
let (x, w) = band(cb, area);
let h = ratio_height(cb, w);
cb.bounds = RectF::new(x, area.bottom() - m.bottom - h, w, h);
area.height = (area.height - h - m.vertical()).max(0.0);
}
Align::Left | Align::MostLeft => {
let (y, h) = column(cb, area);
let w = ratio_width(cb, h);
cb.bounds = RectF::new(area.x + m.left, y, w, h);
let used = w + m.horizontal();
area.x += used;
area.width = (area.width - used).max(0.0);
}
Align::Right | Align::MostRight => {
let (y, h) = column(cb, area);
let w = ratio_width(cb, h);
cb.bounds = RectF::new(area.right() - m.right - w, y, w, h);
area.width = (area.width - w - m.horizontal()).max(0.0);
}
_ => {}
}
}
fn place(cb: &mut ControlBase, area: RectF, full: RectF) {
let m = cb.margins();
let (w, h) = (cb.width(), cb.height());
let inner = area.deflate(m);
let ratio = cb.aspect_ratio();
cb.bounds = match cb.align() {
Align::Client | Align::Center if ratio > 0.0 => {
let (fw, fh) = if inner.width / inner.height.max(0.01) > ratio { (inner.height * ratio, inner.height) } else { (inner.width, inner.width / ratio) };
RectF::new(inner.x + (inner.width - fw) / 2.0, inner.y + (inner.height - fh) / 2.0, fw, fh)
}
Align::Client => {
let w = cb.clamp_width(inner.width);
RectF::new(inner.x + ((inner.width - w) / 2.0).max(0.0), inner.y, w, cb.clamp_height(inner.height))
}
Align::Contents => full.deflate(m),
Align::Center => {
let (w, h) = (cb.clamp_width(w), cb.clamp_height(h));
RectF::new(inner.x + (inner.width - w) / 2.0, inner.y + (inner.height - h) / 2.0, w, h)
}
Align::VertCenter => {
let w = cb.clamp_width(inner.width);
let h = ratio_height(cb, w);
RectF::new(inner.x, inner.y + (inner.height - h) / 2.0, w, h)
}
Align::HorzCenter => {
let h = cb.clamp_height(inner.height);
let w = ratio_width(cb, h);
RectF::new(inner.x + (inner.width - w) / 2.0, inner.y, w, h)
}
Align::Horizontal => {
let w = cb.clamp_width(inner.width);
RectF::new(inner.x, cb.top(), w, ratio_height(cb, w))
}
Align::Vertical => {
let h = cb.clamp_height(inner.height);
RectF::new(cb.left(), inner.y, ratio_width(cb, h), h)
}
Align::Fit => {
let scale = if w > 0.0 && h > 0.0 { (inner.width / w).min(inner.height / h) } else { 0.0 };
let (fw, fh) = (w * scale, h * scale);
RectF::new(inner.x + (inner.width - fw) / 2.0, inner.y + (inner.height - fh) / 2.0, fw, fh)
}
Align::None => {
let r = anchored(cb, full.size());
let (w, h) = (cb.clamp_width(r.width), cb.clamp_height(r.height));
if ratio > 0.0 { RectF::new(r.x, r.y, w, w / ratio) } else { RectF::new(r.x, r.y, w, h) }
}
_ => cb.bounds,
};
}
fn ratio_height(cb: &ControlBase, width: f32) -> f32 {
let ratio = cb.aspect_ratio();
cb.clamp_height(if ratio > 0.0 { (width.max(0.0) / ratio).max(0.0) } else { cb.height() })
}
fn ratio_width(cb: &ControlBase, height: f32) -> f32 {
let ratio = cb.aspect_ratio();
cb.clamp_width(if ratio > 0.0 { height.max(0.0) * ratio } else { cb.width() })
}
fn anchored(cb: &mut ControlBase, parent: SizeF) -> RectF {
let requested = RectF::new(cb.left(), cb.top(), cb.width(), cb.height());
let anchors = cb.anchors();
if anchors == Anchors::default() {
return requested;
}
let reference = *cb.anchor_ref.get_or_insert(AnchorRef { parent, rect: requested });
let dw = parent.width - reference.parent.width;
let dh = parent.height - reference.parent.height;
let (x, width) = axis(
reference.rect.x,
reference.rect.width,
dw,
anchors.contains(Anchors::LEFT),
anchors.contains(Anchors::RIGHT),
);
let (y, height) = axis(
reference.rect.y,
reference.rect.height,
dh,
anchors.contains(Anchors::TOP),
anchors.contains(Anchors::BOTTOM),
);
RectF::new(x, y, width.max(0.0), height.max(0.0))
}
fn axis(pos: f32, len: f32, delta: f32, near: bool, far: bool) -> (f32, f32) {
match (near, far) {
(true, true) => (pos, len + delta),
(false, true) => (pos + delta, len),
(true, false) => (pos, len),
(false, false) => (pos + delta / 2.0, len),
}
}
impl Ui {
pub fn follow(&mut self, id: ComponentId, target: ComponentId) {
if let Some(cb) = self.cb_mut(id) {
cb.follow = Some((target, None));
}
self.invalidate();
}
pub fn shown_rect(&self, id: ComponentId) -> Option<RectF> {
let mut rect = self.absolute_rect(id)?;
let mut parent = self.parent(id);
while let Some(p) = parent {
let cb = self.cb(p)?;
if !cb.visible() {
return None;
}
if cb.clip_children() || self.parent(p).is_none() {
rect = rect.intersect(self.absolute_rect(p)?)?;
}
parent = self.parent(p);
}
Some(rect)
}
fn place_followers(&mut self, root: ComponentId) {
for child in self.children(root).to_vec() {
let Some((target, at)) = self.cb(child).and_then(|cb| cb.follow) else { continue };
let Some(t) = self.absolute_rect(target) else { continue };
let at = at.unwrap_or_else(|| self.cb(child).map_or(PointF::ZERO, |cb| PointF::new(cb.left() - t.x, cb.top() - t.y)));
let shown = self.shown_rect(target).is_some();
if let Some(cb) = self.cb_mut(child) {
let size = cb.bounds.size();
cb.bounds = RectF::new(t.x + at.x, t.y + at.y, size.width, size.height);
cb.set_visible(shown);
cb.follow = Some((target, Some(at)));
}
}
}
}
pub fn is_right_to_left_language(code: &str) -> bool {
let base = code.split(['-', '_']).next().unwrap_or("").to_lowercase();
matches!(base.as_str(), "ar" | "he" | "iw" | "fa" | "ur" | "ps" | "sd" | "ug" | "yi" | "dv" | "ckb")
}
impl Ui {
pub fn set_right_to_left(&mut self, on: bool) {
if self.rtl != on {
self.rtl = on;
self.invalidate();
}
}
pub fn right_to_left(&self) -> bool {
self.rtl
}
pub fn is_mirrored(&self, id: ComponentId) -> bool {
if !self.rtl {
return false;
}
let mut current = Some(id);
while let Some(c) = current {
if self.cb(c).is_some_and(|cb| cb.keep_left_to_right) {
return false;
}
current = self.parent(c);
}
true
}
pub(crate) fn flips_content(&self, id: ComponentId) -> bool {
self.is_mirrored(id) && !self.component(id).is_some_and(|c| c.accepts_text())
}
}