Skip to content

Commit

Permalink
Add flutter settings channel and window brightness to macOS shell (fl…
Browse files Browse the repository at this point in the history
  • Loading branch information
jonahwilliams authored May 3, 2019
1 parent 4808446 commit 1bcbaf7
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions shell/platform/darwin/macos/framework/Source/FLEViewController.mm
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,16 @@ - (void)dispatchMouseEvent:(nonnull NSEvent*)event phase:(FlutterPointerPhase)ph
*/
- (void)dispatchKeyEvent:(NSEvent*)event ofType:(NSString*)type;

/**
* Initializes the KVO for user settings and passes the initial user settings to the engine.
*/
- (void)sendInitialSettings;

/**
* Responsds to updates in the user settings and passes this data to the engine.
*/
- (void)onSettingsChanged:(NSNotification*)notification;

@end

#pragma mark - Static methods provided to engine configuration
Expand Down Expand Up @@ -181,6 +191,9 @@ @implementation FLEViewController {
// A message channel for passing key events to the Flutter engine. This should be replaced with
// an embedding API; see Issue #47.
FlutterBasicMessageChannel* _keyEventChannel;

// A message channel for sending user settings to the flutter engine.
FlutterBasicMessageChannel* _settingsChannel;
}

@dynamic view;
Expand Down Expand Up @@ -298,6 +311,10 @@ - (void)addInternalPlugins {
[FlutterBasicMessageChannel messageChannelWithName:@"flutter/keyevent"
binaryMessenger:self
codec:[FlutterJSONMessageCodec sharedInstance]];
_settingsChannel =
[FlutterBasicMessageChannel messageChannelWithName:@"flutter/settings"
binaryMessenger:self
codec:[FlutterJSONMessageCodec sharedInstance]];
}

- (BOOL)launchEngineInternalWithAssetsPath:(NSURL*)assets
Expand Down Expand Up @@ -345,6 +362,9 @@ - (BOOL)launchEngineInternalWithAssetsPath:(NSURL*)assets
NSLog(@"Failed to start Flutter engine: error %d", result);
return NO;
}
// Send the initial user settings such as brightness and text scale factor
// to the engine.
[self sendInitialSettings];
return YES;
}

Expand Down Expand Up @@ -472,6 +492,28 @@ - (void)dispatchKeyEvent:(NSEvent*)event ofType:(NSString*)type {
}];
}

- (void)onSettingsChanged:(NSNotification*)notification {
// TODO(jonahwilliams): https://github.com/flutter/flutter/issues/32015.
NSString* brightness =
[[NSUserDefaults standardUserDefaults] stringForKey:@"AppleInterfaceStyle"];
[_settingsChannel sendMessage:@{
@"platformBrightness" : [brightness isEqualToString:@"Dark"] ? @"dark" : @"light",
// TODO(jonahwilliams): https://github.com/flutter/flutter/issues/32006.
@"textScaleFactor" : @1.0,
@"alwaysUse24HourFormat" : @false
}];
}

- (void)sendInitialSettings {
// TODO(jonahwilliams): https://github.com/flutter/flutter/issues/32015.
[[NSDistributedNotificationCenter defaultCenter]
addObserver:self
selector:@selector(onSettingsChanged:)
name:@"AppleInterfaceThemeChangedNotification"
object:nil];
[self onSettingsChanged:nil];
}

#pragma mark - FLEReshapeListener

/**
Expand Down

0 comments on commit 1bcbaf7

Please sign in to comment.