config: allow specifying `custom-shader` by path
Browse files at this commitd9cb1c0f87f3dc468431d2356dbdffd2237ed0c3Parent: 180e6c31 changed file +73 -5
niri-config/src/animations.rsmodified+73 -5
| @@ -1,8 +1,13 @@ | ||
| 1 | +use std::cell::RefCell; | |
| 2 | +use std::fs; | |
| 3 | +use std::rc::Rc; | |
| 4 | + | |
| 5 | +use knuffel::ast::SpannedNode; | |
| 1 | 6 | use knuffel::errors::DecodeError; |
| 2 | 7 | use knuffel::Decode as _; |
| 3 | 8 | |
| 4 | -use crate::utils::{expect_only_children, parse_arg_node, MergeWith}; | |
| 5 | -use crate::FloatOrInt; | |
| 9 | +use crate::utils::{expand_home_path, expect_only_children, parse_arg_node, MergeWith}; | |
| 10 | +use crate::{FloatOrInt, Includes}; | |
| 6 | 11 | |
| 7 | 12 | #[derive(Debug, Clone, PartialEq)] |
| 8 | 13 | pub struct Animations { |
| @@ -151,6 +156,57 @@ impl Default for WorkspaceSwitchAnim { | ||
| 151 | 156 | } |
| 152 | 157 | } |
| 153 | 158 | |
| 159 | +fn parse_custom_shader_path<S: knuffel::traits::ErrorSpan>( | |
| 160 | + spanned: &SpannedNode<S>, | |
| 161 | + ctx: &mut knuffel::decode::Context<S>, | |
| 162 | +) -> Result<String, DecodeError<S>> { | |
| 163 | + let mut shader_text = None; | |
| 164 | + | |
| 165 | + for (name, val) in &spanned.properties { | |
| 166 | + if &***name == "path" { | |
| 167 | + let path_val = knuffel::traits::DecodeScalar::decode(val, ctx)?; | |
| 168 | + let Some(path) = expand_home_path(path_val, spanned, ctx) else { | |
| 169 | + continue; | |
| 170 | + }; | |
| 171 | + | |
| 172 | + let includes = ctx.get::<Rc<RefCell<Includes>>>().unwrap(); | |
| 173 | + includes.borrow_mut().0.push(path.to_path_buf()); | |
| 174 | + | |
| 175 | + match fs::read_to_string(&path) { | |
| 176 | + Ok(text) => shader_text = Some(text), | |
| 177 | + Err(e) => ctx.emit_error(DecodeError::missing( | |
| 178 | + spanned, | |
| 179 | + format!("failed to read custom shader from {path:?}: {e}"), | |
| 180 | + )), | |
| 181 | + } | |
| 182 | + } else { | |
| 183 | + ctx.emit_error(DecodeError::unexpected( | |
| 184 | + &val.literal, | |
| 185 | + "property", | |
| 186 | + format!("unexpected property `{}`", name.escape_default()), | |
| 187 | + )); | |
| 188 | + } | |
| 189 | + } | |
| 190 | + | |
| 191 | + for arg in &spanned.arguments { | |
| 192 | + ctx.emit_error(DecodeError::unexpected( | |
| 193 | + &arg.literal, | |
| 194 | + "argument", | |
| 195 | + "unexpected argument", | |
| 196 | + )); | |
| 197 | + } | |
| 198 | + | |
| 199 | + for child in spanned.children() { | |
| 200 | + ctx.emit_error(DecodeError::unexpected( | |
| 201 | + child, | |
| 202 | + "child", | |
| 203 | + "unexpected children", | |
| 204 | + )); | |
| 205 | + } | |
| 206 | + | |
| 207 | + shader_text.ok_or_else(|| DecodeError::missing(spanned, "custom shader path missing")) | |
| 208 | +} | |
| 209 | + | |
| 154 | 210 | #[derive(Debug, Clone, PartialEq)] |
| 155 | 211 | pub struct WindowOpenAnim { |
| 156 | 212 | pub anim: Animation, |
| @@ -383,7 +439,11 @@ where | ||
| 383 | 439 | let mut custom_shader = None; |
| 384 | 440 | let anim = Animation::decode_node(node, ctx, default, |child, ctx| { |
| 385 | 441 | if &**child.node_name == "custom-shader" { |
| 386 | - custom_shader = parse_arg_node("custom-shader", child, ctx)?; | |
| 442 | + custom_shader = if child.properties.contains_key("path") { | |
| 443 | + Some(parse_custom_shader_path(child, ctx)?) | |
| 444 | + } else { | |
| 445 | + Some(parse_arg_node("custom-shader", child, ctx)?) | |
| 446 | + }; | |
| 387 | 447 | Ok(true) |
| 388 | 448 | } else { |
| 389 | 449 | Ok(false) |
| @@ -409,7 +469,11 @@ where | ||
| 409 | 469 | let mut custom_shader = None; |
| 410 | 470 | let anim = Animation::decode_node(node, ctx, default, |child, ctx| { |
| 411 | 471 | if &**child.node_name == "custom-shader" { |
| 412 | - custom_shader = parse_arg_node("custom-shader", child, ctx)?; | |
| 472 | + custom_shader = if child.properties.contains_key("path") { | |
| 473 | + Some(parse_custom_shader_path(child, ctx)?) | |
| 474 | + } else { | |
| 475 | + Some(parse_arg_node("custom-shader", child, ctx)?) | |
| 476 | + }; | |
| 413 | 477 | Ok(true) |
| 414 | 478 | } else { |
| 415 | 479 | Ok(false) |
| @@ -435,7 +499,11 @@ where | ||
| 435 | 499 | let mut custom_shader = None; |
| 436 | 500 | let anim = Animation::decode_node(node, ctx, default, |child, ctx| { |
| 437 | 501 | if &**child.node_name == "custom-shader" { |
| 438 | - custom_shader = parse_arg_node("custom-shader", child, ctx)?; | |
| 502 | + custom_shader = if child.properties.contains_key("path") { | |
| 503 | + Some(parse_custom_shader_path(child, ctx)?) | |
| 504 | + } else { | |
| 505 | + Some(parse_arg_node("custom-shader", child, ctx)?) | |
| 506 | + }; | |
| 439 | 507 | Ok(true) |
| 440 | 508 | } else { |
| 441 | 509 | Ok(false) |