forked from hbang/TypeStatus-Mac
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTweak.xm
230 lines (175 loc) · 7.02 KB
/
Tweak.xm
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
#import <AppKit/AppKit.h>
#import <IMCore/IMAccount.h>
#import <IMCore/IMHandle.h>
#import <IMCore/IMServiceImpl.h>
#import <IMFoundation/FZMessage.h>
#import <version.h>
NSBundle *bundle;
NSStatusItem *statusItem;
NSUserDefaults *userDefaults;
NSUInteger typingIndicators = 0;
#pragma mark - Constants
typedef NS_ENUM(NSUInteger, HBTSStatusBarType) {
HBTSStatusBarTypeTyping,
HBTSStatusBarTypeRead,
HBTSStatusBarTypeEmpty
};
static NSString *const kHBTSPreferencesSuiteName = @"ws.hbang.typestatusmac";
static NSString *const kHBTSPreferencesLastVersionKey = @"LastVersion";
static NSString *const kHBTSPreferencesInvertedKey = @"Inverted";
static NSString *const kHBTSPreferencesDurationKey = @"OverlayDuration";
static NSTimeInterval const kHBTSTypingTimeout = 60;
#pragma mark - Contact names
NSString *HBTSNameForHandle(NSString *address) {
IMAccount *account = IMPreferredSendingAccountForAddressesWithFallbackService(@[ address ], [IMServiceImpl iMessageService]);
if (!account._isUsableForSending) {
return address;
}
IMHandle *handle = [account imHandleWithID:address];
return handle._displayNameWithAbbreviation ?: address;
}
#pragma mark - Tint image
NSImage *HBTSTintImageWithColor(NSImage *image, NSColor *color) {
// https://stackoverflow.com/a/16138027/709376
NSImage *newImage = [[image copy] autorelease];
[newImage lockFocus];
[color set];
NSRectFillUsingOperation((NSRect){ NSZeroPoint, newImage.size }, NSCompositeSourceAtop);
[newImage unlockFocus];
return newImage;
}
#pragma mark - Status item stuff
void HBTSSetStatus(HBTSStatusBarType type, NSString *handle) {
static NSImage *TypingIcon;
static NSImage *TypingIconInverted;
static NSImage *ReadIcon;
static NSImage *ReadIconInverted;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
TypingIcon = [[bundle imageForResource:@"Typing.tiff"] retain];
[TypingIcon setTemplate:YES]; // eugh. dot notation doesn't work for this.
TypingIcon.size = CGSizeMake(22.f, 22.f);
ReadIcon = [[bundle imageForResource:@"Read.tiff"] retain];
[ReadIcon setTemplate:YES];
ReadIcon.size = CGSizeMake(22.f, 22.f);
if (!IS_OSX_OR_NEWER(10_10)) {
TypingIconInverted = [HBTSTintImageWithColor(TypingIcon, [NSColor whiteColor]) retain];
ReadIconInverted = [HBTSTintImageWithColor(ReadIcon, [NSColor whiteColor]) retain];
}
});
if (type == HBTSStatusBarTypeEmpty) {
statusItem.length = 0;
statusItem.title = nil;
statusItem.attributedTitle = nil;
return;
}
BOOL inverted = !IS_OSX_OR_NEWER(10_10) && [userDefaults boolForKey:kHBTSPreferencesInvertedKey];
NSString *name = HBTSNameForHandle(handle);
if (IS_OSX_OR_NEWER(10_10)) {
statusItem.title = name; // It Just Works(tm)
} else {
statusItem.attributedTitle = [[[NSAttributedString alloc] initWithString:name attributes:@{
NSFontAttributeName: [NSFont menuBarFontOfSize:0],
NSForegroundColorAttributeName: [NSColor colorWithCalibratedWhite:inverted ? 0.7490196078f : 0 alpha:1]
}] autorelease];
}
switch (type) {
case HBTSStatusBarTypeTyping:
statusItem.image = inverted ? TypingIconInverted : TypingIcon;
break;
case HBTSStatusBarTypeRead:
statusItem.image = inverted ? ReadIconInverted : ReadIcon;
break;
case HBTSStatusBarTypeEmpty:
break;
}
statusItem.length = -1;
}
#pragma mark - Typing detection
%hook IMChatRegistry
- (void)_processMessageForAccount:(id)account chat:(id)chat style:(unsigned char)style chatProperties:(id)properties message:(FZMessage *)message {
%orig;
if (message.flags == 4104) {
typingIndicators++;
HBTSSetStatus(HBTSStatusBarTypeTyping, message.handle);
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(kHBTSTypingTimeout * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
HBTSSetStatus(HBTSStatusBarTypeEmpty, nil);
});
} else {
if (typingIndicators == 0) {
return;
}
typingIndicators--;
if (typingIndicators == 0) {
HBTSSetStatus(HBTSStatusBarTypeEmpty, nil);
}
}
}
%end
%hook FZMessage
- (void)setTimeRead:(NSDate *)timeRead {
%orig;
if (!self.sender && [[NSDate date] timeIntervalSinceDate:self.timeRead] < 1) {
HBTSSetStatus(HBTSStatusBarTypeRead, self.handle);
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)([userDefaults doubleForKey:kHBTSPreferencesDurationKey] * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
HBTSSetStatus(HBTSStatusBarTypeEmpty, nil);
});
}
}
%end
#pragma mark - First run
void HBTSShowFirstRunAlert() {
NSAlert *alert = [[[NSAlert alloc] init] autorelease];
alert.messageText = @"Welcome to TypeStatus";
alert.informativeText = @"You’ll now see subtle notifications in your menu bar when someone is typing an iMessage to you or reads an iMessage you sent.\nIf you like TypeStatus, don’t forget to let your friends know about it!";
if (!IS_OSX_OR_NEWER(10_10)) {
alert.showsSuppressionButton = YES;
alert.suppressionButton.title = @"I use a dark menu bar theme";
}
[alert runModal];
[userDefaults setObject:bundle.infoDictionary[@"CFBundleVersion"] forKey:kHBTSPreferencesLastVersionKey];
[userDefaults setBool:alert.suppressionButton.state == NSOnState forKey:kHBTSPreferencesInvertedKey];
}
#pragma mark - Updates
void HBTSCheckUpdate() {
NSString *currentVersion = bundle.infoDictionary[@"CFBundleShortVersionString"];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSData *data = [NSURLConnection sendSynchronousRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://cdn.hbang.ws/updates/typestatusmac.json?version=%@", currentVersion]] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:30] returningResponse:nil error:nil];
if (!data || !data.length) {
NSLog(@"TypeStatus: update check failed - no data received");
return;
}
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
if (!json) {
NSLog(@"TypeStatus: json deserialization failed");
return;
}
if (![json[@"version"] isEqualToString:currentVersion]) {
dispatch_async(dispatch_get_main_queue(), ^{
NSAlert *alert = [[[NSAlert alloc] init] autorelease];
alert.messageText = @"A TypeStatus update is available";
alert.informativeText = [NSString stringWithFormat:@"The new version is %@. You have version %@.", json[@"version"], currentVersion];
[alert addButtonWithTitle:@"Install"];
[alert addButtonWithTitle:@"No Thanks"];
if ([alert runModal] == NSAlertFirstButtonReturn) {
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:json[@"url"]]];
}
});
}
});
}
#pragma mark - Constructor
%ctor {
%init;
bundle = [[NSBundle bundleWithIdentifier:@"ws.hbang.typestatus.mac"] retain];
statusItem = [[[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength] retain];
userDefaults = [[NSUserDefaults alloc] initWithSuiteName:kHBTSPreferencesSuiteName];
[userDefaults registerDefaults:@{
kHBTSPreferencesDurationKey: @5.0,
kHBTSPreferencesInvertedKey: @NO
}];
if (![userDefaults objectForKey:kHBTSPreferencesLastVersionKey]) {
HBTSShowFirstRunAlert();
}
HBTSCheckUpdate();
}