Switch from Asuka to MyGo, keeping the API client and the feature set: - servers: saved servers, passwordless and password login, proxy, self-signed certificates, per-server device tokens (Keystore), connect form, instance list sheet with switch/disconnect - sessions: virtualized list, search, new session, rename/archive/delete, running indicator, event stream keeps it live - chat: pushed window with system back bar, streaming Markdown via ui/markdown, reasoning and tool rows with image previews, footer menu (read aloud via a Kotlin TTS plugin, copy, fork), user message menu (copy, revert, fork), context usage ring and detail sheet, attachments (photos/files via the document picker), permissions - release builds allow plain http:// LAN servers (Go's net/http dials directly, no cleartext manifest needed) - a local fork of MyGo (third_party/mygo) lowers the Android host's minSdk to 24 (Android 7.0); Go 1.27.4 via mise Version 0.3.0.
383 lines
13 KiB
Go
383 lines
13 KiB
Go
package ui
|
|
|
|
import (
|
|
"runtime"
|
|
"slices"
|
|
"time"
|
|
)
|
|
|
|
// OutlineState is the state of an Outline or an OutlineTable: its rows, as
|
|
// a List's, and the items that are open, showing their children. The rows'
|
|
// keys are the items, of type K, so that their place, choice and state
|
|
// follow them as items open and close above them.
|
|
//
|
|
// type app struct {
|
|
// files ui.OutlineState[string] // paths
|
|
// file int
|
|
// }
|
|
//
|
|
// app.files.List.Selected = &app.file
|
|
type OutlineState[K comparable] struct {
|
|
// List is the state of the rows: their place, and how they are chosen,
|
|
// as a List's. Its Key is the rows' items; a Selection takes them, of
|
|
// type K.
|
|
List ListState
|
|
// Open holds the items showing their children.
|
|
Open Selection[K]
|
|
// Indent is the width in DIPs each level adds before a row's arrow,
|
|
// without a Prefix, the theme's Space(4) when zero. It must not be
|
|
// negative.
|
|
Indent float32
|
|
// Prefix, when set, builds the start of each row in place of the
|
|
// outline's indentation and arrow, before what row builds, for rows of
|
|
// a look of your own: r tells how deep the row's item is and whether
|
|
// it is open, and Arrow makes the element opening and closing it. An
|
|
// Outline's rows then have neither padding nor a gap, and center
|
|
// what they hold; an OutlineTable's first cells keep their padding.
|
|
Prefix func(r OutlineRow)
|
|
// Generation, when not zero, lets the outline keep its rows from one
|
|
// frame to the next, walking the open items again only when Open, the
|
|
// roots or Generation change: change it, as with Generation++,
|
|
// whenever children would return other items, or nil where it did not,
|
|
// or the other way round. Open is seen changing through its own
|
|
// methods, not through a copy of it sharing its items. When zero, the
|
|
// outline walks all the open items in every frame, which costs as
|
|
// much as they are many, and shows what children returns at once.
|
|
Generation uint64
|
|
rows []outlineRow[K]
|
|
// children is the function giving the items' children in the last
|
|
// build, for the rows' arrows.
|
|
children func(K) []K
|
|
// The Generation, the stamp of Open and the roots the rows were made
|
|
// from, while Generation is not zero.
|
|
madeGen uint64
|
|
madeOpen uint64
|
|
madeFrom []K
|
|
}
|
|
|
|
// OutlineRow is a row of an outline as its Prefix builds it, while it
|
|
// builds it.
|
|
type OutlineRow struct {
|
|
// Depth is how deep the row's item is: 0 at the top. Branch tells
|
|
// that it has children, and Open that they show.
|
|
Depth int
|
|
Branch, Open bool
|
|
c *context
|
|
// The outline, row and tree item Arrow makes the arrow of, without a
|
|
// closure built for each row in every frame.
|
|
outline outlineArrows
|
|
row int
|
|
item *node
|
|
}
|
|
|
|
// outlineArrows makes the arrows of an outline's rows: an OutlineState of
|
|
// any type of item.
|
|
type outlineArrows interface {
|
|
arrow(c *context, i int, item *node) *node
|
|
}
|
|
|
|
// Arrow creates the row's arrow without a look: a box that opens or
|
|
// closes the item when clicked, with all inside it with Option (Alt),
|
|
// leaving the keyboard focus where it is. Size it and draw it from Open.
|
|
// Of an item without children, it is a box that does nothing, which
|
|
// keeps the rows' content in line.
|
|
func (r OutlineRow) Arrow() Element {
|
|
if r.c == nil || r.c.rt == nil {
|
|
return Element{}
|
|
}
|
|
// A constructor, as the generated ones: its click applies as it
|
|
// returns.
|
|
r.c.rt.beginBuild()
|
|
a := r.outline.arrow(r.c, r.row, r.item)
|
|
r.c.rt.endBuild()
|
|
return wrapElement(a)
|
|
}
|
|
|
|
// outlineRow is a row of an outline: its item, how deep it is, the row of
|
|
// its parent (-1 for the items at the top), and whether it has children.
|
|
type outlineRow[K comparable] struct {
|
|
item K
|
|
depth int
|
|
parent int
|
|
branch bool
|
|
}
|
|
|
|
// Rows returns how many rows the outline shows: its items at the top, and
|
|
// the children of those open, in the last frame.
|
|
func (s *OutlineState[K]) Rows() int { return len(s.rows) }
|
|
|
|
// Item returns the item row i shows, in the last frame.
|
|
func (s *OutlineState[K]) Item(row int) K { return s.rows[row].item }
|
|
|
|
// Depth returns how deep the item of row i is: 0 for the items at the top.
|
|
func (s *OutlineState[K]) Depth(row int) int { return s.rows[row].depth }
|
|
|
|
// flatten lists the rows the outline shows: roots, and the children of
|
|
// the items open, or keeps those of the last build while Generation is
|
|
// not zero and neither it, Open nor roots changed.
|
|
func (s *OutlineState[K]) flatten(roots []K, children func(K) []K) {
|
|
if !(s.Indent >= 0) {
|
|
panic("ui: an OutlineState's Indent must not be negative")
|
|
}
|
|
s.children = children
|
|
if s.Generation != 0 && s.madeGen == s.Generation && s.madeOpen == s.Open.stamp && slices.Equal(s.madeFrom, roots) {
|
|
return
|
|
}
|
|
s.madeGen, s.madeOpen = s.Generation, s.Open.stamp
|
|
s.madeFrom = s.madeFrom[:0]
|
|
if s.Generation != 0 {
|
|
s.madeFrom = append(s.madeFrom, roots...)
|
|
}
|
|
s.rows = s.rows[:0]
|
|
var walk func(items []K, depth, parent int)
|
|
walk = func(items []K, depth, parent int) {
|
|
for _, item := range items {
|
|
kids := children(item)
|
|
s.rows = append(s.rows, outlineRow[K]{item: item, depth: depth, parent: parent, branch: kids != nil})
|
|
if kids != nil && s.Open.Has(item) {
|
|
walk(kids, depth+1, len(s.rows)-1)
|
|
}
|
|
}
|
|
}
|
|
walk(roots, 0, -1)
|
|
}
|
|
|
|
// setOpen opens or closes an item, and with all its children's children.
|
|
func (s *OutlineState[K]) setOpen(item K, open, all bool, children func(K) []K) {
|
|
if open {
|
|
s.Open.Add(item)
|
|
} else {
|
|
s.Open.Remove(item)
|
|
}
|
|
if all {
|
|
for _, kid := range children(item) {
|
|
if children(kid) != nil {
|
|
s.setOpen(kid, open, true, children)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Outline creates a list of items in a tree, as AppKit's outline view and
|
|
// SwiftUI's List of children: roots at the top, and below each item open
|
|
// its children, indented, which children returns, nil for an item without
|
|
// any. A click on an item's arrow opens or closes it, with all its
|
|
// children's children with Option (Alt). row builds the content of an
|
|
// item's row, after its arrow; Gap, Padding and the rest are a List's.
|
|
//
|
|
// The outline builds only the rows in view, as a List does. It walks the
|
|
// open items in every frame, unless s.Generation tells it when children
|
|
// changes, which keeps trees of any size as fast as small ones. With
|
|
// s.List.Selected or Selection, rows are chosen as a List's, and while
|
|
// the outline has the focus, Right opens the item chosen or goes to its
|
|
// first child, and Left closes it or goes to its parent, with Option on
|
|
// macOS and Shift elsewhere, as in GTK, opening and closing all inside.
|
|
//
|
|
// app.files.List.Selected = &app.file
|
|
// ui.Outline(c, &app.files, []string{"/"}, func(dir string) []string {
|
|
// return app.children[dir] // nil for files
|
|
// }, func(path string) {
|
|
// ui.Text(c, filepath.Base(path))
|
|
// }).Grow(1)
|
|
func coreOutline[K comparable](c *context, s *OutlineState[K], roots []K, children func(K) []K, row func(item K)) *node {
|
|
e := coreScroll(c)
|
|
e.widget, e.role = "Outline", RoleTree
|
|
s.flatten(roots, children)
|
|
s.List.Key = func(i int) any { return s.rows[i].item }
|
|
buildList(c, e, e, &s.List, len(s.rows), func(i int) {
|
|
r := coreRow(c).AlignItems(Center)
|
|
if s.Prefix == nil {
|
|
r.Gap(c.theme.Space(1)).Padding(c.theme.Space(1), c.theme.Space(2))
|
|
}
|
|
r.Role(RoleNone)
|
|
r.Children(func() { s.prefix(c, i, children, func() { row(s.rows[i].item) }) })
|
|
}, treeList)
|
|
e.afterInput(func() { s.keys(e, children) })
|
|
return e
|
|
}
|
|
|
|
// OutlineTable creates an outline whose rows are a Table's, as AppKit's
|
|
// outline view with columns: the first of columns shows the items' arrows,
|
|
// indented as they are deep, before what cell builds. It takes the
|
|
// columns, the sort and the keys of a Table, and those of an Outline.
|
|
//
|
|
// ui.OutlineTable(c, &app.files, cols, roots, children, func(path string, col int) {
|
|
// switch col {
|
|
// case 0:
|
|
// ui.Text(c, filepath.Base(path)).SingleLine()
|
|
// case 1:
|
|
// ui.Text(c, app.size(path))
|
|
// }
|
|
// })
|
|
func coreOutlineTable[K comparable](c *context, s *OutlineState[K], columns []TableColumn, roots []K, children func(K) []K, cell func(item K, col int)) *node {
|
|
s.flatten(roots, children)
|
|
s.List.Key = func(i int) any { return s.rows[i].item }
|
|
e := table(c, &s.List, columns, len(s.rows), func(i, col int) {
|
|
if col != 0 {
|
|
cell(s.rows[i].item, col)
|
|
return
|
|
}
|
|
s.prefix(c, i, children, func() { cell(s.rows[i].item, 0) })
|
|
}, treeTableList)
|
|
e.afterInput(func() { s.keys(e, children) })
|
|
return e
|
|
}
|
|
|
|
// prefix builds the indentation and the arrow of row i, or what Prefix
|
|
// builds there, then content, and tells assistive technology how deep the
|
|
// row is and whether it is open, opening and closing it as that asks.
|
|
func (s *OutlineState[K]) prefix(c *context, i int, children func(K) []K, content func()) {
|
|
t := c.theme
|
|
r := s.rows[i]
|
|
open := r.branch && s.Open.Has(r.item)
|
|
item := treeItemOf(c, r.depth, r.branch, open)
|
|
if item != nil && r.branch && item.st.expand != 0 {
|
|
// Assistive technology opening or closing it, once the outline is
|
|
// built, as a click on the arrow does.
|
|
item.afterInput(func() {
|
|
if st := item.st; st.expand != 0 {
|
|
if (st.expand > 0) != s.Open.Has(r.item) {
|
|
s.toggle(i, item, false, children)
|
|
}
|
|
st.expand = 0
|
|
c.rt.consumed = true
|
|
}
|
|
})
|
|
}
|
|
if s.Prefix != nil {
|
|
s.Prefix(OutlineRow{Depth: r.depth, Branch: r.branch, Open: open, c: c, outline: s, row: i, item: item})
|
|
content()
|
|
return
|
|
}
|
|
if r.depth > 0 {
|
|
indent := s.Indent
|
|
if indent == 0 {
|
|
indent = t.Space(4)
|
|
}
|
|
coreBox(c).Width(float32(r.depth) * indent).Shrink(0)
|
|
}
|
|
a := s.arrow(c, i, item).Size(t.Space(4), t.Space(4))
|
|
if r.branch {
|
|
turn := a.Animate("open", 90*b2f(open), 150*time.Millisecond)
|
|
rtl := a.rtl()
|
|
a.Draw(func(p *Painter, rect Rect) {
|
|
p.StrokePath(disclosurePath(rect, turn, rtl), 1.5, arrowColor(t, item))
|
|
})
|
|
}
|
|
content()
|
|
}
|
|
|
|
// treeItemOf returns the list's element holding the row being built,
|
|
// which is the tree's item, telling it how deep the row is, whether it has
|
|
// children and whether they show; nil outside a list's row.
|
|
func treeItemOf(c *context, depth int, branch, open bool) *node {
|
|
for p := c.parent; p != nil; p = p.parent {
|
|
if p.listRow {
|
|
p.level, p.expandable, p.expanded = depth+1, branch, open
|
|
return p
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// arrow creates the box of row i's arrow, which opens or closes its item
|
|
// when clicked, with all inside it with Option (Alt); item is the tree's
|
|
// item holding the row.
|
|
func (s *OutlineState[K]) arrow(c *context, i int, item *node) *node {
|
|
a := coreBox(c).Shrink(0).Role(RoleNone)
|
|
if r := s.rows[i]; r.branch {
|
|
a.flags |= flagClickable | flagKeepFocus
|
|
children := s.children
|
|
a.afterInput(func() {
|
|
if a.Clicked() {
|
|
s.toggle(i, item, a.ClickModifiers()&Alt != 0, children)
|
|
}
|
|
})
|
|
}
|
|
return a
|
|
}
|
|
|
|
// toggle opens row i's item if it is closed, or closes it, with all
|
|
// inside it with all, and tells the tree's item holding the row.
|
|
func (s *OutlineState[K]) toggle(i int, item *node, all bool, children func(K) []K) {
|
|
key := s.rows[i].item
|
|
open := s.Open.Has(key)
|
|
s.setOpen(key, !open, all, children)
|
|
s.closed(i, open)
|
|
if item != nil {
|
|
item.expanded = !open
|
|
}
|
|
}
|
|
|
|
// closed moves the choice to row i as it closes, from a row inside it,
|
|
// which no longer shows.
|
|
func (s *OutlineState[K]) closed(i int, wasOpen bool) {
|
|
sel := s.List.cursor()
|
|
if !wasOpen || sel == nil || *sel <= i {
|
|
return
|
|
}
|
|
for p := *sel; p >= 0; p = s.rows[p].parent {
|
|
if p == i {
|
|
s.List.frame.choose(i)
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
// keys opens and closes the item chosen with Right and Left while the
|
|
// outline has the focus, or moves to its first child or its parent.
|
|
func (s *OutlineState[K]) keys(owner *node, children func(K) []K) {
|
|
sel := s.List.cursor()
|
|
if sel == nil {
|
|
return
|
|
}
|
|
// Right opens, toward where the rows' content goes; Left right to
|
|
// left.
|
|
back, forth := owner.arrows()
|
|
right, left := owner.Shortcut(0, forth), owner.Shortcut(0, back)
|
|
allRight, allLeft := owner.Shortcut(allMod, forth), owner.Shortcut(allMod, back)
|
|
i := *sel
|
|
if i < 0 || i >= len(s.rows) {
|
|
return
|
|
}
|
|
r := s.rows[i]
|
|
open := r.branch && s.Open.Has(r.item)
|
|
f := &s.List.frame
|
|
switch {
|
|
case (right || allRight) && r.branch && (!open || allRight):
|
|
s.setOpen(r.item, true, allRight, children)
|
|
case right && open && i+1 < len(s.rows) && s.rows[i+1].parent == i:
|
|
f.choose(i + 1)
|
|
case (left || allLeft) && open:
|
|
s.setOpen(r.item, false, allLeft, children)
|
|
case (left || allLeft) && r.parent >= 0:
|
|
f.choose(r.parent)
|
|
}
|
|
}
|
|
|
|
// allMod opens and closes all inside an item with the arrows: Option on
|
|
// macOS, Shift elsewhere, as in GTK, where Alt and the arrows go back and
|
|
// forward.
|
|
var allMod = func() Modifiers {
|
|
if runtime.GOOS == "darwin" {
|
|
return Alt
|
|
}
|
|
return Shift
|
|
}()
|
|
|
|
// arrowColor is the color of the arrow of an item of a tree: muted, or
|
|
// the accent's text on the item chosen.
|
|
func arrowColor(t *Theme, item *node) Color {
|
|
if item != nil && item.checked == 2 {
|
|
return t.AccentText
|
|
}
|
|
return t.TextMuted
|
|
}
|
|
|
|
// rotate returns a function turning points around (cx, cy) by deg degrees
|
|
// clockwise.
|
|
func rotate(cx, cy, deg float32) func(x, y float32) (float32, float32) {
|
|
sin, cos := sincos(deg)
|
|
return func(x, y float32) (float32, float32) { return cx + x*cos - y*sin, cy + x*sin + y*cos }
|
|
}
|