Skip to content
Closed
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
2 changes: 2 additions & 0 deletions base/state_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ type ColorPickerIDs struct {

type ColorPickerLabels struct {
Color, Saturation, Hue, Alpha, Hex, Clear, Cancel, Apply string
// InvalidColor is the message shown for an invalid Hex value.
InvalidColor string
}

// ColorPickerLayout is also the input geometry. Pointer and Key update a copy
Expand Down
5 changes: 4 additions & 1 deletion ui/color_paint.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@ func paintColorPickerPanel(s *ColorPickerState, d *DrawContext, c C, ID gfx.ID,
options.Scope = s.GetHandlers().GetParts().At(8)
options.Hex = s.HexPtr().Snapshot(c.GetF(), ids.Hex, labels.Hex, "#3B82F6")
if s.GetInvalid() {
options.Error = "Invalid color"
options.Error = labels.InvalidColor
if options.Error == "" {
options.Error = "Invalid color"
}
}
}
picker := components.BeginColorPicker(canvas, *d.Theme, d.Rect, options)
Expand Down
35 changes: 35 additions & 0 deletions ui/color_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ui_test
import (
"testing"

"github.com/vivaui/viva/base"
"github.com/vivaui/viva/gfx"
"github.com/vivaui/viva/ui"
)
Expand Down Expand Up @@ -62,6 +63,40 @@ func TestColorPickerAppliesATypedHex(t *testing.T) {
}
}

func TestColorPickerInvalidHexUsesPanelLabel(t *testing.T) {
for _, tc := range []struct{ name, label, want string }{
{"localized", "颜色格式无效", "颜色格式无效"},
{"default", "", "Invalid color"},
} {
t.Run(tc.name, func(t *testing.T) {
var state ui.ColorPickerState
original := gfx.RGB(10, 20, 30)
color := original
d := openPage(t, func(c ui.C) {
ui.Column(c).Pad(16).Children(func() {
ui.ColorPicker(c, &state, &color, "Accent").PanelLabels(base.ColorPickerLabels{InvalidColor: tc.label})
})
})
d.click("Accent")
d.click("Hex color")
state.SetHexText("#wrong")
d.click("OK")
if !state.Open() || state.Applied() || color != original {
t.Fatal("invalid input changed or applied the color")
}
found := false
for _, label := range d.driver.Snapshot().Labels {
if label.Value == tc.want {
found = true
}
}
if !found {
t.Fatalf("panel did not paint validation message %q", tc.want)
}
})
}
}

func TestColorPickerCancelAndEscapeKeepTheColor(t *testing.T) {
var state ui.ColorPickerState
color := gfx.RGB(10, 20, 30)
Expand Down