@@ -32,7 +32,18 @@ @interface FLEViewController ()
3232/* *
3333 * A list of additional responders to keyboard events. Keybord events are forwarded to all of them.
3434 */
35- @property NSMutableOrderedSet <NSResponder*>* additionalKeyResponders;
35+ @property (nonatomic ) NSMutableOrderedSet <NSResponder*>* additionalKeyResponders;
36+
37+ /* *
38+ * The tracking area used to generate hover events, if enabled.
39+ */
40+ @property (nonatomic ) NSTrackingArea * trackingArea;
41+
42+ /* *
43+ * Updates |trackingArea| for the current tracking settings, creating it with
44+ * the correct mode if tracking is enabled, or removing it if not.
45+ */
46+ - (void )configureTrackingArea ;
3647
3748/* *
3849 * Creates and registers plugins used by this view controller.
@@ -202,12 +213,28 @@ - (void)dealloc {
202213 }
203214}
204215
216+ - (void )setView : (NSView *)view {
217+ if (_trackingArea) {
218+ [self .view removeTrackingArea: _trackingArea];
219+ }
220+ [super setView: view];
221+ [self configureTrackingArea ];
222+ }
223+
205224- (void )loadView {
206225 self.view = [[FLEView alloc ] init ];
207226}
208227
209228#pragma mark - Public methods
210229
230+ - (void )setMouseTrackingMode : (FlutterMouseTrackingMode)mode {
231+ if (_mouseTrackingMode == mode) {
232+ return ;
233+ }
234+ _mouseTrackingMode = mode;
235+ [self configureTrackingArea ];
236+ }
237+
211238- (BOOL )launchEngineWithAssetsPath : (NSURL *)assets
212239 commandLineArguments : (NSArray <NSString*>*)arguments {
213240 return [self launchEngineInternalWithAssetsPath: assets
@@ -241,6 +268,35 @@ - (void)removeKeyResponder:(NSResponder*)responder {
241268
242269#pragma mark - Private methods
243270
271+ - (void )configureTrackingArea {
272+ if (_mouseTrackingMode != FlutterMouseTrackingModeNone && self.view ) {
273+ NSTrackingAreaOptions options =
274+ NSTrackingMouseEnteredAndExited | NSTrackingMouseMoved | NSTrackingInVisibleRect;
275+ switch (_mouseTrackingMode) {
276+ case FlutterMouseTrackingModeInKeyWindow:
277+ options |= NSTrackingActiveInKeyWindow;
278+ break ;
279+ case FlutterMouseTrackingModeInActiveApp:
280+ options |= NSTrackingActiveInActiveApp;
281+ break ;
282+ case FlutterMouseTrackingModeAlways:
283+ options |= NSTrackingActiveAlways;
284+ break ;
285+ default :
286+ NSLog (@" Error: Unrecognized mouse tracking mode: %ld " , _mouseTrackingMode);
287+ return ;
288+ }
289+ _trackingArea = [[NSTrackingArea alloc ] initWithRect: NSZeroRect
290+ options: options
291+ owner: self
292+ userInfo: nil ];
293+ [self .view addTrackingArea: _trackingArea];
294+ } else if (_trackingArea) {
295+ [self .view removeTrackingArea: _trackingArea];
296+ _trackingArea = nil ;
297+ }
298+ }
299+
244300- (void )addInternalPlugins {
245301 _textInputPlugin = [[FLETextInputPlugin alloc ] initWithViewController: self ];
246302 _keyEventChannel =
@@ -471,4 +527,16 @@ - (void)mouseDragged:(NSEvent*)event {
471527 [self dispatchMouseEvent: event phase: kMove ];
472528}
473529
530+ - (void )mouseEntered : (NSEvent *)event {
531+ [self dispatchMouseEvent: event phase: kAdd ];
532+ }
533+
534+ - (void )mouseExited : (NSEvent *)event {
535+ [self dispatchMouseEvent: event phase: kRemove ];
536+ }
537+
538+ - (void )mouseMoved : (NSEvent *)event {
539+ [self dispatchMouseEvent: event phase: kHover ];
540+ }
541+
474542@end
0 commit comments