ui_app

source of pkg::alloy::ui::tree

1// The element tree: each element's widget, its children, its layout and2// its canvas node.34import std::string;5import std::vector;6import std::option;7import pkg::alloy::window;8import pkg::alloy::draw;9import pkg::alloy::text as text;1011import ui;12import store;13import layers;14import motion;1516// What laying out reads comes first, then the rest by width, which keeps17// a node at 136 bytes and layout as fast as measured.18exp type Node = struct {19    exp widget: Option<*var Widget> = ::None,20    exp children: Vector<u32> = Vector<u32>::empty(),21    pub limits: Limits, // last laid out within22    exp size: Extent,23    exp position: Point = ORIGIN, // in its parent's coordinates24    // where painting shows it, its edges snapped to device pixels25    // (painting.alloy), and how far its top left lies past the pixel it26    // was snapped to, in device pixels (none until shown)27    exp shown_at: Point = ORIGIN,28    pub shown_size: Extent = Extent { .width = 0.0, .height = 0.0 },29    pub residue: Option<Point> = ::None,30    // its last paint made with its layout (`Widget::take_paint`), which31    // does not follow the size it is shown at32    pub laid_paint: bool = false,33    exp parent: Option<u32> = ::None,34    exp generation: u32 = 0,35    exp alive: bool = true,36    // laid out as part of its parent (show and each)37    exp transparent: bool = false,38    // a hidden view's (switch, screens): with all under it, not laid out,39    // drawn, hit, focused nor described40    exp hidden: bool = false,41    pub needs_layout: bool = true,42    pub needs_paint: bool = true,43    pub moved: bool = true,44    // it or a node below it has something for painting to bring to the45    // canvas: painting skips the subtrees where none has46    pub pending: bool = true,47    exp hovered: bool = false,48    pub pressed: bool = false,49    // given a transition (transitions.alloy), which plays as it goes50    exp moving: bool = false,51    // showing a resource still loading (network.alloy), for screen readers52    exp busy: bool = false,53    // which way it reads as last laid out, and the way `directed` gave it54    // (languages.alloy)55    exp direction: LayoutDirection = LayoutDirection::LeftToRight,56    pub directed: Option<LayoutDirection> = ::None,57    // when its options last changed, or it was made, in milliseconds on58    // the window's clock (`look.since`)59    pub changed: u32 = 0,60    exp draw: Option<NodeId> = ::None,61    // the canvas node a clipping element paints into, under `draw`,62    // whose clip keeps it and the children within the element63    pub content: Option<NodeId> = ::None,64    exp overlay: Option<NodeId> = ::None, // over the children of a clipping element65};6667// A new node for the widget, adopting the elements it holds.68exp fn new_node(self ui: &var Ui, widget: *var Widget, transparent: bool) -> u32 {69    var index: u32 = 0;70    var generation: u32 = 0;71    if (ui.free_nodes.pop() is ::Some |k|) {72        index = k;73        generation = ui.node(k).generation;74    } else {75        index = ui.nodes.length() to u32;76        ui.nodes.push(Node::empty());77    }78    var node = Node::empty();79    node.generation = generation;80    node.transparent = transparent;81    node.changed = ui.clock_ms();82    for (widget.children()) |&child| {83        ui.check_unparented(child);84        ui.node_var(child.index).parent = ::Some(index);85        node.children.push(child.index);86    }87    node.widget = ::Some(move widget);88    ui.nodes.set(index to u64, move node);89    ui.owners.get_var(ui.owner to u64).nodes.push(index);90    return index;91}9293fn Node::empty() -> Node => Node { .limits = Limits {}, .size = Extent::zero() };9495exp fn element(self ui: &Ui, index: u32) -> Element => Element {96    .index = index,97    .generation = ui.node(index).generation98};99100pub fn check_node(self ui: &Ui, index: u32, generation: u32) {101    if (!ui.exists(Element { .index = index, .generation = generation })) {102        panic "pkg::alloy::ui: an element used after its owner went";103    }104}105106// Checks that the element is there and has no parent yet.107fn check_unparented(self ui: &Ui, child: Element) {108    ui.check_node(child.index, child.generation);109    if (ui.node(child.index).parent is ::Some) {110        panic "pkg::alloy::ui: an element is given to two parents";111    }112}113114// Frees the node, its canvas node with it.115pub fn free_node(self ui: &var Ui, index: u32) {116    const node = &var ui.node_var(index);117    if (!node.alive) {118        return;119    }120    const parent = node.parent;121    if (node.draw is ::Some |id| && ui.scene is ::Some |&var scene|) {122        if (scene.canvas.contains(id)) {123            scene.canvas.remove(id);124        }125    }126    node.alive = false;127    node.generation += 1;128    var widget = move node.widget;129    node.widget = ::None;130    node.draw = ::None;131    node.content = ::None;132    node.overlay = ::None;133    node.children.clear();134    node.parent = ::None;135    if (parent is ::Some |p| && ui.node(p).alive) {136        const siblings = &var ui.node_var(p).children;137        for ([..siblings.length()]) |k| {138            if (siblings.get(k) == index) {139                siblings.remove(k);140                break;141            }142        }143        ui.mark_layout(p);144    }145    forget(&var ui.hovered, index);146    forget(&var ui.pressed, index);147    forget(&var ui.focused, index);148    forget(&var ui.root, index);149    ui.free_nodes.push(index);150    // a handler may be running in it, as a dialog's button closing the151    // dialog: it goes once the event is handled152    if (ui.handling is ::Some && widget is ::Some |move w|) {153        ui.graveyard.push(move w);154    }155}156157// Empties the slot when it holds the node.158fn forget(slot: &var Option<u32>, index: u32) {159    if (slot == ::Some(index)) {160        slot = ::None;161    }162}163164// Makes the element the node's last child.165exp fn adopt(self ui: &var Ui, parent: u32, child: Element) {166    ui.check_unparented(child);167    ui.adopt_index(parent, child.index);168}169170pub fn adopt_index(self ui: &var Ui, parent: u32, child: u32) {171    ui.node_var(child).parent = ::Some(parent);172    ui.node_var(parent).children.push(child);173    ui.node_var(child).moved = true;174    ui.mark_sync(child);175}176177// Marks the node as having something for painting, and its ancestors as178// holding it, up to the first already marked.179pub fn mark_sync(self ui: &var Ui, index: u32) {180    ui.node_var(index).pending = true;181    var above = ui.node(index).parent;182    while (above is ::Some |k| && !ui.node(k).pending) {183        ui.node_var(k).pending = true;184        above = ui.node(k).parent;185    }186    ui.paint_pending = true;187    ui.access_stale = true;188}189190// Takes the node's children from it, leaving them parentless.191pub fn disown_children(self ui: &var Ui, parent: u32) {192    const children = copy ui.node(parent).children;193    for (children.view()) |&child| {194        ui.node_var(child).parent = ::None;195    }196    ui.node_var(parent).children.clear();197}198199// Lays the node out again, and the ancestors whose size may change with200// it.201exp fn mark_layout(self ui: &var Ui, index: u32) {202    var at: Option<u32> = ::Some(index);203    while (at is ::Some |k|) {204        const node = &var ui.node_var(k);205        if (node.needs_layout && !node.transparent) {206            break;207        }208        node.needs_layout = true;209        at = node.parent;210    }211    ui.layout_pending = true;212    ui.access_stale = true;213}214215// Lays out and paints again all under the node, which turned the other216// way.217pub fn mark_below(self ui: &var Ui, index: u32) {218    const count = ui.node(index).children.length();219    for ([..count]) |k| {220        const child = ui.node(index).children.get(k);221        const node = &var ui.node_var(child);222        node.needs_layout = true;223        node.needs_paint = true;224        node.pending = true;225        ui.mark_below(child);226    }227    ui.paint_pending = true;228}229230exp fn mark_paint(self ui: &var Ui, index: u32) {231    ui.node_var(index).needs_paint = true;232    ui.mark_sync(index);233}234235// Lays out or paints the node again as the change says.236exp fn apply(self ui: &var Ui, index: u32, change: Change) {237    match (change) {238        ::Layout {239            ui.mark_layout(index);240            ui.mark_paint(index);241        }242        ::Paint {243            ui.mark_paint(index);244        }245        ::Nothing {}246    }247}248249// Gives a bound element its new options.250pub fn renew_node<W: Widget>(self ui: &var Ui, element: Element, next: W) {251    if (!ui.exists(element)) {252        return;253    }254    var change = Change::Nothing;255    var closing = Option<Layer>::None;256    if (ui.node_var(element.index).widget is ::Some |&var widget| && widget is W |&var old|) {257        change = old.renew(move next);258        // a popup open on options that changed closes, as a Select's list259        if (W: Popup) {260            closing = old.take_closing();261        }262    }263    if (!(change is ::Nothing)) {264        ui.node_var(element.index).changed = ui.clock_ms();265    }266    ui.apply(element.index, change);267    if (closing is ::Some |layer|) {268        ui.close(layer);269    }270}271272exp fn node_sizing(self ui: &Ui, index: u32) -> Sizing {273    if (ui.node(index).widget is ::Some |&widget|) {274        return widget.sizing();275    }276    return Sizing {};277}278279// Lays out and paints every element again, as for a new theme.280exp fn invalidate_all(self ui: &var Ui) {281    for (ui.layers.view()) |&slot| {282        ui.node_var(slot.host.index).moved = true;283    }284    for (ui.nodes.items()) |&var node| {285        if (node.alive) {286            node.needs_layout = true;287            node.needs_paint = true;288            node.pending = true;289        }290    }291    ui.layout_pending = true;292    ui.paint_pending = true;293}294295// The fonts text is laid out with: the canvas's, or the Ui's own with296// none (a headless Ui's, or one's before its window's first frame),297// which hold the bundled faces alone, so that layout there is the same298// on every machine and asks the system for nothing; each given what299// `install_fonts` installs.300exp fn fonts(self ui: &var Ui) -> &var text::Fonts {301    if (ui.scene is ::Some |&var scene|) {302        return &var scene.canvas.fonts();303    }304    if (ui.own_fonts is ::None) {305        var made = text::Fonts::bundled();306        for (ui.font_installs.view()) |install| {307            install(&var made);308        }309        ui.own_fonts = ::Some(move made);310    }311    if (ui.own_fonts is ::Some |&var fonts|) {312        return &var fonts;313    }314    panic "pkg::alloy::ui: no fonts";315}