private static AppBuilder CreateApplication(StridePlatformOptions options) { var builderType = typeof(AppBuilderBase <>).MakeGenericType(typeof(AppBuilder)); var configureMethod = builderType.GetMethod(nameof(AppBuilder.Configure), BindingFlags.Public | BindingFlags.Static, null, Array.Empty <Type>(), null); var builder = (AppBuilder)configureMethod.MakeGenericMethod(options.ApplicationType).Invoke(null, Array.Empty <object>()); builder .UseStride(); switch (GraphicsDevice.Platform) { default: builder.UseSkia(); break; } options.ConfigureApp?.Invoke(builder); var lifetime = new ClassicDesktopStyleApplicationLifetime { Args = Environment.GetCommandLineArgs(), ShutdownMode = ShutdownMode.OnExplicitShutdown }; builder.SetupWithLifetime(lifetime); return(builder); }
private void UIThreadFunc() { ApplicationLifetime = new ClassicDesktopStyleApplicationLifetime(); ApplicationLifetime.ShutdownMode = ShutdownMode.OnMainWindowClose; AppBuilder.Configure <App>().UsePlatformDetect().SetupWithLifetime(ApplicationLifetime); ApplicationLifetime.Start(Array.Empty <string>()); }
public void MainWindow_Closed_Shutdown_Should_Be_Cancellable() { using (UnitTestApplication.Start(TestServices.StyledWindow)) using (var lifetime = new ClassicDesktopStyleApplicationLifetime()) { lifetime.ShutdownMode = ShutdownMode.OnMainWindowClose; var hasExit = false; lifetime.Exit += (_, _) => hasExit = true; var mainWindow = new Window(); mainWindow.Show(); lifetime.MainWindow = mainWindow; var window = new Window(); window.Show(); var raised = 0; lifetime.ShutdownRequested += (_, e) => { e.Cancel = true; ++raised; }; mainWindow.Close(); Assert.Equal(1, raised); Assert.False(hasExit); } }
public void Shutdown_NotCancellable_By_Preventing_Window_Close() { using (UnitTestApplication.Start(TestServices.StyledWindow)) using (var lifetime = new ClassicDesktopStyleApplicationLifetime()) { var hasExit = false; lifetime.Exit += (_, _) => hasExit = true; var windowA = new Window(); windowA.Show(); var windowB = new Window(); windowB.Show(); var raised = 0; windowA.Closing += (_, e) => { e.Cancel = true; ++raised; }; lifetime.Shutdown(); Assert.Equal(1, raised); Assert.True(hasExit); } }
public void Should_Exit_After_Last_Window_Closed() { using (UnitTestApplication.Start(TestServices.StyledWindow)) using (var lifetime = new ClassicDesktopStyleApplicationLifetime()) { lifetime.ShutdownMode = ShutdownMode.OnLastWindowClose; var hasExit = false; lifetime.Exit += (_, _) => hasExit = true; var windowA = new Window(); windowA.Show(); var windowB = new Window(); windowB.Show(); windowA.Close(); Assert.False(hasExit); windowB.Close(); Assert.True(hasExit); } }
public void Should_Only_Exit_On_Explicit_Exit() { using (UnitTestApplication.Start(TestServices.StyledWindow)) using (var lifetime = new ClassicDesktopStyleApplicationLifetime()) { lifetime.ShutdownMode = ShutdownMode.OnExplicitShutdown; var hasExit = false; lifetime.Exit += (s, e) => hasExit = true; var windowA = new Window(); windowA.Show(); var windowB = new Window(); windowB.Show(); windowA.Close(); Assert.False(hasExit); windowB.Close(); Assert.False(hasExit); lifetime.Shutdown(); Assert.True(hasExit); } }
public void Should_Exit_After_MainWindow_Closed() { using (UnitTestApplication.Start(TestServices.StyledWindow)) using (var lifetime = new ClassicDesktopStyleApplicationLifetime()) { lifetime.ShutdownMode = ShutdownMode.OnMainWindowClose; var hasExit = false; lifetime.Exit += (s, e) => hasExit = true; var mainWindow = new Window(); mainWindow.Show(); lifetime.MainWindow = mainWindow; var window = new Window(); window.Show(); mainWindow.Close(); Assert.True(hasExit); } }
public void Should_Allow_Canceling_Shutdown_Via_ShutdownRequested_Event() { using (UnitTestApplication.Start(TestServices.StyledWindow)) using (var lifetime = new ClassicDesktopStyleApplicationLifetime()) { var lifetimeEvents = new Mock <IPlatformLifetimeEventsImpl>(); AvaloniaLocator.CurrentMutable.Bind <IPlatformLifetimeEventsImpl>().ToConstant(lifetimeEvents.Object); lifetime.Start(Array.Empty <string>()); var window = new Window(); var raised = 0; window.Show(); lifetime.ShutdownRequested += (s, e) => { e.Cancel = true; ++raised; }; lifetimeEvents.Raise(x => x.ShutdownRequested += null, new ShutdownRequestedEventArgs()); Assert.Equal(1, raised); Assert.Equal(new[] { window }, lifetime.Windows); } }
private static AppBuilder BuildAvaloniaApp() { var lifetime = new ClassicDesktopStyleApplicationLifetime(); lifetime.ShutdownMode = ShutdownMode.OnMainWindowClose; return(AppBuilder.Configure <App>().UseReactiveUI().UsePlatformDetect().SetupWithLifetime(lifetime)); }
public static int StartWithClassicDesktopLifetime <T>( this T builder, string[] args, ShutdownMode shutdownMode = ShutdownMode.OnLastWindowClose) where T : AppBuilderBase <T>, new() { var lifetime = new ClassicDesktopStyleApplicationLifetime() { ShutdownMode = shutdownMode }; builder.SetupWithLifetime(lifetime); return(lifetime.Start(args)); }
public void Should_Set_ExitCode_After_Shutdown() { using (UnitTestApplication.Start(TestServices.MockThreadingInterface)) using (var lifetime = new ClassicDesktopStyleApplicationLifetime()) { lifetime.Shutdown(1337); var exitCode = lifetime.Start(Array.Empty <string>()); Assert.Equal(1337, exitCode); } }
public void Show_Should_Add_Window_To_OpenWindows() { using (UnitTestApplication.Start(TestServices.StyledWindow)) using (var lifetime = new ClassicDesktopStyleApplicationLifetime()) { var window = new Window(); window.Show(); Assert.Equal(new[] { window }, lifetime.Windows); } }
public (IGameWindow, ISoundPlayer) Run(IGameFrame frame) { AvaloniaWindow window = null; ClassicDesktopStyleApplicationLifetime lifetime; if (Application.Current == null) { var builder = AppBuilder.Configure <Application>() .UsePlatformDetect() .With(new AvaloniaNativePlatformOptions { UseGpu = true }); lifetime = new ClassicDesktopStyleApplicationLifetime() { }; Task.Run(() => { builder.Start((app, args) => { window = CreateWindow(frame); lifetime.MainWindow = window; window.Show(); window.Closed += (o, e) => Environment.Exit(0); app.Run(System.Threading.CancellationToken.None); }, null); }); while (window == null) { } } else { AvaloniaWindow win = null; Avalonia.Threading.Dispatcher.UIThread.InvokeAsync(() => { win = CreateWindow(frame); win.Show(); }); while (win == null) { } return(win, soundPlayer); } soundPlayer = new AvaloniaSoundPlayer(); return(window, soundPlayer); }
public void Close_Should_Remove_Window_From_OpenWindows() { using (UnitTestApplication.Start(TestServices.StyledWindow)) using (var lifetime = new ClassicDesktopStyleApplicationLifetime()) { var window = new Window(); window.Show(); Assert.Equal(1, lifetime.Windows.Count); window.Close(); Assert.Empty(lifetime.Windows); } }
public void Window_Should_Be_Added_To_OpenWindows_Only_Once() { using (UnitTestApplication.Start(TestServices.StyledWindow)) using (var lifetime = new ClassicDesktopStyleApplicationLifetime()) { var window = new Window(); window.Show(); window.Show(); window.IsVisible = true; Assert.Equal(new[] { window }, lifetime.Windows); window.Close(); } }
// Initialization code. Don't use any Avalonia, third-party APIs or any // SynchronizationContext-reliant code before AppMain is called: things aren't initialized // yet and stuff might break. public static void Main(string[] args) { if (args.Length > 0 && args[0] == "--run-test") { StreamWriter testWriter = new StreamWriter("test.log"); testWriter.WriteLine("it-works"); testWriter.Close(); return; } var appState = new AppState(); Sikor.Container.IServiceProvider[] services = { new Storage(), new Logger(), new JiraWrapper(), appState }; foreach (Sikor.Container.IServiceProvider service in services) { service.Register(); } var Ui = BuildAvaloniaApp(); //adds view models to container var lifetime = new ClassicDesktopStyleApplicationLifetime(); Ui.SetupWithLifetime(lifetime); Sikor.Container.ServiceContainer.Init(); //Ui.StartWithClassicDesktopLifetime(args); appState.PostInit(); foreach (Sikor.Container.IServiceProvider service in services) { if (service.GetTypeString() == typeof(AppState).ToString()) { continue; //skip appsstate } service.PostInit(); } lifetime.Start(args); }
public void AutoSuspendHelper_Should_Immediately_Fire_IsLaunchingNew() { using (UnitTestApplication.Start(TestServices.MockWindowingPlatform)) using (var lifetime = new ClassicDesktopStyleApplicationLifetime(Application.Current)) { var isLaunchingReceived = false; var application = AvaloniaLocator.Current.GetService <Application>(); application.ApplicationLifetime = lifetime; // Initialize ReactiveUI Suspension as in real-world scenario. var suspension = new AutoSuspendHelper(application.ApplicationLifetime); RxApp.SuspensionHost.IsLaunchingNew.Subscribe(_ => isLaunchingReceived = true); suspension.OnFrameworkInitializationCompleted(); Assert.True(isLaunchingReceived); } }
public void Start() { var builder = AppBuilder.Configure <App>(); builder.UsePlatformDetect(); lifetime = new ClassicDesktopStyleApplicationLifetime() { Args = new string[0], ShutdownMode = ShutdownMode.OnMainWindowClose }; builder.SetupWithLifetime(lifetime); while (true) { Dispatcher.UIThread.RunJobs(); } }
public void Should_Close_All_Remaining_Open_Windows_After_Explicit_Exit_Call() { using (UnitTestApplication.Start(TestServices.StyledWindow)) using (var lifetime = new ClassicDesktopStyleApplicationLifetime()) { var windows = new List <Window> { new Window(), new Window(), new Window(), new Window() }; foreach (var window in windows) { window.Show(); } Assert.Equal(4, lifetime.Windows.Count); lifetime.Shutdown(); Assert.Empty(lifetime.Windows); } }
public void Impl_Closing_Should_Remove_Window_From_OpenWindows() { var windowImpl = new Mock <IWindowImpl>(); windowImpl.SetupProperty(x => x.Closed); windowImpl.Setup(x => x.Scaling).Returns(1); var services = TestServices.StyledWindow.With( windowingPlatform: new MockWindowingPlatform(() => windowImpl.Object)); using (UnitTestApplication.Start(services)) using (var lifetime = new ClassicDesktopStyleApplicationLifetime()) { var window = new Window(); window.Show(); Assert.Equal(1, lifetime.Windows.Count); windowImpl.Object.Closed(); Assert.Empty(lifetime.Windows); } }
public void Shutdown_Doesnt_Raise_Shutdown_Requested() { using (UnitTestApplication.Start(TestServices.StyledWindow)) using (var lifetime = new ClassicDesktopStyleApplicationLifetime()) { var hasExit = false; lifetime.Exit += (_, _) => hasExit = true; var raised = 0; lifetime.ShutdownRequested += (_, _) => { ++raised; }; lifetime.Shutdown(); Assert.Equal(0, raised); Assert.True(hasExit); } }
public void ShouldPersistState_Should_Fire_On_App_Exit_When_SuspensionDriver_Is_Initialized() { using (UnitTestApplication.Start(TestServices.MockWindowingPlatform)) using (var lifetime = new ClassicDesktopStyleApplicationLifetime(Application.Current)) { var shouldPersistReceived = false; var application = AvaloniaLocator.Current.GetService <Application>(); application.ApplicationLifetime = lifetime; // Initialize ReactiveUI Suspension as in real-world scenario. var suspension = new AutoSuspendHelper(application.ApplicationLifetime); RxApp.SuspensionHost.CreateNewAppState = () => new AppState { Example = "Foo" }; RxApp.SuspensionHost.ShouldPersistState.Subscribe(_ => shouldPersistReceived = true); RxApp.SuspensionHost.SetupDefaultSuspendResume(new DummySuspensionDriver()); suspension.OnFrameworkInitializationCompleted(); lifetime.Shutdown(); Assert.True(shouldPersistReceived); Assert.Equal("Foo", RxApp.SuspensionHost.GetAppState <AppState>().Example); } }
public void Start <TMainWindow>(Func <object>?dataContextProvider = null) where TMainWindow : Window, new() { AfterSetup(builder => { var window = new TMainWindow(); if (dataContextProvider != null) { window.DataContext = dataContextProvider(); } ((IClassicDesktopStyleApplicationLifetime)builder.Instance !.ApplicationLifetime !) .MainWindow = window; }); // Copy-pasted because we can't call extension methods due to generic constraints var lifetime = new ClassicDesktopStyleApplicationLifetime() { ShutdownMode = ShutdownMode.OnMainWindowClose }; SetupWithLifetime(lifetime); lifetime.Start(Array.Empty <string>()); }
public AvaloniaHostedService(ClassicDesktopStyleApplicationLifetime classicDesktopStyleApplicationLifetime, IHostApplicationLifetime hostApplicationLifetime) { _classicDesktopStyleApplicationLifetime = classicDesktopStyleApplicationLifetime; _hostApplicationLifetime = hostApplicationLifetime; }