Flutter Engine
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 
5 #import "flutter/shell/platform/darwin/macos/framework/Source/FlutterView.h"
6 
7 #import "flutter/shell/platform/darwin/macos/framework/Source/FlutterRenderingBackend.h"
8 #import "flutter/shell/platform/darwin/macos/framework/Source/FlutterResizeSynchronizer.h"
9 #import "flutter/shell/platform/darwin/macos/framework/Source/FlutterSurfaceManager.h"
10 #import "flutter/shell/platform/darwin/macos/framework/Source/MacOSGLContextSwitch.h"
11 
12 #import <OpenGL/gl.h>
13 #import <QuartzCore/QuartzCore.h>
14 
15 @interface FlutterView () {
16  __weak id<FlutterViewReshapeListener> _reshapeListener;
18  id<FlutterResizableBackingStoreProvider> _resizableBackingStoreProvider;
19 }
20 
21 @end
22 
23 @implementation FlutterView
24 
25 - (instancetype)initWithMTLDevice:(id<MTLDevice>)device
26  commandQueue:(id<MTLCommandQueue>)commandQueue
27  reshapeListener:(id<FlutterViewReshapeListener>)reshapeListener {
28  self = [super initWithFrame:NSZeroRect];
29  if (self) {
30  [self setWantsLayer:YES];
31  [self setLayerContentsRedrawPolicy:NSViewLayerContentsRedrawDuringViewResize];
32  _reshapeListener = reshapeListener;
33  _resizableBackingStoreProvider =
34  [[FlutterMetalResizableBackingStoreProvider alloc] initWithDevice:device
35  commandQueue:commandQueue
36  layer:self.layer];
37  _resizeSynchronizer =
38  [[FlutterResizeSynchronizer alloc] initWithDelegate:_resizableBackingStoreProvider];
39  }
40  return self;
41 }
42 
43 - (instancetype)initWithMainContext:(NSOpenGLContext*)mainContext
44  reshapeListener:(id<FlutterViewReshapeListener>)reshapeListener {
45  return [self initWithFrame:NSZeroRect mainContext:mainContext reshapeListener:reshapeListener];
46 }
47 
48 - (instancetype)initWithFrame:(NSRect)frame
49  mainContext:(NSOpenGLContext*)mainContext
50  reshapeListener:(id<FlutterViewReshapeListener>)reshapeListener {
51  self = [super initWithFrame:frame];
52  if (self) {
53  [self setWantsLayer:YES];
54  _reshapeListener = reshapeListener;
55  _resizableBackingStoreProvider =
56  [[FlutterOpenGLResizableBackingStoreProvider alloc] initWithMainContext:mainContext
57  layer:self.layer];
58  _resizeSynchronizer =
59  [[FlutterResizeSynchronizer alloc] initWithDelegate:_resizableBackingStoreProvider];
60  }
61  return self;
62 }
63 
64 - (FlutterRenderBackingStore*)backingStoreForSize:(CGSize)size {
65  if ([_resizeSynchronizer shouldEnsureSurfaceForSize:size]) {
66  [_resizableBackingStoreProvider onBackingStoreResized:size];
67  }
68  return [_resizableBackingStoreProvider backingStore];
69 }
70 
71 - (void)present {
72  [_resizeSynchronizer requestCommit];
73 }
74 
75 - (void)reshaped {
76  CGSize scaledSize = [self convertSizeToBacking:self.bounds.size];
77  [_resizeSynchronizer beginResize:scaledSize
78  notify:^{
79  [_reshapeListener viewDidReshape:self];
80  }];
81 }
82 
83 #pragma mark - NSView overrides
84 
85 - (void)setFrameSize:(NSSize)newSize {
86  [super setFrameSize:newSize];
87  [self reshaped];
88 }
89 
90 /**
91  * Declares that the view uses a flipped coordinate system, consistent with Flutter conventions.
92  */
93 - (BOOL)isFlipped {
94  return YES;
95 }
96 
97 - (BOOL)isOpaque {
98  return YES;
99 }
100 
101 - (BOOL)acceptsFirstResponder {
102  return YES;
103 }
104 
105 - (void)viewDidChangeBackingProperties {
106  [super viewDidChangeBackingProperties];
107  // Force redraw
108  [_reshapeListener viewDidReshape:self];
109 }
110 
111 - (void)shutdown {
112  [_resizeSynchronizer shutdown];
113 }
114 #pragma mark - NSAccessibility overrides
115 
116 - (BOOL)isAccessibilityElement {
117  return YES;
118 }
119 
120 - (NSAccessibilityRole)accessibilityRole {
121  return NSAccessibilityGroupRole;
122 }
123 
124 - (NSString*)accessibilityLabel {
125  // TODO(chunhtai): Provides a way to let developer customize the accessibility
126  // label.
127  // https://github.com/flutter/flutter/issues/75446
128  NSString* applicationName =
129  [NSBundle.mainBundle objectForInfoDictionaryKey:@"CFBundleDisplayName"];
130  if (!applicationName) {
131  applicationName = [NSBundle.mainBundle objectForInfoDictionaryKey:@"CFBundleName"];
132  }
133  return applicationName;
134 }
135 
136 @end
constexpr std::size_t size(T(&array)[N])
Definition: size.h:13
void present()
Definition: FlutterView.mm:71
id< FlutterResizableBackingStoreProvider > _resizableBackingStoreProvider
Definition: FlutterView.mm:18
__weak id< FlutterViewReshapeListener > _reshapeListener
Definition: FlutterView.mm:16
int BOOL
Definition: windows_types.h:37
FlutterResizeSynchronizer * _resizeSynchronizer
Definition: FlutterView.mm:17