Skip to content

Commit 1d48bcb

Browse files
committed
System tray icon
Add code for creation of system tray icon, menu and handle menu events
1 parent 495b3a9 commit 1d48bcb

File tree

5 files changed

+65
-3
lines changed

5 files changed

+65
-3
lines changed

src/App.Commands.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ public static bool IsCheckForUpdateCommandVisible
3737
}
3838
}
3939

40-
public static readonly Command OpenPreferenceCommand = new Command(_ => OpenDialog(new Views.Preference()));
40+
public static readonly Command Unminimize = new Command(_ => ShowWindow());
41+
public static readonly Command OpenPreferenceCommand = new Command(_ => { ShowWindow(); OpenDialog(new Views.Preference()); });
4142
public static readonly Command OpenHotkeysCommand = new Command(_ => OpenDialog(new Views.Hotkeys()));
4243
public static readonly Command OpenAppDataDirCommand = new Command(_ => Native.OS.OpenInFileManager(Native.OS.DataDir));
4344
public static readonly Command OpenAboutCommand = new Command(_ => OpenDialog(new Views.About()));

src/App.axaml.cs

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
using Avalonia.Markup.Xaml;
1515
using Avalonia.Media;
1616
using Avalonia.Media.Fonts;
17+
using Avalonia.Media.Imaging;
18+
using Avalonia.Platform;
1719
using Avalonia.Platform.Storage;
1820
using Avalonia.Styling;
1921
using Avalonia.Threading;
@@ -85,6 +87,7 @@ public override void Initialize()
8587
SetLocale(pref.Locale);
8688
SetTheme(pref.Theme, pref.ThemeOverrides);
8789
SetFonts(pref.DefaultFontFamily, pref.MonospaceFontFamily, pref.OnlyUseMonoFontInEditor);
90+
SetupTrayIcon(pref.SystemTrayIcon);
8891
}
8992

9093
public override void OnFrameworkInitializationCompleted()
@@ -192,6 +195,45 @@ public static void SetTheme(string theme, string themeOverridesFile)
192195
}
193196
}
194197

198+
public static void SetupTrayIcon(bool enable)
199+
{
200+
if (enable)
201+
{
202+
var icons = new TrayIcons {
203+
new TrayIcon {
204+
Icon = new WindowIcon(new Bitmap(AssetLoader.Open(new Uri("avares://SourceGit/App.ico")))),
205+
Menu = [
206+
new NativeMenuItem(Text("Open")) {Command = Unminimize},
207+
new NativeMenuItem(Text("Preference")) {Command = OpenPreferenceCommand},
208+
new NativeMenuItemSeparator(),
209+
new NativeMenuItem(Text("Quit")) {Command = QuitCommand},
210+
]
211+
}
212+
};
213+
icons[0].Clicked += (_, _) => ToggleWindow();
214+
TrayIcon.SetIcons(Current, icons);
215+
}
216+
}
217+
218+
private static void ToggleWindow() {
219+
if (Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop) {
220+
if (desktop.MainWindow.IsVisible) {
221+
desktop.MainWindow.Hide();
222+
} else {
223+
ShowWindow();
224+
}
225+
}
226+
}
227+
228+
private static void ShowWindow()
229+
{
230+
if (Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop) {
231+
desktop.MainWindow.WindowState = WindowState.Normal;
232+
desktop.MainWindow.Show();
233+
desktop.MainWindow.BringIntoView();
234+
desktop.MainWindow.Focus();
235+
}
236+
}
195237
public static void SetFonts(string defaultFont, string monospaceFont, bool onlyUseMonospaceFontInEditor)
196238
{
197239
var app = Current as App;
@@ -545,11 +587,15 @@ private void TryLaunchedAsNormal(IClassicDesktopStyleApplicationLifetime desktop
545587
if (desktop.Args != null && desktop.Args.Length == 1 && Directory.Exists(desktop.Args[0]))
546588
startupRepo = desktop.Args[0];
547589

590+
var pref = ViewModels.Preference.Instance;
591+
if (pref.SystemTrayIcon) {
592+
desktop.ShutdownMode = ShutdownMode.OnExplicitShutdown;
593+
}
594+
548595
_launcher = new ViewModels.Launcher(startupRepo);
549596
desktop.MainWindow = new Views.Launcher() { DataContext = _launcher };
550597

551598
#if !DISABLE_UPDATE_DETECTION
552-
var pref = ViewModels.Preference.Instance;
553599
if (pref.ShouldCheck4UpdateOnStartup())
554600
Check4Update();
555601
#endif

src/Resources/Locales/en_US.axaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,7 @@
420420
<x:String x:Key="Text.MoveRepositoryNode.Target" xml:space="preserve">Select parent node for:</x:String>
421421
<x:String x:Key="Text.Name" xml:space="preserve">Name:</x:String>
422422
<x:String x:Key="Text.NotConfigured" xml:space="preserve">Git has NOT been configured. Please to go [Preference] and configure it first.</x:String>
423+
<x:String x:Key="Text.Open" xml:space="preserve">Open SourceGit</x:String>
423424
<x:String x:Key="Text.OpenAppDataDir" xml:space="preserve">Open Data Storage Directory</x:String>
424425
<x:String x:Key="Text.OpenWith" xml:space="preserve">Open with...</x:String>
425426
<x:String x:Key="Text.Optional" xml:space="preserve">Optional.</x:String>

src/ViewModels/Preference.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,12 @@ public double LastCheckUpdateTime
348348
set => SetProperty(ref _lastCheckUpdateTime, value);
349349
}
350350

351+
public bool SystemTrayIcon
352+
{
353+
get => _systemTrayIcon;
354+
set => SetProperty(ref _systemTrayIcon, value);
355+
}
356+
351357
public bool IsGitConfigured()
352358
{
353359
var path = GitInstallPath;
@@ -678,5 +684,7 @@ private string FixFontFamilyName(string name)
678684
private string _externalMergeToolPath = string.Empty;
679685

680686
private uint _statisticsSampleColor = 0xFF00FF00;
687+
688+
private bool _systemTrayIcon = false;
681689
}
682690
}

src/Views/Launcher.axaml.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,13 @@ protected override void OnKeyUp(KeyEventArgs e)
255255

256256
protected override void OnClosing(WindowClosingEventArgs e)
257257
{
258-
(DataContext as ViewModels.Launcher)?.Quit(Width, Height);
258+
var pref = ViewModels.Preference.Instance;
259+
if (pref.SystemTrayIcon) {
260+
e.Cancel = true;
261+
Hide();
262+
} else {
263+
(DataContext as ViewModels.Launcher)?.Quit(Width, Height);
264+
}
259265
base.OnClosing(e);
260266
}
261267

0 commit comments

Comments
 (0)