forge
Explore
Log in

Don't you dare go Hollow

config: Extract appearance and layout

Browse files at this commit

02e3f9f66ee71ce5252df3bb7b7c9743e95fdcbcIvan MolodetskikhParent: 19c576c

5 changed files +1287 -1264

niri-config/src/appearance.rsadded+1143 -0
@@ -0,0 +1,1143 @@
1+use std::ops::{Mul, MulAssign};
2+use std::str::FromStr;
3+
4+use knuffel::errors::DecodeError;
5+use miette::{miette, IntoDiagnostic as _};
6+use smithay::backend::renderer::Color32F;
7+
8+use crate::FloatOrInt;
9+
10+pub const DEFAULT_BACKGROUND_COLOR: Color = Color::from_array_unpremul([0.25, 0.25, 0.25, 1.]);
11+pub const DEFAULT_BACKDROP_COLOR: Color = Color::from_array_unpremul([0.15, 0.15, 0.15, 1.]);
12+
13+/// RGB color in [0, 1] with unpremultiplied alpha.
14+#[derive(Debug, Default, Clone, Copy, PartialEq)]
15+pub struct Color {
16+ pub r: f32,
17+ pub g: f32,
18+ pub b: f32,
19+ pub a: f32,
20+}
21+
22+impl Color {
23+ pub const fn new_unpremul(r: f32, g: f32, b: f32, a: f32) -> Self {
24+ Self { r, g, b, a }
25+ }
26+
27+ pub fn from_rgba8_unpremul(r: u8, g: u8, b: u8, a: u8) -> Self {
28+ Self::from_array_unpremul([r, g, b, a].map(|x| x as f32 / 255.))
29+ }
30+
31+ pub fn from_array_premul([r, g, b, a]: [f32; 4]) -> Self {
32+ let a = a.clamp(0., 1.);
33+
34+ if a == 0. {
35+ Self::new_unpremul(0., 0., 0., 0.)
36+ } else {
37+ Self {
38+ r: (r / a).clamp(0., 1.),
39+ g: (g / a).clamp(0., 1.),
40+ b: (b / a).clamp(0., 1.),
41+ a,
42+ }
43+ }
44+ }
45+
46+ pub const fn from_array_unpremul([r, g, b, a]: [f32; 4]) -> Self {
47+ Self { r, g, b, a }
48+ }
49+
50+ pub fn from_color32f(color: Color32F) -> Self {
51+ Self::from_array_premul(color.components())
52+ }
53+
54+ pub fn to_array_unpremul(self) -> [f32; 4] {
55+ [self.r, self.g, self.b, self.a]
56+ }
57+
58+ pub fn to_array_premul(self) -> [f32; 4] {
59+ let [r, g, b, a] = [self.r, self.g, self.b, self.a];
60+ [r * a, g * a, b * a, a]
61+ }
62+}
63+
64+impl Mul<f32> for Color {
65+ type Output = Self;
66+
67+ fn mul(mut self, rhs: f32) -> Self::Output {
68+ self.a *= rhs;
69+ self
70+ }
71+}
72+
73+impl MulAssign<f32> for Color {
74+ fn mul_assign(&mut self, rhs: f32) {
75+ self.a *= rhs;
76+ }
77+}
78+
79+#[derive(knuffel::Decode, Debug, Clone, Copy, PartialEq)]
80+pub struct Gradient {
81+ #[knuffel(property, str)]
82+ pub from: Color,
83+ #[knuffel(property, str)]
84+ pub to: Color,
85+ #[knuffel(property, default = 180)]
86+ pub angle: i16,
87+ #[knuffel(property, default)]
88+ pub relative_to: GradientRelativeTo,
89+ #[knuffel(property(name = "in"), str, default)]
90+ pub in_: GradientInterpolation,
91+}
92+
93+impl From<Color> for Gradient {
94+ fn from(value: Color) -> Self {
95+ Self {
96+ from: value,
97+ to: value,
98+ angle: 0,
99+ relative_to: GradientRelativeTo::Window,
100+ in_: GradientInterpolation::default(),
101+ }
102+ }
103+}
104+
105+#[derive(knuffel::DecodeScalar, Debug, Default, Clone, Copy, PartialEq, Eq)]
106+pub enum GradientRelativeTo {
107+ #[default]
108+ Window,
109+ WorkspaceView,
110+}
111+
112+#[derive(Default, Debug, Clone, Copy, PartialEq)]
113+pub struct GradientInterpolation {
114+ pub color_space: GradientColorSpace,
115+ pub hue_interpolation: HueInterpolation,
116+}
117+
118+#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
119+pub enum GradientColorSpace {
120+ #[default]
121+ Srgb,
122+ SrgbLinear,
123+ Oklab,
124+ Oklch,
125+}
126+
127+#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
128+pub enum HueInterpolation {
129+ #[default]
130+ Shorter,
131+ Longer,
132+ Increasing,
133+ Decreasing,
134+}
135+
136+#[derive(Debug, Default, Clone, Copy, PartialEq)]
137+pub struct CornerRadius {
138+ pub top_left: f32,
139+ pub top_right: f32,
140+ pub bottom_right: f32,
141+ pub bottom_left: f32,
142+}
143+
144+impl From<CornerRadius> for [f32; 4] {
145+ fn from(value: CornerRadius) -> Self {
146+ [
147+ value.top_left,
148+ value.top_right,
149+ value.bottom_right,
150+ value.bottom_left,
151+ ]
152+ }
153+}
154+
155+impl From<f32> for CornerRadius {
156+ fn from(value: f32) -> Self {
157+ Self {
158+ top_left: value,
159+ top_right: value,
160+ bottom_right: value,
161+ bottom_left: value,
162+ }
163+ }
164+}
165+
166+impl CornerRadius {
167+ pub fn fit_to(self, width: f32, height: f32) -> Self {
168+ // Like in CSS: https://drafts.csswg.org/css-backgrounds/#corner-overlap
169+ let reduction = f32::min(
170+ f32::min(
171+ width / (self.top_left + self.top_right),
172+ width / (self.bottom_left + self.bottom_right),
173+ ),
174+ f32::min(
175+ height / (self.top_left + self.bottom_left),
176+ height / (self.top_right + self.bottom_right),
177+ ),
178+ );
179+ let reduction = f32::min(1., reduction);
180+
181+ Self {
182+ top_left: self.top_left * reduction,
183+ top_right: self.top_right * reduction,
184+ bottom_right: self.bottom_right * reduction,
185+ bottom_left: self.bottom_left * reduction,
186+ }
187+ }
188+
189+ pub fn expanded_by(mut self, width: f32) -> Self {
190+ // Radius = 0 is preserved, so that square corners remain square.
191+ if self.top_left > 0. {
192+ self.top_left += width;
193+ }
194+ if self.top_right > 0. {
195+ self.top_right += width;
196+ }
197+ if self.bottom_right > 0. {
198+ self.bottom_right += width;
199+ }
200+ if self.bottom_left > 0. {
201+ self.bottom_left += width;
202+ }
203+
204+ if width < 0. {
205+ self.top_left = self.top_left.max(0.);
206+ self.top_right = self.top_right.max(0.);
207+ self.bottom_left = self.bottom_left.max(0.);
208+ self.bottom_right = self.bottom_right.max(0.);
209+ }
210+
211+ self
212+ }
213+
214+ pub fn scaled_by(self, scale: f32) -> Self {
215+ Self {
216+ top_left: self.top_left * scale,
217+ top_right: self.top_right * scale,
218+ bottom_right: self.bottom_right * scale,
219+ bottom_left: self.bottom_left * scale,
220+ }
221+ }
222+}
223+
224+#[derive(knuffel::Decode, Debug, Clone, Copy, PartialEq)]
225+pub struct FocusRing {
226+ #[knuffel(child)]
227+ pub off: bool,
228+ #[knuffel(child, unwrap(argument), default = Self::default().width)]
229+ pub width: FloatOrInt<0, 65535>,
230+ #[knuffel(child, default = Self::default().active_color)]
231+ pub active_color: Color,
232+ #[knuffel(child, default = Self::default().inactive_color)]
233+ pub inactive_color: Color,
234+ #[knuffel(child, default = Self::default().urgent_color)]
235+ pub urgent_color: Color,
236+ #[knuffel(child)]
237+ pub active_gradient: Option<Gradient>,
238+ #[knuffel(child)]
239+ pub inactive_gradient: Option<Gradient>,
240+ #[knuffel(child)]
241+ pub urgent_gradient: Option<Gradient>,
242+}
243+
244+impl Default for FocusRing {
245+ fn default() -> Self {
246+ Self {
247+ off: false,
248+ width: FloatOrInt(4.),
249+ active_color: Color::from_rgba8_unpremul(127, 200, 255, 255),
250+ inactive_color: Color::from_rgba8_unpremul(80, 80, 80, 255),
251+ urgent_color: Color::from_rgba8_unpremul(155, 0, 0, 255),
252+ active_gradient: None,
253+ inactive_gradient: None,
254+ urgent_gradient: None,
255+ }
256+ }
257+}
258+
259+#[derive(knuffel::Decode, Debug, Clone, Copy, PartialEq)]
260+pub struct Border {
261+ #[knuffel(child)]
262+ pub off: bool,
263+ #[knuffel(child, unwrap(argument), default = Self::default().width)]
264+ pub width: FloatOrInt<0, 65535>,
265+ #[knuffel(child, default = Self::default().active_color)]
266+ pub active_color: Color,
267+ #[knuffel(child, default = Self::default().inactive_color)]
268+ pub inactive_color: Color,
269+ #[knuffel(child, default = Self::default().urgent_color)]
270+ pub urgent_color: Color,
271+ #[knuffel(child)]
272+ pub active_gradient: Option<Gradient>,
273+ #[knuffel(child)]
274+ pub inactive_gradient: Option<Gradient>,
275+ #[knuffel(child)]
276+ pub urgent_gradient: Option<Gradient>,
277+}
278+
279+impl Default for Border {
280+ fn default() -> Self {
281+ Self {
282+ off: true,
283+ width: FloatOrInt(4.),
284+ active_color: Color::from_rgba8_unpremul(255, 200, 127, 255),
285+ inactive_color: Color::from_rgba8_unpremul(80, 80, 80, 255),
286+ urgent_color: Color::from_rgba8_unpremul(155, 0, 0, 255),
287+ active_gradient: None,
288+ inactive_gradient: None,
289+ urgent_gradient: None,
290+ }
291+ }
292+}
293+
294+impl From<Border> for FocusRing {
295+ fn from(value: Border) -> Self {
296+ Self {
297+ off: value.off,
298+ width: value.width,
299+ active_color: value.active_color,
300+ inactive_color: value.inactive_color,
301+ urgent_color: value.urgent_color,
302+ active_gradient: value.active_gradient,
303+ inactive_gradient: value.inactive_gradient,
304+ urgent_gradient: value.urgent_gradient,
305+ }
306+ }
307+}
308+
309+impl From<FocusRing> for Border {
310+ fn from(value: FocusRing) -> Self {
311+ Self {
312+ off: value.off,
313+ width: value.width,
314+ active_color: value.active_color,
315+ inactive_color: value.inactive_color,
316+ urgent_color: value.urgent_color,
317+ active_gradient: value.active_gradient,
318+ inactive_gradient: value.inactive_gradient,
319+ urgent_gradient: value.urgent_gradient,
320+ }
321+ }
322+}
323+
324+#[derive(knuffel::Decode, Debug, Clone, Copy, PartialEq)]
325+pub struct Shadow {
326+ #[knuffel(child)]
327+ pub on: bool,
328+ #[knuffel(child, default = Self::default().offset)]
329+ pub offset: ShadowOffset,
330+ #[knuffel(child, unwrap(argument), default = Self::default().softness)]
331+ pub softness: FloatOrInt<0, 1024>,
332+ #[knuffel(child, unwrap(argument), default = Self::default().spread)]
333+ pub spread: FloatOrInt<-1024, 1024>,
334+ #[knuffel(child, unwrap(argument), default = Self::default().draw_behind_window)]
335+ pub draw_behind_window: bool,
336+ #[knuffel(child, default = Self::default().color)]
337+ pub color: Color,
338+ #[knuffel(child)]
339+ pub inactive_color: Option<Color>,
340+}
341+
342+impl Default for Shadow {
343+ fn default() -> Self {
344+ Self {
345+ on: false,
346+ offset: ShadowOffset {
347+ x: FloatOrInt(0.),
348+ y: FloatOrInt(5.),
349+ },
350+ softness: FloatOrInt(30.),
351+ spread: FloatOrInt(5.),
352+ draw_behind_window: false,
353+ color: Color::from_rgba8_unpremul(0, 0, 0, 0x70),
354+ inactive_color: None,
355+ }
356+ }
357+}
358+
359+#[derive(knuffel::Decode, Debug, Clone, Copy, PartialEq)]
360+pub struct ShadowOffset {
361+ #[knuffel(property, default)]
362+ pub x: FloatOrInt<-65535, 65535>,
363+ #[knuffel(property, default)]
364+ pub y: FloatOrInt<-65535, 65535>,
365+}
366+
367+#[derive(knuffel::Decode, Debug, Clone, Copy, PartialEq)]
368+pub struct WorkspaceShadow {
369+ #[knuffel(child)]
370+ pub off: bool,
371+ #[knuffel(child, default = Self::default().offset)]
372+ pub offset: ShadowOffset,
373+ #[knuffel(child, unwrap(argument), default = Self::default().softness)]
374+ pub softness: FloatOrInt<0, 1024>,
375+ #[knuffel(child, unwrap(argument), default = Self::default().spread)]
376+ pub spread: FloatOrInt<-1024, 1024>,
377+ #[knuffel(child, default = Self::default().color)]
378+ pub color: Color,
379+}
380+
381+impl Default for WorkspaceShadow {
382+ fn default() -> Self {
383+ Self {
384+ off: false,
385+ offset: ShadowOffset {
386+ x: FloatOrInt(0.),
387+ y: FloatOrInt(10.),
388+ },
389+ softness: FloatOrInt(40.),
390+ spread: FloatOrInt(10.),
391+ color: Color::from_rgba8_unpremul(0, 0, 0, 0x50),
392+ }
393+ }
394+}
395+
396+impl From<WorkspaceShadow> for Shadow {
397+ fn from(value: WorkspaceShadow) -> Self {
398+ Self {
399+ on: !value.off,
400+ offset: value.offset,
401+ softness: value.softness,
402+ spread: value.spread,
403+ draw_behind_window: false,
404+ color: value.color,
405+ inactive_color: None,
406+ }
407+ }
408+}
409+
410+#[derive(knuffel::Decode, Debug, Clone, Copy, PartialEq)]
411+pub struct TabIndicator {
412+ #[knuffel(child)]
413+ pub off: bool,
414+ #[knuffel(child)]
415+ pub hide_when_single_tab: bool,
416+ #[knuffel(child)]
417+ pub place_within_column: bool,
418+ #[knuffel(child, unwrap(argument), default = Self::default().gap)]
419+ pub gap: FloatOrInt<-65535, 65535>,
420+ #[knuffel(child, unwrap(argument), default = Self::default().width)]
421+ pub width: FloatOrInt<0, 65535>,
422+ #[knuffel(child, default = Self::default().length)]
423+ pub length: TabIndicatorLength,
424+ #[knuffel(child, unwrap(argument), default = Self::default().position)]
425+ pub position: TabIndicatorPosition,
426+ #[knuffel(child, unwrap(argument), default = Self::default().gaps_between_tabs)]
427+ pub gaps_between_tabs: FloatOrInt<0, 65535>,
428+ #[knuffel(child, unwrap(argument), default = Self::default().corner_radius)]
429+ pub corner_radius: FloatOrInt<0, 65535>,
430+ #[knuffel(child)]
431+ pub active_color: Option<Color>,
432+ #[knuffel(child)]
433+ pub inactive_color: Option<Color>,
434+ #[knuffel(child)]
435+ pub urgent_color: Option<Color>,
436+ #[knuffel(child)]
437+ pub active_gradient: Option<Gradient>,
438+ #[knuffel(child)]
439+ pub inactive_gradient: Option<Gradient>,
440+ #[knuffel(child)]
441+ pub urgent_gradient: Option<Gradient>,
442+}
443+
444+impl Default for TabIndicator {
445+ fn default() -> Self {
446+ Self {
447+ off: false,
448+ hide_when_single_tab: false,
449+ place_within_column: false,
450+ gap: FloatOrInt(5.),
451+ width: FloatOrInt(4.),
452+ length: TabIndicatorLength {
453+ total_proportion: Some(0.5),
454+ },
455+ position: TabIndicatorPosition::Left,
456+ gaps_between_tabs: FloatOrInt(0.),
457+ corner_radius: FloatOrInt(0.),
458+ active_color: None,
459+ inactive_color: None,
460+ urgent_color: None,
461+ active_gradient: None,
462+ inactive_gradient: None,
463+ urgent_gradient: None,
464+ }
465+ }
466+}
467+
468+#[derive(knuffel::Decode, Debug, Clone, Copy, PartialEq)]
469+pub struct TabIndicatorLength {
470+ #[knuffel(property)]
471+ pub total_proportion: Option<f64>,
472+}
473+
474+#[derive(knuffel::DecodeScalar, Debug, Clone, Copy, PartialEq)]
475+pub enum TabIndicatorPosition {
476+ Left,
477+ Right,
478+ Top,
479+ Bottom,
480+}
481+
482+#[derive(knuffel::Decode, Debug, Clone, Copy, PartialEq)]
483+pub struct InsertHint {
484+ #[knuffel(child)]
485+ pub off: bool,
486+ #[knuffel(child, default = Self::default().color)]
487+ pub color: Color,
488+ #[knuffel(child)]
489+ pub gradient: Option<Gradient>,
490+}
491+
492+impl Default for InsertHint {
493+ fn default() -> Self {
494+ Self {
495+ off: false,
496+ color: Color::from_rgba8_unpremul(127, 200, 255, 128),
497+ gradient: None,
498+ }
499+ }
500+}
501+
502+#[derive(knuffel::DecodeScalar, Debug, Clone, Copy, PartialEq, Eq)]
503+pub enum BlockOutFrom {
504+ Screencast,
505+ ScreenCapture,
506+}
507+
508+#[derive(knuffel::Decode, Debug, Default, Clone, Copy, PartialEq)]
509+pub struct BorderRule {
510+ #[knuffel(child)]
511+ pub off: bool,
512+ #[knuffel(child)]
513+ pub on: bool,
514+ #[knuffel(child, unwrap(argument))]
515+ pub width: Option<FloatOrInt<0, 65535>>,
516+ #[knuffel(child)]
517+ pub active_color: Option<Color>,
518+ #[knuffel(child)]
519+ pub inactive_color: Option<Color>,
520+ #[knuffel(child)]
521+ pub urgent_color: Option<Color>,
522+ #[knuffel(child)]
523+ pub active_gradient: Option<Gradient>,
524+ #[knuffel(child)]
525+ pub inactive_gradient: Option<Gradient>,
526+ #[knuffel(child)]
527+ pub urgent_gradient: Option<Gradient>,
528+}
529+
530+#[derive(knuffel::Decode, Debug, Default, Clone, Copy, PartialEq)]
531+pub struct ShadowRule {
532+ #[knuffel(child)]
533+ pub off: bool,
534+ #[knuffel(child)]
535+ pub on: bool,
536+ #[knuffel(child)]
537+ pub offset: Option<ShadowOffset>,
538+ #[knuffel(child, unwrap(argument))]
539+ pub softness: Option<FloatOrInt<0, 1024>>,
540+ #[knuffel(child, unwrap(argument))]
541+ pub spread: Option<FloatOrInt<-1024, 1024>>,
542+ #[knuffel(child, unwrap(argument))]
543+ pub draw_behind_window: Option<bool>,
544+ #[knuffel(child)]
545+ pub color: Option<Color>,
546+ #[knuffel(child)]
547+ pub inactive_color: Option<Color>,
548+}
549+
550+#[derive(knuffel::Decode, Debug, Default, Clone, Copy, PartialEq)]
551+pub struct TabIndicatorRule {
552+ #[knuffel(child)]
553+ pub active_color: Option<Color>,
554+ #[knuffel(child)]
555+ pub inactive_color: Option<Color>,
556+ #[knuffel(child)]
557+ pub urgent_color: Option<Color>,
558+ #[knuffel(child)]
559+ pub active_gradient: Option<Gradient>,
560+ #[knuffel(child)]
561+ pub inactive_gradient: Option<Gradient>,
562+ #[knuffel(child)]
563+ pub urgent_gradient: Option<Gradient>,
564+}
565+
566+impl BorderRule {
567+ pub fn merge_with(&mut self, other: &Self) {
568+ if other.off {
569+ self.off = true;
570+ self.on = false;
571+ }
572+
573+ if other.on {
574+ self.off = false;
575+ self.on = true;
576+ }
577+
578+ if let Some(x) = other.width {
579+ self.width = Some(x);
580+ }
581+ if let Some(x) = other.active_color {
582+ self.active_color = Some(x);
583+ }
584+ if let Some(x) = other.inactive_color {
585+ self.inactive_color = Some(x);
586+ }
587+ if let Some(x) = other.urgent_color {
588+ self.urgent_color = Some(x);
589+ }
590+ if let Some(x) = other.active_gradient {
591+ self.active_gradient = Some(x);
592+ }
593+ if let Some(x) = other.inactive_gradient {
594+ self.inactive_gradient = Some(x);
595+ }
596+ if let Some(x) = other.urgent_gradient {
597+ self.urgent_gradient = Some(x);
598+ }
599+ }
600+
601+ pub fn resolve_against(&self, mut config: Border) -> Border {
602+ config.off |= self.off;
603+ if self.on {
604+ config.off = false;
605+ }
606+
607+ if let Some(x) = self.width {
608+ config.width = x;
609+ }
610+ if let Some(x) = self.active_color {
611+ config.active_color = x;
612+ config.active_gradient = None;
613+ }
614+ if let Some(x) = self.inactive_color {
615+ config.inactive_color = x;
616+ config.inactive_gradient = None;
617+ }
618+ if let Some(x) = self.urgent_color {
619+ config.urgent_color = x;
620+ config.urgent_gradient = None;
621+ }
622+ if let Some(x) = self.active_gradient {
623+ config.active_gradient = Some(x);
624+ }
625+ if let Some(x) = self.inactive_gradient {
626+ config.inactive_gradient = Some(x);
627+ }
628+ if let Some(x) = self.urgent_gradient {
629+ config.urgent_gradient = Some(x);
630+ }
631+
632+ config
633+ }
634+}
635+
636+impl ShadowRule {
637+ pub fn merge_with(&mut self, other: &Self) {
638+ if other.off {
639+ self.off = true;
640+ self.on = false;
641+ }
642+
643+ if other.on {
644+ self.off = false;
645+ self.on = true;
646+ }
647+
648+ if let Some(x) = other.offset {
649+ self.offset = Some(x);
650+ }
651+ if let Some(x) = other.softness {
652+ self.softness = Some(x);
653+ }
654+ if let Some(x) = other.spread {
655+ self.spread = Some(x);
656+ }
657+ if let Some(x) = other.draw_behind_window {
658+ self.draw_behind_window = Some(x);
659+ }
660+ if let Some(x) = other.color {
661+ self.color = Some(x);
662+ }
663+ if let Some(x) = other.inactive_color {
664+ self.inactive_color = Some(x);
665+ }
666+ }
667+
668+ pub fn resolve_against(&self, mut config: Shadow) -> Shadow {
669+ config.on |= self.on;
670+ if self.off {
671+ config.on = false;
672+ }
673+
674+ if let Some(x) = self.offset {
675+ config.offset = x;
676+ }
677+ if let Some(x) = self.softness {
678+ config.softness = x;
679+ }
680+ if let Some(x) = self.spread {
681+ config.spread = x;
682+ }
683+ if let Some(x) = self.draw_behind_window {
684+ config.draw_behind_window = x;
685+ }
686+ if let Some(x) = self.color {
687+ config.color = x;
688+ }
689+ if let Some(x) = self.inactive_color {
690+ config.inactive_color = Some(x);
691+ }
692+
693+ config
694+ }
695+}
696+
697+impl TabIndicatorRule {
698+ pub fn merge_with(&mut self, other: &Self) {
699+ if let Some(x) = other.active_color {
700+ self.active_color = Some(x);
701+ }
702+ if let Some(x) = other.inactive_color {
703+ self.inactive_color = Some(x);
704+ }
705+ if let Some(x) = other.urgent_color {
706+ self.urgent_color = Some(x);
707+ }
708+ if let Some(x) = other.active_gradient {
709+ self.active_gradient = Some(x);
710+ }
711+ if let Some(x) = other.inactive_gradient {
712+ self.inactive_gradient = Some(x);
713+ }
714+ if let Some(x) = other.urgent_gradient {
715+ self.urgent_gradient = Some(x);
716+ }
717+ }
718+}
719+
720+impl FromStr for GradientInterpolation {
721+ type Err = miette::Error;
722+
723+ fn from_str(s: &str) -> Result<Self, Self::Err> {
724+ let mut iter = s.split_whitespace();
725+ let in_part1 = iter.next();
726+ let in_part2 = iter.next();
727+ let in_part3 = iter.next();
728+
729+ let Some(in_part1) = in_part1 else {
730+ return Err(miette!("missing color space"));
731+ };
732+
733+ let color = match in_part1 {
734+ "srgb" => GradientColorSpace::Srgb,
735+ "srgb-linear" => GradientColorSpace::SrgbLinear,
736+ "oklab" => GradientColorSpace::Oklab,
737+ "oklch" => GradientColorSpace::Oklch,
738+ x => {
739+ return Err(miette!(
740+ "invalid color space {x}; can be srgb, srgb-linear, oklab or oklch"
741+ ))
742+ }
743+ };
744+
745+ let interpolation = if let Some(in_part2) = in_part2 {
746+ if color != GradientColorSpace::Oklch {
747+ return Err(miette!("only oklch color space can have hue interpolation"));
748+ }
749+
750+ if in_part3 != Some("hue") {
751+ return Err(miette!(
752+ "interpolation must end with \"hue\", like \"oklch shorter hue\""
753+ ));
754+ } else if iter.next().is_some() {
755+ return Err(miette!("unexpected text after hue interpolation"));
756+ } else {
757+ match in_part2 {
758+ "shorter" => HueInterpolation::Shorter,
759+ "longer" => HueInterpolation::Longer,
760+ "increasing" => HueInterpolation::Increasing,
761+ "decreasing" => HueInterpolation::Decreasing,
762+ x => {
763+ return Err(miette!(
764+ "invalid hue interpolation {x}; \
765+ can be shorter, longer, increasing, decreasing"
766+ ))
767+ }
768+ }
769+ }
770+ } else {
771+ HueInterpolation::default()
772+ };
773+
774+ Ok(Self {
775+ color_space: color,
776+ hue_interpolation: interpolation,
777+ })
778+ }
779+}
780+
781+impl FromStr for Color {
782+ type Err = miette::Error;
783+
784+ fn from_str(s: &str) -> Result<Self, Self::Err> {
785+ let color = csscolorparser::parse(s)
786+ .into_diagnostic()?
787+ .clamp()
788+ .to_array();
789+ Ok(Self::from_array_unpremul(color))
790+ }
791+}
792+
793+#[derive(knuffel::Decode)]
794+struct ColorRgba {
795+ #[knuffel(argument)]
796+ r: u8,
797+ #[knuffel(argument)]
798+ g: u8,
799+ #[knuffel(argument)]
800+ b: u8,
801+ #[knuffel(argument)]
802+ a: u8,
803+}
804+
805+impl From<ColorRgba> for Color {
806+ fn from(value: ColorRgba) -> Self {
807+ let ColorRgba { r, g, b, a } = value;
808+ Self::from_array_unpremul([r, g, b, a].map(|x| x as f32 / 255.))
809+ }
810+}
811+
812+// Manual impl to allow both one-argument string and 4-argument RGBA forms.
813+impl<S> knuffel::Decode<S> for Color
814+where
815+ S: knuffel::traits::ErrorSpan,
816+{
817+ fn decode_node(
818+ node: &knuffel::ast::SpannedNode<S>,
819+ ctx: &mut knuffel::decode::Context<S>,
820+ ) -> Result<Self, DecodeError<S>> {
821+ // Check for unexpected type name.
822+ if let Some(type_name) = &node.type_name {
823+ ctx.emit_error(DecodeError::unexpected(
824+ type_name,
825+ "type name",
826+ "no type name expected for this node",
827+ ));
828+ }
829+
830+ // Get the first argument.
831+ let mut iter_args = node.arguments.iter();
832+ let val = iter_args
833+ .next()
834+ .ok_or_else(|| DecodeError::missing(node, "additional argument is required"))?;
835+
836+ // Check for unexpected type name.
837+ if let Some(typ) = &val.type_name {
838+ ctx.emit_error(DecodeError::TypeName {
839+ span: typ.span().clone(),
840+ found: Some((**typ).clone()),
841+ expected: knuffel::errors::ExpectedType::no_type(),
842+ rust_type: "str",
843+ });
844+ }
845+
846+ // Check the argument type.
847+ let rv = match *val.literal {
848+ // If it's a string, use FromStr.
849+ knuffel::ast::Literal::String(ref s) => {
850+ Color::from_str(s).map_err(|e| DecodeError::conversion(&val.literal, e))
851+ }
852+ // Otherwise, fall back to the 4-argument RGBA form.
853+ _ => return ColorRgba::decode_node(node, ctx).map(Color::from),
854+ }?;
855+
856+ // Check for unexpected following arguments.
857+ if let Some(val) = iter_args.next() {
858+ ctx.emit_error(DecodeError::unexpected(
859+ &val.literal,
860+ "argument",
861+ "unexpected argument",
862+ ));
863+ }
864+
865+ // Check for unexpected properties and children.
866+ for name in node.properties.keys() {
867+ ctx.emit_error(DecodeError::unexpected(
868+ name,
869+ "property",
870+ format!("unexpected property `{}`", name.escape_default()),
871+ ));
872+ }
873+ for child in node.children.as_ref().map(|lst| &lst[..]).unwrap_or(&[]) {
874+ ctx.emit_error(DecodeError::unexpected(
875+ child,
876+ "node",
877+ format!("unexpected node `{}`", child.node_name.escape_default()),
878+ ));
879+ }
880+
881+ Ok(rv)
882+ }
883+}
884+
885+impl<S> knuffel::Decode<S> for CornerRadius
886+where
887+ S: knuffel::traits::ErrorSpan,
888+{
889+ fn decode_node(
890+ node: &knuffel::ast::SpannedNode<S>,
891+ ctx: &mut knuffel::decode::Context<S>,
892+ ) -> Result<Self, DecodeError<S>> {
893+ // Check for unexpected type name.
894+ if let Some(type_name) = &node.type_name {
895+ ctx.emit_error(DecodeError::unexpected(
896+ type_name,
897+ "type name",
898+ "no type name expected for this node",
899+ ));
900+ }
901+
902+ let decode_radius = |ctx: &mut knuffel::decode::Context<S>,
903+ val: &knuffel::ast::Value<S>| {
904+ // Check for unexpected type name.
905+ if let Some(typ) = &val.type_name {
906+ ctx.emit_error(DecodeError::TypeName {
907+ span: typ.span().clone(),
908+ found: Some((**typ).clone()),
909+ expected: knuffel::errors::ExpectedType::no_type(),
910+ rust_type: "str",
911+ });
912+ }
913+
914+ // Decode both integers and floats.
915+ let radius = match *val.literal {
916+ knuffel::ast::Literal::Int(ref x) => f32::from(match x.try_into() {
917+ Ok(x) => x,
918+ Err(err) => {
919+ ctx.emit_error(DecodeError::conversion(&val.literal, err));
920+ 0i16
921+ }
922+ }),
923+ knuffel::ast::Literal::Decimal(ref x) => match x.try_into() {
924+ Ok(x) => x,
925+ Err(err) => {
926+ ctx.emit_error(DecodeError::conversion(&val.literal, err));
927+ 0.
928+ }
929+ },
930+ _ => {
931+ ctx.emit_error(DecodeError::scalar_kind(
932+ knuffel::decode::Kind::Int,
933+ &val.literal,
934+ ));
935+ 0.
936+ }
937+ };
938+
939+ if radius < 0. {
940+ ctx.emit_error(DecodeError::conversion(&val.literal, "radius must be >= 0"));
941+ }
942+
943+ radius
944+ };
945+
946+ // Get the first argument.
947+ let mut iter_args = node.arguments.iter();
948+ let val = iter_args
949+ .next()
950+ .ok_or_else(|| DecodeError::missing(node, "additional argument is required"))?;
951+
952+ let top_left = decode_radius(ctx, val);
953+
954+ let mut rv = CornerRadius {
955+ top_left,
956+ top_right: top_left,
957+ bottom_right: top_left,
958+ bottom_left: top_left,
959+ };
960+
961+ if let Some(val) = iter_args.next() {
962+ rv.top_right = decode_radius(ctx, val);
963+
964+ let val = iter_args.next().ok_or_else(|| {
965+ DecodeError::missing(node, "either 1 or 4 arguments are required")
966+ })?;
967+ rv.bottom_right = decode_radius(ctx, val);
968+
969+ let val = iter_args.next().ok_or_else(|| {
970+ DecodeError::missing(node, "either 1 or 4 arguments are required")
971+ })?;
972+ rv.bottom_left = decode_radius(ctx, val);
973+
974+ // Check for unexpected following arguments.
975+ if let Some(val) = iter_args.next() {
976+ ctx.emit_error(DecodeError::unexpected(
977+ &val.literal,
978+ "argument",
979+ "unexpected argument",
980+ ));
981+ }
982+ }
983+
984+ // Check for unexpected properties and children.
985+ for name in node.properties.keys() {
986+ ctx.emit_error(DecodeError::unexpected(
987+ name,
988+ "property",
989+ format!("unexpected property `{}`", name.escape_default()),
990+ ));
991+ }
992+ for child in node.children.as_ref().map(|lst| &lst[..]).unwrap_or(&[]) {
993+ ctx.emit_error(DecodeError::unexpected(
994+ child,
995+ "node",
996+ format!("unexpected node `{}`", child.node_name.escape_default()),
997+ ));
998+ }
999+
1000+ Ok(rv)
1001+ }
1002+}
1003+
1004+#[cfg(test)]
1005+mod tests {
1006+ use insta::assert_snapshot;
1007+
1008+ use super::*;
1009+
1010+ #[test]
1011+ fn parse_gradient_interpolation() {
1012+ assert_eq!(
1013+ "srgb".parse::<GradientInterpolation>().unwrap(),
1014+ GradientInterpolation {
1015+ color_space: GradientColorSpace::Srgb,
1016+ ..Default::default()
1017+ }
1018+ );
1019+ assert_eq!(
1020+ "srgb-linear".parse::<GradientInterpolation>().unwrap(),
1021+ GradientInterpolation {
1022+ color_space: GradientColorSpace::SrgbLinear,
1023+ ..Default::default()
1024+ }
1025+ );
1026+ assert_eq!(
1027+ "oklab".parse::<GradientInterpolation>().unwrap(),
1028+ GradientInterpolation {
1029+ color_space: GradientColorSpace::Oklab,
1030+ ..Default::default()
1031+ }
1032+ );
1033+ assert_eq!(
1034+ "oklch".parse::<GradientInterpolation>().unwrap(),
1035+ GradientInterpolation {
1036+ color_space: GradientColorSpace::Oklch,
1037+ ..Default::default()
1038+ }
1039+ );
1040+ assert_eq!(
1041+ "oklch shorter hue"
1042+ .parse::<GradientInterpolation>()
1043+ .unwrap(),
1044+ GradientInterpolation {
1045+ color_space: GradientColorSpace::Oklch,
1046+ hue_interpolation: HueInterpolation::Shorter,
1047+ }
1048+ );
1049+ assert_eq!(
1050+ "oklch longer hue".parse::<GradientInterpolation>().unwrap(),
1051+ GradientInterpolation {
1052+ color_space: GradientColorSpace::Oklch,
1053+ hue_interpolation: HueInterpolation::Longer,
1054+ }
1055+ );
1056+ assert_eq!(
1057+ "oklch decreasing hue"
1058+ .parse::<GradientInterpolation>()
1059+ .unwrap(),
1060+ GradientInterpolation {
1061+ color_space: GradientColorSpace::Oklch,
1062+ hue_interpolation: HueInterpolation::Decreasing,
1063+ }
1064+ );
1065+ assert_eq!(
1066+ "oklch increasing hue"
1067+ .parse::<GradientInterpolation>()
1068+ .unwrap(),
1069+ GradientInterpolation {
1070+ color_space: GradientColorSpace::Oklch,
1071+ hue_interpolation: HueInterpolation::Increasing,
1072+ }
1073+ );
1074+
1075+ assert!("".parse::<GradientInterpolation>().is_err());
1076+ assert!("srgb shorter hue".parse::<GradientInterpolation>().is_err());
1077+ assert!("oklch shorter".parse::<GradientInterpolation>().is_err());
1078+ assert!("oklch shorter h".parse::<GradientInterpolation>().is_err());
1079+ assert!("oklch a hue".parse::<GradientInterpolation>().is_err());
1080+ assert!("oklch shorter hue a"
1081+ .parse::<GradientInterpolation>()
1082+ .is_err());
1083+ }
1084+
1085+ #[test]
1086+ fn test_border_rule_on_off_merging() {
1087+ fn is_on(config: &str, rules: &[&str]) -> String {
1088+ let mut resolved = BorderRule {
1089+ off: false,
1090+ on: false,
1091+ width: None,
1092+ active_color: None,
1093+ inactive_color: None,
1094+ urgent_color: None,
1095+ active_gradient: None,
1096+ inactive_gradient: None,
1097+ urgent_gradient: None,
1098+ };
1099+
1100+ for rule in rules.iter().copied() {
1101+ let rule = BorderRule {
1102+ off: rule == "off" || rule == "off,on",
1103+ on: rule == "on" || rule == "off,on",
1104+ ..Default::default()
1105+ };
1106+
1107+ resolved.merge_with(&rule);
1108+ }
1109+
1110+ let config = Border {
1111+ off: config == "off",
1112+ ..Default::default()
1113+ };
1114+
1115+ if resolved.resolve_against(config).off {
1116+ "off"
1117+ } else {
1118+ "on"
1119+ }
1120+ .to_owned()
1121+ }
1122+
1123+ assert_snapshot!(is_on("off", &[]), @"off");
1124+ assert_snapshot!(is_on("off", &["off"]), @"off");
1125+ assert_snapshot!(is_on("off", &["on"]), @"on");
1126+ assert_snapshot!(is_on("off", &["off,on"]), @"on");
1127+
1128+ assert_snapshot!(is_on("on", &[]), @"on");
1129+ assert_snapshot!(is_on("on", &["off"]), @"off");
1130+ assert_snapshot!(is_on("on", &["on"]), @"on");
1131+ assert_snapshot!(is_on("on", &["off,on"]), @"on");
1132+
1133+ assert_snapshot!(is_on("off", &["off", "off"]), @"off");
1134+ assert_snapshot!(is_on("off", &["off", "on"]), @"on");
1135+ assert_snapshot!(is_on("off", &["on", "off"]), @"off");
1136+ assert_snapshot!(is_on("off", &["on", "on"]), @"on");
1137+
1138+ assert_snapshot!(is_on("on", &["off", "off"]), @"off");
1139+ assert_snapshot!(is_on("on", &["off", "on"]), @"on");
1140+ assert_snapshot!(is_on("on", &["on", "off"]), @"off");
1141+ assert_snapshot!(is_on("on", &["on", "on"]), @"on");
1142+ }
1143+}
niri-config/src/layer_rule.rsmodified+1 -1
@@ -1,5 +1,5 @@
1+use crate::appearance::{BlockOutFrom, CornerRadius, ShadowRule};
12 use crate::utils::RegexEq;
2-use crate::{BlockOutFrom, CornerRadius, ShadowRule};
33
44 #[derive(knuffel::Decode, Debug, Default, Clone, PartialEq)]
55 pub struct LayerRule {
niri-config/src/layout.rsadded+132 -0
@@ -0,0 +1,132 @@
1+use knuffel::errors::DecodeError;
2+use niri_ipc::{ColumnDisplay, SizeChange};
3+
4+use crate::appearance::{
5+ Border, FocusRing, InsertHint, Shadow, TabIndicator, DEFAULT_BACKGROUND_COLOR,
6+};
7+use crate::{expect_only_children, Color, FloatOrInt};
8+
9+#[derive(knuffel::Decode, Debug, Clone, PartialEq)]
10+pub struct Layout {
11+ #[knuffel(child, default)]
12+ pub focus_ring: FocusRing,
13+ #[knuffel(child, default)]
14+ pub border: Border,
15+ #[knuffel(child, default)]
16+ pub shadow: Shadow,
17+ #[knuffel(child, default)]
18+ pub tab_indicator: TabIndicator,
19+ #[knuffel(child, default)]
20+ pub insert_hint: InsertHint,
21+ #[knuffel(child, unwrap(children), default)]
22+ pub preset_column_widths: Vec<PresetSize>,
23+ #[knuffel(child)]
24+ pub default_column_width: Option<DefaultPresetSize>,
25+ #[knuffel(child, unwrap(children), default)]
26+ pub preset_window_heights: Vec<PresetSize>,
27+ #[knuffel(child, unwrap(argument), default)]
28+ pub center_focused_column: CenterFocusedColumn,
29+ #[knuffel(child)]
30+ pub always_center_single_column: bool,
31+ #[knuffel(child)]
32+ pub empty_workspace_above_first: bool,
33+ #[knuffel(child, unwrap(argument, str), default = Self::default().default_column_display)]
34+ pub default_column_display: ColumnDisplay,
35+ #[knuffel(child, unwrap(argument), default = Self::default().gaps)]
36+ pub gaps: FloatOrInt<0, 65535>,
37+ #[knuffel(child, default)]
38+ pub struts: Struts,
39+ #[knuffel(child, default = DEFAULT_BACKGROUND_COLOR)]
40+ pub background_color: Color,
41+}
42+
43+impl Default for Layout {
44+ fn default() -> Self {
45+ Self {
46+ focus_ring: Default::default(),
47+ border: Default::default(),
48+ shadow: Default::default(),
49+ tab_indicator: Default::default(),
50+ insert_hint: Default::default(),
51+ preset_column_widths: Default::default(),
52+ default_column_width: Default::default(),
53+ center_focused_column: Default::default(),
54+ always_center_single_column: false,
55+ empty_workspace_above_first: false,
56+ default_column_display: ColumnDisplay::Normal,
57+ gaps: FloatOrInt(16.),
58+ struts: Default::default(),
59+ preset_window_heights: Default::default(),
60+ background_color: DEFAULT_BACKGROUND_COLOR,
61+ }
62+ }
63+}
64+
65+#[derive(knuffel::Decode, Debug, Clone, Copy, PartialEq)]
66+pub enum PresetSize {
67+ Proportion(#[knuffel(argument)] f64),
68+ Fixed(#[knuffel(argument)] i32),
69+}
70+
71+impl From<PresetSize> for SizeChange {
72+ fn from(value: PresetSize) -> Self {
73+ match value {
74+ PresetSize::Proportion(prop) => SizeChange::SetProportion(prop * 100.),
75+ PresetSize::Fixed(fixed) => SizeChange::SetFixed(fixed),
76+ }
77+ }
78+}
79+
80+#[derive(Debug, Clone, Copy, PartialEq)]
81+pub struct DefaultPresetSize(pub Option<PresetSize>);
82+
83+#[derive(knuffel::Decode, Debug, Default, Clone, Copy, PartialEq)]
84+pub struct Struts {
85+ #[knuffel(child, unwrap(argument), default)]
86+ pub left: FloatOrInt<-65535, 65535>,
87+ #[knuffel(child, unwrap(argument), default)]
88+ pub right: FloatOrInt<-65535, 65535>,
89+ #[knuffel(child, unwrap(argument), default)]
90+ pub top: FloatOrInt<-65535, 65535>,
91+ #[knuffel(child, unwrap(argument), default)]
92+ pub bottom: FloatOrInt<-65535, 65535>,
93+}
94+
95+#[derive(knuffel::DecodeScalar, Debug, Default, PartialEq, Eq, Clone, Copy)]
96+pub enum CenterFocusedColumn {
97+ /// Focusing a column will not center the column.
98+ #[default]
99+ Never,
100+ /// The focused column will always be centered.
101+ Always,
102+ /// Focusing a column will center it if it doesn't fit on the screen together with the
103+ /// previously focused column.
104+ OnOverflow,
105+}
106+
107+impl<S> knuffel::Decode<S> for DefaultPresetSize
108+where
109+ S: knuffel::traits::ErrorSpan,
110+{
111+ fn decode_node(
112+ node: &knuffel::ast::SpannedNode<S>,
113+ ctx: &mut knuffel::decode::Context<S>,
114+ ) -> Result<Self, DecodeError<S>> {
115+ expect_only_children(node, ctx);
116+
117+ let mut children = node.children();
118+
119+ if let Some(child) = children.next() {
120+ if let Some(unwanted_child) = children.next() {
121+ ctx.emit_error(DecodeError::unexpected(
122+ unwanted_child,
123+ "node",
124+ "expected no more than one child",
125+ ));
126+ }
127+ PresetSize::decode_node(child, ctx).map(Some).map(Self)
128+ } else {
129+ Ok(Self(None))
130+ }
131+ }
132+}
niri-config/src/lib.rsmodified+8 -1259
@@ -5,7 +5,6 @@ use std::collections::HashSet;
55 use std::ffi::OsStr;
66 use std::fs::{self, File};
77 use std::io::Write;
8-use std::ops::{Mul, MulAssign};
98 use std::path::{Path, PathBuf};
109 use std::str::FromStr;
1110 use std::time::Duration;
@@ -16,17 +15,15 @@ use miette::{miette, Context, IntoDiagnostic};
1615 use niri_ipc::{
1716 ColumnDisplay, LayoutSwitchTarget, PositionChange, SizeChange, WorkspaceReferenceArg,
1817 };
19-use smithay::backend::renderer::Color32F;
2018 use smithay::input::keyboard::keysyms::KEY_NoSymbol;
2119 use smithay::input::keyboard::xkb::{keysym_from_name, KEYSYM_CASE_INSENSITIVE};
2220 use smithay::input::keyboard::Keysym;
2321
24-pub const DEFAULT_BACKGROUND_COLOR: Color = Color::from_array_unpremul([0.25, 0.25, 0.25, 1.]);
25-pub const DEFAULT_BACKDROP_COLOR: Color = Color::from_array_unpremul([0.15, 0.15, 0.15, 1.]);
26-
2722 pub mod animations;
23+pub mod appearance;
2824 pub mod input;
2925 pub mod layer_rule;
26+pub mod layout;
3027 pub mod output;
3128 pub mod utils;
3229 pub mod window_rule;
@@ -34,8 +31,10 @@ pub mod window_rule;
3431 pub use crate::animations::{
3532 Animation, AnimationCurve, AnimationKind, Animations, EasingParams, SpringParams,
3633 };
34+pub use crate::appearance::*;
3735 pub use crate::input::{Input, ModKey, ScrollMethod, TrackLayout, WarpMouseToFocusMode, Xkb};
3836 pub use crate::layer_rule::LayerRule;
37+pub use crate::layout::*;
3938 pub use crate::output::{Output, OutputName, Outputs, Position, Vrr};
4039 pub use crate::window_rule::{FloatingPosition, RelativeTo, WindowRule};
4140
@@ -93,18 +92,6 @@ pub struct Config {
9392 pub workspaces: Vec<Workspace>,
9493 }
9594
96-#[derive(knuffel::DecodeScalar, Debug, Default, PartialEq, Eq, Clone, Copy)]
97-pub enum CenterFocusedColumn {
98- /// Focusing a column will not center the column.
99- #[default]
100- Never,
101- /// The focused column will always be centered.
102- Always,
103- /// Focusing a column will center it if it doesn't fit on the screen together with the
104- /// previously focused column.
105- OnOverflow,
106-}
107-
10895 #[derive(Debug, Clone, Copy, PartialEq)]
10996 pub struct Percent(pub f64);
11097
@@ -112,62 +99,6 @@ pub struct Percent(pub f64);
11299 #[derive(Debug, Default, Clone, Copy, PartialEq)]
113100 pub struct FloatOrInt<const MIN: i32, const MAX: i32>(pub f64);
114101
115-#[derive(knuffel::Decode, Debug, Clone, PartialEq)]
116-pub struct Layout {
117- #[knuffel(child, default)]
118- pub focus_ring: FocusRing,
119- #[knuffel(child, default)]
120- pub border: Border,
121- #[knuffel(child, default)]
122- pub shadow: Shadow,
123- #[knuffel(child, default)]
124- pub tab_indicator: TabIndicator,
125- #[knuffel(child, default)]
126- pub insert_hint: InsertHint,
127- #[knuffel(child, unwrap(children), default)]
128- pub preset_column_widths: Vec<PresetSize>,
129- #[knuffel(child)]
130- pub default_column_width: Option<DefaultPresetSize>,
131- #[knuffel(child, unwrap(children), default)]
132- pub preset_window_heights: Vec<PresetSize>,
133- #[knuffel(child, unwrap(argument), default)]
134- pub center_focused_column: CenterFocusedColumn,
135- #[knuffel(child)]
136- pub always_center_single_column: bool,
137- #[knuffel(child)]
138- pub empty_workspace_above_first: bool,
139- #[knuffel(child, unwrap(argument, str), default = Self::default().default_column_display)]
140- pub default_column_display: ColumnDisplay,
141- #[knuffel(child, unwrap(argument), default = Self::default().gaps)]
142- pub gaps: FloatOrInt<0, 65535>,
143- #[knuffel(child, default)]
144- pub struts: Struts,
145- #[knuffel(child, default = DEFAULT_BACKGROUND_COLOR)]
146- pub background_color: Color,
147-}
148-
149-impl Default for Layout {
150- fn default() -> Self {
151- Self {
152- focus_ring: Default::default(),
153- border: Default::default(),
154- shadow: Default::default(),
155- tab_indicator: Default::default(),
156- insert_hint: Default::default(),
157- preset_column_widths: Default::default(),
158- default_column_width: Default::default(),
159- center_focused_column: Default::default(),
160- always_center_single_column: false,
161- empty_workspace_above_first: false,
162- default_column_display: ColumnDisplay::Normal,
163- gaps: FloatOrInt(16.),
164- struts: Default::default(),
165- preset_window_heights: Default::default(),
166- background_color: DEFAULT_BACKGROUND_COLOR,
167- }
168- }
169-}
170-
171102 #[derive(knuffel::Decode, Debug, Clone, PartialEq, Eq)]
172103 pub struct SpawnAtStartup {
173104 #[knuffel(arguments)]
@@ -180,407 +111,6 @@ pub struct SpawnShAtStartup {
180111 pub command: String,
181112 }
182113
183-#[derive(knuffel::Decode, Debug, Clone, Copy, PartialEq)]
184-pub struct FocusRing {
185- #[knuffel(child)]
186- pub off: bool,
187- #[knuffel(child, unwrap(argument), default = Self::default().width)]
188- pub width: FloatOrInt<0, 65535>,
189- #[knuffel(child, default = Self::default().active_color)]
190- pub active_color: Color,
191- #[knuffel(child, default = Self::default().inactive_color)]
192- pub inactive_color: Color,
193- #[knuffel(child, default = Self::default().urgent_color)]
194- pub urgent_color: Color,
195- #[knuffel(child)]
196- pub active_gradient: Option<Gradient>,
197- #[knuffel(child)]
198- pub inactive_gradient: Option<Gradient>,
199- #[knuffel(child)]
200- pub urgent_gradient: Option<Gradient>,
201-}
202-
203-impl Default for FocusRing {
204- fn default() -> Self {
205- Self {
206- off: false,
207- width: FloatOrInt(4.),
208- active_color: Color::from_rgba8_unpremul(127, 200, 255, 255),
209- inactive_color: Color::from_rgba8_unpremul(80, 80, 80, 255),
210- urgent_color: Color::from_rgba8_unpremul(155, 0, 0, 255),
211- active_gradient: None,
212- inactive_gradient: None,
213- urgent_gradient: None,
214- }
215- }
216-}
217-
218-#[derive(knuffel::Decode, Debug, Clone, Copy, PartialEq)]
219-pub struct Gradient {
220- #[knuffel(property, str)]
221- pub from: Color,
222- #[knuffel(property, str)]
223- pub to: Color,
224- #[knuffel(property, default = 180)]
225- pub angle: i16,
226- #[knuffel(property, default)]
227- pub relative_to: GradientRelativeTo,
228- #[knuffel(property(name = "in"), str, default)]
229- pub in_: GradientInterpolation,
230-}
231-
232-impl From<Color> for Gradient {
233- fn from(value: Color) -> Self {
234- Self {
235- from: value,
236- to: value,
237- angle: 0,
238- relative_to: GradientRelativeTo::Window,
239- in_: GradientInterpolation::default(),
240- }
241- }
242-}
243-
244-#[derive(knuffel::DecodeScalar, Debug, Default, Clone, Copy, PartialEq, Eq)]
245-pub enum GradientRelativeTo {
246- #[default]
247- Window,
248- WorkspaceView,
249-}
250-
251-#[derive(Default, Debug, Clone, Copy, PartialEq)]
252-pub struct GradientInterpolation {
253- pub color_space: GradientColorSpace,
254- pub hue_interpolation: HueInterpolation,
255-}
256-
257-#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
258-pub enum GradientColorSpace {
259- #[default]
260- Srgb,
261- SrgbLinear,
262- Oklab,
263- Oklch,
264-}
265-
266-#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
267-pub enum HueInterpolation {
268- #[default]
269- Shorter,
270- Longer,
271- Increasing,
272- Decreasing,
273-}
274-
275-#[derive(knuffel::Decode, Debug, Clone, Copy, PartialEq)]
276-pub struct Border {
277- #[knuffel(child)]
278- pub off: bool,
279- #[knuffel(child, unwrap(argument), default = Self::default().width)]
280- pub width: FloatOrInt<0, 65535>,
281- #[knuffel(child, default = Self::default().active_color)]
282- pub active_color: Color,
283- #[knuffel(child, default = Self::default().inactive_color)]
284- pub inactive_color: Color,
285- #[knuffel(child, default = Self::default().urgent_color)]
286- pub urgent_color: Color,
287- #[knuffel(child)]
288- pub active_gradient: Option<Gradient>,
289- #[knuffel(child)]
290- pub inactive_gradient: Option<Gradient>,
291- #[knuffel(child)]
292- pub urgent_gradient: Option<Gradient>,
293-}
294-
295-impl Default for Border {
296- fn default() -> Self {
297- Self {
298- off: true,
299- width: FloatOrInt(4.),
300- active_color: Color::from_rgba8_unpremul(255, 200, 127, 255),
301- inactive_color: Color::from_rgba8_unpremul(80, 80, 80, 255),
302- urgent_color: Color::from_rgba8_unpremul(155, 0, 0, 255),
303- active_gradient: None,
304- inactive_gradient: None,
305- urgent_gradient: None,
306- }
307- }
308-}
309-
310-impl From<Border> for FocusRing {
311- fn from(value: Border) -> Self {
312- Self {
313- off: value.off,
314- width: value.width,
315- active_color: value.active_color,
316- inactive_color: value.inactive_color,
317- urgent_color: value.urgent_color,
318- active_gradient: value.active_gradient,
319- inactive_gradient: value.inactive_gradient,
320- urgent_gradient: value.urgent_gradient,
321- }
322- }
323-}
324-
325-impl From<FocusRing> for Border {
326- fn from(value: FocusRing) -> Self {
327- Self {
328- off: value.off,
329- width: value.width,
330- active_color: value.active_color,
331- inactive_color: value.inactive_color,
332- urgent_color: value.urgent_color,
333- active_gradient: value.active_gradient,
334- inactive_gradient: value.inactive_gradient,
335- urgent_gradient: value.urgent_gradient,
336- }
337- }
338-}
339-
340-#[derive(knuffel::Decode, Debug, Clone, Copy, PartialEq)]
341-pub struct Shadow {
342- #[knuffel(child)]
343- pub on: bool,
344- #[knuffel(child, default = Self::default().offset)]
345- pub offset: ShadowOffset,
346- #[knuffel(child, unwrap(argument), default = Self::default().softness)]
347- pub softness: FloatOrInt<0, 1024>,
348- #[knuffel(child, unwrap(argument), default = Self::default().spread)]
349- pub spread: FloatOrInt<-1024, 1024>,
350- #[knuffel(child, unwrap(argument), default = Self::default().draw_behind_window)]
351- pub draw_behind_window: bool,
352- #[knuffel(child, default = Self::default().color)]
353- pub color: Color,
354- #[knuffel(child)]
355- pub inactive_color: Option<Color>,
356-}
357-
358-impl Default for Shadow {
359- fn default() -> Self {
360- Self {
361- on: false,
362- offset: ShadowOffset {
363- x: FloatOrInt(0.),
364- y: FloatOrInt(5.),
365- },
366- softness: FloatOrInt(30.),
367- spread: FloatOrInt(5.),
368- draw_behind_window: false,
369- color: Color::from_rgba8_unpremul(0, 0, 0, 0x70),
370- inactive_color: None,
371- }
372- }
373-}
374-
375-#[derive(knuffel::Decode, Debug, Clone, Copy, PartialEq)]
376-pub struct ShadowOffset {
377- #[knuffel(property, default)]
378- pub x: FloatOrInt<-65535, 65535>,
379- #[knuffel(property, default)]
380- pub y: FloatOrInt<-65535, 65535>,
381-}
382-
383-#[derive(knuffel::Decode, Debug, Clone, Copy, PartialEq)]
384-pub struct WorkspaceShadow {
385- #[knuffel(child)]
386- pub off: bool,
387- #[knuffel(child, default = Self::default().offset)]
388- pub offset: ShadowOffset,
389- #[knuffel(child, unwrap(argument), default = Self::default().softness)]
390- pub softness: FloatOrInt<0, 1024>,
391- #[knuffel(child, unwrap(argument), default = Self::default().spread)]
392- pub spread: FloatOrInt<-1024, 1024>,
393- #[knuffel(child, default = Self::default().color)]
394- pub color: Color,
395-}
396-
397-impl Default for WorkspaceShadow {
398- fn default() -> Self {
399- Self {
400- off: false,
401- offset: ShadowOffset {
402- x: FloatOrInt(0.),
403- y: FloatOrInt(10.),
404- },
405- softness: FloatOrInt(40.),
406- spread: FloatOrInt(10.),
407- color: Color::from_rgba8_unpremul(0, 0, 0, 0x50),
408- }
409- }
410-}
411-
412-impl From<WorkspaceShadow> for Shadow {
413- fn from(value: WorkspaceShadow) -> Self {
414- Self {
415- on: !value.off,
416- offset: value.offset,
417- softness: value.softness,
418- spread: value.spread,
419- draw_behind_window: false,
420- color: value.color,
421- inactive_color: None,
422- }
423- }
424-}
425-
426-#[derive(knuffel::Decode, Debug, Clone, Copy, PartialEq)]
427-pub struct TabIndicator {
428- #[knuffel(child)]
429- pub off: bool,
430- #[knuffel(child)]
431- pub hide_when_single_tab: bool,
432- #[knuffel(child)]
433- pub place_within_column: bool,
434- #[knuffel(child, unwrap(argument), default = Self::default().gap)]
435- pub gap: FloatOrInt<-65535, 65535>,
436- #[knuffel(child, unwrap(argument), default = Self::default().width)]
437- pub width: FloatOrInt<0, 65535>,
438- #[knuffel(child, default = Self::default().length)]
439- pub length: TabIndicatorLength,
440- #[knuffel(child, unwrap(argument), default = Self::default().position)]
441- pub position: TabIndicatorPosition,
442- #[knuffel(child, unwrap(argument), default = Self::default().gaps_between_tabs)]
443- pub gaps_between_tabs: FloatOrInt<0, 65535>,
444- #[knuffel(child, unwrap(argument), default = Self::default().corner_radius)]
445- pub corner_radius: FloatOrInt<0, 65535>,
446- #[knuffel(child)]
447- pub active_color: Option<Color>,
448- #[knuffel(child)]
449- pub inactive_color: Option<Color>,
450- #[knuffel(child)]
451- pub urgent_color: Option<Color>,
452- #[knuffel(child)]
453- pub active_gradient: Option<Gradient>,
454- #[knuffel(child)]
455- pub inactive_gradient: Option<Gradient>,
456- #[knuffel(child)]
457- pub urgent_gradient: Option<Gradient>,
458-}
459-
460-impl Default for TabIndicator {
461- fn default() -> Self {
462- Self {
463- off: false,
464- hide_when_single_tab: false,
465- place_within_column: false,
466- gap: FloatOrInt(5.),
467- width: FloatOrInt(4.),
468- length: TabIndicatorLength {
469- total_proportion: Some(0.5),
470- },
471- position: TabIndicatorPosition::Left,
472- gaps_between_tabs: FloatOrInt(0.),
473- corner_radius: FloatOrInt(0.),
474- active_color: None,
475- inactive_color: None,
476- urgent_color: None,
477- active_gradient: None,
478- inactive_gradient: None,
479- urgent_gradient: None,
480- }
481- }
482-}
483-
484-#[derive(knuffel::Decode, Debug, Clone, Copy, PartialEq)]
485-pub struct TabIndicatorLength {
486- #[knuffel(property)]
487- pub total_proportion: Option<f64>,
488-}
489-
490-#[derive(knuffel::DecodeScalar, Debug, Clone, Copy, PartialEq)]
491-pub enum TabIndicatorPosition {
492- Left,
493- Right,
494- Top,
495- Bottom,
496-}
497-
498-#[derive(knuffel::Decode, Debug, Clone, Copy, PartialEq)]
499-pub struct InsertHint {
500- #[knuffel(child)]
501- pub off: bool,
502- #[knuffel(child, default = Self::default().color)]
503- pub color: Color,
504- #[knuffel(child)]
505- pub gradient: Option<Gradient>,
506-}
507-
508-impl Default for InsertHint {
509- fn default() -> Self {
510- Self {
511- off: false,
512- color: Color::from_rgba8_unpremul(127, 200, 255, 128),
513- gradient: None,
514- }
515- }
516-}
517-
518-/// RGB color in [0, 1] with unpremultiplied alpha.
519-#[derive(Debug, Default, Clone, Copy, PartialEq)]
520-pub struct Color {
521- pub r: f32,
522- pub g: f32,
523- pub b: f32,
524- pub a: f32,
525-}
526-
527-impl Color {
528- pub const fn new_unpremul(r: f32, g: f32, b: f32, a: f32) -> Self {
529- Self { r, g, b, a }
530- }
531-
532- pub fn from_rgba8_unpremul(r: u8, g: u8, b: u8, a: u8) -> Self {
533- Self::from_array_unpremul([r, g, b, a].map(|x| x as f32 / 255.))
534- }
535-
536- pub fn from_array_premul([r, g, b, a]: [f32; 4]) -> Self {
537- let a = a.clamp(0., 1.);
538-
539- if a == 0. {
540- Self::new_unpremul(0., 0., 0., 0.)
541- } else {
542- Self {
543- r: (r / a).clamp(0., 1.),
544- g: (g / a).clamp(0., 1.),
545- b: (b / a).clamp(0., 1.),
546- a,
547- }
548- }
549- }
550-
551- pub const fn from_array_unpremul([r, g, b, a]: [f32; 4]) -> Self {
552- Self { r, g, b, a }
553- }
554-
555- pub fn from_color32f(color: Color32F) -> Self {
556- Self::from_array_premul(color.components())
557- }
558-
559- pub fn to_array_unpremul(self) -> [f32; 4] {
560- [self.r, self.g, self.b, self.a]
561- }
562-
563- pub fn to_array_premul(self) -> [f32; 4] {
564- let [r, g, b, a] = [self.r, self.g, self.b, self.a];
565- [r * a, g * a, b * a, a]
566- }
567-}
568-
569-impl Mul<f32> for Color {
570- type Output = Self;
571-
572- fn mul(mut self, rhs: f32) -> Self::Output {
573- self.a *= rhs;
574- self
575- }
576-}
577-
578-impl MulAssign<f32> for Color {
579- fn mul_assign(&mut self, rhs: f32) {
580- self.a *= rhs;
581- }
582-}
583-
584114 #[derive(knuffel::Decode, Debug, PartialEq)]
585115 pub struct Cursor {
586116 #[knuffel(child, unwrap(argument), default = String::from("default"))]
@@ -604,36 +134,6 @@ impl Default for Cursor {
604134 }
605135 }
606136
607-#[derive(knuffel::Decode, Debug, Clone, Copy, PartialEq)]
608-pub enum PresetSize {
609- Proportion(#[knuffel(argument)] f64),
610- Fixed(#[knuffel(argument)] i32),
611-}
612-
613-impl From<PresetSize> for SizeChange {
614- fn from(value: PresetSize) -> Self {
615- match value {
616- PresetSize::Proportion(prop) => SizeChange::SetProportion(prop * 100.),
617- PresetSize::Fixed(fixed) => SizeChange::SetFixed(fixed),
618- }
619- }
620-}
621-
622-#[derive(Debug, Clone, Copy, PartialEq)]
623-pub struct DefaultPresetSize(pub Option<PresetSize>);
624-
625-#[derive(knuffel::Decode, Debug, Default, Clone, Copy, PartialEq)]
626-pub struct Struts {
627- #[knuffel(child, unwrap(argument), default)]
628- pub left: FloatOrInt<-65535, 65535>,
629- #[knuffel(child, unwrap(argument), default)]
630- pub right: FloatOrInt<-65535, 65535>,
631- #[knuffel(child, unwrap(argument), default)]
632- pub top: FloatOrInt<-65535, 65535>,
633- #[knuffel(child, unwrap(argument), default)]
634- pub bottom: FloatOrInt<-65535, 65535>,
635-}
636-
637137 #[derive(knuffel::Decode, Debug, Default, Clone, Copy, PartialEq, Eq)]
638138 pub struct HotkeyOverlay {
639139 #[knuffel(child)]
@@ -717,15 +217,15 @@ pub struct Overview {
717217 #[knuffel(child, default = Self::default().backdrop_color)]
718218 pub backdrop_color: Color,
719219 #[knuffel(child, default)]
720- pub workspace_shadow: WorkspaceShadow,
220+ pub workspace_shadow: appearance::WorkspaceShadow,
721221 }
722222
723223 impl Default for Overview {
724224 fn default() -> Self {
725225 Self {
726226 zoom: FloatOrInt(0.5),
727- backdrop_color: DEFAULT_BACKDROP_COLOR,
728- workspace_shadow: WorkspaceShadow::default(),
227+ backdrop_color: appearance::DEFAULT_BACKDROP_COLOR,
228+ workspace_shadow: appearance::WorkspaceShadow::default(),
729229 }
730230 }
731231 }
@@ -769,100 +269,6 @@ pub struct Workspace {
769269 #[derive(Debug, Clone, PartialEq, Eq)]
770270 pub struct WorkspaceName(pub String);
771271
772-#[derive(Debug, Default, Clone, Copy, PartialEq)]
773-pub struct CornerRadius {
774- pub top_left: f32,
775- pub top_right: f32,
776- pub bottom_right: f32,
777- pub bottom_left: f32,
778-}
779-
780-impl From<CornerRadius> for [f32; 4] {
781- fn from(value: CornerRadius) -> Self {
782- [
783- value.top_left,
784- value.top_right,
785- value.bottom_right,
786- value.bottom_left,
787- ]
788- }
789-}
790-
791-impl From<f32> for CornerRadius {
792- fn from(value: f32) -> Self {
793- Self {
794- top_left: value,
795- top_right: value,
796- bottom_right: value,
797- bottom_left: value,
798- }
799- }
800-}
801-
802-#[derive(knuffel::DecodeScalar, Debug, Clone, Copy, PartialEq, Eq)]
803-pub enum BlockOutFrom {
804- Screencast,
805- ScreenCapture,
806-}
807-
808-#[derive(knuffel::Decode, Debug, Default, Clone, Copy, PartialEq)]
809-pub struct BorderRule {
810- #[knuffel(child)]
811- pub off: bool,
812- #[knuffel(child)]
813- pub on: bool,
814- #[knuffel(child, unwrap(argument))]
815- pub width: Option<FloatOrInt<0, 65535>>,
816- #[knuffel(child)]
817- pub active_color: Option<Color>,
818- #[knuffel(child)]
819- pub inactive_color: Option<Color>,
820- #[knuffel(child)]
821- pub urgent_color: Option<Color>,
822- #[knuffel(child)]
823- pub active_gradient: Option<Gradient>,
824- #[knuffel(child)]
825- pub inactive_gradient: Option<Gradient>,
826- #[knuffel(child)]
827- pub urgent_gradient: Option<Gradient>,
828-}
829-
830-#[derive(knuffel::Decode, Debug, Default, Clone, Copy, PartialEq)]
831-pub struct ShadowRule {
832- #[knuffel(child)]
833- pub off: bool,
834- #[knuffel(child)]
835- pub on: bool,
836- #[knuffel(child)]
837- pub offset: Option<ShadowOffset>,
838- #[knuffel(child, unwrap(argument))]
839- pub softness: Option<FloatOrInt<0, 1024>>,
840- #[knuffel(child, unwrap(argument))]
841- pub spread: Option<FloatOrInt<-1024, 1024>>,
842- #[knuffel(child, unwrap(argument))]
843- pub draw_behind_window: Option<bool>,
844- #[knuffel(child)]
845- pub color: Option<Color>,
846- #[knuffel(child)]
847- pub inactive_color: Option<Color>,
848-}
849-
850-#[derive(knuffel::Decode, Debug, Default, Clone, Copy, PartialEq)]
851-pub struct TabIndicatorRule {
852- #[knuffel(child)]
853- pub active_color: Option<Color>,
854- #[knuffel(child)]
855- pub inactive_color: Option<Color>,
856- #[knuffel(child)]
857- pub urgent_color: Option<Color>,
858- #[knuffel(child)]
859- pub active_gradient: Option<Gradient>,
860- #[knuffel(child)]
861- pub inactive_gradient: Option<Gradient>,
862- #[knuffel(child)]
863- pub urgent_gradient: Option<Gradient>,
864-}
865-
866272 #[derive(Debug, Default, PartialEq)]
867273 pub struct Binds(pub Vec<Bind>);
868274
@@ -1776,383 +1182,6 @@ impl Default for Config {
17761182 }
17771183 }
17781184
1779-impl BorderRule {
1780- pub fn merge_with(&mut self, other: &Self) {
1781- if other.off {
1782- self.off = true;
1783- self.on = false;
1784- }
1785-
1786- if other.on {
1787- self.off = false;
1788- self.on = true;
1789- }
1790-
1791- if let Some(x) = other.width {
1792- self.width = Some(x);
1793- }
1794- if let Some(x) = other.active_color {
1795- self.active_color = Some(x);
1796- }
1797- if let Some(x) = other.inactive_color {
1798- self.inactive_color = Some(x);
1799- }
1800- if let Some(x) = other.urgent_color {
1801- self.urgent_color = Some(x);
1802- }
1803- if let Some(x) = other.active_gradient {
1804- self.active_gradient = Some(x);
1805- }
1806- if let Some(x) = other.inactive_gradient {
1807- self.inactive_gradient = Some(x);
1808- }
1809- if let Some(x) = other.urgent_gradient {
1810- self.urgent_gradient = Some(x);
1811- }
1812- }
1813-
1814- pub fn resolve_against(&self, mut config: Border) -> Border {
1815- config.off |= self.off;
1816- if self.on {
1817- config.off = false;
1818- }
1819-
1820- if let Some(x) = self.width {
1821- config.width = x;
1822- }
1823- if let Some(x) = self.active_color {
1824- config.active_color = x;
1825- config.active_gradient = None;
1826- }
1827- if let Some(x) = self.inactive_color {
1828- config.inactive_color = x;
1829- config.inactive_gradient = None;
1830- }
1831- if let Some(x) = self.urgent_color {
1832- config.urgent_color = x;
1833- config.urgent_gradient = None;
1834- }
1835- if let Some(x) = self.active_gradient {
1836- config.active_gradient = Some(x);
1837- }
1838- if let Some(x) = self.inactive_gradient {
1839- config.inactive_gradient = Some(x);
1840- }
1841- if let Some(x) = self.urgent_gradient {
1842- config.urgent_gradient = Some(x);
1843- }
1844-
1845- config
1846- }
1847-}
1848-
1849-impl ShadowRule {
1850- pub fn merge_with(&mut self, other: &Self) {
1851- if other.off {
1852- self.off = true;
1853- self.on = false;
1854- }
1855-
1856- if other.on {
1857- self.off = false;
1858- self.on = true;
1859- }
1860-
1861- if let Some(x) = other.offset {
1862- self.offset = Some(x);
1863- }
1864- if let Some(x) = other.softness {
1865- self.softness = Some(x);
1866- }
1867- if let Some(x) = other.spread {
1868- self.spread = Some(x);
1869- }
1870- if let Some(x) = other.draw_behind_window {
1871- self.draw_behind_window = Some(x);
1872- }
1873- if let Some(x) = other.color {
1874- self.color = Some(x);
1875- }
1876- if let Some(x) = other.inactive_color {
1877- self.inactive_color = Some(x);
1878- }
1879- }
1880-
1881- pub fn resolve_against(&self, mut config: Shadow) -> Shadow {
1882- config.on |= self.on;
1883- if self.off {
1884- config.on = false;
1885- }
1886-
1887- if let Some(x) = self.offset {
1888- config.offset = x;
1889- }
1890- if let Some(x) = self.softness {
1891- config.softness = x;
1892- }
1893- if let Some(x) = self.spread {
1894- config.spread = x;
1895- }
1896- if let Some(x) = self.draw_behind_window {
1897- config.draw_behind_window = x;
1898- }
1899- if let Some(x) = self.color {
1900- config.color = x;
1901- }
1902- if let Some(x) = self.inactive_color {
1903- config.inactive_color = Some(x);
1904- }
1905-
1906- config
1907- }
1908-}
1909-
1910-impl TabIndicatorRule {
1911- pub fn merge_with(&mut self, other: &Self) {
1912- if let Some(x) = other.active_color {
1913- self.active_color = Some(x);
1914- }
1915- if let Some(x) = other.inactive_color {
1916- self.inactive_color = Some(x);
1917- }
1918- if let Some(x) = other.urgent_color {
1919- self.urgent_color = Some(x);
1920- }
1921- if let Some(x) = other.active_gradient {
1922- self.active_gradient = Some(x);
1923- }
1924- if let Some(x) = other.inactive_gradient {
1925- self.inactive_gradient = Some(x);
1926- }
1927- if let Some(x) = other.urgent_gradient {
1928- self.urgent_gradient = Some(x);
1929- }
1930- }
1931-}
1932-
1933-impl CornerRadius {
1934- pub fn fit_to(self, width: f32, height: f32) -> Self {
1935- // Like in CSS: https://drafts.csswg.org/css-backgrounds/#corner-overlap
1936- let reduction = f32::min(
1937- f32::min(
1938- width / (self.top_left + self.top_right),
1939- width / (self.bottom_left + self.bottom_right),
1940- ),
1941- f32::min(
1942- height / (self.top_left + self.bottom_left),
1943- height / (self.top_right + self.bottom_right),
1944- ),
1945- );
1946- let reduction = f32::min(1., reduction);
1947-
1948- Self {
1949- top_left: self.top_left * reduction,
1950- top_right: self.top_right * reduction,
1951- bottom_right: self.bottom_right * reduction,
1952- bottom_left: self.bottom_left * reduction,
1953- }
1954- }
1955-
1956- pub fn expanded_by(mut self, width: f32) -> Self {
1957- // Radius = 0 is preserved, so that square corners remain square.
1958- if self.top_left > 0. {
1959- self.top_left += width;
1960- }
1961- if self.top_right > 0. {
1962- self.top_right += width;
1963- }
1964- if self.bottom_right > 0. {
1965- self.bottom_right += width;
1966- }
1967- if self.bottom_left > 0. {
1968- self.bottom_left += width;
1969- }
1970-
1971- if width < 0. {
1972- self.top_left = self.top_left.max(0.);
1973- self.top_right = self.top_right.max(0.);
1974- self.bottom_left = self.bottom_left.max(0.);
1975- self.bottom_right = self.bottom_right.max(0.);
1976- }
1977-
1978- self
1979- }
1980-
1981- pub fn scaled_by(self, scale: f32) -> Self {
1982- Self {
1983- top_left: self.top_left * scale,
1984- top_right: self.top_right * scale,
1985- bottom_right: self.bottom_right * scale,
1986- bottom_left: self.bottom_left * scale,
1987- }
1988- }
1989-}
1990-
1991-impl FromStr for GradientInterpolation {
1992- type Err = miette::Error;
1993-
1994- fn from_str(s: &str) -> Result<Self, Self::Err> {
1995- let mut iter = s.split_whitespace();
1996- let in_part1 = iter.next();
1997- let in_part2 = iter.next();
1998- let in_part3 = iter.next();
1999-
2000- let Some(in_part1) = in_part1 else {
2001- return Err(miette!("missing color space"));
2002- };
2003-
2004- let color = match in_part1 {
2005- "srgb" => GradientColorSpace::Srgb,
2006- "srgb-linear" => GradientColorSpace::SrgbLinear,
2007- "oklab" => GradientColorSpace::Oklab,
2008- "oklch" => GradientColorSpace::Oklch,
2009- x => {
2010- return Err(miette!(
2011- "invalid color space {x}; can be srgb, srgb-linear, oklab or oklch"
2012- ))
2013- }
2014- };
2015-
2016- let interpolation = if let Some(in_part2) = in_part2 {
2017- if color != GradientColorSpace::Oklch {
2018- return Err(miette!("only oklch color space can have hue interpolation"));
2019- }
2020-
2021- if in_part3 != Some("hue") {
2022- return Err(miette!(
2023- "interpolation must end with \"hue\", like \"oklch shorter hue\""
2024- ));
2025- } else if iter.next().is_some() {
2026- return Err(miette!("unexpected text after hue interpolation"));
2027- } else {
2028- match in_part2 {
2029- "shorter" => HueInterpolation::Shorter,
2030- "longer" => HueInterpolation::Longer,
2031- "increasing" => HueInterpolation::Increasing,
2032- "decreasing" => HueInterpolation::Decreasing,
2033- x => {
2034- return Err(miette!(
2035- "invalid hue interpolation {x}; \
2036- can be shorter, longer, increasing, decreasing"
2037- ))
2038- }
2039- }
2040- }
2041- } else {
2042- HueInterpolation::default()
2043- };
2044-
2045- Ok(Self {
2046- color_space: color,
2047- hue_interpolation: interpolation,
2048- })
2049- }
2050-}
2051-
2052-impl FromStr for Color {
2053- type Err = miette::Error;
2054-
2055- fn from_str(s: &str) -> Result<Self, Self::Err> {
2056- let color = csscolorparser::parse(s)
2057- .into_diagnostic()?
2058- .clamp()
2059- .to_array();
2060- Ok(Self::from_array_unpremul(color))
2061- }
2062-}
2063-
2064-#[derive(knuffel::Decode)]
2065-struct ColorRgba {
2066- #[knuffel(argument)]
2067- r: u8,
2068- #[knuffel(argument)]
2069- g: u8,
2070- #[knuffel(argument)]
2071- b: u8,
2072- #[knuffel(argument)]
2073- a: u8,
2074-}
2075-
2076-impl From<ColorRgba> for Color {
2077- fn from(value: ColorRgba) -> Self {
2078- let ColorRgba { r, g, b, a } = value;
2079- Self::from_array_unpremul([r, g, b, a].map(|x| x as f32 / 255.))
2080- }
2081-}
2082-
2083-// Manual impl to allow both one-argument string and 4-argument RGBA forms.
2084-impl<S> knuffel::Decode<S> for Color
2085-where
2086- S: knuffel::traits::ErrorSpan,
2087-{
2088- fn decode_node(
2089- node: &knuffel::ast::SpannedNode<S>,
2090- ctx: &mut knuffel::decode::Context<S>,
2091- ) -> Result<Self, DecodeError<S>> {
2092- // Check for unexpected type name.
2093- if let Some(type_name) = &node.type_name {
2094- ctx.emit_error(DecodeError::unexpected(
2095- type_name,
2096- "type name",
2097- "no type name expected for this node",
2098- ));
2099- }
2100-
2101- // Get the first argument.
2102- let mut iter_args = node.arguments.iter();
2103- let val = iter_args
2104- .next()
2105- .ok_or_else(|| DecodeError::missing(node, "additional argument is required"))?;
2106-
2107- // Check for unexpected type name.
2108- if let Some(typ) = &val.type_name {
2109- ctx.emit_error(DecodeError::TypeName {
2110- span: typ.span().clone(),
2111- found: Some((**typ).clone()),
2112- expected: knuffel::errors::ExpectedType::no_type(),
2113- rust_type: "str",
2114- });
2115- }
2116-
2117- // Check the argument type.
2118- let rv = match *val.literal {
2119- // If it's a string, use FromStr.
2120- knuffel::ast::Literal::String(ref s) => {
2121- Color::from_str(s).map_err(|e| DecodeError::conversion(&val.literal, e))
2122- }
2123- // Otherwise, fall back to the 4-argument RGBA form.
2124- _ => return ColorRgba::decode_node(node, ctx).map(Color::from),
2125- }?;
2126-
2127- // Check for unexpected following arguments.
2128- if let Some(val) = iter_args.next() {
2129- ctx.emit_error(DecodeError::unexpected(
2130- &val.literal,
2131- "argument",
2132- "unexpected argument",
2133- ));
2134- }
2135-
2136- // Check for unexpected properties and children.
2137- for name in node.properties.keys() {
2138- ctx.emit_error(DecodeError::unexpected(
2139- name,
2140- "property",
2141- format!("unexpected property `{}`", name.escape_default()),
2142- ));
2143- }
2144- for child in node.children.as_ref().map(|lst| &lst[..]).unwrap_or(&[]) {
2145- ctx.emit_error(DecodeError::unexpected(
2146- child,
2147- "node",
2148- format!("unexpected node `{}`", child.node_name.escape_default()),
2149- ));
2150- }
2151-
2152- Ok(rv)
2153- }
2154-}
2155-
21561185 fn expect_only_children<S>(
21571186 node: &knuffel::ast::SpannedNode<S>,
21581187 ctx: &mut knuffel::decode::Context<S>,
@@ -2184,33 +1213,6 @@ fn expect_only_children<S>(
21841213 }
21851214 }
21861215
2187-impl<S> knuffel::Decode<S> for DefaultPresetSize
2188-where
2189- S: knuffel::traits::ErrorSpan,
2190-{
2191- fn decode_node(
2192- node: &knuffel::ast::SpannedNode<S>,
2193- ctx: &mut knuffel::decode::Context<S>,
2194- ) -> Result<Self, DecodeError<S>> {
2195- expect_only_children(node, ctx);
2196-
2197- let mut children = node.children();
2198-
2199- if let Some(child) = children.next() {
2200- if let Some(unwanted_child) = children.next() {
2201- ctx.emit_error(DecodeError::unexpected(
2202- unwanted_child,
2203- "node",
2204- "expected no more than one child",
2205- ));
2206- }
2207- PresetSize::decode_node(child, ctx).map(Some).map(Self)
2208- } else {
2209- Ok(Self(None))
2210- }
2211- }
2212-}
2213-
22141216 fn parse_arg_node<S: knuffel::traits::ErrorSpan, T: knuffel::traits::DecodeScalar<S>>(
22151217 name: &str,
22161218 node: &knuffel::ast::SpannedNode<S>,
@@ -2299,125 +1301,6 @@ impl<S: knuffel::traits::ErrorSpan> knuffel::DecodeScalar<S> for WorkspaceName {
22991301 }
23001302 }
23011303
2302-impl<S> knuffel::Decode<S> for CornerRadius
2303-where
2304- S: knuffel::traits::ErrorSpan,
2305-{
2306- fn decode_node(
2307- node: &knuffel::ast::SpannedNode<S>,
2308- ctx: &mut knuffel::decode::Context<S>,
2309- ) -> Result<Self, DecodeError<S>> {
2310- // Check for unexpected type name.
2311- if let Some(type_name) = &node.type_name {
2312- ctx.emit_error(DecodeError::unexpected(
2313- type_name,
2314- "type name",
2315- "no type name expected for this node",
2316- ));
2317- }
2318-
2319- let decode_radius = |ctx: &mut knuffel::decode::Context<S>,
2320- val: &knuffel::ast::Value<S>| {
2321- // Check for unexpected type name.
2322- if let Some(typ) = &val.type_name {
2323- ctx.emit_error(DecodeError::TypeName {
2324- span: typ.span().clone(),
2325- found: Some((**typ).clone()),
2326- expected: knuffel::errors::ExpectedType::no_type(),
2327- rust_type: "str",
2328- });
2329- }
2330-
2331- // Decode both integers and floats.
2332- let radius = match *val.literal {
2333- knuffel::ast::Literal::Int(ref x) => f32::from(match x.try_into() {
2334- Ok(x) => x,
2335- Err(err) => {
2336- ctx.emit_error(DecodeError::conversion(&val.literal, err));
2337- 0i16
2338- }
2339- }),
2340- knuffel::ast::Literal::Decimal(ref x) => match x.try_into() {
2341- Ok(x) => x,
2342- Err(err) => {
2343- ctx.emit_error(DecodeError::conversion(&val.literal, err));
2344- 0.
2345- }
2346- },
2347- _ => {
2348- ctx.emit_error(DecodeError::scalar_kind(
2349- knuffel::decode::Kind::Int,
2350- &val.literal,
2351- ));
2352- 0.
2353- }
2354- };
2355-
2356- if radius < 0. {
2357- ctx.emit_error(DecodeError::conversion(&val.literal, "radius must be >= 0"));
2358- }
2359-
2360- radius
2361- };
2362-
2363- // Get the first argument.
2364- let mut iter_args = node.arguments.iter();
2365- let val = iter_args
2366- .next()
2367- .ok_or_else(|| DecodeError::missing(node, "additional argument is required"))?;
2368-
2369- let top_left = decode_radius(ctx, val);
2370-
2371- let mut rv = CornerRadius {
2372- top_left,
2373- top_right: top_left,
2374- bottom_right: top_left,
2375- bottom_left: top_left,
2376- };
2377-
2378- if let Some(val) = iter_args.next() {
2379- rv.top_right = decode_radius(ctx, val);
2380-
2381- let val = iter_args.next().ok_or_else(|| {
2382- DecodeError::missing(node, "either 1 or 4 arguments are required")
2383- })?;
2384- rv.bottom_right = decode_radius(ctx, val);
2385-
2386- let val = iter_args.next().ok_or_else(|| {
2387- DecodeError::missing(node, "either 1 or 4 arguments are required")
2388- })?;
2389- rv.bottom_left = decode_radius(ctx, val);
2390-
2391- // Check for unexpected following arguments.
2392- if let Some(val) = iter_args.next() {
2393- ctx.emit_error(DecodeError::unexpected(
2394- &val.literal,
2395- "argument",
2396- "unexpected argument",
2397- ));
2398- }
2399- }
2400-
2401- // Check for unexpected properties and children.
2402- for name in node.properties.keys() {
2403- ctx.emit_error(DecodeError::unexpected(
2404- name,
2405- "property",
2406- format!("unexpected property `{}`", name.escape_default()),
2407- ));
2408- }
2409- for child in node.children.as_ref().map(|lst| &lst[..]).unwrap_or(&[]) {
2410- ctx.emit_error(DecodeError::unexpected(
2411- child,
2412- "node",
2413- format!("unexpected node `{}`", child.node_name.escape_default()),
2414- ));
2415- }
2416-
2417- Ok(rv)
2418- }
2419-}
2420-
24211304 impl<S> knuffel::Decode<S> for Binds
24221305 where
24231306 S: knuffel::traits::ErrorSpan,
@@ -2702,7 +1585,7 @@ impl FromStr for Percent {
27021585
27031586 #[cfg(test)]
27041587 mod tests {
2705- use insta::{assert_debug_snapshot, assert_snapshot};
1588+ use insta::assert_debug_snapshot;
27061589 use niri_ipc::PositionChange;
27071590 use pretty_assertions::assert_eq;
27081591
@@ -4452,81 +3335,6 @@ mod tests {
44523335 assert!("10% ".parse::<PositionChange>().is_err());
44533336 }
44543337
4455- #[test]
4456- fn parse_gradient_interpolation() {
4457- assert_eq!(
4458- "srgb".parse::<GradientInterpolation>().unwrap(),
4459- GradientInterpolation {
4460- color_space: GradientColorSpace::Srgb,
4461- ..Default::default()
4462- }
4463- );
4464- assert_eq!(
4465- "srgb-linear".parse::<GradientInterpolation>().unwrap(),
4466- GradientInterpolation {
4467- color_space: GradientColorSpace::SrgbLinear,
4468- ..Default::default()
4469- }
4470- );
4471- assert_eq!(
4472- "oklab".parse::<GradientInterpolation>().unwrap(),
4473- GradientInterpolation {
4474- color_space: GradientColorSpace::Oklab,
4475- ..Default::default()
4476- }
4477- );
4478- assert_eq!(
4479- "oklch".parse::<GradientInterpolation>().unwrap(),
4480- GradientInterpolation {
4481- color_space: GradientColorSpace::Oklch,
4482- ..Default::default()
4483- }
4484- );
4485- assert_eq!(
4486- "oklch shorter hue"
4487- .parse::<GradientInterpolation>()
4488- .unwrap(),
4489- GradientInterpolation {
4490- color_space: GradientColorSpace::Oklch,
4491- hue_interpolation: HueInterpolation::Shorter,
4492- }
4493- );
4494- assert_eq!(
4495- "oklch longer hue".parse::<GradientInterpolation>().unwrap(),
4496- GradientInterpolation {
4497- color_space: GradientColorSpace::Oklch,
4498- hue_interpolation: HueInterpolation::Longer,
4499- }
4500- );
4501- assert_eq!(
4502- "oklch decreasing hue"
4503- .parse::<GradientInterpolation>()
4504- .unwrap(),
4505- GradientInterpolation {
4506- color_space: GradientColorSpace::Oklch,
4507- hue_interpolation: HueInterpolation::Decreasing,
4508- }
4509- );
4510- assert_eq!(
4511- "oklch increasing hue"
4512- .parse::<GradientInterpolation>()
4513- .unwrap(),
4514- GradientInterpolation {
4515- color_space: GradientColorSpace::Oklch,
4516- hue_interpolation: HueInterpolation::Increasing,
4517- }
4518- );
4519-
4520- assert!("".parse::<GradientInterpolation>().is_err());
4521- assert!("srgb shorter hue".parse::<GradientInterpolation>().is_err());
4522- assert!("oklch shorter".parse::<GradientInterpolation>().is_err());
4523- assert!("oklch shorter h".parse::<GradientInterpolation>().is_err());
4524- assert!("oklch a hue".parse::<GradientInterpolation>().is_err());
4525- assert!("oklch shorter hue a"
4526- .parse::<GradientInterpolation>()
4527- .is_err());
4528- }
4529-
45303338 #[test]
45313339 fn parse_iso_level_shifts() {
45323340 assert_eq!(
@@ -4566,63 +3374,4 @@ mod tests {
45663374 assert_eq!(config.input.keyboard.repeat_delay, 600);
45673375 assert_eq!(config.input.keyboard.repeat_rate, 25);
45683376 }
4569-
4570- #[test]
4571- fn test_border_rule_on_off_merging() {
4572- fn is_on(config: &str, rules: &[&str]) -> String {
4573- let mut resolved = BorderRule {
4574- off: false,
4575- on: false,
4576- width: None,
4577- active_color: None,
4578- inactive_color: None,
4579- urgent_color: None,
4580- active_gradient: None,
4581- inactive_gradient: None,
4582- urgent_gradient: None,
4583- };
4584-
4585- for rule in rules.iter().copied() {
4586- let rule = BorderRule {
4587- off: rule == "off" || rule == "off,on",
4588- on: rule == "on" || rule == "off,on",
4589- ..Default::default()
4590- };
4591-
4592- resolved.merge_with(&rule);
4593- }
4594-
4595- let config = Border {
4596- off: config == "off",
4597- ..Default::default()
4598- };
4599-
4600- if resolved.resolve_against(config).off {
4601- "off"
4602- } else {
4603- "on"
4604- }
4605- .to_owned()
4606- }
4607-
4608- assert_snapshot!(is_on("off", &[]), @"off");
4609- assert_snapshot!(is_on("off", &["off"]), @"off");
4610- assert_snapshot!(is_on("off", &["on"]), @"on");
4611- assert_snapshot!(is_on("off", &["off,on"]), @"on");
4612-
4613- assert_snapshot!(is_on("on", &[]), @"on");
4614- assert_snapshot!(is_on("on", &["off"]), @"off");
4615- assert_snapshot!(is_on("on", &["on"]), @"on");
4616- assert_snapshot!(is_on("on", &["off,on"]), @"on");
4617-
4618- assert_snapshot!(is_on("off", &["off", "off"]), @"off");
4619- assert_snapshot!(is_on("off", &["off", "on"]), @"on");
4620- assert_snapshot!(is_on("off", &["on", "off"]), @"off");
4621- assert_snapshot!(is_on("off", &["on", "on"]), @"on");
4622-
4623- assert_snapshot!(is_on("on", &["off", "off"]), @"off");
4624- assert_snapshot!(is_on("on", &["off", "on"]), @"on");
4625- assert_snapshot!(is_on("on", &["on", "off"]), @"off");
4626- assert_snapshot!(is_on("on", &["on", "on"]), @"on");
4627- }
46283377 }
niri-config/src/window_rule.rsmodified+3 -4
@@ -1,10 +1,9 @@
11 use niri_ipc::ColumnDisplay;
22
3+use crate::appearance::{BlockOutFrom, BorderRule, CornerRadius, ShadowRule, TabIndicatorRule};
4+use crate::layout::DefaultPresetSize;
35 use crate::utils::RegexEq;
4-use crate::{
5- BlockOutFrom, BorderRule, CornerRadius, DefaultPresetSize, FloatOrInt, ShadowRule,
6- TabIndicatorRule,
7-};
6+use crate::FloatOrInt;
87
98 #[derive(knuffel::Decode, Debug, Default, Clone, PartialEq)]
109 pub struct WindowRule {