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
//! Captured pointer resizing supported by the Wry host; all state stays in Rust.
use std::cell::RefCell;
use glory::render::{DragCapture, DragPhase, Navigation};
use glory::style::{Length, Style};
use crate::*;
const HANDLE: f64 = 10.0;
const MIN_PANE: f64 = 260.0;
fn slot_style(ratio: f64) -> Style {
Style {
grow: Some(ratio),
shrink: Some(1.0),
..Style::new().width(Length::Px(0.0))
}
}
fn bounded_ratio(ratio: f64, width: f64) -> f64 {
let minimum = if width.is_finite() && width > HANDLE {
(MIN_PANE / (width - HANDLE)).clamp(0.2, 0.5)
} else {
0.2
};
if ratio.is_finite() {
ratio.clamp(minimum, 1.0 - minimum)
} else {
0.5
}
}
struct DragOrigin {
x: f64,
ratio: f64,
original_ratio: f64,
width: f64,
server: Option<String>,
}
pub(crate) fn panes(
ui: Ui,
left: impl Widget + 'static,
right: impl Widget + 'static,
) -> impl Widget {
let state = ui.state;
let width = ui.scope.cage(0.0);
let origin = Rc::new(RefCell::new(None::<DragOrigin>));
let resize_origin = origin.clone();
let dragging = ui.scope.cage(false);
let drag_ui = ui.clone();
let resize_ui = ui.clone();
let reset_ui = ui;
row()
.class("panes")
.on(resize, move |event| {
if let Event::Resize { size, .. } = event {
width.revise(|value| *value = size.width);
// A resized container invalidates the captured geometry. End
// this gesture instead of allowing a jump on its next move.
if let Some(initial) = resize_origin.borrow_mut().take()
&& resize_ui.state.with(|s| s.selected == initial.server)
{
resize_ui
.state
.revise(|s| s.file_split = initial.original_ratio);
}
dragging.revise(|value| *value = false);
}
})
.fill((
col()
.class("pane-slot local-slot")
.style(state.map(move |s| slot_style(bounded_ratio(s.file_split, width.get()))))
.fill(left),
view()
.class(dragging.map(|active| {
if *active {
"pane-splitter dragging"
} else {
"pane-splitter"
}
.to_string()
}))
.role(Role::Group)
.name(i18n::bind("调整文件栏宽度,上下方向键调整,Home 恢复均分"))
.focus(FocusMode::TabStop)
.semantics(Some(Box::new(Semantics {
drag_capture: Some(DragCapture::Press),
..Default::default()
})))
.on(drag, move |event| {
let Event::Drag { phase, x, .. } = event else {
return;
};
match phase {
DragPhase::Start => {
let measured = width.get();
if measured <= HANDLE {
return;
}
let (ratio, original_ratio, server) = drag_ui.state.with(|s| {
(
bounded_ratio(s.file_split, measured),
s.file_split,
s.selected.clone(),
)
});
*origin.borrow_mut() = Some(DragOrigin {
x,
ratio,
original_ratio,
width: measured,
server,
});
dragging.revise(|value| *value = true);
}
DragPhase::Move | DragPhase::Drop => {
let mut start = origin.borrow_mut();
let Some(initial) = start.as_ref() else {
return;
};
if drag_ui.state.with(|s| s.selected != initial.server) {
start.take();
dragging.revise(|value| *value = false);
return;
}
let ratio = bounded_ratio(
initial.ratio + (x - initial.x) / (initial.width - HANDLE),
initial.width,
);
drag_ui.state.revise(|s| s.file_split = ratio);
if phase == DragPhase::Drop {
start.take();
dragging.revise(|value| *value = false);
drag_ui.save_file_split(ratio);
}
}
DragPhase::Cancel => {
if let Some(initial) = origin.borrow_mut().take()
&& drag_ui.state.with(|s| s.selected == initial.server)
{
drag_ui
.state
.revise(|s| s.file_split = initial.original_ratio);
}
dragging.revise(|value| *value = false);
}
}
})
.on(navigate, move |event| {
if let Event::Navigate { command, .. } = event {
let ratio = reset_ui.state.with(|s| s.file_split);
let ratio = match command {
Navigation::Previous => ratio - 0.05,
Navigation::Next => ratio + 0.05,
Navigation::First => 0.5,
_ => return,
};
reset_ui.save_file_split(bounded_ratio(ratio, width.get()));
}
}),
col()
.class("pane-slot remote-slot")
.style(
state.map(move |s| slot_style(1.0 - bounded_ratio(s.file_split, width.get()))),
)
.fill(right),
))
}