Skip to content

Commit e3d62d3

Browse files
committed
Make all UIViewControllers load XAML resource if the XAML type is present
CHANGES ======= 1. Removed initWithXAMLPage and initWithXAMLClass; initWithNibName will load the XAML resource over the NIB if applicable 2. Renamed UIEmptyController to UIEmptyView TESTS ===== 1. Unit tests - PASSED 2. XAMLTest verification - desktop
1 parent 363f64f commit e3d62d3

File tree

9 files changed

+73
-120
lines changed

9 files changed

+73
-120
lines changed

Frameworks/UIKit/StarboardXaml/StarboardXaml.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@ void AppEventListener::_RegisterEventHandlers() {
6363
Windows::UI::Xaml::Application::Current->Resuming += ref new Windows::Foundation::EventHandler<Platform::Object^>(this, &AppEventListener::_OnResuming);
6464

6565
// Register for Window Visibility change event.
66-
// TODO::
67-
// todo-nithishm-03072016 - Move this out of the Windows Visibility event in future.
66+
// TODO: Move this out of the Windows Visibility event in future
6867
Xaml::Window::Current->VisibilityChanged += ref new Xaml::WindowVisibilityChangedEventHandler(this, &AppEventListener::_OnAppVisibilityChanged);
68+
6969
// Register for Application Memory Usage Increase event.
7070
MemoryManager::AppMemoryUsageIncreased += ref new Windows::Foundation::EventHandler<Platform::Object^>(this, &AppEventListener::_OnAppMemoryUsageChanged);
7171
}

Frameworks/UIKit/UIApplication.mm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
#include "UWP/WindowsSystemDisplay.h"
4646
#include "UrlLauncher.h"
4747

48-
#include "UIEmptyController.h"
48+
#include "UIEmptyView.h"
4949

5050
#include "CACompositor.h"
5151
#include "UIInterface.h"
@@ -1426,7 +1426,7 @@ static void evaluateKeyboard(id self) {
14261426

14271427
if (_blankView == nil) {
14281428
CGRect frame = { 0 };
1429-
_blankView = [[UIEmptyController alloc] initWithFrame:frame];
1429+
_blankView = [[UIEmptyView alloc] initWithFrame:frame];
14301430
[_blankView setBackgroundColor:[UIColor blackColor]];
14311431
[_blankView setAutoresizesSubviews:FALSE];
14321432
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616

1717
#pragma once
1818

19-
#include "UIKit/UIView.h"
19+
#import "UIKit/UIView.h"
2020

21-
@interface UIEmptyController : UIView {
21+
@interface UIEmptyView : UIView {
2222
@public
2323
}
2424

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
//
1515
//******************************************************************************
1616

17-
#include "Starboard.h"
18-
#include "UIEmptyController.h"
17+
#import "Starboard.h"
18+
#import "UIEmptyView.h"
1919

20-
@implementation UIEmptyController
20+
@implementation UIEmptyView
2121

2222
@end

Frameworks/UIKit/UIViewController.mm

Lines changed: 57 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,7 @@
3838
#import "UIViewControllerInternal.h"
3939
#import "UIApplicationInternal.h"
4040

41-
// TODO: Rename UIEmptyController to UIEmptyView
42-
#import "UIEmptyController.h"
43-
41+
#import "UIEmptyView.h"
4442
#import "UIViewInternal.h"
4543
#import "UIViewControllerInternal.h"
4644
#import "UIStoryboardInternal.h"
@@ -51,6 +49,8 @@
5149
#import "UWP/WindowsUIXaml.h"
5250
#import "UWP/WindowsFoundation.h"
5351

52+
#import "StringHelpers.h"
53+
5454
#include "COMIncludes.h"
5555
#include "WinString.h"
5656
#include "RoApi.h"
@@ -695,7 +695,7 @@ - (instancetype)initWithCoder:(NSCoder*)coder {
695695
}
696696

697697
priv->_childViewControllers = [coder decodeObjectForKey:@"UIChildViewControllers"];
698-
// assert(nibName != nil);
698+
699699
priv->_edgesForExtendedLayout = [coder decodeIntForKey:@"UIKeyEdgesForExtendedLayout"];
700700

701701
return self;
@@ -762,30 +762,33 @@ - (NSBundle*)nibBundle {
762762
@Status Interoperable
763763
*/
764764
- (instancetype)initWithNibName:(NSString*)strNib bundle:(NSBundle*)bundle {
765-
if (!priv) {
766-
priv = (UIViewControllerPriv*)IwCalloc(1, sizeof(UIViewControllerPriv));
767-
priv->_curOrientation = UIInterfaceOrientationPortrait;
768-
priv->_contentSizeForViewInPopover.width = 320.0f;
769-
priv->_contentSizeForViewInPopover.height = 1100.0f;
770-
}
765+
if (self = [super init]) {
766+
if (!priv) {
767+
priv = (UIViewControllerPriv*)IwCalloc(1, sizeof(UIViewControllerPriv));
768+
priv->_curOrientation = UIInterfaceOrientationPortrait;
769+
priv->_contentSizeForViewInPopover.width = 320.0f;
770+
priv->_contentSizeForViewInPopover.height = 1100.0f;
771+
}
771772

772-
if (bundle == nil) {
773-
priv->nibBundle = [NSBundle mainBundle];
774-
} else {
775-
priv->nibBundle = bundle;
776-
}
773+
if (bundle == nil) {
774+
priv->nibBundle = [NSBundle mainBundle];
775+
} else {
776+
priv->nibBundle = bundle;
777+
}
777778

778-
if ([strNib length] > 0) {
779-
priv->nibName = [strNib copy];
780-
} else {
781-
priv->nibName = nil;
782-
}
779+
if ([strNib length] > 0) {
780+
priv->nibName.attach([strNib copy]);
781+
} else {
782+
priv->nibName = nil;
783+
}
783784

784-
// TODO: Check to see if we have a XAML class file locally in the project
785-
// 1. Find local XAML files (*.xaml.cpp) - assume the filename as the class name
786-
// 2. Set priv->_xamlClass = filename (e.g. <ClassName>.xaml.cpp)
787-
// 3. Set priv->_xamlPage = filename (NOT SURE IF PAGE WILL WORK WITHOUT THE CODE-BEHIND METHODS)
788-
// 4. Remove initWithXAMLClass and initWithXAMLPage
785+
// Load the XAML version if found. First prepend the auto-generated XAML namespace to the XAML class name
786+
NSString* xamlClassName = [NSString stringWithFormat:@"IslandwoodAutoGenNamespace.%@", strNib];
787+
auto xamlType = [self _returnXamlType:xamlClassName];
788+
if (xamlType.Get() != nullptr) {
789+
priv->_xamlClassName = xamlClassName;
790+
}
791+
}
789792

790793
return self;
791794
}
@@ -794,25 +797,9 @@ - (instancetype)initWithNibName:(NSString*)strNib bundle:(NSBundle*)bundle {
794797
@Status Interoperable
795798
*/
796799
- (instancetype)init {
797-
[super init];
798-
799800
return [self initWithNibName:nil bundle:nil];
800801
}
801802

802-
- (instancetype)initWithXAMLClass:(NSString*)className {
803-
self = [self initWithNibName:nil bundle:nil];
804-
priv->_xamlClassName = className;
805-
806-
return self;
807-
}
808-
809-
- (instancetype)initWithXAMLPage:(NSString*)pageName {
810-
self = [self initWithNibName:nil bundle:nil];
811-
priv->_xamlPageName = pageName;
812-
813-
return self;
814-
}
815-
816803
/**
817804
@Status Interoperable
818805
*/
@@ -821,7 +808,7 @@ - (void)loadView {
821808
return;
822809
}
823810

824-
if (priv->_xamlClassName != nil || priv->_xamlPageName != nil) {
811+
if (priv->_xamlClassName != nil) {
825812
[self _loadXamlPage];
826813
return;
827814
}
@@ -919,7 +906,7 @@ - (void)loadView {
919906
frame = [self _modalPresentationFormSheetFrame];
920907
}
921908

922-
UIView* view = [[[UIEmptyController alloc] initWithFrame:frame] autorelease];
909+
UIView* view = [[[UIEmptyView alloc] initWithFrame:frame] autorelease];
923910
[view setAutoresizingMask:UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth];
924911
[self setView:view];
925912
}
@@ -942,20 +929,6 @@ - (void)_doResizeToScreen {
942929
frame = [self _modalPresentationFormSheetFrame];
943930
}
944931

945-
/*
946-
// TODO: Remove if this is no longer applicable
947-
UIInterfaceOrientation curOrientation = (UIInterfaceOrientation)[[UIApplication sharedApplication] statusBarOrientation];
948-
switch (curOrientation) {
949-
case UIInterfaceOrientationLandscapeLeft:
950-
case UIInterfaceOrientationLandscapeRight: {
951-
float temp = frame.size.width;
952-
frame.size.width = frame.size.height;
953-
frame.size.height = temp;
954-
}
955-
break;
956-
}
957-
*/
958-
959932
[priv->view setFrame:frame];
960933
}
961934
}
@@ -978,7 +951,7 @@ - (UIView*)view {
978951
frame = [self _modalPresentationFormSheetFrame];
979952
}
980953

981-
UIView* view = [[[UIEmptyController alloc] initWithFrame:frame] autorelease];
954+
UIView* view = [[[UIEmptyView alloc] initWithFrame:frame] autorelease];
982955
[self setView:view];
983956
}
984957
}
@@ -1804,8 +1777,23 @@ - (BOOL)isEditing {
18041777
return priv->_isEditing;
18051778
}
18061779

1780+
- (Microsoft::WRL::ComPtr<ABI::Windows::UI::Xaml::Markup::IXamlType>)_returnXamlType:(NSString*)xamlClassName {
1781+
Microsoft::WRL::ComPtr<ABI::Windows::UI::Xaml::Markup::IXamlMetadataProvider> xamlMetaProvider = nullptr;
1782+
auto inspApplicationCurrent = [WXApplication.current comObj];
1783+
inspApplicationCurrent.As(&xamlMetaProvider);
1784+
1785+
auto hClassName = Strings::NarrowToWide<HSTRING>(xamlClassName);
1786+
Microsoft::WRL::ComPtr<ABI::Windows::UI::Xaml::Markup::IXamlType> xamlType = nullptr;
1787+
HRESULT hr = xamlMetaProvider->GetXamlTypeByFullName(hClassName.Get(), xamlType.GetAddressOf());
1788+
if (SUCCEEDED(hr) && xamlType.Get() != nullptr) {
1789+
return xamlType;
1790+
}
1791+
1792+
return nullptr;
1793+
}
1794+
18071795
- (void)_loadXamlPage {
1808-
if (priv->_xamlClassName == nil && priv->_xamlPageName == nil) {
1796+
if (priv->_xamlClassName == nil) {
18091797
return;
18101798
}
18111799

@@ -1815,50 +1803,20 @@ - (void)_loadXamlPage {
18151803
}
18161804

18171805
// Create a template UIView
1818-
UIView* view = [[[UIEmptyController alloc] initWithFrame:frame] autorelease];
1806+
UIView* view = [[[UIEmptyView alloc] initWithFrame:frame] autorelease];
18191807
[view setAutoresizingMask:UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth];
18201808
[self setView:view];
18211809

1822-
// If we find a XAML class or page, attempt to load it
1823-
if (priv->_xamlClassName != nil) {
1824-
Microsoft::WRL::ComPtr<IInspectable> pageObj;
1825-
Microsoft::WRL::ComPtr<ABI::Windows::UI::Xaml::Markup::IXamlType> xamlType;
1826-
Microsoft::WRL::ComPtr<ABI::Windows::UI::Xaml::Markup::IXamlMetadataProvider> xamlMeta;
1827-
Microsoft::WRL::ComPtr<IInspectable> insp = [WXApplication.current comObj];
1828-
insp.As(&xamlMeta);
1829-
1830-
uint32_t classNameLen = [priv->_xamlClassName length];
1831-
unichar* className = (unichar*)IwMalloc(sizeof(unichar) * (classNameLen + 1));
1832-
auto freeClassName = wil::ScopeExit([&]() {
1833-
IwFree(className);
1834-
});
1835-
1836-
HSTRING hClassName;
1837-
auto freeHClassName = wil::ScopeExit([&]() {
1838-
WindowsDeleteString(hClassName);
1839-
});
1840-
1841-
// Copy the string into className and null terminate it
1842-
[priv->_xamlClassName getCharacters:className];
1843-
className[classNameLen] = '\0';
1844-
1845-
WindowsCreateString((LPCWSTR)className, classNameLen, &hClassName);
1846-
xamlMeta->GetXamlTypeByFullName(hClassName, &xamlType);
1847-
xamlType->ActivateInstance(&pageObj);
1848-
1849-
priv->_page = (WXCPage*)NSAllocateObject([WXCPage class], 0, NULL);
1850-
[priv->_page setComObj:pageObj];
1851-
} else {
1852-
// TODO: If we load a XAML page, are we bypassing the code-gen methods to trigger events (e.g. *.xaml.cpp) and forward them to an
1853-
// UIView-class object
1854-
priv->_page = [WXCPage make];
1855-
WFUri* location = [WFUri makeUri:[NSString stringWithFormat:@"ms-appx:///%@", (NSString*)priv->_xamlPageName]];
1856-
[WXApplication loadComponentWithResourceLocation:priv->_page
1857-
resourceLocator:location
1858-
componentResourceLocation:WUXCPComponentResourceLocationApplication];
1859-
}
1810+
// Activate an instance of the XAML class and its page
1811+
Microsoft::WRL::ComPtr<IInspectable> pageObj = nullptr;
1812+
auto xamlType = [self _returnXamlType:priv->_xamlClassName];
1813+
xamlType->ActivateInstance(pageObj.GetAddressOf());
1814+
1815+
priv->_page = (WXCPage*)NSAllocateObject([WXCPage class], 0, NULL);
1816+
[priv->_page setComObj:pageObj];
18601817

1861-
unsigned int propListCount;
1818+
// Walk the list of outlets and assign them to the corresponding XAML UIElement
1819+
unsigned int propListCount = 0;
18621820
objc_property_t* props = (objc_property_t*)class_copyPropertyList([self class], &propListCount);
18631821
auto freeProps = wil::ScopeExit([&]() {
18641822
IwFree(props);

Frameworks/include/UIViewControllerInternal.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ struct UIViewControllerPriv {
7171

7272
idretaintype(WXCPage) _page;
7373
idretaintype(NSString) _xamlClassName;
74-
idretaintype(NSString) _xamlPageName;
7574
};
7675

7776
@interface _UIViewControllerContextTransitioning : NSObject <UIViewControllerContextTransitioning>

build/UIKit/lib/UIKitLib.vcxproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
<ClangCompile Include="$(MSBuildThisFileDirectory)..\..\..\Frameworks\UIKit\UIControl.mm" />
4545
<ClangCompile Include="$(MSBuildThisFileDirectory)..\..\..\Frameworks\UIKit\UIDevice.mm" />
4646
<ClangCompile Include="$(MSBuildThisFileDirectory)..\..\..\Frameworks\UIKit\UIDocument.mm" />
47-
<ClangCompile Include="$(MSBuildThisFileDirectory)..\..\..\Frameworks\UIKit\UIEmptyController.mm" />
47+
<ClangCompile Include="$(MSBuildThisFileDirectory)..\..\..\Frameworks\UIKit\UIEmptyView.mm" />
4848
<ClangCompile Include="$(MSBuildThisFileDirectory)..\..\..\Frameworks\UIKit\UIEvent.mm" />
4949
<ClangCompile Include="$(MSBuildThisFileDirectory)..\..\..\Frameworks\UIKit\UIFont.mm" />
5050
<ClangCompile Include="$(MSBuildThisFileDirectory)..\..\..\Frameworks\UIKit\UIFontDescriptor.mm" />
@@ -124,7 +124,7 @@
124124
<ClangCompile Include="$(MSBuildThisFileDirectory)..\..\..\Frameworks\UIKit\UITextView.mm" />
125125
<ClangCompile Include="$(MSBuildThisFileDirectory)..\..\..\Frameworks\UIKit\UIToolbar.mm">
126126
<ObjectiveCARC>true</ObjectiveCARC>
127-
</ClangCompile>
127+
</ClangCompile>
128128
<ClangCompile Include="$(MSBuildThisFileDirectory)..\..\..\Frameworks\UIKit\UIWebView.mm" />
129129
<ClangCompile Include="$(MSBuildThisFileDirectory)..\..\..\Frameworks\UIKit\UISwipeGestureRecognizer.mm" />
130130
<ClangCompile Include="$(MSBuildThisFileDirectory)..\..\..\Frameworks\UIKit\UICollectionView.mm">
@@ -295,7 +295,7 @@
295295
<ItemGroup>
296296
<ClInclude Include="$(MSBuildThisFileDirectory)..\..\..\Frameworks\UIKit\UIAnimationNotification.h" />
297297
<ClInclude Include="$(MSBuildThisFileDirectory)..\..\..\Frameworks\UIKit\UIDeviceInternal.h" />
298-
<ClInclude Include="$(MSBuildThisFileDirectory)..\..\..\Frameworks\UIKit\UIEmptyController.h" />
298+
<ClInclude Include="$(MSBuildThisFileDirectory)..\..\..\Frameworks\UIKit\UIEmptyView.h" />
299299
<ClInclude Include="$(MSBuildThisFileDirectory)..\..\..\Frameworks\UIKit\UIImageCachedObject.h" />
300300
<ClInclude Include="$(MSBuildThisFileDirectory)..\..\..\Frameworks\UIKit\UINavigationControllerInternal.h" />
301301
<ClInclude Include="$(MSBuildThisFileDirectory)..\..\..\Frameworks\UIKit\UIProxyObject.h" />

include/UIKit/UIViewController.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
#pragma once
3333

34-
#include <UIKit/UIKitExport.h>
34+
#import <UIKit/UIKitExport.h>
3535
#import <UIKit/UIAppearanceContainer.h>
3636
#import <UIKit/UIContentContainer.h>
3737
#import <UIKit/UIFocusEnvironment.h>
@@ -173,9 +173,7 @@ UIKIT_EXPORT_CLASS
173173
- (id<UIViewControllerPreviewing>)registerForPreviewingWithDelegate:(id<UIViewControllerPreviewingDelegate>)delegate
174174
sourceView:(UIView*)sourceView STUB_METHOD;
175175
- (id<UIViewControllerTransitionCoordinator>)transitionCoordinator STUB_METHOD;
176-
- (instancetype)initWithNibName:(NSString*)nibName bundle:(NSBundle*)nibBundle;
177-
- (instancetype)initWithXAMLClass:(NSString*)className;
178-
- (instancetype)initWithXAMLPage:(NSString*)pageName;
176+
-(instancetype)initWithNibName:(NSString*)nibName bundle:(NSBundle*)nibBundle NS_DESIGNATED_INITIALIZER;
179177
- (void)addChildViewController:(UIViewController*)childController;
180178
- (void)addKeyCommand:(UIKeyCommand*)keyCommand STUB_METHOD;
181179
- (void)applicationFinishedRestoringState STUB_METHOD;

samples/XAMLTest/XamlTest/AppDelegate.m

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,8 @@ - (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(N
2626
// Override point for customization after application launch.
2727
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
2828

29-
// Load the view from XAML instead of XIB file.
30-
//MainViewController* viewController = [[MainViewController alloc] initWithNibName:@"MainViewController" bundle:nil];
31-
//MainViewController *viewController = [[MainViewController alloc] initWithXAMLPage:@"MainViewController.xaml"];
32-
MainViewController *viewController = [[MainViewController alloc] initWithXAMLClass:@"IslandwoodAutoGenNamespace.MainViewController"];
29+
// Re-use the designated initializer and attempt to load the XAML page if the XAML type based on the class name is present
30+
MainViewController* viewController = [[MainViewController alloc] initWithNibName:@"MainViewController" bundle:nil];
3331

3432
self.window.rootViewController = viewController;
3533
[self.window makeKeyAndVisible];

0 commit comments

Comments
 (0)