From 989da6b32c6e3bb4de154b3d7fb419be4072b4b9 Mon Sep 17 00:00:00 2001 From: mirakyux Date: Wed, 25 Nov 2026 20:58:20 +0800 Subject: [PATCH 1/2] fix(ui): localize color picker validation errors --- base/state_types.go | 2 ++ ui/color_paint.go | 5 ++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/base/state_types.go b/base/state_types.go index b8b06671c..6c9fa65cf 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 e92c92a5a..6c21c4303 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) From d8c0a63cb26c2f0dcc5198ee9dd49ba41e8d9ebe Mon Sep 17 00:00:00 2001 From: mirakyux Date: Thu, 26 Nov 2026 08:23:48 +0800 Subject: [PATCH 2/2] test(ui): cover custom invalid color validation labels --- ui/color_test.go | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/ui/color_test.go b/ui/color_test.go index b43519961..afc640554 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)