rustimal 0.8.0

A minimal, GPU-rendered terminal for Windows that runs Git Bash, with animated split panes and a glass look
//! Drawing a frame, and everything that has to be settled first.

use super::*;

impl App {
    /// Sample all animations for this frame; returns true while anything is still moving.
    pub(super) fn animate(&mut self) -> bool {
        let focus_d = self.dur(FOCUS_SECS);
        let Some(st) = self.state.as_mut() else { return false };
        let now = Instant::now();
        st.now = now;
        let mut moving = false;

        st.multi_anim.set(if st.panes.len() > 1 { 1.0 } else { 0.0 }, focus_d, now);
        st.multi = st.multi_anim.get(now);
        moving |= st.multi_anim.active(now);

        let focused = st.focused;
        let win_focused = st.window_focused;
        if focused != st.last_focused {
            // Restart the blink so the cursor shows right away in the newly focused pane.
            st.last_focused = focused;
            st.blink_epoch = now;
        }
        for p in st.panes.iter_mut() {
            p.focus_anim.set(if p.id == focused && win_focused { 1.0 } else { 0.0 }, focus_d, now);
            p.rect = p.rect_anim.get(now);
            p.focus = p.focus_anim.get(now);
            p.opacity = p.opacity_anim.get(now);
            p.bell = p.bell_anim.get(now);
            moving |= p.rect_anim.active(now)
                || p.focus_anim.active(now)
                || p.opacity_anim.active(now)
                || p.bell_anim.active(now);
        }
        st.closing.retain(|p| p.opacity_anim.active(now));
        for p in st.closing.iter_mut() {
            p.opacity = p.opacity_anim.get(now);
        }
        if st.picker.as_ref().is_some_and(|p| p.closing && !p.opacity_anim.active(now)) {
            st.picker = None;
        }
        moving || !st.closing.is_empty() || st.picker.as_ref().is_some_and(|p| p.animating)
    }

    pub(super) fn redraw(&mut self) {
        let animating = self.animate();
        self.sync_web();
        self.sync_hosts();
        let Some(st) = self.state.as_mut() else { return };

        let mut pending = false;
        for p in st.panes.iter_mut() {
            pending |= p.flush_grid(RESIZE_DEBOUNCE);
        }

        // Pick up new Claude status data; usage is account-wide, so remember the latest.
        let mut usage = st.usage;
        for p in st.panes.iter_mut() {
            if p.poll_status()
                && let Some(s) = &p.claude_status
            {
                usage.five_hour = s.five_hour.or(usage.five_hour);
                usage.seven_day = s.seven_day.or(usage.seven_day);
            }
        }
        if usage != st.usage {
            st.usage = usage;
            if let Some(dir) = crate::config::state_dir() {
                usage.save(&dir.join("claude-usage.json"));
            }
        }
        let claude_visible = st.panes.iter().any(|p| p.claude);

        // Matches go stale as output arrives; recompute them, but not on every frame.
        let mut search_wait: Option<Duration> = None;
        for p in st.panes.iter_mut() {
            if let Some(s) = p.search.as_mut()
                && let Some(wait) = s.refresh_in()
            {
                if wait.is_zero() {
                    s.refresh(&p.term.lock());
                } else {
                    search_wait = Some(search_wait.map_or(wait, |w| w.min(wait)));
                }
            }
        }

        if st.toast.as_ref().is_some_and(|(_, since)| since.elapsed() >= TOAST_SECS) {
            st.toast = None;
        }

        // Commands being typed for clicks: carry on, and fall back to opening what was clicked.
        let mut typing = false;
        for p in st.panes.iter_mut() {
            if let Some(target) = p.pump() {
                links::open(&target, &self.config.editor);
            }
            typing |= p.is_typing();
        }

        let title = match (&st.notice, st.panes.iter().find(|p| p.id == st.focused)) {
            (Some(notice), _) if !st.window_focused => notice.clone(),
            (_, Some(p)) if p.claude && !p.claude_title.is_empty() => {
                format!("{} · {}", p.display_name(), p.claude_title)
            }
            (_, Some(p)) if p.claude => format!("{} · claude", p.display_name()),
            (_, Some(p)) => p.display_name(),
            (_, None) => "rustimal".to_string(),
        };
        if title != st.window_title {
            st.window.set_title(&title);
            st.window_title = title;
        }

        let blink_wait = render::build_scene(st, &self.config, &self.palette);
        for u in st.glyphs.uploads.drain(..) {
            st.gpu.upload(u.x, u.y, u.w, u.h, &u.data);
        }
        let clear = render::clear_color(&self.config, &self.palette, st.gpu.transparent);
        st.gpu.render(&st.window, &st.quads, clear);

        if !st.shown {
            st.shown = true;
            st.window.set_visible(true);
            crate::platform::glass::hide_caption_buttons(&st.window);
        }

        if animating || st.glyphs.overflowed {
            st.glyphs.overflowed = false;
            st.wake_at = None;
            st.window.request_redraw();
        } else {
            let mut wake: Option<Instant> = blink_wait.map(|d| Instant::now() + d);
            if pending {
                let t = Instant::now() + Duration::from_millis(16);
                wake = Some(wake.map_or(t, |w| w.min(t)));
            }
            if claude_visible {
                // Keep reset countdowns current and catch status updates while Claude is idle.
                let t = Instant::now() + Duration::from_secs(2);
                wake = Some(wake.map_or(t, |w| w.min(t)));
            }
            if let Some(wait) = search_wait {
                let t = Instant::now() + wait;
                wake = Some(wake.map_or(t, |w| w.min(t)));
            }
            if typing {
                let t = Instant::now() + Duration::from_millis(20);
                wake = Some(wake.map_or(t, |w| w.min(t)));
            }
            // The spinner and timer of a conversation that is working.
            if st.panes.iter().any(|p| p.chat.as_ref().is_some_and(|c| !c.hidden && c.running())) {
                let t = Instant::now() + chat::CHAT_TICK;
                wake = Some(wake.map_or(t, |w| w.min(t)));
            }
            // Looking for a program's window, and at where it has the keyboard.
            if st.web.hosts.busy() {
                let t = Instant::now() + host::TICK;
                wake = Some(wake.map_or(t, |w| w.min(t)));
            }
            if let Some((_, since)) = &st.toast {
                // Redraw while the message fades, then once more to remove it.
                let t = Instant::now() + Duration::from_millis(40).min(TOAST_SECS.saturating_sub(since.elapsed()));
                wake = Some(wake.map_or(t, |w| w.min(t)));
            }
            st.wake_at = wake;
        }
    }
}