Skip to content

Commit

Permalink
New tray icon menu item to shut down system and new option for single…
Browse files Browse the repository at this point in the history
… click to open main window.
  • Loading branch information
leonzhou-smokeball committed Dec 30, 2024
1 parent c8090bb commit 722049f
Show file tree
Hide file tree
Showing 10 changed files with 139 additions and 12 deletions.
2 changes: 1 addition & 1 deletion App/App.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<SupportedOSPlatformVersion>10.0.22000.0</SupportedOSPlatformVersion>
<ApplicationHighDpiMode>PerMonitorV2</ApplicationHighDpiMode>
<Platforms>AnyCPU;x64;ARM64;x86</Platforms>
<PackageVersion>2.1.5</PackageVersion>
<PackageVersion>2.1.6</PackageVersion>
<PackageProjectUrl>https://github.com/soleon/Percentage</PackageProjectUrl>
<PackageLicenseUrl>https://github.com/soleon/Percentage?tab=MIT-1-ov-file</PackageLicenseUrl>
<PackageIcon>Icon.png</PackageIcon>
Expand Down
2 changes: 2 additions & 0 deletions App/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@ public partial class App
internal const string DefaultBatteryLowColour = "#FFCA5010";
internal const bool DefaultBatteryLowNotification = true;
internal const int DefaultBatteryLowNotificationValue = 20;
internal const bool DefaultDoubleClickActivation = false;
internal const bool DefaultHideAtStartup = false;
internal const bool DefaultIsAutoBatteryChargingColour = false;
internal const bool DefaultIsAutoBatteryCriticalColour = false;
internal const bool DefaultIsAutoBatteryLowColour = false;
internal const bool DefaultIsAutoBatteryNormalColour = true;
internal const int DefaultRefreshSeconds = 60;
internal const bool DefaultShutDownWithoutConfirmation = false;
internal const bool DefaultTrayIconFontBold = false;
internal const int DefaultTrayIconFontSize = 16;
internal const bool DefaultTrayIconFontUnderline = false;
Expand Down
8 changes: 8 additions & 0 deletions App/Extensions/ExternalProcessExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ internal static void ShowRatingView()
StartShellExecutedProgress("ms-windows-store://review/?ProductId=9PCKT2B7DZMW");
}

public static void ShutDownDevice()
{
Process.Start(new ProcessStartInfo("shutdown", "/s /t 0")
{
CreateNoWindow = true
});
}

internal static void SleepDevice()
{
// Parameter 0,0,0 for "SetSuspendState" native function:
Expand Down
9 changes: 7 additions & 2 deletions App/NotifyIconWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
<tray:NotifyIcon x:Name="NotifyIcon"
x:FieldModifier="private"
FocusOnLeftClick="False"
LeftDoubleClick="OnNotifyIconLeftDoubleClick">
LeftDoubleClick="OnNotifyIconLeftDoubleClick"
LeftClick="OnNotifyIconLeftClick">
<tray:NotifyIcon.Menu>
<ContextMenu>
<ui:MenuItem Header="_Details"
Expand All @@ -25,12 +26,16 @@
ToolTip="Open application settings" />
<ui:MenuItem Header="_Power &amp; battery settings"
Click="OnSystemSettingsMenuItemClick"
Icon="{ui:SymbolIcon Power20}"
Icon="{ui:SymbolIcon FlashSettings20}"
ToolTip="Open Windows system power and battery settings" />
<ui:MenuItem Header="_Sleep device"
ToolTip="Turn your device to sleep mode"
Icon="{ui:SymbolIcon WeatherMoon20}"
Click="OnSleepMenuItemClick" />
<ui:MenuItem Header="Sh_ut down device"
ToolTip="Shut down your device"
Icon="{ui:SymbolIcon Power20}"
Click="OnShutDownMenuItemClick" />
<ui:MenuItem Header="_About"
Click="OnAboutMenuItemClick"
Icon="{ui:SymbolIcon Info20}"
Expand Down
53 changes: 51 additions & 2 deletions App/NotifyIconWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,25 @@
using System.ComponentModel;
using System.Reactive.Linq;
using System.Reactive.Subjects;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms;
using System.Windows.Threading;
using Windows.Devices.Power;
using Microsoft.Win32;
using Percentage.App.Extensions;
using Percentage.App.Pages;
using Wpf.Ui.Appearance;
using Wpf.Ui.Controls;
using Application = System.Windows.Application;
using Brush = System.Windows.Media.Brush;
using PowerLineStatus = System.Windows.Forms.PowerLineStatus;
using TimeSpan = System.TimeSpan;
using static Percentage.App.Properties.Settings;
using MessageBox = Wpf.Ui.Controls.MessageBox;
using MessageBoxResult = Wpf.Ui.Controls.MessageBoxResult;
using NotifyIcon = Wpf.Ui.Tray.Controls.NotifyIcon;
using TextBlock = System.Windows.Controls.TextBlock;

namespace Percentage.App;

Expand All @@ -43,6 +47,39 @@ public NotifyIconWindow()
_refreshTimer.Tick += (_, _) => _batteryStatusUpdateSubject.OnNext(false);
}

private static async Task ShutDownAsync()
{
if (!Default.ShutDownWithoutConfirmation)
{
var result = await new MessageBox
{
Title = "Shut Down",
Content = "Are you sure you want to shut down your device?",
PrimaryButtonAppearance = ControlAppearance.Caution,
PrimaryButtonText = "Yes",
SecondaryButtonAppearance = ControlAppearance.Caution,
SecondaryButtonText = "Always Yes",
CloseButtonText = "No"
}.ShowDialogAsync().ConfigureAwait(false);

switch (result)
{
case MessageBoxResult.None:
return;
case MessageBoxResult.Primary:
break;
case MessageBoxResult.Secondary:
Default.ShutDownWithoutConfirmation = true;
Default.Save();
break;
default:
throw new InvalidEnumArgumentException($"{result} is not a supported enum value.");
}
}

ExternalProcessExtensions.ShutDownDevice();
}

private void OnAboutMenuItemClick(object sender, RoutedEventArgs e)
{
Application.Current.ActivateMainWindow().NavigateToPage<AboutPage>();
Expand Down Expand Up @@ -107,9 +144,21 @@ private void OnLoaded(object sender, RoutedEventArgs args)
_refreshTimer.Start();
}

private void OnNotifyIconLeftClick(NotifyIcon sender, RoutedEventArgs e)
{
if (!Default.DoubleClickActivation)
Application.Current.ActivateMainWindow().NavigateToPage<DetailsPage>();
}

private void OnNotifyIconLeftDoubleClick(NotifyIcon sender, RoutedEventArgs e)
{
Application.Current.ActivateMainWindow().NavigateToPage<DetailsPage>();
if (Default.DoubleClickActivation)
Application.Current.ActivateMainWindow().NavigateToPage<DetailsPage>();
}

private void OnShutDownMenuItemClick(object sender, RoutedEventArgs e)
{
_ = ShutDownAsync();
}

private void OnSleepMenuItemClick(object sender, RoutedEventArgs e)
Expand Down
43 changes: 37 additions & 6 deletions App/Pages/SettingsPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,36 @@
<ui:ToggleSwitch
IsChecked="{Binding HideAtStartup, Source={x:Static properties:Settings.Default}, Mode=TwoWay}" />
</ui:CardControl>

<!-- Double click tray icon to activate main UI -->
<ui:CardControl Icon="{ui:SymbolIcon CursorClick20}">
<ui:CardControl.Header>
<StackPanel>
<ui:TextBlock
FontTypography="BodyStrong"
Text="Double click activation" />
<ui:TextBlock
Text="Double click the tray icon to show battery details" />
</StackPanel>
</ui:CardControl.Header>
<ui:ToggleSwitch
IsChecked="{Binding DoubleClickActivation, Source={x:Static properties:Settings.Default}, Mode=TwoWay}" />
</ui:CardControl>

<!-- Shut down without confirmation -->
<ui:CardControl Icon="{ui:SymbolIcon Power20}">
<ui:CardControl.Header>
<StackPanel>
<ui:TextBlock
FontTypography="BodyStrong"
Text="Shut down without confirmation" />
<ui:TextBlock
Text="Do not show confirmation when shutting down your device" />
</StackPanel>
</ui:CardControl.Header>
<ui:ToggleSwitch
IsChecked="{Binding ShutDownWithoutConfirmation, Source={x:Static properties:Settings.Default}, Mode=TwoWay}" />
</ui:CardControl>

<!-- Refresh interval -->
<ui:CardControl Icon="{ui:SymbolIcon ArrowSync20}">
Expand All @@ -63,7 +93,7 @@
FontTypography="BodyStrong"
Text="Refresh" />
<ui:TextBlock
Text="Sets how long the battery status is refreshed" />
Text="How frequent the battery status is refreshed" />
</StackPanel>
</ui:CardControl.Header>
<ComboBox ItemStringFormat="{}{0} seconds"
Expand All @@ -83,7 +113,7 @@
FontTypography="BodyStrong"
Text="Tray icon font" />
<ui:TextBlock
Text="Customise tray icon font" />
Text="Change tray icon font, size and style" />
</StackPanel>
</ui:CardExpander.Header>
<StackPanel>
Expand Down Expand Up @@ -132,7 +162,7 @@
FontTypography="BodyStrong"
Text="Tray icon colour" />
<ui:TextBlock
Text="Set tray icon font colour for different battery status" />
Text="Change tray icon colour for different battery levels" />
</StackPanel>
</ui:CardExpander.Header>
<Grid IsSharedSizeScope="True">
Expand Down Expand Up @@ -183,7 +213,7 @@
FontTypography="BodyStrong"
Text="Battery levels &amp; notifications" />
<ui:TextBlock
Text="Toggle notifications for different battery levels" />
Text="Change notifications for different battery levels" />
</StackPanel>
</ui:CardExpander.Header>
<StackPanel Grid.IsSharedSizeScope="True">
Expand All @@ -208,7 +238,8 @@
</StackPanel>
</ui:CardExpander>

<ui:CardControl Icon="{ui:SymbolIcon Power20}"
<!-- Open system power settings -->
<ui:CardControl Icon="{ui:SymbolIcon FlashSettings20}"
Content="{ui:SymbolIcon Open20}"
Click="OnSystemPowerSettingsClick">
<ui:CardControl.Header>
Expand All @@ -217,7 +248,7 @@
FontTypography="BodyStrong"
Text="Power &amp; battery" />
<ui:TextBlock
Text="Open Windows system power and battery settings" />
Text="Open system power and battery settings" />
</StackPanel>
</ui:CardControl.Header>
</ui:CardControl>
Expand Down
2 changes: 2 additions & 0 deletions App/Pages/SettingsPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ private void OnResetButtonClick(object sender, RoutedEventArgs e)
Default.IsAutoBatteryChargingColour = App.DefaultIsAutoBatteryChargingColour;
Default.IsAutoBatteryLowColour = App.DefaultIsAutoBatteryLowColour;
Default.IsAutoBatteryCriticalColour = App.DefaultIsAutoBatteryCriticalColour;
Default.DoubleClickActivation = App.DefaultDoubleClickActivation;
Default.ShutDownWithoutConfirmation = App.DefaultShutDownWithoutConfirmation;

_ = EnableAutoStart();
}
Expand Down
24 changes: 24 additions & 0 deletions App/Properties/Settings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions App/Properties/Settings.settings
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@
<Setting Name="TrayIconFontSize" Type="System.Double" Scope="User">
<Value Profile="(Default)">16</Value>
</Setting>
<Setting Name="DoubleClickActivation" Type="System.Boolean" Scope="User">
<Value Profile="(Default)">False</Value>
</Setting>
<Setting Name="ShutDownWithoutConfirmation" Type="System.Boolean" Scope="User">
<Value Profile="(Default)">False</Value>
</Setting>
</Settings>
</SettingsFile>

2 changes: 1 addition & 1 deletion Pack/Package.appxmanifest
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<Identity
Name="61867SoleonInnovation.BatteryPercentageIcon"
Publisher="CN=B0B1FE5B-CC73-4F71-BD3F-7B809647826C"
Version="2.1.5.0" />
Version="2.1.6.0" />

<Properties>
<DisplayName>Battery Percentage Icon</DisplayName>
Expand Down

0 comments on commit 722049f

Please sign in to comment.