ui

source of events

1// Input: the topmost interactive element under the pointer is hovered,2// a press captures the pointer to it until the release and focuses it3// when it takes the focus, and keys and text go to the focused element;4// input bubbles up the ancestors until one handles it. Tab and Shift+Tab5// move the focus in tree order.67import std::string;8import std::vector;9import std::option;10import pkg::alloy::window;11import pkg::alloy::draw;1213import ui;14import tree;15import scrolling;16import layers;17import motion;18import extensions;1920// The pointer moved to the point, in logical pixels.21exp fn pointer_moved(self ui: &var Ui, at: Point) {22    ui.pointer = ::Some(at);23    const under = ui.hit_at(at, Hit::Interactive);24    if (ui.pressed is ::Some |pressed|) {25        ui.hover(if (under == ui.pressed) under else ::None);26        ui.dispatch(pressed, Input::Moved(at));27    } else {28        ui.hover(under);29        // an element following the pointer over what it draws30        if (under is ::Some |h| && ui.hovers) {31            ui.send(h, Input::Hover(at));32        }33    }34    ui.extensions_pointer(::Some(at));35}3637pub fn pointer_left(self ui: &var Ui) {38    ui.pointer = ::None;39    if (ui.pressed is ::None) {40        ui.hover(::None);41    }42    ui.extensions_pointer(::None);43}4445// The primary button pressed or released at the point.46exp fn pointer_button(self ui: &var Ui, pressed: bool, at: Point) {47    ui.pointer = ::Some(at);48    if (pressed) {49        ui.pointer_down = ::Some(at);50        // a press outside a popup or menu closes it and goes no further51        if (ui.extensions_pressed(at)) {52            ui.hover(ui.hit_at(at, Hit::Interactive));53            return;54        }55    }56    const under = ui.hit_at(at, Hit::Interactive);57    if (pressed) {58        // the scrim of a modal layer takes the press and does nothing59        if (under is ::None && ui.top_modal() is ::Some |m| && !holds(&ui.panel_frame(m), at)) {60            return;61        }62        ui.hover(under);63        ui.focus_node(ui.focusable_from(under), false);64        if (under is ::Some |target|) {65            ui.pressed = under;66            ui.ease_states(target);67            ui.node_var(target).pressed = true;68            ui.mark_paint(target);69            ui.dispatch(target, Input::Press(at));70        }71        return;72    }73    ui.pointer_down = ::None;74    const target = if (ui.pressed is ::Some |p|) p else {75        ui.extensions_released(at, under);76        return;77    };78    ui.pressed = ::None;79    if (ui.node(target).alive) {80        ui.ease_states(target);81        ui.node_var(target).pressed = false;82        ui.mark_paint(target);83        ui.dispatch(target, Input::Release(at));84        if (under == ::Some(target)) {85            ui.dispatch(target, Input::Click(at));86        }87    }88    if (ui.popup_press is ::Some) {89        ui.extensions_released(at, ui.hit_at(at, Hit::Interactive));90    }91    ui.hover(ui.hit_at(at, Hit::Interactive));92}9394// The context menu's button pressed at the point: the nearest element95// under it with `on_context_menu` is given the point. A press outside a96// popup or menu closes it first and goes no further.97exp fn context_press(self ui: &var Ui, at: Point) {98    ui.pointer = ::Some(at);99    ui.pointer_down = ::Some(at);100    if (ui.extensions_pressed(at)) {101        return;102    }103    ui.context_menu_from(ui.hit_at(at, Hit::Any), at);104}105106// Whether the key opens a context menu: the Menu key, or Shift+F10.107fn opens_context_menu(k: &KeyEvent) -> bool => match (k.key) {108    ::Menu true;109    ::Function |n| n == 10 && k.modifiers.shift;110    else false;111};112113// The context menu's button released at the point.114exp fn context_release(self ui: &var Ui, at: Point) {115    ui.pointer_down = ::None;116    ui.extensions_released(at, ui.hit_at(at, Hit::Interactive));117}118119// Gives the point to the nearest of the node and its ancestors with120// `on_context_menu`, as its handler; whether one took it.121fn context_menu_from(self ui: &var Ui, start: Option<u32>, at: Point) -> bool {122    var found = start;123    while (found is ::Some |k|) {124        var handler = Option<|=| (&var Ui, Point)>::None;125        if (!ui.node(k).transparent && ui.node(k).widget is ::Some |&widget|) {126            if (widget is Column |&c|) {127                handler = copy c.on_context_menu;128            } else if (widget is Row |&r|) {129                handler = copy r.on_context_menu;130            } else if (widget is Stack |&s|) {131                handler = copy s.on_context_menu;132            }133        }134        if (handler is ::Some |&f|) {135            const saved = ui.handling;136            ui.handling = ::Some(k);137            f(&var ui, at);138            ui.handling = saved;139            return true;140        }141        found = ui.node(k).parent;142    }143    return false;144}145146// A key pressed or released: Tab moves the focus, the rest goes to the147// focused element.148exp fn key_event(self ui: &var Ui, key: &KeyEvent) {149    ui.modifiers = key.modifiers;150    // Tab in a menu, Alt+Left going back a screen, before any element151    if (ui.extensions_key(&key)) {152        return;153    }154    if (plain_tab(&key)) {155        if (key.pressed) {156            // a list a Select opened chooses or closes157            if (158                ui.focused is ::Some |f|159                && ui.layer_of(f) is ::Some |k|160                && ui.layers.get(k).overlay.tabbed161            ) {162                ui.to_focused(Input::Key(copy key));163            }164            ui.move_focus(!key.modifiers.shift);165        }166        return;167    }168    if (ui.to_focused(Input::Key(copy key)) || !key.pressed) {169        return;170    }171    if (key.key is ::Escape && ui.extensions_escaped()) {172        return;173    }174    if (opens_context_menu(&key)) {175        // a context menu by the keyboard, below the focused element176        var at = Point { .x = 16.0, .y = 16.0 };177        var from = ui.root;178        if (ui.focused is ::Some |f|) {179            const frame = ui.frame_of(ui.element(f));180            at = Point { .x = frame.x, .y = frame.y + frame.height };181            from = ui.focused;182        }183        ui.focus_ring = true;184        ui.context_menu_from(from, at);185    }186}187188// Whether the key is Tab, or Shift and Tab, which move the focus.189exp fn plain_tab(key: &KeyEvent) -> bool {190    return key.key is ::Tab && !key.modifiers.control && !key.modifiers.alt;191}192193// The wheel or a touchpad scrolled by the delta, in logical pixels, to194// whatever is under the pointer.195pub fn scroll_event(self ui: &var Ui, delta: Point, at: Point) {196    ui.pointer = ::Some(at);197    if (ui.hit_at(at, Hit::Any) is ::Some |under|) {198        ui.dispatch(under, Input::Scroll(delta));199        // what moved under the pointer is hovered now200        ui.hover(ui.hit_at(at, Hit::Interactive));201    }202}203204exp fn text_event(self ui: &var Ui, text: &String) {205    ui.to_focused(Input::Text(copy text));206}207208exp fn preedit_event(self ui: &var Ui, preedit: &Preedit) {209    ui.to_focused(Input::Preedit(copy preedit));210}211212// Gives up the pointer's capture by a press, as a press opening a popup213// does, so the pointer finds what it opened.214exp fn uncapture(self ui: &var Ui) {215    if (ui.pressed is ::Some |p|) {216        ui.pressed = ::None;217        ui.ease_states(p);218        ui.node_var(p).pressed = false;219        ui.mark_paint(p);220    }221}222223// Sends the input to the element alone, a pointer's point in its224// coordinates; whether it took it.225pub fn send(self ui: &var Ui, target: u32, input: Input) -> bool {226    if (!ui.node(target).alive || ui.node(target).transparent) {227        return false;228    }229    const sent = input.relative_to(ui.origin_of(target));230    var handled = false;231    const saved = ui.handling;232    ui.handling = ::Some(target);233    if (ui.node_var(target).widget is ::Some |&var widget|) {234        handled = widget.event(&var ui, &sent);235    }236    ui.handling = saved;237    return handled;238}239240// Sends the input to the focused element, if any; whether one took it.241pub fn to_focused(self ui: &var Ui, input: Input) -> bool {242    if (ui.focused is ::Some |focused|) {243        return ui.dispatch(focused, move input);244    }245    return false;246}247248fn hover(self ui: &var Ui, under: Option<u32>) {249    if (ui.hovered != under) {250        if (ui.hovered is ::Some |old|) {251            ui.ease_states(old);252            ui.node_var(old).hovered = false;253            ui.mark_paint(old);254        }255        ui.hovered = under;256        if (under is ::Some |index|) {257            ui.ease_states(index);258            ui.node_var(index).hovered = true;259            ui.mark_paint(index);260            ui.extensions_hovered(index);261        }262    }263    // the pressed element keeps its cursor while the pointer is away264    const shown = if (ui.pressed is ::Some) ui.pressed else under;265    ui.cursor = Cursor::Arrow;266    if (shown is ::Some |index| && ui.node(index).widget is ::Some |&widget|) {267        ui.cursor = widget.cursor();268    }269}270271// Gives the node the focus, or takes it from all; `ring` when by the272// keyboard.273exp fn focus_node(self ui: &var Ui, target: Option<u32>, ring: bool) {274    if (target is ::Some |index| && !ui.takes_focus(index)) {275        return;276    }277    if (ui.focused == target) {278        if (ui.focus_ring != ring && target is ::Some |index|) {279            ui.focus_ring = ring;280            ui.mark_paint(index);281        }282        return;283    }284    var old = ui.focused;285    ui.focused = target;286    ui.focus_ring = ring;287    ui.access_stale = true;288    if (old is ::Some |left| && ui.node(left).alive) {289        ui.mark_paint(left);290        ui.dispatch(left, Input::Focus(false));291    } else {292        old = ::None;293    }294    if (target is ::Some |index|) {295        ui.mark_paint(index);296        ui.dispatch(index, Input::Focus(true));297        if (ring) {298            ui.reveal(index);299        }300    }301    ui.extensions_focused(old, target, ring);302}303304// The elements taking the focus, in the order Tab goes through them.305exp fn tab_order(self ui: &Ui) -> Vector<u32> {306    var order = Vector<u32>::empty();307    if (ui.layers.length() == 0) {308        if (ui.root is ::Some |root|) {309            ui.collect_focusable(root, &var order);310        }311    } else {312        for (ui.focus_roots().view()) |root| {313            ui.collect_focusable(root, &var order);314        }315    }316    return move order;317}318319exp fn takes_focus(self ui: &Ui, index: u32) -> bool {320    if (ui.node(index).widget is ::Some |&widget|) {321        return widget.focusable();322    }323    return false;324}325326// The node or its nearest ancestor taking the focus.327fn focusable_from(self ui: &Ui, start: Option<u32>) -> Option<u32> {328    var at = start;329    while (at is ::Some |k|) {330        if (!ui.node(k).transparent && ui.takes_focus(k)) {331            return at;332        }333        at = ui.node(k).parent;334    }335    return ::None;336}337338// Focuses the next node taking the focus in tree order, or the previous339// one, going round.340exp fn move_focus(self ui: &var Ui, forward: bool) {341    var order = Vector<u32>::empty();342    if (ui.layers.length() == 0) {343        if (ui.root is ::Some |root|) {344            ui.collect_focusable(root, &var order);345        }346    } else {347        for (ui.focus_roots().view()) |root| {348            ui.collect_focusable(root, &var order);349        }350    }351    const count = order.length();352    if (count == 0) {353        return;354    }355    var at = Option<u64>::None;356    for (order.view(), [..count]) |index, k| {357        if (ui.focused == ::Some(index)) {358            at = ::Some(k);359        }360    }361    const next = if (at is ::Some |k|) (362        if (forward) (k + 1) % count else (k + count - 1) % count363    ) else if (forward) 0 else count - 1;364    ui.focus_node(::Some(order.get(next)), true);365}366367exp fn collect_focusable(self ui: &Ui, index: u32, out: &var Vector<u32>) {368    if (ui.node(index).hidden) {369        return;370    }371    if (!ui.node(index).transparent && ui.takes_focus(index)) {372        out.push(index);373    }374    for (ui.node(index).children.view()) |&child| {375        ui.collect_focusable(child, &var out);376    }377}378379// What a hit test finds: the elements the pointer acts on, or any.380exp type Hit = enum {381    Interactive,382    Any,383};384385// The deepest element of the kind under the point, the last painted386// first.387exp fn hit_at(self ui: &Ui, at: Point, kind: Hit) -> Option<u32> {388    if (ui.layers.length() > 0 && ui.hit_layers(at, kind) is ::Some |found|) {389        return found;390    }391    if (ui.root is ::Some |root|) {392        return ui.hit_node(root, at.x, at.y, kind);393    }394    return ::None;395}396397// The deepest element of the kind under the point, `x` and `y` in the398// parent's coordinates.399pub fn hit_node(self ui: &Ui, index: u32, x: f32, y: f32, kind: Hit) -> Option<u32> {400    const node = &ui.node(index);401    if (node.hidden) {402        return ::None;403    }404    const local_x = x - node.position.x;405    const local_y = y - node.position.y;406    const inside = local_x >= 0.0407        && local_y >= 0.0408        && local_x < node.size.width409        && local_y < node.size.height;410    var clipped = false;411    if (node.widget is ::Some |&widget|) {412        clipped = widget.clips() && !node.transparent;413    }414    if (!clipped || inside) {415        var k = node.children.length();416        while (k > 0) {417            k -= 1;418            const found = ui.hit_node(node.children.get(k), local_x, local_y, kind);419            if (found is ::Some) {420                return found;421            }422        }423    }424    if (!inside || node.transparent) {425        return ::None;426    }427    if (kind is ::Any) {428        return ::Some(index);429    }430    if (node.widget is ::Some |&widget| && widget.interactive()) {431        return ::Some(index);432    }433    return ::None;434}435436// The element's top left from the root's.437exp fn origin_of(self ui: &Ui, index: u32) -> Point {438    var x: f32 = 0.0;439    var y: f32 = 0.0;440    var at: Option<u32> = ::Some(index);441    while (at is ::Some |k|) {442        const node = &ui.node(k);443        x += node.position.x;444        y += node.position.y;445        at = node.parent;446    }447    return Point { .x = x, .y = y };448}449450// Where the focused element takes typed text, from the root's top left.451exp fn input_area(self ui: &Ui) -> Option<Rect> {452    const focused = if (ui.focused is ::Some |f|) f else {453        return ::None;454    };455    if (ui.node(focused).widget is ::Some |&widget| && widget.caret() is ::Some |r|) {456        const origin = ui.origin_of(focused);457        return ::Some(Rect::at(origin.x + r.x, origin.y + r.y, r.width, r.height));458    }459    return ::None;460}461462// Whether the focused element taking text hides it, a password, which463// the input method is told.464pub fn input_secret(self ui: &Ui) -> bool {465    if (466        ui.focused is ::Some |f|467        && ui.node(f).widget is ::Some |&widget|468        && widget is TextField |&field|469    ) {470        return field.secret;471    }472    return false;473}474475// Gives the clipboard's text to the element that asked for it.476pub fn paste_event(self ui: &var Ui, text: &String) {477    if (ui.paste_target is ::Some |target|) {478        ui.paste_target = ::None;479        if (ui.exists(target)) {480            ui.dispatch(target.index, Input::Paste(copy text));481        }482    }483}484485// The input with its point, given from the root's top left, in the486// coordinates of an element there.487fn relative_to(self input: &Input, origin: Point) -> Input {488    const local = (p: Point) => Point { .x = p.x - origin.x, .y = p.y - origin.y };489    return match (input) {490        ::Press |p| Input::Press(local(p));491        ::Release |p| Input::Release(local(p));492        ::Click |p| Input::Click(local(p));493        ::Moved |p| Input::Moved(local(p));494        ::Hover |p| Input::Hover(local(p));495        else copy input;496    };497}498499// Sends the input to the element, then up its ancestors until one500// handles it, a pointer's point in each one's coordinates; whether one501// did.502exp fn dispatch(self ui: &var Ui, target: u32, input: Input) -> bool {503    var at: Option<u32> = ::Some(target);504    while (at is ::Some |k| && ui.node(k).alive) {505        const parent = ui.node(k).parent;506        if (!ui.node(k).transparent) {507            const sent = input.relative_to(ui.origin_of(k));508            var handled = false;509            const saved = ui.handling;510            ui.handling = ::Some(k);511            if (ui.node_var(k).widget is ::Some |&var widget|) {512                handled = widget.event(&var ui, &sent);513            }514            ui.handling = saved;515            if (handled) {516                return true;517            }518        }519        at = parent;520    }521    return false;522}