Flutter macOS Embedder
FlutterView.mm
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
6 
9 
10 #import <QuartzCore/QuartzCore.h>
11 
13  int64_t _viewId;
14  __weak id<FlutterViewReshapeListener> _reshapeListener;
17 }
18 
19 @end
20 
21 @implementation FlutterView
22 
23 - (instancetype)initWithMTLDevice:(id<MTLDevice>)device
24  commandQueue:(id<MTLCommandQueue>)commandQueue
25  reshapeListener:(id<FlutterViewReshapeListener>)reshapeListener
26  threadSynchronizer:(FlutterThreadSynchronizer*)threadSynchronizer
27  viewId:(int64_t)viewId {
28  self = [super initWithFrame:NSZeroRect];
29  if (self) {
30  [self setWantsLayer:YES];
31  [self setBackgroundColor:[NSColor blackColor]];
32  [self setLayerContentsRedrawPolicy:NSViewLayerContentsRedrawDuringViewResize];
33  _viewId = viewId;
34  _reshapeListener = reshapeListener;
35  _threadSynchronizer = threadSynchronizer;
36  _surfaceManager = [[FlutterSurfaceManager alloc] initWithDevice:device
37  commandQueue:commandQueue
38  layer:self.layer
39  delegate:self];
40  }
41  return self;
42 }
43 
44 - (void)onPresent:(CGSize)frameSize withBlock:(dispatch_block_t)block {
45  [_threadSynchronizer performCommitForView:_viewId size:frameSize notify:block];
46 }
47 
49  return _surfaceManager;
50 }
51 
52 - (void)reshaped {
53  CGSize scaledSize = [self convertSizeToBacking:self.bounds.size];
54  [_threadSynchronizer beginResizeForView:_viewId
55  size:scaledSize
56  notify:^{
57  [_reshapeListener viewDidReshape:self];
58  }];
59 }
60 
61 - (void)setBackgroundColor:(NSColor*)color {
62  self.layer.backgroundColor = color.CGColor;
63 }
64 
65 #pragma mark - NSView overrides
66 
67 - (void)setFrameSize:(NSSize)newSize {
68  [super setFrameSize:newSize];
69  [self reshaped];
70 }
71 
72 /**
73  * Declares that the view uses a flipped coordinate system, consistent with Flutter conventions.
74  */
75 - (BOOL)isFlipped {
76  return YES;
77 }
78 
79 - (BOOL)isOpaque {
80  return YES;
81 }
82 
83 /**
84  * Declares that the initial mouse-down when the view is not in focus will send an event to the
85  * view.
86  */
87 - (BOOL)acceptsFirstMouse:(NSEvent*)event {
88  return YES;
89 }
90 
91 - (BOOL)acceptsFirstResponder {
92  return YES;
93 }
94 
95 - (void)cursorUpdate:(NSEvent*)event {
96  // When adding/removing views AppKit will schedule call to current hit-test view
97  // cursorUpdate: at the end of frame to determine possible cursor change. If
98  // the view doesn't implement cursorUpdate: AppKit will set the default (arrow) cursor
99  // instead. This would replace the cursor set by FlutterMouseCursorPlugin.
100  // Empty cursorUpdate: implementation prevents this behavior.
101  // https://github.com/flutter/flutter/issues/111425
102 }
103 
104 - (void)viewDidChangeBackingProperties {
105  [super viewDidChangeBackingProperties];
106  // Force redraw
107  [_reshapeListener viewDidReshape:self];
108 }
109 
110 - (BOOL)layer:(CALayer*)layer
111  shouldInheritContentsScale:(CGFloat)newScale
112  fromWindow:(NSWindow*)window {
113  return YES;
114 }
115 
116 #pragma mark - NSAccessibility overrides
117 
118 - (BOOL)isAccessibilityElement {
119  return YES;
120 }
121 
122 - (NSAccessibilityRole)accessibilityRole {
123  return NSAccessibilityGroupRole;
124 }
125 
126 - (NSString*)accessibilityLabel {
127  // TODO(chunhtai): Provides a way to let developer customize the accessibility
128  // label.
129  // https://github.com/flutter/flutter/issues/75446
130  NSString* applicationName =
131  [NSBundle.mainBundle objectForInfoDictionaryKey:@"CFBundleDisplayName"];
132  if (!applicationName) {
133  applicationName = [NSBundle.mainBundle objectForInfoDictionaryKey:@"CFBundleName"];
134  }
135  return applicationName;
136 }
137 
138 @end
FlutterView::surfaceManager
FlutterSurfaceManager * surfaceManager
Definition: FlutterView.h:60
FlutterSurfaceManager.h
FlutterViewReshapeListener-p
Definition: FlutterView.h:28
FlutterSurfaceManager
Definition: FlutterSurfaceManager.h:41
FlutterView()::_surfaceManager
FlutterSurfaceManager * _surfaceManager
Definition: FlutterView.mm:16
FlutterSurfaceManagerDelegate-p
Definition: FlutterSurfaceManager.h:24
FlutterThreadSynchronizer
Definition: FlutterThreadSynchronizer.h:13
FlutterView()::_threadSynchronizer
FlutterThreadSynchronizer * _threadSynchronizer
Definition: FlutterView.mm:15
FlutterView()::_reshapeListener
__weak id< FlutterViewReshapeListener > _reshapeListener
Definition: FlutterView.mm:14
FlutterView
Definition: FlutterView.h:39
FlutterView()::_viewId
int64_t _viewId
Definition: FlutterView.mm:13
FlutterThreadSynchronizer.h
FlutterView.h
_threadSynchronizer
FlutterThreadSynchronizer * _threadSynchronizer
Definition: FlutterEngine.mm:431