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