Skip to main content

libsurfer/
item_drawing_info.rs

1use crate::{
2    displayed_item::DisplayedFieldRef, displayed_item_tree::VisibleItemIndex,
3    transaction_container::TransactionStreamRef, wave_container::FieldRef,
4};
5
6#[derive(Debug)]
7pub struct VariableDrawingInfo {
8    pub field_ref: FieldRef,
9    pub displayed_field_ref: DisplayedFieldRef,
10    pub vidx: VisibleItemIndex,
11    pub top: f32,
12    pub bottom: f32,
13}
14
15#[derive(Debug)]
16pub struct DividerDrawingInfo {
17    pub vidx: VisibleItemIndex,
18    pub top: f32,
19    pub bottom: f32,
20}
21
22#[derive(Debug)]
23pub struct MarkerDrawingInfo {
24    pub vidx: VisibleItemIndex,
25    pub top: f32,
26    pub bottom: f32,
27    pub idx: u8,
28}
29
30#[derive(Debug)]
31pub struct TimeLineDrawingInfo {
32    pub vidx: VisibleItemIndex,
33    pub top: f32,
34    pub bottom: f32,
35}
36
37#[derive(Debug)]
38pub struct StreamDrawingInfo {
39    pub transaction_stream_ref: TransactionStreamRef,
40    pub vidx: VisibleItemIndex,
41    pub top: f32,
42    pub bottom: f32,
43}
44
45#[derive(Debug)]
46pub struct GroupDrawingInfo {
47    pub vidx: VisibleItemIndex,
48    pub top: f32,
49    pub bottom: f32,
50}
51
52#[derive(Debug)]
53pub struct PlaceholderDrawingInfo {
54    pub vidx: VisibleItemIndex,
55    pub top: f32,
56    pub bottom: f32,
57}
58
59pub enum ItemDrawingInfo {
60    Variable(VariableDrawingInfo),
61    Divider(DividerDrawingInfo),
62    Marker(MarkerDrawingInfo),
63    TimeLine(TimeLineDrawingInfo),
64    Stream(StreamDrawingInfo),
65    Group(GroupDrawingInfo),
66    Placeholder(PlaceholderDrawingInfo),
67}
68
69impl ItemDrawingInfo {
70    #[must_use]
71    pub fn top(&self) -> f32 {
72        match self {
73            ItemDrawingInfo::Variable(drawing_info) => drawing_info.top,
74            ItemDrawingInfo::Divider(drawing_info) => drawing_info.top,
75            ItemDrawingInfo::Marker(drawing_info) => drawing_info.top,
76            ItemDrawingInfo::TimeLine(drawing_info) => drawing_info.top,
77            ItemDrawingInfo::Stream(drawing_info) => drawing_info.top,
78            ItemDrawingInfo::Group(drawing_info) => drawing_info.top,
79            ItemDrawingInfo::Placeholder(drawing_info) => drawing_info.top,
80        }
81    }
82    #[must_use]
83    pub fn bottom(&self) -> f32 {
84        match self {
85            ItemDrawingInfo::Variable(drawing_info) => drawing_info.bottom,
86            ItemDrawingInfo::Divider(drawing_info) => drawing_info.bottom,
87            ItemDrawingInfo::Marker(drawing_info) => drawing_info.bottom,
88            ItemDrawingInfo::TimeLine(drawing_info) => drawing_info.bottom,
89            ItemDrawingInfo::Stream(drawing_info) => drawing_info.bottom,
90            ItemDrawingInfo::Group(drawing_info) => drawing_info.bottom,
91            ItemDrawingInfo::Placeholder(drawing_info) => drawing_info.bottom,
92        }
93    }
94    #[must_use]
95    pub fn vidx(&self) -> VisibleItemIndex {
96        match self {
97            ItemDrawingInfo::Variable(drawing_info) => drawing_info.vidx,
98            ItemDrawingInfo::Divider(drawing_info) => drawing_info.vidx,
99            ItemDrawingInfo::Marker(drawing_info) => drawing_info.vidx,
100            ItemDrawingInfo::TimeLine(drawing_info) => drawing_info.vidx,
101            ItemDrawingInfo::Stream(drawing_info) => drawing_info.vidx,
102            ItemDrawingInfo::Group(drawing_info) => drawing_info.vidx,
103            ItemDrawingInfo::Placeholder(drawing_info) => drawing_info.vidx,
104        }
105    }
106
107    pub(crate) fn get_y_from_anchor(&self, anchor: &crate::graphics::Anchor) -> f32 {
108        match anchor {
109            crate::graphics::Anchor::Top => self.top(),
110            crate::graphics::Anchor::Center => self.center(),
111            crate::graphics::Anchor::Percentual(p) => self.top() + p * (self.height()),
112            crate::graphics::Anchor::Bottom => self.bottom(),
113        }
114    }
115
116    pub(crate) fn center(&self) -> f32 {
117        self.top().midpoint(self.bottom())
118    }
119
120    pub(crate) fn height(&self) -> f32 {
121        self.bottom() - self.top()
122    }
123
124    /// Inverse of `get_y_from_anchor(Anchor::Percentual(_))`: where `y` falls within this
125    /// row's `[top, bottom]` range, as a fraction (0 = top, 1 = bottom).
126    #[must_use]
127    pub(crate) fn percent_of(&self, y: f32) -> f32 {
128        (y - self.top()) / self.height()
129    }
130
131    /// `top()` translated by `offset` (e.g. a panel's current on-screen starting position),
132    /// without allocating a full translated copy.
133    #[must_use]
134    pub(crate) fn top_at(&self, offset: f32) -> f32 {
135        self.top() + offset
136    }
137
138    /// `bottom()` translated by `offset`.
139    #[must_use]
140    pub(crate) fn bottom_at(&self, offset: f32) -> f32 {
141        self.bottom() + offset
142    }
143
144    /// True if this row overlaps `[clip_top, clip_bottom]` (must be in the same coordinate
145    /// space as `self`, e.g. both offset-free/canonical or both shifted by the same amount).
146    #[must_use]
147    pub(crate) fn overlaps(&self, clip_top: f32, clip_bottom: f32) -> bool {
148        self.bottom() >= clip_top && self.top() <= clip_bottom
149    }
150}