feat.托盘

This commit is contained in:
cglcv
2022-06-24 21:46:30 +08:00
parent 7113dfce6b
commit a517e1d90b
7 changed files with 211 additions and 1 deletions

View File

@@ -100,7 +100,11 @@
<Compile Include="Helper\ScrollViewerBinding.cs" />
<Compile Include="Helper\ViewStatusStorage.cs" />
<Compile Include="Helper\FlowDocumentPagePadding.cs" />
<Compile Include="UserControl\TraySettingsUserControl.xaml.cs">
<DependentUpon>TraySettingsUserControl.xaml</DependentUpon>
</Compile>
<Compile Include="Helper\WinAdapter.cs" />
<Compile Include="TrayIcon.cs" />
<Compile Include="UserControl\StartSettingsUserControl.xaml.cs">
<DependentUpon>StartSettingsUserControl.xaml</DependentUpon>
</Compile>
@@ -198,6 +202,9 @@
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</Page>
<Page Include="UserControl\TraySettingsUserControl.xaml">
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="UserControl\VersionUpdateSettingsUserControl.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>

108
src/MeoAsstGui/TrayIcon.cs Normal file
View File

@@ -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;
}
}
}

View File

@@ -0,0 +1,22 @@
<UserControl x:Class="MeoAsstGui.TraySettingsUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:s="https://github.com/canton7/Stylet"
xmlns:dd="urn:gong-wpf-dragdrop"
mc:Ignorable="d"
xmlns:vm="clr-namespace:MeoAsstGui;assembly=MeoAsstGui"
d:DataContext="{d:DesignInstance {x:Type vm:SettingsViewModel}}"
d:DesignHeight="80" d:DesignWidth="550">
<StackPanel VerticalAlignment="Center" Margin="10">
<TextBlock HorizontalAlignment="Center" Block.TextAlignment="Center"
Style="{StaticResource TextBlockDefault}"
TextWrapping="Wrap"
Text="以下设置重启助手后生效"
Margin="10" />
<CheckBox IsChecked="{Binding UseTray}" Content="使用托盘" Margin="10" />
<CheckBox IsChecked="{Binding MinimizeToTray}" IsEnabled="{Binding UseTray}" Content="单击最小化时隐藏至托盘" Margin="10" />
</StackPanel>
</UserControl>

View File

@@ -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
{
/// <summary>
/// TraySettingsUserControl.xaml 的交互逻辑
/// </summary>
public partial class TraySettingsUserControl : UserControl
{
public TraySettingsUserControl()
{
InitializeComponent();
}
}
}

View File

@@ -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<Screen>.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;
}
}
}
}

View File

@@ -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());
}
}
}
}

View File

@@ -53,9 +53,12 @@
<local:ToastSettingsUserControl Margin="20" />
<Rectangle HorizontalAlignment="Stretch" Fill="LightGray" Height="1" />
<TextBlock Style="{StaticResource TextBlockDefault}" Text="{Binding ListTitle[9]}" Foreground="Gray" Margin="5" VerticalAlignment="Center" HorizontalAlignment="Left" />
<local:VersionUpdateSettingsUserControl Margin="20" />
<local:TraySettingsUserControl Margin="20" />
<Rectangle HorizontalAlignment="Stretch" Fill="LightGray" Height="1" />
<TextBlock Style="{StaticResource TextBlockDefault}" Text="{Binding ListTitle[10]}" Foreground="Gray" Margin="5" VerticalAlignment="Center" HorizontalAlignment="Left" />
<local:VersionUpdateSettingsUserControl Margin="20" />
<Rectangle HorizontalAlignment="Stretch" Fill="LightGray" Height="1" />
<TextBlock Style="{StaticResource TextBlockDefault}" Text="{Binding ListTitle[11]}" Foreground="Gray" Margin="5" VerticalAlignment="Center" HorizontalAlignment="Left" />
<local:AboutUserControl Margin="20" />
<!--<Rectangle HorizontalAlignment="Stretch" Fill="LightGray" Height="1" />-->
</StackPanel>