|
| 1 | +using FluentTerminal.App.Views; |
| 2 | +using System; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Threading.Tasks; |
| 5 | +using Windows.ApplicationModel; |
| 6 | +using Windows.ApplicationModel.Activation; |
| 7 | +using Windows.ApplicationModel.AppService; |
| 8 | +using Windows.ApplicationModel.Background; |
| 9 | +using Windows.ApplicationModel.Core; |
| 10 | +using Windows.UI.Core; |
| 11 | +using Windows.UI.ViewManagement; |
| 12 | +using Windows.UI.Xaml; |
| 13 | +using Windows.UI.Xaml.Controls; |
| 14 | +using Windows.UI.Xaml.Navigation; |
| 15 | + |
| 16 | +namespace FluentTerminal.App |
| 17 | +{ |
| 18 | + sealed partial class App : Application |
| 19 | + { |
| 20 | + public App() |
| 21 | + { |
| 22 | + InitializeComponent(); |
| 23 | + Instance = this; |
| 24 | + } |
| 25 | + |
| 26 | + public static BackgroundTaskDeferral AppServiceDeferral = null; |
| 27 | + public static AppServiceConnection Connection = null; |
| 28 | + public static App Instance; |
| 29 | + int? _settingsWindowId; |
| 30 | + int idCreate = 0; |
| 31 | + List<int> idSaved = new List<int>(); |
| 32 | + |
| 33 | + protected override async void OnLaunched(LaunchActivatedEventArgs e) |
| 34 | + { |
| 35 | + var launch = FullTrustProcessLauncher.LaunchFullTrustProcessForCurrentAppAsync().AsTask(); |
| 36 | + var clearCache = WebView.ClearTemporaryWebDataAsync().AsTask(); |
| 37 | + |
| 38 | + await Task.WhenAll(launch, clearCache); |
| 39 | + |
| 40 | + Frame rootFrame = Window.Current.Content as Frame; |
| 41 | + if (rootFrame == null) |
| 42 | + { |
| 43 | + rootFrame = new Frame(); |
| 44 | + rootFrame.NavigationFailed += OnNavigationFailed; |
| 45 | + Window.Current.Content = rootFrame; |
| 46 | + } |
| 47 | + |
| 48 | + if (rootFrame.Content == null) |
| 49 | + { |
| 50 | + CoreApplication.GetCurrentView().TitleBar.ExtendViewIntoTitleBar = true; |
| 51 | + rootFrame.Navigate(typeof(MainPage), e.Arguments); |
| 52 | + idSaved.Add(ApplicationView.GetForCurrentView().Id); |
| 53 | + } |
| 54 | + else if (false) //todo: add option to create new windows on launch |
| 55 | + { |
| 56 | + await CreateNewWindow(typeof(MainPage), true); |
| 57 | + } |
| 58 | + Window.Current.Activate(); |
| 59 | + } |
| 60 | + |
| 61 | + protected override void OnBackgroundActivated(BackgroundActivatedEventArgs args) |
| 62 | + { |
| 63 | + // connection established from the fulltrust process |
| 64 | + base.OnBackgroundActivated(args); |
| 65 | + if (args.TaskInstance.TriggerDetails is AppServiceTriggerDetails details) |
| 66 | + { |
| 67 | + AppServiceDeferral = args.TaskInstance.GetDeferral(); |
| 68 | + args.TaskInstance.Canceled += OnTaskCanceled; |
| 69 | + Connection = details.AppServiceConnection; |
| 70 | + Connection.RequestReceived += OnRequestReceived; |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + private void OnRequestReceived(AppServiceConnection sender, AppServiceRequestReceivedEventArgs args) |
| 75 | + { |
| 76 | + if (args.Request.Message.ContainsKey("exit")) |
| 77 | + { |
| 78 | + App.Current.Exit(); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + private void OnTaskCanceled(IBackgroundTaskInstance sender, BackgroundTaskCancellationReason reason) |
| 83 | + { |
| 84 | + if (AppServiceDeferral != null) |
| 85 | + { |
| 86 | + AppServiceDeferral.Complete(); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + public async Task<int> CreateNewWindow(Type pageType, bool ExtendViewIntoTitleBar) |
| 91 | + { |
| 92 | + var create = CoreApplication.CreateNewView(); |
| 93 | + await create.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => |
| 94 | + { |
| 95 | + CoreApplication.GetCurrentView().TitleBar.ExtendViewIntoTitleBar = ExtendViewIntoTitleBar; |
| 96 | + var frame = new Frame(); |
| 97 | + frame.Navigate(pageType, null); |
| 98 | + Window.Current.Content = frame; |
| 99 | + Window.Current.Activate(); |
| 100 | + |
| 101 | + idCreate = ApplicationView.GetForCurrentView().Id; |
| 102 | + }); |
| 103 | + |
| 104 | + for (int i = idSaved.Count - 1; i >= 0; i--) |
| 105 | + if (await ApplicationViewSwitcher.TryShowAsStandaloneAsync( |
| 106 | + idCreate, ViewSizePreference.Default, |
| 107 | + idSaved[i], ViewSizePreference.Default) |
| 108 | + ) break; |
| 109 | + |
| 110 | + idSaved.Add(idCreate); |
| 111 | + return idCreate; |
| 112 | + } |
| 113 | + |
| 114 | + public async Task ShowNew() |
| 115 | + { |
| 116 | + var id = await CreateNewWindow(typeof(MainPage), true); |
| 117 | + bool viewShown = await ApplicationViewSwitcher.TryShowAsStandaloneAsync(id); |
| 118 | + } |
| 119 | + |
| 120 | + public async Task ShowSettings() |
| 121 | + { |
| 122 | + if (_settingsWindowId == null) |
| 123 | + { |
| 124 | + _settingsWindowId = await CreateNewWindow(typeof(Views.SettingsPage), false); |
| 125 | + } |
| 126 | + bool viewShown = await ApplicationViewSwitcher.TryShowAsStandaloneAsync(_settingsWindowId.Value); |
| 127 | + } |
| 128 | + |
| 129 | + void OnNavigationFailed(object sender, NavigationFailedEventArgs e) |
| 130 | + { |
| 131 | + throw new Exception("Failed to load Page " + e.SourcePageType.FullName); |
| 132 | + } |
| 133 | + } |
| 134 | +} |
0 commit comments