forked from dotnet/maui-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0c3ebd1
commit 9710309
Showing
14 changed files
with
547 additions
and
348 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 8 additions & 9 deletions
17
8.0/PlatformIntegration/NativeEmbeddingDemo/NativeEmbeddingDemo.MacCatalyst/AppDelegate.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,14 @@ | ||
namespace NativeEmbeddingDemo.MacCatalyst | ||
namespace NativeEmbeddingDemo.MacCatalyst; | ||
|
||
[Register("AppDelegate")] | ||
public class AppDelegate : UIApplicationDelegate | ||
{ | ||
[Register("AppDelegate")] | ||
public class AppDelegate : UIApplicationDelegate | ||
{ | ||
public override UIWindow? Window { get; set; } | ||
public override UIWindow? Window { get; set; } | ||
|
||
public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions) => true; | ||
public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions) => true; | ||
|
||
public override UISceneConfiguration GetConfiguration(UIApplication application, UISceneSession connectingSceneSession, UISceneConnectionOptions options) => | ||
Enumerable.FirstOrDefault<NSUserActivity>(options.UserActivities)?.ActivityType == "NewTaskWindow" | ||
public override UISceneConfiguration GetConfiguration(UIApplication application, UISceneSession connectingSceneSession, UISceneConnectionOptions options) => | ||
Enumerable.FirstOrDefault<NSUserActivity>(options.UserActivities)?.ActivityType == "NewTaskWindow" | ||
? new UISceneConfiguration("New Task Configuration", UIWindowSceneSessionRole.Application) | ||
: new UISceneConfiguration("Default Configuration", UIWindowSceneSessionRole.Application); | ||
} | ||
} |
193 changes: 96 additions & 97 deletions
193
...formIntegration/NativeEmbeddingDemo/NativeEmbeddingDemo.MacCatalyst/MainViewController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,132 +1,131 @@ | ||
using Microsoft.Maui.Platform; | ||
|
||
namespace NativeEmbeddingDemo.MacCatalyst | ||
namespace NativeEmbeddingDemo.MacCatalyst; | ||
|
||
public class MainViewController : UIViewController | ||
{ | ||
public class MainViewController : UIViewController | ||
{ | ||
UIWindow GetWindow() => | ||
View?.Window ?? | ||
ParentViewController?.View?.Window ?? | ||
MainViewController.MauiApp.Value.Services.GetRequiredService<IUIApplicationDelegate>().GetWindow() ?? | ||
UIApplication.SharedApplication.Delegate.GetWindow(); | ||
UIWindow GetWindow() => | ||
View?.Window ?? | ||
ParentViewController?.View?.Window ?? | ||
MainViewController.MauiApp.Value.Services.GetRequiredService<IUIApplicationDelegate>().GetWindow() ?? | ||
UIApplication.SharedApplication.Delegate.GetWindow(); | ||
|
||
public static readonly Lazy<MauiApp> MauiApp = new(() => | ||
public static readonly Lazy<MauiApp> MauiApp = new(() => | ||
{ | ||
var mauiApp = MauiProgram.CreateMauiApp(builder => | ||
{ | ||
var mauiApp = MauiProgram.CreateMauiApp(builder => | ||
{ | ||
builder.UseMauiEmbedding(); | ||
}); | ||
return mauiApp; | ||
builder.UseMauiEmbedding(); | ||
}); | ||
return mauiApp; | ||
}); | ||
|
||
public static bool UseWindowContext = true; | ||
public static bool UseWindowContext = true; | ||
|
||
MyMauiContent? mauiView; | ||
MyMauiContent? mauiView; | ||
|
||
public override void ViewDidLoad() | ||
{ | ||
base.ViewDidLoad(); | ||
public override void ViewDidLoad() | ||
{ | ||
base.ViewDidLoad(); | ||
|
||
Title = "Main view controller"; | ||
Title = "Main view controller"; | ||
|
||
View!.BackgroundColor = UIColor.SystemBackground; | ||
View!.BackgroundColor = UIColor.SystemBackground; | ||
|
||
var stackView = new UIStackView | ||
{ | ||
Axis = UILayoutConstraintAxis.Vertical, | ||
Alignment = UIStackViewAlignment.Fill, | ||
Distribution = UIStackViewDistribution.Fill, | ||
Spacing = 8, | ||
TranslatesAutoresizingMaskIntoConstraints = false | ||
}; | ||
View.AddSubview(stackView); | ||
|
||
NSLayoutConstraint.ActivateConstraints(new[] | ||
{ | ||
stackView.TopAnchor.ConstraintEqualTo(View.SafeAreaLayoutGuide.TopAnchor, 20), | ||
stackView.LeadingAnchor.ConstraintEqualTo(View.SafeAreaLayoutGuide.LeadingAnchor, 20), | ||
stackView.TrailingAnchor.ConstraintEqualTo(View.SafeAreaLayoutGuide.TrailingAnchor, -20), | ||
stackView.BottomAnchor.ConstraintLessThanOrEqualTo(View.SafeAreaLayoutGuide.BottomAnchor, -20) | ||
}); | ||
var stackView = new UIStackView | ||
{ | ||
Axis = UILayoutConstraintAxis.Vertical, | ||
Alignment = UIStackViewAlignment.Fill, | ||
Distribution = UIStackViewDistribution.Fill, | ||
Spacing = 8, | ||
TranslatesAutoresizingMaskIntoConstraints = false | ||
}; | ||
View.AddSubview(stackView); | ||
|
||
NSLayoutConstraint.ActivateConstraints(new[] | ||
{ | ||
stackView.TopAnchor.ConstraintEqualTo(View.SafeAreaLayoutGuide.TopAnchor, 20), | ||
stackView.LeadingAnchor.ConstraintEqualTo(View.SafeAreaLayoutGuide.LeadingAnchor, 20), | ||
stackView.TrailingAnchor.ConstraintEqualTo(View.SafeAreaLayoutGuide.TrailingAnchor, -20), | ||
stackView.BottomAnchor.ConstraintLessThanOrEqualTo(View.SafeAreaLayoutGuide.BottomAnchor, -20) | ||
}); | ||
|
||
// Create UIKit button | ||
var uiButton = new UIButton(UIButtonType.System); | ||
uiButton.SetTitle("UIKit button above .NET MAUI controls", UIControlState.Normal); | ||
uiButton.TouchUpInside += OnUIButtonClicked; | ||
stackView.AddArrangedSubview(uiButton); | ||
// Create UIKit button | ||
var uiButton = new UIButton(UIButtonType.System); | ||
uiButton.SetTitle("UIKit button above .NET MAUI controls", UIControlState.Normal); | ||
uiButton.TouchUpInside += OnUIButtonClicked; | ||
stackView.AddArrangedSubview(uiButton); | ||
|
||
// Ensure .NET MAUI app is built before creating .NET MAUI views | ||
var mauiApp = MainViewController.MauiApp.Value; | ||
// Ensure .NET MAUI app is built before creating .NET MAUI views | ||
var mauiApp = MainViewController.MauiApp.Value; | ||
|
||
// Create .NET MAUI context | ||
var mauiContext = UseWindowContext | ||
? mauiApp.CreateEmbeddedWindowContext(GetWindow()) // Create window context | ||
: new MauiContext(mauiApp.Services); // Create app context | ||
// Create .NET MAUI context | ||
var mauiContext = UseWindowContext | ||
? mauiApp.CreateEmbeddedWindowContext(GetWindow()) // Create window context | ||
: new MauiContext(mauiApp.Services); // Create app context | ||
|
||
|
||
// Create .NET MAUI content | ||
mauiView = new MyMauiContent(); | ||
// Create .NET MAUI content | ||
mauiView = new MyMauiContent(); | ||
|
||
// Create native view | ||
var nativeView = mauiView.ToPlatformEmbedded(mauiContext); | ||
nativeView.WidthAnchor.ConstraintEqualTo(View.Frame.Width).Active = true; | ||
nativeView.HeightAnchor.ConstraintEqualTo(500).Active = true; | ||
// Create native view | ||
var nativeView = mauiView.ToPlatformEmbedded(mauiContext); | ||
nativeView.WidthAnchor.ConstraintEqualTo(View.Frame.Width).Active = true; | ||
nativeView.HeightAnchor.ConstraintEqualTo(500).Active = true; | ||
|
||
// Add native view to layout | ||
stackView.AddArrangedSubview(nativeView); | ||
// Add native view to layout | ||
stackView.AddArrangedSubview(nativeView); | ||
|
||
AddNavBarButtons(); | ||
} | ||
AddNavBarButtons(); | ||
} | ||
|
||
void AddNavBarButtons() | ||
{ | ||
var addNewWindowButton = new UIBarButtonItem( | ||
UIImage.GetSystemImage("macwindow.badge.plus"), | ||
UIBarButtonItemStyle.Plain, | ||
(sender, e) => RequestSession()); | ||
void AddNavBarButtons() | ||
{ | ||
var addNewWindowButton = new UIBarButtonItem( | ||
UIImage.GetSystemImage("macwindow.badge.plus"), | ||
UIBarButtonItemStyle.Plain, | ||
(sender, e) => RequestSession()); | ||
|
||
var addNewTaskButton = new UIBarButtonItem( | ||
UIBarButtonSystemItem.Add, | ||
(sender, e) => RequestSession("NewTaskWindow")); | ||
var addNewTaskButton = new UIBarButtonItem( | ||
UIBarButtonSystemItem.Add, | ||
(sender, e) => RequestSession("NewTaskWindow")); | ||
|
||
NavigationItem.RightBarButtonItems = [addNewTaskButton, addNewWindowButton]; | ||
} | ||
NavigationItem.RightBarButtonItems = [addNewTaskButton, addNewWindowButton]; | ||
} | ||
|
||
void RequestSession(string? activityType = null) | ||
void RequestSession(string? activityType = null) | ||
{ | ||
var activity = activityType is null | ||
? null | ||
: new NSUserActivity(activityType); | ||
|
||
if (OperatingSystem.IsMacCatalystVersionAtLeast(17)) | ||
{ | ||
var activity = activityType is null | ||
? null | ||
: new NSUserActivity(activityType); | ||
var request = UISceneSessionActivationRequest.Create(); | ||
request.UserActivity = activity; | ||
|
||
if (OperatingSystem.IsMacCatalystVersionAtLeast(17)) | ||
UIApplication.SharedApplication.ActivateSceneSession(request, error => | ||
{ | ||
var request = UISceneSessionActivationRequest.Create(); | ||
request.UserActivity = activity; | ||
|
||
UIApplication.SharedApplication.ActivateSceneSession(request, error => | ||
{ | ||
Console.WriteLine(new NSErrorException(error)); | ||
}); | ||
} | ||
else | ||
Console.WriteLine(new NSErrorException(error)); | ||
}); | ||
} | ||
else | ||
{ | ||
UIApplication.SharedApplication.RequestSceneSessionActivation(null, activity, null, error => | ||
{ | ||
UIApplication.SharedApplication.RequestSceneSessionActivation(null, activity, null, error => | ||
{ | ||
Console.WriteLine(new NSErrorException(error)); | ||
}); | ||
} | ||
Console.WriteLine(new NSErrorException(error)); | ||
}); | ||
} | ||
} | ||
|
||
async void OnUIButtonClicked(object? sender, EventArgs e) | ||
{ | ||
if (mauiView?.DotNetBot is not Image bot) | ||
return; | ||
async void OnUIButtonClicked(object? sender, EventArgs e) | ||
{ | ||
if (mauiView?.DotNetBot is not Image bot) | ||
return; | ||
|
||
await bot.RotateTo(360, 1000); | ||
bot.Rotation = 0; | ||
await bot.RotateTo(360, 1000); | ||
bot.Rotation = 0; | ||
|
||
bot.HeightRequest = 90; | ||
} | ||
bot.HeightRequest = 90; | ||
} | ||
} | ||
|
77 changes: 38 additions & 39 deletions
77
...rmIntegration/NativeEmbeddingDemo/NativeEmbeddingDemo.MacCatalyst/NewTaskSceneDelegate.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,54 @@ | ||
using AppKit; | ||
|
||
namespace NativeEmbeddingDemo.MacCatalyst | ||
namespace NativeEmbeddingDemo.MacCatalyst; | ||
|
||
[Register(nameof(NewTaskSceneDelegate))] | ||
public class NewTaskSceneDelegate : UIWindowSceneDelegate | ||
{ | ||
[Register(nameof(NewTaskSceneDelegate))] | ||
public class NewTaskSceneDelegate : UIWindowSceneDelegate | ||
{ | ||
public override UIWindow? Window { get; set; } | ||
public override UIWindow? Window { get; set; } | ||
|
||
[Export("scene:willConnectToSession:options:")] | ||
public override void WillConnect(UIScene scene, UISceneSession session, UISceneConnectionOptions connectionOptions) | ||
{ | ||
if (scene is not UIWindowScene windowScene) | ||
return; | ||
[Export("scene:willConnectToSession:options:")] | ||
public override void WillConnect(UIScene scene, UISceneSession session, UISceneConnectionOptions connectionOptions) | ||
{ | ||
if (scene is not UIWindowScene windowScene) | ||
return; | ||
|
||
var window = new UIWindow(windowScene); | ||
window.RootViewController = new NewTaskViewController(); | ||
window.WindowScene!.Title = "New Task"; | ||
window.MakeKeyAndVisible(); | ||
var window = new UIWindow(windowScene); | ||
window.RootViewController = new NewTaskViewController(); | ||
window.WindowScene!.Title = "New Task"; | ||
window.MakeKeyAndVisible(); | ||
|
||
Window = window; | ||
Window = window; | ||
|
||
if (OperatingSystem.IsMacCatalyst()) | ||
{ | ||
ConfigureMacWindowSize(); | ||
ConfigureToolbar(); | ||
} | ||
if (OperatingSystem.IsMacCatalyst()) | ||
{ | ||
ConfigureMacWindowSize(); | ||
ConfigureToolbar(); | ||
} | ||
} | ||
|
||
void ConfigureMacWindowSize() | ||
{ | ||
if (Window?.WindowScene?.SizeRestrictions is null) | ||
return; | ||
void ConfigureMacWindowSize() | ||
{ | ||
if (Window?.WindowScene?.SizeRestrictions is null) | ||
return; | ||
|
||
var fixedSize = new CGSize(400, 250); | ||
Window.WindowScene.SizeRestrictions.MinimumSize = fixedSize; | ||
Window.WindowScene.SizeRestrictions.MaximumSize = fixedSize; | ||
} | ||
var fixedSize = new CGSize(400, 250); | ||
Window.WindowScene.SizeRestrictions.MinimumSize = fixedSize; | ||
Window.WindowScene.SizeRestrictions.MaximumSize = fixedSize; | ||
} | ||
|
||
void ConfigureToolbar() | ||
{ | ||
if (Window?.WindowScene?.Titlebar is null) | ||
return; | ||
void ConfigureToolbar() | ||
{ | ||
if (Window?.WindowScene?.Titlebar is null) | ||
return; | ||
|
||
var toolbar = new NSToolbar(); | ||
toolbar.ShowsBaselineSeparator = false; | ||
var toolbar = new NSToolbar(); | ||
toolbar.ShowsBaselineSeparator = false; | ||
|
||
var titlebar = Window.WindowScene.Titlebar; | ||
titlebar.Toolbar = toolbar; | ||
titlebar.ToolbarStyle = UITitlebarToolbarStyle.Automatic; | ||
titlebar.TitleVisibility = UITitlebarTitleVisibility.Visible; | ||
} | ||
var titlebar = Window.WindowScene.Titlebar; | ||
titlebar.Toolbar = toolbar; | ||
titlebar.ToolbarStyle = UITitlebarToolbarStyle.Automatic; | ||
titlebar.TitleVisibility = UITitlebarTitleVisibility.Visible; | ||
} | ||
} | ||
|
Oops, something went wrong.