Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-ui

Source of release 0.19.1

0.19.1 / tests / controls / cursors.luc

from luce_ui.ui import Application, Command, Button, Cursor, CursorSource, DStack, Panel, Point, Size, TextEditor, Event, DockPosition
from luce_window.input import EventKind, Key

class Regions: CursorSource:
    pub func cursor(self, position: Point, captured: bool) -> Cursor:
        return Cursor.grabbing if captured else (Cursor.pointer if position.x < 30.0 else Cursor.text)

pub func cursors() -> unit!:
    let action = Command("click")
    let button = Button(action = action)
    let app = Application(button)
    app.layout(240.0, 80.0)
    app.dispatch(Event(kind = EventKind.pointer_moved, x = 10.0, y = 5.0))
    assert(app.cursor() == Cursor.pointer)
    action.set_enabled(false)
    assert(app.cursor() == Cursor.arrow)
    action.set_enabled(true)
    button.layout().set_cursor_source(Regions())
    assert(app.cursor() == Cursor.pointer)
    app.dispatch(Event(kind = EventKind.pointer_moved, x = 35.0, y = 5.0))
    assert(app.cursor() == Cursor.text)
    app.dispatch(Event(kind = EventKind.pointer_down, x = 35.0, y = 5.0))
    app.dispatch(Event(kind = EventKind.pointer_moved, x = -50.0, y = 5.0))
    assert(app.cursor() == Cursor.grabbing)
    app.dispatch(Event(cancelled = true))
    assert(app.cursor() == Cursor.arrow)
    app.close()
    detached_drag()

func detached_drag() -> unit!:
    let text = TextEditor("keep this editor")
    text.set_selection(3, 7)
    let first = Panel("First", text, minimum = Size(100.0, 60.0))
    let second = Panel("Second", TextEditor("second"), minimum = Size(100.0, 60.0))
    let stack = DStack([first, second])
    stack.move(second, first, DockPosition.right)
    let app = Application(stack)
    app.layout(805.0, 505.0)
    app.dispatch(Event(kind = EventKind.pointer_moved, x = 100.0, y = 100.0))
    assert(app.cursor() == Cursor.text)
    app.dispatch(Event(kind = EventKind.pointer_moved, x = 402.0, y = 100.0))
    assert(app.cursor() == Cursor.resize_horizontal)
    app.dispatch(Event(kind = EventKind.pointer_down, x = 402.0, y = 100.0))
    app.dispatch(Event(kind = EventKind.pointer_moved, x = 500.0, y = 100.0))
    assert(app.cursor() == Cursor.resize_horizontal)
    app.dispatch(Event(kind = EventKind.pointer_up, x = 500.0, y = 100.0))
    stack.set_fraction(first, 0.5)
    app.layout(805.0, 505.0)
    app.dispatch(Event(kind = EventKind.pointer_moved, x = 12.0, y = 5.0))
    assert(app.cursor() == Cursor.grab)
    app.dispatch(Event(kind = EventKind.pointer_down, x = 12.0, y = 5.0))
    app.dispatch(Event(kind = EventKind.pointer_moved, x = 20.0, y = 100.0))
    assert(app.cursor() == Cursor.grabbing)
    assert(stack.panel_count() == 2 and not first.layout().is_visible())
    assert(stack.tab_bounds(first).width == 0.0 and stack.panel_bounds(first).width == 0.0)
    assert(stack.panel_bounds(second).x == 0.0 and stack.panel_bounds(second).width == 805.0)
    assert(stack.drag_bounds().width > 0.0)
    assert(stack.drop_preview().x == 0.0 and stack.drop_preview().width == 400.0)
    app.layout(1005.0, 505.0)
    assert(stack.panel_bounds(second).width == 1005.0 and stack.drop_preview().width == 500.0)
    app.layout(805.0, 505.0)
    app.dispatch(Event(kind = EventKind.pointer_moved, x = -20.0, y = 100.0))
    # Let go outside, the tab would float: still a grab, with no dock target.
    assert(app.cursor() == Cursor.grabbing and stack.drop_preview().width == 0.0)
    app.dispatch(Event(kind = EventKind.key_down, key = Key.escape))
    assert(first.layout().is_visible() and stack.drag_bounds().width == 0.0)
    assert(stack.panel_bounds(second).x == 405.0 and text.selected_text() == "p th")
    app.dispatch(Event(kind = EventKind.pointer_up, x = -20.0, y = 100.0))
    # The release lies on the expanded panel's header, where the dragged source
    # used to be. The detached layout makes this a merge, not a self-drop.
    app.dispatch(Event(kind = EventKind.pointer_down, x = 12.0, y = 5.0))
    app.dispatch(Event(kind = EventKind.pointer_moved, x = 300.0, y = 10.0))
    app.dispatch(Event(kind = EventKind.pointer_up, x = 300.0, y = 10.0))
    assert(stack.same_group(first, second) and stack.group_count() == 1)
    assert(text.selected_text() == "p th" and text.layout().is_focused())
    stack.remove(second)
    app.layout(805.0, 505.0)
    app.dispatch(Event(kind = EventKind.pointer_down, x = 12.0, y = 5.0))
    app.dispatch(Event(kind = EventKind.pointer_moved, x = 300.0, y = 200.0))
    assert(not first.layout().is_visible() and app.cursor() == Cursor.grabbing)
    assert(stack.drop_preview().width == 805.0)
    app.dispatch(Event(kind = EventKind.pointer_up, x = 300.0, y = 200.0))
    assert(stack.panel_count() == 1 and first.layout().is_visible())
    app.dispatch(Event(kind = EventKind.pointer_down, x = 12.0, y = 5.0))
    app.dispatch(Event(kind = EventKind.pointer_moved, x = 300.0, y = 200.0))
    app.dispatch(Event(cancelled = true))
    assert(first.layout().is_visible() and stack.drag_bounds().width == 0.0)
    assert(app.cursor() == Cursor.arrow)
    app.close()