From 9d02133977eda993b23387dc1d86a177721ebccd Mon Sep 17 00:00:00 2001 From: Floyd Wang Date: Tue, 25 Nov 2025 18:05:43 +0800 Subject: [PATCH] color_picker: Only change value when it's confirmed (#1682) --- crates/ui/src/color_picker.rs | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/crates/ui/src/color_picker.rs b/crates/ui/src/color_picker.rs index a3383976..4683ab76 100644 --- a/crates/ui/src/color_picker.rs +++ b/crates/ui/src/color_picker.rs @@ -80,7 +80,6 @@ impl ColorPickerState { InputEvent::Change => { let value = state.read(cx).value(); if let Ok(color) = Hsla::parse_hex(value.as_str()) { - this.value = Some(color); this.hovered_color = Some(color); } } @@ -127,12 +126,21 @@ impl ColorPickerState { self.value } - fn on_escape(&mut self, _: &Cancel, _: &mut Window, cx: &mut Context) { + fn on_escape(&mut self, _: &Cancel, window: &mut Window, cx: &mut Context) { if !self.open { cx.propagate(); } self.open = false; + if self.hovered_color != self.value { + let color = self.value; + self.hovered_color = color; + if let Some(color) = color { + self.state.update(cx, |input, cx| { + input.set_value(color.to_hex(), window, cx); + }); + } + } cx.notify(); } @@ -302,6 +310,18 @@ impl ColorPicker { ]); let state = self.state.clone(); + // If the input value is empty, fill it with the current value. + let input_value = state.read(cx).state.read(cx).value(); + if input_value.is_empty() + && let Some(value) = state.read(cx).value + { + state.update(cx, |state, cx| { + state.state.update(cx, |input, cx| { + input.set_value(value.to_hex(), window, cx); + }); + }); + } + v_flex() .gap_3() .child(