dymokomi/luce-ui
Source of release 0.19.2
0.19.2 / tests / controls / hover.luc
from luce_ui.ui import Application, Command, Menu, Toolbar, Button, TextEditor, Pane, VStack, SplitView, CommandPalette, Event, Font
from luce_window.input import EventKind, Key
pub func hover() -> unit!:
let font = Font()
let first = Menu("File", [Command("Save")], font = font)
let second = Menu("Edit", [Command("Copy")], font = font)
let disabled = Menu("Disabled", [Command("Unavailable")], font = font)
disabled.layout().set_enabled(false)
let third = Menu("View", [Command("Panels")], font = font)
let bar = Toolbar([first, second, disabled, third], font = font)
let editor = TextEditor("one\ntwo\nthree", font = font)
let left = Pane(editor)
let other = Button("Other pane", font = font)
let right = Pane(other)
let split = SplitView(left, right)
let palette = CommandPalette([Command("Find")], font = font)
let root = VStack([bar, split, palette], spacing = 0.0)
let app = Application(root)
app.layout(800.0, 360.0)
app.focus(editor.layout())
app.layout(800.0, 360.0)
let edit = second.layout().bounds()
app.dispatch(Event(kind = EventKind.pointer_moved, x = edit.x + 2.0, y = edit.y + 2.0))
assert(second.layout().is_hovered() and bar.layout().contains_pointer())
assert(not second.is_open() and editor.layout().is_focused())
first.open()
app.layout(800.0, 360.0)
app.dispatch(Event(kind = EventKind.pointer_moved, x = edit.x + 2.0, y = edit.y + 2.0))
assert(second.is_open() and not first.is_open())
let popup = second.layout().child(0).layout().bounds()
app.dispatch(Event(kind = EventKind.pointer_moved, x = popup.x + 2.0, y = popup.y + 2.0))
assert(second.is_open() and not left.layout().contains_pointer())
app.dispatch(Event(kind = EventKind.key_down, key = Key.right))
assert(third.is_open() and not second.is_open() and not disabled.is_open())
app.dispatch(Event(kind = EventKind.key_down, key = Key.left))
assert(second.is_open())
app.dispatch(Event(kind = EventKind.key_down, key = Key.escape))
assert(not second.is_open() and second.layout().is_focused())
# Hover never opens a menu after the session has ended.
app.dispatch(Event(kind = EventKind.pointer_moved, x = 2.0, y = 2.0))
assert(not first.is_open())
# Other modal surfaces cannot turn a pass over the toolbar into a menu.
palette.open()
app.layout(800.0, 360.0)
app.dispatch(Event(kind = EventKind.pointer_moved, x = edit.x + 2.0, y = edit.y + 2.0))
assert(palette.is_open() and not second.is_open() and not bar.layout().contains_pointer())
app.dispatch(Event(kind = EventKind.key_down, key = Key.escape))
let inside = editor.layout().bounds()
app.dispatch(Event(kind = EventKind.pointer_entered, x = inside.x + 12.0, y = inside.y + 12.0))
assert(editor.layout().is_hovered() and left.layout().contains_pointer() and not right.layout().contains_pointer())
assert(editor.layout().pointer_position().x == 12.0)
app.dispatch(Event(kind = EventKind.pointer_left))
assert(not editor.layout().is_hovered() and not root.layout().contains_pointer())
let button = other.layout().bounds()
app.dispatch(Event(kind = EventKind.pointer_down, x = button.x + 2.0, y = button.y + 2.0))
app.dispatch(Event(kind = EventKind.pointer_moved, x = inside.x + 12.0, y = inside.y + 12.0))
assert(left.layout().contains_pointer() and not right.layout().contains_pointer() and other.layout().is_focused())
app.dispatch(Event(kind = EventKind.pointer_up, x = inside.x + 12.0, y = inside.y + 12.0))
# A stationary pointer follows replacement geometry, not detached widgets.
let replacement = TextEditor("replacement", font = font)
left.layout().set_children([replacement])
app.layout(800.0, 360.0)
assert(not editor.layout().contains_pointer() and replacement.layout().is_hovered())
app.dispatch(Event(cancelled = true))
assert(not root.layout().contains_pointer())
let divider = split.layout().child(1).layout()
let handle = divider.bounds()
app.dispatch(Event(kind = EventKind.pointer_moved, x = handle.x + 2.0, y = handle.y + 12.0))
assert(divider.is_hovered() and not left.layout().contains_pointer() and not right.layout().contains_pointer())
app.close()