Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-ui

Source of release 0.19.3

0.19.3 / tests / controls / docking.luc

from luce_ui.ui import Application, DStack, Panel, DockPosition, DockRequest, TextEditor, Text, Size, Event
from luce_window.input import EventKind, Key
from luce_gpu.gpu import Rect

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))

pub func docking() -> unit!:
    let editor = TextEditor("alpha")
    let first = Panel("Alpha", editor, minimum = Size(100.0, 60.0))
    let second = Panel("Beta", TextEditor("beta"), minimum = Size(100.0, 60.0))
    let third = Panel("Output", Text("output"), minimum = Size(100.0, 60.0), closable = false)
    let stack = DStack([first, second, third])
    let app = Application(stack)
    stack.move(second, first, DockPosition.right)
    stack.move(third, second, DockPosition.bottom)
    app.layout(805.0, 505.0)
    assert(stack.panel_count() == 3 and stack.group_count() == 3)
    let a = stack.panel_bounds(first)
    let b = stack.panel_bounds(second)
    let c = stack.panel_bounds(third)
    assert(a.width == 400.0 and b.width == 400.0 and c.width == 400.0)
    assert(b.y == 0.0 and c.y == 255.0 and c.height == 250.0)
    # Divider capture and keyboard resizing use the same split geometry.
    click(app, a.x + a.width + 2.0, 100.0)
    app.dispatch(Event(kind = EventKind.key_down, key = Key.right))
    assert(stack.panel_bounds(first).width == 408.0)
    app.dispatch(Event(kind = EventKind.pointer_down, x = 410.0, y = 100.0))
    app.dispatch(Event(kind = EventKind.pointer_moved, x = 450.0, y = 100.0))
    app.dispatch(Event(kind = EventKind.pointer_up, x = 450.0, y = 100.0))
    let resized_width = stack.panel_bounds(first).width
    assert(resized_width > 447.999 and resized_width < 448.001)
    stack.set_fraction(first, 0.5)
    app.layout(805.0, 505.0)
    app.dispatch(Event(kind = EventKind.pointer_down, x = b.x + 20.0, y = 252.0))
    app.dispatch(Event(kind = EventKind.pointer_moved, x = b.x + 20.0, y = 282.0))
    app.dispatch(Event(kind = EventKind.pointer_up, x = b.x + 20.0, y = 282.0))
    assert(stack.panel_bounds(second).height == 280.0)
    stack.set_fraction(third, 0.5)
    app.layout(805.0, 505.0)
    let identity = editor.layout().id()
    editor.set_selection(5, 5)
    editor.insert(" edited")
    stack.move(second, first)
    app.layout(805.0, 505.0)
    assert(stack.same_group(first, second) and stack.group_count() == 2)
    assert(not first.layout().is_visible() and second.layout().is_visible())
    let tab = stack.tab_bounds(first)
    click(app, tab.x + 8.0, tab.y + 5.0)
    assert(first.layout().is_visible() and not second.layout().is_visible())
    assert(editor.layout().is_focused() and editor.layout().id() == identity)
    assert(editor.text() == "alpha edited")
    editor.undo()
    assert(editor.text() == "alpha")
    # The add menu lists the catalog: a pick docks a new panel as a tab of the
    # group; picking it again only selects it; a split lists the panels again
    # and docks the next pick beside the group.
    stack.set_catalog(["gamma", "delta"], ["Gamma", "Delta"])
    var requests = 0
    var positions: list[DockPosition] = []
    let added = stack.on_add(func(request: DockRequest) -> unit!:
        let made = Panel(request.id(), Text(request.id()), minimum = Size(100.0, 60.0), id = request.id())
        stack.add(made, request.panel, request.position)
        positions.append(request.position)
        requests += 1)
    let plus = stack.add_bounds(first)
    click(app, plus.x + plus.width * 0.5, plus.y + 5.0)
    app.dispatch(Event(kind = EventKind.key_down, key = Key.enter))
    app.layout(805.0, 505.0)
    let gamma = stack.find("gamma") else return
    assert(requests == 1 and positions[0] == DockPosition.tab and stack.same_group(first, gamma))
    let groups = stack.group_count()
    stack.select(first)
    app.layout(805.0, 505.0)
    let again = stack.add_bounds(first)
    click(app, again.x + again.width * 0.5, again.y + 5.0)
    app.dispatch(Event(kind = EventKind.key_down, key = Key.enter))
    app.layout(805.0, 505.0)
    assert(requests == 1 and gamma.layout().is_visible() and stack.group_count() == groups)
    let split = stack.add_bounds(first)
    click(app, split.x + split.width * 0.5, split.y + 5.0)
    app.dispatch(Event(kind = EventKind.key_down, key = Key.down))
    app.dispatch(Event(kind = EventKind.key_down, key = Key.down))
    app.dispatch(Event(kind = EventKind.key_down, key = Key.enter))
    app.dispatch(Event(kind = EventKind.key_down, key = Key.down))
    app.dispatch(Event(kind = EventKind.key_down, key = Key.enter))
    app.layout(805.0, 505.0)
    assert(requests == 2 and positions[1] == DockPosition.right and stack.group_count() == groups + 1)
    let delta = stack.find("delta") else return
    stack.remove(delta)
    stack.remove(gamma)
    assert(stack.find("gamma") == none)
    added.disconnect()
    # A refused close leaves both tab membership and content intact.
    stack.set_close_handler(func(panel: Panel) -> bool: return false)
    let closable = stack.tab_bounds(first)
    click(app, closable.x + closable.width - 5.0, closable.y + 5.0)
    assert(stack.panel_count() == 3 and editor.text() == "alpha")
    # Drag a tab into the bottom region, preserving the same editor object.
    let dragged = stack.tab_bounds(first)
    let target = stack.panel_bounds(third)
    app.dispatch(Event(kind = EventKind.pointer_down, x = dragged.x + 8.0, y = dragged.y + 5.0))
    app.dispatch(Event(kind = EventKind.pointer_moved, x = target.x + target.width * 0.5, y = target.y + target.height - 10.0))
    assert(stack.drop_preview().height > 0.0)
    app.dispatch(Event(kind = EventKind.pointer_up, x = target.x + target.width * 0.5, y = target.y + target.height - 10.0))
    assert(stack.group_count() == 3 and not stack.same_group(first, second))
    assert(editor.layout().id() == identity and editor.text() == "alpha")
    # Escape cancels the preview without committing a proposed merge.
    let origin = stack.tab_bounds(first)
    let destination = stack.panel_bounds(second)
    app.dispatch(Event(kind = EventKind.pointer_down, x = origin.x + 8.0, y = origin.y + 5.0))
    app.dispatch(Event(kind = EventKind.pointer_moved, x = destination.x + destination.width * 0.5, y = destination.y + destination.height * 0.5))
    app.dispatch(Event(kind = EventKind.key_down, key = Key.escape))
    app.dispatch(Event(kind = EventKind.pointer_up, x = destination.x + destination.width * 0.5, y = destination.y + destination.height * 0.5))
    assert(stack.group_count() == 3 and stack.drop_preview().height == 0.0)
    stack.remove(second)
    assert(stack.panel_count() == 2 and stack.group_count() == 2)
    app.layout(160.0, 100.0)
    assert(stack.panel_bounds(first).width >= 102.0)
    app.close()
    overflow()
    directional()
    floating()

## Ctrl/Cmd+Alt+arrows move to the nearest pane along an axis: with a left column
## and a stacked right column, each arrow lands on the neighbor a user would expect.
func directional() -> unit!:
    let left = Panel("Left", TextEditor("left"), minimum = Size(100.0, 60.0))
    let top = Panel("Top", TextEditor("top"), minimum = Size(100.0, 60.0))
    let bottom = Panel("Bottom", TextEditor("bottom"), minimum = Size(100.0, 60.0))
    let stack = DStack([left, top, bottom])
    let app = Application(stack)
    stack.move(top, left, DockPosition.right)
    stack.move(bottom, top, DockPosition.bottom)
    app.layout(805.0, 505.0)
    stack.select(top)
    app.layout(805.0, 505.0)
    stack.focus_direction(-1.0, 0.0)
    app.layout(805.0, 505.0)
    assert(left.layout().contains_focus() and not top.layout().contains_focus())
    stack.select(bottom)
    app.layout(805.0, 505.0)
    stack.focus_direction(0.0, -1.0)
    app.layout(805.0, 505.0)
    assert(top.layout().contains_focus())
    stack.focus_direction(0.0, 1.0)
    app.layout(805.0, 505.0)
    assert(bottom.layout().contains_focus())
    stack.focus_direction(-1.0, 0.0)
    app.layout(805.0, 505.0)
    assert(left.layout().contains_focus())
    # No pane lies further left, so focus stays put.
    stack.focus_direction(-1.0, 0.0)
    app.layout(805.0, 505.0)
    assert(left.layout().contains_focus())
    app.close()

func overflow() -> unit!:
    var panels: list[Panel] = []
    for index in 0..<10:
        panels.append(Panel("Document", TextEditor("text")))
    let stack = DStack(panels)
    let app = Application(stack)
    app.layout(340.0, 200.0)
    assert(stack.tab_bounds(panels[9]).width > 0.0)
    assert(stack.tab_bounds(panels[0]).width == 0.0)
    let plus = stack.add_bounds(panels[9])
    assert(plus.x + plus.width == 339.0)
    # The previous control reveals hidden tabs without moving the trailing +.
    click(app, 4.0, 5.0)
    assert(panels[8].layout().is_visible() and not panels[9].layout().is_visible())
    stack.select(panels[0])
    app.layout(340.0, 200.0)
    assert(stack.tab_bounds(panels[0]).width > 0.0)
    assert(stack.tab_bounds(panels[9]).width == 0.0)
    assert(stack.add_bounds(panels[0]).x == plus.x)
    app.close()

func drag(app: Application, x0: float, y0: float, x1: float, y1: float) -> unit!:
    app.dispatch(Event(kind = EventKind.pointer_down, x = x0, y = y0))
    app.dispatch(Event(kind = EventKind.pointer_moved, x = (x0 + x1) * 0.5, y = (y0 + y1) * 0.5))
    app.dispatch(Event(kind = EventKind.pointer_moved, x = x1, y = y1))
    app.dispatch(Event(kind = EventKind.pointer_up, x = x1, y = y1))

## A tab let go over a pane's middle floats in its own window; the window moves
## by its title, resizes by its corner, and docks again when let go over a pane's
## edge or pressed twice on its title. A panel not yet in the stack can float
## from the start, and a floating window's close button closes it.
func floating() -> unit!:
    let editor = TextEditor("left")
    let left = Panel("Left", editor, minimum = Size(100.0, 60.0))
    let right = Panel("Right", TextEditor("right"), minimum = Size(100.0, 60.0))
    let stack = DStack([left, right])
    let app = Application(stack)
    stack.move(right, left, DockPosition.right)
    app.layout(805.0, 505.0)
    let tab = stack.tab_bounds(right)
    # Mid-drag the left pane fills the stack, so its middle is the stack's.
    app.dispatch(Event(kind = EventKind.pointer_down, x = tab.x + 8.0, y = tab.y + 5.0))
    app.dispatch(Event(kind = EventKind.pointer_moved, x = 300.0, y = 200.0))
    app.dispatch(Event(kind = EventKind.pointer_moved, x = 402.0, y = 260.0))
    app.dispatch(Event(kind = EventKind.pointer_up, x = 402.0, y = 260.0))
    app.layout(805.0, 505.0)
    assert(stack.is_floating(right) and stack.group_count() == 1 and stack.panel_count() == 2)
    var window = stack.float_bounds(right)
    assert(window.width > 100.0 and window.height > 60.0)
    assert(right.layout().bounds().y > window.y and right.layout().bounds().x > window.x)
    # Move by the title: let go over the middle, it stays afloat.
    drag(app, window.x + 30.0, window.y + 5.0, window.x + 80.0, window.y - 35.0)
    app.layout(805.0, 505.0)
    let moved = stack.float_bounds(right)
    assert(stack.is_floating(right) and moved.x == window.x + 50.0 and moved.y == window.y - 40.0)
    # Resize by the bottom-right corner; the move left room below.
    drag(app, moved.x + moved.width - 3.0, moved.y + moved.height - 3.0, moved.x + moved.width + 27.0, moved.y + moved.height + 17.0)
    app.layout(805.0, 505.0)
    window = stack.float_bounds(right)
    assert(window.width == moved.width + 30.0 and window.height == moved.height + 20.0)
    # Clicking the docked editor under nothing floating leaves the window alone.
    assert(stack.is_floating(right))
    # Let go over the docked pane's right edge: it docks beside it.
    let target = stack.panel_bounds(left)
    drag(app, window.x + 30.0, window.y + 5.0, target.x + target.width - 10.0, target.y + target.height * 0.5)
    app.layout(805.0, 505.0)
    assert(not stack.is_floating(right) and stack.group_count() == 2 and not stack.same_group(left, right))
    assert(stack.panel_bounds(right).x > stack.panel_bounds(left).x)
    # Float by code, then a double press on the title docks it as a tab.
    stack.float(right)
    app.layout(805.0, 505.0)
    assert(stack.is_floating(right) and stack.group_count() == 1)
    window = stack.float_bounds(right)
    click(app, window.x + 30.0, window.y + 5.0)
    click(app, window.x + 30.0, window.y + 5.0)
    app.layout(805.0, 505.0)
    assert(not stack.is_floating(right) and stack.same_group(left, right))
    assert(editor.text() == "left")
    # A new panel floats from the start; its close button removes it.
    let extra = Panel("Extra", Text("extra"), minimum = Size(80.0, 40.0))
    stack.float(extra)
    app.layout(805.0, 505.0)
    assert(stack.contains(extra) and stack.is_floating(extra) and stack.panel_count() == 3)
    window = stack.float_bounds(extra)
    assert(window.x > 0.0 and window.y >= 0.0 and window.x + window.width <= 805.0)
    click(app, window.x + window.width - 8.0, window.y + 8.0)
    app.layout(805.0, 505.0)
    assert(not stack.contains(extra) and stack.panel_count() == 2)
    app.close()

## The arrangement as text: the split tree in pre-order, each group's tabs by
## id with its selected one, then the floating windows; a panel without an id
## is written as `-`.
pub func docking_layout() -> unit!:
    let first = Panel("Alpha", Text("a"), minimum = Size(100.0, 60.0), id = "alpha")
    let second = Panel("Beta", Text("b"), minimum = Size(100.0, 60.0), id = "beta")
    let third = Panel("Gamma", Text("c"), minimum = Size(100.0, 60.0), id = "gamma")
    let fourth = Panel("Delta", Text("d"), minimum = Size(100.0, 60.0))
    let stack = DStack([first, second, third])
    let app = Application(stack)
    stack.move(second, first, DockPosition.right)
    stack.move(third, second, DockPosition.bottom)
    stack.set_fraction(first, 0.25)
    stack.set_fraction(third, 0.5)
    app.layout(805.0, 505.0)
    assert(stack.layout_text() == "luce-dock 1\nsplit h 0.25\ngroup 0 alpha\nsplit v 0.5\ngroup 0 beta\ngroup 0 gamma\n")
    # Tabs in order, the selected one's place first; a floating window last.
    stack.add(fourth, first, DockPosition.tab)
    stack.select(first)
    stack.float(third, Rect(40.0, 60.0, 300.0, 200.0))
    app.layout(805.0, 505.0)
    let text = stack.layout_text()
    assert(text.starts_with("luce-dock 1\nsplit h 0.25\ngroup 0 alpha -\ngroup 0 beta\nfloat "))
    assert(text.ends_with(" gamma\n"))