Skip to content

Commit

Permalink
WOCOperationMode should change UIUserInterfaceIdiom. Support UIModalP…
Browse files Browse the repository at this point in the history
…resentationFormSheet on tablets (non-fullscreen presentation).

Also adds examples to WOCCatalog app including resizing modal via
preferredContentSize and toggling tablet mode at runtime.
  • Loading branch information
ehren committed Apr 28, 2016
1 parent f1204d8 commit 8b10200
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 83 deletions.
32 changes: 0 additions & 32 deletions Frameworks/StarboardXaml/ApplicationMain.mm
Original file line number Diff line number Diff line change
Expand Up @@ -39,26 +39,6 @@ void SetCACompositorClient(CACompositorClientInterface* client) {
_compositorClient = client;
}

struct ApplicationProperties {
float width;
float height;
float scale;
std::string name;
bool isTablet;
bool isLandscape;
};

ApplicationProperties g_applicationProperties;

std::string GetAppNameFromPList() {
NSString* appName = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleDisplayName"];
if (appName != nil) {
return [appName UTF8String];
}

return "Starboard";
}

int ApplicationMainStart(
int argc, char* argv[], const char* principalName, const char* delegateName, float windowWidth, float windowHeight) {
// Note: We must use nil rather than an empty string for these class names
Expand All @@ -68,11 +48,6 @@ int ApplicationMainStart(
WOCDisplayMode* displayMode = [UIApplication displayMode];
[displayMode _setWindowSize:CGSizeMake(windowWidth, windowHeight)];

float defaultWidth = GetCACompositor()->screenWidth();
float defaultHeight = GetCACompositor()->screenHeight();
float defaultScale = GetCACompositor()->screenScale();
bool defaultTablet = false;

[NSBundle setMainBundlePath:@"."];

NSDictionary* infoDict = [[NSBundle mainBundle] infoDictionary];
Expand Down Expand Up @@ -124,14 +99,7 @@ int ApplicationMainStart(
[UIApplication setStartupDisplayMode:displayMode];
}

g_applicationProperties.width = defaultWidth;
g_applicationProperties.height = defaultHeight;
g_applicationProperties.scale = defaultScale;
g_applicationProperties.name = GetAppNameFromPList();
g_applicationProperties.isTablet = defaultTablet;

[displayMode _updateDisplaySettings];
GetCACompositor()->setTablet(g_applicationProperties.isTablet);

UIApplicationMainInit(argc, argv, principalClassName, delegateClassName, defaultOrientation);
return UIApplicationMainLoop();
Expand Down
1 change: 1 addition & 0 deletions Frameworks/UIKit/UIApplication.mm
Original file line number Diff line number Diff line change
Expand Up @@ -2578,6 +2578,7 @@ - (void)_updateDisplaySettings {
break;
}

GetCACompositor()->setTablet(_operationMode == WOCOperationModeTablet);
GetCACompositor()->setScreenSize(newWidth, newHeight, newMagnification, newRotation);
GetCACompositor()->setDeviceSize(newWidth, newHeight);

Expand Down
88 changes: 44 additions & 44 deletions Frameworks/UIKit/UIViewController.mm
Original file line number Diff line number Diff line change
Expand Up @@ -344,14 +344,7 @@ - (CGRect)_orientationRect:(UIInterfaceOrientation)orientation {
appFrame = [[UIScreen mainScreen] applicationFrame];

if (priv->_presentationStyle == UIModalPresentationFormSheet) {
CGRect screenFrame;
screenFrame = [[UIScreen mainScreen] applicationFrame];

appFrame.size.width = 540;
appFrame.size.height = 575;

appFrame.origin.x = screenFrame.origin.x + screenFrame.size.width / 2.0f - appFrame.size.width / 2.0f;
appFrame.origin.y = screenFrame.origin.y + screenFrame.size.height / 2.0f - appFrame.size.height / 2.0f;
appFrame = [self _modalPresentationFormSheetFrame];
}

CGRect rect;
Expand Down Expand Up @@ -386,6 +379,35 @@ - (CGRect)_orientationRect:(UIInterfaceOrientation)orientation {
return rect;
}

- (CGRect)_modalPresentationFormSheetFrame {
if (!GetCACompositor()->isTablet()) {
// fullscreen on non-tablets
return [[UIScreen mainScreen] applicationFrame];
}

CGRect frame;
if (!CGSizeEqualToSize(priv->_preferredContentSize, CGSizeZero)) {
frame.size = priv->_preferredContentSize;
} else {
frame.size.width = 540;
frame.size.height = 620;
}

CGRect screenFrame = [[UIScreen mainScreen] applicationFrame];
frame.origin.x = screenFrame.origin.x + screenFrame.size.width / 2.0f - frame.size.width / 2.0f;
frame.origin.y = screenFrame.origin.y + screenFrame.size.height / 2.0f - frame.size.height / 2.0f;

return frame;
}

- (BOOL)_hidesParent {
if (priv->_presentationStyle == UIModalPresentationFormSheet && GetCACompositor()->isTablet()) {
return NO;
}

return YES;
}

- (void)_setResizeToScreen:(BOOL)resize {
priv->_resizeToScreen = TRUE;
}
Expand All @@ -408,14 +430,7 @@ - (void)setOrientationInternal:(UIInterfaceOrientation)orientation animated:(BOO
appFrame = [[UIScreen mainScreen] applicationFrame];
}
if (priv->_presentationStyle == UIModalPresentationFormSheet) {
CGRect screenFrame;
screenFrame = [[UIScreen mainScreen] applicationFrame];

appFrame.size.width = GetCACompositor()->screenWidth();
appFrame.size.height = GetCACompositor()->screenHeight();

appFrame.origin.x = screenFrame.origin.x + screenFrame.size.width / 2.0f - appFrame.size.width / 2.0f;
appFrame.origin.y = screenFrame.origin.y + screenFrame.size.height / 2.0f - appFrame.size.height / 2.0f;
appFrame = [self _modalPresentationFormSheetFrame];
}

switch (orientation) {
Expand Down Expand Up @@ -787,14 +802,7 @@ - (void)loadView {

frame = [[UIScreen mainScreen] applicationFrame]; /** This is correct **/
if (priv->_presentationStyle == UIModalPresentationFormSheet) {
CGRect screenFrame;
screenFrame = [[UIScreen mainScreen] applicationFrame];

frame.size.width = 540;
frame.size.height = 575;

frame.origin.x = screenFrame.origin.x + screenFrame.size.width / 2.0f - frame.size.width / 2.0f;
frame.origin.y = screenFrame.origin.y + screenFrame.size.height / 2.0f - frame.size.height / 2.0f;
frame = [self _modalPresentationFormSheetFrame];
}

UIView* view = [[[UIEmptyController alloc] initWithFrame:frame] autorelease];
Expand All @@ -817,14 +825,7 @@ - (void)_doResizeToScreen {
}

if (priv->_presentationStyle == UIModalPresentationFormSheet) {
CGRect screenFrame;
screenFrame = [[UIScreen mainScreen] applicationFrame];

frame.size.width = 540;
frame.size.height = 575;

frame.origin.x = screenFrame.origin.x + screenFrame.size.width / 2.0f - frame.size.width / 2.0f;
frame.origin.y = screenFrame.origin.y + screenFrame.size.height / 2.0f - frame.size.height / 2.0f;
frame = [self _modalPresentationFormSheetFrame];
}
UIInterfaceOrientation curOrientation = (UIInterfaceOrientation)[[UIApplication sharedApplication] statusBarOrientation];

Expand Down Expand Up @@ -858,14 +859,7 @@ - (UIView*)view {

frame = [[UIScreen mainScreen] applicationFrame];
if (priv->_presentationStyle == UIModalPresentationFormSheet) {
CGRect screenFrame;
screenFrame = [[UIScreen mainScreen] applicationFrame];

frame.size.width = 540;
frame.size.height = 575;

frame.origin.x = screenFrame.origin.x + screenFrame.size.width / 2.0f - frame.size.width / 2.0f;
frame.origin.y = screenFrame.origin.y + screenFrame.size.height / 2.0f - frame.size.height / 2.0f;
frame = [self _modalPresentationFormSheetFrame];
}

UIView* view = [[[UIEmptyController alloc] initWithFrame:frame] autorelease];
Expand Down Expand Up @@ -1069,7 +1063,9 @@ - (void)presentViewController:(UIViewController*)controller animated:(BOOL)anima
[[UIApplication sharedApplication] beginIgnoringInteractionEvents];
}

[self _notifyViewWillDisappear:animated];
if ([controller _hidesParent]) {
[self _notifyViewWillDisappear:animated];
}

priv->_modalViewController = controller;
priv->_presentedViewController = controller;
Expand Down Expand Up @@ -1225,7 +1221,9 @@ - (void)_notifyDidAppearAnimated:(UIView*)view {
- (void)_transitionStopped:(id)context {
UIView* view = [self view];

[[priv->_parentViewController view] setHidden:TRUE];
if ([self _hidesParent]) {
[[priv->_parentViewController view] setHidden:TRUE];
}
[self _notifyDidAppearAnimated:view];
[[UIApplication sharedApplication] endIgnoringInteractionEvents];
}
Expand Down Expand Up @@ -1264,7 +1262,7 @@ - (void)_addToTop:(NSNumber*)animatedValue {
g_presentingAnimated = TRUE;
[[UIApplication sharedApplication] beginIgnoringInteractionEvents];
[self _notifyViewWillAppear:TRUE];
} else {
} else if ([self _hidesParent]) {
[[priv->_parentViewController view] setHidden:TRUE];
}

Expand Down Expand Up @@ -1331,7 +1329,9 @@ - (void)_addToTop:(NSNumber*)animatedValue {
g_presentingAnimated = FALSE;
}

[priv->_parentViewController _notifyViewDidDisappear:animated];
if ([self _hidesParent]) {
[priv->_parentViewController _notifyViewDidDisappear:animated];
}
} else {
TraceVerbose(TAG, L"Modal controller doesn't have a parent!");
}
Expand Down
58 changes: 52 additions & 6 deletions samples/WOCCatalog/WOCCatalog/ControlsViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,30 @@
#import "ControlsViewController.h"
#import "PopoverViewController.h"

#define UIPOPOVERCONTROL_ROW 4
static const int POPOVERCONTROL_ROW = 4;
static const int MODALFORMSHEET_ROW = 5;

@interface ControlsViewController ()

@property (nonatomic) BOOL resizeModal;

@end

@implementation ControlsViewController

- (void)viewDidLoad {
[super viewDidLoad];
}

- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:(BOOL)animated];

// UIModalPresentationFormSheet shouldn't cover parent with tablet.
assert(!([[self presentedViewController] modalPresentationStyle] == UIModalPresentationFormSheet && [[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad));
}

- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section {
return 5;
return 6;
}

- (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath {
Expand Down Expand Up @@ -96,26 +110,43 @@ - (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSI
cell.textLabel.text = @"UIProgressView";
cell.selectionStyle = UITableViewCellSelectionStyleNone;

} else if (indexPath.row == UIPOPOVERCONTROL_ROW) {
} else if (indexPath.row == POPOVERCONTROL_ROW) {
// UIPopoverPresentationController

cell.textLabel.text = @"UIPopupPresentationController (press me)";
cell.textLabel.text = @"UIPopupPresentationController (press to launch)";
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
} else if (indexPath.row == MODALFORMSHEET_ROW) {
// form sheet modal

UITableViewCell* subtitleCell = [tableView dequeueReusableCellWithIdentifier:@"MenuSubtitleCell"];
if (!subtitleCell) {
subtitleCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"MenuSubtitleCell"];
}

subtitleCell.textLabel.text = @"UIModalPresentationFormSheet (press to launch)";
subtitleCell.detailTextLabel.text = @"Toggle switch to resize (tablet only)";
subtitleCell.selectionStyle = UITableViewCellSelectionStyleBlue;
UISwitch* resizeSwitch = [[UISwitch alloc] init];
resizeSwitch.on = self.resizeModal;
[resizeSwitch addTarget:self action:@selector(toggleResizeModal) forControlEvents:UIControlEventValueChanged];
subtitleCell.accessoryView = resizeSwitch;

return subtitleCell;
}

return cell;
}

- (NSIndexPath*)tableView:(UITableView*)tableView willSelectRowAtIndexPath:(NSIndexPath*)indexPath {
if (indexPath.row == UIPOPOVERCONTROL_ROW) {
if (indexPath.row == POPOVERCONTROL_ROW || indexPath.row == MODALFORMSHEET_ROW) {
return indexPath;
}

return nil;
}

- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath {
if (indexPath.row == UIPOPOVERCONTROL_ROW) {
if (indexPath.row == POPOVERCONTROL_ROW) {
// Note: presenting a UIViewController with the UIModalPresentationStyle of Popover does not yet present the view controller
// as a popover. UIPopoverController has been depricated in ios9.0.
// This work will need to be completed as part of UIPopoverPresentationController work.
Expand All @@ -133,7 +164,22 @@ - (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)
presentationController.permittedArrowDirections = UIPopoverArrowDirectionLeft | UIPopoverArrowDirectionRight;
presentationController.sourceView = self.view;
presentationController.sourceRect = CGRectMake(100, 100, 100, 100);
} else if (indexPath.row == MODALFORMSHEET_ROW) {
UIViewController* viewController = [[PopoverViewController alloc] initWithImage:[UIImage imageNamed:@"photo1.jpg"]];
viewController.modalPresentationStyle = UIModalPresentationFormSheet;

if (self.resizeModal) {
viewController.preferredContentSize = CGSizeMake(200, 200);
}

[self presentViewController:viewController animated:YES completion:^{
assert(!([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad && self.view.hidden));
}];
}
}

- (void)toggleResizeModal {
self.resizeModal = !self.resizeModal;
}

@end
14 changes: 14 additions & 0 deletions samples/WOCCatalog/WOCCatalog/DisplayModeViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ - (void)toggleAdjustWindowSize:(UISwitch*)sender {
[UIApplication.displayMode updateDisplaySettings];
}

- (void)toggleTabletMode:(UISwitch*)sender {
UIApplication.displayMode.operationMode = sender.on ? WOCOperationModeTablet : WOCOperationModePhone;
[UIApplication.displayMode updateDisplaySettings];
}

- (void)toggleScreenAwake:(UISwitch*)sender {
[[UIApplication sharedApplication] setIdleTimerDisabled:sender.on];
}
Expand Down Expand Up @@ -262,6 +267,15 @@ - (id)init {
cell.textLabel.text = @"Disable idle screen timer";
[self.rows addObject:cell];

UISwitch* enableTabletMode = [UISwitch new];
enableTabletMode.on = [[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad;
[enableTabletMode addTarget:self action:@selector(toggleTabletMode:) forControlEvents:UIControlEventValueChanged];

cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MenuCell"];
cell.accessoryView = enableTabletMode;
cell.textLabel.text = @"Tablet Mode";
[self.rows addObject:cell];

fixedWidth = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 100, 20)];
fixedWidth.text = [NSString stringWithFormat:@"%.1f", UIApplication.displayMode.fixedWidth];
fixedWidth.borderStyle = UITextBorderStyleBezel;
Expand Down
4 changes: 3 additions & 1 deletion samples/WOCCatalog/WOCCatalog/PopoverViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,12 @@ - (void)viewDidLoad {
scrollView.maximumZoomScale = 1.;
scrollView.minimumZoomScale = minimumScale;
scrollView.zoomScale = minimumScale;

imageView.center = CGPointMake(scrollView.contentSize.width * 0.5, scrollView.contentSize.height * 0.5);
}

- (void)createLayout {
[scrollView setBackgroundColor:[UIColor whiteColor]];
[scrollView setBackgroundColor:[UIColor lightGrayColor]];
}

- (void)scrollViewDidZoom:(UIScrollView*)aScrollView {
Expand Down

0 comments on commit 8b10200

Please sign in to comment.