diff --git a/base/state_types.go b/base/state_types.go index b8b06671..6c9fa65c 100644 --- a/base/state_types.go +++ b/base/state_types.go @@ -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 diff --git a/ui/color_paint.go b/ui/color_paint.go index e92c92a5..6c21c430 100644 --- a/ui/color_paint.go +++ b/ui/color_paint.go @@ -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) diff --git a/ui/color_test.go b/ui/color_test.go index b4351996..afc64055 100644 --- a/ui/color_test.go +++ b/ui/color_test.go @@ -3,6 +3,7 @@ package ui_test import ( "testing" + "github.com/vivaui/viva/base" "github.com/vivaui/viva/gfx" "github.com/vivaui/viva/ui" ) @@ -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)