-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmod.rs
More file actions
507 lines (470 loc) · 17.4 KB
/
Copy pathmod.rs
File metadata and controls
507 lines (470 loc) · 17.4 KB
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
//! SidebarShell component providing a resizable sidebar panel with glass effects.
//!
//! This module provides a reusable sidebar shell that handles:
//! - Resizable panel with configurable min/max width constraints
//! - Theme-controlled panel shadow elevation
//! - Glass surface effects via SurfacePreset
//! - Draggable resize handle on the inner edge
//! - Support for left/right placement
//!
//! # Resize Model
//!
//! SidebarShell uses a consumer-managed resize model. The component provides:
//! - `on_resize_start`: Called when the user starts dragging the resizer (mouse down)
//! - `on_resize_end`: Called when the user stops dragging (mouse up)
//!
//! The consumer is responsible for:
//! - Tracking drag state (resizing: bool, start_x, start_width)
//! - Handling mouse move events at the window/root level
//! - Calculating and applying the new width
//!
//! This model is necessary because GPUI's `RenderOnce` components cannot use
//! `cx.listener()` or `window.listener_for()` patterns.
//!
//! # Example
//!
//! ```rust,ignore
//! use ui::sidebar_shell::SidebarShell;
//!
//! SidebarShell::left(px(self.sidebar_width))
//! .min_width(px(200.0))
//! .max_width(px(400.0))
//! .on_resize_start(move |width, x, _window, cx| {
//! // Store resize start state: width, x position
//! })
//! .on_resize_end(move |_window, cx| {
//! // Clear resize state
//! })
//! .child(sidebar_content)
//! ```
use std::rc::Rc;
use gpui::{
AnyElement, App, BoxShadow, Hsla, InteractiveElement, IntoElement, ParentElement, Pixels,
RenderOnce, StyleRefinement, Styled, Window, div, hsla, point, prelude::FluentBuilder, px,
};
use smallvec::SmallVec;
use crate::{
ActiveTheme, ElevationToken, Side, StyledExt, SurfaceContext, SurfacePreset,
global_state::GlobalState,
};
/// Default values for sidebar shell configuration.
const DEFAULT_MIN_WIDTH: f32 = 200.0;
const DEFAULT_MAX_WIDTH: f32 = 400.0;
const DEFAULT_RESIZER_WIDTH: f32 = 6.0;
/// Creates a 3-layer shadow effect for elevated sidebar panels.
///
/// This shadow configuration provides a natural depth effect with:
/// - Subtle near-edge shadow (4% opacity)
/// - Medium distance shadow (8% opacity)
/// - Far distance shadow (12% opacity)
pub fn sidebar_shadow() -> Vec<BoxShadow> {
vec![
BoxShadow {
color: hsla(0., 0., 0., 0.04),
offset: point(px(0.0), px(1.0)),
blur_radius: px(6.0),
spread_radius: px(0.0),
inset: false,
},
BoxShadow {
color: hsla(0., 0., 0., 0.08),
offset: point(px(0.0), px(8.0)),
blur_radius: px(22.0),
spread_radius: px(0.0),
inset: false,
},
BoxShadow {
color: hsla(0., 0., 0., 0.12),
offset: point(px(0.0), px(22.0)),
blur_radius: px(54.0),
spread_radius: px(0.0),
inset: false,
},
]
}
/// A resizable sidebar panel with a theme-controlled shadow and glass surface effects.
///
/// SidebarShell provides a container for sidebar content that handles:
/// - Absolute positioning with configurable inset from window edges
/// - A single complete panel shadow, controlled by its elevation
/// - Glass blur and noise effects via SurfacePreset::panel()
/// - Draggable resize handle with hover feedback
///
/// By default, the shell uses the theme's panel elevation. Explicit elevation overrides may
/// require a larger inset near window edges to avoid native-window clipping.
///
/// The component uses a builder pattern for configuration and implements
/// `ParentElement` for adding child content, `Styled` for style refinement,
/// and `RenderOnce` for rendering.
///
/// # Resize Model
///
/// Uses consumer-managed resize. The component fires `on_resize_start` and
/// `on_resize_end` callbacks, but the consumer must handle mouse move events
/// at the window level to track the actual resize operation.
///
/// # Layout Structure
///
/// ```text
/// +-- Outer Container (absolute, inset from edges) --+
/// | +-- Surface (shadow + glass effects) --------+ |
/// | | | |
/// | | [Child Content] | |
/// | | | |
/// | +--------------------------------------------+ |
/// | [Resizer Handle] (on inner edge) |
/// +------------------------------------------------+
/// ```
#[derive(IntoElement)]
pub struct SidebarShell {
/// Current width of the sidebar in pixels.
width: Pixels,
/// Minimum width constraint for resizing.
min_width: Pixels,
/// Maximum width constraint for resizing.
max_width: Pixels,
/// Width of the resize handle in pixels.
resizer_width: Pixels,
/// Optional override for resizer hover background color.
resizer_hover_bg: Option<Hsla>,
/// Callback invoked when resize starts (mouse down on resizer).
/// Receives: (current_width, mouse_x, window, cx)
on_resize_start: Option<Rc<dyn Fn(Pixels, Pixels, &mut Window, &mut App)>>,
/// Callback invoked when resize ends (mouse up).
on_resize_end: Option<Rc<dyn Fn(&mut Window, &mut App)>>,
/// Optional shadow elevation override for the complete sidebar panel.
/// If `None`, the theme's panel elevation is used.
elevation: Option<ElevationToken>,
/// Placement side (left or right).
side: Side,
/// Inset from window edges in pixels. If `None`, inherits from context.
inset: Option<Pixels>,
/// Additional top inset applied above the inherited/explicit inset.
top_inset: Pixels,
/// Whether blur effects are enabled for the glass surface.
/// If `None`, the value is inherited from the parent context (e.g., WindowShell).
/// If `Some(value)`, the explicit value is used.
blur_enabled: Option<bool>,
/// Child elements rendered inside the surface.
children: SmallVec<[AnyElement; 1]>,
/// Style refinement for the outer container.
style: StyleRefinement,
}
impl SidebarShell {
/// Creates a new left-aligned sidebar shell with the specified width.
///
/// # Arguments
///
/// * `width` - The initial width of the sidebar in pixels.
///
/// # Example
///
/// ```rust,ignore
/// let sidebar = SidebarShell::left(px(260.0));
/// ```
pub fn left(width: impl Into<Pixels>) -> Self {
Self::new(width, Side::Left)
}
/// Creates a new right-aligned sidebar shell with the specified width.
///
/// # Arguments
///
/// * `width` - The initial width of the sidebar in pixels.
///
/// # Example
///
/// ```rust,ignore
/// let sidebar = SidebarShell::right(px(300.0));
/// ```
pub fn right(width: impl Into<Pixels>) -> Self {
Self::new(width, Side::Right)
}
fn new(width: impl Into<Pixels>, side: Side) -> Self {
Self {
width: width.into(),
min_width: px(DEFAULT_MIN_WIDTH),
max_width: px(DEFAULT_MAX_WIDTH),
resizer_width: px(DEFAULT_RESIZER_WIDTH),
resizer_hover_bg: None,
on_resize_start: None,
on_resize_end: None,
elevation: None,
side,
inset: None,
top_inset: px(0.0),
blur_enabled: None, // Inherit from context by default
children: SmallVec::new(),
style: StyleRefinement::default(),
}
}
/// Sets the minimum width constraint for resizing.
///
/// The sidebar cannot be resized smaller than this width.
/// Default: 200px.
pub fn min_width(mut self, width: impl Into<Pixels>) -> Self {
self.min_width = width.into();
self
}
/// Sets the maximum width constraint for resizing.
///
/// The sidebar cannot be resized larger than this width.
/// Default: 400px.
pub fn max_width(mut self, width: impl Into<Pixels>) -> Self {
self.max_width = width.into();
self
}
/// Sets the width of the resize handle.
///
/// Default: 6px.
pub fn resizer_width(mut self, width: impl Into<Pixels>) -> Self {
self.resizer_width = width.into();
self
}
/// Sets the hover background color for the resize handle.
///
/// If not set, defaults to theme foreground at 20% opacity.
pub fn resizer_hover_bg(mut self, color: impl Into<Hsla>) -> Self {
self.resizer_hover_bg = Some(color.into());
self
}
/// Sets the callback invoked when resize starts (mouse down on resizer).
///
/// The callback receives the current width and mouse X position.
/// The consumer should store these to calculate width delta during mouse move.
///
/// # Example
///
/// ```rust,ignore
/// SidebarShell::left(px(260.0))
/// .on_resize_start(|width, x, window, cx| {
/// // Store: resizing = true, start_width = width, start_x = x
/// })
/// ```
pub fn on_resize_start(
mut self,
callback: impl Fn(Pixels, Pixels, &mut Window, &mut App) + 'static,
) -> Self {
self.on_resize_start = Some(Rc::new(callback));
self
}
/// Sets the callback invoked when resize ends (mouse up).
///
/// The consumer should clear their resize state.
///
/// # Example
///
/// ```rust,ignore
/// SidebarShell::left(px(260.0))
/// .on_resize_end(|window, cx| {
/// // Store: resizing = false
/// })
/// ```
pub fn on_resize_end(mut self, callback: impl Fn(&mut Window, &mut App) + 'static) -> Self {
self.on_resize_end = Some(Rc::new(callback));
self
}
/// Sets the inset from window edges.
///
/// This creates space between the sidebar and the window bounds.
/// Default: inherited from context (4px when no parent scope provides a value).
pub fn inset(mut self, inset: impl Into<Pixels>) -> Self {
self.inset = Some(inset.into());
self
}
/// Sets additional top inset on top of the base inset.
///
/// Default: 0px.
pub fn top_inset(mut self, inset: impl Into<Pixels>) -> Self {
self.top_inset = inset.into();
self
}
/// Explicitly sets whether blur effects are enabled for the glass surface.
///
/// When set, this value overrides any inherited context from the parent
/// `WindowShell`. When not called, the sidebar inherits `blur_enabled`
/// from the parent context.
///
/// When disabled, the surface will not render backdrop blur or noise
/// overlays, which can improve performance on systems that don't
/// support blur effects well.
///
/// # Example
///
/// ```rust,ignore
/// // Inherit from WindowShell context (default behavior)
/// SidebarShell::left(px(260.0))
/// .child(content)
///
/// // Explicitly override to disable blur
/// SidebarShell::left(px(260.0))
/// .blur_enabled(false)
/// .child(content)
/// ```
pub fn blur_enabled(mut self, enabled: bool) -> Self {
self.blur_enabled = Some(enabled);
self
}
/// Sets the shadow elevation level for the complete sidebar panel.
///
/// Controls the single panel shadow using the theme's elevation system.
/// When not set, the theme's panel elevation is used. Large elevations may require a larger
/// inset near window edges to avoid native-window clipping.
///
/// # Example
///
/// ```rust,ignore
/// SidebarShell::left(px(260.0))
/// .elevation(ElevationToken::Md) // Medium shadow
/// .child(content)
/// ```
pub fn elevation(mut self, elevation: ElevationToken) -> Self {
self.elevation = Some(elevation);
self
}
fn surface_preset(&self) -> SurfacePreset {
let mut surface_preset = SurfacePreset::panel();
if let Some(elevation) = self.elevation {
surface_preset = surface_preset.with_elevation(elevation);
}
surface_preset
}
}
impl ParentElement for SidebarShell {
fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
self.children.extend(elements);
}
}
impl Styled for SidebarShell {
fn style(&mut self) -> &mut StyleRefinement {
&mut self.style
}
}
impl RenderOnce for SidebarShell {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let resizer_hover_bg = self
.resizer_hover_bg
.unwrap_or_else(|| cx.theme().foreground.alpha(0.20));
let window_bounds = window.window_bounds().get_bounds();
let window_height = window_bounds.size.height;
let inset = self
.inset
.unwrap_or_else(|| GlobalState::global(cx).floating_inset());
let top = inset + self.top_inset;
let bottom = inset;
let sidebar_height = (window_height - (top + bottom)).max(px(0.0));
let sidebar_width = self.width;
// Use explicit value if set, otherwise inherit from context
let blur_enabled = self
.blur_enabled
.unwrap_or_else(|| GlobalState::global(cx).blur_enabled());
let sidebar_surface = self
.surface_preset()
.wrap_with_bounds(
div(),
sidebar_width,
sidebar_height,
window,
cx,
SurfaceContext { blur_enabled },
)
.children(self.children)
.id("sidebar-shell-surface")
.size_full();
let resizer_half = self.resizer_width / 2.0;
let resizer_left = if self.side.is_left() {
self.width - resizer_half
} else {
-resizer_half
};
let is_left = self.side.is_left();
let on_resize_start = self.on_resize_start.clone();
let on_resize_end = self.on_resize_end.clone();
let outer = div()
.id("sidebar-shell")
.absolute()
.top(top)
.bottom(bottom)
.w(self.width)
.map(|el| {
if is_left {
el.left(inset)
} else {
el.right(inset)
}
})
.child(sidebar_surface)
.child(
div()
.id("sidebar-shell-resizer")
.absolute()
.top_0()
.bottom_0()
.left(resizer_left)
.w(self.resizer_width)
.rounded(px(999.0))
.bg(gpui::transparent_black())
.cursor_col_resize()
.hover(move |s| s.bg(resizer_hover_bg))
.when_some(on_resize_start, move |el, callback| {
el.on_mouse_down(gpui::MouseButton::Left, move |event, window, cx| {
cx.stop_propagation();
callback(sidebar_width, event.position.x, window, cx);
})
})
.when_some(on_resize_end, move |el, callback| {
let callback_mouse_up = callback.clone();
el.on_mouse_up(gpui::MouseButton::Left, move |_event, window, cx| {
callback_mouse_up(window, cx);
})
.on_mouse_up_out(
gpui::MouseButton::Left,
move |_event, window, cx| {
callback(window, cx);
},
)
}),
)
.refine_style(&self.style);
outer
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_sidebar_shell_defaults_and_elevation_builder() {
let default_shell = SidebarShell::left(px(260.0));
assert_eq!(default_shell.elevation, None);
assert_eq!(default_shell.min_width, px(DEFAULT_MIN_WIDTH));
assert_eq!(default_shell.max_width, px(DEFAULT_MAX_WIDTH));
assert_eq!(default_shell.resizer_width, px(DEFAULT_RESIZER_WIDTH));
assert_eq!(default_shell.inset, None);
assert_eq!(default_shell.side, Side::Left);
let no_shadow = SidebarShell::left(px(260.0)).elevation(ElevationToken::None);
assert_eq!(no_shadow.elevation, Some(ElevationToken::None));
let large_shadow = SidebarShell::right(px(300.0))
.inset(px(12.0))
.resizer_width(px(8.0))
.elevation(ElevationToken::Lg);
assert_eq!(large_shadow.elevation, Some(ElevationToken::Lg));
assert_eq!(large_shadow.inset, Some(px(12.0)));
assert_eq!(large_shadow.resizer_width, px(8.0));
assert_eq!(large_shadow.side, Side::Right);
}
#[test]
fn test_sidebar_shell_surface_preset_elevation() {
let theme_default = SidebarShell::left(px(260.0)).surface_preset();
assert_eq!(theme_default.elevation, ElevationToken::Lg);
assert!(theme_default.use_theme_elevation_defaults);
let no_shadow = SidebarShell::left(px(260.0))
.elevation(ElevationToken::None)
.surface_preset();
assert_eq!(no_shadow.elevation, ElevationToken::None);
assert!(!no_shadow.use_theme_elevation_defaults);
let large_shadow = SidebarShell::left(px(260.0))
.elevation(ElevationToken::Lg)
.surface_preset();
assert_eq!(large_shadow.elevation, ElevationToken::Lg);
assert!(!large_shadow.use_theme_elevation_defaults);
}
}