Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-ui

Source of release 0.19.2

0.19.2 / tests / controls / dialogs.luc

from luce_ui.ui import Application, Font, Button, TextField, TextPrompt, VStack, Dialog, Command, Shortcut, Event, NumberField, TextEditor
from luce_gpu.gpu import Frame
from luce_window.input import EventKind, Key
from luce_window import window
from luce_ui import ui
import platform
from interop import Connection
import time

class Count:
    pub var clicks: int = 0
    pub var triggers: int = 0
    pub var copies: int = 0
    pub var pastes: int = 0
    pub var cancels: int = 0
    pub var accepts: int = 0
    pub func click(self):
        self.clicks += 1
    pub func trigger(self):
        self.triggers += 1
    pub func copy(self):
        self.copies += 1
    pub func paste(self):
        self.pastes += 1
    pub func cancel(self):
        self.cancels += 1
    pub func accept(self):
        self.accepts += 1

func click(app: Application, x: float, y: float) -> unit!:
    app.dispatch(Event(kind = EventKind.pointer_down, x = x, y = y))
    app.dispatch(Event(kind = EventKind.pointer_up, x = x, y = y))

func render(app: Application) -> unit!:
    let frame = Frame(window.Size(600.0, 500.0, 600.0, 500.0, 1.0))
    app.layout(600.0, 500.0)
    app.render(frame.target())
    frame.close()

## A Dialog is a non-modal floating card: a click behind it reaches what is
## there, and its own field takes focus and typing. A plain-key shortcut waits
## while a field takes text. A prompt is still modal.
pub func dialogs() -> unit!:
    let font = Font()
    let count = Count()
    let behind = Button("Behind", font = font)
    let clicked = behind.on_click(count.click)
    let inside = TextField("", font = font, cells = 12.0)
    let dialog = Dialog([inside], width = 240.0)
    let prompt = TextPrompt(font = font)
    let app = Application(VStack([behind, dialog, prompt], spacing = 0.0))
    let brush = Command("Brush", Shortcut(Key.b), id = "brush")
    let triggered = brush.on_trigger(count.trigger)
    let copy = Command("Copy", Shortcut(Key.c, primary = true), id = "copy")
    let copied = copy.on_trigger(count.copy)
    let paste = Command("Paste", Shortcut(Key.v, primary = true), id = "paste")
    let pasted = paste.on_trigger(count.paste)
    app.set_commands([brush, copy, paste])
    dialog.open()
    render(app)
    let button = behind.layout().bounds()
    click(app, button.x + 4.0, button.y + 4.0)
    render(app)
    assert(count.clicks == 1 and dialog.is_open())
    # The key reaches the command while nothing takes text, and so does the
    # primary-V chord: the application's Paste.
    app.dispatch(Event(kind = EventKind.key_down, key = Key.b))
    assert(count.triggers == 1)
    app.dispatch(Event(kind = EventKind.key_down, key = Key.v, meta = platform.macos, control = not platform.macos))
    assert(count.pastes == 1)
    let field = inside.layout().bounds()
    click(app, field.x + 6.0, field.y + field.height * 0.5)
    render(app)
    assert(inside.layout().is_focused())
    app.dispatch(Event(kind = EventKind.key_down, key = Key.b))
    app.dispatch(Event(kind = EventKind.text_input, codepoint = 98))
    render(app)
    assert(inside.text() == "b" and count.triggers == 1)
    # Cmd-C in the field is the field's copy, not the application's; the paste
    # chord is the field's too, whatever the clipboard holds.
    app.dispatch(Event(kind = EventKind.key_down, key = Key.c, meta = true))
    assert(count.copies == 0)
    app.dispatch(Event(kind = EventKind.key_down, key = Key.v, meta = platform.macos, control = not platform.macos)) catch failure:
        _ = failure
    assert(count.pastes == 1)
    # A prompt holds input: the click behind it is swallowed.
    prompt.open("Name", "")
    render(app)
    click(app, button.x + 4.0, button.y + 4.0)
    render(app)
    assert(count.clicks == 1)
    clicked.disconnect()
    triggered.disconnect()
    copied.disconnect()
    pasted.disconnect()
    app.close()

## Escape and Enter go to an open dialog before any shortcut: Escape cancels,
## Enter accepts. A field being typed in first drops (Escape) or keeps (Enter)
## its text; a multi-line editor keeps Enter for its lines.
pub func dialog_keys() -> unit!:
    let font = Font()
    let count = Count()
    let behind = Button("Behind", font = font)
    let number = NumberField(10.0, 0.0, 100.0, 1.0, font = font, cells = 8.0)
    let name = TextField("", font = font, cells = 12.0)
    let notes = TextEditor("", font = font)
    let dialog = Dialog([number, name, notes], width = 300.0)
    let cancelled = dialog.on_cancel(count.cancel)
    let accepted = dialog.on_accept(count.accept)
    let app = Application(VStack([behind, dialog], spacing = 0.0))
    let escape = Command("Deselect", Shortcut(Key.escape), id = "deselect")
    let triggered = escape.on_trigger(count.trigger)
    app.set_commands([escape])
    render(app)
    # Closed: Escape is the shortcut's.
    app.dispatch(Event(kind = EventKind.key_down, key = Key.escape))
    assert(count.triggers == 1 and count.cancels == 0)
    dialog.open()
    render(app)
    # Open, with focus elsewhere: the dialog cancels, the shortcut waits.
    app.dispatch(Event(kind = EventKind.key_down, key = Key.escape))
    assert(count.triggers == 1 and count.cancels == 1)
    # A typed number stands when Enter accepts.
    let field = number.layout().bounds()
    click(app, field.x + 6.0, field.y + field.height * 0.5)
    render(app)
    app.dispatch(Event(kind = EventKind.text_input, codepoint = 55))
    app.dispatch(Event(kind = EventKind.key_down, key = Key.enter))
    assert(count.accepts == 1 and number.value() == 7.0)
    # Typed text is dropped when Escape cancels.
    let line = name.layout().bounds()
    click(app, line.x + 6.0, line.y + line.height * 0.5)
    render(app)
    app.dispatch(Event(kind = EventKind.text_input, codepoint = 120))
    app.dispatch(Event(kind = EventKind.key_down, key = Key.escape))
    assert(count.cancels == 2 and name.text() == "")
    # Enter in a multi-line editor breaks the line; the dialog stays.
    app.focus(notes.layout())
    render(app)
    app.dispatch(Event(kind = EventKind.key_down, key = Key.enter))
    assert(count.accepts == 1)
    # Held keys and chords are not the dialog's.
    app.dispatch(Event(kind = EventKind.key_down, key = Key.escape, repeated = true))
    assert(count.cancels == 2)
    dialog.hide()
    render(app)
    app.dispatch(Event(kind = EventKind.key_down, key = Key.escape))
    assert(count.cancels == 2 and count.triggers == 2)
    cancelled.disconnect()
    accepted.disconnect()
    triggered.disconnect()
    app.close()

## With a submit listener, the first Enter keeps what was typed and the
## second submits (Photoshop's option bar applying a transform).
pub func number_submit() -> unit!:
    let font = Font()
    let count = Count()
    let number = NumberField(10.0, 0.0, 100.0, 1.0, font = font, cells = 8.0)
    let submitted = number.on_submit(count.accept)
    let app = Application(VStack([number], spacing = 0.0))
    render(app)
    let field = number.layout().bounds()
    click(app, field.x + 6.0, field.y + field.height * 0.5)
    render(app)
    app.dispatch(Event(kind = EventKind.text_input, codepoint = 55))
    app.dispatch(Event(kind = EventKind.key_down, key = Key.enter))
    assert(number.value() == 7.0 and count.accepts == 0)
    app.dispatch(Event(kind = EventKind.key_down, key = Key.enter))
    assert(count.accepts == 1)
    submitted.disconnect()
    app.close()
    # Steps count from zero, not the minimum: 4 typed in a field from 0.5 stays 4.
    let width = NumberField(1.0, 0.5, 500.0, 1.0, font = font)
    let other = Application(VStack([width], spacing = 0.0))
    render(other)
    let box = width.layout().bounds()
    click(other, box.x + 6.0, box.y + box.height * 0.5)
    render(other)
    other.dispatch(Event(kind = EventKind.text_input, codepoint = 52))
    other.dispatch(Event(kind = EventKind.key_down, key = Key.enter))
    assert(width.value() == 4.0)
    # Typed and set values keep the shown decimals; the step is for drags and arrows.
    let fine = NumberField(1.0, 0.5, 500.0, 1.0, decimals = 1, font = font)
    fine.set_value(3.5)
    assert(fine.value() == 3.5)
    width.set_value(3.5)
    assert(width.value() == 4.0)
    other.close()

## `edit` opens a field ready to type, as Fusion's heads-up value box: the
## shown value is selected, so typing replaces it; one Enter commits and
## submits; Escape keeps the value and calls on_cancel.
pub func number_edit() -> unit!:
    let font = Font()
    let count = Count()
    let number = NumberField(12.5, 0.0, 1000.0, 1.0, 2, font = font, cells = 8.0)
    let submitted = number.on_submit(count.accept)
    let cancelled = number.on_cancel(count.cancel)
    let app = Application(VStack([number], spacing = 0.0))
    render(app)
    number.edit()
    render(app)
    assert(number.layout().is_focused())
    app.dispatch(Event(kind = EventKind.text_input, codepoint = 53))
    app.dispatch(Event(kind = EventKind.text_input, codepoint = 48))
    app.dispatch(Event(kind = EventKind.key_down, key = Key.enter))
    assert(number.value() == 50.0 and count.accepts == 1 and count.cancels == 0)
    # Escape throws the typing away.
    number.edit()
    render(app)
    app.dispatch(Event(kind = EventKind.text_input, codepoint = 57))
    app.dispatch(Event(kind = EventKind.key_down, key = Key.escape))
    assert(number.value() == 50.0 and count.accepts == 1 and count.cancels == 1)
    # Typing begun by a click still takes two Enters to submit.
    let field = number.layout().bounds()
    click(app, field.x + 6.0, field.y + field.height * 0.5)
    render(app)
    app.dispatch(Event(kind = EventKind.text_input, codepoint = 55))
    app.dispatch(Event(kind = EventKind.key_down, key = Key.enter))
    assert(number.value() == 7.0 and count.accepts == 1)
    submitted.disconnect()
    cancelled.disconnect()
    app.close()

## A field's edit stands when focus moves on: a click on a button, or Tab,
## commits it (Save clicked with the caret still in a field keeps the text).
pub func field_leaves() -> unit!:
    let font = Font()
    let count = Count()
    let path = TextField("", font = font, cells = 16.0)
    let other = TextField("", font = font, cells = 16.0)
    let save = Button("Save", font = font)
    var committed = ""
    let kept = path.on_commit(func (value: str) -> unit!:
        committed = value)
    let clicked = save.on_click(count.click)
    let app = Application(VStack([path, other, save], spacing = 0.0))
    render(app)
    let line = path.layout().bounds()
    click(app, line.x + 6.0, line.y + line.height * 0.5)
    render(app)
    app.dispatch(Event(kind = EventKind.text_input, codepoint = 120))
    let button = save.layout().bounds()
    click(app, button.x + 4.0, button.y + button.height * 0.5)
    render(app)
    assert(path.text() == "x" and committed == "x")
    # Tab from the field keeps its edit too.
    click(app, line.x + 6.0, line.y + line.height * 0.5)
    render(app)
    app.dispatch(Event(kind = EventKind.text_input, codepoint = 121))
    app.dispatch(Event(kind = EventKind.key_down, key = Key.tab))
    render(app)
    # A click into the field selects all on macOS and places the caret on
    # Windows, so "y" either replaced "x" or went in front of it.
    assert(path.text() == committed and (committed == "y" or committed == "yx"))
    # Escape still drops what was typed since.
    click(app, line.x + 6.0, line.y + line.height * 0.5)
    render(app)
    app.dispatch(Event(kind = EventKind.text_input, codepoint = 122))
    app.dispatch(Event(kind = EventKind.key_down, key = Key.escape))
    assert(path.text() == committed)
    kept.disconnect()
    clicked.disconnect()
    app.close()

## A focused field keeps typing and its editing chords, but function keys and
## Alt chords stay the application's commands.
pub func field_command_keys() -> unit!:
    let font = Font()
    let count = Count()
    let field = TextField("", font = font, cells = 16.0)
    let app = Application(VStack([field], spacing = 0.0))
    let next = Command("Next Panel", Shortcut(Key.f6), id = "next")
    let wrap = Command("Wrap", Shortcut(Key.z, primary = true, alt = true), id = "wrap")
    let undo = Command("Undo", Shortcut(Key.z, primary = true), id = "undo")
    let triggered_next = next.on_trigger(count.trigger)
    let triggered_wrap = wrap.on_trigger(count.trigger)
    let triggered_undo = undo.on_trigger(count.click)
    app.set_commands([next, wrap, undo])
    render(app)
    app.focus(field.layout())
    render(app)
    app.dispatch(Event(kind = EventKind.key_down, key = Key.f6))
    app.dispatch(Event(kind = EventKind.key_down, key = Key.z, control = true, alt = true, meta = true))
    assert(count.triggers == 2)
    # Cmd/Ctrl+Z is the field's own undo, not the application's.
    app.dispatch(Event(kind = EventKind.key_down, key = Key.z, control = true, meta = true))
    assert(count.clicks == 0)
    triggered_next.disconnect()
    triggered_wrap.disconnect()
    triggered_undo.disconnect()
    app.close()

## Enter submits a field every time, also when its text did not change (an
## address bar loads its page again); a commit only follows a change.
pub func field_submits() -> unit!:
    let address = TextField("https://example.com", cells = 24.0)
    var submitted: list[str] = []
    var committed = 0
    let submits = address.on_submit(func (value: str) -> unit!:
        submitted.append(value))
    let commits = address.on_commit(func (value: str) -> unit!:
        committed += 1)
    let app = Application(VStack([address], spacing = 0.0))
    render(app)
    app.focus(address.layout())
    render(app)
    app.dispatch(Event(kind = EventKind.key_down, key = Key.enter))
    assert(submitted.length == 1 and submitted[0] == "https://example.com" and committed == 0)
    app.dispatch(Event(kind = EventKind.key_down, key = Key.end))
    app.dispatch(Event(kind = EventKind.text_input, codepoint = 47))
    app.dispatch(Event(kind = EventKind.key_down, key = Key.enter))
    assert(submitted.length == 2 and submitted[1] == "https://example.com/" and committed == 1)
    # A held Enter repeats nothing.
    app.dispatch(Event(kind = EventKind.key_down, key = Key.enter, repeated = true))
    assert(submitted.length == 2)
    submits.disconnect()
    commits.disconnect()
    app.close()

## The title, the next wake-up and descriptor watches are reachable before the
## window opens; the loop applies them when it runs.
pub func application_wakes() -> unit!:
    let app = Application(VStack([], spacing = 0.0), title = "First")
    app.set_title("Second — ü")
    app.wake_after(0.25)
    app.wake_at(0)
    app.wake_at(time.now() + 1000000)
    # Watching runs in the window's loop (src/watches.lucb tests it on sockets); here the surface
    # and its checks: a descriptor must be valid.
    var calls = 0
    var refused = false
    _ = app.watch(-1, func () -> unit!:
        calls += 1) catch failure:
        refused = failure.code == ui.invalid
        recover 0
    assert(refused and calls == 0)
    app.unwatch(1)
    app.close()

## A focused field takes the editing chords (select all, copy, cut, paste, undo,
## redo) before the application's commands; with no field focused they are the
## application's again.
pub func field_editing_shortcuts() -> unit!:
    let count = Count()
    let field = TextField("https://example.com", cells = 24.0)
    let other = Button("Elsewhere")
    let app = Application(VStack([field, other], spacing = 0.0))
    var commands: list[Command] = []
    var triggered: list[Connection] = []
    for key in [Key.a, Key.c, Key.x, Key.v, Key.z]:
        let command = Command("Page chord", Shortcut(key, primary = true))
        triggered.append(command.on_trigger(count.trigger))
        commands.append(command)
    let redo = Command("Page redo", Shortcut(Key.z, primary = true, shift = true))
    triggered.append(redo.on_trigger(count.trigger))
    commands.append(redo)
    app.set_commands(commands)
    render(app)
    app.focus(field.layout())
    render(app)
    let mac = platform.macos
    # Select all, then typing replaces the whole address.
    app.dispatch(Event(kind = EventKind.key_down, key = Key.a, meta = mac, control = not mac))
    app.dispatch(Event(kind = EventKind.text_input, codepoint = 120))
    assert(field.text() == "x" and count.triggers == 0)
    app.dispatch(Event(kind = EventKind.key_down, key = Key.z, meta = mac, control = not mac))
    assert(field.text() == "https://example.com")
    app.dispatch(Event(kind = EventKind.key_down, key = Key.z, meta = mac, control = not mac, shift = true))
    assert(field.text() == "x")
    for key in [Key.c, Key.x, Key.v]:
        app.dispatch(Event(kind = EventKind.key_down, key = key, meta = mac, control = not mac))
    assert(count.triggers == 0)
    # select_all from code, as an address bar does on focus.
    field.set_text("luce.luciaos.com")
    field.select_all()
    app.dispatch(Event(kind = EventKind.text_input, codepoint = 121))
    assert(field.text() == "y")
    # Focus elsewhere: the chords are the application's commands again.
    app.focus(other.layout())
    render(app)
    app.dispatch(Event(kind = EventKind.key_down, key = Key.a, meta = mac, control = not mac))
    assert(count.triggers == 1)
    for connection in triggered:
        connection.disconnect()
    app.close()

## A disabled command's shortcut doesn't take the key: the press goes on to the next
## command with the same shortcut (and, with none, to the focused widget).
pub func disabled_shortcut_passes() -> unit!:
    let count = Count()
    let app = Application(VStack([Button("Here", font = Font())], spacing = 0.0))
    let off = Command("Preview", Shortcut(Key.d), id = "preview")
    let off_triggered = off.on_trigger(count.copy)
    let on = Command("Dimension", Shortcut(Key.d), id = "dimension")
    let on_triggered = on.on_trigger(count.trigger)
    app.set_commands([off, on])
    render(app)
    app.dispatch(Event(kind = EventKind.key_down, key = Key.d))
    assert(count.copies == 1 and count.triggers == 0)
    off.set_enabled(false)
    app.dispatch(Event(kind = EventKind.key_down, key = Key.d))
    assert(count.copies == 1 and count.triggers == 1)
    on.set_enabled(false)
    app.dispatch(Event(kind = EventKind.key_down, key = Key.d))
    assert(count.copies == 1 and count.triggers == 1)
    off_triggered.disconnect()
    on_triggered.disconnect()