1// Tooltips (decisions/F11.md, F11a): a Tooltip wraps the2// element it describes, and shows its text in a layer above it (below3// when there is no room) once the pointer rests on it for half a second,4// or at once when the keyboard's focus comes to it. It stays while the5// pointer is over the element or the tooltip, and Escape or a press6// hides it (WCAG 1.4.13); it takes no input and no focus. To a screen7// reader its text is the element's description.89import std::string;10import std::vector;11import std::option;12import std::time;13import std::math;14import pkg::alloy::window;15import pkg::alloy::draw;1617import pkg::alloy::ui;18import pkg::alloy::ui::store;19import pkg::alloy::ui::tree;20import pkg::alloy::ui::layout;21import pkg::alloy::ui::painting;22import pkg::alloy::ui::events;23import pkg::alloy::ui::widgets;24import pkg::alloy::ui::fields;25import pkg::alloy::ui::scrolling;26import pkg::alloy::ui::running;27import pkg::alloy::ui::timers;28import pkg::alloy::ui::layers;29import pkg::alloy::ui::motion;30import pkg::alloy::ui::languages;31import pkg::alloy::ui::extensions;32import pkg::alloy::ui::views;33import ui_controls;3435// How long the pointer rests before a tooltip shows.36const REST: u64 = 500;3738// The tooltip the pointer or the focus is on, the timer that will show39// it, and its layer while it shows.40pub type TipState = struct {41 pub target: Option<u32> = ::None, // the Tooltip element42 pub timer: Option<Timer> = ::None,43 pub layer: Option<Layer> = ::None,44 pub by_focus: bool = false,45};4647// The tooltips' state, an extension installed as the first Tooltip lays48// out, which follows the pointer and the focus from then on.49pub type Tips : Extension = struct {50 pub tip: TipState = TipState {},51};5253pub fn pointer(self t: &var Tips, ui: &var Ui, at: Option<Point>) {54 ui.track_tooltip(at);55}5657pub fn focused(self t: &var Tips, ui: &var Ui, from: Option<u32>, onto: Option<u32>, ring: bool) {58 ui.focus_tooltip(onto, ring);59}6061fn tip(self ui: &var Ui) -> &var TipState {62 if (ui.extension_var<Tips>() is ::Some |&var t|) {63 return &var t.tip;64 }65 panic "pkg::alloy::ui: no tooltips";66}6768// The Tooltip element holding the node, if any.69fn tooltip_of(self ui: &Ui, start: Option<u32>) -> Option<u32> {70 var at = start;71 while (at is ::Some |k|) {72 if (ui.node(k).widget is ::Some |&widget| && widget is Tooltip) {73 return at;74 }75 at = ui.node(k).parent;76 }77 return ::None;78}7980// The pointer moved to the point: the tooltip under it waits to show,81// the one it left goes, unless the pointer is over the tooltip itself.82pub fn track_tooltip(self ui: &var Ui, at: Option<Point>) {83 if (ui.tip().target is ::None && ui.tip().layer is ::None && ui.layers.length() == 0) {84 // no tooltip shows: only one under the pointer matters85 if (ui.tooltip_of(ui.hovered) is ::None) {86 return;87 }88 }89 if (90 ui.tip().layer is ::Some |layer|91 && ui.layer_index(layer) is ::Some |k|92 && at is ::Some |p|93 ) {94 if (holds(&ui.panel_frame(k), p)) {95 return;96 }97 }98 const over = ui.tooltip_of(ui.hovered);99 if (over == ui.tip().target) {100 return;101 }102 if (ui.tip().by_focus && over is ::None) {103 return;104 }105 ui.hide_tooltip();106 ui.tip().target = over;107 if (over is ::Some |target|) {108 const element = ui.element(target);109 ui.tip().timer = ::Some(ui.after(Duration::from_milliseconds(REST), |element| (ui: &var Ui) {110 ui.tip().timer = ::None;111 if (ui.exists(element) && ui.tip().target == ::Some(element.index)) {112 ui.show_tooltip(element.index);113 }114 }));115 }116}117118// The focus came to the node, from the keyboard when `ring`: a tooltip119// holding it shows at once, and one shown for the focus before goes.120pub fn focus_tooltip(self ui: &var Ui, focused: Option<u32>, ring: bool) {121 const holding = if (ring) ui.tooltip_of(focused) else Option<u32>::None;122 if (holding is ::None && !ui.tip().by_focus) {123 return;124 }125 if (holding == ui.tip().target && ui.tip().layer is ::Some) {126 return;127 }128 ui.hide_tooltip();129 if (holding is ::Some |target|) {130 ui.tip().target = holding;131 ui.show_tooltip(target);132 ui.tip().by_focus = true;133 }134}135136fn show_tooltip(self ui: &var Ui, target: u32) {137 var said = if (138 ui.node(target).widget is ::Some |&widget|139 && widget is Tooltip |&t|140 ) copy t.text else {141 return;142 };143 const saved = ui.handling;144 ui.handling = ::None;145 const layer = ui.open_layer(Overlay {146 .place = Place::Above(ui.element(target)),147 .role = LayerRole::Tooltip,148 .label = copy said,149 .panel = false,150 .keyboard = false,151 .passive = true152 }, |move said| (ui: &var Ui, layer: Layer) -> Element {153 return ui.add(Column {154 .padding = Padding::symmetric(8.0, 4.0),155 .background = Ink::Text,156 .radius = 4.0,157 .sizing = { .max_width = 280.0 },158 .children = &[159 ui.add(Text {160 .text = copy said,161 .size = ::Some(13.0),162 .color = Ink::Background,163 }),164 ],165 });166 });167 ui.handling = saved;168 ui.tip().layer = ::Some(layer);169}170171// Hides the tooltip showing, or forgets the one waiting to.172pub fn hide_tooltip(self ui: &var Ui) {173 if (ui.tip().timer is ::Some |timer|) {174 ui.cancel(timer);175 }176 if (ui.tip().layer is ::Some |layer|) {177 ui.tip().layer = ::None;178 ui.close(layer);179 }180 ui.tip() = TipState {};181}182183// Lays out what it holds as a stack does, the Ui then following the184// pointer and the focus for tooltips.185pub fn layout(self t: &var Tooltip, arrange: &var Arrange, limits: Limits) -> Extent {186 if (arrange.ui.extension<Tips>() is ::None) {187 arrange.ui.install(new Tips {});188 }189 var size = Extent::zero();190 for ([..arrange.count()]) |k| {191 const child = arrange.lay_out(k, limits.loosened());192 arrange.place(k, Point { .x = 0.0, .y = 0.0 });193 size.width = max(size.width, child.width);194 size.height = max(size.height, child.height);195 }196 return limits.constrain(size);197}198199exp fn paint(self t: &Tooltip, look: &Look, paint: &var Paint) {}200201pub fn children(self t: &Tooltip) -> &[Element] => &t.children;202203pub fn sizing(self t: &Tooltip) -> Sizing => t.sizing;204205// Found by the pointer over the parts of what it holds that are not.206pub fn interactive(self t: &Tooltip) -> bool => true;207208// To a screen reader nothing itself, its text describing what it holds.209pub fn semantics(self t: &Tooltip) -> Semantics => Semantics { .description = copy t.text };210211exp fn renew(self t: &var Tooltip, next: Tooltip) -> Change {212 t.text = move next.text;213 t.sizing = next.sizing;214 return Change::Layout;215}