728 lines
24 KiB
Go
728 lines
24 KiB
Go
package ui
|
|||
|
|
|
||
|
|
import (
|
||
|
|
"slices"
|
||
|
|
|
||
|
|
"github.com/egoist/mygo/internal/platform"
|
||
|
|
|
||
|
|
transfer "github.com/egoist/mygo/transfer"
|
||
|
|
)
|
||
|
|
|
||
|
|
// TextInput creates a single-line text input editing *value.
|
||
|
|
func coreTextInput(c *context, value *string) *node { return textInput(c, value, false) }
|
||
|
|
|
||
|
|
// TextArea creates a multi-line text input editing *value.
|
||
|
|
func coreTextArea(c *context, value *string) *node { return textInput(c, value, true) }
|
||
|
|
|
||
|
|
func textInput(c *context, value *string, multiline bool) *node {
|
||
|
|
t := c.theme
|
||
|
|
e := textInputBase(c, value, multiline)
|
||
|
|
e.Padding(t.Space(1.5), t.Space(2.5)).Radius(t.Radius).Background(t.Surface).Border(1, t.Border)
|
||
|
|
if multiline {
|
||
|
|
e.MinHeight(t.Space(20))
|
||
|
|
}
|
||
|
|
e.styleFn = func(e *node) { inputBorder(t, e, e) }
|
||
|
|
return e
|
||
|
|
}
|
||
|
|
|
||
|
|
func textInputBase(c *context, value *string, multiline bool) *node {
|
||
|
|
e := c.newElement(kindInput)
|
||
|
|
e.flags |= flagEditable | flagFocusable | flagHover
|
||
|
|
e.widget = "TextInput"
|
||
|
|
if multiline {
|
||
|
|
e.widget = "TextArea"
|
||
|
|
// It scrolls its text as a scroll container does its children.
|
||
|
|
e.flags |= flagScrollY
|
||
|
|
}
|
||
|
|
st := e.st
|
||
|
|
st.traits = platform.TextTraits{}
|
||
|
|
if st.editor == nil || st.editor.document != nil {
|
||
|
|
st.editor = newEditor(c.rt.now, c.rt.text)
|
||
|
|
st.editor.setText(*value)
|
||
|
|
st.editor.caret, st.editor.anchor = st.editor.buf.n, st.editor.buf.n
|
||
|
|
}
|
||
|
|
ed := st.editor
|
||
|
|
ed.value = value
|
||
|
|
|
||
|
|
if ed.client == nil {
|
||
|
|
ed.client = &widgetTextInput{ed: ed, rt: c.rt, id: e.id}
|
||
|
|
}
|
||
|
|
e.textClient = ed.client
|
||
|
|
ed.multiline = multiline
|
||
|
|
if multiline && ed.area == nil {
|
||
|
|
ed.area = &area{reveal: true}
|
||
|
|
}
|
||
|
|
// The input shares the string of *value: the same string is equal at
|
||
|
|
// once, whatever its length.
|
||
|
|
if !ed.nativeDirty && ed.buf.s != *value {
|
||
|
|
ed.setText(*value)
|
||
|
|
ed.compose = ""
|
||
|
|
}
|
||
|
|
ed.rebuild()
|
||
|
|
ed.ranges = ed.ranges[:0]
|
||
|
|
e.onValueInput(stringInput)
|
||
|
|
|
||
|
|
return e
|
||
|
|
}
|
||
|
|
|
||
|
|
// Placeholder shows s in an empty text input. On a field around an input,
|
||
|
|
// as SearchField, TokenField and Combobox, it shows in the field's input,
|
||
|
|
// in place of the field's own: SearchField shows Search without one.
|
||
|
|
func (e *node) Placeholder(s string) *node {
|
||
|
|
in := e
|
||
|
|
if in.st.editor == nil && in.focusTo != nil {
|
||
|
|
in = in.focusTo
|
||
|
|
}
|
||
|
|
if in.st.editor != nil {
|
||
|
|
in.st.editor.placeholder = s
|
||
|
|
}
|
||
|
|
return e
|
||
|
|
}
|
||
|
|
|
||
|
|
// PlaceholderColor sets the color of this input's placeholder for this build.
|
||
|
|
// When unset it uses the theme's TextMuted. It does not change entered text,
|
||
|
|
// other inputs or the theme. A transparent color hides the placeholder.
|
||
|
|
func (e *node) PlaceholderColor(color Color) *node {
|
||
|
|
if ed := e.st.editor; ed != nil && e.flags&flagEditable != 0 {
|
||
|
|
ed.placeholderColor, ed.placeholderColorSet = color, true
|
||
|
|
}
|
||
|
|
return e
|
||
|
|
}
|
||
|
|
|
||
|
|
// CaretWidth sets this input's painted caret width in DIPs for this build.
|
||
|
|
// It defaults to one DIP. Positive widths are rounded to device pixels,
|
||
|
|
// at least one pixel wide; zero and negative widths hide the caret. It does
|
||
|
|
// not change text layout, input-method candidate bounds or caret blinking.
|
||
|
|
// Other elements are unaffected.
|
||
|
|
func (e *node) CaretWidth(width float32) *node {
|
||
|
|
if ed := e.st.editor; ed != nil && e.flags&flagEditable != 0 {
|
||
|
|
ed.caretWidth = max(width, 0)
|
||
|
|
}
|
||
|
|
return e
|
||
|
|
}
|
||
|
|
|
||
|
|
// ReadOnly makes a text input show its text without letting the user
|
||
|
|
// change it: the text can still be selected and copied, from the keyboard
|
||
|
|
// too, as it takes the focus, without a caret; assistive technology reads
|
||
|
|
// it as read-only.
|
||
|
|
//
|
||
|
|
// On an element of your own that MyGo does not edit or operate, a widget
|
||
|
|
// that draws its value and handles its keys itself, it only tells
|
||
|
|
// assistive technology that the element is read-only, as ARIA's
|
||
|
|
// aria-readonly, and takes set-value, increment and decrement from it:
|
||
|
|
// for a text box
|
||
|
|
// (RoleTextField), combo box, check box, switch, slider, stepper, radio
|
||
|
|
// group, list box, grid or tree grid, and for the cells and headers
|
||
|
|
// (RoleCell, RoleColumnHeader, RoleRowHeader) of a grid or a tree grid,
|
||
|
|
// not of a table. Other roles, and MyGo's own controls but for the text
|
||
|
|
// inputs and text areas it is meant for, ignore it.
|
||
|
|
func (e *node) ReadOnly(on bool) *node {
|
||
|
|
if ed := e.st.editor; ed != nil && e.flags&flagEditable != 0 {
|
||
|
|
ed.readOnly = on
|
||
|
|
if on {
|
||
|
|
ed.compose = ""
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
e.readOnly = on
|
||
|
|
}
|
||
|
|
return e
|
||
|
|
}
|
||
|
|
|
||
|
|
// Multiline tells assistive technology that an element of the text box
|
||
|
|
// role (Role(RoleTextField)) that MyGo does not edit takes several lines,
|
||
|
|
// as ARIA's aria-multiline. Text inputs and text areas know it
|
||
|
|
// themselves, from the control they are, and other roles ignore it.
|
||
|
|
func (e *node) Multiline(on bool) *node {
|
||
|
|
if ed := e.st.editor; ed == nil || e.flags&flagEditable == 0 {
|
||
|
|
e.multiline = on
|
||
|
|
}
|
||
|
|
return e
|
||
|
|
}
|
||
|
|
|
||
|
|
// MaxLength keeps the user from making a text input's value longer than n,
|
||
|
|
// counted as HTML's maxlength counts it, in UTF-16 code units, a newline
|
||
|
|
// as one: text typed, pasted or committed by an input method is cut to
|
||
|
|
// what fits at the caret, and replacing a selection takes what fits in its
|
||
|
|
// place. A value the app sets is not cut. n <= 0 means no limit.
|
||
|
|
// Like ReadOnly, it holds for the input that comes until the next frame.
|
||
|
|
func (e *node) MaxLength(n int) *node {
|
||
|
|
if ed := e.st.editor; ed != nil && e.flags&flagEditable != 0 {
|
||
|
|
ed.maxLength = n
|
||
|
|
}
|
||
|
|
return e
|
||
|
|
}
|
||
|
|
|
||
|
|
// OnPaste has fn see a paste into a text input, by Cmd+V, Ctrl+V or a
|
||
|
|
// Paste menu item, before it is inserted, on the main thread as the paste
|
||
|
|
// comes: text is the clipboard's text, empty when it holds none, as with
|
||
|
|
// an image, which fn reads with Services.ReadClipboardData. fn reports
|
||
|
|
// whether it took the paste; a paste taken changes nothing, its selection
|
||
|
|
// and undo history included, as when a web page prevents a paste's
|
||
|
|
// default and attaches a large block instead of inserting it. Like
|
||
|
|
// ReadOnly, it holds for the input that comes until the next frame.
|
||
|
|
//
|
||
|
|
// fn runs between frames, where an Element kept from the view is no
|
||
|
|
// longer valid, and the input writes what it holds over its bound value,
|
||
|
|
// so setting the value from fn is lost. To put text of its own in place
|
||
|
|
// of the paste, as a link to an image it attaches, fn keeps the text and
|
||
|
|
// the view inserts it with ReplaceText as it is built again, which a
|
||
|
|
// paste seen by fn does at once:
|
||
|
|
//
|
||
|
|
// svc := c.Services()
|
||
|
|
// in := ui.TextArea(c, &app.draft).OnPaste(func(string) bool {
|
||
|
|
// img := svc.ReadClipboardData(transfer.PNG)
|
||
|
|
// if len(img.Items()) == 0 {
|
||
|
|
// return false // text pastes as usual
|
||
|
|
// }
|
||
|
|
// app.insert = app.attach(img) // ""
|
||
|
|
// return true
|
||
|
|
// })
|
||
|
|
// if app.insert != "" {
|
||
|
|
// start, end := in.TextSelection()
|
||
|
|
// in.ReplaceText(start, end, app.insert) // a step of undo
|
||
|
|
// app.insert = ""
|
||
|
|
// }
|
||
|
|
func (e *node) OnPaste(fn func(text string) bool) *node {
|
||
|
|
if ed := e.st.editor; ed != nil && e.flags&flagEditable != 0 {
|
||
|
|
ed.onPaste = fn
|
||
|
|
}
|
||
|
|
return e
|
||
|
|
}
|
||
|
|
|
||
|
|
// OnTextKey has fn see a key pressed in the text input before the input
|
||
|
|
// takes it, on the main thread as the key comes, with the text typed
|
||
|
|
// before it in place, as AppKit's doCommandBySelector and a web page's
|
||
|
|
// keydown on a text field: Enter that sends a message, Up that edits the
|
||
|
|
// last one, Tab that accepts a completion, Escape that cancels, Cmd+Enter.
|
||
|
|
// fn reports whether it took the key; one taken changes nothing but what
|
||
|
|
// fn sets: a text area inserts no newline, a single-line input does not
|
||
|
|
// submit, Tab does not move the focus. One left goes on as it would
|
||
|
|
// without fn.
|
||
|
|
//
|
||
|
|
// fn sees the keys that type no text: those with Cmd or Ctrl, and Enter,
|
||
|
|
// Tab, Escape, Backspace, Delete, the arrows and the other keys that move
|
||
|
|
// the caret, as pressed (macOS's Control keys that move the caret too).
|
||
|
|
// While an input method composes, its keys are its own. Keys that the
|
||
|
|
// window or an element around the focus handles as a Shortcut go to the
|
||
|
|
// shortcut first, as with HandleInput, unless the input edits with them,
|
||
|
|
// as with Enter and the arrows. Like ReadOnly, OnTextKey holds for the
|
||
|
|
// input that comes until the next frame.
|
||
|
|
//
|
||
|
|
// fn runs between frames, where an Element kept from the view is no longer
|
||
|
|
// valid: it reads and replaces the text through k instead, as a chat
|
||
|
|
// composer sends with Enter, clears what it sent, and breaks the line with
|
||
|
|
// Shift+Enter:
|
||
|
|
//
|
||
|
|
// ui.TextArea(c, &app.draft).OnTextKey(func(k ui.TextKey) bool {
|
||
|
|
// if k.Key != ui.KeyEnter || k.Mods != 0 {
|
||
|
|
// return false // Shift+Enter breaks the line
|
||
|
|
// }
|
||
|
|
// app.send(k.Text())
|
||
|
|
// k.SetText("")
|
||
|
|
// return true
|
||
|
|
// })
|
||
|
|
func (e *node) OnTextKey(fn func(k TextKey) bool) *node {
|
||
|
|
if ed := e.st.editor; ed != nil && e.flags&flagEditable != 0 {
|
||
|
|
ed.onTextKey = fn
|
||
|
|
}
|
||
|
|
return e
|
||
|
|
}
|
||
|
|
|
||
|
|
// A TextKey is a key pressed in a text input, as OnTextKey's fn sees it.
|
||
|
|
// It is valid while fn runs: Text and SetText panic after fn returns.
|
||
|
|
type TextKey struct {
|
||
|
|
Key Key
|
||
|
|
Mods Modifiers
|
||
|
|
ed *editor
|
||
|
|
seq uint64
|
||
|
|
}
|
||
|
|
|
||
|
|
// valid panics unless k's fn is running.
|
||
|
|
func (k TextKey) valid() *editor {
|
||
|
|
if k.ed == nil || !k.ed.keying || k.ed.keySeq != k.seq {
|
||
|
|
panic("ui: a TextKey is valid only while its OnTextKey fn runs")
|
||
|
|
}
|
||
|
|
return k.ed
|
||
|
|
}
|
||
|
|
|
||
|
|
// Text returns the text of the input as the key comes, with what was typed
|
||
|
|
// before it, which may be ahead of the bound value: that takes what is
|
||
|
|
// typed as the next frame is built.
|
||
|
|
func (k TextKey) Text() string { return k.valid().String() }
|
||
|
|
|
||
|
|
// SetText replaces the text of the input, and its bound value, as the app
|
||
|
|
// does by setting the value as the view is built: a composer clears the
|
||
|
|
// draft it sent, Up puts the last message back. Writing the bound value
|
||
|
|
// from fn is not enough, as the editor writes what it holds over it.
|
||
|
|
func (k TextKey) SetText(s string) {
|
||
|
|
ed := k.valid()
|
||
|
|
if ed.document != nil {
|
||
|
|
// A TextBuffer takes the text as when the app sets it.
|
||
|
|
ed.document.Set(s)
|
||
|
|
ed.loadBuffer(ed.document.Snapshot())
|
||
|
|
ed.bufferDirty = false
|
||
|
|
} else {
|
||
|
|
ed.setText(s)
|
||
|
|
ed.compose = ""
|
||
|
|
if ed.value != nil {
|
||
|
|
*ed.value = s
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if w := ed.client; w != nil {
|
||
|
|
// What the view built before the key shows the old text.
|
||
|
|
w.rt.consumed = true
|
||
|
|
w.rt.requestFrame()
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// Lines makes a text area as high as its text, wrapped at its width, from
|
||
|
|
// min lines up to max, past which it scrolls, as a message field grows with
|
||
|
|
// what is typed: TextArea(c, &draft).Lines(1, 8). Without it, a text area is
|
||
|
|
// as high as its paragraphs, three lines at least, unless given a height.
|
||
|
|
func (e *node) Lines(min, max int) *node {
|
||
|
|
if ed := e.st.editor; ed != nil && ed.area != nil {
|
||
|
|
ed.lines = [2]int{min, max}
|
||
|
|
}
|
||
|
|
return e
|
||
|
|
}
|
||
|
|
|
||
|
|
// TextSelection returns the selection of a text input as offsets in runes
|
||
|
|
// into its text, the caret where start equals end, as the app reads it to
|
||
|
|
// complete the word being typed.
|
||
|
|
func (e *node) TextSelection() (start, end int) {
|
||
|
|
ed := e.st.editor
|
||
|
|
if ed == nil {
|
||
|
|
return 0, 0
|
||
|
|
}
|
||
|
|
return ed.selection()
|
||
|
|
}
|
||
|
|
|
||
|
|
// TextAnchorCaret returns the selection of a text input as offsets in runes
|
||
|
|
// into its text, from where it was started to its caret: the anchor is after
|
||
|
|
// the caret when the selection was made backwards.
|
||
|
|
func (e *node) TextAnchorCaret() (anchor, caret int) {
|
||
|
|
ed := e.st.editor
|
||
|
|
if ed == nil {
|
||
|
|
return 0, 0
|
||
|
|
}
|
||
|
|
return ed.anchor, ed.caret
|
||
|
|
}
|
||
|
|
|
||
|
|
// SetTextSelection selects the runes of a text input from start to end, or
|
||
|
|
// puts the caret at start when they are equal, and scrolls it into view: the
|
||
|
|
// caret after a word the app completed. The offsets are kept within the text.
|
||
|
|
func (e *node) SetTextSelection(start, end int) *node {
|
||
|
|
ed := e.st.editor
|
||
|
|
if ed == nil {
|
||
|
|
return e
|
||
|
|
}
|
||
|
|
n := ed.buf.n
|
||
|
|
start, end = min(max(start, 0), n), min(max(end, 0), n)
|
||
|
|
ed.editorSelection = editorSelection{anchor: start, caret: end}
|
||
|
|
ed.hasDesired = false
|
||
|
|
if ed.area != nil {
|
||
|
|
ed.area.reveal = true
|
||
|
|
}
|
||
|
|
e.c.rt.requestFrame()
|
||
|
|
return e
|
||
|
|
}
|
||
|
|
|
||
|
|
// ReplaceText replaces the runes of a text input from start to end with s,
|
||
|
|
// as an edit, a step of undo of its own: Undo brings back the text the
|
||
|
|
// user had, and Redo s again. A command that writes a message replaces all
|
||
|
|
// of it, from 0 to math.MaxInt; a completion, the word before the caret.
|
||
|
|
// The offsets are kept within the text, as SetTextSelection's, and what was
|
||
|
|
// typed in this frame is applied first. Setting the bound value instead
|
||
|
|
// takes the new text into the last step, which suits a reformat of what
|
||
|
|
// the user typed, not a text that replaces it (see TextInput). As a paste
|
||
|
|
// does, it puts the caret after s, and a single-line input takes line
|
||
|
|
// breaks as spaces; MaxLength does not cut it, as it does not cut a value
|
||
|
|
// the app sets. Changed reports it. It does nothing when the runes are s
|
||
|
|
// already.
|
||
|
|
func (e *node) ReplaceText(start, end int, s string) *node {
|
||
|
|
if !e.hasState() || e.st.editor == nil || e.flags&flagEditable == 0 {
|
||
|
|
return e
|
||
|
|
}
|
||
|
|
// What was typed this frame comes first.
|
||
|
|
e.c.rt.applyInputs()
|
||
|
|
ed := e.st.editor
|
||
|
|
ed.syncBuffer()
|
||
|
|
n := ed.buf.n
|
||
|
|
start, end = min(max(start, 0), n), min(max(end, 0), n)
|
||
|
|
start, end = min(start, end), max(start, end)
|
||
|
|
// As the input takes it, a text it holds already is no edit.
|
||
|
|
s = ed.edited(s)
|
||
|
|
if ed.buf.slice(start, end) == s {
|
||
|
|
return e
|
||
|
|
}
|
||
|
|
ed.compose, ed.compositionActive = "", false
|
||
|
|
ed.record(false)
|
||
|
|
maxLength := ed.maxLength
|
||
|
|
ed.maxLength = 0
|
||
|
|
ed.replace(start, end, s)
|
||
|
|
ed.maxLength = maxLength
|
||
|
|
if ed.document != nil {
|
||
|
|
snapshot, ok := ed.document.compareRestore(ed.published, ed.buf.root)
|
||
|
|
if !ok {
|
||
|
|
// The app changed the buffer meanwhile, from another goroutine:
|
||
|
|
// its text wins, as it does over the user's edits.
|
||
|
|
ed.loadBuffer(snapshot)
|
||
|
|
return e
|
||
|
|
}
|
||
|
|
ed.published = snapshot
|
||
|
|
} else if ed.value != nil {
|
||
|
|
*ed.value = ed.buf.s
|
||
|
|
}
|
||
|
|
if ed.area != nil {
|
||
|
|
ed.area.reveal = true
|
||
|
|
}
|
||
|
|
e.st.markChanged()
|
||
|
|
e.c.rt.requestFrame()
|
||
|
|
return e
|
||
|
|
}
|
||
|
|
|
||
|
|
// Composing reports whether an input method composes text in a text
|
||
|
|
// input, as Pinyin before a candidate is chosen: its value holds the text
|
||
|
|
// once composed. Keys typed meanwhile are the input method's, as Enter
|
||
|
|
// choosing a candidate: they submit nothing and press no shortcut.
|
||
|
|
func (e *node) Composing() bool {
|
||
|
|
ed := e.st.editor
|
||
|
|
return ed != nil && ed.compose != ""
|
||
|
|
}
|
||
|
|
|
||
|
|
// Password hides what a text input holds, in the frames that call it: an
|
||
|
|
// input that stops calling it shows its text again, as a field's eye
|
||
|
|
// button does. It does nothing to a text area: as on every platform, only
|
||
|
|
// single-line fields hide their text.
|
||
|
|
func (e *node) Password() *node {
|
||
|
|
if ed := e.st.editor; ed != nil && !ed.multiline {
|
||
|
|
ed.password = true
|
||
|
|
}
|
||
|
|
return e
|
||
|
|
}
|
||
|
|
|
||
|
|
// KeyboardType is the layout of the on-screen keyboard for a text input.
|
||
|
|
type KeyboardType = platform.KeyboardKind
|
||
|
|
|
||
|
|
// The keyboards of phones and tablets. Hardware keyboards and desktops type as
|
||
|
|
// they do.
|
||
|
|
const (
|
||
|
|
KeyboardDefault = platform.KeyboardDefault
|
||
|
|
KeyboardEmail = platform.KeyboardEmail
|
||
|
|
KeyboardNumber = platform.KeyboardNumber
|
||
|
|
KeyboardDecimal = platform.KeyboardDecimal
|
||
|
|
KeyboardPhone = platform.KeyboardPhone
|
||
|
|
KeyboardURL = platform.KeyboardURL
|
||
|
|
KeyboardSearch = platform.KeyboardSearch
|
||
|
|
)
|
||
|
|
|
||
|
|
// ReturnKeyType is what the return key of an on-screen keyboard says.
|
||
|
|
type ReturnKeyType = platform.ReturnKeyKind
|
||
|
|
|
||
|
|
// The return keys of phones and tablets. ReturnNext moves the focus to the
|
||
|
|
// next element after it submits.
|
||
|
|
const (
|
||
|
|
ReturnDefault = platform.ReturnDefault
|
||
|
|
ReturnDone = platform.ReturnDone
|
||
|
|
ReturnGo = platform.ReturnGo
|
||
|
|
ReturnNext = platform.ReturnNext
|
||
|
|
ReturnSearch = platform.ReturnSearch
|
||
|
|
ReturnSend = platform.ReturnSend
|
||
|
|
)
|
||
|
|
|
||
|
|
// Capitalization is what an on-screen keyboard capitalizes as one types.
|
||
|
|
type Capitalization = platform.CapitalizeKind
|
||
|
|
|
||
|
|
// The capitalizations of on-screen keyboards.
|
||
|
|
const (
|
||
|
|
CapitalizeDefault = platform.CapitalizeDefault
|
||
|
|
CapitalizeNone = platform.CapitalizeNone
|
||
|
|
CapitalizeWords = platform.CapitalizeWords
|
||
|
|
CapitalizeSentences = platform.CapitalizeSentences
|
||
|
|
CapitalizeCharacters = platform.CapitalizeCharacters
|
||
|
|
)
|
||
|
|
|
||
|
|
// AutofillKind names what a text input holds, for the system to offer it from
|
||
|
|
// its passwords and contacts.
|
||
|
|
type AutofillKind = platform.AutofillKind
|
||
|
|
|
||
|
|
// The kinds of text the system fills in.
|
||
|
|
const (
|
||
|
|
AutofillNone = platform.AutofillNone
|
||
|
|
AutofillUsername = platform.AutofillUsername
|
||
|
|
AutofillPassword = platform.AutofillPassword
|
||
|
|
AutofillNewPassword = platform.AutofillNewPassword
|
||
|
|
AutofillOneTimeCode = platform.AutofillOneTimeCode
|
||
|
|
AutofillEmail = platform.AutofillEmail
|
||
|
|
AutofillName = platform.AutofillName
|
||
|
|
AutofillPhone = platform.AutofillPhone
|
||
|
|
AutofillAddress = platform.AutofillAddress
|
||
|
|
AutofillPostalCode = platform.AutofillPostalCode
|
||
|
|
AutofillCreditCard = platform.AutofillCreditCard
|
||
|
|
)
|
||
|
|
|
||
|
|
// Keyboard chooses the on-screen keyboard's layout for a text input, on
|
||
|
|
// phones and tablets.
|
||
|
|
func (e *node) Keyboard(k KeyboardType) *node {
|
||
|
|
if e.hasState() {
|
||
|
|
e.st.traits.Keyboard = k
|
||
|
|
}
|
||
|
|
return e
|
||
|
|
}
|
||
|
|
|
||
|
|
// ReturnKey sets what the return key of an on-screen keyboard says, for a
|
||
|
|
// single-line input. It submits the input as Enter does; ReturnNext moves
|
||
|
|
// the focus on as well.
|
||
|
|
func (e *node) ReturnKey(k ReturnKeyType) *node {
|
||
|
|
if e.hasState() {
|
||
|
|
e.st.traits.ReturnKey = k
|
||
|
|
}
|
||
|
|
return e
|
||
|
|
}
|
||
|
|
|
||
|
|
// Capitalize sets what an on-screen keyboard capitalizes as one types.
|
||
|
|
func (e *node) Capitalize(c Capitalization) *node {
|
||
|
|
if e.hasState() {
|
||
|
|
e.st.traits.Capitalize = c
|
||
|
|
}
|
||
|
|
return e
|
||
|
|
}
|
||
|
|
|
||
|
|
// Autocorrect turns an on-screen keyboard's corrections and suggestions on
|
||
|
|
// or off for a text input, on by default where the keyboard has them.
|
||
|
|
func (e *node) Autocorrect(on bool) *node {
|
||
|
|
if e.hasState() {
|
||
|
|
e.st.traits.Autocorrect = platform.AutocorrectOff
|
||
|
|
if on {
|
||
|
|
e.st.traits.Autocorrect = platform.AutocorrectOn
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return e
|
||
|
|
}
|
||
|
|
|
||
|
|
// Autofill tells the system what a text input holds, for it to offer it
|
||
|
|
// from its passwords and contacts, as with iOS's Passwords above the
|
||
|
|
// keyboard.
|
||
|
|
func (e *node) Autofill(k AutofillKind) *node {
|
||
|
|
if e.hasState() {
|
||
|
|
e.st.traits.Autofill = k
|
||
|
|
}
|
||
|
|
return e
|
||
|
|
}
|
||
|
|
|
||
|
|
// Search makes a text input one for searching, in the frames that call
|
||
|
|
// it, as ARIA's searchbox and HTML's <input type=search>: assistive
|
||
|
|
// technology reads it as a search field, as it reads a SearchField's
|
||
|
|
// input, while the view draws it as it likes, with an icon and a clear
|
||
|
|
// button of its own, and handles its keys. Name it with Label.
|
||
|
|
//
|
||
|
|
// ui.TextInput(c, &app.query).Placeholder("Search").Label("Search mail").Search()
|
||
|
|
func (e *node) Search() *node {
|
||
|
|
in := e
|
||
|
|
if in.flags&flagEditable == 0 && in.focusTo != nil {
|
||
|
|
in = in.focusTo // the input of the field around it
|
||
|
|
}
|
||
|
|
if in.flags&flagEditable != 0 {
|
||
|
|
in.search = true
|
||
|
|
}
|
||
|
|
return e
|
||
|
|
}
|
||
|
|
|
||
|
|
// Selectable lets the user select and copy text by dragging, double-clicking
|
||
|
|
// a word, triple-clicking a paragraph, or using Shift with the arrows. Its
|
||
|
|
// context menu has Copy and Select All; Cmd+C copies the selection.
|
||
|
|
//
|
||
|
|
// On a container, its Text and RichText descendants share one selection.
|
||
|
|
// Copy joins their selected text with newlines, in the order they were built.
|
||
|
|
// Nested Selectable containers have independent selections. Text inside
|
||
|
|
// controls, such as buttons and text inputs, keeps the control's interaction.
|
||
|
|
// Unselectable excludes a subtree. On a Text or RichText alone, the selection
|
||
|
|
// stays within that paragraph. Inline elements share their paragraph's
|
||
|
|
// selection; set Selectable on the paragraph.
|
||
|
|
func (e *node) Selectable() *node {
|
||
|
|
if e.isInline() {
|
||
|
|
return e
|
||
|
|
}
|
||
|
|
if e.kind == kindText || e.kind == kindBox {
|
||
|
|
e.flags &^= flagUnselectable
|
||
|
|
e.flags |= flagSelectable
|
||
|
|
}
|
||
|
|
return e
|
||
|
|
}
|
||
|
|
|
||
|
|
// SelectedText is the part of a paragraph that the selection of a
|
||
|
|
// Selectable container holds.
|
||
|
|
type SelectedText struct {
|
||
|
|
// ID is the paragraph's element's (Element.ID).
|
||
|
|
ID uint64
|
||
|
|
// Text is the paragraph's text, and Start and End the runes of it the
|
||
|
|
// selection holds.
|
||
|
|
Text string
|
||
|
|
Start, End int
|
||
|
|
}
|
||
|
|
|
||
|
|
// SelectedTexts returns the parts of the paragraphs that the selection of
|
||
|
|
// a Selectable container holds, in the order they were built, or none,
|
||
|
|
// as of the last frame: to copy them otherwise, as a context menu's item
|
||
|
|
// does.
|
||
|
|
func (e *node) SelectedTexts() []SelectedText {
|
||
|
|
return e.c.rt.selectedTexts(e.id)
|
||
|
|
}
|
||
|
|
|
||
|
|
// CopyData sets what Copy, its shortcut and the context menu's put on
|
||
|
|
// the clipboard for the selection of a Selectable container: fn gets its
|
||
|
|
// parts, as SelectedTexts returns them, and returns the data, as a
|
||
|
|
// document copies its paragraphs with blank lines between them as text
|
||
|
|
// (transfer.TextData), and as HTML too to keep their formatting.
|
||
|
|
func (e *node) CopyData(fn func(parts []SelectedText) transfer.Data) *node {
|
||
|
|
e.copyData = fn
|
||
|
|
return e
|
||
|
|
}
|
||
|
|
|
||
|
|
// Unselectable excludes a paragraph or container subtree from a surrounding
|
||
|
|
// Selectable container. A Selectable container nested inside it can provide
|
||
|
|
// a selection of its own. For inline elements, set it on their paragraph.
|
||
|
|
func (e *node) Unselectable() *node {
|
||
|
|
if e.isInline() {
|
||
|
|
return e
|
||
|
|
}
|
||
|
|
e.flags &^= flagSelectable
|
||
|
|
e.flags |= flagUnselectable
|
||
|
|
return e
|
||
|
|
}
|
||
|
|
|
||
|
|
// TextRange styles runes Start up to End of a text input's text: their
|
||
|
|
// color, when its alpha is not zero, and their weight, when Weight is set,
|
||
|
|
// as a message field shows the mentions in what is typed.
|
||
|
|
type TextRange struct {
|
||
|
|
Start, End int
|
||
|
|
Color Color
|
||
|
|
Weight int
|
||
|
|
}
|
||
|
|
|
||
|
|
// TextRanges styles runs of a text input's text, in the frames that call
|
||
|
|
// it. Ranges may overlap and nest, as the attributes of an attributed
|
||
|
|
// string do: where they do, each of Color and Weight is that of the last
|
||
|
|
// range given that sets it, across the calls of the frame. They follow the
|
||
|
|
// text as it is: an app that finds them in the text finds them again as it
|
||
|
|
// changes. A password shows none; an input method's composition shows
|
||
|
|
// unstyled between them.
|
||
|
|
func (e *node) TextRanges(ranges ...TextRange) *node {
|
||
|
|
ed := e.st.editor
|
||
|
|
if ed == nil || e.flags&flagEditable == 0 {
|
||
|
|
return e
|
||
|
|
}
|
||
|
|
for _, r := range ranges {
|
||
|
|
if r.End > r.Start {
|
||
|
|
ed.ranges = append(ed.ranges, r)
|
||
|
|
ed.apart = false
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return e
|
||
|
|
}
|
||
|
|
|
||
|
|
// rangeMerge is what sortRanges reuses from frame to frame.
|
||
|
|
type rangeMerge struct {
|
||
|
|
given []TextRange
|
||
|
|
points, starts []int
|
||
|
|
all, colors rangeHeap
|
||
|
|
weights rangeHeap
|
||
|
|
}
|
||
|
|
|
||
|
|
// sortRanges splits the ranges into runs apart, sorted by start, where
|
||
|
|
// each of Color and Weight is that of the last range given setting it, as
|
||
|
|
// TextRanges says, once the frame has given them all: a frame calling
|
||
|
|
// TextRanges for each range merges them once. It takes O(n log n) for n
|
||
|
|
// ranges: a sweep over their ends, with the ranges covering it on heaps
|
||
|
|
// by the order they were given in.
|
||
|
|
func (ed *editor) sortRanges() {
|
||
|
|
m := &ed.merge
|
||
|
|
given := append(m.given[:0], ed.ranges...)
|
||
|
|
points, starts := m.points[:0], m.starts[:0]
|
||
|
|
for i, r := range given {
|
||
|
|
points = append(points, r.Start, r.End)
|
||
|
|
starts = append(starts, i)
|
||
|
|
}
|
||
|
|
slices.Sort(points)
|
||
|
|
points = slices.Compact(points)
|
||
|
|
slices.SortFunc(starts, func(a, b int) int { return given[a].Start - given[b].Start })
|
||
|
|
m.all, m.colors, m.weights = m.all[:0], m.colors[:0], m.weights[:0]
|
||
|
|
out, next := ed.ranges[:0], 0
|
||
|
|
for k := 0; k+1 < len(points); k++ {
|
||
|
|
from, to := points[k], points[k+1]
|
||
|
|
for ; next < len(starts) && given[starts[next]].Start <= from; next++ {
|
||
|
|
i := starts[next]
|
||
|
|
m.all.push(i)
|
||
|
|
if given[i].Color.A > 0 {
|
||
|
|
m.colors.push(i)
|
||
|
|
}
|
||
|
|
if given[i].Weight != 0 {
|
||
|
|
m.weights.push(i)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if _, ok := m.all.top(given, from); !ok {
|
||
|
|
continue // between ranges
|
||
|
|
}
|
||
|
|
r := TextRange{Start: from, End: to}
|
||
|
|
if i, ok := m.colors.top(given, from); ok {
|
||
|
|
r.Color = given[i].Color
|
||
|
|
}
|
||
|
|
if i, ok := m.weights.top(given, from); ok {
|
||
|
|
r.Weight = given[i].Weight
|
||
|
|
}
|
||
|
|
if n := len(out); n > 0 && out[n-1].End == from && out[n-1].Color == r.Color && out[n-1].Weight == r.Weight {
|
||
|
|
out[n-1].End = to // a run as one, as an attributed string's
|
||
|
|
continue
|
||
|
|
}
|
||
|
|
out = append(out, r)
|
||
|
|
}
|
||
|
|
m.given, m.points, m.starts = given[:0], points[:0], starts[:0]
|
||
|
|
ed.ranges, ed.apart = out, true
|
||
|
|
}
|
||
|
|
|
||
|
|
// rangeHeap is a heap of ranges by the order they were given in, the last
|
||
|
|
// on top.
|
||
|
|
type rangeHeap []int
|
||
|
|
|
||
|
|
func (h *rangeHeap) push(i int) {
|
||
|
|
*h = append(*h, i)
|
||
|
|
s := *h
|
||
|
|
for j := len(s) - 1; j > 0; {
|
||
|
|
p := (j - 1) / 2
|
||
|
|
if s[p] >= s[j] {
|
||
|
|
break
|
||
|
|
}
|
||
|
|
s[p], s[j] = s[j], s[p]
|
||
|
|
j = p
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// top returns the last range given that covers at, taking off those that
|
||
|
|
// end by it, which cover nothing after it either.
|
||
|
|
func (h *rangeHeap) top(given []TextRange, at int) (int, bool) {
|
||
|
|
s := *h
|
||
|
|
for len(s) > 0 && given[s[0]].End <= at {
|
||
|
|
n := len(s) - 1
|
||
|
|
s[0] = s[n]
|
||
|
|
s = s[:n]
|
||
|
|
for j := 0; ; {
|
||
|
|
c := 2*j + 1
|
||
|
|
if c >= n {
|
||
|
|
break
|
||
|
|
}
|
||
|
|
if c+1 < n && s[c+1] > s[c] {
|
||
|
|
c++
|
||
|
|
}
|
||
|
|
if s[j] >= s[c] {
|
||
|
|
break
|
||
|
|
}
|
||
|
|
s[j], s[c] = s[c], s[j]
|
||
|
|
j = c
|
||
|
|
}
|
||
|
|
}
|
||
|
|
*h = s
|
||
|
|
if len(s) == 0 {
|
||
|
|
return 0, false
|
||
|
|
}
|
||
|
|
return s[0], true
|
||
|
|
}
|