From a517e1d90b53ea76fdbfceb11f7730ea96afc2f9 Mon Sep 17 00:00:00 2001 From: cglcv Date: Fri, 24 Jun 2022 21:46:30 +0800 Subject: [PATCH] =?UTF-8?q?feat.=E6=89=98=E7=9B=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/MeoAsstGui/MeoAsstGui.csproj | 7 ++ src/MeoAsstGui/TrayIcon.cs | 108 ++++++++++++++++++ .../UserControl/TraySettingsUserControl.xaml | 22 ++++ .../TraySettingsUserControl.xaml.cs | 26 +++++ src/MeoAsstGui/ViewModels/RootViewModel.cs | 16 +++ .../ViewModels/SettingsViewModel.cs | 28 +++++ src/MeoAsstGui/Views/SettingsView.xaml | 5 +- 7 files changed, 211 insertions(+), 1 deletion(-) create mode 100644 src/MeoAsstGui/TrayIcon.cs create mode 100644 src/MeoAsstGui/UserControl/TraySettingsUserControl.xaml create mode 100644 src/MeoAsstGui/UserControl/TraySettingsUserControl.xaml.cs diff --git a/src/MeoAsstGui/MeoAsstGui.csproj b/src/MeoAsstGui/MeoAsstGui.csproj index e6d0e8566f..a7a39e17ba 100644 --- a/src/MeoAsstGui/MeoAsstGui.csproj +++ b/src/MeoAsstGui/MeoAsstGui.csproj @@ -100,7 +100,11 @@ + + TraySettingsUserControl.xaml + + StartSettingsUserControl.xaml @@ -198,6 +202,9 @@ MSBuild:Compile Designer + + MSBuild:Compile + Designer MSBuild:Compile diff --git a/src/MeoAsstGui/TrayIcon.cs b/src/MeoAsstGui/TrayIcon.cs new file mode 100644 index 0000000000..eb13a804d2 --- /dev/null +++ b/src/MeoAsstGui/TrayIcon.cs @@ -0,0 +1,108 @@ +// MeoAsstGui - A part of the MeoAssistantArknights project +// Copyright (C) 2021 MistEO and Contributors +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows; +using System.Windows.Forms; + +namespace MeoAsstGui +{ + public partial class TrayIcon : Window + { + + NotifyIcon notifyIcon = null; + WindowState ws; //记录窗体状态 + private bool _isMinimizeToTaskbar = Convert.ToBoolean(ViewStatusStorage.Get("MinimizeToTray", bool.FalseString)); + public TrayIcon() + { + Icon(); + } + private void Icon() + { + this.notifyIcon = new NotifyIcon(); + //this.notifyIcon.BalloonTipText = "Hello, 文件监视器"; //设置程序启动时显示的文本 + this.notifyIcon.Text = "MaaAssistantArknights";//最小化到托盘时,鼠标点击时显示的文本 + this.notifyIcon.Icon = System.Drawing.Icon.ExtractAssociatedIcon(System.Windows.Forms.Application.ExecutablePath); + this.notifyIcon.Visible = true; + notifyIcon.MouseClick += NotifyIcon_MouseClick; + notifyIcon.MouseDoubleClick += OnNotifyIconDoubleClick; + App.Current.MainWindow.StateChanged += MainWindow_StateChanged; + + MenuItem menu1 = new System.Windows.Forms.MenuItem("打开助手"); + menu1.Click += App_show; + MenuItem menu2 = new System.Windows.Forms.MenuItem("退出"); + menu2.Click += App_exit; + System.Windows.Forms.MenuItem[] menuItems = new MenuItem[] { menu1, menu2 }; + this.notifyIcon.ContextMenu = new System.Windows.Forms.ContextMenu(menuItems); + } + + private void MainWindow_StateChanged(object sender, EventArgs e) + { + ws = App.Current.MainWindow.WindowState; + if (ws == WindowState.Minimized) + { + SetShowInTaskbar(false); + } + else SetShowInTaskbar(true); + + } + + private void NotifyIcon_MouseClick(object sender, MouseEventArgs e) + { + if (ws == WindowState.Minimized) + { + ws = App.Current.MainWindow.WindowState = WindowState.Normal; + App.Current.MainWindow.Activate(); + SetShowInTaskbar(true); + } + else + { + ws = App.Current.MainWindow.WindowState = WindowState.Minimized; + SetShowInTaskbar(false); + } + } + + void App_exit(object sender, EventArgs e) + { + System.Windows.Application.Current.Shutdown(); + } + + void App_show(object sender, EventArgs e) + { + SetShowInTaskbar(true); + ws = App.Current.MainWindow.WindowState = WindowState.Normal; + App.Current.MainWindow.Activate(); + } + + private void OnNotifyIconDoubleClick(object sender, EventArgs e) + { + if (ws == WindowState.Minimized) + { + SetShowInTaskbar(true); + ws = App.Current.MainWindow.WindowState = WindowState.Normal; + App.Current.MainWindow.Activate(); + } + } + + private void SetShowInTaskbar(bool state) + { + if (_isMinimizeToTaskbar) + { + App.Current.MainWindow.ShowInTaskbar = state; + } + else return; + } + } +} diff --git a/src/MeoAsstGui/UserControl/TraySettingsUserControl.xaml b/src/MeoAsstGui/UserControl/TraySettingsUserControl.xaml new file mode 100644 index 0000000000..c3b3d44797 --- /dev/null +++ b/src/MeoAsstGui/UserControl/TraySettingsUserControl.xaml @@ -0,0 +1,22 @@ + + + + + + + + \ No newline at end of file diff --git a/src/MeoAsstGui/UserControl/TraySettingsUserControl.xaml.cs b/src/MeoAsstGui/UserControl/TraySettingsUserControl.xaml.cs new file mode 100644 index 0000000000..579a058ed2 --- /dev/null +++ b/src/MeoAsstGui/UserControl/TraySettingsUserControl.xaml.cs @@ -0,0 +1,26 @@ +// MeoAsstGui - A part of the MeoAssistantArknights project +// Copyright (C) 2021 MistEO and Contributors +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY + +using System.Windows.Controls; + +namespace MeoAsstGui +{ + /// + /// TraySettingsUserControl.xaml 的交互逻辑 + /// + public partial class TraySettingsUserControl : UserControl + { + public TraySettingsUserControl() + { + InitializeComponent(); + } + } +} diff --git a/src/MeoAsstGui/ViewModels/RootViewModel.cs b/src/MeoAsstGui/ViewModels/RootViewModel.cs index 8fd0ead121..e1020cd8f9 100644 --- a/src/MeoAsstGui/ViewModels/RootViewModel.cs +++ b/src/MeoAsstGui/ViewModels/RootViewModel.cs @@ -9,6 +9,7 @@ // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY +using System; using System.Threading.Tasks; using Stylet; using StyletIoC; @@ -17,6 +18,7 @@ namespace MeoAsstGui { public class RootViewModel : Conductor.Collection.OneActive { + private TrayIcon _trayIcon; private readonly IContainer _container; private readonly IWindowManager _windowManager; @@ -28,6 +30,11 @@ namespace MeoAsstGui protected override void OnViewLoaded() { + if (Convert.ToBoolean(ViewStatusStorage.Get("UseTray", bool.TrueString))) + { + _trayIcon = new TrayIcon(); + } + CheckAndUpdateNow(); InitProxy(); InitViewModels(); @@ -95,5 +102,14 @@ namespace MeoAsstGui get => _windowTitle; set => SetAndNotify(ref _windowTitle, value); } + protected override void OnClose() + { + base.OnClose(); + if (Convert.ToBoolean(ViewStatusStorage.Get("UseTray", bool.TrueString))) + { + _trayIcon.Close(); + _trayIcon = null; + } + } } } diff --git a/src/MeoAsstGui/ViewModels/SettingsViewModel.cs b/src/MeoAsstGui/ViewModels/SettingsViewModel.cs index c32e853b35..a31255cb41 100644 --- a/src/MeoAsstGui/ViewModels/SettingsViewModel.cs +++ b/src/MeoAsstGui/ViewModels/SettingsViewModel.cs @@ -54,6 +54,7 @@ namespace MeoAsstGui _listTitle.Add("启动设置"); _listTitle.Add("定时执行"); _listTitle.Add("通知显示"); + _listTitle.Add("托盘设置"); _listTitle.Add("软件更新"); _listTitle.Add("关于我们"); @@ -1262,5 +1263,32 @@ namespace MeoAsstGui } } } + /* 托盘设置 */ + private bool _usetray = Convert.ToBoolean(ViewStatusStorage.Get("UseTray", bool.TrueString)); + public bool UseTray + { + get { return _usetray; } + set + { + SetAndNotify(ref _usetray, value); + ViewStatusStorage.Set("UseTray", value.ToString()); + if (!Convert.ToBoolean(value)) + { + ViewStatusStorage.Set("MinimizeToTray", bool.FalseString); + MinimizeToTray = false; + } + } + } + + private bool _minimizeToTray = Convert.ToBoolean(ViewStatusStorage.Get("MinimizeToTray", bool.FalseString)); + public bool MinimizeToTray + { + get { return _minimizeToTray; } + set + { + SetAndNotify(ref _minimizeToTray, value); + ViewStatusStorage.Set("MinimizeToTray", value.ToString()); + } + } } } diff --git a/src/MeoAsstGui/Views/SettingsView.xaml b/src/MeoAsstGui/Views/SettingsView.xaml index 9a5ab3189e..9248cd8fef 100644 --- a/src/MeoAsstGui/Views/SettingsView.xaml +++ b/src/MeoAsstGui/Views/SettingsView.xaml @@ -53,9 +53,12 @@ - + + + +