| 1 | |
| 2 | |
| 3 | |
| 4 | use crate::*; |
| 5 | use crate::player::text_field::{FieldKey, TextField}; |
| 6 | use crate::ui::theme; |
| 7 | |
| 8 | |
| 9 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 10 | pub(crate) enum AccentSlider { |
| 11 | Vivid, |
| 12 | Hue, |
| 13 | } |
| 14 | |
| 15 | |
| 16 | pub(crate) fn vivid_at(along: f32) -> u8 { |
| 17 | (along.clamp(0., 1.) * 100.).round() as u8 |
| 18 | } |
| 19 | |
| 20 | |
| 21 | |
| 22 | pub(crate) fn recoloured(rgb: u32, along: f32) -> u32 { |
| 23 | let (l, c, _) = theme::to_oklch(rgb); |
| 24 | let (l, c) = match c < 0.02 { |
| 25 | true => (0.78, 0.09), |
| 26 | false => (l, c), |
| 27 | }; |
| 28 | theme::from_oklch(l, c, f64::from(along.clamp(0., 1.)) * 359.) |
| 29 | } |
| 30 | |
| 31 | impl Player { |
| 32 | |
| 33 | pub(crate) fn keep_accent(&mut self) { |
| 34 | if let Err(e) = theme::save_accent() { |
| 35 | let path = theme::accent_path(); |
| 36 | self.notify_user(format!("Accent colour could not be kept — {} — {e}", path.display()).into()); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | |
| 41 | pub(crate) fn drag_accent(&mut self, slider: AccentSlider, x: Pixels, cx: &mut Context<Self>) { |
| 42 | match slider { |
| 43 | AccentSlider::Vivid => theme::set_vivid(vivid_at(frac_along(x, self.accent_vivid_bar.get()))), |
| 44 | AccentSlider::Hue => { |
| 45 | let now = theme::custom_colour().unwrap_or(theme::CUSTOM_DEFAULT); |
| 46 | theme::set_custom(recoloured(now, frac_along(x, self.accent_hue_bar.get()))); |
| 47 | } |
| 48 | } |
| 49 | cx.notify(); |
| 50 | } |
| 51 | |
| 52 | |
| 53 | pub(crate) fn end_accent_drag(&mut self) { |
| 54 | if self.accent_drag.take().is_some() { |
| 55 | self.keep_accent(); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | |
| 60 | pub(crate) fn begin_accent_hex(&mut self, cx: &mut Context<Self>) { |
| 61 | let now = theme::custom_colour().map_or(String::new(), |c| format!("#{c:06x}")); |
| 62 | self.accent_hex = Some(TextField::selected(now)); |
| 63 | cx.notify(); |
| 64 | } |
| 65 | |
| 66 | |
| 67 | |
| 68 | pub(crate) fn commit_accent_hex(&mut self, cx: &mut Context<Self>) { |
| 69 | if self.accent_hex.take().is_some() { |
| 70 | self.keep_accent(); |
| 71 | cx.notify(); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | |
| 76 | pub(crate) fn accent_key(&mut self, key: &str, ctrl: bool, alt: bool, key_char: Option<&str>, cx: &mut Context<Self>) { |
| 77 | let Some(field) = self.accent_hex.as_mut() else { return }; |
| 78 | let result = field.key(key, ctrl, alt, key_char, 7); |
| 79 | field.text.retain(|c| c.is_ascii_alphanumeric() || c == '#'); |
| 80 | let typed = theme::parse_hex(&field.text); |
| 81 | match result { |
| 82 | FieldKey::Commit | FieldKey::Cancel => self.commit_accent_hex(cx), |
| 83 | FieldKey::Edited => { |
| 84 | if let Some(rgb) = typed { |
| 85 | theme::set_custom(rgb); |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | cx.notify(); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | #[cfg(test)] |
| 94 | mod tests { |
| 95 | use super::*; |
| 96 | |
| 97 | #[test] |
| 98 | fn vivid_follows_the_track_and_hue_keeps_its_colour_family() { |
| 99 | assert_eq!((vivid_at(0.), vivid_at(0.5), vivid_at(1.), vivid_at(7.)), (0, 50, 100, 100)); |
| 100 | let turned = recoloured(0xff4d8d, 0.6); |
| 101 | assert_ne!(turned, 0xff4d8d); |
| 102 | assert_ne!(recoloured(0x808080, 0.3), 0x808080); |
| 103 | } |
| 104 | } |