1// Views kept hidden while another shows (decisions/F11.md, F11c,2// `switch`, and pkg::alloy::ui_app's screens): a hidden view's element is3// skipped by layout, painting, hit tests, the focus and the accessibility4// tree, its canvas node hidden with what it draws, and its owner's5// bindings wait for it to show. A view shown may take the focus once it6// is laid out.78import std::string;9import std::vector;10import std::option;11import std::map;12import std::hash as hash;13import pkg::alloy::window;1415import ui;16import store;17import tree;18import events;19import layers;20import motion;21import transitions;2223/* --- Hidden views --- */2425// A view made: its owner and its element.26exp type View = struct {27 exp owner: u32,28 exp root: u32,29};3031// A switch's views, by key, and the key shown.32pub type Views<K: hash::Key> = struct {33 pub shown: Option<K>,34 pub views: Map<K, View>,35};3637// Hides the view, keeping all it made, or shows it again.38exp fn set_view_hidden(self ui: &var Ui, view: View, hidden: bool) {39 if (ui.node(view.root).hidden == hidden) {40 return;41 }42 ui.set_owner_hidden(view.owner, hidden);43 ui.node_var(view.root).hidden = hidden;44 ui.mark_sync(view.root);45 if (!hidden) {46 return;47 }48 // what the pointer and the keyboard were on goes with it49 if (ui.focused is ::Some |f| && ui.within(f, view.root)) {50 ui.focus_node(::None, false);51 }52 if (ui.hovered is ::Some |h| && ui.within(h, view.root)) {53 ui.ease_states(h);54 ui.node_var(h).hovered = false;55 ui.hovered = ::None;56 }57 if (ui.pressed is ::Some |p| && ui.within(p, view.root)) {58 ui.ease_states(p);59 ui.node_var(p).pressed = false;60 ui.pressed = ::None;61 }62}6364// Shows the view for the key now given, hiding or freeing the one shown.65pub fn run_switch<K: hash::Key>(66 self ui: &var Ui,67 element: Element,68 state: Signal<Views<K>>,69 key: &|&| (&Ui) -> K,70 view: &|&| (&var Ui, K) -> Element,71 keep: Keep72) {73 var next = key(&ui);74 var leaving = Option<K>::None;75 if (state.peek(&ui).shown is ::Some |&k|) {76 if (k.same(&next)) {77 return;78 }79 leaving = ::Some(copy k);80 }81 if (leaving is ::Some |&k|) {82 const was = state.peek(&ui).views.get(&k);83 if (keep is ::All) {84 ui.set_view_hidden(was, true);85 ui.switch_out(was.root, false);86 } else {87 ui.switch_out(was.root, true);88 ui.free_owner(was.owner);89 state.peek_var(&var ui).views.remove(&k);90 }91 }92 if (state.peek(&ui).views.try_get(&next) is ::Some |&kept|) {93 ui.set_view_hidden(kept, false);94 ui.switch_in(kept.root);95 } else {96 const made = ui.new_owner(ui.owner);97 const root = ui.owned(made, &|&view, &next| (ui: &var Ui) -> Element => view(98 &var ui,99 copy next100 ));101 ui.adopt(element.index, root);102 state.peek_var(&var ui).views.insert(copy next, View { .owner = made, .root = root.index });103 if (leaving is ::Some) {104 ui.switch_in(root.index);105 }106 }107 state.peek_var(&var ui).shown = ::Some(move next);108 ui.mark_layout(element.index);109}110111// Where the focus goes once a screen shown is laid out: to the element,112// else to the screen's first taking it.113exp type ScreenFocus = struct {114 exp root: Element,115 exp element: Option<Element>,116 exp ring: bool,117};118119// Focuses the element a screen shown gives the focus to, or its first120// taking it; with none, the focus stays.121pub fn focus_screen(self ui: &var Ui, after: ScreenFocus) {122 if (!ui.exists(after.root)) {123 return;124 }125 if (after.element is ::Some |e| && ui.exists(e) && ui.within(e.index, after.root.index)) {126 ui.focus_node(::Some(e.index), after.ring);127 return;128 }129 var order = Vector<u32>::empty();130 ui.collect_focusable(after.root.index, &var order);131 if (order.try_get(0) is ::Some |&first|) {132 ui.focus_node(::Some(first), after.ring);133 }134}