1use serde::Serialize;
25
26pub mod page;
27
28pub const ROW_HEIGHT: f32 = 32.0;
31
32#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize)]
34#[serde(rename_all = "snake_case")]
35pub enum Keyboard {
36 None,
38 Hover,
40 #[default]
42 Focus,
43 Exclusive,
45}
46
47#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize)]
49#[serde(rename_all = "snake_case")]
50pub enum ClaimKey {
51 Enter,
52 Escape,
53 Space,
54 Tab,
55}
56
57impl ClaimKey {
58 pub fn name(self) -> &'static str {
60 match self {
61 ClaimKey::Enter => "Enter",
62 ClaimKey::Escape => "Escape",
63 ClaimKey::Space => "Space",
64 ClaimKey::Tab => "Tab",
65 }
66 }
67}
68
69#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize)]
71pub struct Pointer {
72 pub drag: bool,
74 pub drag_out: bool,
76 pub wheel: bool,
77 pub cursor_images: bool,
79}
80
81#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize)]
83#[serde(rename_all = "snake_case")]
84pub enum Workers {
85 #[default]
86 None,
87 DrainedWhenDrawn,
90 Bounded,
95}
96
97#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize)]
99#[serde(rename_all = "snake_case")]
100pub enum Display {
101 #[default]
102 Srgb,
103 WideWanted,
106 WideRequired,
108}
109
110#[derive(Clone, Copy, Debug, Default, PartialEq, Serialize)]
112pub struct SizeNeed {
113 pub min: [f32; 2],
115 pub preferred: Option<[f32; 2]>,
116 pub max: Option<[f32; 2]>,
117 pub fills: bool,
119 pub aspect: Option<f32>,
121}
122
123#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize)]
125#[serde(rename_all = "snake_case")]
126pub enum Instances {
127 #[default]
129 Several,
130 Single,
133}
134
135#[derive(Clone, Debug, Default, PartialEq, Serialize)]
137pub struct Mountable {
138 pub id: String,
140 pub name: String,
142 pub from: String,
144 pub keyboard: Keyboard,
145 pub claims: Vec<ClaimKey>,
147 pub keymap: bool,
149 pub surface: Option<String>,
151 pub pointer: Pointer,
152 pub workers: Workers,
153 pub keep_under_modal: bool,
156 pub display: Display,
157 pub size: SizeNeed,
158 pub instances: Instances,
159 pub view_state: Option<String>,
162 pub app: bool,
164 pub role: String,
166 pub asks: bool,
168 pub session: bool,
171 pub popups: bool,
174 pub preview: bool,
176 pub descriptor_field: bool,
178}
179
180impl Mountable {
181 pub fn new(id: &str, name: &str) -> Self {
184 Self {
185 id: id.into(),
186 name: name.into(),
187 role: "group".into(),
188 ..Self::default()
189 }
190 }
191
192 pub fn crate_name(mut self, name: &str) -> Self {
194 self.from = name.into();
195 self
196 }
197
198 pub fn keyboard(mut self, keyboard: Keyboard) -> Self {
199 self.keyboard = keyboard;
200 self
201 }
202
203 pub fn claims(mut self, keys: &[ClaimKey]) -> Self {
205 for k in keys {
206 if !self.claims.contains(k) {
207 self.claims.push(*k);
208 }
209 }
210 self
211 }
212
213 pub fn keymap(mut self) -> Self {
215 self.keymap = true;
216 self
217 }
218
219 pub fn surface(mut self, name: &str) -> Self {
221 self.surface = Some(name.into());
222 self.keymap = true;
223 self
224 }
225
226 pub fn drag(mut self) -> Self {
227 self.pointer.drag = true;
228 self
229 }
230
231 pub fn drag_out(mut self) -> Self {
232 self.pointer.drag_out = true;
233 self
234 }
235
236 pub fn wheel(mut self) -> Self {
237 self.pointer.wheel = true;
238 self
239 }
240
241 pub fn cursor_images(mut self) -> Self {
242 self.pointer.cursor_images = true;
243 self
244 }
245
246 pub fn workers(mut self) -> Self {
248 self.workers = Workers::DrainedWhenDrawn;
249 self
250 }
251
252 pub fn bounded_workers(mut self) -> Self {
255 self.workers = Workers::Bounded;
256 self
257 }
258
259 pub fn keep_under_modal(mut self) -> Self {
261 self.keep_under_modal = true;
262 self
263 }
264
265 pub fn display(mut self, display: Display) -> Self {
266 self.display = display;
267 self
268 }
269
270 pub fn min(mut self, width: f32, height: f32) -> Self {
272 self.size.min = [width.max(0.0), height.max(0.0)];
273 self
274 }
275
276 pub fn preferred(mut self, width: f32, height: f32) -> Self {
277 self.size.preferred = Some([width, height]);
278 self
279 }
280
281 pub fn max(mut self, width: f32, height: f32) -> Self {
282 self.size.max = Some([width, height]);
283 self
284 }
285
286 pub fn fills(mut self) -> Self {
288 self.size.fills = true;
289 self
290 }
291
292 pub fn aspect(mut self, ratio: f32) -> Self {
294 self.size.aspect = Some(ratio);
295 self
296 }
297
298 pub fn single(mut self) -> Self {
300 self.instances = Instances::Single;
301 self
302 }
303
304 pub fn view_state(mut self, key: &str) -> Self {
306 self.view_state = Some(key.into());
307 self
308 }
309
310 pub fn needs_app(mut self) -> Self {
312 self.app = true;
313 self
314 }
315
316 pub fn role(mut self, role: &str) -> Self {
317 self.role = role.into();
318 self
319 }
320
321 pub fn asks(mut self) -> Self {
323 self.asks = true;
324 self
325 }
326
327 pub fn session(mut self) -> Self {
329 self.session = true;
330 self
331 }
332
333 pub fn popups(mut self) -> Self {
335 self.popups = true;
336 self
337 }
338
339 pub fn preview(mut self) -> Self {
341 self.preview = true;
342 self
343 }
344
345 pub fn descriptor_field(mut self) -> Self {
347 self.descriptor_field = true;
348 self
349 }
350
351 pub fn claims_key(&self, key: ClaimKey) -> bool {
353 self.claims.contains(&key)
354 }
355}
356
357#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize)]
359#[serde(rename_all = "snake_case")]
360pub enum HostSize {
361 #[default]
363 Fill,
364 Given,
367 Content,
369 Row,
371 NoUi,
373}
374
375#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize)]
377#[serde(rename_all = "snake_case")]
378pub enum FocusPolicy {
379 #[default]
381 Shared,
382 ClickInto,
384 ModalExclusive,
386 None,
387}
388
389#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize)]
391#[serde(rename_all = "snake_case")]
392pub enum Dismiss {
393 Answer,
394 Escape,
395 OutsideClick,
396}
397
398#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize)]
399#[serde(rename_all = "snake_case")]
400pub enum Lifetime {
401 #[default]
402 Persistent,
403 Transient,
404}
405
406#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize)]
408#[serde(rename_all = "snake_case")]
409pub enum Window {
410 #[default]
411 Main,
412 Auxiliary,
413 FollowsPanel,
415}
416
417#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize)]
419#[serde(rename_all = "snake_case")]
420pub enum Keep {
421 Place,
422 Size,
423 ViewValues,
424}
425
426#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize)]
428#[serde(rename_all = "snake_case")]
429pub enum HostDisplay {
430 #[default]
431 Srgb,
432 Wide,
434}
435
436#[derive(Clone, Debug, Default, PartialEq, Serialize)]
438pub struct HostOffer {
439 pub id: String,
441 pub code: String,
443 pub name: String,
445 pub source: String,
447 pub size: HostSize,
448 pub resizable: bool,
450 pub focus: FocusPolicy,
451 pub owns_keys: Vec<ClaimKey>,
454 pub honours_claims: bool,
457 pub dismissed_by: Vec<Dismiss>,
458 pub lifetime: Lifetime,
459 pub window: Window,
460 pub keymap: bool,
462 pub projection: bool,
464 pub keeps: Vec<Keep>,
465 pub gives_ui: bool,
467 pub gives_app: bool,
469 pub scope: bool,
471 pub fault_boundary: bool,
473 pub display: HostDisplay,
474 pub cursor_images: bool,
476 pub free_content: bool,
478 pub hides_content: bool,
481 pub headless: bool,
483}
484
485impl HostOffer {
486 pub fn new(id: &str, code: &str, name: &str) -> Self {
490 Self {
491 id: id.into(),
492 code: code.into(),
493 name: name.into(),
494 keymap: true,
495 gives_ui: true,
496 gives_app: true,
497 scope: true,
498 fault_boundary: true,
499 cursor_images: true,
500 free_content: true,
501 headless: true,
502 ..Self::default()
503 }
504 }
505
506 pub fn source(mut self, source: &str) -> Self {
508 self.source = source.into();
509 self
510 }
511
512 pub fn size(mut self, size: HostSize) -> Self {
513 self.size = size;
514 if size == HostSize::NoUi {
515 self.gives_ui = false;
516 }
517 self
518 }
519
520 pub fn resizable(mut self, on: bool) -> Self {
521 self.resizable = on;
522 self
523 }
524
525 pub fn focus(mut self, focus: FocusPolicy) -> Self {
526 self.focus = focus;
527 self
528 }
529
530 pub fn owns_keys(mut self, keys: &[ClaimKey]) -> Self {
532 self.owns_keys = keys.to_vec();
533 self
534 }
535
536 pub fn honours_claims(mut self) -> Self {
538 self.honours_claims = true;
539 self
540 }
541
542 pub fn dismissed_by(mut self, by: &[Dismiss]) -> Self {
543 self.dismissed_by = by.to_vec();
544 self
545 }
546
547 pub fn lifetime(mut self, lifetime: Lifetime) -> Self {
548 self.lifetime = lifetime;
549 self
550 }
551
552 pub fn window(mut self, window: Window) -> Self {
553 self.window = window;
554 self
555 }
556
557 pub fn keymap(mut self, on: bool) -> Self {
558 self.keymap = on;
559 self
560 }
561
562 pub fn projection(mut self, on: bool) -> Self {
563 self.projection = on;
564 self
565 }
566
567 pub fn keeps(mut self, keeps: &[Keep]) -> Self {
568 self.keeps = keeps.to_vec();
569 self
570 }
571
572 pub fn gives_ui(mut self, on: bool) -> Self {
573 self.gives_ui = on;
574 self
575 }
576
577 pub fn gives_app(mut self, on: bool) -> Self {
578 self.gives_app = on;
579 self
580 }
581
582 pub fn scope(mut self, on: bool) -> Self {
583 self.scope = on;
584 self
585 }
586
587 pub fn fault_boundary(mut self, on: bool) -> Self {
588 self.fault_boundary = on;
589 self
590 }
591
592 pub fn display(mut self, display: HostDisplay) -> Self {
593 self.display = display;
594 self
595 }
596
597 pub fn cursor_images(mut self, on: bool) -> Self {
598 self.cursor_images = on;
599 self
600 }
601
602 pub fn free_content(mut self, on: bool) -> Self {
603 self.free_content = on;
604 self
605 }
606
607 pub fn hides_content(mut self, on: bool) -> Self {
608 self.hides_content = on;
609 self
610 }
611
612 pub fn headless(mut self, on: bool) -> Self {
613 self.headless = on;
614 self
615 }
616
617 pub fn is_modal(&self) -> bool {
619 self.focus == FocusPolicy::ModalExclusive
620 }
621}
622
623#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize)]
625#[serde(rename_all = "snake_case")]
626pub enum Rule {
627 Size,
629 Keyboard,
632 Session,
635 Question,
637 Display,
639 Instances,
641 Application,
643 Ui,
645 Workers,
647 ViewState,
649 DialogContent,
651}
652
653impl Rule {
654 pub const ALL: [Rule; 11] = [
655 Rule::Size,
656 Rule::Keyboard,
657 Rule::Session,
658 Rule::Question,
659 Rule::Display,
660 Rule::Instances,
661 Rule::Application,
662 Rule::Ui,
663 Rule::Workers,
664 Rule::ViewState,
665 Rule::DialogContent,
666 ];
667
668 pub fn number(self) -> &'static str {
670 match self {
671 Rule::Size => "R1",
672 Rule::Keyboard => "R2",
673 Rule::Session => "R3",
674 Rule::Question => "R4",
675 Rule::Display => "R5",
676 Rule::Instances => "R6",
677 Rule::Application => "R7",
678 Rule::Ui => "R8",
679 Rule::Workers => "R9",
680 Rule::ViewState => "R10",
681 Rule::DialogContent => "R11",
682 }
683 }
684
685 pub fn name(self) -> &'static str {
686 match self {
687 Rule::Size => "size",
688 Rule::Keyboard => "keyboard",
689 Rule::Session => "sessions and lifetime",
690 Rule::Question => "questions",
691 Rule::Display => "display",
692 Rule::Instances => "instances",
693 Rule::Application => "application",
694 Rule::Ui => "a Ui",
695 Rule::Workers => "workers",
696 Rule::ViewState => "view state",
697 Rule::DialogContent => "dialog content",
698 }
699 }
700
701 pub fn compares(self) -> &'static str {
703 match self {
704 Rule::Size => {
705 "The size needed (minimum, fills) against the room the host gives (a rect, a given size, the content's height, one row)."
706 }
707 Rule::Keyboard => {
708 "The keyboard needed and the keys claimed against the host's focus policy and the keys it owns; bindings of the keymap against the host's keymap reach."
709 }
710 Rule::Session => {
711 "A session (unsaved work, a running program) or popups of the component's own against a transient host and what dismisses it."
712 }
713 Rule::Question => "A question to answer against a host that is not modal.",
714 Rule::Display => {
715 "Wide gamut wanted or required, and bitmap cursors, against what the host's window can show."
716 }
717 Rule::Instances => {
718 "One state per application against a host that may show it beside another mount."
719 }
720 Rule::Application => "A component needing the application against a host giving none.",
721 Rule::Ui => "Any component against a host that gives no `Ui`.",
722 Rule::Workers => {
723 "Workers drained when the component is drawn against a host that can stop drawing it."
724 }
725 Rule::ViewState => {
726 "A view-state key against a host that keeps no view values and is outside the projection."
727 }
728 Rule::DialogContent => {
729 "Free content against a dialog of the four kinds, which takes value-descriptor fields only."
730 }
731 }
732 }
733}
734
735#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize)]
737#[serde(rename_all = "snake_case")]
738pub enum CodeKind {
739 Condition,
741 Design,
743 Limit,
745 Upstream,
747}
748
749#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize)]
751pub struct Code {
752 pub code: &'static str,
754 pub kind: CodeKind,
755 pub title: &'static str,
757 pub text: &'static str,
759 pub then: &'static str,
762}
763
764pub const CODES: &[Code] = &[
766 Code {
767 code: "c1",
768 kind: CodeKind::Condition,
769 title: "Content-sized host",
770 text: "The palette, the raw modal, the popover and the dialogs of the four kinds size themselves from their content: a component that fills the space it is given must be given an explicit size (`.size()`, a `ScrollArea` with a maximum height) or it grows to the window. A palette keeps its content's size until the user resizes it.",
771 then: "",
772 },
773 Code {
774 code: "c2",
775 kind: CodeKind::Condition,
776 title: "No keymap in this window",
777 text: "The host's window is not reached by the key dispatcher, so bindings of the keymap (global and surface ones: an editor's find and save, the terminal's clear, the library's commands) do not fire while it has the keyboard. Keys a widget reads itself (arrows, zoom keys, typing) work. No host of egui-pop has this condition since E3 closed; a host an application declares may.",
778 then: "",
779 },
780 Code {
781 code: "c3",
782 kind: CodeKind::Condition,
783 title: "sRGB and system cursors",
784 text: "Wide gamut and HDR fall back to sRGB in this host, with the reason reported (U2); bitmap tool cursors fall back to their system cursor (U1).",
785 then: "",
786 },
787 Code {
788 code: "c4",
789 kind: CodeKind::Condition,
790 title: "The host takes its keys first",
791 text: "The host owns keys the component claims: Escape closes a modal or a popover before any component sees it, Enter accepts a dialog (ADR 0007's Escape order). A component's own Escape (a graph's deselect, a terminal program's Escape) never arrives; Shift+Escape still leaves the terminal.",
792 then: "",
793 },
794 Code {
795 code: "c5",
796 kind: CodeKind::Condition,
797 title: "Popups inside a transient host",
798 text: "The component opens egui areas of its own (completion lists, context menus, drop-downs) outside egui-pop's overlay stack; whether a click in them counts as a click outside the host (which closes a popover) is not checked.",
799 then: "",
800 },
801 Code {
802 code: "c6",
803 kind: CodeKind::Condition,
804 title: "One row",
805 text: "A compact size given by the application: a slider at least 120 wide, a small thumbnail, a thin bar, within one row.",
806 then: "",
807 },
808 Code {
809 code: "c7",
810 kind: CodeKind::Condition,
811 title: "One state per application",
812 text: "The component draws a service's state: two mounts (a panel and a popover) show the same jobs or the same draft, not two independent instances.",
813 then: "",
814 },
815 Code {
816 code: "c8",
817 kind: CodeKind::Condition,
818 title: "As a descriptor field only",
819 text: "In a dialog of the four kinds, the component exists only as a `Form` field made from a value descriptor (a slider from a float or int), not as a free widget; a `Custom` dialog takes it as it is.",
820 then: "",
821 },
822 Code {
823 code: "c9",
824 kind: CodeKind::Condition,
825 title: "Claims honoured by declaration",
826 text: "The host owns Enter and Escape, and gives them to a content that claims them: when the content claims Enter, Ctrl+Enter accepts; Escape goes first to a content that claims it and has something to cancel (a drag), then cancels.",
827 then: "",
828 },
829 Code {
830 code: "c10",
831 kind: CodeKind::Condition,
832 title: "A session per showing",
833 text: "The host is transient: shown again, the component starts a new session (a new program, an empty draft) unless the application keeps its state; closing it asks first or loses the session.",
834 then: "",
835 },
836 Code {
837 code: "c11",
838 kind: CodeKind::Condition,
839 title: "A queue while hidden",
840 text: "The host can stop drawing its content (a tab behind another, a palette hidden by Tab or rolled up) while the component's workers keep delivering: their queue grows until it is drawn again (docs/threading.md, section 2.2). No component of egui-pop has it: each drains its queue off-draw or caps it (`Mountable::bounded_workers`); a component an application declares with `workers()` may.",
841 then: "",
842 },
843 Code {
844 code: "c12",
845 kind: CodeKind::Condition,
846 title: "View state not kept",
847 text: "The host keeps no view values and is outside the context stack's projection: the component's zoom, scroll or selection is not kept across places and restarts.",
848 then: "",
849 },
850 Code {
851 code: "D1",
852 kind: CodeKind::Design,
853 title: "Line hosts hold one row",
854 text: "The top bar, the caption, the activity bar and the status bar hold indicators and small controls; nothing there takes the keyboard for long or needs more than a row.",
855 then: "An item in the line that opens a popover (the jobs centre's status item) or focuses a panel (`<panel>.focus`).",
856 },
857 Code {
858 code: "D2",
859 kind: CodeKind::Design,
860 title: "Viewport overlays are data",
861 text: "Overlays on the image are shapes and handles in image coordinates, drawn by the viewport, their edits handed back as signals; there is no `Ui` to put a component in.",
862 then: "A palette floating over the canvas (a navigator, a toolbox), or a heads-up host over the viewport (not built yet: decided for later).",
863 },
864 Code {
865 code: "D3",
866 kind: CodeKind::Design,
867 title: "Popovers are transient",
868 text: "A click outside or Escape closes them; an edit session (graph wiring, an editor with unsaved text, a running program, a theme draft) would end by accident (confirmed 2026-10-14).",
869 then: "A panel, a palette, or a dialog that asks before closing.",
870 },
871 Code {
872 code: "D4",
873 kind: CodeKind::Design,
874 title: "Only a modal asks",
875 text: "Palettes are never modal (decided 2026-10-13), nor are panels and popovers: a question that must be answered before going on goes in a modal.",
876 then: "A dialog.",
877 },
878 Code {
879 code: "D5",
880 kind: CodeKind::Design,
881 title: "One dialog at a time",
882 text: "Dialogs queue; a dialog does not open another over it.",
883 then: "Draw the second choice inline in the dialog (as a form's path field draws the in-app file picker).",
884 },
885 Code {
886 code: "D6",
887 kind: CodeKind::Design,
888 title: "The four dialog kinds take descriptors",
889 text: "`Confirm`, `Form`, `Choose` and `Info` are drawn from data: a form's fields come from value descriptors, and no kind takes free content.",
890 then: "A `Custom` dialog, which takes a body and keeps every dialog rule.",
891 },
892 Code {
893 code: "E1",
894 kind: CodeKind::Limit,
895 title: "No free-content dialog (closed)",
896 text: "Closed by `Dialog::Custom` (ADR 0013): a dialog with a body of its own, keeping the dialog rules.",
897 then: "Done.",
898 },
899 Code {
900 code: "E2",
901 kind: CodeKind::Limit,
902 title: "Palettes are not resizable (closed)",
903 text: "Closed: a palette is resized by its right and bottom edges, between the `palette.min-*` and `palette.max-*` tokens, its size kept in the layout.",
904 then: "Done.",
905 },
906 Code {
907 code: "E3",
908 kind: CodeKind::Limit,
909 title: "No keymap in auxiliary windows (closed)",
910 text: "Closed: the key dispatcher runs in each auxiliary window's pass, in that window's context (its focus chain, `window` naming its panel).",
911 then: "Done.",
912 },
913 Code {
914 code: "E4",
915 kind: CodeKind::Limit,
916 title: "Overlays live in the main window (closed)",
917 text: "Closed: each window has its own overlay stack. A dialog, the command palette and a popup menu open over the window in use, a context menu over its widget, and the toasts show in the active window, each going to the main window when its window closes; while a modal is open every other window is blocked.",
918 then: "Done.",
919 },
920 Code {
921 code: "E5",
922 kind: CodeKind::Limit,
923 title: "No custom page in the settings window (closed)",
924 text: "Closed by settings pages (`Ctx::settings_page`): a page a plugin draws itself in the settings window, placed in its tree, searched, with its reset (column T).",
925 then: "Done.",
926 },
927 Code {
928 code: "U1",
929 kind: CodeKind::Upstream,
930 title: "No custom cursor in immediate viewports",
931 text: "eframe shows no custom cursor in an immediate viewport (nor on the web): bitmap cursors fall back to system cursors there.",
932 then: "[upstream.md](upstream.md#eframe-no-custom-cursor-on-the-web-or-in-immediate-viewports)",
933 },
934 Code {
935 code: "U2",
936 kind: CodeKind::Upstream,
937 title: "Wide gamut in the main window only",
938 text: "Wide gamut goes through the image's own Wayland surface, made from the main window's handles: eframe gives no surface colour space and winit no colour management, so other windows draw sRGB.",
939 then: "[upstream.md](upstream.md#wayland-no-colour-management-through-winit)",
940 },
941 Code {
942 code: "U3",
943 kind: CodeKind::Upstream,
944 title: "No owner window for egui viewports",
945 text: "A palette in a native utility window would float above every application.",
946 then: "[upstream.md](upstream.md#winit-and-egui-no-owner-window-for-a-viewport)",
947 },
948];
949
950pub fn code(code: &str) -> Option<&'static Code> {
952 CODES.iter().find(|c| c.code == code)
953}
954
955#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize)]
957pub struct Condition {
958 pub rule: Rule,
959 pub code: &'static str,
961}
962
963impl Condition {
964 pub fn text(&self) -> &'static str {
966 code(self.code).map_or("", |c| c.text)
967 }
968}
969
970#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize)]
972pub struct Refusal {
973 pub rule: Rule,
974 pub code: &'static str,
976 pub kind: CodeKind,
977}
978
979impl Refusal {
980 pub fn reason(&self) -> String {
982 match code(self.code) {
983 Some(c) if !c.then.is_empty() && self.kind == CodeKind::Design => format!(
984 "{} ({}, {}): {} Instead: {}",
985 c.title,
986 self.rule.number(),
987 c.code,
988 c.text,
989 c.then
990 ),
991 Some(c) => format!(
992 "{} ({}, {}): {}",
993 c.title,
994 self.rule.number(),
995 c.code,
996 c.text
997 ),
998 None => format!("{} ({})", self.rule.name(), self.code),
999 }
1000 }
1001}
1002
1003#[derive(Clone, Debug, PartialEq, Serialize)]
1005#[serde(tag = "fit", rename_all = "snake_case")]
1006pub enum Fit {
1007 Yes,
1008 Under {
1010 conditions: Vec<Condition>,
1011 },
1012 No {
1014 refusal: Refusal,
1015 also: Vec<Refusal>,
1016 },
1017}
1018
1019impl Fit {
1020 pub fn fits(&self) -> bool {
1022 !matches!(self, Fit::No { .. })
1023 }
1024
1025 pub fn conditions(&self) -> &[Condition] {
1027 match self {
1028 Fit::Under { conditions } => conditions,
1029 _ => &[],
1030 }
1031 }
1032
1033 pub fn refusal(&self) -> Option<&Refusal> {
1035 match self {
1036 Fit::No { refusal, .. } => Some(refusal),
1037 _ => None,
1038 }
1039 }
1040
1041 pub fn cell(&self) -> String {
1043 match self {
1044 Fit::Yes => "yes".into(),
1045 Fit::Under { conditions } => {
1046 let codes: Vec<&str> = conditions.iter().map(|c| c.code).collect();
1047 format!("if {}", codes.join(" "))
1048 }
1049 Fit::No { refusal, .. } => refusal.code.into(),
1050 }
1051 }
1052}
1053
1054#[derive(Debug, Default)]
1056struct Found {
1057 conditions: Vec<Condition>,
1058 refusals: Vec<Refusal>,
1059}
1060
1061impl Found {
1062 fn condition(&mut self, rule: Rule, code: &'static str) {
1063 if !self.conditions.iter().any(|c| c.code == code) {
1064 self.conditions.push(Condition { rule, code });
1065 }
1066 }
1067
1068 fn refuse(&mut self, rule: Rule, code: &'static str, kind: CodeKind) {
1069 if !self
1070 .refusals
1071 .iter()
1072 .any(|r| r.code == code && r.rule == rule)
1073 {
1074 self.refusals.push(Refusal { rule, code, kind });
1075 }
1076 }
1077}
1078
1079pub fn check(m: &Mountable, h: &HostOffer) -> Fit {
1083 let mut f = Found::default();
1084 ui(m, h, &mut f);
1085 dialog_content(m, h, &mut f);
1086 application(m, h, &mut f);
1087 size(m, h, &mut f);
1088 keyboard(m, h, &mut f);
1089 session(m, h, &mut f);
1090 question(m, h, &mut f);
1091 display(m, h, &mut f);
1092 instances(m, h, &mut f);
1093 workers(m, h, &mut f);
1094 view_state(m, h, &mut f);
1095 let mut refusals = f.refusals.into_iter();
1096 match refusals.next() {
1097 Some(refusal) => Fit::No {
1098 refusal,
1099 also: refusals.collect(),
1100 },
1101 None if f.conditions.is_empty() => Fit::Yes,
1102 None => {
1103 let mut conditions = f.conditions;
1104 conditions.sort_by_key(|c| order_of(c.code));
1105 Fit::Under { conditions }
1106 }
1107 }
1108}
1109
1110fn order_of(code: &str) -> usize {
1112 CODES
1113 .iter()
1114 .position(|c| c.code == code)
1115 .unwrap_or(usize::MAX)
1116}
1117
1118fn ui(_: &Mountable, h: &HostOffer, f: &mut Found) {
1120 if h.size == HostSize::NoUi || !h.gives_ui {
1121 f.refuse(Rule::Ui, "D2", CodeKind::Design);
1122 }
1123}
1124
1125fn application(m: &Mountable, h: &HostOffer, f: &mut Found) {
1127 if m.app && !h.gives_app {
1128 f.refuse(Rule::Application, "D2", CodeKind::Design);
1129 }
1130}
1131
1132fn dialog_content(m: &Mountable, h: &HostOffer, f: &mut Found) {
1134 if !h.free_content && h.gives_ui {
1135 if m.descriptor_field {
1136 f.condition(Rule::DialogContent, "c8");
1137 } else {
1138 f.refuse(Rule::DialogContent, "D6", CodeKind::Design);
1139 }
1140 }
1141}
1142
1143fn size(m: &Mountable, h: &HostOffer, f: &mut Found) {
1145 match h.size {
1146 HostSize::Row => {
1147 if m.size.fills || m.size.min[1] > ROW_HEIGHT {
1148 f.refuse(Rule::Size, "D1", CodeKind::Design);
1149 } else {
1150 f.condition(Rule::Size, "c6");
1151 }
1152 }
1153 HostSize::Content if m.size.fills => f.condition(Rule::Size, "c1"),
1154 _ => {}
1155 }
1156}
1157
1158fn keyboard(m: &Mountable, h: &HostOffer, f: &mut Found) {
1160 if h.size == HostSize::Row && m.keyboard == Keyboard::Exclusive {
1161 f.refuse(Rule::Keyboard, "D1", CodeKind::Design);
1162 }
1163 if m.claims.iter().any(|k| h.owns_keys.contains(k)) {
1164 if h.honours_claims {
1165 f.condition(Rule::Keyboard, "c9");
1166 } else {
1167 f.condition(Rule::Keyboard, "c4");
1168 }
1169 } else if m.keyboard == Keyboard::Exclusive && !h.owns_keys.is_empty() && !h.honours_claims {
1170 f.condition(Rule::Keyboard, "c4");
1171 }
1172 if m.keymap && !h.keymap {
1173 f.condition(Rule::Keyboard, "c2");
1174 }
1175}
1176
1177fn session(m: &Mountable, h: &HostOffer, f: &mut Found) {
1179 let transient = h.lifetime == Lifetime::Transient;
1180 if m.session && h.dismissed_by.contains(&Dismiss::OutsideClick) {
1181 f.refuse(Rule::Session, "D3", CodeKind::Design);
1182 } else if m.session && transient {
1183 f.condition(Rule::Session, "c10");
1184 }
1185 if m.popups && transient {
1186 f.condition(Rule::Session, "c5");
1187 }
1188}
1189
1190fn question(m: &Mountable, h: &HostOffer, f: &mut Found) {
1192 if m.asks && !h.is_modal() {
1193 f.refuse(Rule::Question, "D4", CodeKind::Design);
1194 }
1195}
1196
1197fn display(m: &Mountable, h: &HostOffer, f: &mut Found) {
1199 match (m.display, h.display) {
1200 (Display::WideWanted, HostDisplay::Srgb) => f.condition(Rule::Display, "c3"),
1201 (Display::WideRequired, HostDisplay::Srgb) => {
1202 f.refuse(Rule::Display, "U2", CodeKind::Upstream);
1203 }
1204 _ => {}
1205 }
1206 if m.pointer.cursor_images && !h.cursor_images {
1207 f.condition(Rule::Display, "c3");
1208 }
1209}
1210
1211fn instances(m: &Mountable, _: &HostOffer, f: &mut Found) {
1213 if m.instances == Instances::Single {
1214 f.condition(Rule::Instances, "c7");
1215 }
1216}
1217
1218fn workers(m: &Mountable, h: &HostOffer, f: &mut Found) {
1220 if m.workers == Workers::DrainedWhenDrawn && h.hides_content {
1221 f.condition(Rule::Workers, "c11");
1222 }
1223}
1224
1225fn view_state(m: &Mountable, h: &HostOffer, f: &mut Found) {
1227 if m.view_state.is_some() && !h.projection && !h.keeps.contains(&Keep::ViewValues) {
1228 f.condition(Rule::ViewState, "c12");
1229 }
1230}
1231
1232#[cfg(test)]
1233mod tests {
1234 use super::*;
1235
1236 fn panel() -> HostOffer {
1237 HostOffer::new("panel", "P", "Docked panel").hides_content(true)
1238 }
1239
1240 fn row() -> HostOffer {
1241 HostOffer::new("status-bar", "S", "Status bar").size(HostSize::Row)
1242 }
1243
1244 fn popover() -> HostOffer {
1245 HostOffer::new("popover", "O", "Popover")
1246 .size(HostSize::Content)
1247 .focus(FocusPolicy::ClickInto)
1248 .owns_keys(&[ClaimKey::Escape])
1249 .dismissed_by(&[Dismiss::Escape, Dismiss::OutsideClick])
1250 .lifetime(Lifetime::Transient)
1251 }
1252
1253 fn modal() -> HostOffer {
1254 HostOffer::new("modal", "M", "Raw modal")
1255 .size(HostSize::Content)
1256 .focus(FocusPolicy::ModalExclusive)
1257 .owns_keys(&[ClaimKey::Escape])
1258 .dismissed_by(&[Dismiss::Escape])
1259 .lifetime(Lifetime::Transient)
1260 }
1261
1262 fn custom_dialog() -> HostOffer {
1263 HostOffer::new("dialog.custom", "C", "Custom dialog")
1264 .size(HostSize::Given)
1265 .focus(FocusPolicy::ModalExclusive)
1266 .owns_keys(&[ClaimKey::Enter, ClaimKey::Escape])
1267 .honours_claims()
1268 .dismissed_by(&[Dismiss::Answer, Dismiss::Escape])
1269 .lifetime(Lifetime::Transient)
1270 }
1271
1272 fn window() -> HostOffer {
1273 HostOffer::new("window", "W", "Auxiliary window")
1274 .window(Window::Auxiliary)
1275 .keymap(false)
1276 .cursor_images(false)
1277 }
1278
1279 fn overlay() -> HostOffer {
1280 HostOffer::new("viewport.overlay", "V", "Viewport overlay")
1281 .size(HostSize::NoUi)
1282 .gives_app(false)
1283 }
1284
1285 fn codes(fit: &Fit) -> Vec<&'static str> {
1286 match fit {
1287 Fit::Yes => Vec::new(),
1288 Fit::Under { conditions } => conditions.iter().map(|c| c.code).collect(),
1289 Fit::No { refusal, also } => std::iter::once(refusal.code)
1290 .chain(also.iter().map(|r| r.code))
1291 .collect(),
1292 }
1293 }
1294
1295 #[test]
1296 fn a_plain_widget_fits_a_panel() {
1297 let m = Mountable::new("button", "Button");
1298 assert_eq!(check(&m, &panel()), Fit::Yes);
1299 assert_eq!(check(&m, &panel()).cell(), "yes");
1300 }
1301
1302 #[test]
1303 fn r1_size_a_filling_component_needs_a_size_in_a_content_host_and_no_row() {
1304 let m = Mountable::new("grid", "Grid").fills();
1305 assert_eq!(codes(&check(&m, &popover())), ["c1"]);
1306 assert_eq!(check(&m, &panel()), Fit::Yes);
1307 assert_eq!(check(&m, &custom_dialog()), Fit::Yes, "a given size");
1308 let fit = check(&m, &row());
1309 assert_eq!(fit.refusal().unwrap().rule, Rule::Size);
1310 assert_eq!(fit.refusal().unwrap().kind, CodeKind::Design);
1311 assert_eq!(fit.cell(), "D1");
1312 let slider = Mountable::new("slider", "Slider").min(120.0, 20.0);
1314 assert_eq!(codes(&check(&slider, &row())), ["c6"]);
1315 let tall = Mountable::new("card", "Card").min(200.0, 120.0);
1316 assert_eq!(codes(&check(&tall, &row())), ["D1"]);
1317 }
1318
1319 #[test]
1320 fn r2_keyboard_claims_against_owned_keys_and_the_keymap() {
1321 let graph = Mountable::new("graph", "Graph").claims(&[ClaimKey::Escape]);
1322 assert_eq!(codes(&check(&graph, &modal())), ["c4"]);
1323 assert_eq!(codes(&check(&graph, &custom_dialog())), ["c9"], "honoured");
1324 assert_eq!(check(&graph, &panel()), Fit::Yes, "the panel owns no key");
1325 let terminal = Mountable::new("terminal", "Terminal").keyboard(Keyboard::Exclusive);
1326 assert_eq!(codes(&check(&terminal, &modal())), ["c4"]);
1327 let fit = check(&terminal.clone().min(0.0, 10.0), &row());
1328 assert_eq!(fit.refusal().unwrap().rule, Rule::Keyboard);
1329 let editor = Mountable::new("editor", "Editor").surface("editor");
1330 assert!(editor.keymap);
1331 assert_eq!(codes(&check(&editor, &window())), ["c2"]);
1332 assert_eq!(check(&editor, &panel()), Fit::Yes);
1333 }
1334
1335 #[test]
1336 fn r3_a_session_never_goes_in_a_popover_and_restarts_in_a_transient_host() {
1337 let terminal = Mountable::new("terminal", "Terminal").session();
1338 let fit = check(&terminal, &popover());
1339 assert_eq!(fit.refusal().unwrap().rule, Rule::Session);
1340 assert_eq!(fit.cell(), "D3");
1341 assert_eq!(codes(&check(&terminal, &modal())), ["c10"]);
1342 assert_eq!(check(&terminal, &panel()), Fit::Yes);
1343 let code = Mountable::new("code", "Code editor").popups();
1344 assert_eq!(codes(&check(&code, &popover())), ["c5"]);
1345 }
1346
1347 #[test]
1348 fn r4_only_a_modal_asks() {
1349 let q = Mountable::new("question", "Question").asks();
1350 assert_eq!(check(&q, &panel()).cell(), "D4");
1351 assert_eq!(
1352 check(&q, &popover()).refusal().unwrap().rule,
1353 Rule::Question
1354 );
1355 assert!(check(&q, &modal()).fits());
1356 assert!(check(&q, &custom_dialog()).fits());
1357 }
1358
1359 #[test]
1360 fn r5_wide_gamut_degrades_when_wanted_and_refuses_upstream_when_required() {
1361 let wanted = Mountable::new("viewport", "Viewport").display(Display::WideWanted);
1362 assert_eq!(codes(&check(&wanted, &window())), ["c3"]);
1363 let wide = panel().display(HostDisplay::Wide);
1364 assert_eq!(check(&wanted, &wide), Fit::Yes);
1365 let required = Mountable::new("mastering", "Mastering").display(Display::WideRequired);
1366 let fit = check(&required, &window());
1367 assert_eq!(fit.refusal().unwrap().kind, CodeKind::Upstream);
1368 assert_eq!(fit.cell(), "U2");
1369 assert!(check(&required, &wide).fits());
1370 let cursors = Mountable::new("brush", "Brush").cursor_images();
1371 assert_eq!(codes(&check(&cursors, &window())), ["c3"]);
1372 assert_eq!(check(&cursors, &panel()), Fit::Yes);
1373 }
1374
1375 #[test]
1376 fn r6_a_single_component_shows_one_state_wherever_it_is() {
1377 let jobs = Mountable::new("jobs", "Jobs centre").single();
1378 assert_eq!(codes(&check(&jobs, &panel())), ["c7"]);
1379 assert_eq!(codes(&check(&jobs, &popover())), ["c7"]);
1380 }
1381
1382 #[test]
1383 fn r7_and_r8_a_data_host_takes_no_component() {
1384 let plain = Mountable::new("label", "Label");
1385 let fit = check(&plain, &overlay());
1386 assert_eq!(fit.refusal().unwrap().rule, Rule::Ui);
1387 assert_eq!(fit.cell(), "D2");
1388 let app = Mountable::new("tips", "Tips").needs_app();
1389 let fit = check(&app, &overlay());
1390 assert_eq!(codes(&fit), ["D2", "D2"], "R8, then R7");
1391 match fit {
1392 Fit::No { also, .. } => assert_eq!(also[0].rule, Rule::Application),
1393 other => panic!("{other:?}"),
1394 }
1395 let bare = panel().gives_app(false);
1397 assert_eq!(
1398 check(&app, &bare).refusal().unwrap().rule,
1399 Rule::Application
1400 );
1401 assert!(check(&plain, &bare).fits());
1402 }
1403
1404 #[test]
1405 fn r9_workers_queue_while_the_host_hides_the_component() {
1406 let v = Mountable::new("viewport", "Viewport").workers();
1407 assert_eq!(codes(&check(&v, &panel())), ["c11"]);
1408 assert_eq!(check(&v, &custom_dialog()), Fit::Yes, "drawn every frame");
1409 }
1410
1411 #[test]
1412 fn r9_bounded_workers_fit_a_host_that_hides_the_component() {
1413 let v = Mountable::new("viewport", "Viewport").bounded_workers();
1414 assert_eq!(check(&v, &panel()), Fit::Yes);
1415 }
1416
1417 #[test]
1418 fn r10_a_view_state_is_kept_only_by_a_host_that_keeps_it() {
1419 let v = Mountable::new("tree", "Tree").view_state("tree.expanded");
1420 assert_eq!(codes(&check(&v, &modal())), ["c12"]);
1421 assert_eq!(check(&v, &panel().projection(true)), Fit::Yes);
1422 assert_eq!(check(&v, &panel().keeps(&[Keep::ViewValues])), Fit::Yes);
1423 }
1424
1425 #[test]
1426 fn r11_the_four_dialog_kinds_take_descriptor_fields_only() {
1427 let four = modal()
1428 .owns_keys(&[ClaimKey::Enter, ClaimKey::Escape])
1429 .free_content(false);
1430 let viewport = Mountable::new("viewport", "Viewport");
1431 let fit = check(&viewport, &four);
1432 assert_eq!(fit.refusal().unwrap().rule, Rule::DialogContent);
1433 assert_eq!(fit.cell(), "D6");
1434 let slider = Mountable::new("slider", "Slider").descriptor_field();
1435 assert_eq!(codes(&check(&slider, &four)), ["c8"]);
1436 assert!(check(&viewport, &custom_dialog()).fits());
1437 }
1438
1439 #[test]
1440 fn conditions_are_kept_in_code_order_and_the_worst_verdict_wins() {
1441 let terminal = Mountable::new("terminal", "Terminal")
1442 .keyboard(Keyboard::Exclusive)
1443 .claims(&[ClaimKey::Escape, ClaimKey::Enter])
1444 .surface("terminal")
1445 .session()
1446 .fills()
1447 .workers();
1448 let fit = check(&terminal, &modal());
1449 assert_eq!(fit.cell(), "if c1 c4 c10");
1450 assert_eq!(check(&terminal, &popover()).cell(), "D3");
1451 assert_eq!(check(&terminal, &window()).cell(), "if c2");
1452 }
1453
1454 #[test]
1455 fn every_code_used_has_its_text_and_refusals_say_what_to_do_instead() {
1456 for c in CODES {
1457 assert!(!c.title.is_empty() && !c.text.is_empty(), "{}", c.code);
1458 if c.kind != CodeKind::Condition {
1459 assert!(!c.then.is_empty(), "{}", c.code);
1460 }
1461 }
1462 let fit = check(&Mountable::new("session", "S").session(), &popover());
1463 let reason = fit.refusal().unwrap().reason();
1464 assert!(reason.contains("R3"), "{reason}");
1465 assert!(reason.contains("Instead: A panel"), "{reason}");
1466 assert_eq!(Rule::ALL.len(), 11);
1467 let numbers: std::collections::BTreeSet<&str> =
1468 Rule::ALL.iter().map(|r| r.number()).collect();
1469 assert_eq!(numbers.len(), 11);
1470 }
1471}