Flutter macOS Embedder
FlutterView.h
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 #ifndef FLUTTER_SHELL_PLATFORM_DARWIN_MACOS_FRAMEWORK_SOURCE_FLUTTERVIEW_H_
6 #define FLUTTER_SHELL_PLATFORM_DARWIN_MACOS_FRAMEWORK_SOURCE_FLUTTERVIEW_H_
7 
8 #import <Cocoa/Cocoa.h>
9 
12 
13 #include <stdint.h>
14 #include <optional>
15 
16 @class FlutterView;
17 
18 @protocol FlutterViewContentDelegate <NSObject>
19 
20 /**
21  * Called when the view's content changes. If the size changed the container
22  * should update its layout to accommodate new size.
23  */
24 - (void)viewDidUpdateContents:(nonnull FlutterView*)view withSize:(NSSize)newSize;
25 
26 @end
27 
28 /**
29  * Interface that facilitates process of sizing the FlutterView to its
30  * content. It is used to determine the content constraints and to notify
31  * the view container about the size change so that the parent view can
32  * be resized (and repositioned) accordingly.
33  */
34 @protocol FlutterViewSizingDelegate <NSObject>
35 
36 /**
37  * When view should be sized to content, this method should return the minimum
38  * logical size of the view.
39  * For views that are not sized to content, will return std::nullopt;
40  */
41 - (std::optional<NSSize>)minimumViewSize:(nonnull FlutterView*)view;
42 
43 /**
44  * When view should be sized to content, this method should return the maximum
45  * logical size of the view.
46  * For views that are not sized to content, this method should return std::nullopt.
47  */
48 - (std::optional<NSSize>)maximumViewSize:(nonnull FlutterView*)view;
49 
50 @end
51 
52 /**
53  * Delegate for FlutterView.
54  */
55 @protocol FlutterViewDelegate <NSObject>
56 /**
57  * Called when the view's backing store changes size.
58  */
59 - (void)viewDidReshape:(nonnull NSView*)view;
60 
61 /**
62  * Called to determine whether the view should accept first responder status.
63  */
64 - (BOOL)viewShouldAcceptFirstResponder:(nonnull NSView*)view;
65 
66 @end
67 
68 /**
69  * View capable of acting as a rendering target and input source for the Flutter
70  * engine.
71  */
72 @interface FlutterView : NSView
73 
74 /**
75  * Initialize a FlutterView that will be rendered to using Metal rendering apis.
76  */
77 - (nullable instancetype)initWithMTLDevice:(nonnull id<MTLDevice>)device
78  commandQueue:(nonnull id<MTLCommandQueue>)commandQueue
79  delegate:(nonnull id<FlutterViewDelegate>)delegate
80  viewIdentifier:(FlutterViewIdentifier)viewIdentifier
81  enableWideGamut:(BOOL)enableWideGamut NS_DESIGNATED_INITIALIZER;
82 
83 - (nullable instancetype)initWithFrame:(NSRect)frameRect
84  pixelFormat:(nullable NSOpenGLPixelFormat*)format NS_UNAVAILABLE;
85 - (nonnull instancetype)initWithFrame:(NSRect)frameRect NS_UNAVAILABLE;
86 - (nullable instancetype)initWithCoder:(nonnull NSCoder*)coder NS_UNAVAILABLE;
87 - (nonnull instancetype)init NS_UNAVAILABLE;
88 
89 /**
90  * Returns SurfaceManager for this view. SurfaceManager is responsible for
91  * providing and presenting render surfaces.
92  */
93 @property(readonly, nonatomic, nonnull) FlutterSurfaceManager* surfaceManager;
94 
95 /**
96  * Optional content delegate. If set, the view will inform the delegate about content change.
97  */
98 @property(readwrite, nonatomic, weak, nullable) id<FlutterViewContentDelegate> contentDelegate;
99 
100 /**
101  * Optional sizing delegate. If set, the view can be sized to its content.
102  */
103 @property(readwrite, nonatomic, weak, nullable) id<FlutterViewSizingDelegate> sizingDelegate;
104 
105 /**
106  * By default, the `FlutterSurfaceManager` creates two layers to manage Flutter
107  * content, the content layer and containing layer. To set the native background
108  * color, onto which the Flutter content is drawn, call this method with the
109  * NSColor which you would like to override the default, black background color
110  * with.
111  */
112 - (void)setBackgroundColor:(nonnull NSColor*)color;
113 
114 /**
115  * Called from the engine to notify the view that mouse cursor was updated while
116  * the mouse is over the view. The view is responsible from restoring the cursor
117  * when the mouse enters the view from another subview.
118  */
119 - (void)didUpdateMouseCursor:(nonnull NSCursor*)cursor;
120 
121 /**
122  * Called from the controller to unblock resize synchronizer when shutting down.
123  */
124 - (void)shutDown;
125 
126 /**
127  * Whether this view is sized to contents. If so, resize synchronization
128  * will be disabled.
129  */
130 @property(nonatomic, readonly) BOOL sizedToContents;
131 
132 /**
133  * When sized to contents, this property returns the minimum content size.
134  * If not sized to contents, this property returns NSZeroSize.
135  */
136 @property(nonatomic, readonly) CGSize minimumContentSize;
137 
138 /**
139  * When sized to contents, this property returns the maximum content size.
140  * If not sized to contents, this property returns NSZeroSize.
141  */
142 @property(nonatomic, readonly) CGSize maximumContentSize;
143 
144 /**
145  * Informs the view that layout constraints have changed. The view should
146  * send reconfigure event to the engine so that new content matches the constraints.
147  */
148 - (void)constraintsDidChange;
149 
150 @end
151 
152 #endif // FLUTTER_SHELL_PLATFORM_DARWIN_MACOS_FRAMEWORK_SOURCE_FLUTTERVIEW_H_
int64_t FlutterViewIdentifier
CGSize maximumContentSize
Definition: FlutterView.h:142
void shutDown()
Definition: FlutterView.mm:66
BOOL sizedToContents
Definition: FlutterView.h:130
FlutterSurfaceManager * surfaceManager
Definition: FlutterView.h:93
void constraintsDidChange()
Definition: FlutterView.mm:200
id< FlutterViewContentDelegate > contentDelegate
Definition: FlutterView.h:98
nonnull instancetype NS_UNAVAILABLE()
CGSize minimumContentSize
Definition: FlutterView.h:136
id< FlutterViewSizingDelegate > sizingDelegate
Definition: FlutterView.h:103