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 | use knuffel::errors::DecodeError; | 6 | use knuffel::errors::DecodeError; |
| 2 | use knuffel::Decode as _; | 7 | use knuffel::Decode as _; |
| 3 | 8 | ||
| 4 | -use crate::utils::{expect_only_children, parse_arg_node, MergeWith}; | 9 | +use crate::utils::{expand_home_path, expect_only_children, parse_arg_node, MergeWith}; |
| 5 | -use crate::FloatOrInt; | 10 | +use crate::{FloatOrInt, Includes}; |
| 6 | 11 | ||
| 7 | #[derive(Debug, Clone, PartialEq)] | 12 | #[derive(Debug, Clone, PartialEq)] |
| 8 | pub struct Animations { | 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 | #[derive(Debug, Clone, PartialEq)] | 210 | #[derive(Debug, Clone, PartialEq)] |
| 155 | pub struct WindowOpenAnim { | 211 | pub struct WindowOpenAnim { |
| 156 | pub anim: Animation, | 212 | pub anim: Animation, |
| @@ -383,7 +439,11 @@ where | |||
| 383 | let mut custom_shader = None; | 439 | let mut custom_shader = None; |
| 384 | let anim = Animation::decode_node(node, ctx, default, |child, ctx| { | 440 | let anim = Animation::decode_node(node, ctx, default, |child, ctx| { |
| 385 | if &**child.node_name == "custom-shader" { | 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 | Ok(true) | 447 | Ok(true) |
| 388 | } else { | 448 | } else { |
| 389 | Ok(false) | 449 | Ok(false) |
| @@ -409,7 +469,11 @@ where | |||
| 409 | let mut custom_shader = None; | 469 | let mut custom_shader = None; |
| 410 | let anim = Animation::decode_node(node, ctx, default, |child, ctx| { | 470 | let anim = Animation::decode_node(node, ctx, default, |child, ctx| { |
| 411 | if &**child.node_name == "custom-shader" { | 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 | Ok(true) | 477 | Ok(true) |
| 414 | } else { | 478 | } else { |
| 415 | Ok(false) | 479 | Ok(false) |
| @@ -435,7 +499,11 @@ where | |||
| 435 | let mut custom_shader = None; | 499 | let mut custom_shader = None; |
| 436 | let anim = Animation::decode_node(node, ctx, default, |child, ctx| { | 500 | let anim = Animation::decode_node(node, ctx, default, |child, ctx| { |
| 437 | if &**child.node_name == "custom-shader" { | 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 | Ok(true) | 507 | Ok(true) |
| 440 | } else { | 508 | } else { |
| 441 | Ok(false) | 509 | Ok(false) |