zaxis 0.0.5

An event-driven immediate mode GUI for desktop tools
Documentation
use super::Ui;
use crate::{layout::LayoutCursor, AccessRole, Id, Layout, Rect, Vec2};
use std::hash::Hash;

pub struct TabPagesState {
    pub last_frame: u64,
    current: usize,
    outgoing: Option<usize>,
    pending: Option<usize>,
    direction: f32,
}

impl Ui<'_> {
    /// A stable, scoped channel ID for an application animation/property.
    pub fn animation_id(&self, source: impl Hash) -> Id {
        self.scope.with(("animation", Id::new(source)))
    }
    pub fn transition<T: crate::Interpolate>(
        &mut self,
        source: impl Hash,
        target: T,
        options: crate::TweenOptions,
    ) -> crate::Animated<T> {
        self.context.transition_visible(
            self.animation_id(source),
            None,
            target,
            options,
            !self.clip_rect().is_empty(),
        )
    }
    /// See [`crate::Context::transition_smooth`].
    pub fn transition_smooth<T: crate::SpringValue + crate::Interpolate>(
        &mut self,
        source: impl Hash,
        target: T,
        options: crate::TweenOptions,
    ) -> crate::Animated<T> {
        self.context.transition_smooth_visible(
            self.animation_id(source),
            target,
            options,
            !self.clip_rect().is_empty(),
        )
    }
    pub fn transition_from<T: crate::Interpolate>(
        &mut self,
        source: impl Hash,
        initial: T,
        target: T,
        options: crate::TweenOptions,
    ) -> crate::Animated<T> {
        self.context.transition_visible(
            self.animation_id(source),
            Some(initial),
            target,
            options,
            !self.clip_rect().is_empty(),
        )
    }
    pub fn animate<T: Clone + 'static, A: crate::Animation<T>>(
        &mut self,
        source: impl Hash,
        create: impl FnOnce() -> A,
    ) -> crate::Animated<T> {
        self.animate_with(source, crate::AnimationOptions::default(), create)
    }
    pub fn animate_with<T: Clone + 'static, A: crate::Animation<T>>(
        &mut self,
        source: impl Hash,
        options: crate::AnimationOptions,
        create: impl FnOnce() -> A,
    ) -> crate::Animated<T> {
        let pass = self.context.animation_pass(!self.clip_rect().is_empty());
        self.context
            .animations
            .animate(self.animation_id(source), options, create, pass)
    }

    pub(super) fn transition_property<T: crate::Interpolate>(
        &mut self,
        id: Id,
        rect: Rect,
        target: T,
        options: crate::TweenOptions,
    ) -> T {
        self.context
            .transition_visible(
                id,
                None,
                target,
                options,
                !rect.intersect(self.clip_rect()).is_empty(),
            )
            .value
    }

    /// Directional page slides in a fixed allocation. While switching, only the
    /// latest requested index is queued. `build` runs for both visible pages during
    /// a slide; controls are disabled until it ends. Hidden pages are not built.
    /// The translated layout drives paint and hit geometry under a fixed clip.
    pub fn tab_pages(
        &mut self,
        source: impl Hash,
        selected: usize,
        size: Vec2,
        build: impl FnMut(&mut Ui<'_>, usize),
    ) {
        self.tab_pages_with(source, selected, size, true, build);
    }

    /// Like [`Self::tab_pages`], but the pages keep their normal look and input while they
    /// slide: nothing is disabled or dimmed, so the motion is the only change.
    pub fn tab_pages_live(
        &mut self,
        source: impl Hash,
        selected: usize,
        size: Vec2,
        build: impl FnMut(&mut Ui<'_>, usize),
    ) {
        self.tab_pages_with(source, selected, size, false, build);
    }

    fn tab_pages_with(
        &mut self,
        source: impl Hash,
        selected: usize,
        size: Vec2,
        dim: bool,
        mut build: impl FnMut(&mut Ui<'_>, usize),
    ) {
        let id = self.scope.with(("tab-pages", Id::new(source)));
        let rect = self.allocate_space(size.min(Vec2::new(self.available_width(), size.y)));
        let mut state = self
            .context
            .containers
            .tab_pages
            .remove(&id)
            .unwrap_or(TabPagesState {
                last_frame: 0,
                current: selected,
                outgoing: None,
                pending: None,
                direction: 1.0,
            });
        state.last_frame = self.context.frame;
        let progress_id = id.with("slide");
        let mut progress = 1.0;
        if state.outgoing.is_some() {
            state.pending = (selected != state.current).then_some(selected);
            let pass = self
                .context
                .animation_pass(!rect.intersect(self.clip_rect()).is_empty());
            let motion = self
                .context
                .animations
                .read::<f32>(progress_id, pass)
                .unwrap();
            progress = motion.value;
            if motion.completed() {
                state.outgoing = None;
            }
        }
        if state.outgoing.is_none() {
            let next = state.pending.take().unwrap_or(selected);
            if next != state.current {
                state.direction = if next > state.current { 1.0 } else { -1.0 };
                state.outgoing = Some(state.current);
                state.current = next;
                let tween =
                    crate::Tween::with_options(0.0_f32, 1.0, self.style().motion.page.clone());
                self.context.restart_animation(progress_id, tween);
                let pass = self
                    .context
                    .animation_pass(!rect.intersect(self.clip_rect()).is_empty());
                let motion = self
                    .context
                    .animations
                    .read::<f32>(progress_id, pass)
                    .unwrap();
                progress = motion.value;
                if motion.completed() {
                    state.outgoing = None;
                }
            }
        }
        let switching = state.outgoing.is_some();
        let distance = (rect.size().x + 20.0) * state.direction;
        if let Some(outgoing) = state.outgoing {
            // The page sliding away is decoration: assistive technology sees one page.
            let leaving = id.with(("page", outgoing));
            let scope = self.a11y_begin(leaving, AccessRole::TabPanel, |node| {
                node.hidden(true);
            });
            self.build_page(
                id,
                outgoing,
                rect,
                -distance * progress,
                false,
                dim,
                &mut build,
            );
            self.a11y_end(scope, None);
        }
        let panel = id.with(("page", state.current));
        let tab = self.selected_tab(panel);
        let scope = self.a11y_begin(panel, AccessRole::TabPanel, |node| {
            if let Some(tab) = tab {
                node.labelled_by(tab);
            }
        });
        self.build_page(
            id,
            state.current,
            rect,
            if switching {
                distance * (1.0 - progress)
            } else {
                0.0
            },
            !switching,
            dim,
            &mut build,
        );
        self.a11y_end(scope, Some(rect));
        self.context.containers.tab_pages.insert(id, state);
    }

    /// The tab that `panel` belongs to: the selected tab of the tab list built last before
    /// the pages in this window (`tab_bar`, `IconTabs`). The pages take an index and know no
    /// tab, so the order of building is the link; the tab is told which panel it controls.
    fn selected_tab(&mut self, panel: Id) -> Option<Id> {
        if !self.context.a11y_on() {
            return None;
        }
        let window = self.window;
        for index in (0..self.context.a11y_len()).rev() {
            let node = self.context.a11y_node_mut(index)?;
            if node.layer == window && node.role == AccessRole::Tab && node.selected == Some(true) {
                node.controls(panel);
                return Some(node.id);
            }
        }
        None
    }

    fn build_page(
        &mut self,
        id: Id,
        index: usize,
        rect: Rect,
        offset: f32,
        interactive: bool,
        dim: bool,
        build: &mut impl FnMut(&mut Ui<'_>, usize),
    ) {
        let bounds = rect.translate(Vec2::new(offset, 0.0));
        let clip = self.clip.intersect(rect).intersect(bounds);
        if clip.is_empty() {
            return;
        }
        let spacing = self.style().spacing.max(0.0);
        let mut page = Ui {
            flow: None,
            context: self.context,
            window: self.window,
            scope: id.with(("page", index)),
            sequence: 0,
            clip,
            layout: LayoutCursor::new(bounds, Layout::Vertical, spacing),
            enabled: self.enabled && (interactive || !dim),
            backdrop_blur: self.backdrop_blur,
            hover_style: self.hover_style,
            local_style: self.local_style.clone(),
            local_style_revision: self.local_style_revision,
        };
        page.begin_layout(crate::Align::Start);
        build(&mut page, index);
        page.finish_layout();
    }
}