Skip to content

Commit

Permalink
fix: label parsing error with window titles (glzr-io#403)
Browse files Browse the repository at this point in the history
  • Loading branch information
lars-berger authored Sep 18, 2023
1 parent 6bac18b commit 4fcf279
Showing 1 changed file with 61 additions and 35 deletions.
96 changes: 61 additions & 35 deletions GlazeWM.Bar/XamlHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,47 +73,73 @@ public static LabelViewModel ParseLabel(
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;
}
}
}
}

0 comments on commit 4fcf279

Please sign in to comment.