Skip to content

FilmCraft: An open-source, nonlinear video editor written by scratch in pure Rust

settings: the hex colour field takes typing

ReReddoc8accc4946a0
diff --git a/crates/ui-egui/src/panels/settings.rs b/crates/ui-egui/src/panels/settings.rs
index 41395d861..e09609cfa 100644
--- a/crates/ui-egui/src/panels/settings.rs
+++ b/crates/ui-egui/src/panels/settings.rs
@@ -534,11 +534,21 @@ fn color_field(app: &mut FilmcraftApp, ui: &mut Ui, d: &mut SettingsDraft, key:
if r.changed() {
put(&mut d.values, key, json!(settings::hex(rgb)));
}
- let mut text = choice_text(get(&d.values, key));
+ // While the field has focus it shows what is typed, even when that is not a colour yet (one
+ // character into `#2f6bdf`); each valid colour is kept as it comes. Leaving shows the kept one.
+ let draft_id = egui::Id::new(("settings-hex", key));
+ let mut text = ui.data(|m| m.get_temp::<String>(draft_id)).unwrap_or_else(|| choice_text(get(&d.values, key)));
let t = ui.add(egui::TextEdit::singleline(&mut text).desired_width(70.0).font(egui::TextStyle::Monospace));
app.auto.add(&format!("settings.{key}.hex"), t.rect, label);
- if t.changed() && settings::parse_hex(&text).is_some() {
- put(&mut d.values, key, json!(settings::hex(settings::parse_hex(&text).unwrap_or(rgb))));
+ if t.has_focus() {
+ ui.data_mut(|m| m.insert_temp(draft_id, text.clone()));
+ } else {
+ ui.data_mut(|m| m.remove::<String>(draft_id));
+ }
+ if t.changed()
+ && let Some(c) = settings::parse_hex(&text)
+ {
+ put(&mut d.values, key, json!(settings::hex(c)));
}
}
diff --git a/crates/ui-egui/tests/settings_ui.rs b/crates/ui-egui/tests/settings_ui.rs
index 3d19aaaff..eca13d504 100644
--- a/crates/ui-egui/tests/settings_ui.rs
+++ b/crates/ui-egui/tests/settings_ui.rs
@@ -333,6 +333,31 @@ fn appearance_labels_and_tooltips_take_effect() {
assert!(d.harness.ctx.global_style().interaction.tooltip_delay < 1.0);
}
+#[test]
+fn hex_field_takes_typing_a_character_at_a_time() {
+ let mut d = Driver::demo();
+ d.menu("app.settings.appearance");
+ let hex = |d: &mut Driver| d.draft()["values"]["appearance"]["highlightColor"].clone();
+ let before = hex(&mut d);
+ d.ok("ui.click", json!({"id": "settings.appearance.highlightColor.hex"}));
+ d.frames(2);
+ d.ok("ui.key", json!({"key": "Cmd+A"}));
+ // not a colour yet: the field keeps it and the setting stays
+ d.ok("ui.type", json!({"text": "#12ab"}));
+ d.frames(2);
+ assert_eq!(hex(&mut d), before);
+ d.ok("ui.type", json!({"text": "34"}));
+ d.frames(2);
+ assert_eq!(hex(&mut d), "#12ab34");
+ // deleting a character leaves the last colour
+ d.ok("ui.key", json!({"key": "Backspace"}));
+ d.frames(2);
+ assert_eq!(hex(&mut d), "#12ab34");
+ d.ok("ui.type", json!({"text": "5"}));
+ d.frames(2);
+ assert_eq!(hex(&mut d), "#12ab35");
+}
+
#[test]
fn memory_and_media_cache_pages() {
let mut d = Driver::demo();