| 1 | use niri_config::layer_rule::{LayerRule, Match}; |
| 2 | use niri_config::BlockOutFrom; |
| 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 | pub block_out_from: Option<BlockOutFrom>, |
| 15 | } |
| 16 | |
| 17 | impl ResolvedLayerRules { |
| 18 | pub const fn empty() -> Self { |
| 19 | Self { |
| 20 | opacity: None, |
| 21 | block_out_from: None, |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | pub fn compute(rules: &[LayerRule], surface: &LayerSurface, is_at_startup: bool) -> Self { |
| 26 | let _span = tracy_client::span!("ResolvedLayerRules::compute"); |
| 27 | |
| 28 | let mut resolved = ResolvedLayerRules::empty(); |
| 29 | |
| 30 | for rule in rules { |
| 31 | let matches = |m: &Match| { |
| 32 | if let Some(at_startup) = m.at_startup { |
| 33 | if at_startup != is_at_startup { |
| 34 | return false; |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | surface_matches(surface, m) |
| 39 | }; |
| 40 | |
| 41 | if !(rule.matches.is_empty() || rule.matches.iter().any(matches)) { |
| 42 | continue; |
| 43 | } |
| 44 | |
| 45 | if rule.excludes.iter().any(matches) { |
| 46 | continue; |
| 47 | } |
| 48 | |
| 49 | if let Some(x) = rule.opacity { |
| 50 | resolved.opacity = Some(x); |
| 51 | } |
| 52 | if let Some(x) = rule.block_out_from { |
| 53 | resolved.block_out_from = Some(x); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | resolved |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | fn surface_matches(surface: &LayerSurface, m: &Match) -> bool { |
| 62 | if let Some(namespace_re) = &m.namespace { |
| 63 | if !namespace_re.0.is_match(surface.namespace()) { |
| 64 | return false; |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | true |
| 69 | } |