rustimal 0.8.0

A minimal, GPU-rendered terminal for Windows that runs Git Bash, with animated split panes and a glass look
//! Saving and restoring the set of panes and their folders.

use super::*;

impl App {
    /// The layout to open at startup: the one asked for with `--layout`, or the last session.
    pub(super) fn startup_layout(&self) -> Option<Layout> {
        if let Some(name) = &self.startup_layout {
            return match layouts::load(name) {
                Ok(layout) => Some(layout),
                Err(e) => {
                    log::warn!("{e}");
                    None
                }
            };
        }
        (self.config.restore_session && !self.explicit_dir).then(layouts::load_session).flatten()
    }

    /// The window's layout: the split tree and each pane's folder.
    pub(super) fn current_layout(&self) -> Option<Layout> {
        let st = self.state.as_ref()?;
        if st.panes.is_empty() {
            return None;
        }
        let cwd = |id: PaneId| {
            st.panes.iter().find(|p| p.id == id).and_then(|p| p.cwd.as_ref()).map(|c| c.to_string_lossy().into_owned())
        };
        Some(Layout::from_node(&st.layout, &cwd))
    }

    /// Remember the layout, so the next start can reopen it.
    pub(super) fn autosave_session(&self) {
        if self.config.restore_session
            && let Some(layout) = self.current_layout()
        {
            layouts::save_session(&layout);
        }
    }

    pub(super) fn save_layout(&mut self, name: &str) {
        let Some(layout) = self.current_layout() else { return };
        match layouts::save(name, &layout) {
            Ok(saved) => self.toast(format!("Saved the layout \u{201c}{saved}\u{201d}")),
            Err(e) => self.toast(e),
        }
    }

    pub(super) fn load_layout(&mut self, name: &str) {
        match layouts::load(name) {
            Ok(layout) => {
                if !self.apply_layout(layout, false) {
                    self.toast("Could not open the layout");
                }
            }
            Err(e) => self.toast(e),
        }
    }

    /// Replace all the panes with a layout's. `instant` skips the animation (at startup).
    /// Returns false if no pane could be started.
    pub(super) fn apply_layout(&mut self, layout: Layout, instant: bool) -> bool {
        let Some(mut st) = self.state.take() else { return false };
        let (node, leaves) = match layout.build(st.next_id) {
            Ok(built) => built,
            Err(e) => {
                self.state = Some(st);
                self.toast(e);
                return false;
            }
        };
        // Retire the panes that are there: they fade out where they are.
        let now = Instant::now();
        for mut pane in std::mem::take(&mut st.panes) {
            pane.shutdown();
            if !instant && self.config.animation_speed > 0.0 {
                pane.opacity_anim.set(0.0, self.dur(FADE_OUT_SECS), now);
                st.closing.push(pane);
            }
        }
        st.zoomed = None;
        st.picker = None;
        st.overlay = None;
        st.hover_link = None;
        st.layout = node;
        st.next_id = leaves.iter().map(|l| l.id).max().map_or(st.next_id, |m| m + 1);
        let rects = self.target_rects(&st);
        for leaf in &leaves {
            let target = rects.iter().find(|(id, _)| *id == leaf.id).map(|(_, r)| *r).unwrap_or_default();
            let cwd = leaf.cwd.clone().filter(|p| p.is_dir()).or_else(|| self.start_dir.clone());
            if !self.spawn_pane(&mut st, leaf.id, target, target, cwd) {
                st.layout.remove(leaf.id);
                continue;
            }
            if let Some(pane) = st.panes.iter_mut().find(|p| p.id == leaf.id) {
                if instant {
                    pane.opacity_anim.snap(1.0);
                    pane.opacity = 1.0;
                }
                if let Some(run) = &leaf.run {
                    pane.start_typing(run.clone(), None);
                }
            }
        }
        let started = !st.panes.is_empty();
        if started {
            st.focused = st.layout.first_leaf();
            st.last_focused = st.focused;
        }
        self.state = Some(st);
        if started {
            self.relayout(instant, true);
            self.autosave_session();
        }
        started
    }
}