egui_pop_core/regions.rs
1//! F6 through a window's regions: the parts of the window (an area, a palette,
2//! the status bar), each a [`focus::region`].
3//!
4//! [`next`] says where the keyboard goes; whoever binds F6 moves it there (an
5//! application: the commands `workbench.next_region` and
6//! `workbench.previous_region`). It changes no Tab order: a region is a focus
7//! scope, not a [`tab::scope`].
8//!
9//! - **The order** is the regions' ranks, and it wraps. From nothing it starts
10//! at the first, or the last going back; after Escape or a press, nothing
11//! holds the focus and the focus chain still says where the user is, so it
12//! goes on from that region.
13//! - **Where it lands**: the widget focused there last, if it can still take the
14//! focus; else the one the region names with [`focus::prefer`]; else the first
15//! focusable widget of a scope inside the region, the content before what
16//! stands around it (a dock's tabs, a palette's band); else nowhere, for a
17//! [`Region::holding`], whose content then holds the focus itself; else any
18//! focusable widget of the region in view. A region with nothing to land on
19//! is passed over.
20//! - **Panel [`tab::scope`]s** of a region take that region's
21//! turn, each a stop of the round, at the stop focused there last or its
22//! first; what lies around them is reached by Tab. F6 goes on out of them.
23//! - **Nothing moves behind a modal**, overlay or scope.
24//! - **A focused widget taking every key** (a terminal) gets F6 before any
25//! binding does.
26//! - **Each window has its own regions**, as it has its own focus.
27//!
28//! upstream: egui lists no focusable widgets, but its widget rectangles carry
29//! each widget's sense in the order drawn, as a scope's widgets are recorded;
30//! the last pass's are enough to choose from before anything is drawn.
31//!
32//! [`Region::holding`]: crate::focus::Region::holding
33//!
34//! ```
35//! use egui_pop_core::{focus, regions};
36//!
37//! fn window(ui: &mut egui::Ui) {
38//! focus::region(ui, &focus::Region::new("list", 0), |ui| {
39//! let _ = ui.button("Row");
40//! });
41//! focus::region(ui, &focus::Region::new("status", 1), |ui| {
42//! let _ = ui.button("Item");
43//! });
44//! if ui.input(|i| i.key_pressed(egui::Key::F6))
45//! && let Some(regions::Hop { target: Some(id), .. }) = regions::next(ui.ctx(), false)
46//! {
47//! ui.memory_mut(|m| m.request_focus(id));
48//! }
49//! }
50//! ```
51
52use egui::{Context, Id, WidgetRect};
53
54use crate::focus::{self, DrawnRegion};
55use crate::{overlay, place, tab};
56
57/// Where F6 goes.
58#[derive(Clone, Debug, PartialEq, Eq)]
59pub struct Hop {
60 /// The region's name.
61 pub region: String,
62 /// The widget to focus. `None`: nothing in the region's scopes can take
63 /// the focus, and the region said its content can hold it
64 /// ([`Region::holding`](crate::focus::Region::holding)).
65 pub target: Option<Id>,
66}
67
68/// The next region's landing, or the previous one's with `back`. `None` behind
69/// a modal, or with nowhere to go.
70pub fn next(ctx: &Context, back: bool) -> Option<Hop> {
71 if overlay::modal_open(ctx) || tab::modal_drawn(ctx) {
72 return None;
73 }
74 let (regions, current) = focus::drawn_regions(ctx);
75 let hops = hops(ctx, ®ions);
76 if hops.is_empty() {
77 return None;
78 }
79 let n = hops.len();
80 let at = match (hops.iter().position(|h| h.here), current) {
81 (Some(p), _) if back => (p + n - 1) % n,
82 (Some(p), _) => (p + 1) % n,
83 (None, None) if back => n - 1,
84 (None, None) => 0,
85 (None, Some(c)) if back => hops.iter().rposition(|h| h.region < c).unwrap_or(n - 1),
86 // Its panel scopes are its turn, and the focus is in none of them.
87 (None, Some(c)) => {
88 let first = |h: &Step| h.region > c || (h.region == c && h.sub);
89 hops.iter().position(first).unwrap_or(0)
90 }
91 };
92 let step = &hops[at];
93 Some(Hop {
94 region: regions[step.region].name.clone(),
95 target: step.target,
96 })
97}
98
99/// One stop of F6's round.
100struct Step {
101 /// Its region, as an index.
102 region: usize,
103 target: Option<Id>,
104 /// A panel scope in the region.
105 sub: bool,
106 /// The focus is in that scope.
107 here: bool,
108}
109
110/// F6's round through the regions, skipping those with nothing to land on.
111fn hops(ctx: &Context, regions: &[DrawnRegion]) -> Vec<Step> {
112 let panels = tab::panel_hops(ctx);
113 let mut out = Vec::new();
114 for (i, r) in regions.iter().enumerate() {
115 let inside = panels.iter().filter(|p| {
116 let at = ctx.viewport(|v| v.prev_pass.widgets.order(p.first));
117 at.is_some_and(|(layer, order)| layer == r.layer && r.widgets.contains(&order))
118 });
119 let before = out.len();
120 out.extend(inside.map(|p| Step {
121 region: i,
122 target: Some(p.target),
123 sub: true,
124 here: p.here,
125 }));
126 if out.len() == before
127 && let Some(target) = landing(ctx, r)
128 {
129 out.push(Step {
130 region: i,
131 target,
132 sub: false,
133 here: false,
134 });
135 }
136 }
137 out
138}
139
140/// Where a region takes the focus, in [the order](self): `None` for nowhere,
141/// `Some(None)` for its content holding it.
142fn landing(ctx: &Context, r: &DrawnRegion) -> Option<Option<Id>> {
143 let screen = ctx.viewport_rect();
144 ctx.viewport(|v| {
145 let drawn: Vec<&WidgetRect> = v.prev_pass.widgets.get_layer(r.layer).collect();
146 let own = drawn.get(r.widgets.clone()).unwrap_or_default();
147 let usable = |id: &Id| own.iter().any(|w| w.id == *id && place::can_focus(w));
148 let named = r
149 .remembered
150 .iter()
151 .chain(&r.preferred)
152 .find(|id| usable(id));
153 if let Some(id) = named {
154 return Some(Some(*id));
155 }
156 let in_view = |w: &&&WidgetRect| {
157 place::can_focus(w) && w.interact_rect.intersect(screen).is_positive()
158 };
159 let in_scope = |offset: usize| {
160 let order = r.widgets.start + offset;
161 r.inner.iter().any(|span| span.contains(&order))
162 };
163 let inner = own
164 .iter()
165 .enumerate()
166 .filter(|(i, _)| in_scope(*i))
167 .map(|(_, w)| w)
168 .find(in_view);
169 if let Some(w) = inner {
170 return Some(Some(w.id));
171 }
172 if r.holding {
173 return Some(None);
174 }
175 let any = own.iter().find(in_view);
176 any.map(|w| Some(w.id))
177 })
178}