Repository navigation
Expand file tree
/
Copy pathSourceInfoViewController.m
More file actions
159 lines (138 loc) · 6.88 KB
/
Copy pathSourceInfoViewController.m
File metadata and controls
159 lines (138 loc) · 6.88 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
// 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 "SourceInfoViewController.h"
#import "SourceRow+Display.h"
static NSFont *_font(void)
{
return [NSFont systemFontOfSize:[NSFont smallSystemFontSize]];
}
static NSTextField *_title(NSString *string)
{
NSTextField *const field=[NSTextField labelWithString:string];
field.font=_font();
field.textColor=[NSColor secondaryLabelColor];
return field;
}
static NSTextField *_value(NSString *string)
{
NSTextField *const field=[NSTextField wrappingLabelWithString:string?:@"—"];
field.selectable=YES;
field.font=_font();
[field.widthAnchor constraintLessThanOrEqualToConstant:320].active=YES;
return field;
}
// The scroll total and system direction change with every event, without notifications, so they're
// looked at again this often while the popover is shown.
static const NSTimeInterval kRefreshInterval=0.1;
@interface SourceInfoViewController ()
@property SourceRow *row;
@property BOOL editable;
@property NSTextField *directionField;
@property NSTextField *scrolledField;
@property NSTimer *refreshTimer;
@end
@implementation SourceInfoViewController
- (instancetype)initWithRow:(SourceRow *)row editable:(BOOL)editable
{
self=[super initWithNibName:nil bundle:nil];
if (self) {
self.row=row;
self.editable=editable;
}
return self;
}
- (void)loadView
{
SourceRow *const row=self.row;
// empty for the default name, which shows as the placeholder (the binding's, since it replaces the field's own)
NSTextField *const nameField=[NSTextField textFieldWithString:@""];
[nameField.widthAnchor constraintEqualToConstant:240].active=YES;
[nameField bind:NSValueBinding toObject:row withKeyPath:@"customName" options:@{NSContinuouslyUpdatesValueBindingOption: @YES,
NSNullPlaceholderBindingOption: row.defaultName}];
NSPopUpButton *const horizontalPopUp=[[NSPopUpButton alloc] initWithFrame:NSZeroRect pullsDown:NO];
horizontalPopUp.controlSize=NSControlSizeSmall;
horizontalPopUp.font=_font();
[horizontalPopUp addItemWithTitle:SourceDirectionModeTitle(ScrollDirectionModeMatchVertical)];
horizontalPopUp.lastItem.tag=ScrollDirectionModeMatchVertical;
// its tag would otherwise be 0, Unchanged's, and the binding selects the first item with the tag
NSMenuItem *const separator=[NSMenuItem separatorItem];
separator.tag=-1;
[horizontalPopUp.menu addItem:separator];
for (NSNumber *mode in @[@(ScrollDirectionModeReversed), @(ScrollDirectionModeUnchanged)]) {
[horizontalPopUp addItemWithTitle:SourceDirectionModeTitle([mode integerValue])];
horizontalPopUp.lastItem.tag=[mode integerValue];
}
[horizontalPopUp bind:NSSelectedTagBinding toObject:row withKeyPath:@"horizontalMode" options:nil];
nameField.enabled=self.editable;
horizontalPopUp.enabled=self.editable;
NSDateFormatter *const dateFormatter=[[NSDateFormatter alloc] init];
dateFormatter.dateStyle=NSDateFormatterMediumStyle;
dateFormatter.timeStyle=NSDateFormatterShortStyle;
self.directionField=_value(row.systemDirectionDescription);
self.scrolledField=_value(self.scrolledString);
NSMutableArray<NSArray<NSView *> *> *const rows=[NSMutableArray arrayWithArray:@[
@[_title(NSLocalizedString(@"Name:", @"Source info: label before the source's name field")), nameField],
@[_title(NSLocalizedString(@"Horizontal scrolling:", @"Source info: label before the horizontal scrolling menu")), horizontalPopUp],
@[_title(NSLocalizedString(@"Kind:", @"Source info: label before the kind of source, such as Mouse or App")), _value(row.kindDescription)],
@[_title(NSLocalizedString(@"Reported name:", @"Source info: label before the name the device or app reports")), _value(row.reportedName)],
@[_title(NSLocalizedString(@"Identifier:", @"Source info: label before the identifier of the source's settings")), _value(row.key)],
]];
if (row.path) {
[rows addObject:@[_title(NSLocalizedString(@"Path:", @"Source info: label before the location of an app")), _value(row.path)]];
}
[rows addObjectsFromArray:@[
@[_title(NSLocalizedString(@"Scrolling type:", @"Source info: label before Continuous or Discrete")), _value(row.scrollingTypeDescription)],
@[_title(NSLocalizedString(@"System direction:", @"Source info: label before the direction the system delivers the source's scrolling in, such as Natural")), self.directionField],
@[_title(NSLocalizedString(@"First seen:", @"Source info: label before the date the source first scrolled")), _value([dateFormatter stringFromDate:row.firstSeen])],
@[_title(NSLocalizedString(@"Points scrolled:", @"Source info: label before the total distance the source has scrolled, as a number of points (the unit of distance on screen)")), self.scrolledField],
]];
NSGridView *const grid=[NSGridView gridViewWithViews:rows];
grid.translatesAutoresizingMaskIntoConstraints=NO;
grid.rowAlignment=NSGridRowAlignmentFirstBaseline;
grid.rowSpacing=6;
grid.columnSpacing=6;
[grid columnAtIndex:0].xPlacement=NSGridCellPlacementTrailing;
NSView *const view=[[NSView alloc] init];
[view addSubview:grid];
[NSLayoutConstraint activateConstraints:@[
[grid.topAnchor constraintEqualToAnchor:view.topAnchor constant:14],
[grid.leadingAnchor constraintEqualToAnchor:view.leadingAnchor constant:14],
[view.trailingAnchor constraintEqualToAnchor:grid.trailingAnchor constant:14],
[view.bottomAnchor constraintEqualToAnchor:grid.bottomAnchor constant:14],
]];
self.view=view;
}
// the unit is in the label, so the value needs no plural forms
- (NSString *)scrolledString
{
return [NSNumberFormatter localizedStringFromNumber:@(round(self.row.scrollTotal)) numberStyle:NSNumberFormatterDecimalStyle];
}
- (void)viewWillAppear
{
[super viewWillAppear];
[self refresh];
__weak SourceInfoViewController *const weakSelf=self;
self.refreshTimer=[NSTimer timerWithTimeInterval:kRefreshInterval repeats:YES block:^(NSTimer *timer) {
[weakSelf refresh];
}];
self.refreshTimer.tolerance=kRefreshInterval/5;
[[NSRunLoop mainRunLoop] addTimer:self.refreshTimer forMode:NSRunLoopCommonModes];
}
- (void)viewDidDisappear
{
[super viewDidDisappear];
[self.refreshTimer invalidate];
self.refreshTimer=nil;
}
- (void)refresh
{
NSString *const direction=self.row.systemDirectionDescription;
if (![self.directionField.stringValue isEqualToString:direction]) {
self.directionField.stringValue=direction;
}
NSString *const scrolled=self.scrolledString;
if (![self.scrolledField.stringValue isEqualToString:scrolled]) {
self.scrolledField.stringValue=scrolled;
}
}
@end