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
//! `SplitPanel` — two live panes with a draggable divider (gpui entity).
//!
//! Pane content is a builder closure re-invoked every render (like `Tabs`), so
//! panes show live data — including another `SplitPanel`'s element, which is
//! how nested layouts are built.
//!
//! ```ignore
//! let split = cx.new(|cx| {
//! SplitPanel::new(cx)
//! .direction(SplitDirection::Horizontal)
//! .ratio(0.3)
//! .min_first(120.0)
//! .first(|_, _| Text::new("Sidebar"))
//! .second(|_, _| Text::new("Main content"))
//! });
//! cx.subscribe(&split, |_, _, SplitPanelEvent::Resized(ratio), _| { /* … */ })
//! .detach();
//! ```
use gpui::prelude::*;
use gpui::{
div, px, App, Context, DragMoveEvent, Empty, EntityId, EventEmitter, IntoElement, Window,
};
use crate::data::Content;
use crate::devtools::Probed;
use crate::style::FlexExt;
use crate::theme::theme;
/// Which way the panes are laid out. `Horizontal` places them side by side
/// (a vertical divider, column-resize cursor); `Vertical` stacks them.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum SplitDirection {
#[default]
Horizontal,
Vertical,
}
/// Emitted while the divider is dragged. Carries the new first-pane ratio
/// in `0.0..=1.0`.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum SplitPanelEvent {
Resized(f32),
}
/// Drag payload for the divider. Carries the owning panel's id so nested
/// `SplitPanel`s ignore each other's drags (`on_drag_move` fires for every
/// active drag of this type, anywhere in the window).
struct DividerDrag {
panel: EntityId,
}
/// A resizable two-pane layout. Create with
/// `cx.new(|cx| SplitPanel::new(cx).first(..).second(..))` and give the
/// element a sized parent — the panel fills it.
pub struct SplitPanel {
direction: SplitDirection,
first: Option<Content>,
second: Option<Content>,
ratio: f32,
min_first: f32,
min_second: f32,
handle_size: f32,
}
impl EventEmitter<SplitPanelEvent> for SplitPanel {}
impl SplitPanel {
pub fn new(_cx: &mut Context<Self>) -> Self {
SplitPanel {
direction: SplitDirection::Horizontal,
first: None,
second: None,
ratio: 0.5,
min_first: 40.0,
min_second: 40.0,
handle_size: 6.0,
}
}
pub fn direction(mut self, direction: SplitDirection) -> Self {
self.direction = direction;
self
}
/// The first pane (left / top). Rebuilt each render so it can show live
/// data — including another `SplitPanel`'s element for nesting.
pub fn first<E>(mut self, content: impl Fn(&mut Window, &mut App) -> E + 'static) -> Self
where
E: IntoElement,
{
self.first = Some(Box::new(move |window, cx| {
content(window, cx).into_any_element()
}));
self
}
/// The second pane (right / bottom). Rebuilt each render.
pub fn second<E>(mut self, content: impl Fn(&mut Window, &mut App) -> E + 'static) -> Self
where
E: IntoElement,
{
self.second = Some(Box::new(move |window, cx| {
content(window, cx).into_any_element()
}));
self
}
/// Initial share of the axis given to the first pane (clamped to `0..=1`).
pub fn ratio(mut self, ratio: f32) -> Self {
self.ratio = ratio.clamp(0.0, 1.0);
self
}
/// Minimum pixel size of the first pane while dragging.
pub fn min_first(mut self, min: f32) -> Self {
self.min_first = min.max(0.0);
self
}
/// Minimum pixel size of the second pane while dragging.
pub fn min_second(mut self, min: f32) -> Self {
self.min_second = min.max(0.0);
self
}
/// Thickness of the divider's grab area in pixels.
pub fn handle_size(mut self, size: f32) -> Self {
self.handle_size = size.max(1.0);
self
}
/// The current first-pane ratio.
pub fn current_ratio(&self) -> f32 {
self.ratio
}
}
/// Resolve a divider drag into the next first-pane ratio. `pos` is the pointer
/// offset from the container's leading edge along the split axis, `extent` the
/// container's size on that axis. The divider centers under the pointer, and
/// both panes keep their minimum sizes.
fn drag_ratio(pos: f32, extent: f32, handle: f32, min_first: f32, min_second: f32) -> f32 {
let avail = (extent - handle).max(1.0);
let lo = min_first.min(avail);
let hi = (avail - min_second).max(lo);
(pos - handle * 0.5).clamp(lo, hi) / avail
}
impl Render for SplitPanel {
fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let t = theme(cx);
let line = t.border().hsla();
let grip = t.primary().alpha(0.35);
let horizontal = matches!(self.direction, SplitDirection::Horizontal);
let handle = self.handle_size;
let ratio = self.ratio.clamp(0.0, 1.0);
let min_first = self.min_first;
let min_second = self.min_second;
let panel = cx.entity().entity_id();
let first = self.first.as_ref().map(|build| build(window, cx));
let second = self.second.as_ref().map(|build| build(window, cx));
let mut first_pane = div().flex_basis(px(0.0)).grow(ratio).overflow_hidden();
first_pane = if horizontal {
first_pane.min_w(px(min_first))
} else {
first_pane.min_h(px(min_first))
};
if let Some(el) = first {
first_pane = first_pane.child(el);
}
let mut second_pane = div()
.flex_basis(px(0.0))
.grow(1.0 - ratio)
.overflow_hidden();
second_pane = if horizontal {
second_pane.min_w(px(min_second))
} else {
second_pane.min_h(px(min_second))
};
if let Some(el) = second {
second_pane = second_pane.child(el);
}
let mut divider = div()
.id("guise-splitpanel-divider")
.flex_none()
.flex()
.items_center()
.justify_center()
.hover(move |s| s.bg(grip))
.on_drag(DividerDrag { panel }, |_, _offset, _window, cx| {
cx.new(|_| Empty)
});
divider = if horizontal {
divider
.w(px(handle))
.h_full()
.cursor_col_resize()
.child(div().w(px(1.0)).h_full().bg(line))
} else {
divider
.h(px(handle))
.w_full()
.cursor_row_resize()
.child(div().h(px(1.0)).w_full().bg(line))
};
let mut root = div()
.id("guise-splitpanel")
.size_full()
.flex()
.on_drag_move(cx.listener(
move |this, ev: &DragMoveEvent<DividerDrag>, _window, cx| {
let source = ev.drag(cx).panel;
if source != panel {
return;
}
let bounds = ev.bounds;
let (pos, extent) = if matches!(this.direction, SplitDirection::Horizontal) {
(
f32::from(ev.event.position.x - bounds.left()),
f32::from(bounds.size.width),
)
} else {
(
f32::from(ev.event.position.y - bounds.top()),
f32::from(bounds.size.height),
)
};
let next = drag_ratio(
pos,
extent,
this.handle_size,
this.min_first,
this.min_second,
);
if (next - this.ratio).abs() > f32::EPSILON {
this.ratio = next;
cx.emit(SplitPanelEvent::Resized(next));
cx.notify();
}
},
));
root = if horizontal {
root.flex_row()
} else {
root.flex_col()
};
root.child(first_pane)
.child(divider)
.child(second_pane)
.probe("SplitPanel")
}
}
#[cfg(test)]
mod tests {
use super::drag_ratio;
#[test]
fn centered_pointer_is_half() {
// 206px container, 6px handle: pointer at 103 puts 100px of the
// 200px of pane space on each side.
assert_eq!(drag_ratio(103.0, 206.0, 6.0, 0.0, 0.0), 0.5);
}
#[test]
fn clamps_to_min_first() {
let ratio = drag_ratio(10.0, 406.0, 6.0, 80.0, 0.0);
assert_eq!(ratio, 80.0 / 400.0);
}
#[test]
fn clamps_to_min_second() {
let ratio = drag_ratio(400.0, 406.0, 6.0, 0.0, 120.0);
assert_eq!(ratio, (400.0 - 120.0) / 400.0);
}
#[test]
fn overshoot_stays_in_range() {
assert_eq!(drag_ratio(-500.0, 206.0, 6.0, 0.0, 0.0), 0.0);
assert_eq!(drag_ratio(900.0, 206.0, 6.0, 0.0, 0.0), 1.0);
}
#[test]
fn degenerate_extent_prefers_min_first() {
// Container smaller than the minimums: the first pane's floor wins,
// and the result never divides by zero.
let ratio = drag_ratio(30.0, 60.0, 6.0, 100.0, 100.0);
assert_eq!(ratio, 1.0);
}
}