use crate::component::ComponentId;
use crate::geometry::SizeF;
use crate::prop::{PropEnum, PropValue};
use crate::ui::Ui;
pub const PARENT: &str = "parent";
pub const INDEX: &str = "index";
pub const PLATFORMS: [&str; 6] = ["windows", "linux", "macos", "android", "ios", "web"];
pub fn current_platform() -> &'static str {
if cfg!(target_arch = "wasm32") {
"web"
} else if cfg!(target_os = "android") {
"android"
} else if cfg!(target_os = "ios") {
"ios"
} else if cfg!(target_os = "macos") {
"macos"
} else if cfg!(target_os = "windows") {
"windows"
} else {
"linux"
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum ViewOrientation {
#[default]
Any,
Portrait,
Landscape,
}
impl PropEnum for ViewOrientation {
const NAMES: &'static [&'static str] = &["any", "portrait", "landscape"];
fn from_index(index: usize) -> Option<Self> {
[ViewOrientation::Any, ViewOrientation::Portrait, ViewOrientation::Landscape].get(index).copied()
}
fn index(self) -> usize {
self as usize
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct FormView {
pub name: String,
pub min_width: f32,
pub max_width: f32,
pub min_height: f32,
pub max_height: f32,
pub orientation: ViewOrientation,
pub platforms: Vec<String>,
pub font_scale: f32,
pub spacing_scale: f32,
pub changes: Vec<ViewChange>,
}
impl FormView {
pub fn new(name: impl Into<String>, max_width: f32) -> Self {
FormView {
name: name.into(),
min_width: 0.0,
max_width,
min_height: 0.0,
max_height: 0.0,
orientation: ViewOrientation::Any,
platforms: Vec::new(),
font_scale: 1.0,
spacing_scale: 1.0,
changes: Vec::new(),
}
}
pub fn matches(&self, size: SizeF, platform: &str) -> bool {
let within = |v: f32, min: f32, max: f32| (min <= 0.0 || v >= min) && (max <= 0.0 || v <= max);
within(size.width, self.min_width, self.max_width)
&& within(size.height, self.min_height, self.max_height)
&& match self.orientation {
ViewOrientation::Any => true,
ViewOrientation::Portrait => size.height > size.width,
ViewOrientation::Landscape => size.width >= size.height,
}
&& (self.platforms.is_empty() || self.platforms.iter().any(|p| p == platform))
}
fn width_key(&self) -> f32 {
if self.max_width > 0.0 { self.max_width } else { f32::INFINITY }
}
fn scales(&self) -> (f32, f32) {
let clean = |s: f32| if s.is_finite() && (0.25..=4.0).contains(&s) { s } else { 1.0 };
(clean(self.font_scale), clean(self.spacing_scale))
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct ViewChange {
pub component: String,
pub property: String,
pub value: PropValue,
}
#[derive(Default)]
pub(crate) struct ViewState {
views: Vec<FormView>,
active: Option<String>,
forced: Option<Option<String>>,
base: Vec<(ComponentId, String, PropValue)>,
moves: Vec<(ComponentId, ComponentId, usize)>,
scales: (f32, f32),
}
impl Ui {
pub fn set_views(&mut self, form: ComponentId, mut views: Vec<FormView>) {
views.sort_by(|a, b| a.width_key().total_cmp(&b.width_key()));
self.restore_view_base(form);
let state = self.views.entry(form).or_default();
state.views = views;
state.active = None;
self.layout_dirty = true;
self.request_repaint();
}
pub fn views(&self, form: ComponentId) -> &[FormView] {
self.views.get(&form).map_or(&[], |s| &s.views)
}
pub fn active_view(&self, form: ComponentId) -> Option<&str> {
self.views.get(&form)?.active.as_deref()
}
pub fn view_scales(&self, form: ComponentId) -> (f32, f32) {
self.own_view_scales(form).unwrap_or((1.0, 1.0))
}
pub(crate) fn own_view_scales(&self, form: ComponentId) -> Option<(f32, f32)> {
self.views.get(&form).filter(|s| s.active.is_some()).map(|s| s.scales)
}
pub fn force_view(&mut self, form: ComponentId, view: Option<&str>) {
let state = self.views.entry(form).or_default();
state.forced = view.map(|v| (!v.is_empty()).then(|| v.to_string()));
self.restore_view_base(form);
self.layout_dirty = true;
self.request_repaint();
}
pub fn view_base(&self, id: ComponentId, property: &str) -> Option<&PropValue> {
let form = self.form_of(id);
self.views.get(&form)?.base.iter().find(|(c, p, _)| *c == id && p == property).map(|(_, _, v)| v)
}
pub fn base_children(&self, id: ComponentId) -> Vec<ComponentId> {
let mut list = self.children(id).to_vec();
let form = self.form_of(id);
let Some(state) = self.views.get(&form).filter(|s| !s.moves.is_empty()) else { return list };
for &(moved, old_parent, old_index) in state.moves.iter().rev() {
list.retain(|&c| c != moved);
if old_parent == id {
list.insert(old_index.min(list.len()), moved);
}
}
list
}
pub fn view_base_place(&self, id: ComponentId) -> Option<(ComponentId, usize)> {
let form = self.form_of(id);
self.views.get(&form)?.moves.iter().find(|m| m.0 == id).map(|&(_, p, i)| (p, i))
}
pub fn update_active_view(
&mut self,
form: ComponentId,
changes: Vec<ViewChange>,
base: Vec<(ComponentId, String, PropValue)>,
moves: Vec<(ComponentId, ComponentId, usize)>,
) {
let Some(state) = self.views.get_mut(&form) else { return };
let Some(active) = state.active.clone() else { return };
if let Some(view) = state.views.iter_mut().find(|v| v.name == active) {
view.changes = changes;
}
state.base = base;
state.moves = moves;
}
pub fn set_view_scales(&mut self, form: ComponentId, name: &str, font_scale: f32, spacing_scale: f32) {
let Some(state) = self.views.get_mut(&form) else { return };
let Some(view) = state.views.iter_mut().find(|v| v.name == name) else { return };
view.font_scale = font_scale;
view.spacing_scale = spacing_scale;
if state.active.as_deref() == Some(name) {
state.scales = view.scales();
}
self.layout_dirty = true;
self.request_repaint();
}
fn restore_view_base(&mut self, form: ComponentId) {
let Some(state) = self.views.get_mut(&form) else { return };
let base = std::mem::take(&mut state.base);
let moves = std::mem::take(&mut state.moves);
let had_scales = state.scales != (1.0, 1.0);
state.active = None;
state.scales = (1.0, 1.0);
for (id, prop, value) in base.into_iter().rev() {
let _ = self.set_prop_quiet(id, &prop, value);
}
for (id, parent, index) in moves.into_iter().rev() {
if self.parent(id) != Some(parent) {
let _ = self.set_parent(id, Some(parent));
}
let _ = self.set_child_index(id, index);
}
if had_scales {
self.invalidate_form(form);
}
}
fn view_target(&self, form: ComponentId, component: &str) -> Option<ComponentId> {
if component.is_empty() { Some(form) } else { self.find_in(form, component) }
}
pub(crate) fn apply_views(&mut self) {
let forms: Vec<ComponentId> = self.views.keys().copied().collect();
let platform = current_platform();
for form in forms {
let Some(size) = self.cb(form).map(|cb| SizeF::new(cb.width(), cb.height())) else {
self.views.remove(&form);
continue;
};
let state = &self.views[&form];
let wanted = match &state.forced {
Some(view) => view.clone(),
None => state.views.iter().find(|v| v.matches(size, platform)).map(|v| v.name.clone()),
};
if wanted == state.active {
continue;
}
self.restore_view_base(form);
if !self.design_mode {
self.queue_event(form, "on_view_change", crate::EventArgs::Text(wanted.clone().unwrap_or_default()));
}
let Some(view) = wanted.as_ref().and_then(|w| self.views[&form].views.iter().find(|v| &v.name == w).cloned()) else { continue };
let mut moves = Vec::new();
for change in view.changes.iter().filter(|c| c.property == PARENT).chain(view.changes.iter().filter(|c| c.property == INDEX)) {
let Some(id) = self.view_target(form, &change.component) else { continue };
let Some(old_parent) = self.parent(id) else { continue };
let old_index = self.children(old_parent).iter().position(|&c| c == id).unwrap_or(0);
let done = if change.property == PARENT {
let target = match change.value.to_str().unwrap_or("") {
"" => Some(form),
name => self.find_in(form, name).or_else(|| (self.name(form) == Some(name)).then_some(form)),
};
target.is_some_and(|t| t == old_parent || self.set_parent(id, Some(t)).is_ok())
} else {
let index = change.value.to_f64().map_or(0, |i| i.max(0.0) as usize);
self.set_child_index(id, index).is_ok()
};
if done {
moves.push((id, old_parent, old_index));
}
}
let mut base = Vec::new();
for change in view.changes.iter().filter(|c| c.property != PARENT && c.property != INDEX) {
let Some(id) = self.view_target(form, &change.component) else { continue };
let Ok(old) = self.get_prop(id, &change.property) else { continue };
if self.set_prop_quiet(id, &change.property, change.value.clone()).is_ok() {
base.push((id, change.property.clone(), old));
}
}
let scales = view.scales();
if scales != (1.0, 1.0) {
self.invalidate_form(form);
}
if let Some(state) = self.views.get_mut(&form) {
state.base = base;
state.moves = moves;
state.active = wanted;
state.scales = scales;
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
fn size(width: f32, height: f32) -> SizeF {
SizeF::new(width, height)
}
#[test]
fn the_limits_include_the_edges_and_zero_is_no_limit() {
let mut v = FormView::new("faixa", 599.0);
v.min_width = 400.0;
assert!(v.matches(size(599.0, 800.0), "windows"), "o máximo vale");
assert!(v.matches(size(400.0, 800.0), "windows"), "o mínimo vale");
assert!(!v.matches(size(599.5, 800.0), "windows"));
assert!(!v.matches(size(399.0, 800.0), "windows"));
let open = FormView::new("sem limite", 0.0);
assert!(open.matches(size(10.0, 10.0), "web") && open.matches(size(99_999.0, 99_999.0), "web"));
let mut tall = FormView::new("alta", 0.0);
(tall.min_height, tall.max_height) = (500.0, 900.0);
assert!(tall.matches(size(300.0, 500.0), "linux") && tall.matches(size(300.0, 900.0), "linux"));
assert!(!tall.matches(size(300.0, 901.0), "linux") && !tall.matches(size(300.0, 499.0), "linux"));
}
#[test]
fn a_square_form_is_landscape() {
let mut portrait = FormView::new("retrato", 0.0);
portrait.orientation = ViewOrientation::Portrait;
let mut landscape = FormView::new("paisagem", 0.0);
landscape.orientation = ViewOrientation::Landscape;
assert!(landscape.matches(size(500.0, 500.0), "ios") && !portrait.matches(size(500.0, 500.0), "ios"));
assert!(portrait.matches(size(500.0, 501.0), "ios") && !landscape.matches(size(500.0, 501.0), "ios"));
}
#[test]
fn the_platforms_restrict_and_empty_means_all() {
let mut mobile = FormView::new("móvel", 0.0);
mobile.platforms = vec!["android".into(), "ios".into()];
assert!(mobile.matches(size(400.0, 800.0), "android"));
assert!(!mobile.matches(size(400.0, 800.0), "windows"));
assert!(FormView::new("todas", 0.0).matches(size(400.0, 800.0), "windows"));
assert!(PLATFORMS.contains(¤t_platform()));
}
#[test]
fn invalid_scales_become_one() {
let mut v = FormView::new("x", 0.0);
for (font, spacing, expected) in [(0.5, 2.0, (0.5, 2.0)), (0.0, 5.0, (1.0, 1.0)), (f32::NAN, 0.25, (1.0, 0.25)), (4.0, f32::INFINITY, (4.0, 1.0))] {
(v.font_scale, v.spacing_scale) = (font, spacing);
assert_eq!(v.scales(), expected, "{font} {spacing}");
}
}
#[test]
fn the_narrowest_view_comes_first_and_no_limit_last() {
let mut ui = Ui::new();
let form = ComponentId { index: 0, generation: 0 };
ui.set_views(form, vec![FormView::new("larga", 0.0), FormView::new("tablet", 1023.0), FormView::new("phone", 599.0)]);
let names: Vec<&str> = ui.views(form).iter().map(|v| v.name.as_str()).collect();
assert_eq!(names, ["phone", "tablet", "larga"]);
}
}