Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-ui

Source of release 0.19.1

0.19.1 / tests / controls / contextual.luc

from luce_ui.ui import Application, Button, Command, CommandPalette, ContextMenu, TextPrompt, TextEditor, ListView, VStack, Pane, Event, Font, Theme
from luce_window.input import EventKind, Key
from luce_ui.ui import ListItem
import time

pub func contextual() -> unit!:
    let font = Font()
    let editor = TextEditor("selected text", font = font)
    let action = Command("Edit Settings")
    var invoked = 0
    let connection = action.on_trigger(func (): invoked += 1)
    let context = ContextMenu(editor, [action], font = font)
    assert(context.layout().minimum_size().width == editor.layout().minimum_size().width)
    let pane = Pane(context)
    let palette = CommandPalette([action, Command("Edit Theme"), Command("Build Source")], font = font)
    let prompt = TextPrompt(font = font)
    let root = VStack([pane, palette, prompt], spacing = 0.0)
    let app = Application(root)
    app.layout(640.0, 360.0)
    app.focus(editor.layout())
    app.layout(640.0, 360.0)
    assert(pane.layout().contains_focus())
    editor.set_selection(0, editor.length())
    app.dispatch(Event(kind = EventKind.pointer_down, button = 1, x = font.advance() * 9.0, y = 12.0))
    assert(context.is_open() and editor.selected_text() == "selected text")
    let popup = context.layout().child(1).layout().bounds()
    assert(popup.x >= 0.0 and popup.x + popup.width <= 640.0 and popup.y + popup.height <= 360.0)
    assert(pane.layout().contains_focus())
    app.dispatch(Event(kind = EventKind.key_down, key = Key.enter))
    assert(invoked == 1 and not context.is_open() and editor.layout().is_focused())
    app.dispatch(Event(kind = EventKind.key_down, key = Key.f10, shift = true))
    assert(context.is_open())
    app.dispatch(Event(kind = EventKind.key_down, key = Key.escape))
    assert(editor.layout().is_focused())
    palette.open()
    app.layout(640.0, 360.0)
    assert(palette.layout().is_focused() and pane.layout().contains_focus())
    for scalar in [116, 72, 101, 77, 101]:
        app.dispatch(Event(kind = EventKind.text_input, codepoint = scalar))
    assert(palette.query() == "tHeMe" and palette.result_count() == 1)
    app.dispatch(Event(kind = EventKind.key_down, key = Key.a, control = true))
    app.dispatch(Event(kind = EventKind.text_input, codepoint = 122))
    assert(palette.result_count() == 0)
    app.dispatch(Event(kind = EventKind.key_down, key = Key.enter))
    assert(palette.is_open() and invoked == 1)
    app.dispatch(Event(kind = EventKind.key_down, key = Key.escape))
    assert(editor.layout().is_focused())
    palette.open()
    app.layout(640.0, 360.0)
    # An empty palette lists nothing, so a query selects the command before Enter.
    for scalar in [83, 101, 116]:
        app.dispatch(Event(kind = EventKind.text_input, codepoint = scalar))
    assert(palette.result_count() == 1)
    app.dispatch(Event(kind = EventKind.key_down, key = Key.enter))
    assert(invoked == 2 and editor.layout().is_focused())
    var submitted = ""
    let accepted = prompt.on_submit(func (text: str): submitted = text)
    prompt.open("New file", "old")
    app.layout(640.0, 360.0)
    app.dispatch(Event(kind = EventKind.text_input, codepoint = 955))
    app.dispatch(Event(kind = EventKind.text_input, codepoint = 128512))
    assert(prompt.value() == "λ😀")
    app.dispatch(Event(kind = EventKind.key_down, key = Key.enter))
    assert(submitted == "λ😀" and not prompt.is_open() and editor.layout().is_focused())
    prompt.open("Delete file?", editable = false)
    app.layout(640.0, 360.0)
    app.dispatch(Event(kind = EventKind.key_down, key = Key.escape))
    assert(submitted == "λ😀")
    let listing = ListView([ListItem("one"), ListItem("two")], font = font)
    var activated = -1
    let selected = listing.on_activate(func (index: int): activated = index)
    let list_context = ContextMenu(listing, [Command("Duplicate")], font = font)
    let lists = Application(list_context)
    lists.layout(320.0, 200.0)
    lists.dispatch(Event(kind = EventKind.pointer_down, button = 1, x = 20.0, y = font.line_height() + 2.0))
    assert(listing.selected_index() == 1 and activated == -1 and list_context.is_open())
    lists.dispatch(Event(kind = EventKind.key_down, key = Key.escape))
    lists.dispatch(Event(kind = EventKind.pointer_down, button = 1, x = 20.0, y = 190.0))
    assert(listing.selected_index() == -1 and activated == -1)
    # A button held a moment opens its flyout while still held; sliding onto
    # an item and letting go chooses it, as a tool group's flyout.
    let chosen = Command("Pencil")
    var picked = 0
    let picking = chosen.on_trigger(func (): picked += 1)
    let face = Button(icon = "brush-tool", font = font)
    face.layout().set_size(32.0, 32.0)
    var clicked = 0
    let clicking = face.on_click(func (): clicked += 1)
    let flyout = ContextMenu(face, [chosen], font = font)
    let holding = face.on_hold(func () -> unit!: flyout.open_at(34.0, 0.0, held = true))
    let rail = Application(flyout)
    rail.layout(320.0, 200.0)
    rail.dispatch(Event(kind = EventKind.pointer_down, button = 0, x = 16.0, y = 16.0, inside = true))
    let started = time.now()
    while time.now() - started < 450000000:
        _ = 0
    face.poll_hold()
    rail.layout(320.0, 200.0)
    assert(flyout.is_open())
    let menu = flyout.layout().child(1).layout().bounds()
    rail.dispatch(Event(kind = EventKind.pointer_moved, x = menu.x + menu.width * 0.5, y = menu.y + menu.height * 0.5))
    rail.dispatch(Event(kind = EventKind.pointer_up, button = 0, x = menu.x + menu.width * 0.5, y = menu.y + menu.height * 0.5))
    assert(picked == 1 and clicked == 0 and not flyout.is_open())
    picking.disconnect()
    clicking.disconnect()
    holding.disconnect()
    let theme = Theme()
    assert(theme.gutter != theme.background and theme.active_border != theme.border)
    # Replacing content twice in one callback must not confuse retained traversal
    # membership with the current parent edge.
    let first = TextEditor("one")
    let second = TextEditor("two")
    let host = VStack([first])
    let switching = Application(host)
    switching.layout(400.0, 200.0)
    host.set_children([second])
    host.set_children([first])
    switching.layout(400.0, 200.0)
    switching.focus(first.layout())
    switching.layout(400.0, 200.0)
    assert(first.layout().is_focused() and not second.layout().is_focused())
    font.configure(16.0)
    assert(font.size() == 16.0)
    var refused = false
    font.configure(-1.0) catch failure: refused = true
    assert(refused and font.size() == 16.0)
    selected.disconnect()
    accepted.disconnect()
    connection.disconnect()