forked from akahippac/Barmoji
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Barmoji.xm
302 lines (240 loc) · 13 KB
/
Barmoji.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
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
//
// Barmoji.xm
// Barmoji
//
// Created by Juan Carlos Perez <[email protected]> 01/16/2018
// © CP Digital Darkroom <[email protected]> All rights reserved.
//
#import "Barmoji.h"
#import "BarmojiCollectionView.h"
#import <version.h>
extern "C" CFNotificationCenterRef CFNotificationCenterGetDistributedCenter(void);
BOOL barmojiEnabled = NO;
BOOL barmojiHideGlobe = NO;
BOOL barmojiHideDictation = NO;
BOOL showingBarmoji = YES;
int barmojiEmojisPosition = 1;
int barmojiFeedbackType = 7;
int barmojiBottomLeading = 60;
int barmojiBottomTrailing = -60;
int barmojiBottomHeight = -20;
int barmojiEmojiPerRow = 6;
%group thirteenPlus
%hook TUIPredictionView
%property (retain, nonatomic) BarmojiCollectionView *barmoji;
- (instancetype)initWithFrame:(CGRect)frame {
TUIPredictionView *predictionView = %orig;
if (predictionView) {
if (barmojiEmojisPosition == 1) {
self.barmoji = [[BarmojiCollectionView alloc] initForPredictiveBar:YES];
self.barmoji.feedbackType = barmojiFeedbackType;
self.barmoji.translatesAutoresizingMaskIntoConstraints = NO;
[predictionView addSubview:self.barmoji];
[predictionView addConstraint:[NSLayoutConstraint constraintWithItem:self.barmoji attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:predictionView attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0]];
[predictionView addConstraint:[NSLayoutConstraint constraintWithItem:self.barmoji attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:predictionView attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:0]];
[predictionView addConstraint:[NSLayoutConstraint constraintWithItem:self.barmoji attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:30]];
[predictionView addConstraint:[NSLayoutConstraint constraintWithItem:self.barmoji attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:predictionView attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0]];
UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(flipSubviewHiddenStatus:)];
longPressGesture.minimumPressDuration = 0.25;
[self addGestureRecognizer:longPressGesture];
}
}
return predictionView;
}
- (void)addSubview:(UIView *)subview {
if (barmojiEmojisPosition == 1) {
if (![subview isKindOfClass:[BarmojiCollectionView class]]) {
subview.hidden = YES;
}
}
%orig;
}
- (void)layoutSubviews {
%orig;
[[NSNotificationCenter defaultCenter] postNotificationName:@"TryReloadTest" object:nil];
}
- (void)_didRecognizeTapGesture:(id)arg1 {
if (barmojiEmojisPosition == 1 && showingBarmoji) {
return;
}
%orig;
}
%new
- (void)flipSubviewHiddenStatus:(UILongPressGestureRecognizer *)recognizer {
if (recognizer.state == UIGestureRecognizerStateBegan) {
[self toggleBarmoji];
}
}
%new
- (void)toggleBarmoji {
showingBarmoji = !showingBarmoji;
for (UIView *subview in self.subviews) {
subview.hidden = !subview.hidden;
subview.userInteractionEnabled = !subview.userInteractionEnabled;
}
}
- (void)setAutocorrectionList:(TIAutocorrectionList *)list animated:(BOOL)animated {
%orig;
TIKeyboardCandidateSingle *correction = list.autocorrection;
BOOL wantsCorrection = list.shouldAcceptTopCandidate || list.containsAutofillCandidates || list.containsProactiveTriggers;
if (wantsCorrection && ![correction.input isEqualToString:correction.candidate]) {
if (showingBarmoji) {
[self toggleBarmoji];
}
} else if (!showingBarmoji) {
[self toggleBarmoji];
}
}
%end // TUIPredictionView
%end // thirteenPlus group
%group lessThirteen
%hook UIKeyboardPredictionView
%property (retain, nonatomic) BarmojiCollectionView *barmoji;
- (instancetype)initWithFrame:(CGRect)frame {
UIKeyboardPredictionView *predictionView = %orig;
if (predictionView) {
if (barmojiEmojisPosition == 1) {
self.barmoji = [[BarmojiCollectionView alloc] initForPredictiveBar:YES];
self.barmoji.feedbackType = barmojiFeedbackType;
self.barmoji.translatesAutoresizingMaskIntoConstraints = NO;
[predictionView addSubview:self.barmoji];
[predictionView addConstraint:[NSLayoutConstraint constraintWithItem:self.barmoji attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:predictionView attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0]];
[predictionView addConstraint:[NSLayoutConstraint constraintWithItem:self.barmoji attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:predictionView attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:0]];
[predictionView addConstraint:[NSLayoutConstraint constraintWithItem:self.barmoji attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:30]];
[predictionView addConstraint:[NSLayoutConstraint constraintWithItem:self.barmoji attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:predictionView attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0]];
UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(flipSubviewHiddenStatus:)];
longPressGesture.minimumPressDuration = 0.25;
[self addGestureRecognizer:longPressGesture];
}
}
return predictionView;
}
- (void)addSubview:(UIView *)subview {
if (barmojiEmojisPosition == 1) {
if (![subview isKindOfClass:[BarmojiCollectionView class]]) {
subview.hidden = YES;
}
}
%orig;
}
- (void)activateCandidateAtPoint:(CGPoint)arg1 {
if (barmojiEmojisPosition == 1 && showingBarmoji) {
return;
}
%orig;
}
%new
- (void)flipSubviewHiddenStatus:(UILongPressGestureRecognizer *)recognizer {
if (recognizer.state == UIGestureRecognizerStateBegan) {
showingBarmoji = !showingBarmoji;
for (UIView *subview in self.subviews) {
subview.hidden = !subview.hidden;
subview.userInteractionEnabled = !subview.userInteractionEnabled;
}
}
}
%end // UIKeyboardPredictionView
%end // lessThirteen group
%group common
%hook UIKeyboardDockView
%property (retain, nonatomic) BarmojiCollectionView *barmoji;
- (instancetype)initWithFrame:(CGRect)frame {
UIKeyboardDockView *dockView = %orig;
if (dockView) {
if (barmojiEmojisPosition == 2) {
UIDeviceOrientation deviceOrientation = [[UIDevice currentDevice] orientation];
BOOL deviceLandscape = UIDeviceOrientationIsLandscape(deviceOrientation);
self.barmoji = [[BarmojiCollectionView alloc] initForPredictiveBar:NO];
self.alpha = deviceLandscape ? 0.0 : 1.0;
self.barmoji.feedbackType = barmojiFeedbackType;
self.barmoji.translatesAutoresizingMaskIntoConstraints = NO;
[dockView addSubview:self.barmoji];
[dockView addConstraint:[NSLayoutConstraint constraintWithItem:self.barmoji attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:dockView attribute:NSLayoutAttributeLeading multiplier:1.0 constant:barmojiBottomLeading]];
[dockView addConstraint:[NSLayoutConstraint constraintWithItem:self.barmoji attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:dockView attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:barmojiBottomTrailing]];
[dockView addConstraint:[NSLayoutConstraint constraintWithItem:self.barmoji attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:60]];
[dockView addConstraint:[NSLayoutConstraint constraintWithItem:self.barmoji attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:dockView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:barmojiBottomHeight]];
}
}
return dockView;
}
- (void)setLeftDockItem:(UIKeyboardDockItem *)arg1 {
if (barmojiHideGlobe) { return;}
%orig;
}
- (void)setRightDockItem:(UIKeyboardDockItem *)arg1 {
if (barmojiHideDictation) { return;}
%orig;
}
- (void)layoutSubviews {
%orig;
[[NSNotificationCenter defaultCenter] postNotificationName:@"barmoji_reloadLayout" object:nil];
}
%end // UIKeyboardDockView
//Fix for barmoji view overlapping when dicatation is running
%hook UISystemKeyboardDockController
- (void)updateDockItemsVisibility {
%orig;
self.dockView.barmoji.hidden = self.dockView.centerDockItem ? !self.dockView.centerDockItem.view.hidden : NO;
}
%end
%end // common group
static void loadPrefs() {
NSMutableDictionary *prefs = [[NSMutableDictionary alloc] initWithContentsOfFile:@"/private/var/mobile/Library/Preferences/com.cpdigitaldarkroom.barmoji.plist"];
barmojiEnabled = ([prefs objectForKey:@"BarmojiEnabled"] ? [[prefs objectForKey:@"BarmojiEnabled"] boolValue] : NO);
barmojiFeedbackType = ([prefs objectForKey:@"BarmojiFeedbackType"] ? [[prefs objectForKey:@"BarmojiFeedbackType"] intValue] : 7);
barmojiBottomLeading = ([prefs objectForKey:@"BarmojiBottomLeading"] ? [[prefs objectForKey:@"BarmojiBottomLeading"] intValue] : 60);
barmojiBottomTrailing = ([prefs objectForKey:@"BarmojiBottomTrailing"] ? [[prefs objectForKey:@"BarmojiBottomTrailing"] intValue] : -60);
barmojiBottomHeight = ([prefs objectForKey:@"BarmojiBottomHeight"] ? [[prefs objectForKey:@"BarmojiBottomHeight"] intValue] : -20);
barmojiEmojiPerRow = ([prefs objectForKey:@"BarmojiEmojiPerRow"] ? [[prefs objectForKey:@"BarmojiEmojiPerRow"] intValue] : 6);
barmojiHideGlobe = ([prefs objectForKey:@"BarmojiHideGlobe"] ? [[prefs objectForKey:@"BarmojiHideGlobe"] boolValue] : NO);
barmojiHideDictation = ([prefs objectForKey:@"BarmojiHideDictation"] ? [[prefs objectForKey:@"BarmojiHideDictation"] boolValue] : NO);
barmojiEmojisPosition = ([prefs objectForKey:@"EmojisPosition"] ? [[prefs objectForKey:@"EmojisPosition"] intValue] : 1);
}
static void updateSettings(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo) {
NSDictionary *info = (__bridge NSDictionary *)userInfo;
barmojiEnabled = [info[@"enabled"] boolValue];
barmojiFeedbackType = [info[@"feedbackType"] intValue];
barmojiBottomLeading = [info[@"BarmojiBottomLeading"] intValue];
barmojiBottomTrailing = [info[@"BarmojiBottomTrailing"] intValue];
barmojiBottomHeight = [info[@"BarmojiBottomHeight"] intValue];
barmojiEmojiPerRow = [info[@"BarmojiEmojiPerRow"] intValue];
barmojiHideGlobe = [info[@"BarmojiHideGlobe"] boolValue];
barmojiHideDictation = [info[@"BarmojiHideDictation"] boolValue];
barmojiEmojisPosition = [info[@"EmojisPosition"] intValue];
}
%ctor {
@autoreleasepool {
// check if process is springboard or an application
// this prevents our tweak from running in non-application (with UI)
// processes and also prevents bad behaving tweaks to invoke our tweak
loadPrefs();
if (barmojiEnabled) {
NSArray *args = [[NSClassFromString(@"NSProcessInfo") processInfo] arguments];
if (args.count != 0) {
NSString *executablePath = args[0];
if (executablePath) {
NSString *processName = [executablePath lastPathComponent];
BOOL isSpringBoard = [processName isEqualToString:@"SpringBoard"];
BOOL isApplication = [executablePath rangeOfString:@"/Application"].location != NSNotFound;
if (isSpringBoard || isApplication) {
if (@available(iOS 13, *)) {
NSBundle *bundle = [NSBundle bundleWithPath:@"/System/Library/PrivateFrameworks/TextInputUI.framework"];
if (!bundle.loaded) [bundle load];
if (barmojiEmojisPosition == 1) {
%init(thirteenPlus)
}
} else {
if (barmojiEmojisPosition == 1) {
%init(lessThirteen)
}
}
CFNotificationCenterAddObserver(CFNotificationCenterGetDistributedCenter(), NULL, updateSettings, CFSTR("com.cpdigitaldarkroom.barmoji.settings"), NULL, CFNotificationSuspensionBehaviorDeliverImmediately);
if (barmojiEmojisPosition == 2) {
%init(common);
}
}
}
}
}
}
}