Skip to content

Commit

Permalink
Cleaning up UIWindow initialization and usage (#2445)
Browse files Browse the repository at this point in the history
* Cleaning up UIWindow initialization and usage

- Non storyboard apps are expected to create their own UIWindow and makeKeyAndVisible
- Storyboard apps get a UIWindow that they need to makeKeyAndVisible
- Fixing up annotations on UIWindow and UIApplication
- Removed a lot of dead code from UIApplication, including two UIWindows that we don't need/use.
- Switched UIWindow.mm and UIApplication.mm to ARC

Fixes #1836: Apps initialized with a storyboard don't get a main window (they get the 'popup window').

* s

* Fixing up annotations; using NOTINPLAN_* macros to headers where applicable.

* Added functional tests for UIApplicationDelegate.  Also cleaning up a bit more of the app launch path.

* Removing accidentally-added file.

* Incorporating CR feedback.
  • Loading branch information
jaredhms authored Apr 14, 2017
1 parent d8fc28e commit dcded83
Show file tree
Hide file tree
Showing 36 changed files with 853 additions and 1,353 deletions.
4 changes: 2 additions & 2 deletions Frameworks/AVFoundation/AVAudioPlayer.mm
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ - (instancetype)init {
[weakSelf _handleMediaElementMediaFailed:sender args:e];
});

// We utilize the internal popup UIWindow, just to make sure we're always in the Xaml scene graph.
[[[UIApplication sharedApplication] _popupWindow] addSubview:_hiddenView];
// Add ourselves to the keyWindow even though we're hidden, just to make sure we're always in the Xaml scene graph.
[[[UIApplication sharedApplication] keyWindow] addSubview:_hiddenView];
}

return self;
Expand Down
70 changes: 0 additions & 70 deletions Frameworks/UIKit/StarboardXaml/ApplicationCompositor.cpp

This file was deleted.

32 changes: 0 additions & 32 deletions Frameworks/UIKit/StarboardXaml/ApplicationCompositor.h

This file was deleted.

11 changes: 2 additions & 9 deletions Frameworks/UIKit/StarboardXaml/ApplicationMain.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,5 @@
#include "StarboardXaml.h"
#include <inspectable.h>

// IOS application main startup path
extern "C" int ApplicationMainStart(const char* principalName,
const char* delegateName,
float windowWidth,
float windowHeight,
ActivationType activationType,
IInspectable* activationArg);

void SetTemporaryFolder(const wchar_t* folder);
// ObjectiveC application entry point to be called after initial WinRT app launch/activation
void RunApplicationMain(const char* principalName, const char* delegateName, ActivationType activationType, IInspectable* activationArg);
54 changes: 31 additions & 23 deletions Frameworks/UIKit/StarboardXaml/ApplicationMain.mm
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#import "ApplicationMain.h"
#import <windows.foundation.h>
#import <windows.applicationmodel.activation.h>
#import "winrt/Windows.Storage.h"
#import <winrt/Windows.UI.Xaml.h>
#include <COMIncludes_End.h>

Expand All @@ -38,7 +39,6 @@
#import <StringHelpers.h>
#import <CollectionHelpers.h>
#import <UIInterface.h>
#import <CACompositorClient.h>
#import <UIApplicationInternal.h>
#import <MainDispatcher.h>
#import <_UIPopupViewController.h>
Expand All @@ -47,18 +47,31 @@
using namespace winrt::Windows::UI::Xaml;
namespace WF = winrt::Windows::Foundation;

static CACompositorClientInterface* _compositorClient = NULL;
void _SetTemporaryFolder(const wchar_t* folder) {
NSSetTemporaryDirectory([NSString stringWithCharacters:reinterpret_cast<const unichar*>(folder) length:wcslen(folder)]);
}

void _InitializeApp() {
// Only init once.
// No lock needed as this is only called from the UI thread.
static bool initialized = false;
if (initialized) {
return;
}
initialized = true;

void SetCACompositorClient(CACompositorClientInterface* client) {
_compositorClient = client;
// Set our writable and temp folders
IwSetWritableFolder(winrt::Windows::Storage::ApplicationData::Current().LocalFolder().Path().data());
_SetTemporaryFolder(winrt::Windows::Storage::ApplicationData::Current().TemporaryFolder().Path().data());

// Set the waiter routine for the main runloop to yield
SetupMainRunLoopTimedMultipleWaiter();
}

int ApplicationMainStart(const char* principalName,
const char* delegateName,
float windowWidth,
float windowHeight,
ActivationType activationType,
IInspectable* activationArg) {
void RunApplicationMain(const char* principalName, const char* delegateName, ActivationType activationType, IInspectable* activationArg) {
// Perform initialization
_InitializeApp();

// Note: We must use nil rather than an empty string for these class names
NSString* principalClassName = Strings::IsEmpty<const char*>(principalName) ? nil : [[NSString alloc] initWithCString:principalName];
NSString* delegateClassName = Strings::IsEmpty<const char*>(delegateName) ? nil : [[NSString alloc] initWithCString:delegateName];
Expand All @@ -81,8 +94,8 @@ int ApplicationMainStart(const char* principalName,
THROW_NS_IF_FAILED(Collections::WRLToNSCollection(map, &userInput));

NSDictionary* toastAction = @{
UIApplicationLaunchOptionsToastActionArgumentKey : toastArgument.argument,
UIApplicationLaunchOptionsToastActionUserInputKey : userInput
UIApplicationLaunchOptionsToastActionArgumentKey:toastArgument.argument,
UIApplicationLaunchOptionsToastActionUserInputKey:userInput
};
activationArgument = toastAction;
} else if (activationType == ActivationTypeVoiceCommand) {
Expand All @@ -96,18 +109,20 @@ int ApplicationMainStart(const char* principalName,
activationArgument = activatedEventArgs;
}

// Grab window dimensions from CPP/WinRT and initialize our display accordingly
auto windowBounds = winrt::Windows::UI::Xaml::Window::Current().Bounds();
WOCDisplayMode* displayMode = [UIApplication displayMode];
[displayMode _setWindowSize:CGSizeMake(windowWidth, windowHeight)];
[displayMode _setWindowSize:CGSizeMake(windowBounds.Width, windowBounds.Height)];

if (activationType == ActivationTypeLibrary) {
// In library mode, honor app's native display size
[displayMode setDisplayPreset:WOCDisplayPresetNative];
}

NSDictionary* infoDict = [[NSBundle mainBundle] infoDictionary];

// Figure out what our initial default orientation should be from Info.plist
// TODO: #2438 UIApplication/UIWindow/UIScreen/UIDevice orientation/rotation needs some work
UIInterfaceOrientation defaultOrientation = UIInterfaceOrientationUnknown;
NSDictionary* infoDict = [[NSBundle mainBundle] infoDictionary];
if (infoDict != nil) {
defaultOrientation = EbrGetWantedOrientation();

Expand Down Expand Up @@ -156,7 +171,7 @@ int ApplicationMainStart(const char* principalName,

[displayMode _updateDisplaySettings];

UIApplicationMainInit(principalClassName, delegateClassName, defaultOrientation, (int)activationType, activationArgument);
_UIApplicationMainInit(principalClassName, delegateClassName, (int)activationType, activationArgument);

if (activationType == ActivationTypeLibrary) {
// Create a top-level UIWindow with popup view controller, which will not normally be visible.
Expand All @@ -174,12 +189,5 @@ int ApplicationMainStart(const char* principalName,
// The apps can (and do) handle application launch delegate from the first tine NSRunloop run is called, and that can
// take a REALLY long time to complete, causing PLM to terminate us. We were getting lucky in some cases and the app would launch.
// This will also fix the number of sporadic launch test failures that we have seen in the labs.

ScheduleMainRunLoopAsync();

return 0;
}

void SetTemporaryFolder(const wchar_t* folder) {
NSSetTemporaryDirectory([NSString stringWithCharacters:reinterpret_cast<const unichar*>(folder) length:wcslen(folder)]);
}
10 changes: 6 additions & 4 deletions Frameworks/UIKit/StarboardXaml/StarboardXaml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
#include "ApplicationMain.h"
#include "StringConversion.h"
#include "Windows.UI.Xaml.h"

#include "ApplicationCompositor.h"
#include "XamlCompositor.h"
#include "StringHelpers.h"
#include <LoggingNative.h>
Expand Down Expand Up @@ -238,8 +236,12 @@ void DoApplicationLaunch(ActivationType activationType, Platform::Object^ activa
Xaml::Window::Current->Activate();
}

auto startupRect = Xaml::Window::Current->Bounds;
RunApplicationMain(g_principalClassName, g_delegateClassName, startupRect.Width, startupRect.Height, activationType, activationArg);
// Convert Object^ to IInspectable* so it can be passed into Objective C
RunApplicationMain(
Strings::WideToNarrow(g_principalClassName->Data()).c_str(),
Strings::WideToNarrow(g_delegateClassName->Data()).c_str(),
activationType,
reinterpret_cast<IInspectable*>(activationArg));

_appEvents = ref new AppEventListener();
_appEvents->_RegisterEventHandlers();
Expand Down
1 change: 0 additions & 1 deletion Frameworks/UIKit/StarboardXaml/XamlCompositor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
#include <collection.h>
#include <assert.h>

#include "ApplicationCompositor.h"
#include <StringHelpers.h>

#include "DisplayProperties.h"
Expand Down
5 changes: 3 additions & 2 deletions Frameworks/UIKit/UIAlertView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -440,8 +440,9 @@ - (BOOL)isVisible {
curHeight += 10.0f;
}

UIView* popupWindow = [[UIApplication sharedApplication] _popupLayer];
[[popupWindow superview] bringSubviewToFront:popupWindow];
// Grab the _popupWindow
// TODO: Switch this over to a Xaml ContentDialog so we don't need the extra _popupWindow
UIView* popupWindow = (UIView*)[[UIApplication sharedApplication] _popupWindow];

CGRect fullScreen;
fullScreen = [popupWindow bounds];
Expand Down
Loading

0 comments on commit dcded83

Please sign in to comment.