config: Introduce MergeWith trait
Browse files at this commita2727ba2c9cfb22e4184235c6dfd8d8819328169Parent: 8df62315 changed files +34 -6
- niri-config/src/appearance.rs +11 -6
- niri-config/src/utils.rs +3 -0
- niri-config/src/utils/merge_with.rs +18 -0
- src/layer/mod.rs +1 -0
- src/window/mod.rs +1 -0
niri-config/src/appearance.rsmodified+11 -6
| @@ -5,6 +5,7 @@ use knuffel::errors::DecodeError; | |||
| 5 | use miette::{miette, IntoDiagnostic as _}; | 5 | use miette::{miette, IntoDiagnostic as _}; |
| 6 | use smithay::backend::renderer::Color32F; | 6 | use smithay::backend::renderer::Color32F; |
| 7 | 7 | ||
| 8 | +use crate::utils::MergeWith; | ||
| 8 | use crate::FloatOrInt; | 9 | use crate::FloatOrInt; |
| 9 | 10 | ||
| 10 | pub const DEFAULT_BACKGROUND_COLOR: Color = Color::from_array_unpremul([0.25, 0.25, 0.25, 1.]); | 11 | pub const DEFAULT_BACKGROUND_COLOR: Color = Color::from_array_unpremul([0.25, 0.25, 0.25, 1.]); |
| @@ -563,8 +564,8 @@ pub struct TabIndicatorRule { | |||
| 563 | pub urgent_gradient: Option<Gradient>, | 564 | pub urgent_gradient: Option<Gradient>, |
| 564 | } | 565 | } |
| 565 | 566 | ||
| 566 | -impl BorderRule { | 567 | +impl MergeWith<Self> for BorderRule { |
| 567 | - pub fn merge_with(&mut self, other: &Self) { | 568 | + fn merge_with(&mut self, other: &Self) { |
| 568 | if other.off { | 569 | if other.off { |
| 569 | self.off = true; | 570 | self.off = true; |
| 570 | self.on = false; | 571 | self.on = false; |
| @@ -600,7 +601,9 @@ impl BorderRule { | |||
| 600 | self.urgent_gradient = Some(x); | 601 | self.urgent_gradient = Some(x); |
| 601 | } | 602 | } |
| 602 | } | 603 | } |
| 604 | +} | ||
| 603 | 605 | ||
| 606 | +impl BorderRule { | ||
| 604 | pub fn resolve_against(&self, mut config: Border) -> Border { | 607 | pub fn resolve_against(&self, mut config: Border) -> Border { |
| 605 | config.off |= self.off; | 608 | config.off |= self.off; |
| 606 | if self.on { | 609 | if self.on { |
| @@ -636,8 +639,8 @@ impl BorderRule { | |||
| 636 | } | 639 | } |
| 637 | } | 640 | } |
| 638 | 641 | ||
| 639 | -impl ShadowRule { | 642 | +impl MergeWith<Self> for ShadowRule { |
| 640 | - pub fn merge_with(&mut self, other: &Self) { | 643 | + fn merge_with(&mut self, other: &Self) { |
| 641 | if other.off { | 644 | if other.off { |
| 642 | self.off = true; | 645 | self.off = true; |
| 643 | self.on = false; | 646 | self.on = false; |
| @@ -667,7 +670,9 @@ impl ShadowRule { | |||
| 667 | self.inactive_color = Some(x); | 670 | self.inactive_color = Some(x); |
| 668 | } | 671 | } |
| 669 | } | 672 | } |
| 673 | +} | ||
| 670 | 674 | ||
| 675 | +impl ShadowRule { | ||
| 671 | pub fn resolve_against(&self, mut config: Shadow) -> Shadow { | 676 | pub fn resolve_against(&self, mut config: Shadow) -> Shadow { |
| 672 | config.on |= self.on; | 677 | config.on |= self.on; |
| 673 | if self.off { | 678 | if self.off { |
| @@ -697,8 +702,8 @@ impl ShadowRule { | |||
| 697 | } | 702 | } |
| 698 | } | 703 | } |
| 699 | 704 | ||
| 700 | -impl TabIndicatorRule { | 705 | +impl MergeWith<Self> for TabIndicatorRule { |
| 701 | - pub fn merge_with(&mut self, other: &Self) { | 706 | + fn merge_with(&mut self, other: &Self) { |
| 702 | if let Some(x) = other.active_color { | 707 | if let Some(x) = other.active_color { |
| 703 | self.active_color = Some(x); | 708 | self.active_color = Some(x); |
| 704 | self.active_gradient = None; | 709 | self.active_gradient = None; |
niri-config/src/utils.rsmodified+3 -0
| @@ -4,6 +4,9 @@ use knuffel::errors::DecodeError; | |||
| 4 | use miette::miette; | 4 | use miette::miette; |
| 5 | use regex::Regex; | 5 | use regex::Regex; |
| 6 | 6 | ||
| 7 | +mod merge_with; | ||
| 8 | +pub use merge_with::*; | ||
| 9 | + | ||
| 7 | #[derive(Debug, Clone, Copy, PartialEq)] | 10 | #[derive(Debug, Clone, Copy, PartialEq)] |
| 8 | pub struct Percent(pub f64); | 11 | pub struct Percent(pub f64); |
| 9 | 12 | ||
niri-config/src/utils/merge_with.rsadded+18 -0
| @@ -0,0 +1,18 @@ | |||
| 1 | +pub trait MergeWith<T> { | ||
| 2 | + fn merge_with(&mut self, part: &T); | ||
| 3 | + | ||
| 4 | + fn merged_with(mut self, part: &T) -> Self | ||
| 5 | + where | ||
| 6 | + Self: Sized, | ||
| 7 | + { | ||
| 8 | + self.merge_with(part); | ||
| 9 | + self | ||
| 10 | + } | ||
| 11 | + | ||
| 12 | + fn from_part(part: &T) -> Self | ||
| 13 | + where | ||
| 14 | + Self: Default + Sized, | ||
| 15 | + { | ||
| 16 | + Self::default().merged_with(part) | ||
| 17 | + } | ||
| 18 | +} | ||
src/layer/mod.rsmodified+1 -0
| @@ -1,4 +1,5 @@ | |||
| 1 | use niri_config::layer_rule::{LayerRule, Match}; | 1 | use niri_config::layer_rule::{LayerRule, Match}; |
| 2 | +use niri_config::utils::MergeWith as _; | ||
| 2 | use niri_config::{BlockOutFrom, CornerRadius, ShadowRule}; | 3 | use niri_config::{BlockOutFrom, CornerRadius, ShadowRule}; |
| 3 | use smithay::desktop::LayerSurface; | 4 | use smithay::desktop::LayerSurface; |
| 4 | 5 | ||
src/window/mod.rsmodified+1 -0
| @@ -1,5 +1,6 @@ | |||
| 1 | use std::cmp::{max, min}; | 1 | use std::cmp::{max, min}; |
| 2 | 2 | ||
| 3 | +use niri_config::utils::MergeWith as _; | ||
| 3 | use niri_config::window_rule::{Match, WindowRule}; | 4 | use niri_config::window_rule::{Match, WindowRule}; |
| 4 | use niri_config::{ | 5 | use niri_config::{ |
| 5 | BlockOutFrom, BorderRule, CornerRadius, FloatingPosition, PresetSize, ShadowRule, | 6 | BlockOutFrom, BorderRule, CornerRadius, FloatingPosition, PresetSize, ShadowRule, |