·6 min read
TaskPad icons would not line up: one shared frame, then the checkbox column
In TaskPad a line that starts with !, ?, TODO, -, * or an arrow is drawn as an icon, not as the character you typed. A checkbox line is drawn as a checkbox. Put them one under the other and the text after the marker started at a different x position on every line, and the icons themselves sat a few pixels above or below the baseline. It took two commits on the same day to fix, and the second was about a column, not a pixel.
- Project
- TaskPad
- Date
- Oct 2026
- Stack
- C#, WPF, AvalonEdit
- Commit
- 270faef
Icons that replace characters
TaskPad is a WPF text editor built on AvalonEdit. Markers in the text, such as [ ] for a checkbox, ! for a badge or TODO for a pill, stay in the document as plain characters. A MarkerGenerator swaps them at render time for an InlineObjectElement. That keeps the file plain text and lets the editor show real controls.
The cost is that the editor, not me, decides where an inline element goes vertically. A comment I left at the top of Align.cs says it directly: AvalonEdit places inline elements at the top of the text line, and a line's top depends on its tallest element. A small badge and a taller comment bubble on the same line, or on neighbouring lines, end up at different heights relative to the text.
One frame for everything
Commit 525a0a8 ("fix: align inline icons with the text") fixed the vertical part by adding Align.cs. Every inline element, whether checkbox, badge, pill, comment bubble, due-date pill, hex swatch or link chip, is wrapped by Align.Host in a canvas with the same height and the same baseline anchor. The content is centred on the middle of the capital letters:
public static double CapMiddle(ITextRunConstructionContext ctx)
{
var props = ctx.GlobalTextRunProperties;
double em = props.FontRenderingEmSize;
double caps = 0.7;
foreach (var family in props.Typeface.FontFamily.Source.Split(','))
{
var ff = new FontFamily(family.Trim());
// skip families that aren't installed (WPF would silently hand back a fallback font)
if (!ff.FamilyNames.Values.Any()) continue;
var tf = new Typeface(ff, props.Typeface.Style, props.Typeface.Weight, props.Typeface.Stretch);
if (tf.TryGetGlyphTypeface(out var gt)) { caps = gt.CapsHeight; break; }
}
return em * caps / 2;
}Two details are worth keeping. The cap height comes from the actual glyph typeface, with 0.7 as a fallback, instead of a constant tuned for one font. And the loop skips font families that are not installed, because WPF does not fail on a missing family in a font stack; it silently gives you a fallback, and you would measure the wrong font.
The frame itself is computed once per line context from the text view's default line height and baseline, plus the user's MarkerSpacing setting:
double box = lineH + gap;
a = Math.Max(b, box / 2 + cap);
h = a + Math.Max(lineH - b, box / 2 - cap);a is the baseline offset, h the frame height. Because all elements share them, mixing a checkbox and a bubble on one line no longer changes where the line's top is. The same commit deleted the old per-element hacks, such as TextBlock.SetBaselineOffset(pill, tv.DefaultBaseline - 1) in Due.cs and SetBaselineOffset(icon, size - 1) in Comments.cs: those - 1 values were each tuned against one element and one font.
The second problem was horizontal
With the vertical frame fixed, the lines still did not line up. A checkbox line reads [ ] text: three cells for the marker, then the text in the fifth cell. A tag line like ! text had its icon sized to the characters of the marker, so the text began wherever the marker's width happened to end. Commit 270faef ("fix: put icons in the checkbox column") makes tag icons use the checkbox's column.
Two edits make that work. The parser now includes one following space in the tag token:
// the token includes one following space so the icon can take the same column as a checkbox
info.Tokens.Add(new Tok(TokKind.Tag, i, len + (i + len < n && t[i + len] == ' ' ? 1 : 0)));and TagGlyph receives both the token length and the marker length, so it knows whether that space is there:
/// Icons use the same column as a checkbox: centred in the first 3 character cells, text starting at the 5th
/// (like "[ ] text"), so checkboxes, badges and bullets line up down the page.
FrameworkElement TagGlyph(Tag tag, int tokenLen, int markerLen)
{
bool space = tokenLen > markerLen;
double col = _charW * 3, w = col + (space ? _charW : 0);
...
}The glyph is now centred inside the first col pixels, not inside its whole width. That needed a new parameter on Align.Host, centreSpan, so left is computed against the column while the frame is still w wide:
double left = centreX ? Math.Max(1, Math.Round(((centreSpan ?? w) - content.DesiredSize.Width) / 2)) : marginLeft;The one exception
TODO is wider than three cells. A pill squeezed into a column would be unreadable, so the Todo case keeps its own width and centres within that: tw = _charW * tokenLen, minus one cell if a space was swallowed, and the pill is at least _em * 2.2 wide. Text after TODO therefore starts later than after a bullet, which is the right trade: a badge that fits, over a perfect column.
Why swallowing a space is safe
Folding the space into the token only changes how the line is drawn. The document still contains the same characters, and caret movement, selection and saving are untouched, because the marker is replaced by an element covering exactly the replaced range. The part that needed care was the guard i + len < n, so a marker at the very end of a line does not read past it.
What I would take from this
Two things. If a UI toolkit lets a child decide its own vertical position, the fix is not a per-element offset but one shared frame that every element goes through; the - 1 hacks only ever fixed the case I was looking at. And alignment has two axes. The first commit fixed the vertical axis; the horizontal one needed its own commit, once the icons stopped moving up and down and the text start was the remaining difference.
#wpf#avalonedit#csharp#ui-alignment
Keep reading
Have a project in mind?