| 1 | use niri_config::layer_rule::{LayerRule, Match}; |
| 2 | use niri_config::utils::MergeWith as _; |
| 3 | use niri_config::{BlockOutFrom, CornerRadius, ShadowRule}; |
| 4 | use smithay::desktop::LayerSurface; |
| 5 | |
| 6 | pub mod mapped; |
| 7 | pub use mapped::MappedLayer; |
| 8 | |
| 9 | |
| 10 | #[derive(Debug, PartialEq)] |
| 11 | pub struct ResolvedLayerRules { |
| 12 | |
| 13 | pub opacity: Option<f32>, |
| 14 | |
| 15 | |
| 16 | pub block_out_from: Option<BlockOutFrom>, |
| 17 | |
| 18 | |
| 19 | pub shadow: ShadowRule, |
| 20 | |
| 21 | |
| 22 | pub geometry_corner_radius: Option<CornerRadius>, |
| 23 | |
| 24 | |
| 25 | pub place_within_backdrop: bool, |
| 26 | |
| 27 | |
| 28 | pub baba_is_float: bool, |
| 29 | } |
| 30 | |
| 31 | impl ResolvedLayerRules { |
| 32 | pub const fn empty() -> Self { |
| 33 | Self { |
| 34 | opacity: None, |
| 35 | block_out_from: None, |
| 36 | shadow: ShadowRule { |
| 37 | off: false, |
| 38 | on: false, |
| 39 | offset: None, |
| 40 | softness: None, |
| 41 | spread: None, |
| 42 | draw_behind_window: None, |
| 43 | color: None, |
| 44 | inactive_color: None, |
| 45 | }, |
| 46 | geometry_corner_radius: None, |
| 47 | place_within_backdrop: false, |
| 48 | baba_is_float: false, |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | pub fn compute(rules: &[LayerRule], surface: &LayerSurface, is_at_startup: bool) -> Self { |
| 53 | let _span = tracy_client::span!("ResolvedLayerRules::compute"); |
| 54 | |
| 55 | let mut resolved = ResolvedLayerRules::empty(); |
| 56 | |
| 57 | for rule in rules { |
| 58 | let matches = |m: &Match| { |
| 59 | if let Some(at_startup) = m.at_startup { |
| 60 | if at_startup != is_at_startup { |
| 61 | return false; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | surface_matches(surface, m) |
| 66 | }; |
| 67 | |
| 68 | if !(rule.matches.is_empty() || rule.matches.iter().any(matches)) { |
| 69 | continue; |
| 70 | } |
| 71 | |
| 72 | if rule.excludes.iter().any(matches) { |
| 73 | continue; |
| 74 | } |
| 75 | |
| 76 | if let Some(x) = rule.opacity { |
| 77 | resolved.opacity = Some(x); |
| 78 | } |
| 79 | if let Some(x) = rule.block_out_from { |
| 80 | resolved.block_out_from = Some(x); |
| 81 | } |
| 82 | if let Some(x) = rule.geometry_corner_radius { |
| 83 | resolved.geometry_corner_radius = Some(x); |
| 84 | } |
| 85 | if let Some(x) = rule.place_within_backdrop { |
| 86 | resolved.place_within_backdrop = x; |
| 87 | } |
| 88 | if let Some(x) = rule.baba_is_float { |
| 89 | resolved.baba_is_float = x; |
| 90 | } |
| 91 | |
| 92 | resolved.shadow.merge_with(&rule.shadow); |
| 93 | } |
| 94 | |
| 95 | resolved |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | fn surface_matches(surface: &LayerSurface, m: &Match) -> bool { |
| 100 | if let Some(namespace_re) = &m.namespace { |
| 101 | if !namespace_re.0.is_match(surface.namespace()) { |
| 102 | return false; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | true |
| 107 | } |