1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
//! ScrollArea
//!
//! A styled scrollable container component with support for vertical and/or horizontal scrolling.
//!
//! # Example
//!
//! ```ignore
//! use gpuikit::elements::scroll_area::scroll_area;
//!
//! // Vertical scroll area with max height
//! scroll_area("my-scroll-area")
//! .max_h(px(300.))
//! .vertical()
//! .child(long_content)
//!
//! // Horizontal scroll area
//! scroll_area("horiz-scroll")
//! .max_w(px(400.))
//! .horizontal()
//! .child(wide_content)
//!
//! // Both directions
//! scroll_area("both-scroll")
//! .max_h(px(300.))
//! .max_w(px(400.))
//! .both()
//! .child(large_content)
//! ```
use gpui::{
div, prelude::*, px, AnyElement, App, ElementId, IntoElement, Length, ParentElement,
RenderOnce, Styled, Window,
};
/// Scroll direction for the scroll area
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum ScrollDirection {
/// Vertical scrolling only
#[default]
Vertical,
/// Horizontal scrolling only
Horizontal,
/// Both vertical and horizontal scrolling
Both,
}
/// Creates a new scroll area builder.
///
/// # Arguments
///
/// * `id` - Unique identifier for the scroll area
///
/// # Example
///
/// ```ignore
/// scroll_area("my-scroll-area")
/// .max_h(px(300.))
/// .vertical()
/// .child(content)
/// ```
pub fn scroll_area(id: impl Into<ElementId>) -> ScrollArea {
ScrollArea::new(id)
}
/// A styled scrollable container component.
///
/// Use the [`scroll_area`] function to create an instance.
#[derive(IntoElement)]
pub struct ScrollArea {
id: ElementId,
direction: ScrollDirection,
max_height: Option<Length>,
max_width: Option<Length>,
children: Vec<AnyElement>,
full_width: bool,
full_height: bool,
}
impl ScrollArea {
/// Creates a new scroll area with default settings.
pub fn new(id: impl Into<ElementId>) -> Self {
Self {
id: id.into(),
direction: ScrollDirection::Vertical,
max_height: None,
max_width: None,
children: Vec::new(),
full_width: false,
full_height: false,
}
}
/// Sets the scroll direction to vertical only.
pub fn vertical(mut self) -> Self {
self.direction = ScrollDirection::Vertical;
self
}
/// Sets the scroll direction to horizontal only.
pub fn horizontal(mut self) -> Self {
self.direction = ScrollDirection::Horizontal;
self
}
/// Sets the scroll direction to both vertical and horizontal.
pub fn both(mut self) -> Self {
self.direction = ScrollDirection::Both;
self
}
/// Sets the scroll direction.
pub fn direction(mut self, direction: ScrollDirection) -> Self {
self.direction = direction;
self
}
/// Sets the maximum height of the scroll area.
pub fn max_h(mut self, height: impl Into<Length>) -> Self {
self.max_height = Some(height.into());
self
}
/// Sets the maximum height in pixels.
pub fn max_h_px(self, height: f32) -> Self {
self.max_h(px(height))
}
/// Sets the maximum width of the scroll area.
pub fn max_w(mut self, width: impl Into<Length>) -> Self {
self.max_width = Some(width.into());
self
}
/// Sets the maximum width in pixels.
pub fn max_w_px(self, width: f32) -> Self {
self.max_w(px(width))
}
/// Make the scroll area expand to fill available width.
pub fn full_width(mut self, full_width: bool) -> Self {
self.full_width = full_width;
self
}
/// Make the scroll area expand to fill available height.
pub fn full_height(mut self, full_height: bool) -> Self {
self.full_height = full_height;
self
}
/// Adds a child element to the scroll area.
pub fn child(mut self, child: impl IntoElement) -> Self {
self.children.push(child.into_any_element());
self
}
/// Adds multiple child elements to the scroll area.
pub fn children(mut self, children: impl IntoIterator<Item = impl IntoElement>) -> Self {
self.children
.extend(children.into_iter().map(|c| c.into_any_element()));
self
}
}
impl RenderOnce for ScrollArea {
fn render(self, _window: &mut Window, _cx: &mut App) -> impl IntoElement {
// Build base container with id for scroll state tracking
let container = div()
.id(self.id)
.flex()
.when(
self.direction == ScrollDirection::Vertical,
|this| this.flex_col(),
)
.when(self.full_width, |this| this.w_full())
.when(self.full_height, |this| this.h_full())
.when_some(self.max_height, |this, height| this.max_h(height))
.when_some(self.max_width, |this, width| this.max_w(width));
// Apply scroll behavior based on direction
let container = match self.direction {
ScrollDirection::Vertical => container.overflow_y_scroll().overflow_x_hidden(),
ScrollDirection::Horizontal => container.overflow_x_scroll().overflow_y_hidden(),
ScrollDirection::Both => container.overflow_y_scroll().overflow_x_scroll(),
};
// Stop scroll wheel propagation to prevent parent scrolling
container
.on_scroll_wheel(|_, _, cx| {
cx.stop_propagation();
})
.children(self.children)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_scroll_direction_default() {
assert_eq!(ScrollDirection::default(), ScrollDirection::Vertical);
}
#[test]
fn test_scroll_area_builder() {
let area = scroll_area("test-id").vertical();
assert_eq!(area.direction, ScrollDirection::Vertical);
let area = scroll_area("test-id").horizontal();
assert_eq!(area.direction, ScrollDirection::Horizontal);
let area = scroll_area("test-id").both();
assert_eq!(area.direction, ScrollDirection::Both);
}
}