2020-04-07 14:19:33 -07:00
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System ;
using System.Windows ;
2020-08-17 10:00:56 -07:00
using Microsoft.PowerLauncher.Telemetry ;
2020-04-07 14:19:33 -07:00
using Microsoft.PowerToys.Settings.UI.Views ;
2020-08-17 10:00:56 -07:00
using Microsoft.PowerToys.Telemetry ;
2020-04-07 10:19:14 -07:00
using Microsoft.Toolkit.Wpf.UI.XamlHost ;
2020-09-04 11:56:52 +03:00
using Windows.Data.Json ;
2020-04-07 14:19:33 -07:00
namespace Microsoft.PowerToys.Settings.UI.Runner
{
// Interaction logic for MainWindow.xaml.
public partial class MainWindow : Window
{
2020-06-22 15:38:55 -07:00
private bool isOpen = true ;
2020-04-07 14:19:33 -07:00
public MainWindow ( )
2020-08-17 10:00:56 -07:00
{
var bootTime = new System . Diagnostics . Stopwatch ( ) ;
bootTime . Start ( ) ;
this . InitializeComponent ( ) ;
bootTime . Stop ( ) ;
2020-05-15 09:08:39 -07:00
PowerToysTelemetry . Log . WriteEvent ( new SettingsBootEvent ( ) { BootTimeMs = bootTime . ElapsedMilliseconds } ) ;
2020-04-07 14:19:33 -07:00
}
private void WindowsXamlHost_ChildChanged ( object sender , EventArgs e )
{
// Hook up x:Bind source.
WindowsXamlHost windowsXamlHost = sender as WindowsXamlHost ;
ShellPage shellPage = windowsXamlHost . GetUwpInternalObject ( ) as ShellPage ;
2020-03-24 19:55:02 -07:00
if ( shellPage ! = null )
{
2020-03-30 02:02:25 -07:00
// send IPC Message
2020-04-07 10:19:14 -07:00
shellPage . SetDefaultSndMessageCallback ( msg = >
2020-03-24 19:55:02 -07:00
{
2020-05-15 09:08:39 -07:00
// IPC Manager is null when launching runner directly
2020-05-05 11:44:54 -07:00
Program . GetTwoWayIPCManager ( ) ? . Send ( msg ) ;
2020-03-24 19:55:02 -07:00
} ) ;
2020-05-05 10:02:31 -07:00
// send IPC Message
shellPage . SetRestartAdminSndMessageCallback ( msg = >
{
Program . GetTwoWayIPCManager ( ) . Send ( msg ) ;
System . Windows . Application . Current . Shutdown ( ) ; // close application
} ) ;
2020-06-23 15:53:02 +03:00
// send IPC Message
shellPage . SetCheckForUpdatesMessageCallback ( msg = >
{
Program . GetTwoWayIPCManager ( ) . Send ( msg ) ;
} ) ;
2020-09-04 11:56:52 +03:00
// receive IPC Message
Program . IPCMessageReceivedCallback = ( string msg ) = >
{
if ( ShellPage . ShellHandler . IPCResponseHandleList ! = null )
{
try
{
JsonObject json = JsonObject . Parse ( msg ) ;
foreach ( Action < JsonObject > handle in ShellPage . ShellHandler . IPCResponseHandleList )
{
handle ( json ) ;
}
}
catch ( Exception )
{
}
}
} ;
2020-05-05 10:02:31 -07:00
shellPage . SetElevationStatus ( Program . IsElevated ) ;
2020-05-14 12:36:27 +03:00
shellPage . SetIsUserAnAdmin ( Program . IsUserAnAdmin ) ;
2020-05-05 10:02:31 -07:00
shellPage . Refresh ( ) ;
2020-04-07 14:19:33 -07:00
}
2020-06-22 15:38:55 -07:00
2020-09-16 09:47:43 -07:00
// XAML Islands: If the window is open, explicity force it to be shown to solve the blank dialog issue https://github.com/microsoft/PowerToys/issues/3384
2020-08-17 10:00:56 -07:00
if ( isOpen )
2020-06-22 15:38:55 -07:00
{
2020-08-17 10:00:56 -07:00
Show ( ) ;
2020-06-22 15:38:55 -07:00
}
2020-08-17 10:00:56 -07:00
}
private void MainWindow_Closing ( object sender , System . ComponentModel . CancelEventArgs e )
{
isOpen = false ;
2020-09-16 09:47:43 -07:00
// XAML Islands: If the window is closed while minimized, exit the process. Required to avoid process not terminating issue - https://github.com/microsoft/PowerToys/issues/4430
if ( WindowState = = WindowState . Minimized )
{
// Run Environment.Exit on a separate task to avoid performance impact
System . Threading . Tasks . Task . Run ( ( ) = > { Environment . Exit ( 0 ) ; } ) ;
}
2020-08-17 10:00:56 -07:00
}
2020-04-07 14:19:33 -07:00
}
2020-04-10 15:22:07 -07:00
}