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