Skip to content

Commit c3ac80e

Browse files
committed
Fix the commit subject sitting above the history row (issue #679)
The subject was nudged a point above centre by hand, and only on rows that had a graph line, so it lined up neither with the columns beside it nor with the ref labels next to it. * Centre the text field on the row instead of offsetting it, and lay every row out rather than only those with a graph line, which left the rest wherever the nib happened to put them. * Snap the result to the backing grid. Centring lands on a half point whenever the row and the text differ by an odd amount, and text off the pixel grid renders blurred on a 1x display. * Cover both with tests over the rectangles the cell lays out, since none of this drawing is checkable any other way. The ref labels are deliberately untouched: their capsule is the measured line box, so it is the same height in every script, and where the glyphs sit inside it depends on the branch name rather than on any offset the cell could apply.
1 parent 13dd3f7 commit c3ac80e

3 files changed

Lines changed: 142 additions & 20 deletions

File tree

‎Classes/Views/PBGitRevisionCell.m‎

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -329,34 +329,36 @@ - (void)layout
329329
NSRect rect = self.bounds;
330330
cellInfo = [self.objectValue lineInfo];
331331

332-
if (cellInfo) {
333-
float pathWidth = 0;
332+
float pathWidth = 0;
334333

335-
if (!controller.hasNonlinearPath) {
336-
pathWidth = 10 + COLUMN_WIDTH * cellInfo.numColumns;
337-
}
334+
if (cellInfo && !controller.hasNonlinearPath) {
335+
pathWidth = 10 + COLUMN_WIDTH * cellInfo.numColumns;
336+
}
338337

339-
NSRect ownRect;
340-
NSDivideRect(rect, &ownRect, &rect, pathWidth, NSMinXEdge);
338+
NSRect ownRect;
339+
NSDivideRect(rect, &ownRect, &rect, pathWidth, NSMinXEdge);
341340

342-
NSArray<NSValue *> *rectValues = [self rectsForRefsinRect:rect];
341+
NSArray<NSValue *> *rectValues = [self rectsForRefsinRect:rect];
343342

344-
if (rectValues.count > 0) {
345-
const CGFloat PADDING = 4;
346-
NSRect lastRect = rectValues.lastObject.rectValue;
343+
if (rectValues.count > 0) {
344+
const CGFloat PADDING = 4;
345+
NSRect lastRect = rectValues.lastObject.rectValue;
347346

348-
rect.size.width -= lastRect.origin.x - rect.origin.x + lastRect.size.width - PADDING;
349-
rect.origin.x = lastRect.origin.x + lastRect.size.width + PADDING;
350-
}
347+
rect.size.width -= lastRect.origin.x - rect.origin.x + lastRect.size.width - PADDING;
348+
rect.origin.x = lastRect.origin.x + lastRect.size.width + PADDING;
349+
}
351350

352-
NSRect frame = self.textField.frame;
351+
NSRect frame = self.textField.frame;
353352

354-
frame.origin.x = floor(rect.origin.x);
355-
frame.origin.y = floor((self.bounds.size.height - frame.size.height) / 2) - 1;
356-
frame.size.width = floor(self.bounds.size.width - frame.origin.x);
353+
frame.origin.x = rect.origin.x;
354+
frame.origin.y = NSMidY(self.bounds) - frame.size.height / 2;
355+
frame.size.width = self.bounds.size.width - frame.origin.x;
357356

358-
self.textField.frame = frame;
359-
}
357+
// Centring lands on a half point whenever the row and the text differ by an
358+
// odd amount, and text drawn off the pixel grid renders blurred on a 1x
359+
// display. Snap to whatever the grid is rather than rounding by hand, so
360+
// this holds on Retina too.
361+
self.textField.frame = [self backingAlignedRect:frame options:NSAlignAllEdgesNearest];
360362
}
361363

362364
- (PBGitCommit *)objectValue

‎GitX.xcodeproj/project.pbxproj‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@
165165
63E155858B2CC783EE10F1D9 /* PBGitIndexRefreshTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 689D57B4A78CEF3B32EB0046 /* PBGitIndexRefreshTests.m */; };
166166
9A3F7C21E48B450D9C2A1F60 /* PBGitIndexDiffCacheTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 9A3F7C21E48B450D9C2A1F61 /* PBGitIndexDiffCacheTests.m */; };
167167
6C2A9E14B7D3402F81A5C931 /* PBWebHistoryControllerCurrentRefTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 6C2A9E14B7D3402F81A5C930 /* PBWebHistoryControllerCurrentRefTests.m */; };
168+
6C2A9E14B7D3402F81A5D931 /* PBGitRevisionCellLayoutTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 6C2A9E14B7D3402F81A5D930 /* PBGitRevisionCellLayoutTests.m */; };
168169
764739E2E065426C828C6A8D /* NSStringTruncateTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 69760FA8A15F4EA2AD3D034A /* NSStringTruncateTests.m */; };
169170
7C837A4B980A48D1B91EDF98 /* PBGitHistoryCommitMenuTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 661EC20D32B74C04B2DA75E4 /* PBGitHistoryCommitMenuTests.m */; };
170171
7C4D19A8E52B461AB3D80E70 /* GLFileViewContentCacheTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 7C4D19A8E52B461AB3D80E71 /* GLFileViewContentCacheTests.m */; };
@@ -728,6 +729,7 @@
728729
689D57B4A78CEF3B32EB0046 /* PBGitIndexRefreshTests.m */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.c.objc; name = PBGitIndexRefreshTests.m; path = GitXTests/PBGitIndexRefreshTests.m; sourceTree = "<group>"; };
729730
9A3F7C21E48B450D9C2A1F61 /* PBGitIndexDiffCacheTests.m */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.c.objc; name = PBGitIndexDiffCacheTests.m; path = GitXTests/PBGitIndexDiffCacheTests.m; sourceTree = "<group>"; };
730731
6C2A9E14B7D3402F81A5C930 /* PBWebHistoryControllerCurrentRefTests.m */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.c.objc; name = PBWebHistoryControllerCurrentRefTests.m; path = GitXTests/PBWebHistoryControllerCurrentRefTests.m; sourceTree = "<group>"; };
732+
6C2A9E14B7D3402F81A5D930 /* PBGitRevisionCellLayoutTests.m */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.c.objc; name = PBGitRevisionCellLayoutTests.m; path = GitXTests/PBGitRevisionCellLayoutTests.m; sourceTree = "<group>"; };
731733
69760FA8A15F4EA2AD3D034A /* NSStringTruncateTests.m */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.c.objc; name = NSStringTruncateTests.m; path = GitXTests/NSStringTruncateTests.m; sourceTree = "<group>"; };
732734
661EC20D32B74C04B2DA75E4 /* PBGitHistoryCommitMenuTests.m */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.c.objc; name = PBGitHistoryCommitMenuTests.m; path = GitXTests/PBGitHistoryCommitMenuTests.m; sourceTree = "<group>"; };
733735
7C4D19A8E52B461AB3D80E71 /* GLFileViewContentCacheTests.m */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.c.objc; name = GLFileViewContentCacheTests.m; path = GitXTests/GLFileViewContentCacheTests.m; sourceTree = "<group>"; };
@@ -875,6 +877,7 @@
875877
689D57B4A78CEF3B32EB0046 /* PBGitIndexRefreshTests.m */,
876878
9A3F7C21E48B450D9C2A1F61 /* PBGitIndexDiffCacheTests.m */,
877879
6C2A9E14B7D3402F81A5C930 /* PBWebHistoryControllerCurrentRefTests.m */,
880+
6C2A9E14B7D3402F81A5D930 /* PBGitRevisionCellLayoutTests.m */,
878881
69760FA8A15F4EA2AD3D034A /* NSStringTruncateTests.m */,
879882
661EC20D32B74C04B2DA75E4 /* PBGitHistoryCommitMenuTests.m */,
880883
7C4D19A8E52B461AB3D80E71 /* GLFileViewContentCacheTests.m */,
@@ -1839,6 +1842,7 @@
18391842
63E155858B2CC783EE10F1D9 /* PBGitIndexRefreshTests.m in Sources */,
18401843
9A3F7C21E48B450D9C2A1F60 /* PBGitIndexDiffCacheTests.m in Sources */,
18411844
6C2A9E14B7D3402F81A5C931 /* PBWebHistoryControllerCurrentRefTests.m in Sources */,
1845+
6C2A9E14B7D3402F81A5D931 /* PBGitRevisionCellLayoutTests.m in Sources */,
18421846
764739E2E065426C828C6A8D /* NSStringTruncateTests.m in Sources */,
18431847
7C837A4B980A48D1B91EDF98 /* PBGitHistoryCommitMenuTests.m in Sources */,
18441848
7C4D19A8E52B461AB3D80E70 /* GLFileViewContentCacheTests.m in Sources */,
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
//
2+
// PBGitRevisionCellLayoutTests.m
3+
// GitXTests
4+
//
5+
6+
#import <XCTest/XCTest.h>
7+
8+
#import "PBGitRevisionCell.h"
9+
#import "PBGitCommit.h"
10+
#import "PBGitRef.h"
11+
#import "PBGraphCellInfo.h"
12+
13+
// The cell draws the subject itself and lays its own text field out, so the
14+
// only way to tell whether a row lines up is to ask it for the rectangles.
15+
// -rectsForRefsinRect: is the one the drawing and layout paths both use;
16+
// -rectAtIndex: probes a 10000pt tall rect for hit testing and says nothing
17+
// about where a label sits in a row.
18+
@interface PBGitRevisionCell (LayoutTests)
19+
- (NSArray<NSValue *> *)rectsForRefsinRect:(NSRect)rect;
20+
@end
21+
22+
// -[PBGitCommit refs] reads through to the repository's table, so a commit
23+
// with no repository has none. This stub carries its own.
24+
@interface PBLayoutTestCommit : PBGitCommit
25+
@property (nonatomic, strong) NSMutableArray *stubRefs;
26+
@end
27+
28+
@implementation PBLayoutTestCommit
29+
- (NSMutableArray *)refs
30+
{
31+
return self.stubRefs;
32+
}
33+
@end
34+
35+
@interface PBGitRevisionCellLayoutTests : XCTestCase
36+
@end
37+
38+
@implementation PBGitRevisionCellLayoutTests
39+
40+
static const CGFloat kRowHeight = 20;
41+
static const CGFloat kTextHeight = 14;
42+
43+
- (PBGitRevisionCell *)cellWithRefs:(NSArray<NSString *> *)refNames graphLine:(BOOL)hasGraphLine
44+
{
45+
PBGitRevisionCell *cell = [[PBGitRevisionCell alloc] initWithFrame:NSMakeRect(0, 0, 400, kRowHeight)];
46+
47+
NSTextField *field = [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 400, kTextHeight)];
48+
[cell addSubview:field];
49+
cell.textField = field;
50+
51+
PBLayoutTestCommit *commit = [PBLayoutTestCommit new];
52+
NSMutableArray *refs = [NSMutableArray array];
53+
for (NSString *name in refNames)
54+
[refs addObject:[PBGitRef refFromString:name]];
55+
commit.stubRefs = refs;
56+
57+
if (hasGraphLine) {
58+
PBGraphCellInfo *info = [[PBGraphCellInfo alloc] initWithPosition:0 andLines:NULL];
59+
info.nLines = 0;
60+
info.numColumns = 1;
61+
commit.lineInfo = info;
62+
}
63+
64+
cell.objectValue = commit;
65+
return cell;
66+
}
67+
68+
- (void)testTheSubjectSitsOnTheRowCentre
69+
{
70+
PBGitRevisionCell *cell = [self cellWithRefs:@[ @"refs/heads/master" ] graphLine:YES];
71+
72+
[cell layout];
73+
74+
XCTAssertEqualWithAccuracy(NSMidY(cell.textField.frame), NSMidY(cell.bounds), 0.5,
75+
@"the subject is not on the row's centre line");
76+
}
77+
78+
- (void)testTheSubjectSitsOnTheRowCentreWithoutAGraphLine
79+
{
80+
PBGitRevisionCell *cell = [self cellWithRefs:@[] graphLine:NO];
81+
82+
[cell layout];
83+
84+
XCTAssertEqualWithAccuracy(NSMidY(cell.textField.frame), NSMidY(cell.bounds), 0.5,
85+
@"a commit with no graph line leaves the subject where the nib put it");
86+
}
87+
88+
- (void)testTheSubjectStaysCentredWhenTheHeightsDifferByAnOddAmount
89+
{
90+
PBGitRevisionCell *cell = [self cellWithRefs:@[] graphLine:NO];
91+
cell.frame = NSMakeRect(0, 0, 400, kRowHeight + 1);
92+
93+
[cell layout];
94+
95+
XCTAssertEqualWithAccuracy(NSMidY(cell.textField.frame), NSMidY(cell.bounds), 0.5,
96+
@"an odd difference between the row and the text is not centred");
97+
XCTAssertTrue(NSEqualRects(cell.textField.frame,
98+
[cell backingAlignedRect:cell.textField.frame options:NSAlignAllEdgesNearest]),
99+
@"the subject is off the pixel grid, which renders blurred at 1x");
100+
}
101+
102+
- (void)testTheRefLabelsSitOnTheRowCentre
103+
{
104+
NSArray *names = @[ @"refs/heads/master", @"refs/remotes/origin/master" ];
105+
PBGitRevisionCell *cell = [self cellWithRefs:names graphLine:YES];
106+
107+
NSArray<NSValue *> *rects = [cell rectsForRefsinRect:cell.bounds];
108+
XCTAssertEqual(rects.count, names.count, @"the cell did not lay every ref out");
109+
110+
for (NSValue *value in rects) {
111+
XCTAssertEqualWithAccuracy(NSMidY(value.rectValue), NSMidY(cell.bounds), 0.5,
112+
@"a ref label is not on the row's centre line");
113+
}
114+
}
115+
116+
@end

0 commit comments

Comments
 (0)