Skip to content

GlazeWM is a tiling window manager for macOS and Windows inspired by i3wm.

fix: label parsing error with window titles (#403)

lars-berger4fcf2794306c
diff --git a/GlazeWM.Bar/XamlHelper.cs b/GlazeWM.Bar/XamlHelper.cs
index dced5174..1b664f5f 100644
--- a/GlazeWM.Bar/XamlHelper.cs
+++ b/GlazeWM.Bar/XamlHelper.cs
@@ -73,47 +73,73 @@ namespace GlazeWM.Bar
labelWithVariables = labelWithVariables.Replace($"{{{key}}}", value());
}
- // Wrap `labelString` in arbitrary tag to make it valid XML.
- var wrappedLabel = $"<Label>{labelWithVariables}</Label>";
- var labelXml = XElement.Parse(wrappedLabel);
+ var labelSpans = ToLabelSpans(labelWithVariables, viewModel);
+ return new LabelViewModel(labelSpans);
+ }
- var labelSpans = labelXml.Nodes().Select(node =>
+ private static List<LabelSpan> ToLabelSpans(
+ string labelWithVariables,
+ ComponentViewModel viewModel)
+ {
+ var unparsedLabelSpans = new List<LabelSpan>()
{
- var value = node switch
- {
- XText text => text.Value,
- XElement element => element.Value,
- _ => throw new ArgumentException("Invalid XML.", nameof(labelString))
- };
+ new(
+ labelWithVariables,
+ viewModel.Foreground,
+ viewModel.FontFamily,
+ viewModel.FontWeight,
+ viewModel.FontSize)
+ };
- string foreground = null;
- string fontFamily = null;
- string fontWeight = null;
- string fontSize = null;
+ if (!labelWithVariables.Contains("<attr"))
+ return unparsedLabelSpans;
- var ancestor = node;
+ try
+ {
+ // Wrap `labelString` in arbitrary tag to make it valid XML.
+ var wrappedLabel = $"<Label>{labelWithVariables}</Label>";
+ var labelXml = XElement.Parse(wrappedLabel);
- while (ancestor is not null)
+ return labelXml.Nodes().Select(node =>
{
- foreground ??= (ancestor as XElement)?.Attribute("fg")?.Value;
- fontFamily ??= (ancestor as XElement)?.Attribute("ff")?.Value;
- fontWeight ??= (ancestor as XElement)?.Attribute("fw")?.Value;
- fontSize ??= (ancestor as XElement)?.Attribute("fs")?.Value;
-
- // Traverse upwards to get attributes to apply.
- ancestor = ancestor.Parent;
- }
-
- return new LabelSpan(
- value,
- foreground ?? viewModel.Foreground,
- fontFamily ?? viewModel.FontFamily,
- fontWeight ?? viewModel.FontWeight,
- fontSize ?? viewModel.FontSize
- );
- }).ToList();
-
- return new LabelViewModel(labelSpans);
+ var value = node switch
+ {
+ XText text => text.Value,
+ XElement element => element.Value,
+ _ => throw new ArgumentException("Invalid XML.", nameof(labelWithVariables))
+ };
+
+ string foreground = null;
+ string fontFamily = null;
+ string fontWeight = null;
+ string fontSize = null;
+
+ var ancestor = node;
+
+ while (ancestor is not null)
+ {
+ foreground ??= (ancestor as XElement)?.Attribute("fg")?.Value;
+ fontFamily ??= (ancestor as XElement)?.Attribute("ff")?.Value;
+ fontWeight ??= (ancestor as XElement)?.Attribute("fw")?.Value;
+ fontSize ??= (ancestor as XElement)?.Attribute("fs")?.Value;
+
+ // Traverse upwards to get attributes to apply.
+ ancestor = ancestor.Parent;
+ }
+
+ return new LabelSpan(
+ value,
+ foreground ?? viewModel.Foreground,
+ fontFamily ?? viewModel.FontFamily,
+ fontWeight ?? viewModel.FontWeight,
+ fontSize ?? viewModel.FontSize
+ );
+ }).ToList();
+ }
+ catch (Exception)
+ {
+ return unparsedLabelSpans;
+ }
}
}
}