forked from kylehowells/SwipeSelection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTweak.mm
552 lines (444 loc) · 18.4 KB
/
Tweak.mm
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
// **************************************************** //
// **************************************************** //
// ********** Design outline ********** //
// **************************************************** //
// **************************************************** //
//
// 1 finger moves the cursour
// 2 fingers moves it one word at a time
//
// Should be able to move between 1 and 2 fingers without lifting your hand.
// If a selection has been made and you move right the selection starts moving from the end.
// - else it starts at the beginning.
//
// Holding shift selects text between the starting point and the destination.
// - the starting point is the reverse of the non selection movement.
// - - movement to the right starts at the start of existing selections.
//
// Movement upwards when in 2 finger mode should jump to the nearest word in the new line.
// - But another movement up again (without sideways movement) will jump to the nearest word to the originals x location,
// - - this ensures that the cursour doesn't jump about moving far away from it's start point.
//
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import <UIKit/UITextInput.h>
#import <UIKit/UIGestureRecognizerSubclass.h>
#pragma mark - Headers
@protocol UITextInputPrivate <UITextInput, UITextInputTokenizer> //, UITextInputTraits_Private, UITextSelectingContainer>
-(BOOL)shouldEnableAutoShift;
-(NSRange)selectionRange;
-(CGRect)rectForNSRange:(NSRange)nsrange;
-(NSRange)_markedTextNSRange;
//-(id)selectedDOMRange;
//-(id)wordInRange:(id)range;
//-(void)setSelectedDOMRange:(id)range affinityDownstream:(BOOL)downstream;
//-(void)replaceRangeWithTextWithoutClosingTyping:(id)textWithoutClosingTyping replacementText:(id)text;
//-(CGRect)rectContainingCaretSelection;
-(void)moveBackward:(unsigned)backward;
-(void)moveForward:(unsigned)forward;
-(unsigned short)characterBeforeCaretSelection;
-(id)wordContainingCaretSelection;
-(id)wordRangeContainingCaretSelection;
-(id)markedText;
-(void)setMarkedText:(id)text;
-(BOOL)hasContent;
-(void)selectAll;
-(id)textColorForCaretSelection;
-(id)fontForCaretSelection;
-(BOOL)hasSelection;
@end
@interface UIKBShape : NSObject
@end
@interface UIKBKey : UIKBShape
@property(copy) NSString * name;
@property(copy) NSString * representedString;
@property(copy) NSString * displayString;
@property(copy) NSString * displayType;
@property(copy) NSString * interactionType;
@property(copy) NSString * variantType;
//@property(copy) UIKBAttributeList * attributes;
@property(copy) NSString * overrideDisplayString;
@property(copy) NSString * clientVariantRepresentedString;
@property(copy) NSString * clientVariantActionName;
@property BOOL visible;
@property BOOL hidden;
@property BOOL disabled;
@property BOOL isGhost;
@property int splitMode;
@end
@interface UIKeyboardLayout : UIView
-(UIKBKey*)keyHitTest:(CGPoint)point;
@end
@interface UIKeyboardImpl : UIView
+(UIKeyboardImpl*)sharedInstance;
+(UIKeyboardImpl*)activeInstance;
@property (readonly, assign, nonatomic) UIResponder <UITextInputPrivate> *privateInputDelegate;
@property (readonly, assign, nonatomic) UIResponder <UITextInput> *inputDelegate;
-(BOOL)isLongPress;
-(id)_layout;
-(BOOL)callLayoutIsShiftKeyBeingHeld;
-(void)_KHKeyboardGestureDidPan:(UIPanGestureRecognizer*)gesture;
-(void)handleDelete;
@end
#pragma mark - Helper functions
UITextPosition *KH_tokenizerMovePositionWithGranularitInDirection(id <UITextInput, UITextInputTokenizer> tokenizer, UITextPosition *startPosition, UITextGranularity granularity, UITextDirection direction);
UITextPosition *KH_tokenizerMovePositionWithGranularitInDirection(id <UITextInput, UITextInputTokenizer> tokenizer, UITextPosition *startPosition, UITextGranularity granularity, UITextDirection direction){
if (tokenizer && startPosition) {
return [tokenizer positionFromPosition:startPosition toBoundary:granularity inDirection:direction];
}
return nil;
}
BOOL KH_positionsSame(id <UITextInput, UITextInputTokenizer> tokenizer, UITextPosition *position1, UITextPosition *position2);
BOOL KH_positionsSame(id <UITextInput, UITextInputTokenizer> tokenizer, UITextPosition *position1, UITextPosition *position2){
return ([tokenizer comparePosition:position1 toPosition:position2] == NSOrderedSame);
}
#pragma mark - GestureRecognizer
@interface KHPanGestureRecognizer : UIPanGestureRecognizer
@end
#pragma mark - Hooks
%hook UIKeyboardImpl
-(id)initWithFrame:(CGRect)rect{
id orig = %orig;
if (orig){
KHPanGestureRecognizer *pan = [[KHPanGestureRecognizer alloc] initWithTarget:self action:@selector(_KHKeyboardGestureDidPan:)];
pan.cancelsTouchesInView = NO;
[self addGestureRecognizer:pan];
[pan release];
}
return orig;
}
%new
-(void)_KHKeyboardGestureDidPan:(UIPanGestureRecognizer*)gesture{
// Location info (may change)
static UITextRange *startingtextRange = nil;
static CGPoint previousPosition;
// Basic info
static BOOL shiftHeldDown = NO;
static BOOL hasStarted = NO;
static BOOL longPress = NO;
static BOOL handWriting = NO;
static BOOL haveCheckedHand = NO;
static BOOL isFirstShiftDown = NO;
static int touchesWhenShiting = 0;
int touchesCount = [gesture numberOfTouches];
UIKeyboardImpl *keyboardImpl = self;//[%c(UIKeyboardImpl) sharedInstance];
if ([keyboardImpl respondsToSelector:@selector(isLongPress)]) {
BOOL nLongTouch = [keyboardImpl isLongPress];
if (nLongTouch) {
longPress = nLongTouch;
}
}
id currentLayout = nil;
if ([keyboardImpl respondsToSelector:@selector(_layout)]) {
currentLayout = [keyboardImpl _layout];
}
// Chinese handwriting check - (hacky)
if ([currentLayout respondsToSelector:@selector(subviews)] && !handWriting && !haveCheckedHand) {
NSArray *subviews = [((UIView*)currentLayout) subviews];
for (UIView *subview in subviews) {
if ([subview respondsToSelector:@selector(subviews)]) {
NSArray *arrayToCheck = [subview subviews];
for (id view in arrayToCheck) {
NSString *classString = [NSStringFromClass([view class]) lowercaseString];
NSString *substring = [@"Handwriting" lowercaseString];
if ([classString rangeOfString:substring].location != NSNotFound) {
handWriting = YES;
break;
}
}
}
}
haveCheckedHand = YES;
}
if ([keyboardImpl respondsToSelector:@selector(callLayoutIsShiftKeyBeingHeld)] && !shiftHeldDown) {
shiftHeldDown = [keyboardImpl callLayoutIsShiftKeyBeingHeld];
isFirstShiftDown = YES;
touchesWhenShiting = touchesCount;
}
id <UITextInputPrivate, NSObject, NSCoding> privateInputDelegate = nil;
if ([keyboardImpl respondsToSelector:@selector(privateInputDelegate)]) {
privateInputDelegate = (id)keyboardImpl.privateInputDelegate;
}
if (!privateInputDelegate && [keyboardImpl respondsToSelector:@selector(inputDelegate)]) {
privateInputDelegate = (id)keyboardImpl.inputDelegate;
}
if (gesture.state == UIGestureRecognizerStateEnded || gesture.state == UIGestureRecognizerStateCancelled) {
if ([privateInputDelegate respondsToSelector:@selector(selectedTextRange)]) {
UITextRange *range = [privateInputDelegate selectedTextRange];
if (range && !range.empty) {
CGRect screenBounds = [UIScreen mainScreen].bounds;
CGRect rect = CGRectMake(screenBounds.size.width * 0.5, screenBounds.size.height*0.5, 1, 1);
if ([privateInputDelegate respondsToSelector:@selector(firstRectForRange:)]) {
rect = [privateInputDelegate firstRectForRange:range];
}
UIView *view = nil;
if ([privateInputDelegate isKindOfClass:[UIView class]]) {
view = (UIView*)privateInputDelegate;
}
else if ([privateInputDelegate respondsToSelector:@selector(inputDelegate)]) {
id v = [keyboardImpl inputDelegate];
if (v != privateInputDelegate) {
if ([v isKindOfClass:[UIView class]]) {
view = (UIView*)v;
}
}
}
UIMenuController *menu = [UIMenuController sharedMenuController];
[menu setTargetRect:rect inView:view];
[menu setMenuVisible:YES animated:YES];
}
}
shiftHeldDown = NO;
longPress = NO;
hasStarted = NO;
handWriting = NO;
haveCheckedHand = NO;
touchesCount = 0;
touchesWhenShiting = 0;
gesture.cancelsTouchesInView = NO;
}
else if (longPress || handWriting || !privateInputDelegate) {
return;
}
else if (gesture.state == UIGestureRecognizerStateBegan) {
previousPosition = [gesture locationInView:self];
if ([privateInputDelegate respondsToSelector:@selector(selectedTextRange)]) {
[startingtextRange release], startingtextRange = nil;
startingtextRange = [[privateInputDelegate selectedTextRange] retain];
}
}
else if (gesture.state == UIGestureRecognizerStateChanged) {
UITextRange *currentRange = startingtextRange;
if ([privateInputDelegate respondsToSelector:@selector(selectedTextRange)]) {
currentRange = nil;
currentRange = [[[privateInputDelegate selectedTextRange] retain] autorelease];
}
CGPoint position = [gesture locationInView:self];
CGPoint delta = CGPointMake(position.x - previousPosition.x, position.y - previousPosition.y);
// Should we even run?
CGFloat deadZone = 10;
if (!hasStarted && delta.x < deadZone && delta.x > (-deadZone)) {
return;
}
// We are running so shut other things off/down
gesture.cancelsTouchesInView = YES;
hasStarted = YES;
// Make x & y positive for comparision
CGFloat positiveX = ((delta.x >= 0) ? delta.x : (-delta.x));
// CGFloat positiveY = ((delta.y >= 0) ? delta.y : (-delta.y));
// Determine the direction it should be going in
UITextDirection textDirection;
if (delta.x < 0) {
textDirection = UITextStorageDirectionBackward;
}
else {
textDirection = UITextStorageDirectionForward;
}
// Only do these new big 'jumps' if we've moved far enough
CGFloat xMinimum = 12;
// CGFloat yMinimum = 1;
CGFloat neededTouches = 2;
if (shiftHeldDown && (touchesWhenShiting >= 2)) {
neededTouches = 3;
}
UITextGranularity granularity = UITextGranularityCharacter;
// Handle different touches
if (touchesCount >= neededTouches) {
// make it skip words
granularity = UITextGranularityWord;
xMinimum = 26;
}
// Should we move the cusour or extend the current range.
BOOL extendRange = shiftHeldDown;
static UITextPosition *pivotPoint = nil;
// Get the new range
UITextPosition *positionStart = currentRange.start;
UITextPosition *positionEnd = currentRange.end;
// The moving position is
UITextPosition *_position = nil;
// If this is the first run we are selecting then pick our pivot point
if (isFirstShiftDown) {
[pivotPoint release], pivotPoint = nil;
if (delta.x > 0 || delta.y < -20) {
pivotPoint = [positionStart retain];
}
else {
pivotPoint = [positionEnd retain];
}
}
if (extendRange && pivotPoint) {
// Find which position isn't our pivot and move that.
BOOL startIsPivot = KH_positionsSame(privateInputDelegate, pivotPoint, positionStart);
if (startIsPivot) {
_position = positionEnd;
}
else {
_position = positionStart;
}
}
else {
_position = (delta.x > 0) ? positionEnd : positionStart;
if (!pivotPoint) {
pivotPoint = _position;
}
}
// Is it right to left at the current selection point?
if ([privateInputDelegate baseWritingDirectionForPosition:_position inDirection:UITextStorageDirectionForward] == UITextWritingDirectionRightToLeft) {
if (textDirection == UITextStorageDirectionForward){
textDirection = UITextStorageDirectionBackward;
}
else {
textDirection = UITextStorageDirectionForward;
}
}
id <UITextInputTokenizer> tokenizer = nil;
if ([privateInputDelegate respondsToSelector:@selector(positionFromPosition:toBoundary:inDirection:)]) {
tokenizer = privateInputDelegate;
}
else if ([privateInputDelegate respondsToSelector:@selector(tokenizer)]) {
tokenizer = privateInputDelegate.tokenizer;
}
if (tokenizer) {
// Move X
if (positiveX >= 1) {
UITextPosition *_position_old = _position;
_position = KH_tokenizerMovePositionWithGranularitInDirection(tokenizer, _position, granularity, textDirection);
// If I tried to move it and got nothing back reset it to what I had.
if (!_position){ _position = _position_old; }
// If I tried to move it a word at a time and nothing happened
if (granularity == UITextGranularityWord && (KH_positionsSame(privateInputDelegate, currentRange.start, _position) &&
!KH_positionsSame(privateInputDelegate, privateInputDelegate.beginningOfDocument, _position))) {
_position = KH_tokenizerMovePositionWithGranularitInDirection(tokenizer, _position, UITextGranularityCharacter, textDirection);
xMinimum = 4;
}
// Another sanity check
if (!_position || positiveX < xMinimum){
_position = _position_old;
}
}
// Move Y
// if (positiveY >= yMinimum) {
// UITextPosition *_position_old = _position;
//
// CGRect caretRect = [privateInputDelegate caretRectForPosition:_position];
//
// CGFloat yDiff = delta.y * 0.8;
//
// CGPoint newLinePoint = CGPointMake(caretRect.origin.x + (caretRect.size.width * 0.5), caretRect.origin.y + (caretRect.size.height * 0.5) + yDiff);
// newLinePoint = [[privateInputDelegate textInputView] convertPoint:newLinePoint toView:nil];
// _position = [privateInputDelegate closestPositionToPoint:newLinePoint];
//
// if (!_position){ _position = _position_old; }
// }
}
if (!extendRange && _position) {
[pivotPoint release], pivotPoint = nil;
pivotPoint = [_position retain];
}
// Get a new text range
UITextRange *textRange = startingtextRange = nil;
if ([privateInputDelegate respondsToSelector:@selector(textRangeFromPosition:toPosition:)]) {
textRange = [privateInputDelegate textRangeFromPosition:pivotPoint toPosition:_position];
}
CGPoint oldPrevious = previousPosition;
// Should I change X?
if (positiveX > xMinimum) { //|| positiveY > yMinimum) {
previousPosition = position;
}
isFirstShiftDown = NO;
if (textRange && (oldPrevious.x != previousPosition.x || oldPrevious.y != previousPosition.y)) {
[privateInputDelegate setSelectedTextRange:textRange];
}
}
}
%end
#pragma mark - GestureRecognizer implementation
@implementation KHPanGestureRecognizer
- (BOOL)canBePreventedByGestureRecognizer:(UIGestureRecognizer *)preventingGestureRecognizer{
if ([preventingGestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]]) {
return YES;
}
return NO;
}
- (BOOL)canPreventGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer{
UIKeyboardImpl *keyboardImpl = [%c(UIKeyboardImpl) sharedInstance];
BOOL longPress = NO;
if ([keyboardImpl respondsToSelector:@selector(isLongPress)]) {
longPress = [keyboardImpl isLongPress];
}
// Seperate from the if statement in case of change in later iOS version I can add a second check above.
if (longPress) {
return NO;
}
return [super canPreventGestureRecognizer:gestureRecognizer];
}
@end
//
// Code from : @iamharicc
//
// iAmharic <[email protected]>
//
static BOOL shiftByDelete;
static BOOL isLongPressed;
static BOOL isDeleteKey;
%hook UIKeyboardLayoutStar
/*==============touchesBegan================*/
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
NSString *key = [[[self keyHitTest:[touch locationInView:touch.view]] representedString] lowercaseString];
if ([key isEqualToString:@"delete"]) {
isDeleteKey = YES;
}
else {
isDeleteKey = NO;
}
%orig;
}
/*==============touchesMoved================*/
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
NSString *key = [[[self keyHitTest:[touch locationInView:touch.view]] representedString] lowercaseString];
if ([key isEqualToString:@"delete"]) {
shiftByDelete = YES;
}
%orig;
}
-(void)touchesCancelled:(id)arg1 withEvent:(id)arg2 {
%orig(arg1, arg2);
shiftByDelete = NO;
isLongPressed = NO;
}
/*==============touchesEnded================*/
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
isDeleteKey = NO;
UITouch *touch = [touches anyObject];
NSString *key = [[[self keyHitTest:[touch locationInView:touch.view]] representedString] lowercaseString];
if ([key isEqualToString:@"delete"]&& !isLongPressed) {
UIKeyboardImpl *kb = [UIKeyboardImpl activeInstance];
[kb handleDelete];
}
shiftByDelete = NO;
isLongPressed = NO;
%orig;
}
-(BOOL)isShiftKeyBeingHeld {
if(shiftByDelete) {
return YES;
}
return %orig;
}
%end
/*==============UIKeyboardImpl================*/
%hook UIKeyboardImpl
-(BOOL)isLongPress {
isLongPressed = %orig;
return isLongPressed;
}
-(void)handleDelete {
if (!isLongPressed && isDeleteKey) {
}
else {
%orig;
}
}
%end