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

Added the ability to set properties in interface builder for FlutterViewController #19458

Merged
merged 1 commit into from
Jul 10, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@ FLUTTER_EXPORT
nibName:(nullable NSString*)nibName
bundle:(nullable NSBundle*)nibBundle NS_DESIGNATED_INITIALIZER;

/**
* Initializer that is called from loading a FlutterViewController from a XIB.
*
* See also:
* https://developer.apple.com/documentation/foundation/nscoding/1416145-initwithcoder?language=objc
*/
- (instancetype)initWithCoder:(NSCoder*)aDecoder NS_DESIGNATED_INITIALIZER;

/**
* Registers a callback that will be invoked when the Flutter view has been rendered.
* The callback will be fired only once.
Expand Down Expand Up @@ -194,6 +202,17 @@ FLUTTER_EXPORT
*/
@property(nonatomic, readonly) NSObject<FlutterBinaryMessenger>* binaryMessenger;

/**
* If the `FlutterViewController` creates a `FlutterEngine`, this property
* determines if that `FlutterEngine` has `allowHeadlessExecution` set.
*
* The intention is that this is used with the XIB. Otherwise, a
* `FlutterEngine` can just be sent to the init methods.
*
* See also: `-[FlutterEngine initWithName:project:allowHeadlessExecution:]`
*/
@property(nonatomic, readonly) BOOL engineAllowHeadlessExecution;

@end

NS_ASSUME_NONNULL_END
Expand Down
36 changes: 24 additions & 12 deletions shell/platform/darwin/ios/framework/Source/FlutterViewController.mm
Original file line number Diff line number Diff line change
Expand Up @@ -111,22 +111,26 @@ - (instancetype)initWithEngine:(FlutterEngine*)engine
return self;
}

- (void)sharedSetupWithProject:(nullable FlutterDartProject*)project {
_viewOpaque = YES;
_weakFactory = std::make_unique<fml::WeakPtrFactory<FlutterViewController>>(self);
_engine.reset([[FlutterEngine alloc] initWithName:@"io.flutter"
Copy link
Member

Choose a reason for hiding this comment

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

Isn't _engine nil here?

Copy link
Member Author

Choose a reason for hiding this comment

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

_engine is a smart pointer, it represents nil until this reset function is called on it.

project:project
allowHeadlessExecution:self.engineAllowHeadlessExecution]);
_flutterView.reset([[FlutterView alloc] initWithDelegate:_engine opaque:self.isViewOpaque]);
[_engine.get() createShell:nil libraryURI:nil];
_engineNeedsLaunch = YES;
_ongoingTouches = [[NSMutableSet alloc] init];
[self loadDefaultSplashScreenView];
[self performCommonViewControllerInitialization];
}

- (instancetype)initWithProject:(nullable FlutterDartProject*)project
nibName:(nullable NSString*)nibName
bundle:(nullable NSBundle*)nibBundle {
self = [super initWithNibName:nibName bundle:nibBundle];
if (self) {
_viewOpaque = YES;
_weakFactory = std::make_unique<fml::WeakPtrFactory<FlutterViewController>>(self);
_engine.reset([[FlutterEngine alloc] initWithName:@"io.flutter"
Copy link
Member

Choose a reason for hiding this comment

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

How is this working? Where is _engine set?

Copy link
Member Author

Choose a reason for hiding this comment

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

_engine is now set in sharedSeutpWithProject

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh, to be clear, awakeFromNib is calling sharedSetupWithProject. Only after that call are the key-value properties set.

project:project
allowHeadlessExecution:NO]);
_flutterView.reset([[FlutterView alloc] initWithDelegate:_engine opaque:self.isViewOpaque]);
[_engine.get() createShell:nil libraryURI:nil];
_engineNeedsLaunch = YES;
_ongoingTouches = [[NSMutableSet alloc] init];
[self loadDefaultSplashScreenView];
[self performCommonViewControllerInitialization];
[self sharedSetupWithProject:project];
}

return self;
Expand All @@ -137,7 +141,15 @@ - (instancetype)initWithNibName:(NSString*)nibNameOrNil bundle:(NSBundle*)nibBun
}

- (instancetype)initWithCoder:(NSCoder*)aDecoder {
return [self initWithProject:nil nibName:nil bundle:nil];
self = [super initWithCoder:aDecoder];
Copy link
Member

Choose a reason for hiding this comment

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

The properties should be pulled off aDecoder at initialization (with copies where appropriate)

  _viewOpaque = aDecoder->_viewOpaque;
  _weakFactory = aDecoder->_weakFactory;
  _engine = aDecoder->_engine;
  _flutterView = aDecoder->_flutterView;
  _isDisplayingFlutterUI = [aDecoder decodeObjectForKey:@"isDisplayingFlutterUI"];
  _isHomeIndicatorHidden = [aDecoder decodeObjectForKey:@"isHomeIndicatorHidden"];
  _isPresentingViewControllerAnimating = [aDecoder decodeObjectForKey:@"isPresentingViewControllerAnimating"];

not re-set up as if it's a newly initialized object (it's being deserialized).

Copy link
Member Author

Choose a reason for hiding this comment

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

The NSCoder doesn't have a representation of a full object. It just has what is coming from the xib, so it won't have things like a _weakFactory. The default initWithCoder will use key-value to set properties that have been specified in the xib, you don't need to create custom readers for them.

Copy link
Member

@jmagman jmagman Jul 8, 2020

Choose a reason for hiding this comment

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

I never did grok UIViewController or NSViewController. Looked at more docs, you're right. Magic APIs...

return self;
}

- (void)awakeFromNib {
[super awakeFromNib];
if (!_engine.get()) {
[self sharedSetupWithProject:nil];
}
}

- (instancetype)init {
Expand Down