Skip to content

Commit 062c8f1

Browse files
committed
Stop the ref labels reading low in their capsules (issue #679)
A ref label's capsule is the text's line box, which reserves room for an ascender and a descender that most branch names never both use, so the glyphs sat low in it and a name ending in a descender came to rest on the capsule's border with nothing underneath it. * Lift the text a point inside the capsule rather than drawing it into the capsule itself. That balances the space above the glyphs against the space below and gives a descender its clearance, without the capsule changing size at all. * Leave the capsule alone. Padding it was the first attempt and it is the wrong remedy: it preserves the ratio of air above the glyphs to air below and merely adds more of both, which reads worse. * Cover the lift with tests, one of which pins the capsule's height so that the padding does not get tried a second time. Leave the baseline where it is. The labels sit on cap-height centring, which is what keeps master and MASTER sharing a baseline rather than drifting apart from one another.
1 parent 11ca47b commit 062c8f1

2 files changed

Lines changed: 53 additions & 1 deletion

File tree

‎Classes/Views/PBGitRevisionCell.m‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,19 @@ - (NSColor *)colorForRef:(PBGitRef *)ref
256256
return array;
257257
}
258258

259+
- (NSRect)rectForRefLabelText:(NSString *)name inRect:(NSRect)rect
260+
{
261+
static const CGFloat ref_label_lift = 1;
262+
263+
NSSize textSize = [name sizeWithAttributes:[self attributesForRefLabel]];
264+
NSRect textRect = rect;
265+
266+
textRect.origin.y += (rect.size.height - textSize.height) / 2 - ref_label_lift;
267+
textRect.size.height = textSize.height;
268+
269+
return textRect;
270+
}
271+
259272
- (void)drawLabelAtIndex:(int)index inRect:(NSRect)rect
260273
{
261274
NSArray *refs = self.objectValue.refs;
@@ -269,7 +282,8 @@ - (void)drawLabelAtIndex:(int)index inRect:(NSRect)rect
269282

270283
// [[NSColor blackColor] set];
271284
// [border stroke];
272-
[[ref shortName] drawInRect:rect withAttributes:attributes];
285+
[[ref shortName] drawInRect:[self rectForRefLabelText:[ref shortName] inRect:rect]
286+
withAttributes:attributes];
273287
}
274288

275289
- (void)drawRefsInRect:(NSRect)refRect

‎GitXTests/PBGitRevisionCellLayoutTests.m‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
// about where a label sits in a row.
1818
@interface PBGitRevisionCell (LayoutTests)
1919
- (NSArray<NSValue *> *)rectsForRefsinRect:(NSRect)rect;
20+
- (NSMutableDictionary *)attributesForRefLabel;
21+
- (NSRect)rectForRefLabelText:(NSString *)name inRect:(NSRect)rect;
2022
@end
2123

2224
// -[PBGitCommit refs] reads through to the repository's table, so a commit
@@ -113,4 +115,40 @@ - (void)testTheRefLabelsSitOnTheRowCentre
113115
}
114116
}
115117

118+
- (void)testARefLabelCapsuleStaysTheSizeOfItsText
119+
{
120+
PBGitRevisionCell *cell = [self cellWithRefs:@[ @"refs/heads/master" ] graphLine:YES];
121+
122+
NSRect capsule = [cell rectsForRefsinRect:cell.bounds].firstObject.rectValue;
123+
NSSize text = [@"master" sizeWithAttributes:[cell attributesForRefLabel]];
124+
125+
XCTAssertEqualWithAccuracy(capsule.size.height, text.height, 0.01,
126+
@"growing the capsule keeps the air above the glyphs and only adds more");
127+
}
128+
129+
- (void)testARefLabelsTextIsLiftedInsideItsCapsule
130+
{
131+
PBGitRevisionCell *cell = [self cellWithRefs:@[ @"refs/heads/master" ] graphLine:YES];
132+
133+
NSRect capsule = [cell rectsForRefsinRect:cell.bounds].firstObject.rectValue;
134+
NSRect text = [cell rectForRefLabelText:@"master" inRect:capsule];
135+
136+
XCTAssertEqualWithAccuracy(text.size.height,
137+
[@"master" sizeWithAttributes:[cell attributesForRefLabel]].height, 0.01,
138+
@"the text is not drawn into its own line box");
139+
XCTAssertLessThan(NSMinY(text), NSMinY(capsule),
140+
@"the text was not lifted, so a name in lowercase still reads low in its capsule");
141+
}
142+
143+
- (void)testARefLabelLeavesRoomBelowADescender
144+
{
145+
PBGitRevisionCell *cell = [self cellWithRefs:@[ @"refs/remotes/origin/master" ] graphLine:YES];
146+
147+
NSRect capsule = [cell rectsForRefsinRect:cell.bounds].firstObject.rectValue;
148+
NSRect text = [cell rectForRefLabelText:@"origin/master" inRect:capsule];
149+
150+
XCTAssertGreaterThan(NSMaxY(capsule), NSMaxY(text),
151+
@"a descender still reaches the capsule's border with nothing under it");
152+
}
153+
116154
@end

0 commit comments

Comments
 (0)