Files
MujicaUI/input/text_area.go
T
ZacharyZhang-NYandClaude Opus 5.5 bcce34cda1 Upgrade MyGo to v0.5.5
MyGo v0.5.5 returns ui.Element as a checked value handle instead of a
pointer. Migrated with `mygo migrate-ui`, then fixed what the codemod
leaves to review:

- Element nil checks become .Valid(); absent elements are ui.Element{}.
- Input handlers and paint callbacks run outside the build pass, where
  elements expire and Context methods are inert: read IsDisabled into a
  local during the build, keep a ui.Handle for Focus, use c.Services()
  for clipboard access, create ui.Local state during the build.
- Add core.AutoFocus: v0.5.5 records an overlay's opener after its
  content builds, so same-frame autofocus lost focus restore on close.
  It focuses one frame later; cascader rows defer their focus likewise.
- core.OverlayLayer identifies build passes by the root element, not
  c.Now(), which repeats across passes and under the Tester clock.
- Context menus survive the opening press counted as PressedOutside;
  split button tooltip no longer reorders its open menu; disabled
  menubar rows pass presses to the panel; Form defers blur-started
  async checks so the growing message no longer moves Submit away
  from the release; Combobox drops its disabled copy now that MyGo
  blocks typing under a disabled wrapper (canary flipped to a guard).
- The Tester clock is virtual: tests use Advance/Now instead of
  time.Sleep/time.Now and read element state inside the view. The web
  mirror advances the clock to wall time before each frame and input.
- AGENTS.md and docs describe the value Element API and the new pin.

Snapshot goldens are darwin-only and were not re-verified here.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-17 01:37:54 -04:00

105 lines
3.2 KiB
Go

package input
import (
"fmt"
"unicode/utf8"
"github.com/egoist/mygo/ui"
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/core"
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/theme"
)
// TextAreaOptions configure a TextArea.
type TextAreaOptions struct {
Placeholder string
// Label names the field for assistive technology.
Label string
// MinHeight is the least height in DIPs; zero fits three lines.
MinHeight float32
// MaxHeight caps the height in DIPs, then the text scrolls; zero grows freely.
MaxHeight float32
// MaxLength shows a character count against it; zero hides the count.
MaxLength int
// SubmitKey with SubmitMods submits; a zero key means Cmd+Enter.
SubmitKey ui.Key
SubmitMods ui.Modifiers
Disabled bool
ReadOnly bool
// Error marks the value invalid and shows the message below.
Error string
// Keys sees keys before the editor, for PromptComposer's menus.
Keys func(ui.InputEvent) bool
}
// TextArea is a multi-line field editing *value.
func TextArea(c *ui.Context, value *string, opts TextAreaOptions) InputResult {
if value == nil {
panic("mujica: TextArea needs a value")
}
if opts.MinHeight < 0 || opts.MaxHeight < 0 || opts.MaxHeight > 0 && opts.MaxHeight < opts.MinHeight {
panic("mujica: TextArea needs 0 <= MinHeight <= MaxHeight")
}
if opts.MaxLength < 0 {
panic("mujica: TextArea MaxLength is negative")
}
key, mods := opts.SubmitKey, opts.SubmitMods
if key == 0 {
key, mods = ui.KeyEnter, ui.Cmd
}
d := core.Density(c)
n := utf8.RuneCountInString(*value)
over := opts.MaxLength > 0 && n > opts.MaxLength
errMsg := opts.Error
if errMsg == "" && over {
errMsg = fmt.Sprintf(core.Msg(c, "text_area.over"), n-opts.MaxLength)
}
count := textAreaCount(c, n, opts.MaxLength)
var r InputResult
look := &textInputLook{disabled: opts.Disabled, readOnly: opts.ReadOnly, err: errMsg, area: true}
r.Element, r.Input = textInputFrame(c, look, func(frame ui.Element) ui.Element {
in := TextInputEditor(c, frame, value, opts.ReadOnly || look.disabled, ui.TextAreaBase).Grow(1).Padding(d.Unit()*2, d.Unit()*3).
Placeholder(opts.Placeholder).Label(opts.Label)
if opts.MinHeight > 0 {
in.MinHeight(opts.MinHeight)
}
if opts.MaxHeight > 0 {
in.MaxHeight(opts.MaxHeight)
}
r.changed = in.Changed() && !opts.ReadOnly && !look.disabled
// Taken before the editor, which would make a plain Enter a newline.
submit := ui.Local(frame, "submit", func() bool { return false })
in.HandleInput(func(ev ui.InputEvent) bool {
if opts.Keys != nil && opts.Keys(ev) {
return true
}
if ev.Kind == ui.InputKeyDown && ev.Key == key && ev.Mods == mods {
*submit = true
return true
}
return false
})
r.submitted = *submit && !look.disabled
*submit = false
return in
}, count)
return r
}
// textAreaCount builds the character count against max, or nil without one.
func textAreaCount(c *ui.Context, n, max int) func() {
if max == 0 {
return nil
}
return func() {
k := core.Tokens(c)
color := k.TextMuted
over := n > max
if over {
color = k.Danger
}
ui.Text(c, fmt.Sprintf("%d / %d", n, max)).TextColor(color).FontSize(core.FontSize(c, theme.CaptionSize)).
FontFeatures("tnum").Shrink(0).Label(fmt.Sprintf(core.Msg(c, "text_area.count"), n, max))
}
}