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
use crate::event::{Event, EventCtx};
use crate::geometry::{Color, Rect};
use crate::painter::Painter;
use crate::theme::Theme;
use crate::widget::{PopupRequest, Widget};
use crate::widgets::{TabAction, tab_action};
/// Vertical layout container. Each child is given a horizontal slice of the
/// column's bounds: either a *fixed* height it asked for, or it shares the
/// space left after every fixed child has been laid out (a *fill* child).
/// Optional *overlay* children sit on top of everything else — useful for
/// modal dialogs that should float over the menu bar / editor.
///
/// `Column` propagates `layout` to its children whenever its own bounds
/// change, which makes it the building block for windows whose chrome (menu
/// bar, status bar) sits at fixed sizes around a content widget that flexes
/// with the window — exactly what Notepad needs.
///
/// Like `Container`, it handles pointer capture, keyboard focus, accelerator
/// routing, and the overlay paint pass.
pub struct Column {
bounds: Rect,
pub background: Option<Color>,
children: Vec<Child>,
/// Widgets that live on top of the column's normal layout. They
/// receive the column's full bounds via `layout`, paint last (so they
/// appear above siblings), and pre-empt event dispatch whenever they
/// report `captures_pointer() == true` — the mechanism that makes
/// modal dialogs actually modal.
overlays: Vec<Box<dyn Widget>>,
captured: Option<usize>,
focused: Option<usize>,
}
struct Child {
widget: Box<dyn Widget>,
mode: SizeMode,
}
#[derive(Clone, Copy)]
enum SizeMode {
Fixed(i32),
Fill,
}
impl Column {
pub fn new() -> Self {
Self {
bounds: Rect::new(0, 0, 0, 0),
// Transparent by default (see `Container::new`): the runtime fills
// the window with `theme.background`, so an opaque white here is
// redundant. Set one only to override what shows behind the column.
background: None,
children: Vec::new(),
overlays: Vec::new(),
captured: None,
focused: None,
}
}
pub fn with_background(mut self, color: Color) -> Self {
self.background = Some(color);
self
}
/// Add a child with a *fixed* logical-pixel height. Width is always the
/// full column width.
pub fn add_fixed(mut self, widget: impl Widget + 'static, height: i32) -> Self {
self.push_fixed(widget, height);
self
}
pub fn push_fixed(&mut self, widget: impl Widget + 'static, height: i32) {
self.children.push(Child {
widget: Box::new(widget),
mode: SizeMode::Fixed(height),
});
}
/// Add a child that fills the leftover height. Multiple fill children
/// split the remaining space equally.
pub fn add_fill(mut self, widget: impl Widget + 'static) -> Self {
self.push_fill(widget);
self
}
pub fn push_fill(&mut self, widget: impl Widget + 'static) {
self.children.push(Child {
widget: Box::new(widget),
mode: SizeMode::Fill,
});
}
/// Add a widget that floats over the column, receives the column's
/// *full* bounds on layout, and pre-empts event dispatch while it
/// reports `captures_pointer() == true`. Use this for modal dialogs.
pub fn add_overlay(mut self, widget: impl Widget + 'static) -> Self {
self.push_overlay(widget);
self
}
pub fn push_overlay(&mut self, widget: impl Widget + 'static) {
self.overlays.push(Box::new(widget));
}
/// Direct keyboard focus to a specific child by index (its position among
/// all children — fixed and fill — in add order). Clears any previous
/// focus and delegates into the child via `focus_first`, so wrapper
/// widgets pick the right nested leaf. Returns `true` if the index named a
/// focusable child. Use it to choose a non-default initial focus target
/// (e.g. focus a content list rather than a leading toolbar field).
pub fn focus_child(&mut self, index: usize) -> bool {
if self.children.get(index).map(|c| c.widget.focusable()) != Some(true) {
return false;
}
if let Some(old) = self.focused
&& old != index
&& let Some(c) = self.children.get_mut(old)
{
c.widget.set_focused(false);
}
let focused = self.children[index].widget.focus_first();
if focused {
self.focused = Some(index);
}
focused
}
fn choose_target(&self, event: &Event) -> Option<usize> {
if event.is_keyboard() {
return self.focused;
}
if let Some(idx) = self.captured {
return Some(idx);
}
let pos = event.position()?;
(0..self.children.len())
.rev()
.find(|&i| self.children[i].widget.bounds().contains(pos))
}
/// Index of the first overlay that's currently asserting pre-emptive
/// capture (typically: a dialog that's just been shown).
fn active_overlay(&self) -> Option<usize> {
self.overlays.iter().position(|o| o.captures_pointer())
}
fn change_focus(&mut self, new_focus: Option<usize>, ctx: &mut EventCtx) {
if new_focus == self.focused {
return;
}
if let Some(old) = self.focused
&& let Some(c) = self.children.get_mut(old)
{
c.widget.set_focused(false);
}
if let Some(new) = new_focus
&& let Some(c) = self.children.get_mut(new)
{
// Use `focus_first` so wrapper widgets that delegate focus to a
// nested target get a chance to set up the right leaf.
c.widget.focus_first();
}
self.focused = new_focus;
ctx.request_paint();
}
fn focusable_count(&self) -> usize {
self.children
.iter()
.filter(|c| c.widget.focusable())
.count()
}
fn cycle_focus(&mut self, dir: i32, ctx: &mut EventCtx) -> bool {
let n = self.children.len();
if n == 0 {
return false;
}
let candidates: Vec<usize> = (0..n)
.filter(|&i| self.children[i].widget.focusable())
.collect();
if candidates.is_empty() {
return false;
}
let next = next_in_cycle(&candidates, self.focused, dir);
if Some(next) == self.focused {
return false;
}
self.change_focus(Some(next), ctx);
true
}
}
fn next_in_cycle(candidates: &[usize], current: Option<usize>, dir: i32) -> usize {
let n = candidates.len() as i32;
let cur_pos = current.and_then(|c| candidates.iter().position(|&i| i == c));
match cur_pos {
None => {
if dir > 0 {
candidates[0]
} else {
candidates[(n - 1) as usize]
}
}
Some(p) => {
let np = ((p as i32) + dir).rem_euclid(n) as usize;
candidates[np]
}
}
}
impl Default for Column {
fn default() -> Self {
Self::new()
}
}
impl Widget for Column {
fn bounds(&self) -> Rect {
self.bounds
}
fn layout(&mut self, bounds: Rect) {
self.bounds = bounds;
let total_fixed: i32 = self
.children
.iter()
.filter_map(|c| match c.mode {
SizeMode::Fixed(h) => Some(h),
SizeMode::Fill => None,
})
.sum();
let fill_count = self
.children
.iter()
.filter(|c| matches!(c.mode, SizeMode::Fill))
.count() as i32;
let leftover = (bounds.h - total_fixed).max(0);
let fill_each = if fill_count > 0 {
leftover / fill_count
} else {
0
};
// Award any rounding slack to the last fill child so we exactly
// cover the column's bounds.
let fill_last_extra = if fill_count > 0 {
leftover - fill_each * fill_count
} else {
0
};
let mut y = bounds.y;
let mut fill_seen = 0;
for child in &mut self.children {
let h = match child.mode {
SizeMode::Fixed(h) => h,
SizeMode::Fill => {
fill_seen += 1;
if fill_seen == fill_count {
fill_each + fill_last_extra
} else {
fill_each
}
}
};
child.widget.layout(Rect::new(bounds.x, y, bounds.w, h));
y += h;
}
// Overlays float over the whole column, so they receive the
// column's bounds rather than a slot.
for overlay in &mut self.overlays {
overlay.layout(bounds);
}
}
fn paint(&mut self, painter: &mut Painter, theme: &Theme) {
if let Some(bg) = self.background {
painter.fill_rect(self.bounds, bg);
}
for child in &mut self.children {
child.widget.paint(painter, theme);
}
for child in &mut self.children {
child.widget.paint_overlay(painter, theme);
}
for overlay in &mut self.overlays {
overlay.paint(painter, theme);
overlay.paint_overlay(painter, theme);
}
}
fn paint_overlay(&mut self, painter: &mut Painter, theme: &Theme) {
for child in &mut self.children {
child.widget.paint_overlay(painter, theme);
}
for overlay in &mut self.overlays {
overlay.paint_overlay(painter, theme);
}
}
fn event(&mut self, event: &Event, ctx: &mut EventCtx) {
// Modal capture: any overlay that's actively capturing swallows
// every event before normal dispatch can see it. Returns must
// happen before any borrow of self.children is taken.
if let Some(idx) = self.active_overlay() {
self.overlays[idx].event(event, ctx);
return;
}
if !event.is_keyboard() && event.position().is_none() && self.captured.is_none() {
for child in &mut self.children {
child.widget.event(event, ctx);
}
return;
}
// A focused child that's itself capturing the pointer — an open
// `Dropdown`, or a `Container` whose open dropdown it forwards — owns
// the keyboard while it's up. Skip the accelerator pass (so a sibling
// default button's Enter can't pre-empt it) and Tab cycling; the
// focused dispatch below still delivers the key.
let focused_capturing = self
.focused
.and_then(|i| self.children.get(i))
.is_some_and(|c| c.widget.captures_pointer());
if event.is_keyboard() && !focused_capturing {
let mut accelerator_blocking = false;
for (idx, child) in self.children.iter_mut().enumerate() {
if child.widget.accepts_accelerators() && Some(idx) != self.focused {
child.widget.event(event, ctx);
if ctx.is_consumed() {
return;
}
if child.widget.captures_pointer() {
accelerator_blocking = true;
}
}
}
if accelerator_blocking {
return;
}
// Tab / Shift+Tab cycle focus between sibling focusable
// children before the event reaches the focused widget. The
// matching `Char('\t')` is swallowed so a single Tab press
// doesn't move focus twice; when this column has fewer than
// two focusable children we let both events fall through so
// a sole `TextEditor` can still receive `'\t'`.
match tab_action(event) {
Some(TabAction::Cycle(dir)) if self.cycle_focus(dir, ctx) => {
return;
}
Some(TabAction::Swallow) if self.focusable_count() >= 2 => return,
_ => {}
}
}
let Some(idx) = self.choose_target(event) else {
return;
};
let captured_was_set = self.captured == Some(idx);
{
let child = &mut self.children[idx];
child.widget.event(event, ctx);
if !event.is_keyboard() {
if child.widget.captures_pointer() {
self.captured = Some(idx);
} else if captured_was_set {
self.captured = None;
}
}
}
if ctx.focus_requested {
ctx.focus_requested = false;
self.change_focus(Some(idx), ctx);
}
if ctx.focus_released {
ctx.focus_released = false;
if self.focused == Some(idx) {
self.change_focus(None, ctx);
}
}
}
fn captures_pointer(&self) -> bool {
self.captured.is_some() || self.active_overlay().is_some()
}
fn focusable(&self) -> bool {
self.children.iter().any(|c| c.widget.focusable())
|| self.overlays.iter().any(|o| o.focusable())
}
fn focus_first(&mut self) -> bool {
for (idx, child) in self.children.iter_mut().enumerate() {
if child.widget.focus_first() {
self.focused = Some(idx);
return true;
}
}
false
}
fn popup_request(&self) -> Option<PopupRequest> {
for overlay in &self.overlays {
if let Some(req) = overlay.popup_request() {
return Some(req);
}
}
for child in &self.children {
if let Some(req) = child.widget.popup_request() {
return Some(req);
}
}
None
}
fn wants_ticks(&self) -> bool {
self.children.iter().any(|c| c.widget.wants_ticks())
|| self.overlays.iter().any(|o| o.wants_ticks())
}
}