Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions cmd/viva/initfiles/AGENTS.md.txt
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,11 @@ func (m *model) page(c ui.C) {
(opens as the pointer rests on it, on a click, or on Right/Enter/Space; Left goes back), in a ContextMenu too.
- A right-click menu: `ui.ContextMenu(c, row, func(menu *ui.MenuItems) { ... })` (Shift+F10 opens it too); items
take `.Icon(icon)`, `.Keys(ui.Primary, "c")` (shown, not bound), `.Checked(on)` and `.Chosen(on)`.
- A row a menu cannot say: `menu.ContentPage(40, func(c ui.C, bounds ui.Rect) { ui.ColorSwatch(c, ...) })` declares page controls
in a row of their own (the keyboard passes over it); `menu.Content(height, paint)` is the canvas painter for a row
the page cannot declare.
- `ui.SelectID(c, id, &m.size, choices, nil)` is `ui.Select` for a control the application names: a row's words can be its `LabelFor`, and the application's key and focus routing can target it.
- Color choices: `ui.ColorSwatch(c, color, chosen, "Red")` is a radio (`.Flush()` fills its border with the color, `.TabStop(chosen)` makes one swatch the Tab stop, `ColorSwatchID` keeps an application identity); `ui.ColorPicker(c, &m.picker, &m.color, "Accent")` opens a panel to pick one, `.Draft()` follows the drag before Apply and `.Applied()` says a choice was committed.
- A side panel: `ui.Drawer(c, &m.open, "Title", ui.DrawerSpec{Side: ui.DrawerLeft, Size: 320}, body, footer)`;
an empty title gives the body the whole panel, `NonModal` keeps the keyboard on the page, `Swipe` adds a
handle that a drag toward the edge closes it by (past half its size, or thrown).
Expand Down Expand Up @@ -564,6 +569,8 @@ ui.Row(c).Fill().Children(func() {
```

(`Splitter` in a `Column` divides top and bottom; the first pane then gets `Height(m.split.Pos)`.)
`.SplitterThickness(1)` makes the divider take one pixel of the layout, as a hairline between the panes, while the
pointer keeps the eight-pixel strip around it.

## Animation and time

Expand Down
303 changes: 303 additions & 0 deletions ui/color.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,303 @@
package ui

import (
"strconv"

"github.com/vivaui/viva/components"
"github.com/vivaui/viva/gfx"
"github.com/vivaui/viva/text"
)

// ColorSwatch adds a circular color choice, a radio button in a group of them: color is
// what it shows (a transparent color is "no color", drawn as a slash), selected rings
// it, and label names it for assistive technology. A click is Clicked; the application
// owns the choice. It is as wide and tall as its Size, 28 when it has none.
//
// if ui.ColorSwatch(c.Key(name), color, m.color == color, name).Clicked() { m.color = color }
func ColorSwatch(c C, color Color, selected bool, label string) Box {
return colorSwatch(c, c.page.controlID(pkRadio), color, selected, label)
}

// ColorSwatchID is ColorSwatch for a choice the application already names.
func ColorSwatchID(c C, id gfx.ID, color Color, selected bool, label string) Box {
return colorSwatch(c, id, color, selected, label)
}

func colorSwatch(c C, id gfx.ID, color Color, selected bool, label string) Box {
b := Slot(c).Size(28, 28).Shrink(0)
n := b.n()
n.id, n.label = id, label
b.Draw(func(d *DrawContext) {
extra := b.f.extra(b.i)
swatch := components.BeginColorSwatch(d.Canvas, *d.Theme, id, d.Rect, color).Label(label).Selected(selected).
TabStop(!extra.swatchSkipTab)
if extra.swatchFlush {
swatch = swatch.Flush()
}
swatch.Paint()
})
return b
}

// Flush makes a ColorSwatch fill its hairline border with its color instead of
// leaving a background-colored ring inside it, for a small swatch whose color
// would otherwise read as faded. Other elements ignore it.
func (b Box) Flush() Box {
b.n()
b.f.extra(b.i).swatchFlush = true
return b
}

// TabStop says whether Tab may enter a ColorSwatch: a radio set gives its chosen
// swatch the stop and the others false, as components.ColorSwatch's TabStop. Other
// elements ignore it.
func (b Box) TabStop(on bool) Box {
b.n()
b.f.extra(b.i).swatchSkipTab = !on
return b
}

// ColorPickerState is what a ColorPicker keeps between paints: whether its panel is open
// and the color being staged in it. Keep one per picker in the model; the zero value is
// closed.
type ColorPickerState struct {
open, restoreFocus, applied bool
draft components.ColorPickerValue
hexEditor text.Editor
hex Field
seen, summary string
invalid bool
handlers Handlers
layout components.ColorPickerLayout
target *Color
bound bool
}

// Open reports whether the picker's panel is open.
func (s *ColorPickerState) Open() bool { return s.open }

// Draft is the color being staged while the panel is open, which follows the plane, the
// hue bar and the hex field as they change and is not yet the application's: a preview
// that tracks the drag reads it. It reports false when the panel is closed.
func (s *ColorPickerState) Draft() (Color, bool) {
if !s.open {
return Color{}, false
}
return s.draft.Color(), true
}

// Applied reports whether the user applied (or cleared) a color since the last time it was
// asked, so the application can save it; it reads true once.
func (s *ColorPickerState) Applied() bool {
applied := s.applied
s.applied = false
return applied
}

// ColorPickerSpec is how a ColorPicker looks: Label names it for assistive technology
// (and heads its panel), Round makes the trigger a disc, Icon draws a glyph over it (a
// pipette for a custom color), Empty shows it as no color whatever its value, and
// Labels override the panel's words.
type ColorPickerSpec struct {
// ID, when set, names the trigger with the application's identity, as
// ColorSwatchID does; the page's own identity names it otherwise. A row's
// words can then be its LabelFor, as for any control the application names.
ID gfx.ID
Label string
Round bool
Icon *gfx.Icon
Empty bool
Labels components.ColorPickerLabels
}

// ColorPicker adds a color trigger over *color that opens a panel to pick one: a plane for
// saturation and brightness, a hue bar, a hex field and Clear, Cancel and Apply. The
// color is staged while the panel is open and written to *color on Apply (Enter in the
// hex field); Cancel, Escape and a press outside leave it as it was. A transparent
// *color is no color, which Clear sets. The box is the 32 pixel trigger; Applied
// reports a change.
//
// ui.ColorPicker(c, &m.picker, &m.accent, "Accent")
// if m.picker.Applied() { m.save() }
func ColorPicker(c C, state *ColorPickerState, color *Color, label string) Box {
return ColorPickerWith(c, state, ColorPickerSpec{Label: label}, color)
}

// ColorPickerWith is ColorPicker with a spec.
func ColorPickerWith(c C, s *ColorPickerState, spec ColorPickerSpec, color *Color) Box {
p := c.page
p.forbidInMemo("ColorPicker")
id := spec.ID
if id.IsZero() {
id = p.controlID(pkControl)
}
s.target = color
if !s.bound {
s.hex.Bind(&s.hexEditor)
s.handlers.Key, s.handlers.Pointer = s.key, s.pointer
s.bound = true
}
if s.open {
p.overlay = true
control := Control(c, spec.Label, &s.handlers).Absolute().Left(0).Top(0).Size(0, 0)
control.HitWith(s.handlers.parts.At(8), spec.Label, gfx.HitOptions{Role: gfx.RoleGroup, SkipTab: true, SkipFocus: true})
p.fields = append(p.fields, fieldEntry{id: s.handlers.parts.At(3), field: &s.hex})
s.hex.id = s.handlers.parts.At(3)
s.syncHex()
}
b := Slot(c).Size(32, 32).Shrink(0)
n := b.n()
n.id, n.label = id, spec.Label
b.DrawOver(func(d *DrawContext) { s.paint(d, c, id, spec) })
return b
}

// parts are the identities the panel's regions take.
func (s *ColorPickerState) ids(trigger gfx.ID) components.ColorPickerIDs {
if !s.open {
return components.ColorPickerIDs{Trigger: trigger}
}
at := s.handlers.parts.At
return components.ColorPickerIDs{Trigger: trigger, Saturation: at(0), Hue: at(1), Hex: at(3), Clear: at(4), Cancel: at(5), Apply: at(6), Dismiss: at(7)}
}

func (s *ColorPickerState) paint(d *DrawContext, c C, id gfx.ID, spec ColorPickerSpec) {
canvas := d.Canvas
ids := s.ids(id)
if canvas.Clicked(id) {
if s.open {
s.close()
} else {
s.openPanel(spec)
}
return
}
if s.restoreFocus {
canvas.RequestFocus(id)
s.restoreFocus = false
}
labels := spec.Labels
if labels.Color == "" {
labels.Color = spec.Label
}
value, empty := s.draft, false
if !s.open {
value = value.WithColor(*s.target)
empty = s.target.Alpha == 0
}
empty = empty || spec.Empty
options := components.ColorPickerOptions{
IDs: ids, Labels: labels, Value: value, Open: s.open, Empty: empty,
RoundTrigger: spec.Round, TriggerIcon: spec.Icon, SaturationText: s.summary,
Viewport: Rect{Width: c.Bounds().Width, Height: c.Bounds().Height}, Layer: c.f.widgetLayer() + 2,
}
if s.open {
options.Scope = s.handlers.parts.At(8)
options.Hex = s.hex.snapshot(c.f, ids.Hex, labels.Hex, "#3B82F6")
if s.invalid {
options.Error = "Invalid color"
}
}
picker := components.BeginColorPicker(canvas, *d.Theme, d.Rect, options)
picker.PaintTrigger()
if !s.open {
return
}
s.layout = picker.PaintPanel()
switch {
case canvas.Clicked(ids.Dismiss), canvas.Clicked(ids.Cancel):
s.close()
case canvas.Clicked(ids.Clear):
*s.target, s.applied = Color{}, true
s.close()
case canvas.Clicked(ids.Apply):
s.apply()
}
}

func (s *ColorPickerState) openPanel(ColorPickerSpec) {
color := *s.target
if color.Alpha == 0 {
color = Color{Red: 0.231, Green: 0.51, Blue: 0.965, Alpha: 1} // #3B82F6, as the field's placeholder
}
s.setValue(components.ColorPickerValue{}.WithColor(color))
s.open = true
}

func (s *ColorPickerState) close() { s.open, s.restoreFocus = false, true }

func (s *ColorPickerState) apply() {
s.syncHex()
if s.invalid {
return
}
*s.target, s.applied = s.draft.Color(), true
s.close()
}

func (s *ColorPickerState) setValue(v components.ColorPickerValue) {
s.draft = v
var buf [9]byte
value := string(v.AppendHex(buf[:0], false))
if s.hexEditor.String() != value {
s.hexEditor = text.Editor{}
s.hexEditor.Insert(value)
}
s.seen, s.invalid = value, false
s.summary = summary(v)
}

func summary(v components.ColorPickerValue) string {
return strconv.Itoa(int(v.Saturation*100+.5)) + "% / " + strconv.Itoa(int(v.Brightness*100+.5)) + "%"
}

// syncHex takes what was typed in the hex field as the staged color, when it is one.
func (s *ColorPickerState) syncHex() {
value := s.hexEditor.String()
if value == s.seen {
return
}
s.seen = value
color, valid := components.ParseColorHex(value, false)
s.invalid = !valid
if valid {
s.draft = s.draft.WithColor(color)
s.summary = summary(s.draft)
}
}

func (s *ColorPickerState) pointer(event gfx.PointerEvent) gfx.EventResult {
if !s.open {
return gfx.EventResult{}
}
value, result := s.layout.Pointer(event, s.draft)
if value != s.draft {
s.setValue(value)
}
return result
}

func (s *ColorPickerState) key(event gfx.KeyEvent) gfx.EventResult {
if !s.open || event.Release {
return gfx.EventResult{}
}
hex := s.handlers.parts.At(3)
if event.Key == gfx.KeyEscape || event.Key == gfx.KeyEnter && event.Target == hex && event.Modifiers == 0 {
if _, composing := s.hexEditor.Composition(); composing {
return gfx.EventResult{}
}
if !event.Repeat {
if event.Key == gfx.KeyEscape {
s.close()
} else {
s.apply()
}
}
return gfx.EventResult{Handled: true, Repaint: true}
}
value, result := s.layout.Key(event, s.draft)
if value != s.draft {
s.setValue(value)
}
return result
}
Loading
Loading