Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

[macos] Add hover support to FLEViewController #7975

Merged
merged 1 commit into from
Feb 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions shell/platform/darwin/macos/framework/Headers/FLEViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,17 @@
#import "FlutterMacros.h"
#endif

typedef NS_ENUM(NSInteger, FlutterMouseTrackingMode) {
// Hover events will never be sent to Flutter.
FlutterMouseTrackingModeNone = 0,
// Hover events will be sent to Flutter when the view is in the key window.
FlutterMouseTrackingModeInKeyWindow,
// Hover events will be sent to Flutter when the view is in the active app.
FlutterMouseTrackingModeInActiveApp,
// Hover events will be sent to Flutter regardless of window and app focus.
FlutterMouseTrackingModeAlways,
};

/**
* Controls embedder plugins and communication with the underlying Flutter engine, managing a view
* intended to handle key inputs and drawing protocols (see |view|).
Expand All @@ -33,6 +44,12 @@ FLUTTER_EXPORT
*/
@property(nullable) NSView<FLEOpenGLContextHandling>* view;

/**
* The style of mouse tracking to use for the view. Defaults to
* FlutterMouseTrackingModeNone.
*/
@property(nonatomic) FlutterMouseTrackingMode mouseTrackingMode;

/**
* Launches the Flutter engine with the provided configuration.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,18 @@ @interface FLEViewController ()
/**
* A list of additional responders to keyboard events. Keybord events are forwarded to all of them.
*/
@property NSMutableOrderedSet<NSResponder*>* additionalKeyResponders;
@property(nonatomic) NSMutableOrderedSet<NSResponder*>* additionalKeyResponders;

/**
* The tracking area used to generate hover events, if enabled.
*/
@property(nonatomic) NSTrackingArea* trackingArea;

/**
* Updates |trackingArea| for the current tracking settings, creating it with
* the correct mode if tracking is enabled, or removing it if not.
*/
- (void)configureTrackingArea;

/**
* Creates and registers plugins used by this view controller.
Expand Down Expand Up @@ -202,12 +213,28 @@ - (void)dealloc {
}
}

- (void)setView:(NSView*)view {
if (_trackingArea) {
[self.view removeTrackingArea:_trackingArea];
}
[super setView:view];
[self configureTrackingArea];
}

- (void)loadView {
self.view = [[FLEView alloc] init];
}

#pragma mark - Public methods

- (void)setMouseTrackingMode:(FlutterMouseTrackingMode)mode {
if (_mouseTrackingMode == mode) {
return;
}
_mouseTrackingMode = mode;
[self configureTrackingArea];
}

- (BOOL)launchEngineWithAssetsPath:(NSURL*)assets
commandLineArguments:(NSArray<NSString*>*)arguments {
return [self launchEngineInternalWithAssetsPath:assets
Expand Down Expand Up @@ -241,6 +268,35 @@ - (void)removeKeyResponder:(NSResponder*)responder {

#pragma mark - Private methods

- (void)configureTrackingArea {
if (_mouseTrackingMode != FlutterMouseTrackingModeNone && self.view) {
NSTrackingAreaOptions options =
NSTrackingMouseEnteredAndExited | NSTrackingMouseMoved | NSTrackingInVisibleRect;
switch (_mouseTrackingMode) {
case FlutterMouseTrackingModeInKeyWindow:
options |= NSTrackingActiveInKeyWindow;
break;
case FlutterMouseTrackingModeInActiveApp:
options |= NSTrackingActiveInActiveApp;
break;
case FlutterMouseTrackingModeAlways:
options |= NSTrackingActiveAlways;
break;
default:
NSLog(@"Error: Unrecognized mouse tracking mode: %ld", _mouseTrackingMode);
return;
}
_trackingArea = [[NSTrackingArea alloc] initWithRect:NSZeroRect
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is ARC right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, all the macos/framework code is compiled with ARC enabled.

options:options
owner:self
userInfo:nil];
[self.view addTrackingArea:_trackingArea];
} else if (_trackingArea) {
[self.view removeTrackingArea:_trackingArea];
_trackingArea = nil;
}
}

- (void)addInternalPlugins {
_textInputPlugin = [[FLETextInputPlugin alloc] initWithViewController:self];
_keyEventChannel =
Expand Down Expand Up @@ -471,4 +527,16 @@ - (void)mouseDragged:(NSEvent*)event {
[self dispatchMouseEvent:event phase:kMove];
}

- (void)mouseEntered:(NSEvent*)event {
[self dispatchMouseEvent:event phase:kAdd];
}

- (void)mouseExited:(NSEvent*)event {
[self dispatchMouseEvent:event phase:kRemove];
}

- (void)mouseMoved:(NSEvent*)event {
[self dispatchMouseEvent:event phase:kHover];
}

@end