Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-ui

Source of release 0.19.4

0.19.4 / tests / controls / text_views.luc

from luce_ui.ui import Application, TextEditor, Font, Event, FoldRange
from luce_window.input import EventKind, Key, ScrollUnit
from luce_gpu.gpu import Frame
from luce_window import window

pub func text_views() -> unit!:
    scrolling()
    folding()
    folded_extents()
    gutter_edits()
    wrapping()
    change_bars()

func change_bars() -> unit!:
    let font = Font()
    let editor = TextEditor("alpha\nbeta\ngamma", font = font)
    let app = Application(editor)
    app.layout(320.0, 200.0)
    # A baseline equal to the buffer shows no changes.
    editor.set_baseline("alpha\nbeta\ngamma")
    editor.set_selection(0, 0)
    assert(not editor.has_changes())
    # Editing a line diverges from the baseline.
    editor.set_selection(6, 10)
    editor.insert("BETA")
    assert(editor.has_changes())
    # Re-baselining to the current text (as a save would) clears the change state.
    editor.set_baseline(editor.text())
    editor.set_selection(0, 0)
    assert(not editor.has_changes())
    # Clearing the baseline entirely also removes any change bars.
    editor.insert("!")
    assert(editor.has_changes())
    editor.clear_baseline()
    assert(not editor.has_changes())
    app.close()

func wrapping() -> unit!:
    let font = Font()
    let editor = TextEditor("one two three four five six seven eight nine ten\nshort", font = font)
    let app = Application(editor)
    app.layout(220.0, 200.0)
    app.focus(editor.layout())
    app.layout(220.0, 200.0)
    # Off: one visual row per physical line.
    assert(editor.visible_line_count() == 2)
    editor.set_wrap(true)
    app.layout(220.0, 200.0)
    assert(editor.wrapping() and editor.visible_line_count() > 2)
    # Down from the start stays on the first physical line, on its next visual row.
    editor.set_selection(0, 0)
    app.dispatch(Event(kind = EventKind.key_down, key = Key.down))
    assert(editor.line() == 1 and editor.caret() > 0)
    # A click on the second visual row lands inside the first physical line.
    editor.set_selection(0, 0)
    app.dispatch(Event(kind = EventKind.pointer_down, x = font.advance() * 6.0, y = font.line_height() * 1.5))
    app.dispatch(Event(kind = EventKind.pointer_up, x = font.advance() * 6.0, y = font.line_height() * 1.5))
    assert(editor.line() == 1 and editor.caret() > 0)
    # Wrapping pins the horizontal offset to zero even at the far right.
    editor.set_selection(editor.length(), editor.length())
    assert(editor.scroll_offset().width == 0.0)
    # Off again: the two-row layout returns.
    editor.set_wrap(false)
    app.layout(220.0, 200.0)
    assert(not editor.wrapping() and editor.visible_line_count() == 2)
    app.close()

func gutter_edits() -> unit!:
    let font = Font()
    var text = "word"
    for index in 0..<98: text += "\n"
    let editor = TextEditor(text, font = font)
    let app = Application(editor)
    app.layout(320.0, 100.0)
    # Click the second source column across 99/100 lines, then shrink through
    # undo and replacement. Painting, selection and scroll bounds share sizing.
    source_column(app, editor, font.advance() * 6.0)
    editor.set_selection(editor.length(), editor.length())
    editor.insert("\nlast")
    source_column(app, editor, font.advance() * 7.0)
    editor.undo()
    source_column(app, editor, font.advance() * 6.0)
    editor.redo()
    source_column(app, editor, font.advance() * 7.0)
    assert(editor.set_folds([FoldRange(0, editor.length())], editor.version()))
    editor.fold_all()
    assert(editor.visible_line_count() == 1)
    source_column(app, editor, font.advance() * 9.0)
    editor.set_text("word")
    source_column(app, editor, font.advance() * 5.0)
    app.close()

func source_column(app: Application, editor: TextEditor, x: float) -> unit!:
    editor.set_selection(0, 0)
    app.dispatch(Event(kind = EventKind.pointer_down, x = x, y = 8.0))
    app.dispatch(Event(kind = EventKind.pointer_up, x = x, y = 8.0))
    assert(editor.column() == 2 and editor.line() == 1)

func scrolling() -> unit!:
    let font = Font()
    var text = ""
    for index in 0..<160:
        text += "é"
    text += "\n\t😀\nend"
    let editor = TextEditor(text, font = font)
    let app = Application(editor)
    app.layout(320.0, 100.0)
    app.dispatch(Event(kind = EventKind.scroll, x = 100.0, y = 40.0, scroll_x = -40.0, scroll_unit = ScrollUnit.points))
    assert(editor.scroll_offset().width == 40.0)
    app.dispatch(Event(kind = EventKind.scroll, x = 100.0, y = 40.0, scroll_x = 10.0, scroll_unit = ScrollUnit.points))
    assert(editor.scroll_offset().width == 30.0)
    app.dispatch(Event(kind = EventKind.scroll, x = 100.0, y = 40.0, scroll_x = -100000.0, scroll_y = -100000.0, scroll_unit = ScrollUnit.points))
    let limit = editor.scroll_offset().width
    assert(limit > 500.0 and limit < font.advance() * 161.0 and editor.scroll_offset().height == 0.0)
    app.dispatch(Event(kind = EventKind.scroll, x = 100.0, y = 40.0, scroll_x = -1000.0, scroll_unit = ScrollUnit.points))
    assert(editor.scroll_offset().width == limit)
    app.layout(2000.0, 100.0)
    assert(editor.scroll_offset().width == 0.0)
    app.layout(320.0, 100.0)
    editor.set_selection(0, 160)
    editor.insert("short")
    assert(editor.scroll_offset().width == 0.0)
    editor.undo()
    app.dispatch(Event(kind = EventKind.scroll, x = 100.0, y = 40.0, scroll_x = -2.0, scroll_unit = ScrollUnit.lines))
    assert(editor.scroll_offset().width > 0.0)
    editor.set_text("small")
    assert(editor.scroll_offset().width == 0.0)
    let frame = Frame(window.Size(320.0, 100.0, 320.0, 100.0, 1.0))
    app.render(frame.target())
    frame.close()

func folded_extents() -> unit!:
    var body = ""
    for row in 0..<60:
        body += "    "
        for column in 0..<120:
            body += "é"
        body += "\n"
    let editor = TextEditor("block:\n" + body + "after")
    let app = Application(editor)
    app.layout(320.0, 100.0)
    assert(editor.set_folds([FoldRange(0, 7 + body.length)], editor.version()))
    app.dispatch(Event(kind = EventKind.scroll, x = 100.0, y = 40.0, scroll_x = -100000.0, scroll_y = -100000.0, scroll_unit = ScrollUnit.points))
    assert(editor.scroll_offset().width > 0.0 and editor.scroll_offset().height > 0.0)
    editor.fold_all()
    assert(editor.visible_line_count() == 2)
    assert(editor.scroll_offset().width == 0.0 and editor.scroll_offset().height == 0.0)
    editor.unfold_all()
    app.dispatch(Event(kind = EventKind.scroll, x = 100.0, y = 40.0, scroll_x = -100000.0, scroll_y = -100000.0, scroll_unit = ScrollUnit.points))
    assert(editor.scroll_offset().width > 0.0 and editor.scroll_offset().height > 0.0)
    editor.set_selection(7, 7 + body.length)
    editor.insert("")
    assert(editor.visible_line_count() == 2)
    assert(editor.scroll_offset().width == 0.0 and editor.scroll_offset().height == 0.0)

func folding() -> unit!:
    let font = Font()
    let source = "outer:\n    inner:\n        text\n    last\nafter"
    let editor = TextEditor(source, font = font)
    let app = Application(editor)
    app.layout(400.0, 100.0)
    app.focus(editor.layout())
    app.layout(400.0, 100.0)
    let inner = 7
    let last = 31
    let after = 40
    assert(editor.set_folds([FoldRange(0, after), FoldRange(inner, last)], editor.version()))
    let revision = editor.version()
    assert(editor.set_folded(2, true) and editor.visible_line_count() == 4)
    assert(editor.set_folded(1, true) and editor.visible_line_count() == 2)
    assert(editor.text() == source and editor.version() == revision)
    assert(editor.set_folded(1, false) and editor.is_folded(2))
    assert(editor.visible_line_count() == 4)
    _ = editor.set_folded(1, true)
    editor.set_selection(0, 0)
    app.dispatch(Event(kind = EventKind.key_down, key = Key.down))
    assert(editor.line() == 5 and editor.is_folded(1))
    app.dispatch(Event(kind = EventKind.key_down, key = Key.up))
    assert(editor.line() == 1 and editor.is_folded(1))
    editor.set_selection(26, 26)
    assert(not editor.is_folded(1) and not editor.is_folded(2))
    editor.fold_all()
    assert(editor.line() == 1 and editor.visible_line_count() == 2)
    editor.unfold_all()
    assert(editor.visible_line_count() == 5)
    # Gutter toggles use the same visible rows as mouse selection and drawing.
    app.dispatch(Event(kind = EventKind.pointer_down, x = font.advance() * 4.0, y = 8.0))
    app.dispatch(Event(kind = EventKind.pointer_up, x = font.advance() * 4.0, y = 8.0))
    assert(editor.is_folded(1))
    editor.set_selection(6, 6)
    app.dispatch(Event(kind = EventKind.key_down, key = Key.delete))
    assert(not editor.is_folded(1) and editor.text() == source)
    _ = editor.set_folded(1, true)
    editor.set_selection(after, after)
    app.dispatch(Event(kind = EventKind.key_down, key = Key.backspace))
    assert(not editor.is_folded(1) and editor.text() == source)
    _ = editor.set_folded(1, true)
    editor.set_selection(0, editor.length())
    assert(editor.selected_text() == source)
    editor.insert("replacement")
    assert(editor.text() == "replacement" and editor.visible_line_count() == 1)
    editor.undo()
    assert(editor.text() == source and editor.visible_line_count() == 5)
    var invalid = false
    let accepted = editor.set_folds([FoldRange(0, last), FoldRange(inner, after)], editor.version()) catch failure:
        invalid = true
        recover false
    assert(invalid and not accepted)
    assert(not editor.set_folds([FoldRange(-1, 900)], revision))
    editor.set_text("head:\n    last")
    _ = editor.set_folds([FoldRange(0, editor.length())], editor.version())
    editor.fold_all()
    editor.set_selection(editor.length(), editor.length())
    assert(not editor.is_folded(1))
    let frame = Frame(window.Size(400.0, 100.0, 400.0, 100.0, 1.0))
    editor.fold_all()
    app.render(frame.target())
    frame.close()