forge
Explore
Log in

Don't you dare go Hollow

Parse CSS colors in {in,}active-color

Browse files at this commit

494e98c1235133fd5267bfdfb9a5468f1115deffIvan MolodetskikhParent: ec156a8
niri-config/src/lib.rsmodified+93 -6
@@ -422,15 +422,11 @@ impl From<Border> for FocusRing {
422 }422 }
423 }423 }
424 424
425-#[derive(knuffel::Decode, Debug, Default, Clone, Copy, PartialEq, Eq)]425+#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
426 pub struct Color {426 pub struct Color {
427- #[knuffel(argument)]
428 pub r: u8,427 pub r: u8,
429- #[knuffel(argument)]
430 pub g: u8,428 pub g: u8,
431- #[knuffel(argument)]
432 pub b: u8,429 pub b: u8,
433- #[knuffel(argument)]
434 pub a: u8,430 pub a: u8,
435 }431 }
436 432
@@ -849,6 +845,97 @@ impl FromStr for Color {
849 }845 }
850 }846 }
851 847
848+#[derive(knuffel::Decode)]
849+struct ColorRgba {
850+ #[knuffel(argument)]
851+ r: u8,
852+ #[knuffel(argument)]
853+ g: u8,
854+ #[knuffel(argument)]
855+ b: u8,
856+ #[knuffel(argument)]
857+ a: u8,
858+}
859+
860+impl From<ColorRgba> for Color {
861+ fn from(value: ColorRgba) -> Self {
862+ let ColorRgba { r, g, b, a } = value;
863+ Self { r, g, b, a }
864+ }
865+}
866+
867+// Manual impl to allow both one-argument string and 4-argument RGBA forms.
868+impl<S> knuffel::Decode<S> for Color
869+where
870+ S: knuffel::traits::ErrorSpan,
871+{
872+ fn decode_node(
873+ node: &knuffel::ast::SpannedNode<S>,
874+ ctx: &mut knuffel::decode::Context<S>,
875+ ) -> Result<Self, knuffel::errors::DecodeError<S>> {
876+ // Check for unexpected type name.
877+ if let Some(type_name) = &node.type_name {
878+ ctx.emit_error(knuffel::errors::DecodeError::unexpected(
879+ type_name,
880+ "type name",
881+ "no type name expected for this node",
882+ ));
883+ }
884+
885+ // Get the first argument.
886+ let mut iter_args = node.arguments.iter();
887+ let val = iter_args.next().ok_or_else(|| {
888+ knuffel::errors::DecodeError::missing(node, "additional argument is required")
889+ })?;
890+
891+ // Check for unexpected type name.
892+ if let Some(typ) = &val.type_name {
893+ ctx.emit_error(knuffel::errors::DecodeError::TypeName {
894+ span: typ.span().clone(),
895+ found: Some((**typ).clone()),
896+ expected: knuffel::errors::ExpectedType::no_type(),
897+ rust_type: "str",
898+ });
899+ }
900+
901+ // Check the argument type.
902+ let rv = match *val.literal {
903+ // If it's a string, use FromStr.
904+ knuffel::ast::Literal::String(ref s) => Color::from_str(s)
905+ .map_err(|e| knuffel::errors::DecodeError::conversion(&val.literal, e)),
906+ // Otherwise, fall back to the 4-argument RGBA form.
907+ _ => return ColorRgba::decode_node(node, ctx).map(Color::from),
908+ }?;
909+
910+ // Check for unexpected following arguments.
911+ if let Some(val) = iter_args.next() {
912+ ctx.emit_error(knuffel::errors::DecodeError::unexpected(
913+ &val.literal,
914+ "argument",
915+ "unexpected argument",
916+ ));
917+ }
918+
919+ // Check for unexpected properties and children.
920+ for name in node.properties.keys() {
921+ ctx.emit_error(knuffel::errors::DecodeError::unexpected(
922+ name,
923+ "property",
924+ format!("unexpected property `{}`", name.escape_default()),
925+ ));
926+ }
927+ for child in node.children.as_ref().map(|lst| &lst[..]).unwrap_or(&[]) {
928+ ctx.emit_error(knuffel::errors::DecodeError::unexpected(
929+ child,
930+ "node",
931+ format!("unexpected node `{}`", child.node_name.escape_default()),
932+ ));
933+ }
934+
935+ Ok(rv)
936+ }
937+}
938+
852 impl FromStr for Mode {939 impl FromStr for Mode {
853 type Err = miette::Error;940 type Err = miette::Error;
854 941
@@ -1025,7 +1112,7 @@ mod tests {
1025 1112
1026 border {1113 border {
1027 width 31114 width 3
1028- inactive-color 255 200 100 01115+ inactive-color "rgba(255, 200, 100, 0.0)"
1029 }1116 }
1030 1117
1031 preset-column-widths {1118 preset-column-widths {
resources/default-config.kdlmodified+14 -10
@@ -113,18 +113,22 @@ layout {
113 // How many logical pixels the ring extends out from the windows.113 // How many logical pixels the ring extends out from the windows.
114 width 4114 width 4
115 115
116- // Color of the ring on the active monitor: red, green, blue, alpha.116+ // Colors can be set in a variety of ways:
117- active-color 127 200 255 255117+ // - CSS named colors: "red"
118+ // - RGB hex: "#rgb", "#rgba", "#rrggbb", "#rrggbbaa"
119+ // - CSS-like notation: "rgb(255, 127, 0)", rgba(), hsl() and a few others.
118 120
119- // Color of the ring on inactive monitors: red, green, blue, alpha.121+ // Color of the ring on the active monitor.
120- inactive-color 80 80 80 255122+ active-color "#7fc8ff"
123+
124+ // Color of the ring on inactive monitors.
125+ inactive-color "#505050"
126+
127+ // Additionally, there's a legacy RGBA syntax:
128+ // active-color 127 200 255 255
121 129
122 // You can also use gradients. They take precedence over solid colors.130 // You can also use gradients. They take precedence over solid colors.
123 // Gradients are rendered the same as CSS linear-gradient(angle, from, to).131 // Gradients are rendered the same as CSS linear-gradient(angle, from, to).
124- // Colors can be set in a variety of ways here:
125- // - CSS named colors: from="red"
126- // - RGB hex: from="#rgb", from="#rgba", from="#rrggbb", from="#rrggbbaa"
127- // - CSS-like notation: from="rgb(255, 127, 0)", rgba(), hsl() and a few others.
128 // The angle is the same as in linear-gradient, and is optional,132 // The angle is the same as in linear-gradient, and is optional,
129 // defaulting to 180 (top-to-bottom gradient).133 // defaulting to 180 (top-to-bottom gradient).
130 // You can use any CSS linear-gradient tool on the web to set these up.134 // You can use any CSS linear-gradient tool on the web to set these up.
@@ -145,8 +149,8 @@ layout {
145 off149 off
146 150
147 width 4151 width 4
148- active-color 255 200 127 255152+ active-color "#ffc87f"
149- inactive-color 80 80 80 255153+ inactive-color "#505050"
150 154
151 // active-gradient from="#ffbb66" to="#ffc880" angle=45 relative-to="workspace-view"155 // active-gradient from="#ffbb66" to="#ffc880" angle=45 relative-to="workspace-view"
152 // inactive-gradient from="#505050" to="#808080" angle=45 relative-to="workspace-view"156 // inactive-gradient from="#505050" to="#808080" angle=45 relative-to="workspace-view"