5#import <UIKit/UIGestureRecognizerSubclass.h>
6#import <UIKit/UITextView.h>
8#include <QtGui/QGuiApplication>
9#include <QtGui/QInputMethod>
10#include <QtGui/QStyleHints>
12#include <QtGui/private/qinputmethod_p.h>
13#include <QtCore/private/qobject_p.h>
14#include <QtCore/private/qcore_mac_p.h>
31 return static_cast<QInputMethodPrivate *>(QObjectPrivate::get(QGuiApplication::inputMethod()))->platformInputContext();
36 if (!QGuiApplication::focusObject())
39 QInputMethodQueryEvent query(Qt::ImAnchorPosition | Qt::ImCursorPosition);
40 QGuiApplication::sendEvent(QGuiApplication::focusObject(), &query);
41 int anchorPos = query.value(Qt::ImAnchorPosition).toInt();
42 int cursorPos = query.value(Qt::ImCursorPosition).toInt();
43 return {anchorPos, cursorPos};
49 return selection.first != selection.second;
54 [CATransaction begin];
55 [CATransaction setValue:(id)kCFBooleanTrue forKey:kCATransactionDisableActions];
57 [CATransaction commit];
62
63
64
65@interface QIOSEditMenu : NSObject
66@property (nonatomic, assign) BOOL visible;
67@property (nonatomic, readonly) BOOL isHiding;
68@property (nonatomic, readonly) BOOL shownByUs;
69@property (nonatomic, assign) BOOL reshowAfterHidden;
72@implementation QIOSEditMenu
76 if (self = [super init]) {
77 NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
79 [center addObserverForName:UIMenuControllerWillHideMenuNotification
80 object:nil queue:nil usingBlock:^(NSNotification *) {
84 [center addObserverForName:UIMenuControllerDidHideMenuNotification
85 object:nil queue:nil usingBlock:^(NSNotification *) {
88 if (self.reshowAfterHidden) {
91 self.reshowAfterHidden = NO;
92 dispatch_async(dispatch_get_main_queue (), ^{ self.visible = YES; });
95 [center addObserverForName:UIKeyboardDidHideNotification object:nil queue:nil
96 usingBlock:^(NSNotification *) {
107 [[NSNotificationCenter defaultCenter] removeObserver:self name:nil object:nil];
113 return [UIMenuController sharedMenuController].menuVisible;
116- (
void)setVisible:(BOOL)visible
118 if (visible == self.visible)
128 QRectF cr = QPlatformInputContext::cursorRectangle();
129 QRectF ar = QPlatformInputContext::anchorRectangle();
131 CGRect targetRect = cr.united(ar).toCGRect();
132 UIView *focusView =
reinterpret_cast<UIView *>(qApp->focusWindow()->winId());
133 [[UIMenuController sharedMenuController] setTargetRect:targetRect inView:focusView];
134 [[UIMenuController sharedMenuController] setMenuVisible:YES animated:YES];
136 [[UIMenuController sharedMenuController] setMenuVisible:NO animated:YES];
142void showEditMenu(UIView *focusView, QPoint touchPos)
144 const Qt::KeyboardModifiers keyboardModifiers = Qt::NoModifier;
145 QWindow *qtWindow = quiview_cast(focusView).platformWindow->window();
146 const auto globalTouchPos = qtWindow->mapToGlobal(touchPos);
147 const bool contextMenuEventAccepted = QWindowSystemInterface::handleContextMenuEvent<
148 QWindowSystemInterface::SynchronousDelivery>(qtWindow,
149 QIOSIntegration::instance()->touchDevice(), touchPos,
150 globalTouchPos, keyboardModifiers);
152 if (!contextMenuEventAccepted) {
156 QIOSTextInputOverlay::s_editMenu.visible = YES;
162@interface QIOSLoupeLayer : CALayer
163@property (nonatomic, retain) UIView *targetView;
164@property (nonatomic, assign) CGPoint focalPoint;
165@property (nonatomic, assign) BOOL visible;
168@implementation QIOSLoupeLayer {
169 UIView *_snapshotView;
170 BOOL _pendingSnapshotUpdate;
171 UIView *_loupeImageView;
172 CALayer *_containerLayer;
173 CGFloat _loupeOffset;
177- (instancetype)initWithSize:(CGSize)size cornerRadius:(CGFloat)cornerRadius bottomOffset:(CGFloat)bottomOffset
179 if (self = [super init]) {
180 _loupeOffset = bottomOffset + (size.height / 2);
182 _pendingSnapshotUpdate = YES;
183 _updateTimer.setInterval(100);
184 _updateTimer.setSingleShot(
true);
185 QObject::connect(&_updateTimer, &QTimer::timeout, [self](){ [self updateSnapshot]; });
188 self.frame = CGRectMake(0, 0, size.width, size.height);
189 self.cornerRadius = cornerRadius;
190 self.shadowColor = [[UIColor grayColor] CGColor];
191 self.shadowOffset = CGSizeMake(0, 1);
192 self.shadowRadius = 2.0;
193 self.shadowOpacity = 0.75;
194 self.transform = CATransform3DMakeScale(0, 0, 0);
197 _containerLayer = [[CALayer
new] autorelease];
198 _containerLayer.frame = self.bounds;
199 _containerLayer.cornerRadius = cornerRadius;
200 _containerLayer.masksToBounds = YES;
201 [self addSublayer:_containerLayer];
204 const CGFloat inset = 30;
205 CALayer *topShadeLayer = [[CALayer
new] autorelease];
206 topShadeLayer.frame = CGRectOffset(CGRectInset(self.bounds, -inset, -inset), 0, inset / 2);
207 topShadeLayer.borderWidth = inset / 2;
208 topShadeLayer.cornerRadius = cornerRadius;
209 topShadeLayer.borderColor = [[UIColor blackColor] CGColor];
210 topShadeLayer.shadowColor = [[UIColor blackColor] CGColor];
211 topShadeLayer.shadowOffset = CGSizeMake(0, 0);
212 topShadeLayer.shadowRadius = 15.0;
213 topShadeLayer.shadowOpacity = 0.6;
215 CALayer *mask = [[CALayer
new] autorelease];
216 mask.frame = CGRectOffset(self.bounds, inset, inset / 2);
217 mask.backgroundColor = [[UIColor blackColor] CGColor];
218 mask.cornerRadius = cornerRadius;
219 topShadeLayer.mask = mask;
220 [self addSublayer:topShadeLayer];
225 CALayer *borderLayer = [[CALayer
new] autorelease];
226 borderLayer.frame = self.bounds;
227 borderLayer.borderWidth = 0.75;
228 borderLayer.cornerRadius = cornerRadius;
229 borderLayer.borderColor = [[UIColor lightGrayColor] CGColor];
230 [self addSublayer:borderLayer];
242- (
void)setVisible:(BOOL)visible
244 if (_visible == visible)
249 dispatch_async(dispatch_get_main_queue (), ^{
252 self.transform = _visible ? CATransform3DMakeScale(1, 1, 1) : CATransform3DMakeScale(0.0, 0.0, 1);
256- (
void)updateSnapshot
258 _pendingSnapshotUpdate = YES;
259 [self setNeedsDisplay];
262- (
void)setFocalPoint:(CGPoint)point
265 [self updateSnapshot];
269 _updateTimer.start();
278 executeBlockWithoutAnimation(^{
279 if (_pendingSnapshotUpdate) {
280 UIView *newSnapshot = [_targetView snapshotViewAfterScreenUpdates:NO];
281 [_snapshotView.layer removeFromSuperlayer];
282 [_snapshotView release];
283 _snapshotView = [newSnapshot retain];
284 [_containerLayer addSublayer:_snapshotView.layer];
285 _pendingSnapshotUpdate = NO;
288 self.position = CGPointMake(_focalPoint.x, _focalPoint.y - _loupeOffset);
290 const CGFloat loupeScale = 1.5;
291 CGFloat x = -(_focalPoint.x * loupeScale) + self.frame.size.width / 2;
292 CGFloat y = -(_focalPoint.y * loupeScale) + self.frame.size.height / 2;
293 CGFloat w = _targetView.frame.size.width * loupeScale;
294 CGFloat h = _targetView.frame.size.height * loupeScale;
295 _snapshotView.layer.frame = CGRectMake(x, y, w, h);
303@interface QIOSHandleLayer : CALayer <CAAnimationDelegate>
304@property (nonatomic, assign) CGRect cursorRectangle;
305@property (nonatomic, assign) CGFloat handleScale;
306@property (nonatomic, assign) BOOL visible;
307@property (nonatomic, copy) Block onAnimationDidStop;
310@implementation QIOSHandleLayer {
311 CALayer *_handleCursorLayer;
312 CALayer *_handleKnobLayer;
313 Qt::Edge _selectionEdge;
318- (instancetype)initWithKnobAtEdge:(Qt::Edge)selectionEdge
320 if (self = [super init]) {
321 CGColorRef bgColor = [UIColor colorWithRed:0.1 green:0.4 blue:0.9 alpha:1].CGColor;
322 _selectionEdge = selectionEdge;
323 self.handleScale = 0;
325 _handleCursorLayer = [[CALayer
new] autorelease];
326 _handleCursorLayer.masksToBounds = YES;
327 _handleCursorLayer.backgroundColor = bgColor;
328 [self addSublayer:_handleCursorLayer];
330 _handleKnobLayer = [[CALayer
new] autorelease];
331 _handleKnobLayer.masksToBounds = YES;
332 _handleKnobLayer.backgroundColor = bgColor;
333 _handleKnobLayer.cornerRadius = kKnobWidth / 2;
334 [self addSublayer:_handleKnobLayer];
339+ (BOOL)needsDisplayForKey:(NSString *)key
341 if ([key isEqualToString:@
"handleScale"])
343 return [super needsDisplayForKey:key];
346- (id<CAAction>)actionForKey:(NSString *)key
348 if ([key isEqualToString:@
"handleScale"]) {
351 CAKeyframeAnimation * animation = [CAKeyframeAnimation animationWithKeyPath:key];
352 [animation setDuration:0.5];
353 animation.values = @[@(0.0f), @(1.3f), @(1.3f), @(1.0f)];
354 animation.keyTimes = @[@(0.0f), @(0.3f), @(0.9f), @(1.0f)];
357 CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:key];
358 [animation setDelegate:self];
359 animation.fromValue = [self valueForKey:key];
360 [animation setDuration:0.2];
364 return [super actionForKey:key];
367- (
void)animationDidStop:(CAAnimation *)animation finished:(BOOL)flag
371 if (self.onAnimationDidStop)
372 self.onAnimationDidStop();
375- (
void)setVisible:(BOOL)visible
377 if (visible == _visible)
382 self.handleScale = visible ? 1 : 0;
385- (
void)setCursorRectangle:(CGRect)cursorRect
387 if (CGRectEqualToRect(_cursorRectangle, cursorRect))
390 _cursorRectangle = cursorRect;
392 executeBlockWithoutAnimation(^{
393 [self setNeedsDisplay];
394 [self displayIfNeeded];
400 CGFloat cursorWidth = 2;
401 CGPoint origin = _cursorRectangle.origin;
402 CGSize size = _cursorRectangle.size;
403 CGFloat scale = ((QIOSHandleLayer *)[self presentationLayer]).handleScale;
404 CGFloat edgeAdjustment = (_selectionEdge == Qt::LeftEdge) ? 0.5 - cursorWidth : -0.5;
406 CGFloat cursorX = origin.x + (size.width / 2) + edgeAdjustment;
407 CGFloat cursorY = origin.y;
408 CGFloat knobX = cursorX - (kKnobWidth - cursorWidth) / 2;
409 CGFloat knobY = origin.y + ((_selectionEdge == Qt::LeftEdge) ? -kKnobWidth : size.height);
411 _handleCursorLayer.frame = CGRectMake(cursorX, cursorY, cursorWidth, size.height);
412 _handleKnobLayer.frame = CGRectMake(knobX, knobY, kKnobWidth, kKnobWidth);
413 _handleCursorLayer.transform = CATransform3DMakeScale(1, scale, scale);
414 _handleKnobLayer.transform = CATransform3DMakeScale(scale, scale, scale);
422
423
424
425
426@interface QIOSLoupeRecognizer : UIGestureRecognizer <UIGestureRecognizerDelegate>
427@property (nonatomic, assign) QPointF focalPoint;
428@property (nonatomic, assign) BOOL dragTriggersGesture;
429@property (nonatomic, readonly) UIView *focusView;
432@implementation QIOSLoupeRecognizer {
433 QIOSLoupeLayer *_loupeLayer;
434 UIView *_desktopView;
435 CGPoint _firstTouchPoint;
436 CGPoint _lastTouchPoint;
437 QTimer _triggerStateBeganTimer;
438 int _originalCursorFlashTime;
443 if (self = [super initWithTarget:self action:@selector(gestureStateChanged)]) {
445 _triggerStateBeganTimer.setInterval(QGuiApplication::styleHints()->startDragTime());
446 _triggerStateBeganTimer.setSingleShot(
true);
447 QObject::connect(&_triggerStateBeganTimer, &QTimer::timeout, [=](){
448 self.state = UIGestureRecognizerStateBegan;
455- (
void)setEnabled:(BOOL)enabled
457 if (enabled == self.enabled)
460 [super setEnabled:enabled];
463 _focusView = [
reinterpret_cast<UIView *>(qApp->focusWindow()->winId()) retain];
464 _desktopView = [presentationWindow(
nullptr).rootViewController.view retain];
465 Q_ASSERT(_focusView && _desktopView && _desktopView.superview);
466 [_desktopView addGestureRecognizer:self];
468 [_desktopView removeGestureRecognizer:self];
469 [_desktopView release];
471 [_focusView release];
473 _triggerStateBeganTimer.stop();
475 [_loupeLayer removeFromSuperlayer];
476 [_loupeLayer release];
482- (
void)gestureStateChanged
484 switch (self.state) {
485 case UIGestureRecognizerStateBegan:
487 _originalCursorFlashTime = QGuiApplication::styleHints()->cursorFlashTime();
488 QGuiApplication::styleHints()->setCursorFlashTime(0);
491 [self updateFocalPoint:QPointF::fromCGPoint(_lastTouchPoint)];
492 _loupeLayer.visible = YES;
493 QIOSTextInputOverlay::s_editMenu.visible = NO;
495 case UIGestureRecognizerStateChanged:
497 [self updateFocalPoint:QPointF::fromCGPoint(_lastTouchPoint)];
499 case UIGestureRecognizerStateEnded: {
501 QGuiApplication::styleHints()->setCursorFlashTime(_originalCursorFlashTime);
502 const QPoint touchPos = QPointF::fromCGPoint(_lastTouchPoint).toPoint();
503 showEditMenu(_focusView, touchPos);
504 _loupeLayer.visible = NO;
508 _loupeLayer.visible = NO;
517 _loupeLayer = [[self createLoupeLayer] retain];
518 _loupeLayer.targetView = _desktopView;
519 [_desktopView.superview.layer addSublayer:_loupeLayer];
524 return QPointF::fromCGPoint([_loupeLayer.targetView convertPoint:_loupeLayer.focalPoint toView:_focusView]);
527- (
void)setFocalPoint:(QPointF)point
529 _loupeLayer.focalPoint = [_loupeLayer.targetView convertPoint:point.toCGPoint() fromView:_focusView];
532- (
void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
534 [super touchesBegan:touches withEvent:event];
535 if ([event allTouches].count > 1) {
537 self.state = UIGestureRecognizerStateFailed;
541 _firstTouchPoint = [
static_cast<UITouch *>([touches anyObject]) locationInView:_focusView];
542 _lastTouchPoint = _firstTouchPoint;
546 if ([self acceptTouchesBegan:QPointF::fromCGPoint(_firstTouchPoint)])
547 _triggerStateBeganTimer.start();
549 self.state = UIGestureRecognizerStateFailed;
552- (
void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
554 [super touchesMoved:touches withEvent:event];
555 _lastTouchPoint = [
static_cast<UITouch *>([touches anyObject]) locationInView:_focusView];
557 if (self.state == UIGestureRecognizerStatePossible) {
561 int startDragDistance = QGuiApplication::styleHints()->startDragDistance();
562 int dragDistance = hypot(_firstTouchPoint.x - _lastTouchPoint.x, _firstTouchPoint.y - _lastTouchPoint.y);
563 if (dragDistance > startDragDistance) {
564 _triggerStateBeganTimer.stop();
565 self.state = self.dragTriggersGesture ? UIGestureRecognizerStateBegan : UIGestureRecognizerStateFailed;
568 self.state = UIGestureRecognizerStateChanged;
572- (
void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
574 [super touchesEnded:touches withEvent:event];
575 _triggerStateBeganTimer.stop();
576 _lastTouchPoint = [
static_cast<UITouch *>([touches anyObject]) locationInView:_focusView];
577 self.state = self.state == UIGestureRecognizerStatePossible ? UIGestureRecognizerStateFailed : UIGestureRecognizerStateEnded;
580- (
void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
582 [super touchesCancelled:touches withEvent:event];
583 _triggerStateBeganTimer.stop();
584 _lastTouchPoint = [
static_cast<UITouch *>([touches anyObject]) locationInView:_focusView];
585 self.state = UIGestureRecognizerStateCancelled;
590- (BOOL)acceptTouchesBegan:(QPointF)touchPoint
592 Q_UNUSED(touchPoint);
597- (QIOSLoupeLayer *)createLoupeLayer
603- (
void)updateFocalPoint:(QPointF)touchPoint
605 Q_UNUSED(touchPoint);
614
615
616
617
618@interface QIOSCursorRecognizer : QIOSLoupeRecognizer
621@implementation QIOSCursorRecognizer
623- (QIOSLoupeLayer *)createLoupeLayer
625 return [[[QIOSLoupeLayer alloc] initWithSize:CGSizeMake(120, 120) cornerRadius:60 bottomOffset:4] autorelease];
628- (BOOL)acceptTouchesBegan:(QPointF)touchPoint
630 QRectF inputRect = QPlatformInputContext::inputItemRectangle();
631 return !hasSelection() && inputRect.contains(touchPoint);
634- (
void)updateFocalPoint:(QPointF)touchPoint
636 self.focalPoint = touchPoint;
638 const int currentCursorPos = QInputMethod::queryFocusObject(Qt::ImCursorPosition, QVariant()).toInt();
639 const int newCursorPos = QPlatformInputContext::queryFocusObject(Qt::ImCursorPosition, touchPoint).toInt();
640 if (newCursorPos != currentCursorPos)
641 QPlatformInputContext::setSelectionOnFocusObject(touchPoint, touchPoint);
649
650
651
652
653@interface QIOSSelectionRecognizer : QIOSLoupeRecognizer
656@implementation QIOSSelectionRecognizer {
657 CALayer *_clipRectLayer;
658 QIOSHandleLayer *_cursorLayer;
659 QIOSHandleLayer *_anchorLayer;
660 QPointF _touchOffset;
664 QTimer _updateSelectionTimer;
665 QMetaObject::Connection _cursorConnection;
666 QMetaObject::Connection _anchorConnection;
667 QMetaObject::Connection _clipRectConnection;
672 if (self = [super init]) {
673 self.delaysTouchesBegan = YES;
674 self.dragTriggersGesture = YES;
675 _multiLine = QInputMethod::queryFocusObject(Qt::ImHints, QVariant()).toUInt() & Qt::ImhMultiLine;
676 _updateSelectionTimer.setInterval(1);
677 _updateSelectionTimer.setSingleShot(
true);
678 QObject::connect(&_updateSelectionTimer, &QTimer::timeout, [self](){ [self updateSelection]; });
684- (
void)setEnabled:(BOOL)enabled
686 if (enabled == self.enabled)
689 [super setEnabled:enabled];
693 _clipRectLayer = [CALayer
new];
694 _clipRectLayer.masksToBounds = YES;
695 [self.focusView.layer addSublayer:_clipRectLayer];
698 _cursorLayer = [[[QIOSHandleLayer alloc] initWithKnobAtEdge:Qt::RightEdge] autorelease];
699 _anchorLayer = [[[QIOSHandleLayer alloc] initWithKnobAtEdge:Qt::LeftEdge] autorelease];
700 bool selection = hasSelection();
701 _cursorLayer.visible = selection;
702 _anchorLayer.visible = selection;
703 [_clipRectLayer addSublayer:_cursorLayer];
704 [_clipRectLayer addSublayer:_anchorLayer];
711 QInputMethod *im = QGuiApplication::inputMethod();
712 void(QTimer::*start)(
void) = &QTimer::start;
713 _cursorConnection = QObject::connect(im, &QInputMethod::cursorRectangleChanged, &_updateSelectionTimer, start);
714 _anchorConnection = QObject::connect(im, &QInputMethod::anchorRectangleChanged, &_updateSelectionTimer, start);
715 _clipRectConnection = QObject::connect(im, &QInputMethod::inputItemClipRectangleChanged, &_updateSelectionTimer, start);
717 [self updateSelection];
725 __block CALayer *clipRectLayer = _clipRectLayer;
726 __block
int handleCount = 2;
728 if (--handleCount == 0) {
729 [clipRectLayer removeFromSuperlayer];
730 [clipRectLayer release];
734 _cursorLayer.onAnimationDidStop = block;
735 _anchorLayer.onAnimationDidStop = block;
736 _cursorLayer.visible = NO;
737 _anchorLayer.visible = NO;
742 _updateSelectionTimer.stop();
744 QObject::disconnect(_cursorConnection);
745 QObject::disconnect(_anchorConnection);
746 QObject::disconnect(_clipRectConnection);
748 if (QIOSTextInputOverlay::s_editMenu.shownByUs)
749 QIOSTextInputOverlay::s_editMenu.visible = NO;
753- (QIOSLoupeLayer *)createLoupeLayer
755 CGSize loupeSize = CGSizeMake(123, 33);
756 CGSize arrowSize = CGSizeMake(25, 12);
757 CGFloat loupeOffset = arrowSize.height + 20;
760 QIOSLoupeLayer *loupeLayer = [[[QIOSLoupeLayer alloc] initWithSize:loupeSize cornerRadius:5 bottomOffset:loupeOffset] autorelease];
761 CAShapeLayer *arrowLayer = [[[CAShapeLayer alloc] init] autorelease];
764 UIBezierPath *path = [[UIBezierPath
new] autorelease];
765 [path moveToPoint:CGPointMake(0, 0)];
766 [path addLineToPoint:CGPointMake(arrowSize.width / 2, arrowSize.height)];
767 [path addLineToPoint:CGPointMake(arrowSize.width, 0)];
769 arrowLayer.frame = CGRectMake((loupeSize.width - arrowSize.width) / 2, loupeSize.height - 1, arrowSize.width, arrowSize.height);
770 arrowLayer.path = path.CGPath;
771 arrowLayer.backgroundColor = [[UIColor whiteColor] CGColor];
772 arrowLayer.strokeColor = [[UIColor lightGrayColor] CGColor];
773 arrowLayer.lineWidth = 0.75 * 2;
774 arrowLayer.fillColor = nil;
776 CAShapeLayer *mask = [[CAShapeLayer
new] autorelease];
777 mask.frame = arrowLayer.bounds;
778 mask.path = path.CGPath;
779 arrowLayer.mask = mask;
781 [loupeLayer addSublayer:arrowLayer];
786- (BOOL)acceptTouchesBegan:(QPointF)touchPoint
792 const int handleRadius = 50;
793 QPointF cursorCenter = QPlatformInputContext::cursorRectangle().center();
794 QPointF anchorCenter = QPlatformInputContext::anchorRectangle().center();
795 QPointF cursorOffset = QPointF(cursorCenter.x() - touchPoint.x(), cursorCenter.y() - touchPoint.y());
796 QPointF anchorOffset = QPointF(anchorCenter.x() - touchPoint.x(), anchorCenter.y() - touchPoint.y());
797 double cursorDist = hypot(cursorOffset.x(), cursorOffset.y());
798 double anchorDist = hypot(anchorOffset.x(), anchorOffset.y());
800 if (cursorDist > handleRadius && anchorDist > handleRadius)
803 if (cursorDist < anchorDist) {
804 _touchOffset = cursorOffset;
808 _touchOffset = anchorOffset;
816- (
void)updateFocalPoint:(QPointF)touchPoint
818 touchPoint += _touchOffset;
821 SelectionPair selection = querySelection();
822 int touchTextPos = QPlatformInputContext::queryFocusObject(Qt::ImCursorPosition, touchPoint).toInt();
826 selection.second = (touchTextPos > selection.first) ? touchTextPos : selection.first + 1;
828 selection.first = (touchTextPos < selection.second) ? touchTextPos : selection.second - 1;
831 QList<QInputMethodEvent::Attribute> imAttributes;
832 imAttributes.append(QInputMethodEvent::Attribute(
833 QInputMethodEvent::Selection, selection.first, selection.second - selection.first, QVariant()));
834 QInputMethodEvent event(QString(), imAttributes);
835 QGuiApplication::sendEvent(qApp->focusObject(), &event);
838 QRectF handleRect = _dragOnCursor ?
839 QPlatformInputContext::cursorRectangle() :
840 QPlatformInputContext::anchorRectangle();
841 self.focalPoint = QPointF(touchPoint.x(), handleRect.center().y());
844- (
void)updateSelection
846 if (!hasSelection()) {
847 if (_cursorLayer.visible) {
848 _cursorLayer.visible = NO;
849 _anchorLayer.visible = NO;
851 if (QIOSTextInputOverlay::s_editMenu.shownByUs)
852 QIOSTextInputOverlay::s_editMenu.visible = NO;
856 if (!_cursorLayer.visible && QIOSTextInputOverlay::s_editMenu.isHiding) {
861 QIOSTextInputOverlay::s_editMenu.reshowAfterHidden = YES;
865 QRectF inputRect = QPlatformInputContext::inputItemClipRectangle();
866 CGRect cursorRect = QPlatformInputContext::cursorRectangle().toCGRect();
867 CGRect anchorRect = QPlatformInputContext::anchorRectangle().toCGRect();
872 int margin = kKnobWidth + 5;
873 inputRect.adjust(-margin / 2, -margin, margin / 2, margin);
876 executeBlockWithoutAnimation(^{ _clipRectLayer.frame = inputRect.toCGRect(); });
877 _cursorLayer.cursorRectangle = [self.focusView.layer convertRect:cursorRect toLayer:_clipRectLayer];
878 _anchorLayer.cursorRectangle = [self.focusView.layer convertRect:anchorRect toLayer:_clipRectLayer];
879 _cursorLayer.visible = YES;
880 _anchorLayer.visible = YES;
888
889
890
891
892@interface QIOSTapRecognizer : UITapGestureRecognizer
895@implementation QIOSTapRecognizer {
896 int _cursorPosOnPress;
897 bool _menuShouldBeVisible;
903 if (self = [super initWithTarget:self action:@selector(gestureStateChanged)]) {
910- (
void)setEnabled:(BOOL)enabled
912 if (enabled == self.enabled)
915 [super setEnabled:enabled];
918 _focusView = [
reinterpret_cast<UIView *>(qApp->focusWindow()->winId()) retain];
919 [_focusView addGestureRecognizer:self];
921 [_focusView removeGestureRecognizer:self];
922 [_focusView release];
927- (
void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
929 [super touchesBegan:touches withEvent:event];
931 QRectF inputRect = QPlatformInputContext::inputItemClipRectangle();
932 QPointF touchPos = QPointF::fromCGPoint([
static_cast<UITouch *>([touches anyObject]) locationInView:_focusView]);
933 const bool touchInsideInputArea = inputRect.contains(touchPos);
935 if (touchInsideInputArea && hasSelection()) {
941 self.state = UIGestureRecognizerStateFailed;
945 if (QIOSTextInputOverlay::s_editMenu.visible) {
957 if (!touchInsideInputArea) {
960 self.state = UIGestureRecognizerStateFailed;
967 _cursorPosOnPress = QInputMethod::queryFocusObject(Qt::ImCursorPosition, QVariant()).toInt();
970- (
void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
972 if (QIOSTextInputOverlay::s_editMenu.visible) {
973 _menuShouldBeVisible =
false;
975 QPointF touchPos = QPointF::fromCGPoint([
static_cast<UITouch *>([touches anyObject]) locationInView:_focusView]);
976 int cursorPosOnRelease = QPlatformInputContext::queryFocusObject(Qt::ImCursorPosition, touchPos).toInt();
978 if (cursorPosOnRelease == _cursorPosOnPress) {
985 _menuShouldBeVisible =
true;
986 self.state = UIGestureRecognizerStateFailed;
987 dispatch_async(dispatch_get_main_queue(), ^{
988 if (_menuShouldBeVisible)
989 showEditMenu(_focusView, touchPos.toPoint());
991 QIOSTextInputOverlay::s_editMenu.visible =
false;
997 self.state = UIGestureRecognizerStateFailed;
1001 [super touchesEnded:touches withEvent:event];
1004- (
void)gestureStateChanged
1006 if (self.state != UIGestureRecognizerStateEnded)
1009 QIOSTextInputOverlay::s_editMenu.visible = _menuShouldBeVisible;
1018QIOSEditMenu *QIOSTextInputOverlay::s_editMenu =
nullptr;
1020QIOSTextInputOverlay::QIOSTextInputOverlay()
1021 : m_cursorRecognizer(
nullptr)
1022 , m_selectionRecognizer(
nullptr)
1023 , m_openMenuOnTapRecognizer(
nullptr)
1025 if (qt_apple_isApplicationExtension()) {
1026 qWarning() <<
"text input overlays disabled in application extensions";
1030 connect(qApp, &QGuiApplication::focusObjectChanged,
this, &QIOSTextInputOverlay::updateFocusObject);
1033QIOSTextInputOverlay::~QIOSTextInputOverlay()
1036 disconnect(qApp, 0,
this, 0);
1039void QIOSTextInputOverlay::updateFocusObject()
1043 if (m_cursorRecognizer) {
1044 m_cursorRecognizer.enabled = NO;
1045 [m_cursorRecognizer release];
1046 m_cursorRecognizer =
nullptr;
1048 if (m_selectionRecognizer) {
1049 m_selectionRecognizer.enabled = NO;
1050 [m_selectionRecognizer release];
1051 m_selectionRecognizer =
nullptr;
1053 if (m_openMenuOnTapRecognizer) {
1054 m_openMenuOnTapRecognizer.enabled = NO;
1055 [m_openMenuOnTapRecognizer release];
1056 m_openMenuOnTapRecognizer =
nullptr;
1060 [s_editMenu release];
1061 s_editMenu =
nullptr;
1064 const QVariant hintsVariant = QGuiApplication::inputMethod()->queryFocusObject(Qt::ImHints, QVariant());
1065 const Qt::InputMethodHints hints = Qt::InputMethodHints(hintsVariant.toUInt());
1066 if (hints & Qt::ImhNoTextHandles)
1081 const bool inputAccepted = platformInputContext()->inputMethodAccepted();
1082 const bool readOnly = QGuiApplication::inputMethod()->queryFocusObject(Qt::ImReadOnly, QVariant()).toBool();
1084 if (inputAccepted || readOnly) {
1085 if (!(hints & Qt::ImhNoEditMenu))
1086 s_editMenu = [QIOSEditMenu
new];
1087 m_selectionRecognizer = [QIOSSelectionRecognizer
new];
1088 m_openMenuOnTapRecognizer = [QIOSTapRecognizer
new];
1089 m_selectionRecognizer.enabled = YES;
1090 m_openMenuOnTapRecognizer.enabled = YES;
1093 if (inputAccepted) {
1094 m_cursorRecognizer = [QIOSCursorRecognizer
new];
1095 m_cursorRecognizer.enabled = YES;
static SelectionPair querySelection()
static const CGFloat kKnobWidth
static bool hasSelection()
std::pair< int, int > SelectionPair
static void executeBlockWithoutAnimation(Block block)
static QPlatformInputContext * platformInputContext()