2020-04-08 01:19:14 +08: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.
2020-09-04 16:56:52 +08:00
using System ;
2020-10-30 05:24:16 +08:00
using System.Diagnostics.CodeAnalysis ;
using System.Globalization ;
2020-10-23 00:45:48 +08:00
using Microsoft.PowerToys.Settings.UI.Library ;
using Microsoft.PowerToys.Settings.UI.Library.Utilities ;
using Microsoft.PowerToys.Settings.UI.Library.ViewModels ;
2020-08-14 06:02:05 +08:00
using Windows.ApplicationModel.Resources ;
2020-09-04 16:56:52 +08:00
using Windows.Data.Json ;
2020-08-14 06:02:05 +08:00
using Windows.UI.Xaml ;
2020-04-08 01:19:14 +08:00
using Windows.UI.Xaml.Controls ;
2020-03-12 14:25:24 +08:00
namespace Microsoft.PowerToys.Settings.UI.Views
{
2020-03-25 10:55:02 +08:00
/// <summary>
/// General Settings Page.
/// </summary>
2020-03-12 14:25:24 +08:00
public sealed partial class GeneralPage : Page
{
2020-03-25 10:55:02 +08:00
/// <summary>
2020-04-18 06:25:08 +08:00
/// Gets or sets view model.
2020-03-25 10:55:02 +08:00
/// </summary>
2020-04-18 06:25:08 +08:00
public GeneralViewModel ViewModel { get ; set ; }
2020-03-12 14:25:24 +08:00
2020-03-25 10:55:02 +08:00
/// <summary>
/// Initializes a new instance of the <see cref="GeneralPage"/> class.
/// General Settings page constructor.
/// </summary>
2020-10-30 05:24:16 +08:00
[SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "Exceptions from the IPC response handler should be caught and logged.")]
2020-03-12 14:25:24 +08:00
public GeneralPage ( )
{
2020-08-20 06:59:10 +08:00
InitializeComponent ( ) ;
2020-03-25 10:55:02 +08:00
2020-08-14 06:02:05 +08:00
// Load string resources
ResourceLoader loader = ResourceLoader . GetForViewIndependentUse ( ) ;
2020-11-03 01:33:43 +08:00
var settingsUtils = new SettingsUtils ( ) ;
2020-08-14 06:02:05 +08:00
2020-08-20 06:59:10 +08:00
ViewModel = new GeneralViewModel (
2020-09-24 04:20:32 +08:00
SettingsRepository < GeneralSettings > . GetInstance ( settingsUtils ) ,
2020-08-14 06:02:05 +08:00
loader . GetString ( "GeneralSettings_RunningAsAdminText" ) ,
loader . GetString ( "GeneralSettings_RunningAsUserText" ) ,
ShellPage . IsElevated ,
ShellPage . IsUserAnAdmin ,
UpdateUIThemeMethod ,
ShellPage . SendDefaultIPCMessage ,
ShellPage . SendRestartAdminIPCMessage ,
ShellPage . SendCheckForUpdatesIPCMessage ) ;
2020-09-04 16:56:52 +08:00
ShellPage . ShellHandler . IPCResponseHandleList . Add ( ( JsonObject json ) = >
{
try
{
2020-12-18 00:38:23 +08:00
string version = json . GetNamedString ( "version" , string . Empty ) ;
bool isLatest = json . GetNamedBoolean ( "isVersionLatest" , false ) ;
2020-09-04 16:56:52 +08:00
2021-01-12 23:34:02 +08:00
if ( json . ContainsKey ( "version" ) )
{
ViewModel . RequestUpdateCheckedDate ( ) ;
}
2020-09-04 16:56:52 +08:00
var str = string . Empty ;
if ( isLatest )
{
str = ResourceLoader . GetForCurrentView ( ) . GetString ( "GeneralSettings_VersionIsLatest" ) ;
}
2020-10-30 05:24:16 +08:00
else if ( ! string . IsNullOrEmpty ( version ) )
2020-09-04 16:56:52 +08:00
{
str = ResourceLoader . GetForCurrentView ( ) . GetString ( "GeneralSettings_NewVersionIsAvailable" ) ;
2020-10-30 05:24:16 +08:00
if ( ! string . IsNullOrEmpty ( str ) )
2020-09-04 16:56:52 +08:00
{
str + = ": " + version ;
}
}
2020-10-30 05:24:16 +08:00
// Using CurrentCulture since this is user-facing
2020-12-18 00:38:23 +08:00
if ( ! string . IsNullOrEmpty ( str ) )
{
ViewModel . LatestAvailableVersion = string . Format ( CultureInfo . CurrentCulture , str ) ;
}
string updateStateDate = json . GetNamedString ( "updateStateDate" , string . Empty ) ;
if ( ! string . IsNullOrEmpty ( updateStateDate ) & & long . TryParse ( updateStateDate , out var uTCTime ) )
{
var localTime = new DateTime ( 1970 , 1 , 1 , 0 , 0 , 0 , DateTimeKind . Utc ) . AddSeconds ( uTCTime ) . ToLocalTime ( ) ;
ViewModel . UpdateCheckedDate = localTime . ToString ( CultureInfo . CurrentCulture ) ;
}
2020-09-04 16:56:52 +08:00
}
2020-10-30 05:24:16 +08:00
catch ( Exception e )
2020-09-04 16:56:52 +08:00
{
2020-10-30 05:24:16 +08:00
Logger . LogError ( "Exception encountered when reading the version." , e ) ;
2020-09-04 16:56:52 +08:00
}
} ) ;
2020-08-14 06:02:05 +08:00
DataContext = ViewModel ;
}
2020-10-30 05:24:16 +08:00
public static int UpdateUIThemeMethod ( string themeName )
2020-08-14 06:02:05 +08:00
{
2020-10-30 05:24:16 +08:00
switch ( themeName ? . ToUpperInvariant ( ) )
2020-08-14 06:02:05 +08:00
{
2020-10-20 04:32:05 +08:00
case "LIGHT" :
2020-08-14 06:02:05 +08:00
ShellPage . ShellHandler . RequestedTheme = ElementTheme . Light ;
break ;
2020-10-20 04:32:05 +08:00
case "DARK" :
2020-08-14 06:02:05 +08:00
ShellPage . ShellHandler . RequestedTheme = ElementTheme . Dark ;
break ;
2020-10-20 04:32:05 +08:00
case "SYSTEM" :
2020-08-14 06:02:05 +08:00
ShellPage . ShellHandler . RequestedTheme = ElementTheme . Default ;
break ;
2020-10-30 05:24:16 +08:00
default :
Logger . LogError ( $"Unexpected theme name: {themeName}" ) ;
break ;
2020-08-14 06:02:05 +08:00
}
return 0 ;
2020-03-25 10:55:02 +08:00
}
2021-02-09 19:11:51 +08:00
private void OpenColorsSettings_Click ( object sender , RoutedEventArgs e )
{
Helpers . StartProcessHelper . Start ( Helpers . StartProcessHelper . ColorsSettings ) ;
}
2021-03-03 01:56:37 +08:00
private void OobeButton_Click ( object sender , RoutedEventArgs e )
{
ShellPage . OpenOobeWindowCallback ( ) ;
}
2020-03-12 14:25:24 +08:00
}
2020-04-02 21:08:56 +08:00
}