Repository navigation
Expand file tree
/
Copy pathSourcesTableController.m
More file actions
638 lines (567 loc) · 27.2 KB
/
Copy pathSourcesTableController.m
File metadata and controls
638 lines (567 loc) · 27.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
// This file is part of Scroll Reverser <https://pilotmoon.com/scrollreverser/>
// Licensed under Apache License v2.0 <http://www.apache.org/licenses/LICENSE-2.0>
#import "SourcesTableController.h"
#import "SourceInfoViewController.h"
#import "SourceRow+Display.h"
static NSString *const kColumnIcon=@"icon";
static NSString *const kColumnName=@"name";
static NSString *const kColumnDirection=@"direction";
static NSString *const kColumnStepSize=@"stepSize";
static NSString *const kColumnLastUsed=@"lastUsed";
static NSString *const kColumnInfo=@"info";
// the direction column's cells differ by kind of source
static NSString *const kCellDirectionDevice=@"directionDevice";
static NSString *const kCellDirectionApp=@"directionApp";
// "Just now" for this long after a source last scrolled, then a relative time
static const CFTimeInterval kJustNowInterval=60;
static const NSTimeInterval kLastUsedRefreshInterval=10;
static void *_contextRows=&_contextRows;
// whether the hint about horizontal scrolling has been shown, which happens only once
static NSString *const PrefsHorizontalHintShown=@"HorizontalHintShown";
static NSFont *_smallFont(void)
{
return [NSFont systemFontOfSize:[NSFont smallSystemFontSize]];
}
static NSDictionary *_negated(void)
{
return @{NSValueTransformerNameBindingOption: NSNegateBooleanTransformerName};
}
// Labels and images don't dim while disabled, as the controls beside them do, so fade them.
@interface DimmingLabel : NSTextField
@end
@implementation DimmingLabel
- (void)setEnabled:(BOOL)enabled
{
[super setEnabled:enabled];
self.alphaValue=enabled?1:0.5;
}
@end
@interface DimmingImageView : NSImageView
@end
@implementation DimmingImageView
- (void)setEnabled:(BOOL)enabled
{
[super setEnabled:enabled];
self.alphaValue=enabled?1:0.5;
}
@end
/* A row view that is tinted while its source is scrolling. It draws the tint itself rather than
setting backgroundColor, which takes the selected style away from a selected row's pop-up menu.
On a selected row, the selection is lightened (darkened in dark mode) instead, since it covers the
background. */
@interface SourceRowView : NSTableRowView
@property (nonatomic) BOOL scrolling;
@end
@implementation SourceRowView
- (void)setScrolling:(BOOL)scrolling
{
if (scrolling!=_scrolling) {
_scrolling=scrolling;
self.needsDisplay=YES;
}
}
- (void)drawBackgroundInRect:(NSRect)dirtyRect
{
[super drawBackgroundInRect:dirtyRect];
if (self.scrolling) {
[[[NSColor controlAccentColor] colorWithAlphaComponent:0.25] setFill];
NSRectFillUsingOperation(self.bounds, NSCompositingOperationSourceOver);
}
}
- (void)drawSelectionInRect:(NSRect)dirtyRect
{
[super drawSelectionInRect:dirtyRect];
if (self.scrolling) {
const BOOL dark=[[self.effectiveAppearance bestMatchFromAppearancesWithNames:@[NSAppearanceNameAqua, NSAppearanceNameDarkAqua]] isEqualToString:NSAppearanceNameDarkAqua];
[[NSColor colorWithWhite:dark?0:1 alpha:0.25] setFill];
NSRectFillUsingOperation(self.bounds, NSCompositingOperationSourceOver);
}
}
@end
static NSArray<SourceRow *> *_sortedByRecentUse(NSArray<SourceRow *> *rows)
{
return [rows sortedArrayUsingComparator:^NSComparisonResult(SourceRow *a, SourceRow *b) {
return [@(b.lastSeenTime) compare:@(a.lastSeenTime)];
}];
}
@interface SourcesTableController ()
@property SourceTable *sourceTable;
@property NSMutableArray<SourceRow *> *rows; // in the order shown
@property NSTableView *tableView;
@property NSButton *removeButton;
@property NSTextField *placeholder;
@property NSView *view;
@property NSPopover *infoPopover;
@property NSPopover *hintPopover;
@property NSTimer *lastUsedTimer;
@property NSRelativeDateTimeFormatter *dateFormatter;
@property BOOL visible;
@end
@implementation SourcesTableController
- (instancetype)initWithSourceTable:(SourceTable *)sourceTable
{
self=[super init];
if (self) {
self.sourceTable=sourceTable;
self.rows=[NSMutableArray array];
self.dateFormatter=[[NSRelativeDateTimeFormatter alloc] init];
self.dateFormatter.unitsStyle=NSRelativeDateTimeFormatterUnitsStyleShort;
self.view=[self makeView];
}
return self;
}
- (void)dealloc
{
[self windowWillClose];
}
- (NSView *)makeView
{
NSTableView *const table=[[NSTableView alloc] init];
table.style=NSTableViewStyleFullWidth;
table.rowHeight=24;
table.allowsMultipleSelection=NO;
table.allowsColumnReordering=NO;
// only the name column resizes, so it takes up any spare width
table.columnAutoresizingStyle=NSTableViewUniformColumnAutoresizingStyle;
// wide enough for the longest scrolling mode in the user's language
NSPopUpButton *const widestPopUp=[self directionPopUpForCell:nil modes:@[@(ScrollDirectionModeNatural), @(ScrollDirectionModeClassic), @(ScrollDirectionModeUnchanged), @(ScrollDirectionModeReversed)]];
const CGFloat directionWidth=MAX(104, ceil(widestPopUp.fittingSize.width)+8);
CGFloat tableWidth=0;
for (NSArray *definition in @[@[kColumnIcon, @"", @26],
@[kColumnName, NSLocalizedString(@"Source", @"Sources table column header: the device or app that scrolls"), @240],
@[kColumnDirection, NSLocalizedString(@"Scrolling", @"Preferences pane for `Scrolling` settings; also the sources table column header for natural or classic scrolling"), @(directionWidth)],
@[kColumnStepSize, NSLocalizedString(@"Step size", @"Sources table column header: scroll wheel step size"), @96],
@[kColumnLastUsed, NSLocalizedString(@"Last used", @"Sources table column header: when the source last scrolled"), @100],
@[kColumnInfo, @"", @24]]) {
NSTableColumn *const column=[[NSTableColumn alloc] initWithIdentifier:definition[0]];
column.title=definition[1];
// and wide enough for its header in the user's language
column.width=MAX([definition[2] doubleValue], ceil(column.headerCell.cellSize.width)+12);
const BOOL nameColumn=[column.identifier isEqualToString:kColumnName];
column.resizingMask=nameColumn?NSTableColumnAutoresizingMask:NSTableColumnNoResizing;
if (!nameColumn) {
column.headerCell.alignment=NSTextAlignmentCenter;
}
[table addTableColumn:column];
tableWidth+=column.width+table.intercellSpacing.width;
}
table.dataSource=self;
table.delegate=self;
self.tableView=table;
NSScrollView *const scrollView=[[NSScrollView alloc] init];
scrollView.translatesAutoresizingMaskIntoConstraints=NO;
scrollView.documentView=table;
scrollView.hasVerticalScroller=YES;
scrollView.autohidesScrollers=YES;
scrollView.borderType=NSBezelBorder;
self.removeButton=[NSButton buttonWithImage:[NSImage imageNamed:NSImageNameRemoveTemplate] target:self action:@selector(removeClicked:)];
self.removeButton.translatesAutoresizingMaskIntoConstraints=NO;
self.removeButton.bezelStyle=NSBezelStyleSmallSquare;
self.removeButton.toolTip=NSLocalizedString(@"Remove the selected source. It will be added again if it scrolls.", @"Tooltip for the button below the sources table");
self.removeButton.enabled=NO;
// shown while there are no rows, as on first run
self.placeholder=[DimmingLabel wrappingLabelWithString:NSLocalizedString(@"Start scrolling to see your devices here.", @"Shown in the empty sources table, which fills with devices and apps as they scroll")];
self.placeholder.translatesAutoresizingMaskIntoConstraints=NO;
self.placeholder.alignment=NSTextAlignmentCenter;
self.placeholder.textColor=[NSColor secondaryLabelColor];
self.placeholder.hidden=YES;
[self.placeholder bind:NSEnabledBinding toObject:self withKeyPath:@"enabled" options:nil];
NSView *const view=[[NSView alloc] init];
view.translatesAutoresizingMaskIntoConstraints=NO;
for (NSView *subview in @[scrollView, self.removeButton, self.placeholder]) {
[view addSubview:subview];
}
// the area below the header, where the rows go (the clip view extends under the header)
NSLayoutGuide *const rowsArea=[[NSLayoutGuide alloc] init];
[view addLayoutGuide:rowsArea];
[NSLayoutConstraint activateConstraints:@[
[rowsArea.topAnchor constraintEqualToAnchor:table.headerView.bottomAnchor],
[rowsArea.bottomAnchor constraintEqualToAnchor:scrollView.bottomAnchor],
[scrollView.topAnchor constraintEqualToAnchor:view.topAnchor],
[scrollView.leadingAnchor constraintEqualToAnchor:view.leadingAnchor],
[view.trailingAnchor constraintEqualToAnchor:scrollView.trailingAnchor],
[scrollView.widthAnchor constraintGreaterThanOrEqualToConstant:tableWidth+2],
[scrollView.heightAnchor constraintEqualToConstant:200],
[self.removeButton.topAnchor constraintEqualToAnchor:scrollView.bottomAnchor constant:-1],
[self.removeButton.leadingAnchor constraintEqualToAnchor:view.leadingAnchor],
[self.removeButton.widthAnchor constraintEqualToConstant:24],
[self.removeButton.heightAnchor constraintEqualToConstant:22],
[view.bottomAnchor constraintEqualToAnchor:self.removeButton.bottomAnchor],
[self.placeholder.centerXAnchor constraintEqualToAnchor:scrollView.centerXAnchor],
[self.placeholder.centerYAnchor constraintEqualToAnchor:rowsArea.centerYAnchor],
[self.placeholder.widthAnchor constraintLessThanOrEqualToAnchor:scrollView.widthAnchor constant:-40],
]];
return view;
}
#pragma mark Window
- (void)windowWillShow
{
if (self.visible) {
return;
}
self.visible=YES;
[self.rows setArray:_sortedByRecentUse(self.sourceTable.rows)];
[self.tableView deselectAll:nil];
[self.tableView reloadData];
[self updatePlaceholder];
[self.sourceTable addObserver:self forKeyPath:@"rows" options:0 context:_contextRows];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(rowDidStartScrolling:) name:SourceRowDidStartScrollingNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(rowDidStartScrollingHorizontally:) name:SourceRowDidStartScrollingHorizontallyNotification object:nil];
self.lastUsedTimer=[NSTimer timerWithTimeInterval:kLastUsedRefreshInterval target:self selector:@selector(lastUsedTimerFired:) userInfo:nil repeats:YES];
self.lastUsedTimer.tolerance=1;
[[NSRunLoop mainRunLoop] addTimer:self.lastUsedTimer forMode:NSRunLoopCommonModes];
}
- (void)windowWillClose
{
if (!self.visible) {
return;
}
self.visible=NO;
[self.sourceTable removeObserver:self forKeyPath:@"rows" context:_contextRows];
[[NSNotificationCenter defaultCenter] removeObserver:self name:SourceRowDidStartScrollingNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:SourceRowDidStartScrollingHorizontallyNotification object:nil];
[self.lastUsedTimer invalidate];
self.lastUsedTimer=nil;
[self.infoPopover close];
[self.hintPopover close];
}
#pragma mark Keeping up to date
// New rows are added at the top, and removed rows go.
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context
{
if (context!=_contextRows) {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
return;
}
NSArray<SourceRow *> *const current=self.sourceTable.rows;
NSIndexSet *const removed=[self.rows indexesOfObjectsPassingTest:^BOOL(SourceRow *row, NSUInteger idx, BOOL *stop) {
return [current indexOfObjectIdenticalTo:row]==NSNotFound;
}];
SourceRow *const infoRow=((SourceInfoViewController *)self.infoPopover.contentViewController).row;
if (infoRow&&[current indexOfObjectIdenticalTo:infoRow]==NSNotFound) {
[self.infoPopover close];
}
if ([removed count]) {
[self.rows removeObjectsAtIndexes:removed];
[self.tableView removeRowsAtIndexes:removed withAnimation:NSTableViewAnimationEffectFade];
}
for (SourceRow *row in current) {
if ([self.rows indexOfObjectIdenticalTo:row]==NSNotFound) {
[self.rows insertObject:row atIndex:0];
// Not faded in: in a window that isn't on screen, the fade can stop short and leave the row invisible.
[self.tableView insertRowsAtIndexes:[NSIndexSet indexSetWithIndex:0] withAnimation:NSTableViewAnimationEffectNone];
}
}
[self updatePlaceholder];
}
- (void)updatePlaceholder
{
self.placeholder.hidden=[self.rows count]>0;
}
// A source that starts scrolling moves to the top, by moving its row so that the selection follows it,
// and is highlighted until it stops.
- (void)rowDidStartScrolling:(NSNotification *)notification
{
SourceRow *const row=notification.object;
const NSUInteger index=[self.rows indexOfObjectIdenticalTo:row];
if (index==NSNotFound) {
return;
}
if (index>0) {
[self.rows removeObjectAtIndex:index];
[self.rows insertObject:row atIndex:0];
[self.tableView moveRowAtIndex:index toIndex:0];
}
[self updateRowAtIndex:0];
[self scheduleUpdateForRow:row];
}
// Update the row when it stops scrolling. Events keep arriving meanwhile, so look again then.
- (void)scheduleUpdateForRow:(SourceRow *)row
{
const CFTimeInterval delay=MAX(row.lastSeenTime+SourceRowScrollingInterval-CFAbsoluteTimeGetCurrent(), 0.01);
__weak SourcesTableController *const weakSelf=self;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delay*NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
SourcesTableController *const strongSelf=weakSelf;
const NSUInteger index=[strongSelf.rows indexOfObjectIdenticalTo:row];
if (!strongSelf.visible||index==NSNotFound) {
return;
}
[strongSelf updateRowAtIndex:index];
if (row.scrolling) {
[strongSelf scheduleUpdateForRow:row];
}
});
}
/* The first time a source scrolls horizontally while the window is open, point to where its horizontal
scrolling is set, since people don't find it in the info popover. Shown once, ever. */
- (void)rowDidStartScrollingHorizontally:(NSNotification *)notification
{
if ([[NSUserDefaults standardUserDefaults] boolForKey:PrefsHorizontalHintShown]||!self.enabled||self.infoPopover.shown||self.hintPopover.shown) {
return;
}
SourceRow *const row=notification.object;
// once the row has moved to the top, as it does when it starts scrolling
__weak SourcesTableController *const weakSelf=self;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3*NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
SourcesTableController *const strongSelf=weakSelf;
const NSUInteger index=[strongSelf.rows indexOfObjectIdenticalTo:row];
if (!strongSelf.visible||index==NSNotFound||strongSelf.infoPopover.shown) {
return;
}
NSTableCellView *const cell=[strongSelf.tableView viewAtColumn:[strongSelf.tableView columnWithIdentifier:kColumnInfo] row:index makeIfNecessary:NO];
NSButton *const button=cell.subviews.firstObject;
if (![button isKindOfClass:[NSButton class]]||!button.window.visible) {
return;
}
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:PrefsHorizontalHintShown];
[strongSelf showHintAtInfoButton:button];
});
}
// Clicking the hint opens the info popover it points to.
- (void)showHintAtInfoButton:(NSButton *)button
{
NSTextField *const label=[NSTextField wrappingLabelWithString:NSLocalizedString(@"Set horizontal scrolling for this source here.", @"Hint shown once, pointing to a source's details button, the first time a source scrolls horizontally")];
label.translatesAutoresizingMaskIntoConstraints=NO;
// otherwise it wraps as narrow as it can
label.preferredMaxLayoutWidth=220;
NSView *const view=[[NSView alloc] init];
[view addSubview:label];
[NSLayoutConstraint activateConstraints:@[
[label.topAnchor constraintEqualToAnchor:view.topAnchor constant:10],
[label.leadingAnchor constraintEqualToAnchor:view.leadingAnchor constant:12],
[view.trailingAnchor constraintEqualToAnchor:label.trailingAnchor constant:12],
[view.bottomAnchor constraintEqualToAnchor:label.bottomAnchor constant:10],
]];
NSClickGestureRecognizer *const click=[[NSClickGestureRecognizer alloc] initWithTarget:self action:@selector(hintClicked:)];
[view addGestureRecognizer:click];
NSViewController *const controller=[[NSViewController alloc] init];
controller.view=view;
controller.representedObject=button;
// the popover takes its size from this, not from the constraints
controller.preferredContentSize=view.fittingSize;
self.hintPopover=[[NSPopover alloc] init];
self.hintPopover.behavior=NSPopoverBehaviorTransient;
self.hintPopover.contentViewController=controller;
[self.hintPopover showRelativeToRect:button.bounds ofView:button preferredEdge:NSRectEdgeMaxX];
}
- (void)hintClicked:(NSClickGestureRecognizer *)recognizer
{
NSButton *const button=self.hintPopover.contentViewController.representedObject;
[self.hintPopover close];
if (button.window) {
[self infoClicked:button];
}
}
- (void)lastUsedTimerFired:(NSTimer *)timer
{
[self.tableView enumerateAvailableRowViewsUsingBlock:^(NSTableRowView *rowView, NSInteger index) {
[self updateRowAtIndex:index];
}];
}
// The highlight and Last used time, which change with time rather than by observation.
- (void)updateRowAtIndex:(NSInteger)index
{
SourceRowView *const rowView=[self.tableView rowViewAtRow:index makeIfNecessary:NO];
[self updateRowView:rowView forRow:self.rows[index]];
}
- (void)updateRowView:(SourceRowView *)rowView forRow:(SourceRow *)row
{
rowView.scrolling=row.scrolling;
NSTableCellView *const lastUsedCell=[rowView viewAtColumn:[self.tableView columnWithIdentifier:kColumnLastUsed]];
lastUsedCell.textField.stringValue=[self lastUsedStringForRow:row];
}
- (NSString *)lastUsedStringForRow:(SourceRow *)row
{
if (CFAbsoluteTimeGetCurrent()-row.lastSeenTime<kJustNowInterval) {
return NSLocalizedString(@"Just now", @"Sources table: the source scrolled within the last minute");
}
return [self.dateFormatter localizedStringForDate:row.lastSeen relativeToDate:[NSDate date]];
}
#pragma mark Actions
- (void)setEnabled:(BOOL)enabled
{
_enabled=enabled;
// its settings were enabled or not when it opened
[self.infoPopover close];
[self.hintPopover close];
}
- (void)infoClicked:(NSButton *)sender
{
const NSInteger index=[self.tableView rowForView:sender];
if (index<0) {
return;
}
[self.infoPopover close];
self.infoPopover=[[NSPopover alloc] init];
self.infoPopover.behavior=NSPopoverBehaviorTransient;
self.infoPopover.contentViewController=[[SourceInfoViewController alloc] initWithRow:self.rows[index] editable:self.enabled];
[self.infoPopover showRelativeToRect:sender.bounds ofView:sender preferredEdge:NSRectEdgeMaxX];
}
- (void)removeClicked:(id)sender
{
const NSInteger index=self.tableView.selectedRow;
if (index>=0) {
[self.sourceTable removeRow:self.rows[index]];
}
}
#pragma mark Table data source and delegate
- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView
{
return [self.rows count];
}
- (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
return self.rows[row];
}
- (void)tableViewSelectionDidChange:(NSNotification *)notification
{
// removing works whether or not reversing is on
self.removeButton.enabled=self.tableView.selectedRow>=0;
}
- (NSTableRowView *)tableView:(NSTableView *)tableView rowViewForRow:(NSInteger)row
{
return [[SourceRowView alloc] init];
}
- (void)tableView:(NSTableView *)tableView didAddRowView:(SourceRowView *)rowView forRow:(NSInteger)row
{
[self updateRowView:rowView forRow:self.rows[row]];
}
- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)rowIndex
{
SourceRow *const row=self.rows[rowIndex];
NSString *identifier=tableColumn.identifier;
if ([identifier isEqualToString:kColumnDirection]) {
identifier=row.hasDeviceDirection?kCellDirectionDevice:kCellDirectionApp;
}
NSTableCellView *const cell=[tableView makeViewWithIdentifier:identifier owner:self]?:[self makeCellWithIdentifier:identifier];
if ([identifier isEqualToString:kColumnIcon]) {
cell.imageView.image=row.icon;
}
return cell;
}
#pragma mark Cells
// Cells show their row, as their object value, mostly through bindings.
- (NSTableCellView *)makeCellWithIdentifier:(NSString *)identifier
{
NSTableCellView *const cell=[[NSTableCellView alloc] init];
cell.identifier=identifier;
NSView *content;
if ([identifier isEqualToString:kColumnIcon]) {
NSImageView *const imageView=[[DimmingImageView alloc] init];
imageView.imageScaling=NSImageScaleNone;
[imageView.widthAnchor constraintEqualToConstant:22].active=YES;
[imageView.heightAnchor constraintEqualToConstant:18].active=YES;
cell.imageView=imageView;
content=imageView;
}
else if ([identifier isEqualToString:kColumnName]) {
NSTextField *const field=[DimmingLabel labelWithString:@""];
field.lineBreakMode=NSLineBreakByTruncatingTail;
field.cell.usesSingleLineMode=YES;
[field bind:NSValueBinding toObject:cell withKeyPath:@"objectValue.displayName" options:nil];
// the full name, for when it's truncated
[field bind:NSToolTipBinding toObject:cell withKeyPath:@"objectValue.displayName" options:nil];
cell.textField=field;
content=field;
}
else if ([identifier isEqualToString:kCellDirectionDevice]) {
content=[self directionPopUpForCell:cell modes:@[@(ScrollDirectionModeNatural), @(ScrollDirectionModeClassic), @(ScrollDirectionModeUnchanged)]];
}
else if ([identifier isEqualToString:kCellDirectionApp]) {
// an app chooses its own direction, so all we can do is reverse it
content=[self directionPopUpForCell:cell modes:@[@(ScrollDirectionModeReversed), @(ScrollDirectionModeUnchanged)]];
}
else if ([identifier isEqualToString:kColumnStepSize]) {
content=[self stepSizeViewForCell:cell];
}
else if ([identifier isEqualToString:kColumnLastUsed]) {
NSTextField *const field=[DimmingLabel labelWithString:@""];
field.font=_smallFont();
field.textColor=[NSColor secondaryLabelColor];
field.lineBreakMode=NSLineBreakByTruncatingTail;
cell.textField=field;
content=field;
}
else if ([identifier isEqualToString:kColumnInfo]) {
NSButton *const button=[NSButton buttonWithImage:[NSImage imageWithSystemSymbolName:@"gearshape" accessibilityDescription:NSLocalizedString(@"Details", @"Accessibility label of the button that shows a source's details")] target:self action:@selector(infoClicked:)];
button.bordered=NO;
button.toolTip=NSLocalizedString(@"Details and horizontal scrolling", @"Tooltip of the button that shows a source's details");
content=button;
}
if (![identifier isEqualToString:kColumnInfo]) {
[self bindEnabledOfControlsInView:content];
}
content.translatesAutoresizingMaskIntoConstraints=NO;
[cell addSubview:content];
const BOOL leading=[identifier isEqualToString:kColumnName];
[NSLayoutConstraint activateConstraints:@[
[content.centerYAnchor constraintEqualToAnchor:cell.centerYAnchor],
leading?[content.leadingAnchor constraintEqualToAnchor:cell.leadingAnchor constant:2]:[content.centerXAnchor constraintEqualToAnchor:cell.centerXAnchor],
leading?[cell.trailingAnchor constraintEqualToAnchor:content.trailingAnchor constant:2]:[cell.trailingAnchor constraintGreaterThanOrEqualToAnchor:content.trailingAnchor],
]];
return cell;
}
// Controls are enabled only while reversing is on, as well as by any enabled binding they already have.
- (void)bindEnabledOfControlsInView:(NSView *)view
{
if ([view isKindOfClass:[NSControl class]]) {
NSString *const binding=[view infoForBinding:NSEnabledBinding]?@"enabled2":NSEnabledBinding;
[view bind:binding toObject:self withKeyPath:@"enabled" options:nil];
}
for (NSView *subview in view.subviews) {
[self bindEnabledOfControlsInView:subview];
}
}
// The cell may be nil, for measuring.
- (NSPopUpButton *)directionPopUpForCell:(NSTableCellView *)cell modes:(NSArray<NSNumber *> *)modes
{
NSPopUpButton *const popUp=[[NSPopUpButton alloc] initWithFrame:NSZeroRect pullsDown:NO];
popUp.controlSize=NSControlSizeSmall;
popUp.font=_smallFont();
for (NSNumber *mode in modes) {
[popUp addItemWithTitle:SourceDirectionModeTitle([mode integerValue])];
popUp.lastItem.tag=[mode integerValue];
}
if (cell) {
[popUp bind:NSSelectedTagBinding toObject:cell withKeyPath:@"objectValue.verticalMode" options:nil];
}
return popUp;
}
// A checkbox, number field and stepper, or "Continuous" for a source with no steps to size.
- (NSView *)stepSizeViewForCell:(NSTableCellView *)cell
{
NSButton *const checkbox=[NSButton checkboxWithTitle:@"" target:nil action:nil];
checkbox.accessibilityLabel=NSLocalizedString(@"Step size", @"Sources table column header: scroll wheel step size");
[checkbox bind:NSValueBinding toObject:cell withKeyPath:@"objectValue.stepSizeEnabled" options:nil];
NSNumberFormatter *const formatter=[[NSNumberFormatter alloc] init];
formatter.allowsFloats=NO;
formatter.minimum=@(SourceRowMinStepSize);
formatter.maximum=@(SourceRowMaxStepSize);
NSTextField *const field=[NSTextField textFieldWithString:@""];
field.formatter=formatter;
field.alignment=NSTextAlignmentRight;
field.controlSize=NSControlSizeSmall;
field.font=_smallFont();
[field.widthAnchor constraintEqualToConstant:30].active=YES;
[field bind:NSValueBinding toObject:cell withKeyPath:@"objectValue.stepSize" options:@{NSValidatesImmediatelyBindingOption: @YES}];
NSStepper *const stepper=[[NSStepper alloc] init];
stepper.controlSize=NSControlSizeSmall;
stepper.minValue=SourceRowMinStepSize;
stepper.maxValue=SourceRowMaxStepSize;
stepper.increment=1;
stepper.valueWraps=NO;
[stepper bind:NSValueBinding toObject:cell withKeyPath:@"objectValue.stepSize" options:nil];
for (NSControl *control in @[field, stepper]) {
[control bind:NSEnabledBinding toObject:cell withKeyPath:@"objectValue.stepSizeEnabled" options:nil];
}
NSStackView *const controls=[NSStackView stackViewWithViews:@[checkbox, field, stepper]];
controls.spacing=4;
[controls setCustomSpacing:0 afterView:field];
[controls bind:NSHiddenBinding toObject:cell withKeyPath:@"objectValue.continuous" options:nil];
NSTextField *const continuousLabel=[DimmingLabel labelWithString:NSLocalizedString(@"Continuous", @"Scrolling type of a source that scrolls continuously, like a trackpad")];
continuousLabel.font=_smallFont();
continuousLabel.textColor=[NSColor secondaryLabelColor];
[continuousLabel bind:NSHiddenBinding toObject:cell withKeyPath:@"objectValue.continuous" options:_negated()];
return [NSStackView stackViewWithViews:@[controls, continuousLabel]];
}
@end