8 #include <Carbon/Carbon.h>
9 #import <objc/message.h>
11 #include "flutter/common/constants.h"
12 #include "flutter/fml/platform/darwin/cf_utils.h"
22 #include "flutter/shell/platform/embedder/embedder.h"
24 #pragma mark - Static types and data.
30 static constexpr int32_t kMousePointerDeviceId = 0;
31 static constexpr int32_t kPointerPanZoomDeviceId = 1;
36 static constexpr
double kTrackpadTouchInertiaCancelWindowMs = 0.050;
69 bool flutter_state_is_added =
false;
74 bool flutter_state_is_down =
false;
83 bool has_pending_exit =
false;
88 bool flutter_state_is_pan_zoom_started =
false;
93 NSEventPhase pan_gesture_phase = NSEventPhaseNone;
98 NSEventPhase scale_gesture_phase = NSEventPhaseNone;
103 NSEventPhase rotate_gesture_phase = NSEventPhaseNone;
108 NSTimeInterval last_scroll_momentum_changed_time = 0;
113 void GestureReset() {
118 flutter_state_is_pan_zoom_started =
false;
119 pan_gesture_phase = NSEventPhaseNone;
120 scale_gesture_phase = NSEventPhaseNone;
121 rotate_gesture_phase = NSEventPhaseNone;
128 flutter_state_is_added =
false;
129 flutter_state_is_down =
false;
130 has_pending_exit =
false;
137 #pragma mark - Private interface declaration.
154 - (void)setBackgroundColor:(NSColor*)color;
166 @property(nonatomic) NSTrackingArea* trackingArea;
171 @property(nonatomic) MouseState mouseState;
176 @property(nonatomic)
id keyUpMonitor;
181 - (BOOL)launchEngine;
187 - (void)configureTrackingArea;
194 - (void)dispatchMouseEvent:(nonnull NSEvent*)event;
199 - (void)dispatchGestureEvent:(nonnull NSEvent*)event;
204 - (void)dispatchMouseEvent:(nonnull NSEvent*)event phase:(FlutterPointerPhase)phase;
208 #pragma mark - FlutterViewWrapper implementation.
215 - (instancetype)initWithFlutterView:(
FlutterView*)view
217 self = [
super initWithFrame:NSZeroRect];
221 view.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable;
222 [
self addSubview:view];
227 - (void)setBackgroundColor:(NSColor*)color {
228 [_flutterView setBackgroundColor:color];
231 - (BOOL)performKeyEquivalent:(NSEvent*)event {
238 if (
self.window.firstResponder != _flutterView || [
_controller isDispatchingKeyEvent:event]) {
239 return [
super performKeyEquivalent:event];
241 [_flutterView keyDown:event];
245 - (NSArray*)accessibilityChildren {
246 return @[ _flutterView ];
251 - (void)mouseDown:(NSEvent*)event {
252 if (@available(macOS 13.3.1, *)) {
253 [
super mouseDown:event];
265 [
self.nextResponder mouseDown:event];
271 - (void)mouseUp:(NSEvent*)event {
272 if (@available(macOS 13.3.1, *)) {
273 [
super mouseUp:event];
285 [
self.nextResponder mouseUp:event];
291 #pragma mark - FlutterViewController implementation.
297 std::shared_ptr<flutter::AccessibilityBridgeMac>
_bridge;
305 @synthesize viewIdentifier = _viewIdentifier;
307 @dynamic accessibilityBridge;
315 project:controller->_project
316 allowHeadlessExecution:NO];
318 NSCAssert(controller.
engine == nil,
319 @"The FlutterViewController is unexpectedly attached to "
320 @"engine %@ before initialization.",
322 [engine addViewController:controller];
323 NSCAssert(controller.
engine != nil,
324 @"The FlutterViewController unexpectedly stays unattached after initialization. "
325 @"In unit tests, this is likely because either the FlutterViewController or "
326 @"the FlutterEngine is mocked. Please subclass these classes instead.",
328 controller->_mouseTrackingMode = kFlutterMouseTrackingModeInKeyWindow;
330 [controller notifySemanticsEnabledChanged];
333 - (instancetype)initWithCoder:(NSCoder*)coder {
334 self = [
super initWithCoder:coder];
335 NSAssert(
self,
@"Super init cannot be nil");
337 CommonInit(
self, nil);
341 - (instancetype)initWithNibName:(NSString*)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil {
342 self = [
super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
343 NSAssert(
self,
@"Super init cannot be nil");
345 CommonInit(
self, nil);
350 self = [
super initWithNibName:nil bundle:nil];
351 NSAssert(
self,
@"Super init cannot be nil");
354 CommonInit(
self, nil);
359 nibName:(nullable NSString*)nibName
360 bundle:(nullable NSBundle*)nibBundle {
361 NSAssert(engine != nil,
@"Engine is required");
363 self = [
super initWithNibName:nibName bundle:nibBundle];
365 CommonInit(
self, engine);
371 - (BOOL)isDispatchingKeyEvent:(NSEvent*)event {
372 return [_engine.keyboardManager isDispatchingKeyEvent:event];
377 id<MTLDevice> device = _engine.renderer.device;
378 id<MTLCommandQueue> commandQueue = _engine.renderer.commandQueue;
379 if (!device || !commandQueue) {
380 NSLog(
@"Unable to create FlutterView; no MTLDevice or MTLCommandQueue available.");
383 flutterView = [
self createFlutterViewWithMTLDevice:device commandQueue:commandQueue];
384 if (_backgroundColor != nil) {
389 self.view = wrapperView;
390 _flutterView = flutterView;
393 - (void)viewDidLoad {
394 [
self configureTrackingArea];
395 [
self.view setAllowedTouchTypes:NSTouchTypeMaskIndirect];
396 [
self.view setWantsRestingTouches:YES];
397 [_engine viewControllerViewDidLoad:self];
400 - (void)viewWillAppear {
401 [
super viewWillAppear];
402 if (!_engine.running) {
405 [
self listenForMetaModifiedKeyUpEvents];
408 - (void)viewWillDisappear {
411 [NSEvent removeMonitor:_keyUpMonitor];
416 if ([
self attached]) {
417 [_engine removeViewController:self];
421 #pragma mark - Public methods
423 - (void)setMouseTrackingMode:(FlutterMouseTrackingMode)mode {
424 if (_mouseTrackingMode == mode) {
427 _mouseTrackingMode = mode;
428 [
self configureTrackingArea];
431 - (void)setBackgroundColor:(NSColor*)color {
432 _backgroundColor = color;
433 [_flutterView setBackgroundColor:_backgroundColor];
437 NSAssert([
self attached],
@"This view controller is not attached.");
438 return _viewIdentifier;
441 - (void)onPreEngineRestart {
444 - (void)notifySemanticsEnabledChanged {
445 BOOL mySemanticsEnabled = !!
_bridge;
446 BOOL newSemanticsEnabled = _engine.semanticsEnabled;
447 if (newSemanticsEnabled == mySemanticsEnabled) {
450 if (newSemanticsEnabled) {
451 _bridge = [
self createAccessibilityBridgeWithEngine:_engine];
454 _flutterView.accessibilityChildren = nil;
457 NSAssert(newSemanticsEnabled == !!
_bridge,
@"Failed to update semantics for the view.");
460 - (std::weak_ptr<flutter::AccessibilityBridgeMac>)accessibilityBridge {
467 NSAssert(_engine == nil,
@"Already attached to an engine %@.", _engine);
469 _viewIdentifier = viewIdentifier;
471 [_threadSynchronizer registerView:_viewIdentifier];
474 - (void)detachFromEngine {
475 NSAssert(_engine != nil,
@"Not attached to any engine.");
476 [_threadSynchronizer deregisterView:_viewIdentifier];
482 return _engine != nil;
485 - (void)updateSemantics:(const FlutterSemanticsUpdate2*)update {
488 if (!_engine.semanticsEnabled) {
491 for (
size_t i = 0; i < update->node_count; i++) {
492 const FlutterSemanticsNode2* node = update->nodes[i];
493 _bridge->AddFlutterSemanticsNodeUpdate(*node);
496 for (
size_t i = 0; i < update->custom_action_count; i++) {
497 const FlutterSemanticsCustomAction2* action = update->custom_actions[i];
498 _bridge->AddFlutterSemanticsCustomActionUpdate(*action);
504 if (!
self.viewLoaded) {
508 auto root =
_bridge->GetFlutterPlatformNodeDelegateFromID(0).lock();
510 if ([
self.flutterView.accessibilityChildren count] == 0) {
511 NSAccessibilityElement* native_root = root->GetNativeViewAccessible();
512 self.flutterView.accessibilityChildren = @[ native_root ];
515 self.flutterView.accessibilityChildren = nil;
519 #pragma mark - Private methods
521 - (BOOL)launchEngine {
522 if (![_engine runWithEntrypoint:nil]) {
532 - (void)listenForMetaModifiedKeyUpEvents {
533 if (_keyUpMonitor != nil) {
539 _keyUpMonitor = [NSEvent
540 addLocalMonitorForEventsMatchingMask:NSEventMaskKeyUp
541 handler:^NSEvent*(NSEvent* event) {
544 NSResponder* firstResponder = [[event window] firstResponder];
545 if (weakSelf.viewLoaded && weakSelf.flutterView &&
546 (firstResponder == weakSelf.flutterView ||
547 firstResponder == weakSelf.textInputPlugin) &&
548 ([event modifierFlags] & NSEventModifierFlagCommand) &&
549 ([event type] == NSEventTypeKeyUp)) {
550 [weakSelf keyUp:event];
556 - (void)configureTrackingArea {
557 if (!
self.viewLoaded) {
562 if (_mouseTrackingMode != kFlutterMouseTrackingModeNone &&
self.flutterView) {
563 NSTrackingAreaOptions options = NSTrackingMouseEnteredAndExited | NSTrackingMouseMoved |
564 NSTrackingInVisibleRect | NSTrackingEnabledDuringMouseDrag;
565 switch (_mouseTrackingMode) {
566 case kFlutterMouseTrackingModeInKeyWindow:
567 options |= NSTrackingActiveInKeyWindow;
569 case kFlutterMouseTrackingModeInActiveApp:
570 options |= NSTrackingActiveInActiveApp;
572 case kFlutterMouseTrackingModeAlways:
573 options |= NSTrackingActiveAlways;
576 NSLog(
@"Error: Unrecognized mouse tracking mode: %ld", _mouseTrackingMode);
579 _trackingArea = [[NSTrackingArea alloc] initWithRect:NSZeroRect
583 [
self.flutterView addTrackingArea:_trackingArea];
584 }
else if (_trackingArea) {
585 [
self.flutterView removeTrackingArea:_trackingArea];
590 - (void)dispatchMouseEvent:(nonnull NSEvent*)event {
591 FlutterPointerPhase phase = _mouseState.buttons == 0
592 ? (_mouseState.flutter_state_is_down ? kUp : kHover)
593 : (_mouseState.flutter_state_is_down ? kMove : kDown);
594 [
self dispatchMouseEvent:event phase:phase];
597 - (void)dispatchGestureEvent:(nonnull NSEvent*)event {
598 if (event.phase == NSEventPhaseBegan || event.phase == NSEventPhaseMayBegin) {
599 [
self dispatchMouseEvent:event phase:kPanZoomStart];
600 }
else if (event.phase == NSEventPhaseChanged) {
601 [
self dispatchMouseEvent:event phase:kPanZoomUpdate];
602 }
else if (event.phase == NSEventPhaseEnded || event.phase == NSEventPhaseCancelled) {
603 [
self dispatchMouseEvent:event phase:kPanZoomEnd];
604 }
else if (event.phase == NSEventPhaseNone && event.momentumPhase == NSEventPhaseNone) {
605 [
self dispatchMouseEvent:event phase:kHover];
610 if (event.momentumPhase == NSEventPhaseChanged) {
611 _mouseState.last_scroll_momentum_changed_time =
event.timestamp;
614 NSAssert(event.momentumPhase != NSEventPhaseNone,
615 @"Received gesture event with unexpected phase");
619 - (void)dispatchMouseEvent:(NSEvent*)event phase:(FlutterPointerPhase)phase {
620 NSAssert(
self.viewLoaded,
@"View must be loaded before it handles the mouse event");
624 if (_mouseState.flutter_state_is_added && phase == kAdd) {
630 if (phase == kPanZoomStart || phase == kPanZoomEnd) {
631 if (event.type == NSEventTypeScrollWheel) {
632 _mouseState.pan_gesture_phase =
event.phase;
633 }
else if (event.type == NSEventTypeMagnify) {
634 _mouseState.scale_gesture_phase =
event.phase;
635 }
else if (event.type == NSEventTypeRotate) {
636 _mouseState.rotate_gesture_phase =
event.phase;
639 if (phase == kPanZoomStart) {
640 if (event.type == NSEventTypeScrollWheel) {
642 _mouseState.last_scroll_momentum_changed_time = 0;
644 if (_mouseState.flutter_state_is_pan_zoom_started) {
648 _mouseState.flutter_state_is_pan_zoom_started =
true;
650 if (phase == kPanZoomEnd) {
651 if (!_mouseState.flutter_state_is_pan_zoom_started) {
655 NSAssert(event.phase == NSEventPhaseCancelled,
656 @"Received gesture event with unexpected phase");
660 NSEventPhase all_gestures_fields = _mouseState.pan_gesture_phase |
661 _mouseState.scale_gesture_phase |
662 _mouseState.rotate_gesture_phase;
663 NSEventPhase active_mask = NSEventPhaseBegan | NSEventPhaseChanged;
664 if ((all_gestures_fields & active_mask) != 0) {
672 if (!_mouseState.flutter_state_is_added && phase != kAdd) {
674 NSEvent* addEvent = [NSEvent enterExitEventWithType:NSEventTypeMouseEntered
675 location:event.locationInWindow
677 timestamp:event.timestamp
678 windowNumber:event.windowNumber
683 [
self dispatchMouseEvent:addEvent phase:kAdd];
686 NSPoint locationInView = [
self.flutterView convertPoint:event.locationInWindow fromView:nil];
687 NSPoint locationInBackingCoordinates = [
self.flutterView convertPointToBacking:locationInView];
688 int32_t device = kMousePointerDeviceId;
689 FlutterPointerDeviceKind deviceKind = kFlutterPointerDeviceKindMouse;
690 if (phase == kPanZoomStart || phase == kPanZoomUpdate || phase == kPanZoomEnd) {
691 device = kPointerPanZoomDeviceId;
692 deviceKind = kFlutterPointerDeviceKindTrackpad;
694 FlutterPointerEvent flutterEvent = {
695 .struct_size =
sizeof(flutterEvent),
697 .timestamp =
static_cast<size_t>(event.timestamp * USEC_PER_SEC),
698 .x = locationInBackingCoordinates.x,
699 .y = -locationInBackingCoordinates.y,
701 .device_kind = deviceKind,
703 .buttons = phase == kAdd ? 0 : _mouseState.buttons,
704 .view_id = static_cast<FlutterViewIdentifier>(_viewIdentifier),
707 if (phase == kPanZoomUpdate) {
708 if (event.type == NSEventTypeScrollWheel) {
709 _mouseState.delta_x += event.scrollingDeltaX * self.flutterView.layer.contentsScale;
710 _mouseState.delta_y += event.scrollingDeltaY * self.flutterView.layer.contentsScale;
711 }
else if (event.type == NSEventTypeMagnify) {
712 _mouseState.scale += event.magnification;
713 }
else if (event.type == NSEventTypeRotate) {
714 _mouseState.rotation += event.rotation * (-M_PI / 180.0);
716 flutterEvent.pan_x = _mouseState.delta_x;
717 flutterEvent.pan_y = _mouseState.delta_y;
719 flutterEvent.scale = pow(2.0, _mouseState.scale);
720 flutterEvent.rotation = _mouseState.rotation;
721 }
else if (phase == kPanZoomEnd) {
722 _mouseState.GestureReset();
723 }
else if (phase != kPanZoomStart && event.type == NSEventTypeScrollWheel) {
724 flutterEvent.signal_kind = kFlutterPointerSignalKindScroll;
726 double pixelsPerLine = 1.0;
727 if (!event.hasPreciseScrollingDeltas) {
733 pixelsPerLine = 40.0;
735 double scaleFactor =
self.flutterView.layer.contentsScale;
744 double scaledDeltaX = -event.scrollingDeltaX * pixelsPerLine * scaleFactor;
745 double scaledDeltaY = -event.scrollingDeltaY * pixelsPerLine * scaleFactor;
746 if (event.modifierFlags & NSShiftKeyMask) {
747 flutterEvent.scroll_delta_x = scaledDeltaY;
748 flutterEvent.scroll_delta_y = scaledDeltaX;
750 flutterEvent.scroll_delta_x = scaledDeltaX;
751 flutterEvent.scroll_delta_y = scaledDeltaY;
755 [_engine.keyboardManager syncModifiersIfNeeded:event.modifierFlags timestamp:event.timestamp];
756 [_engine sendPointerEvent:flutterEvent];
759 if (phase == kDown) {
760 _mouseState.flutter_state_is_down =
true;
761 }
else if (phase == kUp) {
762 _mouseState.flutter_state_is_down =
false;
763 if (_mouseState.has_pending_exit) {
764 [
self dispatchMouseEvent:event phase:kRemove];
765 _mouseState.has_pending_exit =
false;
767 }
else if (phase == kAdd) {
768 _mouseState.flutter_state_is_added =
true;
769 }
else if (phase == kRemove) {
774 - (void)onAccessibilityStatusChanged:(BOOL)enabled {
775 if (!enabled &&
self.viewLoaded && [_textInputPlugin isFirstResponder]) {
780 [
self.view addSubview:_textInputPlugin];
784 - (std::shared_ptr<flutter::AccessibilityBridgeMac>)createAccessibilityBridgeWithEngine:
786 return std::make_shared<flutter::AccessibilityBridgeMac>(engine,
self);
789 - (nonnull
FlutterView*)createFlutterViewWithMTLDevice:(
id<MTLDevice>)device
790 commandQueue:(
id<MTLCommandQueue>)commandQueue {
791 return [[
FlutterView alloc] initWithMTLDevice:device
792 commandQueue:commandQueue
794 threadSynchronizer:_threadSynchronizer
795 viewIdentifier:_viewIdentifier];
798 - (NSString*)lookupKeyForAsset:(NSString*)asset {
802 - (NSString*)lookupKeyForAsset:(NSString*)asset fromPackage:(NSString*)package {
806 #pragma mark - FlutterViewDelegate
811 - (void)viewDidReshape:(NSView*)view {
812 FML_DCHECK(view == _flutterView);
813 [_engine updateWindowMetricsForViewController:self];
816 - (BOOL)viewShouldAcceptFirstResponder:(NSView*)view {
817 FML_DCHECK(view == _flutterView);
821 return !_textInputPlugin.isFirstResponder;
824 #pragma mark - FlutterPluginRegistry
827 return [_engine registrarForPlugin:pluginName];
830 - (NSObject*)valuePublishedByPlugin:(NSString*)pluginKey {
831 return [_engine valuePublishedByPlugin:pluginKey];
834 #pragma mark - FlutterKeyboardViewDelegate
836 - (BOOL)onTextInputKeyEvent:(nonnull NSEvent*)event {
837 return [_textInputPlugin handleKeyEvent:event];
840 #pragma mark - NSResponder
842 - (BOOL)acceptsFirstResponder {
846 - (void)keyDown:(NSEvent*)event {
847 [_engine.keyboardManager handleEvent:event withContext:self];
850 - (void)keyUp:(NSEvent*)event {
851 [_engine.keyboardManager handleEvent:event withContext:self];
854 - (void)flagsChanged:(NSEvent*)event {
855 [_engine.keyboardManager handleEvent:event withContext:self];
858 - (void)mouseEntered:(NSEvent*)event {
859 if (_mouseState.has_pending_exit) {
860 _mouseState.has_pending_exit =
false;
862 [
self dispatchMouseEvent:event phase:kAdd];
866 - (void)mouseExited:(NSEvent*)event {
867 if (_mouseState.buttons != 0) {
868 _mouseState.has_pending_exit =
true;
871 [
self dispatchMouseEvent:event phase:kRemove];
874 - (void)mouseDown:(NSEvent*)event {
875 _mouseState.buttons |= kFlutterPointerButtonMousePrimary;
876 [
self dispatchMouseEvent:event];
879 - (void)mouseUp:(NSEvent*)event {
880 _mouseState.buttons &= ~static_cast<uint64_t>(kFlutterPointerButtonMousePrimary);
881 [
self dispatchMouseEvent:event];
884 - (void)mouseDragged:(NSEvent*)event {
885 [
self dispatchMouseEvent:event];
888 - (void)rightMouseDown:(NSEvent*)event {
889 _mouseState.buttons |= kFlutterPointerButtonMouseSecondary;
890 [
self dispatchMouseEvent:event];
893 - (void)rightMouseUp:(NSEvent*)event {
894 _mouseState.buttons &= ~static_cast<uint64_t>(kFlutterPointerButtonMouseSecondary);
895 [
self dispatchMouseEvent:event];
898 - (void)rightMouseDragged:(NSEvent*)event {
899 [
self dispatchMouseEvent:event];
902 - (void)otherMouseDown:(NSEvent*)event {
903 _mouseState.buttons |= (1 <<
event.buttonNumber);
904 [
self dispatchMouseEvent:event];
907 - (void)otherMouseUp:(NSEvent*)event {
908 _mouseState.buttons &= ~static_cast<uint64_t>(1 << event.buttonNumber);
909 [
self dispatchMouseEvent:event];
912 - (void)otherMouseDragged:(NSEvent*)event {
913 [
self dispatchMouseEvent:event];
916 - (void)mouseMoved:(NSEvent*)event {
917 [
self dispatchMouseEvent:event];
920 - (void)scrollWheel:(NSEvent*)event {
921 [
self dispatchGestureEvent:event];
924 - (void)magnifyWithEvent:(NSEvent*)event {
925 [
self dispatchGestureEvent:event];
928 - (void)rotateWithEvent:(NSEvent*)event {
929 [
self dispatchGestureEvent:event];
932 - (void)swipeWithEvent:(NSEvent*)event {
936 - (void)touchesBeganWithEvent:(NSEvent*)event {
937 NSTouch* touch =
event.allTouches.anyObject;
939 if ((event.timestamp - _mouseState.last_scroll_momentum_changed_time) <
940 kTrackpadTouchInertiaCancelWindowMs) {
943 NSPoint locationInView = [
self.flutterView convertPoint:event.locationInWindow fromView:nil];
944 NSPoint locationInBackingCoordinates =
945 [
self.flutterView convertPointToBacking:locationInView];
946 FlutterPointerEvent flutterEvent = {
947 .struct_size =
sizeof(flutterEvent),
948 .timestamp =
static_cast<size_t>(event.timestamp * USEC_PER_SEC),
949 .x = locationInBackingCoordinates.x,
950 .y = -locationInBackingCoordinates.y,
951 .device = kPointerPanZoomDeviceId,
952 .signal_kind = kFlutterPointerSignalKindScrollInertiaCancel,
953 .device_kind = kFlutterPointerDeviceKindTrackpad,
957 [_engine sendPointerEvent:flutterEvent];
959 _mouseState.last_scroll_momentum_changed_time = 0;