dymokomi/luce-ui
Source of release 0.19.3
0.19.3 / tests / controls / main.luc
from luce_ui.ui import Application, Font, Button, Text, TextEditor, ListView, Highlight, VStack, Spacer, Widget, Layout, Size, Constraints, Event, Painter
from luce_gpu.gpu import Frame, RenderTarget, Rect, Color
from luce_window import window
from luce_std import math
from luce_ui import ui
from interop import Connection
from luce_window.input import EventKind, Key, ScrollUnit
from luce_ui.ui import Command, Shortcut, Toolbar, Menu, Theme
from text_views import text_views
from wide_text import wide_text
from panes import panes
from contextual import contextual
from hover import hover
from buttons import buttons
from drops import drops
from luce_ui.ui import ListItem, ScrollView
from headers import headers
from docking import docking, docking_layout
from cursors import cursors
from clicks import clicks
from crash_hooks import crash_hooks
from tables import tables
from dialogs import dialogs, dialog_keys, number_submit, number_edit, field_leaves, field_command_keys, field_submits, application_wakes, field_editing_shortcuts, disabled_shortcut_passes
from scene_views import scene_views
from parameters import parameters
from ramps import ramps
from diagnostics import diagnostics
from completion import completion, hover_text
from appearance import appearance_follows
class Custom: Widget:
let box: Layout
var events: int = 0
var fail: bool = false
pub func init(self) -> unit!:
self.box = Layout()
pub func layout(self) -> Layout:
return self.box
pub func measure(self, available: Constraints) -> Size!:
return Size(40.0, 20.0)
pub func draw(self, target: RenderTarget) -> unit!:
if self.fail:
error(ui.invalid, f"custom widget failure {self.events}")
let painter = Painter(target)
painter.rectangle(Rect(0.0, 0.0, 20.0, 20.0), Color(1.0, 0.0, 0.0))
pub func event(self, event: Event) -> unit!:
self.events += 1
class Counter:
var count: int = 0
pub func add(self):
self.count += 1
func raster_surface() -> unit!:
let raster = ui.Raster(2, 2)
assert(raster.width() == 2 and raster.height() == 2)
raster.dab(0.5, 0.5, 1.0, 1.0, Color(1.0, 0.0, 0.0))
let copied = raster.copy()
let grown = raster.resized(3, 3)
raster.clear()
raster.composite(copied)
assert(raster.alpha_at(0, 0) == 1.0 and grown.alpha_at(2, 2) == 0.0)
raster.dab(0.5, 0.5, 1.0, 1.0, Color(), erase = true)
raster.composite(copied, opacity = 0.5)
assert(raster.alpha_at(0, 0) == 0.5)
let frame = Frame(window.Size(20.0, 20.0, 20.0, 20.0, 1.0))
raster.draw(frame.target(), Rect(0.0, 0.0, 20.0, 20.0))
frame.close()
raster.close()
copied.close()
grown.close()
## Compact defaults reach automatic fonts, layout and hit targets; larger user
## fonts still fit their containing controls.
func compact_layout() -> unit!:
let font = Font()
assert(font.size() == 12.0)
let button = Button("Save")
let larger = Button("Save", font = Font(14.0))
assert(button.measure(Constraints()).width < larger.measure(Constraints()).width)
assert(button.measure(Constraints()).height < larger.measure(Constraints()).height)
let small = ui.Checkbox("Small", font = Font(8.0))
assert(small.measure(Constraints()).height >= 16.0)
let first = Text("First")
let second = Text("Second")
let tall = Button("Large", font = Font(24.0))
let shelf = ui.Shelf([tall])
let content = Text("Dialog content")
let dialog = ui.Dialog([content])
let stack = VStack([first, second, button, small, shelf, dialog])
let app = Application(stack)
app.layout(640.0, 480.0)
let a = first.layout().bounds()
let b = second.layout().bounds()
assert(b.y - a.y - a.height == ui.default_spacing)
assert(shelf.layout().bounds().height >= tall.layout().bounds().height)
let counter = Counter()
let connection = button.on_click(counter.add)
let bounds = button.layout().bounds()
let x = bounds.x + bounds.width * 0.5
let y = bounds.y + bounds.height * 0.5
app.dispatch(Event(kind = EventKind.pointer_down, x = x, y = y))
app.dispatch(Event(kind = EventKind.pointer_up, x = x, y = y))
assert(counter.count == 1)
dialog.open()
app.layout(640.0, 480.0)
assert(content.layout().bounds().x - dialog.layout().bounds().x == ui.default_padding)
assert(content.layout().bounds().y - dialog.layout().bounds().y == ui.default_padding)
connection.disconnect()
app.close()
pub func main(arguments: list[str]) -> int!:
compact_layout()
raster_surface()
contextual()
hover()
buttons()
drops()
headers()
docking()
docking_layout()
cursors()
clicks()
tables()
dialogs()
disabled_shortcut_passes()
dialog_keys()
number_submit()
number_edit()
field_leaves()
field_command_keys()
field_submits()
application_wakes()
field_editing_shortcuts()
scrolling()
parameters()
ramps()
crash_hooks()
appearance_follows()
let font = Font(size = 14.0)
assert(font.size() == 14.0 and font.measure("ii") == font.measure("WW"))
let button = Button("pause", font = font)
let counter = Counter()
let connection = button.on_click(counter.add)
button.click()
assert(counter.count == 1)
connection.disconnect()
button.click()
assert(counter.count == 1)
let custom = Custom()
let stack = VStack([button, Text("Mixed case"), custom, Spacer()])
assert(stack.layout().child_count() == 4)
let app = Application(stack)
app.layout(240.0, 180.0)
let clicked = button.on_click(counter.add)
app.dispatch(Event(kind = EventKind.pointer_down, x = 20.0, y = 20.0))
app.dispatch(Event(kind = EventKind.pointer_up, x = 20.0, y = 20.0))
assert(counter.count == 2)
app.dispatch(Event(kind = EventKind.key_down, key = Key.space))
app.dispatch(Event(kind = EventKind.key_up, key = Key.space))
assert(counter.count == 3)
app.dispatch(Event(kind = EventKind.pointer_down, x = 20.0, y = 20.0))
app.dispatch(Event(kind = EventKind.pointer_up, x = 500.0, y = 20.0))
assert(counter.count == 3)
clicked.disconnect()
let widget: Widget = custom
let frame = Frame(window.Size(100.0, 100.0, 100.0, 100.0, 1.0))
let target = frame.target()
custom.fail = true
var rejected = false
app.render(target) catch failure:
assert(failure.code == ui.invalid and failure.message == f"custom widget failure {custom.events}")
rejected = true
assert(rejected)
custom.fail = false
widget.draw(target)
app.render(target)
frame.close()
text_views()
diagnostics()
completion()
hover_text()
wide_text()
panes()
scene_views()
commands_and_themes()
menu_wheel()
cyclic_editor()
editor_controls()
early_selection()
mutations()
signals()
print("ok Luce controls and custom widget")
return 0
class Removal:
let stack: VStack
let replacement: Text
var count: int = 0
pub func init(self, stack: VStack, replacement: Text):
self.stack = stack
self.replacement = replacement
pub func remove(self) -> unit!:
self.count += 1
self.stack.set_children([self.replacement])
func mutations() -> unit!:
let button = Button("Remove me")
let label = Text("Removed")
let stack = VStack([button])
let removal = Removal(stack, label)
let connection = button.on_click(removal.remove)
let app = Application(stack)
app.layout(200.0, 100.0)
app.dispatch(Event(kind = EventKind.pointer_down, x = 10.0, y = 10.0))
app.dispatch(Event(kind = EventKind.pointer_up, x = 10.0, y = 10.0))
assert(removal.count == 1 and not button.layout().is_focused())
app.dispatch(Event(kind = EventKind.key_down, key = Key.space))
app.dispatch(Event(kind = EventKind.key_up, key = Key.space))
assert(removal.count == 1)
connection.disconnect()
stack.set_children([button])
app.layout(200.0, 100.0)
app.dispatch(Event(kind = EventKind.pointer_down, x = 10.0, y = 10.0))
stack.layout().set_enabled(false)
app.dispatch(Event(kind = EventKind.pointer_up, x = 10.0, y = 10.0))
assert(not button.layout().is_focused())
stack.layout().set_enabled(true)
app.dispatch(Event(kind = EventKind.key_down, key = Key.tab))
assert(button.layout().is_focused())
app.close()
var closed = false
app.layout(200.0, 100.0) catch failure:
closed = true
assert(closed)
class Reentrant:
let button: Button
var later: Connection? = none
var first: int = 0
var second: int = 0
pub func init(self, button: Button):
self.button = button
pub func disconnect_next(self) -> unit!:
self.first += 1
if let later = self.later:
later.disconnect()
pub func next(self) -> unit!:
self.second += 1
pub func fail(self) -> unit!:
error(ui.invalid, f"signal failed after {self.first}")
func signals() -> unit!:
let button = Button("Signal")
let state = Reentrant(button)
let first = button.on_click(state.disconnect_next)
state.later = button.on_click(state.next)
button.click()
assert(state.first == 1 and state.second == 0)
first.disconnect()
let failure = button.on_click(state.fail)
var rejected = false
button.click() catch problem:
assert(problem.code == ui.invalid and problem.message == "signal failed after 1")
rejected = true
assert(rejected)
failure.disconnect()
func early_selection() -> unit!:
let listing = ListView([ListItem("only")])
listing.select(0)
let app = Application(VStack([listing]))
app.layout(200.0, 100.0)
assert(listing.scroll_offset() == 0.0)
app.close()
func editor_controls() -> unit!:
let editor = TextEditor("héllo")
let listing = ListView([ListItem("first"), ListItem("second")])
let root = VStack([listing, editor])
let app = Application(root)
app.focus(editor.layout())
app.layout(400.0, 300.0)
assert(editor.layout().is_focused())
editor.set_selection(5, 5)
app.dispatch(Event(kind = EventKind.text_input, codepoint = 128512))
assert(editor.text() == "héllo😀" and editor.caret() == 6)
app.dispatch(Event(kind = EventKind.key_down, key = Key.backspace))
assert(editor.text() == "héllo")
editor.undo()
assert(editor.text() == "héllo😀")
editor.set_highlights([Highlight(0, 5, Color(0.5, 0.2, 0.3))])
var invalid = false
editor.set_highlights([Highlight(0, 99, Color())]) catch failure:
invalid = true
assert(invalid)
let frame = Frame(window.Size(400.0, 300.0, 400.0, 300.0, 1.0))
app.render(frame.target())
frame.close()
app.dispatch(Event(kind = EventKind.key_down, key = Key.tab))
assert(editor.layout().is_focused())
app.dispatch(Event(kind = EventKind.key_down, key = Key.tab, control = true))
assert(listing.layout().is_focused())
app.dispatch(Event(kind = EventKind.key_down, key = Key.down))
assert(listing.selected_index() == 0)
listing.set_items([ListItem("replacement")])
assert(listing.selected_index() == -1)
class EditorSession:
let app: Application
let editor: TextEditor
pub func init(self, app: Application, editor: TextEditor):
self.app = app
self.editor = editor
pub func can_close(self) -> bool:
return self.editor.length() == 0
func cyclic_editor() -> unit!:
let editor = TextEditor("unsaved")
let app = Application(editor)
let session = EditorSession(app, editor)
app.set_close_handler(session.can_close)
app.layout(300.0, 200.0)
func commands_and_themes() -> unit!:
let font = Font()
let counter = Counter()
let save = Command("Save", Shortcut(Key.s, primary = true))
let disabled = Command("Disabled")
disabled.set_enabled(false)
let invoked = save.on_trigger(counter.add)
let button = Button(action = save, font = font)
let menu = Menu("File", [disabled, save], font = font)
let bar = Toolbar([menu, button, Spacer()], font = font)
let editor = TextEditor("let text = 1", font = font)
let content = VStack([bar, editor], spacing = 0.0)
let app = Application(content)
app.set_commands([save])
app.layout(400.0, 300.0)
assert(bar.layout().bounds().height == font.line_height())
assert(button.layout().bounds().height == font.line_height())
app.focus(editor.layout())
app.layout(400.0, 300.0)
app.dispatch(Event(kind = EventKind.key_down, key = Key.s, control = true))
assert(counter.count == 1 and editor.text() == "let text = 1")
app.dispatch(Event(kind = EventKind.key_down, key = Key.s, control = true, repeated = true))
assert(counter.count == 1)
save.set_enabled(false)
button.click()
app.dispatch(Event(kind = EventKind.key_down, key = Key.s, meta = true))
assert(counter.count == 1)
save.set_enabled(true)
button.click()
assert(counter.count == 2)
# A large application registers hundreds of commands; the save shortcut still finds its own.
var many: list[Command] = []
var index = 0
while index < 300:
many.append(Command(f"Command {index}", id = f"command_{index}"))
index += 1
many.append(save)
app.set_commands(many)
app.dispatch(Event(kind = EventKind.key_down, key = Key.s, control = true))
assert(counter.count == 3)
app.set_commands([save])
counter.count = 2
menu.open()
app.layout(400.0, 300.0)
let popup = menu.layout().child(0).layout()
# The popup insets its rows from the frame by a vertical margin on each side.
let menu_pad = math.ceil(font.advance() * 0.6)
assert(popup.is_focused() and popup.bounds().y >= bar.layout().bounds().height)
assert(popup.bounds().height == font.line_height() * 2.0 + menu_pad * 2.0)
app.dispatch(Event(kind = EventKind.key_down, key = Key.enter))
assert(counter.count == 3 and not menu.is_open() and editor.layout().is_focused())
menu.open()
app.layout(400.0, 300.0)
app.dispatch(Event(kind = EventKind.key_down, key = Key.escape))
assert(not menu.is_open() and editor.layout().is_focused())
menu.open()
app.layout(400.0, 300.0)
app.dispatch(Event(kind = EventKind.pointer_down, x = 390.0, y = 290.0))
app.dispatch(Event(kind = EventKind.pointer_up, x = 390.0, y = 290.0))
assert(not menu.is_open() and counter.count == 3)
menu.open()
app.layout(400.0, 300.0)
let item_y = popup.bounds().y + menu_pad + font.line_height() * 1.5
app.dispatch(Event(kind = EventKind.pointer_down, x = 10.0, y = item_y))
app.dispatch(Event(kind = EventKind.pointer_up, x = 10.0, y = item_y))
assert(counter.count == 4 and not menu.is_open())
menu.open()
app.dispatch(Event(kind = EventKind.key_down, key = Key.s, control = true))
assert(counter.count == 4 and menu.is_open())
app.dispatch(Event(cancelled = true))
assert(not menu.is_open())
let spacious = Theme(control_lines = 2.0, inset_cells = 2.0)
app.set_theme(spacious)
app.layout(400.0, 300.0)
assert(bar.layout().bounds().height == font.line_height() * 2.0)
assert(button.layout().theme().inset_cells == 2.0)
button.layout().set_theme(Theme())
app.layout(400.0, 300.0)
assert(button.layout().bounds().height == font.line_height())
button.layout().inherit_theme()
app.layout(400.0, 300.0)
assert(button.layout().bounds().height == font.line_height() * 2.0)
let late = Button("Added later", font = font)
bar.set_children([menu, button, late])
app.layout(400.0, 300.0)
assert(late.layout().theme().inset_cells == 2.0)
let frame = Frame(window.Size(400.0, 300.0, 400.0, 300.0, 1.0))
menu.open()
app.render(frame.target())
frame.close()
invoked.disconnect()
## A column taller than its view scrolls under the wheel, over anything in it;
## a list inside keeps the wheel for itself.
## A menu taller than the window scrolls with the wheel, and the row under the
## pointer is the one Enter triggers.
func menu_wheel() -> unit!:
let font = Font()
let counter = Counter()
var commands: list[Command] = []
var index = 0
while index < 60:
commands.append(Command(f"Family {index}", id = f"family_{index}"))
index += 1
let seventh = commands[6].on_trigger(counter.add)
let menu = Menu("Family", commands, font = font)
let app = Application(VStack([menu, Spacer()], spacing = 0.0))
app.layout(300.0, 200.0)
menu.open()
app.layout(300.0, 200.0)
let popup = menu.layout().child(0).layout().bounds()
let row_y = popup.y + math.ceil(font.advance() * 0.6) + font.line_height() * 0.5
# Two notches down: six rows past the top, the pointer on the first row shown.
app.dispatch(Event(kind = EventKind.scroll, x = popup.x + 4.0, y = row_y, scroll_y = -1.0, scroll_unit = ScrollUnit.lines))
app.dispatch(Event(kind = EventKind.scroll, x = popup.x + 4.0, y = row_y, scroll_y = -1.0, scroll_unit = ScrollUnit.lines))
app.dispatch(Event(kind = EventKind.key_down, key = Key.enter))
assert(counter.count == 1 and not menu.is_open())
# Never past the last row, however far the wheel turns: it sits at the bottom.
let last = Counter()
let final = commands[59].on_trigger(last.add)
menu.open()
app.layout(300.0, 200.0)
let pad = math.ceil(font.advance() * 0.6)
let shown = math.floor((popup.height - pad * 2.0) / font.line_height())
let bottom_y = popup.y + pad + font.line_height() * (shown - 0.5)
index = 0
while index < 40:
app.dispatch(Event(kind = EventKind.scroll, x = popup.x + 4.0, y = bottom_y, scroll_y = -1.0, scroll_unit = ScrollUnit.lines))
index += 1
app.dispatch(Event(kind = EventKind.key_down, key = Key.enter))
assert(last.count == 1 and counter.count == 1)
# A menu opens on its checked command, scrolled into view.
let current = Counter()
let fortieth = commands[40].on_trigger(current.add)
commands[40].set_checked(true)
menu.open()
let frame = Frame(window.Size(300.0, 200.0, 300.0, 200.0, 1.0))
app.layout(300.0, 200.0)
app.render(frame.target())
frame.close()
app.dispatch(Event(kind = EventKind.key_down, key = Key.enter))
assert(current.count == 1)
# Even when the pointer rests on a row before the popup is first drawn,
# the list is scrolled to the checked row: the top row is not Family 0.
let top = Counter()
let zeroth = commands[0].on_trigger(top.add)
menu.open()
app.layout(300.0, 200.0)
app.dispatch(Event(kind = EventKind.pointer_moved, x = popup.x + 4.0, y = row_y))
app.dispatch(Event(kind = EventKind.key_down, key = Key.enter))
assert(top.count == 0 and not menu.is_open())
zeroth.disconnect()
fortieth.disconnect()
seventh.disconnect()
final.disconnect()
func scrolling() -> unit!:
var rows: list[Widget] = []
var index = 0
while index < 12:
rows.append(Button(f"Row {index}"))
index += 1
let column = VStack(rows, spacing = 10.0)
let view = ScrollView(column)
let app = Application(view)
app.layout(200.0, 120.0)
let first = rows[0].layout().bounds().y
assert(view.offset() == 0.0)
# The wheel over the second row scrolls the view, not the button.
let over = rows[1].layout().bounds()
app.dispatch(Event(kind = EventKind.scroll, x = over.x + 4.0, y = over.y + 4.0, scroll_y = -2.0, scroll_unit = ScrollUnit.lines))
app.layout(200.0, 120.0)
assert(view.offset() == 48.0 and rows[0].layout().bounds().y == first - 48.0)
# Never past either end.
view.scroll_to(100000.0)
app.layout(200.0, 120.0)
let bottom = rows[11].layout().bounds()
assert(bottom.y + bottom.height <= 120.0 + 0.001 and bottom.y + bottom.height >= 110.0)
view.scroll_to(-50.0)
assert(view.offset() == 0.0)