forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiBff99ad6Tahinli6 days ago1055 commits

accent.rs104 lines, 3.8 KiBHistoryRawDownload
1//! The accent colour on the window's side: Vivid and the hue slider's drags, the Custom hex being
2//! typed, and keeping what is in force ([`crate::ui::theme`] owns the colours themselves).
3
4use crate::*;
5use crate::player::text_field::{FieldKey, TextField};
6use crate::ui::theme;
7
8/// Which accent slider a hand is on.
9#[derive(Clone, Copy, Debug, PartialEq, Eq)]
10pub(crate) enum AccentSlider {
11 Vivid,
12 Hue,
13}
14
15/// Vivid under a press at `along` (0..1 of the track).
16pub(crate) fn vivid_at(along: f32) -> u8 {
17 (along.clamp(0., 1.) * 100.).round() as u8
18}
19
20/// The custom colour with its hue turned to `along` of the wheel. A grey has no hue to turn, so it
21/// starts from a soft mid colour instead.
22pub(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
31impl Player {
32 /// Writes the accent in force to the config file; a failure is said once.
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 /// A press or drag sample on an accent slider, live; the file is written when the hand lets go.
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 /// The hand let go of an accent slider.
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 /// A press on the hex field: it opens for typing with all of it selected.
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 /// Leaves the hex field. The last good colour is already in force (every valid edit applies at
67 /// once), so an invalid text is simply dropped.
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 /// The keyboard while the hex is being typed: enter keeps, escape leaves.
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)]
94mod 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}