Skip to main content

rmux_core/window/
activity.rs

1use super::{current_unix_timestamp, Window};
2use crate::PaneId;
3
4/// Exact activity state for one pane and its containing shared window.
5///
6/// The fields stay private so callers can only replay a snapshot captured from
7/// another occurrence of the same linked window.
8#[derive(Debug, Clone, Copy, PartialEq, Eq)]
9pub struct WindowPaneActivity {
10    pane_id: PaneId,
11    window_activity_at: i64,
12    pane_activity_at: i64,
13}
14
15impl Window {
16    /// Returns the window creation timestamp as Unix seconds.
17    #[must_use]
18    pub const fn created_at(&self) -> i64 {
19        self.created_at
20    }
21
22    /// Returns the last window activity timestamp as Unix seconds.
23    #[must_use]
24    pub const fn activity_at(&self) -> i64 {
25        self.activity_at
26    }
27
28    /// Records output activity for a specific pane in this window.
29    pub fn touch_activity_for_pane(&mut self, pane_index: u32) -> bool {
30        let Some(position) = self
31            .panes
32            .iter()
33            .position(|pane| pane.index() == pane_index)
34        else {
35            return false;
36        };
37        let now = current_unix_timestamp();
38        self.activity_at = now;
39        self.panes[position].set_activity_at(now);
40        true
41    }
42
43    /// Captures the window and pane activity timestamps for a stable pane.
44    #[must_use]
45    pub fn pane_activity_snapshot(&self, pane_id: PaneId) -> Option<WindowPaneActivity> {
46        let pane = self.panes.iter().find(|pane| pane.id() == pane_id)?;
47        Some(WindowPaneActivity {
48            pane_id,
49            window_activity_at: self.activity_at,
50            pane_activity_at: pane.activity_at(),
51        })
52    }
53
54    /// Applies activity captured from another occurrence of the same linked window.
55    pub fn apply_pane_activity_snapshot(&mut self, activity: WindowPaneActivity) -> bool {
56        let Some(pane) = self
57            .panes
58            .iter_mut()
59            .find(|pane| pane.id() == activity.pane_id)
60        else {
61            return false;
62        };
63        self.activity_at = activity.window_activity_at;
64        pane.set_activity_at(activity.pane_activity_at);
65        true
66    }
67}