add cefcharp example
Browse filesThis view is limited to 50 files because it contains too many changes.
See raw diff
- .gitignore +1 -0
- csharp/BrowserTool/.cursorignore +34 -0
- csharp/BrowserTool/.gitattributes +72 -0
- csharp/BrowserTool/.gitignore +7 -0
- csharp/BrowserTool/BrowserTool.sln +22 -0
- csharp/BrowserTool/BrowserTool/App.config +28 -0
- csharp/BrowserTool/BrowserTool/App.xaml +49 -0
- csharp/BrowserTool/BrowserTool/App.xaml.cs +792 -0
- csharp/BrowserTool/BrowserTool/Browser/BrowserInstanceManager.cs +214 -0
- csharp/BrowserTool/BrowserTool/Browser/BrowserTabManager.cs +79 -0
- csharp/BrowserTool/BrowserTool/Browser/CefDownloadHandler.cs +69 -0
- csharp/BrowserTool/BrowserTool/Browser/CefHelper.cs +21 -0
- csharp/BrowserTool/BrowserTool/Browser/CefLifeSpanHandler.cs +25 -0
- csharp/BrowserTool/BrowserTool/Browser/CefMenuHandler.cs +170 -0
- csharp/BrowserTool/BrowserTool/Browser/CustomTabItem.xaml +39 -0
- csharp/BrowserTool/BrowserTool/Browser/CustomTabItem.xaml.cs +115 -0
- csharp/BrowserTool/BrowserTool/Browser/DarkThemeStyleInjector.cs +77 -0
- csharp/BrowserTool/BrowserTool/Browser/DownloadManager.cs +57 -0
- csharp/BrowserTool/BrowserTool/Browser/DownloadManagerWindow.xaml +41 -0
- csharp/BrowserTool/BrowserTool/Browser/DownloadManagerWindow.xaml.cs +70 -0
- csharp/BrowserTool/BrowserTool/Browser/DownloadTask.cs +131 -0
- csharp/BrowserTool/BrowserTool/Browser/InverseBoolConverter.cs +23 -0
- csharp/BrowserTool/BrowserTool/BrowserTool.csproj +64 -0
- csharp/BrowserTool/BrowserTool/ChangePasswordWindow.xaml +59 -0
- csharp/BrowserTool/BrowserTool/ChangePasswordWindow.xaml.cs +68 -0
- csharp/BrowserTool/BrowserTool/CheckInResultQueue.cs +182 -0
- csharp/BrowserTool/BrowserTool/Controls/LoadingControl.xaml +67 -0
- csharp/BrowserTool/BrowserTool/Controls/LoadingControl.xaml.cs +25 -0
- csharp/BrowserTool/BrowserTool/Controls/SearchBar.xaml +193 -0
- csharp/BrowserTool/BrowserTool/Controls/SearchBar.xaml.cs +221 -0
- csharp/BrowserTool/BrowserTool/Converters/BooleanToVisibilityConverter.cs +28 -0
- csharp/BrowserTool/BrowserTool/Database/AppDbContext.cs +100 -0
- csharp/BrowserTool/BrowserTool/Database/ApplicationDbContext.cs +58 -0
- csharp/BrowserTool/BrowserTool/Database/DatabaseInitializer.cs +66 -0
- csharp/BrowserTool/BrowserTool/Database/Entities/SiteGroup.cs +34 -0
- csharp/BrowserTool/BrowserTool/Database/Entities/SiteItem.cs +88 -0
- csharp/BrowserTool/BrowserTool/Database/Migrations/AddIsDefaultExpandedToSiteGroup.cs +24 -0
- csharp/BrowserTool/BrowserTool/Database/Migrations/InitialCreate.cs +104 -0
- csharp/BrowserTool/BrowserTool/Database/SiteConfig.cs +553 -0
- csharp/BrowserTool/BrowserTool/GroupEditDialog.xaml +122 -0
- csharp/BrowserTool/BrowserTool/GroupEditDialog.xaml.cs +44 -0
- csharp/BrowserTool/BrowserTool/IsTreeViewRootConverter.cs +34 -0
- csharp/BrowserTool/BrowserTool/LoginWindow.xaml +107 -0
- csharp/BrowserTool/BrowserTool/LoginWindow.xaml.cs +66 -0
- csharp/BrowserTool/BrowserTool/MainWindow.xaml +916 -0
- csharp/BrowserTool/BrowserTool/MainWindow.xaml.cs +2413 -0
- csharp/BrowserTool/BrowserTool/NLog.config +39 -0
- csharp/BrowserTool/BrowserTool/Properties/AssemblyInfo.cs +52 -0
- csharp/BrowserTool/BrowserTool/Properties/Resources.Designer.cs +71 -0
- csharp/BrowserTool/BrowserTool/Properties/Resources.resx +117 -0
.gitignore
CHANGED
|
@@ -31,3 +31,4 @@ imgui.ini
|
|
| 31 |
7e644cf859a5e100a21ff67a63486ae0.json
|
| 32 |
__pycache__/
|
| 33 |
|
|
|
|
|
|
| 31 |
7e644cf859a5e100a21ff67a63486ae0.json
|
| 32 |
__pycache__/
|
| 33 |
|
| 34 |
+
csharp/BrowserTool/db/sites.db
|
csharp/BrowserTool/.cursorignore
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Build outputs
|
| 2 |
+
bin/
|
| 3 |
+
obj/
|
| 4 |
+
dist/
|
| 5 |
+
build/
|
| 6 |
+
|
| 7 |
+
# Dependencies
|
| 8 |
+
node_modules/
|
| 9 |
+
packages/
|
| 10 |
+
.vs/
|
| 11 |
+
|
| 12 |
+
# IDE files
|
| 13 |
+
.vscode/
|
| 14 |
+
.idea/
|
| 15 |
+
|
| 16 |
+
# Logs and databases
|
| 17 |
+
*.log
|
| 18 |
+
*.sqlite
|
| 19 |
+
*.db
|
| 20 |
+
|
| 21 |
+
# Temporary files
|
| 22 |
+
*.tmp
|
| 23 |
+
*.temp
|
| 24 |
+
*.swp
|
| 25 |
+
*~
|
| 26 |
+
|
| 27 |
+
# Large binary files
|
| 28 |
+
*.zip
|
| 29 |
+
*.rar
|
| 30 |
+
*.7z
|
| 31 |
+
*.tar
|
| 32 |
+
*.gz
|
| 33 |
+
*.iso
|
| 34 |
+
*.img
|
csharp/BrowserTool/.gitattributes
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
| 2 |
+
*.db filter=lfs diff=lfs merge=lfs -text
|
| 3 |
+
*.pdf filter=lfs diff=lfs merge=lfs -text
|
| 4 |
+
**/data/img/*.txt filter=lfs diff=lfs merge=lfs -text
|
| 5 |
+
src/WeChatOcrCpp/wco_data/**/* filter=lfs diff=lfs merge=lfs -text
|
| 6 |
+
*.aps filter=lfs diff=lfs merge=lfs -text
|
| 7 |
+
*.ico filter=lfs diff=lfs merge=lfs -text
|
| 8 |
+
*.pgm filter=lfs diff=lfs merge=lfs -text
|
| 9 |
+
*.exe filter=lfs diff=lfs merge=lfs -text
|
| 10 |
+
*.dll filter=lfs diff=lfs merge=lfs -text
|
| 11 |
+
*.lib filter=lfs diff=lfs merge=lfs -text
|
| 12 |
+
*.exp filter=lfs diff=lfs merge=lfs -text
|
| 13 |
+
data/img/*.txt filter=lfs diff=lfs merge=lfs -text
|
| 14 |
+
*.ttf filter=lfs diff=lfs merge=lfs -text
|
| 15 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
| 16 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
| 17 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
| 18 |
+
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
| 19 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
| 20 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
| 21 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
| 22 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
| 23 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
| 24 |
+
*.lz4 filter=lfs diff=lfs merge=lfs -text
|
| 25 |
+
*.mds filter=lfs diff=lfs merge=lfs -text
|
| 26 |
+
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
| 27 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
| 28 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
| 29 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
| 30 |
+
*.npz filter=lfs diff=lfs merge=lfs -text
|
| 31 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
| 32 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
| 33 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
| 34 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
| 35 |
+
*.pickle filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
| 37 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
| 38 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
| 39 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
| 40 |
+
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
| 41 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
| 42 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
| 43 |
+
*.tar filter=lfs diff=lfs merge=lfs -text
|
| 44 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
| 45 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
| 46 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
| 47 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
| 48 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 49 |
+
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 50 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 51 |
+
# Audio files - uncompressed
|
| 52 |
+
*.pcm filter=lfs diff=lfs merge=lfs -text
|
| 53 |
+
*.sam filter=lfs diff=lfs merge=lfs -text
|
| 54 |
+
*.raw filter=lfs diff=lfs merge=lfs -text
|
| 55 |
+
# Audio files - compressed
|
| 56 |
+
*.aac filter=lfs diff=lfs merge=lfs -text
|
| 57 |
+
*.flac filter=lfs diff=lfs merge=lfs -text
|
| 58 |
+
*.mp3 filter=lfs diff=lfs merge=lfs -text
|
| 59 |
+
*.ogg filter=lfs diff=lfs merge=lfs -text
|
| 60 |
+
*.wav filter=lfs diff=lfs merge=lfs -text
|
| 61 |
+
# Image files - uncompressed
|
| 62 |
+
*.bmp filter=lfs diff=lfs merge=lfs -text
|
| 63 |
+
*.gif filter=lfs diff=lfs merge=lfs -text
|
| 64 |
+
*.png filter=lfs diff=lfs merge=lfs -text
|
| 65 |
+
*.tiff filter=lfs diff=lfs merge=lfs -text
|
| 66 |
+
# Image files - compressed
|
| 67 |
+
*.jpg filter=lfs diff=lfs merge=lfs -text
|
| 68 |
+
*.jpeg filter=lfs diff=lfs merge=lfs -text
|
| 69 |
+
*.webp filter=lfs diff=lfs merge=lfs -text
|
| 70 |
+
# Video files - compressed
|
| 71 |
+
*.mp4 filter=lfs diff=lfs merge=lfs -text
|
| 72 |
+
*.webm filter=lfs diff=lfs merge=lfs -text
|
csharp/BrowserTool/.gitignore
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
**/bin/
|
| 2 |
+
**/obj/
|
| 3 |
+
**/node_modules/
|
| 4 |
+
**/packages/
|
| 5 |
+
.vs
|
| 6 |
+
**/.vs/
|
| 7 |
+
.vs/
|
csharp/BrowserTool/BrowserTool.sln
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
Microsoft Visual Studio Solution File, Format Version 12.00
|
| 3 |
+
# Visual Studio Version 17
|
| 4 |
+
VisualStudioVersion = 17.12.35527.113
|
| 5 |
+
MinimumVisualStudioVersion = 10.0.40219.1
|
| 6 |
+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BrowserTool", "BrowserTool\BrowserTool.csproj", "{D3A2273B-2DA3-4C63-9B54-DF3702B3D432}"
|
| 7 |
+
EndProject
|
| 8 |
+
Global
|
| 9 |
+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
| 10 |
+
Debug|Any CPU = Debug|Any CPU
|
| 11 |
+
Release|Any CPU = Release|Any CPU
|
| 12 |
+
EndGlobalSection
|
| 13 |
+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
| 14 |
+
{D3A2273B-2DA3-4C63-9B54-DF3702B3D432}.Debug|Any CPU.ActiveCfg = Debug|x64
|
| 15 |
+
{D3A2273B-2DA3-4C63-9B54-DF3702B3D432}.Debug|Any CPU.Build.0 = Debug|x64
|
| 16 |
+
{D3A2273B-2DA3-4C63-9B54-DF3702B3D432}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
| 17 |
+
{D3A2273B-2DA3-4C63-9B54-DF3702B3D432}.Release|Any CPU.Build.0 = Release|Any CPU
|
| 18 |
+
EndGlobalSection
|
| 19 |
+
GlobalSection(SolutionProperties) = preSolution
|
| 20 |
+
HideSolutionNode = FALSE
|
| 21 |
+
EndGlobalSection
|
| 22 |
+
EndGlobal
|
csharp/BrowserTool/BrowserTool/App.config
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<?xml version="1.0" encoding="utf-8" ?>
|
| 2 |
+
<configuration>
|
| 3 |
+
<startup>
|
| 4 |
+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" />
|
| 5 |
+
</startup>
|
| 6 |
+
<appSettings>
|
| 7 |
+
<!-- 是否启用自动签到 -->
|
| 8 |
+
<add key="CheckInEnabled" value="true" />
|
| 9 |
+
<!-- 早签时间 -->
|
| 10 |
+
<add key="MorningCheckInTime" value="08:50" />
|
| 11 |
+
<!-- 晚签时间 -->
|
| 12 |
+
<add key="EveningCheckInTime" value="18:50" />
|
| 13 |
+
<!-- 随机分钟数 -->
|
| 14 |
+
<add key="RandomMinutes" value="10" />
|
| 15 |
+
<!-- 是否启用鼠标活动模拟 -->
|
| 16 |
+
<add key="EnableMouseActivitySimulation" value="true" />
|
| 17 |
+
<!-- 鼠标活动间隔(毫秒) -->
|
| 18 |
+
<add key="MouseActivityInterval" value="120000" />
|
| 19 |
+
<!-- 鼠标移动步长 -->
|
| 20 |
+
<add key="MouseMoveStep" value="1" />
|
| 21 |
+
<!-- 测试环境URL -->
|
| 22 |
+
<add key="TestEnvironmentUrl" value="https://www.qq.com/" />
|
| 23 |
+
<!-- 是否显示手动签到按钮 -->
|
| 24 |
+
<add key="ShowManualCheckInButton" value="true" />
|
| 25 |
+
<!-- 签结果需要匹配的域名列表,用逗号分隔 -->
|
| 26 |
+
<add key="CheckInDomains" value="example.com,test.example.com,dev.example.com" />
|
| 27 |
+
</appSettings>
|
| 28 |
+
</configuration>
|
csharp/BrowserTool/BrowserTool/App.xaml
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<Application x:Class="BrowserTool.App"
|
| 2 |
+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
| 3 |
+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
| 4 |
+
xmlns:tb="http://www.hardcodet.net/taskbar">
|
| 5 |
+
<Application.Resources>
|
| 6 |
+
<ResourceDictionary>
|
| 7 |
+
<!-- 应用程序矢量图标 -->
|
| 8 |
+
<DrawingImage x:Key="AppIcon">
|
| 9 |
+
<DrawingImage.Drawing>
|
| 10 |
+
<GeometryDrawing Brush="RoyalBlue">
|
| 11 |
+
<GeometryDrawing.Geometry>
|
| 12 |
+
<EllipseGeometry Center="8,8" RadiusX="8" RadiusY="8"/>
|
| 13 |
+
</GeometryDrawing.Geometry>
|
| 14 |
+
<GeometryDrawing.Pen>
|
| 15 |
+
<Pen Brush="White" Thickness="2"/>
|
| 16 |
+
</GeometryDrawing.Pen>
|
| 17 |
+
</GeometryDrawing>
|
| 18 |
+
</DrawingImage.Drawing>
|
| 19 |
+
</DrawingImage>
|
| 20 |
+
|
| 21 |
+
<!-- 锁图标 -->
|
| 22 |
+
<DrawingImage x:Key="LockIcon">
|
| 23 |
+
<DrawingImage.Drawing>
|
| 24 |
+
<GeometryDrawing Brush="#2196F3">
|
| 25 |
+
<GeometryDrawing.Geometry>
|
| 26 |
+
<PathGeometry Figures="M12,17A2,2 0 0,0 14,15C14,13.89 13.1,13 12,13A2,2 0 0,0 10,15A2,2 0 0,0 12,17M18,8A2,2 0 0,1 20,10V20A2,2 0 0,1 18,22H6A2,2 0 0,1 4,20V10C4,8.89 4.9,8 6,8H7V6A5,5 0 0,1 12,1A5,5 0 0,1 17,6V8H18M12,3A3,3 0 0,0 9,6V8H15V6A3,3 0 0,0 12,3Z"/>
|
| 27 |
+
</GeometryDrawing.Geometry>
|
| 28 |
+
</GeometryDrawing>
|
| 29 |
+
</DrawingImage.Drawing>
|
| 30 |
+
</DrawingImage>
|
| 31 |
+
|
| 32 |
+
<tb:TaskbarIcon x:Key="TrayIcon"
|
| 33 |
+
IconSource="Resources/app.ico"
|
| 34 |
+
ToolTipText="Browser Tool"
|
| 35 |
+
TrayMouseDoubleClick="TrayIcon_TrayMouseDoubleClick">
|
| 36 |
+
<tb:TaskbarIcon.ContextMenu>
|
| 37 |
+
<ContextMenu>
|
| 38 |
+
<MenuItem Header="显示主窗口" Click="TrayMenu_Show_Click" />
|
| 39 |
+
<MenuItem Header="设置" Click="TrayMenu_Settings_Click" />
|
| 40 |
+
<MenuItem Header="修改密码" Click="TrayMenu_ChangePassword_Click" />
|
| 41 |
+
<Separator />
|
| 42 |
+
<MenuItem Header="退出" Click="TrayMenu_Exit_Click" />
|
| 43 |
+
</ContextMenu>
|
| 44 |
+
</tb:TaskbarIcon.ContextMenu>
|
| 45 |
+
</tb:TaskbarIcon>
|
| 46 |
+
<BooleanToVisibilityConverter x:Key="BoolToVisibility"/>
|
| 47 |
+
</ResourceDictionary>
|
| 48 |
+
</Application.Resources>
|
| 49 |
+
</Application>
|
csharp/BrowserTool/BrowserTool/App.xaml.cs
ADDED
|
@@ -0,0 +1,792 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
using System;
|
| 2 |
+
using System.Diagnostics;
|
| 3 |
+
using System.Drawing;
|
| 4 |
+
using System.IO;
|
| 5 |
+
using System.IO.Pipes;
|
| 6 |
+
using System.Reflection;
|
| 7 |
+
using System.Text;
|
| 8 |
+
using System.Threading;
|
| 9 |
+
using System.Threading.Tasks;
|
| 10 |
+
using System.Windows;
|
| 11 |
+
using CefSharp;
|
| 12 |
+
using Hardcodet.Wpf.TaskbarNotification;
|
| 13 |
+
using System.Runtime.InteropServices;
|
| 14 |
+
using BrowserTool.Database;
|
| 15 |
+
using BrowserTool.Utils;
|
| 16 |
+
using System.Windows.Controls;
|
| 17 |
+
using CefSharp.Wpf;
|
| 18 |
+
using FontStyle = System.Drawing.FontStyle;
|
| 19 |
+
using NLog;
|
| 20 |
+
using NLog.Config;
|
| 21 |
+
using NLog.Targets;
|
| 22 |
+
|
| 23 |
+
namespace BrowserTool
|
| 24 |
+
{
|
| 25 |
+
/// <summary>
|
| 26 |
+
/// App.xaml 的交互逻辑,提供应用程序的生命周期管理和单例模式支持
|
| 27 |
+
/// </summary>
|
| 28 |
+
public partial class App : Application
|
| 29 |
+
{
|
| 30 |
+
#region 常量定义
|
| 31 |
+
|
| 32 |
+
private const string MUTEX_NAME = "BrowserTool_SingleInstance";
|
| 33 |
+
private const string PIPE_NAME = "BrowserToolUrlPipe";
|
| 34 |
+
private const string WINDOW_MESSAGE_NAME = "BrowserTool_ShowMainWindow";
|
| 35 |
+
|
| 36 |
+
#endregion
|
| 37 |
+
|
| 38 |
+
#region Win32 API 导入
|
| 39 |
+
|
| 40 |
+
[DllImport("user32.dll", SetLastError = true)]
|
| 41 |
+
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
|
| 42 |
+
|
| 43 |
+
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
|
| 44 |
+
private static extern int RegisterWindowMessage(string lpString);
|
| 45 |
+
|
| 46 |
+
[DllImport("user32.dll")]
|
| 47 |
+
private static extern bool SetForegroundWindow(IntPtr hWnd);
|
| 48 |
+
|
| 49 |
+
[DllImport("user32.dll")]
|
| 50 |
+
private static extern bool IsIconic(IntPtr hWnd);
|
| 51 |
+
|
| 52 |
+
[DllImport("user32.dll")]
|
| 53 |
+
private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
|
| 54 |
+
|
| 55 |
+
[DllImport("user32.dll")]
|
| 56 |
+
private static extern bool PostMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);
|
| 57 |
+
|
| 58 |
+
#endregion
|
| 59 |
+
|
| 60 |
+
#region 字段和属性
|
| 61 |
+
|
| 62 |
+
/// <summary>
|
| 63 |
+
/// CEF初始化锁对象
|
| 64 |
+
/// </summary>
|
| 65 |
+
private static readonly object _cefInitLock = new();
|
| 66 |
+
|
| 67 |
+
/// <summary>
|
| 68 |
+
/// 主窗口实例
|
| 69 |
+
/// </summary>
|
| 70 |
+
private MainWindow _mainWindow;
|
| 71 |
+
|
| 72 |
+
/// <summary>
|
| 73 |
+
/// 单例应用程序互斥锁
|
| 74 |
+
/// </summary>
|
| 75 |
+
private static readonly Mutex _mutex = new(true, MUTEX_NAME);
|
| 76 |
+
|
| 77 |
+
/// <summary>
|
| 78 |
+
/// 是否拥有互斥锁
|
| 79 |
+
/// </summary>
|
| 80 |
+
private static bool _mutexOwned;
|
| 81 |
+
|
| 82 |
+
/// <summary>
|
| 83 |
+
/// 命名管道服务器线程
|
| 84 |
+
/// </summary>
|
| 85 |
+
private Thread _pipeServerThread;
|
| 86 |
+
|
| 87 |
+
/// <summary>
|
| 88 |
+
/// 命名管道服务器取消令牌源
|
| 89 |
+
/// </summary>
|
| 90 |
+
private CancellationTokenSource _pipeServerCancellation;
|
| 91 |
+
|
| 92 |
+
/// <summary>
|
| 93 |
+
/// 鼠标活动模拟器
|
| 94 |
+
/// </summary>
|
| 95 |
+
private static MouseActivitySimulator _mouseActivitySimulator;
|
| 96 |
+
|
| 97 |
+
/// <summary>
|
| 98 |
+
/// 自动签到模拟器
|
| 99 |
+
/// </summary>
|
| 100 |
+
private static AutoCheckInSimulator _checkInSimulator;
|
| 101 |
+
|
| 102 |
+
/// <summary>
|
| 103 |
+
/// 自定义窗口消息ID
|
| 104 |
+
/// </summary>
|
| 105 |
+
private static readonly int WM_SHOWMAINWINDOW_CUSTOM = RegisterWindowMessage(WINDOW_MESSAGE_NAME);
|
| 106 |
+
|
| 107 |
+
/// <summary>
|
| 108 |
+
/// 日志记录器
|
| 109 |
+
/// </summary>
|
| 110 |
+
private static readonly Logger _logger = LogManager.GetCurrentClassLogger();
|
| 111 |
+
|
| 112 |
+
#endregion
|
| 113 |
+
|
| 114 |
+
#region 托盘菜单事件处理
|
| 115 |
+
|
| 116 |
+
/// <summary>
|
| 117 |
+
/// 处理设置菜单点击事件
|
| 118 |
+
/// </summary>
|
| 119 |
+
/// <param name="sender">事件发送者</param>
|
| 120 |
+
/// <param name="e">事件参数</param>
|
| 121 |
+
private void TrayMenu_Settings_Click(object sender, RoutedEventArgs e)
|
| 122 |
+
{
|
| 123 |
+
if (!LoginManager.IsLoggedIn) return;
|
| 124 |
+
|
| 125 |
+
try
|
| 126 |
+
{
|
| 127 |
+
var settingsWindow = new SettingsWindow
|
| 128 |
+
{
|
| 129 |
+
Owner = _mainWindow
|
| 130 |
+
};
|
| 131 |
+
settingsWindow.ShowDialog();
|
| 132 |
+
}
|
| 133 |
+
catch (Exception ex)
|
| 134 |
+
{
|
| 135 |
+
LogError("打开设置窗口时发生错误", ex);
|
| 136 |
+
ShowErrorMessage("无法打开设置窗口,请重试。");
|
| 137 |
+
}
|
| 138 |
+
}
|
| 139 |
+
|
| 140 |
+
/// <summary>
|
| 141 |
+
/// 处理退出菜单点击事件
|
| 142 |
+
/// </summary>
|
| 143 |
+
/// <param name="sender">事件发送者</param>
|
| 144 |
+
/// <param name="e">事件参数</param>
|
| 145 |
+
private void TrayMenu_Exit_Click(object sender, RoutedEventArgs e) => Shutdown();
|
| 146 |
+
|
| 147 |
+
/// <summary>
|
| 148 |
+
/// 处理修改密码菜单点击事件
|
| 149 |
+
/// </summary>
|
| 150 |
+
/// <param name="sender">事件发送者</param>
|
| 151 |
+
/// <param name="e">事件参数</param>
|
| 152 |
+
private void TrayMenu_ChangePassword_Click(object sender, RoutedEventArgs e)
|
| 153 |
+
{
|
| 154 |
+
if (!Utils.LoginManager.IsLoggedIn) return;
|
| 155 |
+
|
| 156 |
+
try
|
| 157 |
+
{
|
| 158 |
+
var changePasswordWindow = new ChangePasswordWindow
|
| 159 |
+
{
|
| 160 |
+
Owner = _mainWindow
|
| 161 |
+
};
|
| 162 |
+
changePasswordWindow.ShowDialog();
|
| 163 |
+
}
|
| 164 |
+
catch (Exception ex)
|
| 165 |
+
{
|
| 166 |
+
LogError("打开修改密码窗口时发生错误", ex);
|
| 167 |
+
ShowErrorMessage("无法打开修改密码窗口,请重试。");
|
| 168 |
+
}
|
| 169 |
+
}
|
| 170 |
+
|
| 171 |
+
/// <summary>
|
| 172 |
+
/// 处理显示主窗口菜单点击事件
|
| 173 |
+
/// </summary>
|
| 174 |
+
/// <param name="sender">事件发送者</param>
|
| 175 |
+
/// <param name="e">事件参数</param>
|
| 176 |
+
private void TrayMenu_Show_Click(object sender, RoutedEventArgs e)
|
| 177 |
+
{
|
| 178 |
+
if (Utils.LoginManager.IsLoggedIn)
|
| 179 |
+
{
|
| 180 |
+
ShowMainWindow();
|
| 181 |
+
}
|
| 182 |
+
}
|
| 183 |
+
|
| 184 |
+
/// <summary>
|
| 185 |
+
/// 处理托盘图标双击事件
|
| 186 |
+
/// </summary>
|
| 187 |
+
/// <param name="sender">事件发送者</param>
|
| 188 |
+
/// <param name="e">事件参数</param>
|
| 189 |
+
private void TrayIcon_TrayMouseDoubleClick(object sender, RoutedEventArgs e) => ShowMainWindow();
|
| 190 |
+
|
| 191 |
+
#endregion
|
| 192 |
+
|
| 193 |
+
#region 公共方法
|
| 194 |
+
|
| 195 |
+
/// <summary>
|
| 196 |
+
/// 显示主窗口并激活
|
| 197 |
+
/// </summary>
|
| 198 |
+
public void ShowMainWindow()
|
| 199 |
+
{
|
| 200 |
+
if (_mainWindow == null) return;
|
| 201 |
+
|
| 202 |
+
try
|
| 203 |
+
{
|
| 204 |
+
_mainWindow.Show();
|
| 205 |
+
_mainWindow.WindowState = WindowState.Normal;
|
| 206 |
+
_mainWindow.Activate();
|
| 207 |
+
|
| 208 |
+
// 确保窗口在最前面,然后取消置顶
|
| 209 |
+
_mainWindow.Topmost = true;
|
| 210 |
+
_mainWindow.Topmost = false;
|
| 211 |
+
}
|
| 212 |
+
catch (Exception ex)
|
| 213 |
+
{
|
| 214 |
+
LogError("显示主窗口时发生错误", ex);
|
| 215 |
+
}
|
| 216 |
+
}
|
| 217 |
+
|
| 218 |
+
/// <summary>
|
| 219 |
+
/// 获取当前应用程序实例
|
| 220 |
+
/// </summary>
|
| 221 |
+
/// <returns>当前应用程序实例</returns>
|
| 222 |
+
public static App GetCurrentApp() => Current as App;
|
| 223 |
+
|
| 224 |
+
/// <summary>
|
| 225 |
+
/// 获取AutoCheckInSimulator实例
|
| 226 |
+
/// </summary>
|
| 227 |
+
/// <returns>AutoCheckInSimulator实例</returns>
|
| 228 |
+
public static AutoCheckInSimulator GetAutoCheckInSimulator() => _checkInSimulator;
|
| 229 |
+
|
| 230 |
+
#endregion
|
| 231 |
+
|
| 232 |
+
#region 私有方法
|
| 233 |
+
|
| 234 |
+
/// <summary>
|
| 235 |
+
/// 初始化CEF浏览器引擎
|
| 236 |
+
/// </summary>
|
| 237 |
+
/// <exception cref="InvalidOperationException">CEF初始化失败时抛出</exception>
|
| 238 |
+
private void InitializeCef()
|
| 239 |
+
{
|
| 240 |
+
if (Cef.IsInitialized == true) return;
|
| 241 |
+
|
| 242 |
+
lock (_cefInitLock)
|
| 243 |
+
{
|
| 244 |
+
if (Cef.IsInitialized == true) return;
|
| 245 |
+
|
| 246 |
+
try
|
| 247 |
+
{
|
| 248 |
+
var settings = CreateCefSettings();
|
| 249 |
+
Cef.Initialize(settings);
|
| 250 |
+
}
|
| 251 |
+
catch (Exception ex)
|
| 252 |
+
{
|
| 253 |
+
LogError("CEF初始化失败", ex);
|
| 254 |
+
ShowErrorMessage($"浏览器引擎初始化失败: {ex.Message}");
|
| 255 |
+
throw new InvalidOperationException("CEF初始化失败", ex);
|
| 256 |
+
}
|
| 257 |
+
}
|
| 258 |
+
}
|
| 259 |
+
|
| 260 |
+
/// <summary>
|
| 261 |
+
/// 创建CEF设置配置
|
| 262 |
+
/// </summary>
|
| 263 |
+
/// <returns>配置好的CEF设置</returns>
|
| 264 |
+
private static CefSettings CreateCefSettings()
|
| 265 |
+
{
|
| 266 |
+
var settings = new CefSettings();
|
| 267 |
+
string exePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)
|
| 268 |
+
?? throw new InvalidOperationException("无法获取应用程序路径");
|
| 269 |
+
|
| 270 |
+
// 设置缓存路径
|
| 271 |
+
string cachePath = Path.Combine(exePath, "CEF");
|
| 272 |
+
Directory.CreateDirectory(cachePath);
|
| 273 |
+
settings.CachePath = cachePath;
|
| 274 |
+
|
| 275 |
+
// 设置日志文件路径
|
| 276 |
+
string logPath = Path.Combine(exePath, @"CEF\Log");
|
| 277 |
+
Directory.CreateDirectory(logPath);
|
| 278 |
+
settings.LogFile = Path.Combine(logPath, "cef.log");
|
| 279 |
+
|
| 280 |
+
// 基本设置
|
| 281 |
+
settings.Locale = "zh-CN";
|
| 282 |
+
settings.PersistSessionCookies = true;
|
| 283 |
+
|
| 284 |
+
// 性能优化设置
|
| 285 |
+
ConfigureCefPerformance(settings);
|
| 286 |
+
|
| 287 |
+
return settings;
|
| 288 |
+
}
|
| 289 |
+
|
| 290 |
+
/// <summary>
|
| 291 |
+
/// 配置CEF性能优化参数
|
| 292 |
+
/// </summary>
|
| 293 |
+
/// <param name="settings">CEF设置对象</param>
|
| 294 |
+
private static void ConfigureCefPerformance(CefSettings settings)
|
| 295 |
+
{
|
| 296 |
+
var args = settings.CefCommandLineArgs;
|
| 297 |
+
|
| 298 |
+
// 启用硬件加速
|
| 299 |
+
args.Add("enable-gpu", "1");
|
| 300 |
+
args.Add("enable-gpu-compositing", "1");
|
| 301 |
+
args.Add("enable-gpu-rasterization", "1");
|
| 302 |
+
|
| 303 |
+
// 内存优化
|
| 304 |
+
args.Add("disable-gpu-shader-disk-cache", "1");
|
| 305 |
+
args.Add("renderer-process-limit", "1");
|
| 306 |
+
args.Add("disable-extensions", "1");
|
| 307 |
+
args.Add("disable-component-update", "1");
|
| 308 |
+
}
|
| 309 |
+
|
| 310 |
+
/// <summary>
|
| 311 |
+
/// 根据登录状态更新托盘菜单
|
| 312 |
+
/// </summary>
|
| 313 |
+
/// <param name="isLoggedIn">是否已登录</param>
|
| 314 |
+
private void UpdateTrayMenuState(bool isLoggedIn)
|
| 315 |
+
{
|
| 316 |
+
try
|
| 317 |
+
{
|
| 318 |
+
if (Current.Resources["TrayIcon"] is not TaskbarIcon trayIcon ||
|
| 319 |
+
trayIcon.ContextMenu?.Items == null) return;
|
| 320 |
+
|
| 321 |
+
foreach (var item in trayIcon.ContextMenu.Items)
|
| 322 |
+
{
|
| 323 |
+
if (item is not MenuItem menuItem) continue;
|
| 324 |
+
|
| 325 |
+
switch (menuItem.Header?.ToString())
|
| 326 |
+
{
|
| 327 |
+
case "设置":
|
| 328 |
+
UpdateMenuItem(menuItem, isLoggedIn, "请先登录后再使用设置功能");
|
| 329 |
+
break;
|
| 330 |
+
case "修改密码":
|
| 331 |
+
UpdateMenuItem(menuItem, isLoggedIn, "请先登录后再修改密码");
|
| 332 |
+
break;
|
| 333 |
+
}
|
| 334 |
+
}
|
| 335 |
+
|
| 336 |
+
Debug.WriteLine($"[托盘菜单状态更新] 登录状态: {isLoggedIn}");
|
| 337 |
+
}
|
| 338 |
+
catch (Exception ex)
|
| 339 |
+
{
|
| 340 |
+
LogError("更新托盘菜单状态时发生错误", ex);
|
| 341 |
+
}
|
| 342 |
+
}
|
| 343 |
+
|
| 344 |
+
/// <summary>
|
| 345 |
+
/// 更新菜单项状态
|
| 346 |
+
/// </summary>
|
| 347 |
+
/// <param name="menuItem">菜单项</param>
|
| 348 |
+
/// <param name="isEnabled">是否启用</param>
|
| 349 |
+
/// <param name="disabledTooltip">禁用时的提示文本</param>
|
| 350 |
+
private static void UpdateMenuItem(MenuItem menuItem, bool isEnabled, string disabledTooltip)
|
| 351 |
+
{
|
| 352 |
+
menuItem.IsEnabled = isEnabled;
|
| 353 |
+
menuItem.ToolTip = isEnabled ? null : disabledTooltip;
|
| 354 |
+
}
|
| 355 |
+
|
| 356 |
+
/// <summary>
|
| 357 |
+
/// 启动命名管道服务器,用于接收其他实例发送的URL
|
| 358 |
+
/// </summary>
|
| 359 |
+
private void StartPipeServer()
|
| 360 |
+
{
|
| 361 |
+
_pipeServerCancellation = new CancellationTokenSource();
|
| 362 |
+
var cancellationToken = _pipeServerCancellation.Token;
|
| 363 |
+
|
| 364 |
+
_pipeServerThread = new Thread(() => RunPipeServer(cancellationToken))
|
| 365 |
+
{
|
| 366 |
+
IsBackground = true,
|
| 367 |
+
Name = "PipeServerThread"
|
| 368 |
+
};
|
| 369 |
+
_pipeServerThread.Start();
|
| 370 |
+
}
|
| 371 |
+
|
| 372 |
+
/// <summary>
|
| 373 |
+
/// 运行命名管道服务器循环
|
| 374 |
+
/// </summary>
|
| 375 |
+
/// <param name="cancellationToken">取消令牌</param>
|
| 376 |
+
private void RunPipeServer(CancellationToken cancellationToken)
|
| 377 |
+
{
|
| 378 |
+
while (!cancellationToken.IsCancellationRequested)
|
| 379 |
+
{
|
| 380 |
+
try
|
| 381 |
+
{
|
| 382 |
+
using var pipeServer = new NamedPipeServerStream(PIPE_NAME, PipeDirection.In);
|
| 383 |
+
|
| 384 |
+
// 使用异步等待连接,支持取消
|
| 385 |
+
var connectTask = Task.Run(() => pipeServer.WaitForConnection(), cancellationToken);
|
| 386 |
+
connectTask.Wait(cancellationToken);
|
| 387 |
+
|
| 388 |
+
using var reader = new StreamReader(pipeServer, Encoding.UTF8);
|
| 389 |
+
string url = reader.ReadLine();
|
| 390 |
+
|
| 391 |
+
if (!string.IsNullOrWhiteSpace(url))
|
| 392 |
+
{
|
| 393 |
+
ProcessReceivedUrl(url);
|
| 394 |
+
}
|
| 395 |
+
}
|
| 396 |
+
catch (OperationCanceledException)
|
| 397 |
+
{
|
| 398 |
+
// 正常取消,退出循环
|
| 399 |
+
break;
|
| 400 |
+
}
|
| 401 |
+
catch (Exception ex)
|
| 402 |
+
{
|
| 403 |
+
LogError("命名管道服务器运行时发生错误", ex);
|
| 404 |
+
|
| 405 |
+
// 发生错误时短暂延迟后重试
|
| 406 |
+
if (!cancellationToken.IsCancellationRequested)
|
| 407 |
+
{
|
| 408 |
+
Thread.Sleep(1000);
|
| 409 |
+
}
|
| 410 |
+
}
|
| 411 |
+
}
|
| 412 |
+
}
|
| 413 |
+
|
| 414 |
+
/// <summary>
|
| 415 |
+
/// 处理从命名管道接收到的URL
|
| 416 |
+
/// </summary>
|
| 417 |
+
/// <param name="url">接收到的URL</param>
|
| 418 |
+
private void ProcessReceivedUrl(string url)
|
| 419 |
+
{
|
| 420 |
+
try
|
| 421 |
+
{
|
| 422 |
+
Dispatcher.BeginInvoke(new Action(() =>
|
| 423 |
+
{
|
| 424 |
+
if (_mainWindow != null && Utils.LoginManager.IsLoggedIn)
|
| 425 |
+
{
|
| 426 |
+
_mainWindow.OpenUrlInTab("Loading...", url, false);
|
| 427 |
+
ShowMainWindow();
|
| 428 |
+
}
|
| 429 |
+
}));
|
| 430 |
+
}
|
| 431 |
+
catch (Exception ex)
|
| 432 |
+
{
|
| 433 |
+
LogError("处理接收到的URL时发生错误", ex);
|
| 434 |
+
}
|
| 435 |
+
}
|
| 436 |
+
|
| 437 |
+
/// <summary>
|
| 438 |
+
/// 通过命名管道发送URL到主实例
|
| 439 |
+
/// </summary>
|
| 440 |
+
/// <param name="url">要发送的URL</param>
|
| 441 |
+
/// <returns>是否发送成功</returns>
|
| 442 |
+
private static bool SendUrlToPipe(string url)
|
| 443 |
+
{
|
| 444 |
+
try
|
| 445 |
+
{
|
| 446 |
+
using var pipeClient = new NamedPipeClientStream(".", PIPE_NAME, PipeDirection.Out);
|
| 447 |
+
pipeClient.Connect(1000); // 1秒超时
|
| 448 |
+
|
| 449 |
+
using var writer = new StreamWriter(pipeClient, Encoding.UTF8);
|
| 450 |
+
writer.WriteLine(url);
|
| 451 |
+
writer.Flush();
|
| 452 |
+
|
| 453 |
+
return true;
|
| 454 |
+
}
|
| 455 |
+
catch (Exception ex)
|
| 456 |
+
{
|
| 457 |
+
Debug.WriteLine($"[发送URL到管道失败] {ex.Message}");
|
| 458 |
+
return false;
|
| 459 |
+
}
|
| 460 |
+
}
|
| 461 |
+
|
| 462 |
+
/// <summary>
|
| 463 |
+
/// 初始化托盘图标
|
| 464 |
+
/// </summary>
|
| 465 |
+
/// <returns>是否初始化成功</returns>
|
| 466 |
+
private bool InitializeTrayIcon()
|
| 467 |
+
{
|
| 468 |
+
try
|
| 469 |
+
{
|
| 470 |
+
if (Current.Resources["TrayIcon"] is not TaskbarIcon trayIcon) return false;
|
| 471 |
+
|
| 472 |
+
var iconStream = GetResourceStream(new Uri("pack://application:,,,/Resources/app.ico"))?.Stream;
|
| 473 |
+
if (iconStream != null)
|
| 474 |
+
{
|
| 475 |
+
trayIcon.Icon = new Icon(iconStream);
|
| 476 |
+
}
|
| 477 |
+
|
| 478 |
+
trayIcon.TrayMouseDoubleClick += TrayIcon_TrayMouseDoubleClick;
|
| 479 |
+
return true;
|
| 480 |
+
}
|
| 481 |
+
catch (Exception ex)
|
| 482 |
+
{
|
| 483 |
+
LogError("初始化托盘图标时发生错误", ex);
|
| 484 |
+
return false;
|
| 485 |
+
}
|
| 486 |
+
}
|
| 487 |
+
|
| 488 |
+
/// <summary>
|
| 489 |
+
/// 处理命令行参数中的URL
|
| 490 |
+
/// </summary>
|
| 491 |
+
/// <param name="args">命令行参数</param>
|
| 492 |
+
private void ProcessCommandLineUrl(string[] args)
|
| 493 |
+
{
|
| 494 |
+
if (args?.Length == 0) return;
|
| 495 |
+
|
| 496 |
+
string url = args[0];
|
| 497 |
+
if (string.IsNullOrWhiteSpace(url) || !IsValidUrl(url)) return;
|
| 498 |
+
|
| 499 |
+
try
|
| 500 |
+
{
|
| 501 |
+
_mainWindow?.Dispatcher.Invoke(() =>
|
| 502 |
+
{
|
| 503 |
+
_mainWindow.OpenUrlInTab("Loading...", url, false);
|
| 504 |
+
});
|
| 505 |
+
}
|
| 506 |
+
catch (Exception ex)
|
| 507 |
+
{
|
| 508 |
+
LogError("处理命令行URL时发生错误", ex);
|
| 509 |
+
}
|
| 510 |
+
}
|
| 511 |
+
|
| 512 |
+
/// <summary>
|
| 513 |
+
/// 验证URL格式是否有效
|
| 514 |
+
/// </summary>
|
| 515 |
+
/// <param name="url">要验证的URL</param>
|
| 516 |
+
/// <returns>是否为有效URL</returns>
|
| 517 |
+
private static bool IsValidUrl(string url) =>
|
| 518 |
+
url.StartsWith("http://", StringComparison.OrdinalIgnoreCase) ||
|
| 519 |
+
url.StartsWith("https://", StringComparison.OrdinalIgnoreCase) ||
|
| 520 |
+
url.StartsWith("file://", StringComparison.OrdinalIgnoreCase);
|
| 521 |
+
|
| 522 |
+
/// <summary>
|
| 523 |
+
/// 显示登录窗口并处理登录结果
|
| 524 |
+
/// </summary>
|
| 525 |
+
/// <returns>是否登录成功</returns>
|
| 526 |
+
private bool ShowLoginWindow()
|
| 527 |
+
{
|
| 528 |
+
try
|
| 529 |
+
{
|
| 530 |
+
var loginWindow = new LoginWindow();
|
| 531 |
+
if (loginWindow.ShowDialog() != true) return false;
|
| 532 |
+
|
| 533 |
+
// 设置登录状态并订阅状态变化事件
|
| 534 |
+
Utils.LoginManager.SetLoggedIn();
|
| 535 |
+
Utils.LoginManager.OnLoginStatusChanged += UpdateTrayMenuState;
|
| 536 |
+
|
| 537 |
+
// 初始化托盘菜单状态
|
| 538 |
+
UpdateTrayMenuState(true);
|
| 539 |
+
|
| 540 |
+
return true;
|
| 541 |
+
}
|
| 542 |
+
catch (Exception ex)
|
| 543 |
+
{
|
| 544 |
+
LogError("显示登录窗口时发生错误", ex);
|
| 545 |
+
ShowErrorMessage("登录窗口初始化失败,程序即将退出。");
|
| 546 |
+
return false;
|
| 547 |
+
}
|
| 548 |
+
}
|
| 549 |
+
|
| 550 |
+
/// <summary>
|
| 551 |
+
/// 记录错误日志
|
| 552 |
+
/// </summary>
|
| 553 |
+
/// <param name="message">错误消息</param>
|
| 554 |
+
/// <param name="exception">异常对象</param>
|
| 555 |
+
private static void LogError(string message, Exception exception)
|
| 556 |
+
{
|
| 557 |
+
_logger.Error($"[错误] {message}:", exception);
|
| 558 |
+
}
|
| 559 |
+
|
| 560 |
+
/// <summary>
|
| 561 |
+
/// 显示错误消息框
|
| 562 |
+
/// </summary>
|
| 563 |
+
/// <param name="message">错误消息</param>
|
| 564 |
+
private static void ShowErrorMessage(string message)
|
| 565 |
+
{
|
| 566 |
+
MessageBox.Show(message, "错误", MessageBoxButton.OK, MessageBoxImage.Error);
|
| 567 |
+
}
|
| 568 |
+
|
| 569 |
+
#endregion
|
| 570 |
+
|
| 571 |
+
#region 应用程序生命周期
|
| 572 |
+
|
| 573 |
+
/// <summary>
|
| 574 |
+
/// 应用程序启动时的处理逻辑
|
| 575 |
+
/// </summary>
|
| 576 |
+
/// <param name="e">启动事件参数</param>
|
| 577 |
+
protected override void OnStartup(StartupEventArgs e)
|
| 578 |
+
{
|
| 579 |
+
base.OnStartup(e);
|
| 580 |
+
|
| 581 |
+
try
|
| 582 |
+
{
|
| 583 |
+
// 初始化模拟器
|
| 584 |
+
InitializeSimulators();
|
| 585 |
+
|
| 586 |
+
// 检查单例应用程序
|
| 587 |
+
if (!CheckSingleInstance(e)) return;
|
| 588 |
+
|
| 589 |
+
// 初始化应用程序核心组件
|
| 590 |
+
if (!InitializeApplication(e)) return;
|
| 591 |
+
|
| 592 |
+
// 显示登录窗口并处理结果
|
| 593 |
+
if (ShowLoginWindow())
|
| 594 |
+
{
|
| 595 |
+
ShowMainWindowAndActivate();
|
| 596 |
+
}
|
| 597 |
+
else
|
| 598 |
+
{
|
| 599 |
+
Shutdown();
|
| 600 |
+
}
|
| 601 |
+
}
|
| 602 |
+
catch (Exception ex)
|
| 603 |
+
{
|
| 604 |
+
MessageBox.Show($"应用程序启动失败: {ex.Message}\n{ex.StackTrace}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
|
| 605 |
+
}
|
| 606 |
+
}
|
| 607 |
+
|
| 608 |
+
/// <summary>
|
| 609 |
+
/// 初始化模拟器组件
|
| 610 |
+
/// </summary>
|
| 611 |
+
private static void InitializeSimulators()
|
| 612 |
+
{
|
| 613 |
+
_checkInSimulator = new AutoCheckInSimulator();
|
| 614 |
+
_checkInSimulator.Start();
|
| 615 |
+
|
| 616 |
+
_mouseActivitySimulator = new MouseActivitySimulator();
|
| 617 |
+
_mouseActivitySimulator.Start();
|
| 618 |
+
}
|
| 619 |
+
|
| 620 |
+
/// <summary>
|
| 621 |
+
/// 检查单例应用程序实例
|
| 622 |
+
/// </summary>
|
| 623 |
+
/// <param name="e">启动参数</param>
|
| 624 |
+
/// <returns>是否应该继续启动</returns>
|
| 625 |
+
private bool CheckSingleInstance(StartupEventArgs e)
|
| 626 |
+
{
|
| 627 |
+
if (_mutex.WaitOne(TimeSpan.Zero, false))
|
| 628 |
+
{
|
| 629 |
+
_mutexOwned = true;
|
| 630 |
+
StartPipeServer();
|
| 631 |
+
return true;
|
| 632 |
+
}
|
| 633 |
+
|
| 634 |
+
// 已有实例在运行,发送URL到主实例
|
| 635 |
+
HandleExistingInstance(e.Args);
|
| 636 |
+
return false;
|
| 637 |
+
}
|
| 638 |
+
|
| 639 |
+
/// <summary>
|
| 640 |
+
/// 处理已存在实例的情况
|
| 641 |
+
/// </summary>
|
| 642 |
+
/// <param name="args">命令行参数</param>
|
| 643 |
+
private void HandleExistingInstance(string[] args)
|
| 644 |
+
{
|
| 645 |
+
// 发送URL到主实例
|
| 646 |
+
if (args?.Length > 0)
|
| 647 |
+
{
|
| 648 |
+
string url = args[0];
|
| 649 |
+
if (IsValidUrl(url))
|
| 650 |
+
{
|
| 651 |
+
SendUrlToPipe(url);
|
| 652 |
+
}
|
| 653 |
+
}
|
| 654 |
+
|
| 655 |
+
// 激活已存在的窗口
|
| 656 |
+
IntPtr hWnd = FindWindow(null, "Browser Tool");
|
| 657 |
+
if (hWnd != IntPtr.Zero)
|
| 658 |
+
{
|
| 659 |
+
PostMessage(hWnd, WM_SHOWMAINWINDOW_CUSTOM, IntPtr.Zero, IntPtr.Zero);
|
| 660 |
+
}
|
| 661 |
+
|
| 662 |
+
Shutdown();
|
| 663 |
+
}
|
| 664 |
+
|
| 665 |
+
/// <summary>
|
| 666 |
+
/// 初始化应用程序核心组件
|
| 667 |
+
/// </summary>
|
| 668 |
+
/// <param name="e">启动参数</param>
|
| 669 |
+
/// <returns>是否初始化成功</returns>
|
| 670 |
+
private bool InitializeApplication(StartupEventArgs e)
|
| 671 |
+
{
|
| 672 |
+
try
|
| 673 |
+
{
|
| 674 |
+
// 初始化CEF
|
| 675 |
+
InitializeCef();
|
| 676 |
+
|
| 677 |
+
// 初始化数据库
|
| 678 |
+
//DatabaseInitializer.Initialize();
|
| 679 |
+
|
| 680 |
+
// 初始化托盘图标
|
| 681 |
+
if (!InitializeTrayIcon()) return false;
|
| 682 |
+
|
| 683 |
+
// 创建主窗口
|
| 684 |
+
CreateMainWindow(e.Args);
|
| 685 |
+
|
| 686 |
+
return true;
|
| 687 |
+
}
|
| 688 |
+
catch (Exception ex)
|
| 689 |
+
{
|
| 690 |
+
LogError("初始化应用程序组件时发生错误", ex);
|
| 691 |
+
return false;
|
| 692 |
+
}
|
| 693 |
+
}
|
| 694 |
+
|
| 695 |
+
/// <summary>
|
| 696 |
+
/// 创建并配置主窗口
|
| 697 |
+
/// </summary>
|
| 698 |
+
/// <param name="args">命令行参数</param>
|
| 699 |
+
private void CreateMainWindow(string[] args)
|
| 700 |
+
{
|
| 701 |
+
_mainWindow = new MainWindow();
|
| 702 |
+
MainWindow = _mainWindow;
|
| 703 |
+
|
| 704 |
+
// 处理命令行参数
|
| 705 |
+
ProcessCommandLineUrl(args);
|
| 706 |
+
|
| 707 |
+
// 监听主窗口关闭事件
|
| 708 |
+
_mainWindow.Closing += (_, e) =>
|
| 709 |
+
{
|
| 710 |
+
e.Cancel = true;
|
| 711 |
+
_mainWindow.Hide();
|
| 712 |
+
};
|
| 713 |
+
}
|
| 714 |
+
|
| 715 |
+
/// <summary>
|
| 716 |
+
/// 显示并激活主窗口
|
| 717 |
+
/// </summary>
|
| 718 |
+
private void ShowMainWindowAndActivate()
|
| 719 |
+
{
|
| 720 |
+
_mainWindow.Show();
|
| 721 |
+
_mainWindow.WindowState = WindowState.Normal;
|
| 722 |
+
_mainWindow.Activate();
|
| 723 |
+
}
|
| 724 |
+
|
| 725 |
+
/// <summary>
|
| 726 |
+
/// 应用程序退出时的清理逻辑
|
| 727 |
+
/// </summary>
|
| 728 |
+
/// <param name="e">退出事件参数</param>
|
| 729 |
+
protected override void OnExit(ExitEventArgs e)
|
| 730 |
+
{
|
| 731 |
+
try
|
| 732 |
+
{
|
| 733 |
+
CleanupResources();
|
| 734 |
+
}
|
| 735 |
+
catch (Exception ex)
|
| 736 |
+
{
|
| 737 |
+
LogError("应用程序退出清理时发生错误", ex);
|
| 738 |
+
}
|
| 739 |
+
finally
|
| 740 |
+
{
|
| 741 |
+
base.OnExit(e);
|
| 742 |
+
}
|
| 743 |
+
}
|
| 744 |
+
|
| 745 |
+
/// <summary>
|
| 746 |
+
/// 清理应用程序资源
|
| 747 |
+
/// </summary>
|
| 748 |
+
private void CleanupResources()
|
| 749 |
+
{
|
| 750 |
+
// 释放互斥锁
|
| 751 |
+
if (_mutexOwned)
|
| 752 |
+
{
|
| 753 |
+
_mutex.ReleaseMutex();
|
| 754 |
+
_mutexOwned = false;
|
| 755 |
+
}
|
| 756 |
+
|
| 757 |
+
// 停止模拟器
|
| 758 |
+
_checkInSimulator?.Stop();
|
| 759 |
+
_mouseActivitySimulator?.Stop();
|
| 760 |
+
|
| 761 |
+
// 停止命名管道服务器
|
| 762 |
+
_pipeServerCancellation?.Cancel();
|
| 763 |
+
_pipeServerThread?.Join(TimeSpan.FromSeconds(2));
|
| 764 |
+
|
| 765 |
+
// 清理浏览器相关资源
|
| 766 |
+
CleanupBrowserResources();
|
| 767 |
+
}
|
| 768 |
+
|
| 769 |
+
/// <summary>
|
| 770 |
+
/// 清理浏览器相关资源
|
| 771 |
+
/// </summary>
|
| 772 |
+
private static void CleanupBrowserResources()
|
| 773 |
+
{
|
| 774 |
+
try
|
| 775 |
+
{
|
| 776 |
+
Browser.BrowserInstanceManager.Instance?.CleanupAllBrowsers();
|
| 777 |
+
ImageCache.ClearCache();
|
| 778 |
+
|
| 779 |
+
if (Cef.IsInitialized == true)
|
| 780 |
+
{
|
| 781 |
+
Cef.Shutdown();
|
| 782 |
+
}
|
| 783 |
+
}
|
| 784 |
+
catch (Exception ex)
|
| 785 |
+
{
|
| 786 |
+
Debug.WriteLine($"清理浏览器资源时发生错误: {ex.Message}");
|
| 787 |
+
}
|
| 788 |
+
}
|
| 789 |
+
|
| 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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
using System;
|
| 2 |
+
using System.Collections.Generic;
|
| 3 |
+
using System.Linq;
|
| 4 |
+
using System.Windows;
|
| 5 |
+
using CefSharp;
|
| 6 |
+
using CefSharp.Wpf;
|
| 7 |
+
|
| 8 |
+
namespace BrowserTool.Browser
|
| 9 |
+
{
|
| 10 |
+
/// <summary>
|
| 11 |
+
/// 浏览器实例管理器,负责创建和管理浏览器实例
|
| 12 |
+
/// 采用无缓存策略,确保每个标签页都是全新、干净的环境
|
| 13 |
+
/// </summary>
|
| 14 |
+
public class BrowserInstanceManager
|
| 15 |
+
{
|
| 16 |
+
private static BrowserInstanceManager _instance;
|
| 17 |
+
private readonly Dictionary<string, ChromiumWebBrowser> _activeBrowsers = new Dictionary<string, ChromiumWebBrowser>();
|
| 18 |
+
|
| 19 |
+
// 单例模式
|
| 20 |
+
public static BrowserInstanceManager Instance => _instance ??= new BrowserInstanceManager();
|
| 21 |
+
|
| 22 |
+
private BrowserInstanceManager()
|
| 23 |
+
{
|
| 24 |
+
// 私有构造函数,确保单例
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
/// <summary>
|
| 28 |
+
/// 创建一个新的浏览器实例
|
| 29 |
+
/// </summary>
|
| 30 |
+
/// <param name="url">要加载的URL(可为空,由调用方控制加载时机)</param>
|
| 31 |
+
/// <param name="tabId">标签页ID</param>
|
| 32 |
+
/// <returns>浏览器实例</returns>
|
| 33 |
+
public ChromiumWebBrowser GetBrowser(string url, string tabId)
|
| 34 |
+
{
|
| 35 |
+
lock (_activeBrowsers)
|
| 36 |
+
{
|
| 37 |
+
// 如果该标签页已有浏览器实例,先清理
|
| 38 |
+
if (_activeBrowsers.ContainsKey(tabId))
|
| 39 |
+
{
|
| 40 |
+
System.Diagnostics.Debug.WriteLine($"[BrowserInstanceManager] 标签页已存在浏览器实例,先清理 - TabId: {tabId}");
|
| 41 |
+
ReleaseBrowser(tabId, dispose: true);
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
// 总是创建全新的浏览器实例
|
| 45 |
+
var browser = new ChromiumWebBrowser();
|
| 46 |
+
|
| 47 |
+
// 设置通用处理程序
|
| 48 |
+
browser.DownloadHandler = new CefDownloadHandler();
|
| 49 |
+
browser.MenuHandler = new CefMenuHandler();
|
| 50 |
+
browser.LifeSpanHandler = new CefLifeSpanHandler();
|
| 51 |
+
|
| 52 |
+
// 添加页面加载完成事件,注入深色滚动条样式
|
| 53 |
+
browser.FrameLoadEnd += (sender, args) => {
|
| 54 |
+
if (args.Frame.IsMain)
|
| 55 |
+
{
|
| 56 |
+
// 注入深色滚动条样式
|
| 57 |
+
DarkThemeStyleInjector.InjectDarkThemeStyles(args.Frame);
|
| 58 |
+
}
|
| 59 |
+
};
|
| 60 |
+
|
| 61 |
+
// 注册到活动浏览器列表
|
| 62 |
+
_activeBrowsers[tabId] = browser;
|
| 63 |
+
|
| 64 |
+
System.Diagnostics.Debug.WriteLine($"[BrowserInstanceManager] 创建新浏览器实例 - TabId: {tabId}");
|
| 65 |
+
|
| 66 |
+
// 只有在URL不为空且浏览器已初始化时才加载URL
|
| 67 |
+
if (!string.IsNullOrEmpty(url) && browser.IsBrowserInitialized)
|
| 68 |
+
{
|
| 69 |
+
browser.LoadUrl(url);
|
| 70 |
+
System.Diagnostics.Debug.WriteLine($"[BrowserInstanceManager] 直接加载URL: {url}");
|
| 71 |
+
}
|
| 72 |
+
else if (!string.IsNullOrEmpty(url))
|
| 73 |
+
{
|
| 74 |
+
// 如果浏览器未初始化,等待初始化完成后加载
|
| 75 |
+
DependencyPropertyChangedEventHandler browserInitializedHandler = null;
|
| 76 |
+
browserInitializedHandler = (sender, e) =>
|
| 77 |
+
{
|
| 78 |
+
try
|
| 79 |
+
{
|
| 80 |
+
if (browser.IsBrowserInitialized)
|
| 81 |
+
{
|
| 82 |
+
browser.IsBrowserInitializedChanged -= browserInitializedHandler;
|
| 83 |
+
browser.LoadUrl(url);
|
| 84 |
+
System.Diagnostics.Debug.WriteLine($"[BrowserInstanceManager] 浏览器初始化完成后加载URL: {url}");
|
| 85 |
+
}
|
| 86 |
+
}
|
| 87 |
+
catch (Exception ex)
|
| 88 |
+
{
|
| 89 |
+
System.Diagnostics.Debug.WriteLine($"[BrowserInstanceManager] 延迟加载URL时发生异常: {ex.Message}");
|
| 90 |
+
}
|
| 91 |
+
};
|
| 92 |
+
browser.IsBrowserInitializedChanged += browserInitializedHandler;
|
| 93 |
+
}
|
| 94 |
+
|
| 95 |
+
return browser;
|
| 96 |
+
}
|
| 97 |
+
}
|
| 98 |
+
|
| 99 |
+
/// <summary>
|
| 100 |
+
/// 释放并销毁浏览器实例
|
| 101 |
+
/// </summary>
|
| 102 |
+
/// <param name="tabId">标签页ID</param>
|
| 103 |
+
/// <param name="dispose">是否销毁实例(默认为true,确保完全清理)</param>
|
| 104 |
+
public void ReleaseBrowser(string tabId, bool dispose = true)
|
| 105 |
+
{
|
| 106 |
+
if (string.IsNullOrEmpty(tabId))
|
| 107 |
+
{
|
| 108 |
+
System.Diagnostics.Debug.WriteLine("[BrowserInstanceManager] ReleaseBrowser: tabId为空");
|
| 109 |
+
return;
|
| 110 |
+
}
|
| 111 |
+
|
| 112 |
+
lock (_activeBrowsers)
|
| 113 |
+
{
|
| 114 |
+
if (_activeBrowsers.TryGetValue(tabId, out ChromiumWebBrowser browser))
|
| 115 |
+
{
|
| 116 |
+
_activeBrowsers.Remove(tabId);
|
| 117 |
+
System.Diagnostics.Debug.WriteLine($"[BrowserInstanceManager] 释放浏览器实例 - TabId: {tabId}");
|
| 118 |
+
|
| 119 |
+
// 总是销毁实例,确保完全清理
|
| 120 |
+
DisposeBrowserInstance(browser, tabId);
|
| 121 |
+
}
|
| 122 |
+
else
|
| 123 |
+
{
|
| 124 |
+
System.Diagnostics.Debug.WriteLine($"[BrowserInstanceManager] 未找到要释放的浏览器实例 - TabId: {tabId}");
|
| 125 |
+
}
|
| 126 |
+
}
|
| 127 |
+
}
|
| 128 |
+
|
| 129 |
+
/// <summary>
|
| 130 |
+
/// 安全地销毁浏览器实例
|
| 131 |
+
/// </summary>
|
| 132 |
+
/// <param name="browser">浏览器实例</param>
|
| 133 |
+
/// <param name="tabId">标签页ID</param>
|
| 134 |
+
private void DisposeBrowserInstance(ChromiumWebBrowser browser, string tabId)
|
| 135 |
+
{
|
| 136 |
+
try
|
| 137 |
+
{
|
| 138 |
+
if (browser != null && !browser.IsDisposed)
|
| 139 |
+
{
|
| 140 |
+
// 清理事件处理器
|
| 141 |
+
CleanupBrowserEventHandlers(browser);
|
| 142 |
+
|
| 143 |
+
// 销毁浏览器实例
|
| 144 |
+
browser.Dispose();
|
| 145 |
+
System.Diagnostics.Debug.WriteLine($"[BrowserInstanceManager] 浏览器实例已销毁 - TabId: {tabId}");
|
| 146 |
+
}
|
| 147 |
+
}
|
| 148 |
+
catch (Exception ex)
|
| 149 |
+
{
|
| 150 |
+
System.Diagnostics.Debug.WriteLine($"[BrowserInstanceManager] 销毁浏览器实例时发生异常 - TabId: {tabId}, Error: {ex.Message}");
|
| 151 |
+
}
|
| 152 |
+
}
|
| 153 |
+
|
| 154 |
+
/// <summary>
|
| 155 |
+
/// 清理浏览器事件处理器
|
| 156 |
+
/// </summary>
|
| 157 |
+
/// <param name="browser">浏览器实例</param>
|
| 158 |
+
private void CleanupBrowserEventHandlers(ChromiumWebBrowser browser)
|
| 159 |
+
{
|
| 160 |
+
try
|
| 161 |
+
{
|
| 162 |
+
if (browser?.Tag is Dictionary<string, object> browserTags)
|
| 163 |
+
{
|
| 164 |
+
// 清理存储在Tag中的事件处理器引用
|
| 165 |
+
if (browserTags.TryGetValue("titleUpdateHandler", out object titleHandler) &&
|
| 166 |
+
titleHandler is EventHandler<FrameLoadEndEventArgs> titleUpdateHandler)
|
| 167 |
+
{
|
| 168 |
+
browser.FrameLoadEnd -= titleUpdateHandler;
|
| 169 |
+
}
|
| 170 |
+
|
| 171 |
+
if (browserTags.TryGetValue("loadingStateChangedHandler", out object loadingHandler) &&
|
| 172 |
+
loadingHandler is EventHandler<LoadingStateChangedEventArgs> loadingStateChangedHandler)
|
| 173 |
+
{
|
| 174 |
+
browser.LoadingStateChanged -= loadingStateChangedHandler;
|
| 175 |
+
}
|
| 176 |
+
|
| 177 |
+
browserTags.Clear();
|
| 178 |
+
browser.Tag = null;
|
| 179 |
+
|
| 180 |
+
System.Diagnostics.Debug.WriteLine("[BrowserInstanceManager] 浏览器事件处理器已清理");
|
| 181 |
+
}
|
| 182 |
+
}
|
| 183 |
+
catch (Exception ex)
|
| 184 |
+
{
|
| 185 |
+
System.Diagnostics.Debug.WriteLine($"[BrowserInstanceManager] 清理事件处理器时发生异常: {ex.Message}");
|
| 186 |
+
}
|
| 187 |
+
}
|
| 188 |
+
|
| 189 |
+
/// <summary>
|
| 190 |
+
/// 清理所有浏览器实例
|
| 191 |
+
/// </summary>
|
| 192 |
+
public void CleanupAllBrowsers()
|
| 193 |
+
{
|
| 194 |
+
lock (_activeBrowsers)
|
| 195 |
+
{
|
| 196 |
+
// 清理所有活动的浏览器实例
|
| 197 |
+
foreach (var kvp in _activeBrowsers.ToList())
|
| 198 |
+
{
|
| 199 |
+
try
|
| 200 |
+
{
|
| 201 |
+
DisposeBrowserInstance(kvp.Value, kvp.Key);
|
| 202 |
+
}
|
| 203 |
+
catch (Exception ex)
|
| 204 |
+
{
|
| 205 |
+
System.Diagnostics.Debug.WriteLine($"[BrowserInstanceManager] 清理浏览器实例时发生异常 - TabId: {kvp.Key}, Error: {ex.Message}");
|
| 206 |
+
}
|
| 207 |
+
}
|
| 208 |
+
_activeBrowsers.Clear();
|
| 209 |
+
|
| 210 |
+
System.Diagnostics.Debug.WriteLine("[BrowserInstanceManager] 所有浏览器实例已清理");
|
| 211 |
+
}
|
| 212 |
+
}
|
| 213 |
+
}
|
| 214 |
+
}
|
csharp/BrowserTool/BrowserTool/Browser/BrowserTabManager.cs
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
using System.Windows.Controls;
|
| 2 |
+
using CefSharp;
|
| 3 |
+
using CefSharp.Wpf;
|
| 4 |
+
|
| 5 |
+
namespace BrowserTool.Browser
|
| 6 |
+
{
|
| 7 |
+
/// <summary>
|
| 8 |
+
/// 浏览器Tab管理器,负责Tab的添加、关闭等操作
|
| 9 |
+
/// </summary>
|
| 10 |
+
public class BrowserTabManager
|
| 11 |
+
{
|
| 12 |
+
private TabControl _tabControl;
|
| 13 |
+
private MainWindow _mainWindow;
|
| 14 |
+
|
| 15 |
+
public BrowserTabManager(TabControl tabControl, MainWindow mainWindow)
|
| 16 |
+
{
|
| 17 |
+
_tabControl = tabControl;
|
| 18 |
+
_mainWindow = mainWindow;
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
/// <summary>
|
| 22 |
+
/// 添加新Tab
|
| 23 |
+
/// </summary>
|
| 24 |
+
public TabItem AddTab(string header, object content)
|
| 25 |
+
{
|
| 26 |
+
var tabItem = new TabItem();
|
| 27 |
+
var customHeader = new CustomTabItem();
|
| 28 |
+
customHeader.Content = header;
|
| 29 |
+
tabItem.Header = customHeader;
|
| 30 |
+
tabItem.Content = content;
|
| 31 |
+
|
| 32 |
+
// 设置浏览器事件处理
|
| 33 |
+
var chromiumBrowser = content as ChromiumWebBrowser;
|
| 34 |
+
if (chromiumBrowser != null)
|
| 35 |
+
{
|
| 36 |
+
var headerControl = tabItem.Header as CustomTabItem;
|
| 37 |
+
chromiumBrowser.TitleChanged += (s, e) =>
|
| 38 |
+
{
|
| 39 |
+
if (headerControl != null)
|
| 40 |
+
{
|
| 41 |
+
headerControl.Content = e.NewValue;
|
| 42 |
+
}
|
| 43 |
+
};
|
| 44 |
+
|
| 45 |
+
chromiumBrowser.AddressChanged += (s, e) =>
|
| 46 |
+
{
|
| 47 |
+
if (tabItem == _tabControl.SelectedItem)
|
| 48 |
+
{
|
| 49 |
+
var contentPresenter = tabItem.Content as ContentPresenter;
|
| 50 |
+
if (contentPresenter != null)
|
| 51 |
+
{
|
| 52 |
+
var grid = contentPresenter.Content as Grid;
|
| 53 |
+
if (grid != null)
|
| 54 |
+
{
|
| 55 |
+
var urlTextBox = grid.FindName("UrlTextBox") as TextBox;
|
| 56 |
+
if (urlTextBox != null)
|
| 57 |
+
{
|
| 58 |
+
urlTextBox.Text = chromiumBrowser.Address;
|
| 59 |
+
}
|
| 60 |
+
}
|
| 61 |
+
}
|
| 62 |
+
}
|
| 63 |
+
};
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
_tabControl.Items.Add(tabItem);
|
| 67 |
+
_tabControl.SelectedItem = tabItem;
|
| 68 |
+
return tabItem;
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
+
/// <summary>
|
| 72 |
+
/// 关闭指定Tab
|
| 73 |
+
/// </summary>
|
| 74 |
+
public void CloseTab(TabItem tabItem)
|
| 75 |
+
{
|
| 76 |
+
_tabControl.Items.Remove(tabItem);
|
| 77 |
+
}
|
| 78 |
+
}
|
| 79 |
+
}
|
csharp/BrowserTool/BrowserTool/Browser/CefDownloadHandler.cs
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
using CefSharp;
|
| 2 |
+
using System;
|
| 3 |
+
using System.Windows;
|
| 4 |
+
|
| 5 |
+
namespace BrowserTool.Browser
|
| 6 |
+
{
|
| 7 |
+
/// <summary>
|
| 8 |
+
/// CefSharp下载处理器,支持文件下载
|
| 9 |
+
/// </summary>
|
| 10 |
+
public class CefDownloadHandler : IDownloadHandler
|
| 11 |
+
{
|
| 12 |
+
public event Action<string> OnBeforeDownloadFired;
|
| 13 |
+
public event Action<string> OnDownloadUpdatedFired;
|
| 14 |
+
|
| 15 |
+
public bool OnBeforeDownload(IWebBrowser browserControl, IBrowser browser, DownloadItem downloadItem, IBeforeDownloadCallback callback)
|
| 16 |
+
{
|
| 17 |
+
// 下载前回调,可自定义保存路径
|
| 18 |
+
Application.Current.Dispatcher.Invoke(() =>
|
| 19 |
+
{
|
| 20 |
+
OnBeforeDownloadFired?.Invoke(downloadItem.SuggestedFileName);
|
| 21 |
+
// 新建或更新下载任务
|
| 22 |
+
DownloadManager.Instance.AddOrUpdateTask(new DownloadTask
|
| 23 |
+
{
|
| 24 |
+
FileName = downloadItem.SuggestedFileName,
|
| 25 |
+
FullPath = downloadItem.FullPath,
|
| 26 |
+
ReceivedBytes = downloadItem.ReceivedBytes,
|
| 27 |
+
TotalBytes = downloadItem.TotalBytes,
|
| 28 |
+
IsComplete = false,
|
| 29 |
+
Status = "正在下载"
|
| 30 |
+
});
|
| 31 |
+
});
|
| 32 |
+
if (!callback.IsDisposed)
|
| 33 |
+
{
|
| 34 |
+
// 默认保存到用户下载目录
|
| 35 |
+
string path = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "Downloads", downloadItem.SuggestedFileName);
|
| 36 |
+
callback.Continue(path, showDialog: true);
|
| 37 |
+
}
|
| 38 |
+
// 返回true表示下载应该继续
|
| 39 |
+
return true;
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
public void OnDownloadUpdated(IWebBrowser browserControl, IBrowser browser, DownloadItem downloadItem, IDownloadItemCallback callback)
|
| 43 |
+
{
|
| 44 |
+
// 下载进度更新
|
| 45 |
+
Application.Current.Dispatcher.Invoke(() =>
|
| 46 |
+
{
|
| 47 |
+
OnDownloadUpdatedFired?.Invoke(downloadItem.FullPath);
|
| 48 |
+
DownloadManager.Instance.AddOrUpdateTask(new DownloadTask
|
| 49 |
+
{
|
| 50 |
+
FileName = downloadItem.SuggestedFileName,
|
| 51 |
+
FullPath = downloadItem.FullPath,
|
| 52 |
+
ReceivedBytes = downloadItem.ReceivedBytes,
|
| 53 |
+
TotalBytes = downloadItem.TotalBytes,
|
| 54 |
+
IsComplete = downloadItem.IsComplete,
|
| 55 |
+
IsCancelled = downloadItem.IsCancelled,
|
| 56 |
+
IsError = downloadItem.IsCancelled || downloadItem.IsCancelled,
|
| 57 |
+
Status = downloadItem.IsComplete ? "已完成" : (downloadItem.IsCancelled ? "已取消" : "正在下载"),
|
| 58 |
+
Callback = callback
|
| 59 |
+
});
|
| 60 |
+
});
|
| 61 |
+
}
|
| 62 |
+
|
| 63 |
+
// 新增:实现IDownloadHandler接口的CanDownload方法,允许所有下载
|
| 64 |
+
public bool CanDownload(IWebBrowser browserControl, IBrowser browser, string url, string requestMethod)
|
| 65 |
+
{
|
| 66 |
+
return true;
|
| 67 |
+
}
|
| 68 |
+
}
|
| 69 |
+
}
|
csharp/BrowserTool/BrowserTool/Browser/CefHelper.cs
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
using CefSharp.Wpf;
|
| 2 |
+
|
| 3 |
+
namespace BrowserTool.Browser
|
| 4 |
+
{
|
| 5 |
+
/// <summary>
|
| 6 |
+
/// CefSharp浏览器辅助方法
|
| 7 |
+
/// </summary>
|
| 8 |
+
public static class CefHelper
|
| 9 |
+
{
|
| 10 |
+
/// <summary>
|
| 11 |
+
/// 打开开发者工具(F12)
|
| 12 |
+
/// </summary>
|
| 13 |
+
public static void ShowDevTools(ChromiumWebBrowser browser)
|
| 14 |
+
{
|
| 15 |
+
if (browser != null && browser.GetBrowser() != null)
|
| 16 |
+
{
|
| 17 |
+
browser.GetBrowser().GetHost().ShowDevTools();
|
| 18 |
+
}
|
| 19 |
+
}
|
| 20 |
+
}
|
| 21 |
+
}
|
csharp/BrowserTool/BrowserTool/Browser/CefLifeSpanHandler.cs
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
using CefSharp;
|
| 2 |
+
using CefSharp.Wpf;
|
| 3 |
+
|
| 4 |
+
namespace BrowserTool.Browser
|
| 5 |
+
{
|
| 6 |
+
/// <summary>
|
| 7 |
+
/// 拦截target=_blank等新窗口请求,在当前Tab打开
|
| 8 |
+
/// </summary>
|
| 9 |
+
public class CefLifeSpanHandler : ILifeSpanHandler
|
| 10 |
+
{
|
| 11 |
+
public bool DoClose(IWebBrowser chromiumWebBrowser, IBrowser browser) => false;
|
| 12 |
+
public void OnAfterCreated(IWebBrowser chromiumWebBrowser, IBrowser browser) { }
|
| 13 |
+
public void OnBeforeClose(IWebBrowser chromiumWebBrowser, IBrowser browser) { }
|
| 14 |
+
|
| 15 |
+
public bool OnBeforePopup(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, string targetUrl,
|
| 16 |
+
string targetFrameName, WindowOpenDisposition targetDisposition, bool userGesture, IPopupFeatures popupFeatures,
|
| 17 |
+
IWindowInfo windowInfo, IBrowserSettings browserSettings, ref bool noJavascriptAccess, out IWebBrowser newBrowser)
|
| 18 |
+
{
|
| 19 |
+
newBrowser = null;
|
| 20 |
+
// 在当前Tab打开新链接
|
| 21 |
+
chromiumWebBrowser.Load(targetUrl);
|
| 22 |
+
return true; // 阻止新窗口
|
| 23 |
+
}
|
| 24 |
+
}
|
| 25 |
+
}
|
csharp/BrowserTool/BrowserTool/Browser/CefMenuHandler.cs
ADDED
|
@@ -0,0 +1,170 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
| 5 |
+
using System.Threading;
|
| 6 |
+
using System.Threading.Tasks;
|
| 7 |
+
using System.Windows.Controls.Primitives;
|
| 8 |
+
using BrowserTool;
|
| 9 |
+
|
| 10 |
+
namespace BrowserTool.Browser
|
| 11 |
+
{
|
| 12 |
+
/// <summary>
|
| 13 |
+
/// CefSharp右键菜单处理器,支持自定义菜单项
|
| 14 |
+
/// </summary>
|
| 15 |
+
public class CefMenuHandler : IContextMenuHandler
|
| 16 |
+
{
|
| 17 |
+
private const int CMD_VIEW_SOURCE = 26501;
|
| 18 |
+
private const int CMD_COPY_URL = 26502;
|
| 19 |
+
private const int CMD_DOWNLOAD_MANAGER = 26503;
|
| 20 |
+
private const int CMD_TOGGLE_URL_BAR = 26504;
|
| 21 |
+
private const int CMD_GO_TO_URL = 26505;
|
| 22 |
+
private const int CMD_GO_TO_URL_NEW_WINDOW = 26506;
|
| 23 |
+
private const int CMD_REFRESH = 26507;
|
| 24 |
+
private const int CMD_OPEN_LINK_NEW_TAB = 26508;
|
| 25 |
+
private const int CMD_COPY_SELECTION = 26509;
|
| 26 |
+
|
| 27 |
+
public void OnBeforeContextMenu(IWebBrowser browserControl, IBrowser browser, IFrame frame, IContextMenuParams parameters, IMenuModel model)
|
| 28 |
+
{
|
| 29 |
+
// 清除默认菜单
|
| 30 |
+
model.Clear();
|
| 31 |
+
|
| 32 |
+
// 如果右键点击的是链接,添加"在新标签页中打开"选项
|
| 33 |
+
if (!string.IsNullOrEmpty(parameters.LinkUrl))
|
| 34 |
+
{
|
| 35 |
+
model.AddItem((CefMenuCommand)CMD_OPEN_LINK_NEW_TAB, "在新标签页中打开");
|
| 36 |
+
model.AddSeparator();
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
// 如果有选中的文本,添加复制选项
|
| 40 |
+
if (!string.IsNullOrEmpty(parameters.SelectionText))
|
| 41 |
+
{
|
| 42 |
+
model.AddItem((CefMenuCommand)CMD_COPY_SELECTION, "复制选中内容");
|
| 43 |
+
model.AddSeparator();
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
model.AddItem((CefMenuCommand)CMD_VIEW_SOURCE, "查看源代码");
|
| 47 |
+
model.AddItem((CefMenuCommand)CMD_COPY_URL, "复制网址");
|
| 48 |
+
model.AddSeparator();
|
| 49 |
+
model.AddItem((CefMenuCommand)CMD_DOWNLOAD_MANAGER, "下载管理器");
|
| 50 |
+
model.AddSeparator();
|
| 51 |
+
model.AddItem((CefMenuCommand)CMD_TOGGLE_URL_BAR, "显示/隐藏地址栏");
|
| 52 |
+
model.AddItem((CefMenuCommand)CMD_GO_TO_URL, "粘贴并访问");
|
| 53 |
+
model.AddItem((CefMenuCommand)CMD_GO_TO_URL_NEW_WINDOW, "粘贴并在新标签页中访问");
|
| 54 |
+
model.AddSeparator();
|
| 55 |
+
model.AddItem((CefMenuCommand)CMD_REFRESH, "刷新");
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
public bool OnContextMenuCommand(IWebBrowser browserControl, IBrowser browser, IFrame frame, IContextMenuParams parameters, CefMenuCommand commandId, CefEventFlags eventFlags)
|
| 59 |
+
{
|
| 60 |
+
//粘贴并前往
|
| 61 |
+
if ((int)commandId == CMD_GO_TO_URL)
|
| 62 |
+
{
|
| 63 |
+
string clipboardText = Clipboard.GetText();
|
| 64 |
+
if (!string.IsNullOrWhiteSpace(clipboardText) && (clipboardText.StartsWith("http://") || clipboardText.StartsWith("https://")))
|
| 65 |
+
{
|
| 66 |
+
browserControl.Load(clipboardText);
|
| 67 |
+
}
|
| 68 |
+
return true;
|
| 69 |
+
}
|
| 70 |
+
//粘贴并在新标签页中访问
|
| 71 |
+
if ((int)commandId == CMD_GO_TO_URL_NEW_WINDOW)
|
| 72 |
+
{
|
| 73 |
+
string clipboardText = Clipboard.GetText();
|
| 74 |
+
if (!string.IsNullOrWhiteSpace(clipboardText) && (clipboardText.StartsWith("http://") || clipboardText.StartsWith("https://")))
|
| 75 |
+
{
|
| 76 |
+
System.Windows.Application.Current.Dispatcher.Invoke(() => {
|
| 77 |
+
var mainWindow = System.Windows.Application.Current.MainWindow as MainWindow;
|
| 78 |
+
if (mainWindow != null)
|
| 79 |
+
{
|
| 80 |
+
// 在新标签页中打开URL
|
| 81 |
+
// 直接使用 OpenUrlInTab 方法,并设置初始标题为 "Loading..."
|
| 82 |
+
// 浏览器会自动更新标题为网站标题
|
| 83 |
+
mainWindow.Dispatcher.BeginInvoke(new Action(() => {
|
| 84 |
+
// 不传入 keepOriginalTitle 参数或设置为 false,允许标题动态更新
|
| 85 |
+
mainWindow.OpenUrlInTab("Loading...", clipboardText, false);
|
| 86 |
+
}));
|
| 87 |
+
}
|
| 88 |
+
});
|
| 89 |
+
}
|
| 90 |
+
return true;
|
| 91 |
+
}
|
| 92 |
+
if ((int)commandId == CMD_OPEN_LINK_NEW_TAB)
|
| 93 |
+
{
|
| 94 |
+
// 在新标签页中打开链接
|
| 95 |
+
// 先保存LinkUrl,防止parameters对象被释放
|
| 96 |
+
string linkUrl = parameters.LinkUrl;
|
| 97 |
+
System.Windows.Application.Current.Dispatcher.Invoke(() => {
|
| 98 |
+
var mainWindow = System.Windows.Application.Current.MainWindow as MainWindow;
|
| 99 |
+
if (mainWindow != null)
|
| 100 |
+
{
|
| 101 |
+
mainWindow.Dispatcher.BeginInvoke(new Action(() => {
|
| 102 |
+
mainWindow.OpenUrlInTab("Loading...", linkUrl, false,false,0,null,false);
|
| 103 |
+
}));
|
| 104 |
+
}
|
| 105 |
+
});
|
| 106 |
+
return true;
|
| 107 |
+
}
|
| 108 |
+
if ((int)commandId == CMD_VIEW_SOURCE)
|
| 109 |
+
{
|
| 110 |
+
// 查看源代码
|
| 111 |
+
frame.ViewSource();
|
| 112 |
+
return true;
|
| 113 |
+
}
|
| 114 |
+
if ((int)commandId == CMD_COPY_URL)
|
| 115 |
+
{
|
| 116 |
+
// 复制当前URL
|
| 117 |
+
Clipboard.SetText(frame.Url);
|
| 118 |
+
return true;
|
| 119 |
+
}
|
| 120 |
+
if ((int)commandId == CMD_DOWNLOAD_MANAGER)
|
| 121 |
+
{
|
| 122 |
+
// 打开下载管理器窗口
|
| 123 |
+
System.Windows.Application.Current.Dispatcher.Invoke(() =>
|
| 124 |
+
{
|
| 125 |
+
BrowserTool.Browser.DownloadManagerWindow.ShowSingleton();
|
| 126 |
+
});
|
| 127 |
+
return true;
|
| 128 |
+
}
|
| 129 |
+
if ((int)commandId == CMD_TOGGLE_URL_BAR)
|
| 130 |
+
{
|
| 131 |
+
// 显示/隐藏地址栏
|
| 132 |
+
System.Windows.Application.Current.Dispatcher.Invoke(() =>
|
| 133 |
+
{
|
| 134 |
+
var mainWindow = System.Windows.Application.Current.MainWindow as MainWindow;
|
| 135 |
+
if (mainWindow != null)
|
| 136 |
+
{
|
| 137 |
+
mainWindow.ToggleUrlBar();
|
| 138 |
+
}
|
| 139 |
+
});
|
| 140 |
+
return true;
|
| 141 |
+
}
|
| 142 |
+
if ((int)commandId == CMD_REFRESH)
|
| 143 |
+
{
|
| 144 |
+
// 刷新页面
|
| 145 |
+
browser.Reload();
|
| 146 |
+
return true;
|
| 147 |
+
}
|
| 148 |
+
if ((int)commandId == CMD_COPY_SELECTION)
|
| 149 |
+
{
|
| 150 |
+
// 复制选中的内容
|
| 151 |
+
if (!string.IsNullOrEmpty(parameters.SelectionText))
|
| 152 |
+
{
|
| 153 |
+
Clipboard.SetText(parameters.SelectionText);
|
| 154 |
+
}
|
| 155 |
+
return true;
|
| 156 |
+
}
|
| 157 |
+
return false;
|
| 158 |
+
}
|
| 159 |
+
|
| 160 |
+
public void OnContextMenuDismissed(IWebBrowser browserControl, IBrowser browser, IFrame frame)
|
| 161 |
+
{
|
| 162 |
+
}
|
| 163 |
+
|
| 164 |
+
public bool RunContextMenu(IWebBrowser browserControl, IBrowser browser, IFrame frame, IContextMenuParams parameters, IMenuModel model, IRunContextMenuCallback callback)
|
| 165 |
+
{
|
| 166 |
+
// 返回 false 使用默认菜单显示方式
|
| 167 |
+
return false;
|
| 168 |
+
}
|
| 169 |
+
}
|
| 170 |
+
}
|
csharp/BrowserTool/BrowserTool/Browser/CustomTabItem.xaml
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<UserControl x:Class="BrowserTool.Browser.CustomTabItem"
|
| 2 |
+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
| 3 |
+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
| 4 |
+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
| 5 |
+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
| 6 |
+
xmlns:cef="clr-namespace:CefSharp.Wpf;assembly=CefSharp.Wpf"
|
| 7 |
+
mc:Ignorable="d"
|
| 8 |
+
d:DesignHeight="30" d:DesignWidth="120">
|
| 9 |
+
<!-- Chrome风格TabItem,带关闭按钮 -->
|
| 10 |
+
<Grid>
|
| 11 |
+
<Grid.ColumnDefinitions>
|
| 12 |
+
<ColumnDefinition Width="*"/>
|
| 13 |
+
<ColumnDefinition Width="30"/>
|
| 14 |
+
</Grid.ColumnDefinitions>
|
| 15 |
+
<!-- Tab标题 -->
|
| 16 |
+
<ContentPresenter Grid.Column="0" VerticalAlignment="Center" Margin="8,0,0,0"/>
|
| 17 |
+
<!-- 关闭按钮 -->
|
| 18 |
+
<Button Grid.Column="1" Width="20" Height="20" Margin="5,0,5,0" VerticalAlignment="Center"
|
| 19 |
+
Click="CloseButton_Click" ToolTip="关闭标签页"
|
| 20 |
+
Background="Transparent" BorderThickness="0" Padding="0">
|
| 21 |
+
<Path Data="M4,4 L16,16 M16,4 L4,16" Stroke="#888" StrokeThickness="2" StrokeEndLineCap="Round" StrokeStartLineCap="Round"/>
|
| 22 |
+
</Button>
|
| 23 |
+
<!-- 右键菜单 -->
|
| 24 |
+
<Grid.ContextMenu>
|
| 25 |
+
<ContextMenu>
|
| 26 |
+
<MenuItem Header="复制标签页" Click="CopyTab_Click">
|
| 27 |
+
<MenuItem.Icon>
|
| 28 |
+
<TextBlock Text="" FontFamily="Segoe MDL2 Assets" FontSize="14"/>
|
| 29 |
+
</MenuItem.Icon>
|
| 30 |
+
</MenuItem>
|
| 31 |
+
<MenuItem Header="关闭标签页" Click="CloseButton_Click">
|
| 32 |
+
<MenuItem.Icon>
|
| 33 |
+
<TextBlock Text="" FontFamily="Segoe MDL2 Assets" FontSize="14"/>
|
| 34 |
+
</MenuItem.Icon>
|
| 35 |
+
</MenuItem>
|
| 36 |
+
</ContextMenu>
|
| 37 |
+
</Grid.ContextMenu>
|
| 38 |
+
</Grid>
|
| 39 |
+
</UserControl>
|
csharp/BrowserTool/BrowserTool/Browser/CustomTabItem.xaml.cs
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
using System.Windows;
|
| 2 |
+
using System.Windows.Controls;
|
| 3 |
+
using CefSharp;
|
| 4 |
+
using CefSharp.Wpf;
|
| 5 |
+
using System.Windows.Media;
|
| 6 |
+
|
| 7 |
+
namespace BrowserTool.Browser
|
| 8 |
+
{
|
| 9 |
+
/// <summary>
|
| 10 |
+
/// Chrome风格TabItem,带关闭按钮
|
| 11 |
+
/// </summary>
|
| 12 |
+
public partial class CustomTabItem : UserControl
|
| 13 |
+
{
|
| 14 |
+
public CustomTabItem()
|
| 15 |
+
{
|
| 16 |
+
InitializeComponent();
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
// 关闭按钮点击事件,触发Tab关闭
|
| 20 |
+
private void CloseButton_Click(object sender, RoutedEventArgs e)
|
| 21 |
+
{
|
| 22 |
+
// 向父级TabControl冒泡关闭事件
|
| 23 |
+
var tabItem = this.FindParent<TabItem>();
|
| 24 |
+
if (tabItem != null)
|
| 25 |
+
{
|
| 26 |
+
var tabControl = tabItem.FindParent<TabControl>();
|
| 27 |
+
if (tabControl != null)
|
| 28 |
+
{
|
| 29 |
+
tabControl.Items.Remove(tabItem);
|
| 30 |
+
}
|
| 31 |
+
}
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
// 复制标签页
|
| 35 |
+
private void CopyTab_Click(object sender, RoutedEventArgs e)
|
| 36 |
+
{
|
| 37 |
+
var tabItem = this.FindParent<TabItem>();
|
| 38 |
+
if (tabItem != null)
|
| 39 |
+
{
|
| 40 |
+
var tabControl = tabItem.FindParent<TabControl>();
|
| 41 |
+
if (tabControl != null)
|
| 42 |
+
{
|
| 43 |
+
// 获取当前标签页的浏览器控件
|
| 44 |
+
var browser = tabItem.Content as ChromiumWebBrowser;
|
| 45 |
+
if (browser != null)
|
| 46 |
+
{
|
| 47 |
+
// 获取当前URL
|
| 48 |
+
string currentUrl = browser.Address;
|
| 49 |
+
string currentTitle = browser.Title;
|
| 50 |
+
|
| 51 |
+
// 创建新标签页
|
| 52 |
+
var newTab = new TabItem();
|
| 53 |
+
var newBrowser = new ChromiumWebBrowser(currentUrl);
|
| 54 |
+
|
| 55 |
+
// 创建新的标题控件
|
| 56 |
+
var newHeader = new CustomTabItem();
|
| 57 |
+
newHeader.Content = GetNextTabTitle(currentTitle, tabControl);
|
| 58 |
+
|
| 59 |
+
newTab.Header = newHeader;
|
| 60 |
+
newTab.Content = newBrowser;
|
| 61 |
+
|
| 62 |
+
// 在当前标签页后插入新标签页
|
| 63 |
+
int currentIndex = tabControl.Items.IndexOf(tabItem);
|
| 64 |
+
tabControl.Items.Insert(currentIndex + 1, newTab);
|
| 65 |
+
tabControl.SelectedItem = newTab;
|
| 66 |
+
}
|
| 67 |
+
}
|
| 68 |
+
}
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
+
// 获取下一个标签页标题(自动添加数字)
|
| 72 |
+
private string GetNextTabTitle(string baseTitle, TabControl tabControl)
|
| 73 |
+
{
|
| 74 |
+
string newTitle = baseTitle;
|
| 75 |
+
int counter = 2;
|
| 76 |
+
|
| 77 |
+
// 检查是否存在相同标题的标签页
|
| 78 |
+
while (true)
|
| 79 |
+
{
|
| 80 |
+
bool exists = false;
|
| 81 |
+
foreach (TabItem item in tabControl.Items)
|
| 82 |
+
{
|
| 83 |
+
var header = item.Header as CustomTabItem;
|
| 84 |
+
if (header != null && header.Content.ToString() == newTitle)
|
| 85 |
+
{
|
| 86 |
+
exists = true;
|
| 87 |
+
break;
|
| 88 |
+
}
|
| 89 |
+
}
|
| 90 |
+
|
| 91 |
+
if (!exists)
|
| 92 |
+
break;
|
| 93 |
+
|
| 94 |
+
newTitle = $"{baseTitle} {counter}";
|
| 95 |
+
counter++;
|
| 96 |
+
}
|
| 97 |
+
|
| 98 |
+
return newTitle;
|
| 99 |
+
}
|
| 100 |
+
}
|
| 101 |
+
|
| 102 |
+
// 辅助方法:查找父级控件
|
| 103 |
+
public static class VisualTreeHelperExtensions
|
| 104 |
+
{
|
| 105 |
+
public static T FindParent<T>(this DependencyObject child) where T : DependencyObject
|
| 106 |
+
{
|
| 107 |
+
DependencyObject parent = System.Windows.Media.VisualTreeHelper.GetParent(child);
|
| 108 |
+
while (parent != null && !(parent is T))
|
| 109 |
+
{
|
| 110 |
+
parent = System.Windows.Media.VisualTreeHelper.GetParent(parent);
|
| 111 |
+
}
|
| 112 |
+
return parent as T;
|
| 113 |
+
}
|
| 114 |
+
}
|
| 115 |
+
}
|
csharp/BrowserTool/BrowserTool/Browser/DarkThemeStyleInjector.cs
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
using CefSharp;
|
| 2 |
+
using System;
|
| 3 |
+
using System.Text;
|
| 4 |
+
|
| 5 |
+
namespace BrowserTool.Browser
|
| 6 |
+
{
|
| 7 |
+
/// <summary>
|
| 8 |
+
/// 负责向CefSharp浏览器注入深色主题样式的工具类
|
| 9 |
+
/// </summary>
|
| 10 |
+
public class DarkThemeStyleInjector
|
| 11 |
+
{
|
| 12 |
+
// 深色滚动条CSS样式
|
| 13 |
+
private const string DarkScrollbarCss = @"
|
| 14 |
+
::-webkit-scrollbar {
|
| 15 |
+
width: 12px;
|
| 16 |
+
height: 12px;
|
| 17 |
+
background-color: #2D2D30;
|
| 18 |
+
}
|
| 19 |
+
|
| 20 |
+
::-webkit-scrollbar-track {
|
| 21 |
+
background-color: #2D2D30;
|
| 22 |
+
border-radius: 6px;
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
::-webkit-scrollbar-thumb {
|
| 26 |
+
background-color: #686868;
|
| 27 |
+
border-radius: 6px;
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
::-webkit-scrollbar-thumb:hover {
|
| 31 |
+
background-color: #9E9E9E;
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
::-webkit-scrollbar-thumb:active {
|
| 35 |
+
background-color: #BFBFBF;
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
::-webkit-scrollbar-corner {
|
| 39 |
+
background-color: #2D2D30;
|
| 40 |
+
}
|
| 41 |
+
";
|
| 42 |
+
|
| 43 |
+
/// <summary>
|
| 44 |
+
/// 将深色主题样式注入到浏览器中
|
| 45 |
+
/// </summary>
|
| 46 |
+
/// <param name="frame">要注入样式的浏览器框架</param>
|
| 47 |
+
public static void InjectDarkThemeStyles(IFrame frame)
|
| 48 |
+
{
|
| 49 |
+
if (frame == null || !frame.IsValid)
|
| 50 |
+
return;
|
| 51 |
+
|
| 52 |
+
// 创建一个JavaScript,将CSS样式注入到页面头部
|
| 53 |
+
string script = @"
|
| 54 |
+
(function() {
|
| 55 |
+
var style = document.createElement('style');
|
| 56 |
+
style.type = 'text/css';
|
| 57 |
+
style.id = 'browser-tool-dark-theme';
|
| 58 |
+
style.innerHTML = `" + DarkScrollbarCss + @"`;
|
| 59 |
+
|
| 60 |
+
// 如果已存在相同ID的样式,则先移除
|
| 61 |
+
var existingStyle = document.getElementById('browser-tool-dark-theme');
|
| 62 |
+
if (existingStyle) {
|
| 63 |
+
existingStyle.parentNode.removeChild(existingStyle);
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
// 将样式添加到文档头部
|
| 67 |
+
(document.head || document.documentElement).appendChild(style);
|
| 68 |
+
|
| 69 |
+
return true;
|
| 70 |
+
})();
|
| 71 |
+
";
|
| 72 |
+
|
| 73 |
+
// 异步执行JavaScript
|
| 74 |
+
frame.ExecuteJavaScriptAsync(script);
|
| 75 |
+
}
|
| 76 |
+
}
|
| 77 |
+
}
|
csharp/BrowserTool/BrowserTool/Browser/DownloadManager.cs
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
using System.Collections.ObjectModel;
|
| 2 |
+
using System.Linq;
|
| 3 |
+
|
| 4 |
+
namespace BrowserTool.Browser
|
| 5 |
+
{
|
| 6 |
+
/// <summary>
|
| 7 |
+
/// 下载任务管理器,集中管理所有下载任务(单例)
|
| 8 |
+
/// </summary>
|
| 9 |
+
public class DownloadManager
|
| 10 |
+
{
|
| 11 |
+
private static DownloadManager _instance;
|
| 12 |
+
public static DownloadManager Instance => _instance ?? (_instance = new DownloadManager());
|
| 13 |
+
|
| 14 |
+
// 下载任务列表,便于UI绑定
|
| 15 |
+
public ObservableCollection<DownloadTask> Tasks { get; } = new ObservableCollection<DownloadTask>();
|
| 16 |
+
|
| 17 |
+
/// <summary>
|
| 18 |
+
/// 添加或更新下载任务
|
| 19 |
+
/// </summary>
|
| 20 |
+
public void AddOrUpdateTask(DownloadTask task)
|
| 21 |
+
{
|
| 22 |
+
// 检查是否存在相同文件名的正在下载的任务
|
| 23 |
+
var existingTask = Tasks.FirstOrDefault(t =>
|
| 24 |
+
t.FileName == task.FileName &&
|
| 25 |
+
!t.IsComplete &&
|
| 26 |
+
!t.IsCancelled &&
|
| 27 |
+
!t.IsError);
|
| 28 |
+
|
| 29 |
+
if (existingTask == null)
|
| 30 |
+
{
|
| 31 |
+
// 如果不存在,添加新任务
|
| 32 |
+
Tasks.Add(task);
|
| 33 |
+
}
|
| 34 |
+
else
|
| 35 |
+
{
|
| 36 |
+
// 更新现有任务的进度和状态
|
| 37 |
+
existingTask.ReceivedBytes = task.ReceivedBytes;
|
| 38 |
+
existingTask.TotalBytes = task.TotalBytes;
|
| 39 |
+
existingTask.IsComplete = task.IsComplete;
|
| 40 |
+
existingTask.IsCancelled = task.IsCancelled;
|
| 41 |
+
existingTask.IsError = task.IsError;
|
| 42 |
+
existingTask.Status = task.Status;
|
| 43 |
+
}
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
/// <summary>
|
| 47 |
+
/// 移除任务
|
| 48 |
+
/// </summary>
|
| 49 |
+
public void RemoveTask(DownloadTask task)
|
| 50 |
+
{
|
| 51 |
+
if (Tasks.Contains(task))
|
| 52 |
+
{
|
| 53 |
+
Tasks.Remove(task);
|
| 54 |
+
}
|
| 55 |
+
}
|
| 56 |
+
}
|
| 57 |
+
}
|
csharp/BrowserTool/BrowserTool/Browser/DownloadManagerWindow.xaml
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<Window x:Class="BrowserTool.Browser.DownloadManagerWindow"
|
| 2 |
+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
| 3 |
+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
| 4 |
+
xmlns:local="clr-namespace:BrowserTool.Browser"
|
| 5 |
+
Title="下载管理器" Height="400" Width="600"
|
| 6 |
+
WindowStartupLocation="CenterScreen">
|
| 7 |
+
<Window.Resources>
|
| 8 |
+
<local:InverseBoolConverter x:Key="InverseBoolConverter" />
|
| 9 |
+
</Window.Resources>
|
| 10 |
+
<Grid>
|
| 11 |
+
<DataGrid x:Name="DownloadGrid" ItemsSource="{Binding Tasks}" AutoGenerateColumns="False" CanUserAddRows="False" Margin="10">
|
| 12 |
+
<DataGrid.Columns>
|
| 13 |
+
<DataGridTextColumn Header="文件名" Binding="{Binding FileName}" Width="2*"/>
|
| 14 |
+
<DataGridTextColumn Header="状态" Binding="{Binding Status}" Width="*"/>
|
| 15 |
+
<DataGridTemplateColumn Header="进度" Width="2*">
|
| 16 |
+
<DataGridTemplateColumn.CellTemplate>
|
| 17 |
+
<DataTemplate>
|
| 18 |
+
<ProgressBar Minimum="0" Maximum="1" Value="{Binding Progress, Mode=OneWay}" Height="16"/>
|
| 19 |
+
</DataTemplate>
|
| 20 |
+
</DataGridTemplateColumn.CellTemplate>
|
| 21 |
+
</DataGridTemplateColumn>
|
| 22 |
+
<DataGridTemplateColumn Header="操作" Width="*">
|
| 23 |
+
<DataGridTemplateColumn.CellTemplate>
|
| 24 |
+
<DataTemplate>
|
| 25 |
+
<StackPanel Orientation="Horizontal">
|
| 26 |
+
<Button Content="打开" Margin="2,0" Click="OpenFile_Click" IsEnabled="{Binding IsComplete}"/>
|
| 27 |
+
<Button Content="文件夹" Margin="2,0" Click="OpenFolder_Click" IsEnabled="{Binding IsComplete}"/>
|
| 28 |
+
<Button Content="取消" Margin="2,0" Click="Cancel_Click" IsEnabled="{Binding IsComplete, Converter={StaticResource InverseBoolConverter}}"/>
|
| 29 |
+
</StackPanel>
|
| 30 |
+
</DataTemplate>
|
| 31 |
+
</DataGridTemplateColumn.CellTemplate>
|
| 32 |
+
</DataGridTemplateColumn>
|
| 33 |
+
</DataGrid.Columns>
|
| 34 |
+
<DataGrid.ContextMenu>
|
| 35 |
+
<ContextMenu>
|
| 36 |
+
<MenuItem Header="清除已完成" Click="ClearCompleted_Click"/>
|
| 37 |
+
</ContextMenu>
|
| 38 |
+
</DataGrid.ContextMenu>
|
| 39 |
+
</DataGrid>
|
| 40 |
+
</Grid>
|
| 41 |
+
</Window>
|
csharp/BrowserTool/BrowserTool/Browser/DownloadManagerWindow.xaml.cs
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
using System.Diagnostics;
|
| 2 |
+
using System.Windows;
|
| 3 |
+
using System.Windows.Controls;
|
| 4 |
+
using System.Linq;
|
| 5 |
+
|
| 6 |
+
namespace BrowserTool.Browser
|
| 7 |
+
{
|
| 8 |
+
/// <summary>
|
| 9 |
+
/// 下载管理器窗口,集中显示所有下载任务
|
| 10 |
+
/// </summary>
|
| 11 |
+
public partial class DownloadManagerWindow : Window
|
| 12 |
+
{
|
| 13 |
+
private static DownloadManagerWindow _instance;
|
| 14 |
+
public static void ShowSingleton()
|
| 15 |
+
{
|
| 16 |
+
if (_instance == null || !_instance.IsVisible)
|
| 17 |
+
{
|
| 18 |
+
_instance = new DownloadManagerWindow();
|
| 19 |
+
_instance.Show();
|
| 20 |
+
}
|
| 21 |
+
else
|
| 22 |
+
{
|
| 23 |
+
_instance.Activate();
|
| 24 |
+
}
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
public DownloadManagerWindow()
|
| 28 |
+
{
|
| 29 |
+
InitializeComponent();
|
| 30 |
+
this.DataContext = DownloadManager.Instance;
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
// 打开文件
|
| 34 |
+
private void OpenFile_Click(object sender, RoutedEventArgs e)
|
| 35 |
+
{
|
| 36 |
+
if (DownloadGrid.SelectedItem is DownloadTask task && task.IsComplete)
|
| 37 |
+
{
|
| 38 |
+
try { Process.Start(new ProcessStartInfo(task.FullPath) { UseShellExecute = true }); } catch { }
|
| 39 |
+
}
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
// 打开文件夹
|
| 43 |
+
private void OpenFolder_Click(object sender, RoutedEventArgs e)
|
| 44 |
+
{
|
| 45 |
+
if (DownloadGrid.SelectedItem is DownloadTask task && task.IsComplete)
|
| 46 |
+
{
|
| 47 |
+
try { Process.Start("explorer.exe", $"/select,\"{task.FullPath}\""); } catch { }
|
| 48 |
+
}
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
// 取消按钮点击事件
|
| 52 |
+
private void Cancel_Click(object sender, RoutedEventArgs e)
|
| 53 |
+
{
|
| 54 |
+
if ((sender as Button)?.DataContext is DownloadTask task && !task.IsComplete)
|
| 55 |
+
{
|
| 56 |
+
task.Cancel();
|
| 57 |
+
}
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
+
// 清除已完成按钮点击事件
|
| 61 |
+
private void ClearCompleted_Click(object sender, RoutedEventArgs e)
|
| 62 |
+
{
|
| 63 |
+
var completed = DownloadManager.Instance.Tasks.Where(t => t.IsComplete).ToList();
|
| 64 |
+
foreach (var t in completed)
|
| 65 |
+
{
|
| 66 |
+
DownloadManager.Instance.Tasks.Remove(t);
|
| 67 |
+
}
|
| 68 |
+
}
|
| 69 |
+
}
|
| 70 |
+
}
|
csharp/BrowserTool/BrowserTool/Browser/DownloadTask.cs
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
using System;
|
| 2 |
+
using System.ComponentModel;
|
| 3 |
+
using CefSharp;
|
| 4 |
+
|
| 5 |
+
namespace BrowserTool.Browser
|
| 6 |
+
{
|
| 7 |
+
/// <summary>
|
| 8 |
+
/// 下载任务数据结构,支持属性变更通知
|
| 9 |
+
/// </summary>
|
| 10 |
+
public class DownloadTask : INotifyPropertyChanged
|
| 11 |
+
{
|
| 12 |
+
private string _fileName;
|
| 13 |
+
private string _fullPath;
|
| 14 |
+
private long _receivedBytes;
|
| 15 |
+
private long _totalBytes;
|
| 16 |
+
private bool _isComplete;
|
| 17 |
+
private bool _isCancelled;
|
| 18 |
+
private bool _isError;
|
| 19 |
+
private string _status;
|
| 20 |
+
|
| 21 |
+
public string FileName
|
| 22 |
+
{
|
| 23 |
+
get => _fileName;
|
| 24 |
+
set
|
| 25 |
+
{
|
| 26 |
+
_fileName = value;
|
| 27 |
+
OnPropertyChanged(nameof(FileName));
|
| 28 |
+
}
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
public string FullPath
|
| 32 |
+
{
|
| 33 |
+
get => _fullPath;
|
| 34 |
+
set
|
| 35 |
+
{
|
| 36 |
+
_fullPath = value;
|
| 37 |
+
OnPropertyChanged(nameof(FullPath));
|
| 38 |
+
}
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
public long ReceivedBytes
|
| 42 |
+
{
|
| 43 |
+
get => _receivedBytes;
|
| 44 |
+
set
|
| 45 |
+
{
|
| 46 |
+
_receivedBytes = value;
|
| 47 |
+
OnPropertyChanged(nameof(ReceivedBytes));
|
| 48 |
+
OnPropertyChanged(nameof(Progress));
|
| 49 |
+
}
|
| 50 |
+
}
|
| 51 |
+
|
| 52 |
+
public long TotalBytes
|
| 53 |
+
{
|
| 54 |
+
get => _totalBytes;
|
| 55 |
+
set
|
| 56 |
+
{
|
| 57 |
+
_totalBytes = value;
|
| 58 |
+
OnPropertyChanged(nameof(TotalBytes));
|
| 59 |
+
OnPropertyChanged(nameof(Progress));
|
| 60 |
+
}
|
| 61 |
+
}
|
| 62 |
+
|
| 63 |
+
public bool IsComplete
|
| 64 |
+
{
|
| 65 |
+
get => _isComplete;
|
| 66 |
+
set
|
| 67 |
+
{
|
| 68 |
+
_isComplete = value;
|
| 69 |
+
OnPropertyChanged(nameof(IsComplete));
|
| 70 |
+
}
|
| 71 |
+
}
|
| 72 |
+
|
| 73 |
+
public bool IsCancelled
|
| 74 |
+
{
|
| 75 |
+
get => _isCancelled;
|
| 76 |
+
set
|
| 77 |
+
{
|
| 78 |
+
_isCancelled = value;
|
| 79 |
+
OnPropertyChanged(nameof(IsCancelled));
|
| 80 |
+
}
|
| 81 |
+
}
|
| 82 |
+
|
| 83 |
+
public bool IsError
|
| 84 |
+
{
|
| 85 |
+
get => _isError;
|
| 86 |
+
set
|
| 87 |
+
{
|
| 88 |
+
_isError = value;
|
| 89 |
+
OnPropertyChanged(nameof(IsError));
|
| 90 |
+
}
|
| 91 |
+
}
|
| 92 |
+
|
| 93 |
+
public string Status
|
| 94 |
+
{
|
| 95 |
+
get => _status;
|
| 96 |
+
set
|
| 97 |
+
{
|
| 98 |
+
_status = value;
|
| 99 |
+
OnPropertyChanged(nameof(Status));
|
| 100 |
+
}
|
| 101 |
+
}
|
| 102 |
+
|
| 103 |
+
public DateTime StartTime { get; set; } = DateTime.Now;
|
| 104 |
+
|
| 105 |
+
// 下载回调,用于取消下载
|
| 106 |
+
[Newtonsoft.Json.JsonIgnore]
|
| 107 |
+
public IDownloadItemCallback Callback { get; set; }
|
| 108 |
+
|
| 109 |
+
public event PropertyChangedEventHandler PropertyChanged;
|
| 110 |
+
protected void OnPropertyChanged(string name)
|
| 111 |
+
{
|
| 112 |
+
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
|
| 113 |
+
}
|
| 114 |
+
|
| 115 |
+
public double Progress => TotalBytes > 0 ? (double)ReceivedBytes / TotalBytes : 0;
|
| 116 |
+
|
| 117 |
+
/// <summary>
|
| 118 |
+
/// 取消下载
|
| 119 |
+
/// </summary>
|
| 120 |
+
public void Cancel()
|
| 121 |
+
{
|
| 122 |
+
if (Callback != null && !Callback.IsDisposed)
|
| 123 |
+
{
|
| 124 |
+
Callback.Cancel();
|
| 125 |
+
IsCancelled = true;
|
| 126 |
+
Status = "已取消";
|
| 127 |
+
IsComplete = true;
|
| 128 |
+
}
|
| 129 |
+
}
|
| 130 |
+
}
|
| 131 |
+
}
|
csharp/BrowserTool/BrowserTool/Browser/InverseBoolConverter.cs
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
using System;
|
| 2 |
+
using System.Globalization;
|
| 3 |
+
using System.Windows.Data;
|
| 4 |
+
|
| 5 |
+
namespace BrowserTool.Browser
|
| 6 |
+
{
|
| 7 |
+
/// <summary>
|
| 8 |
+
/// 反转布尔值转换器
|
| 9 |
+
/// </summary>
|
| 10 |
+
public class InverseBoolConverter : IValueConverter
|
| 11 |
+
{
|
| 12 |
+
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
|
| 13 |
+
{
|
| 14 |
+
if (value is bool b) return !b;
|
| 15 |
+
return value;
|
| 16 |
+
}
|
| 17 |
+
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
|
| 18 |
+
{
|
| 19 |
+
if (value is bool b) return !b;
|
| 20 |
+
return value;
|
| 21 |
+
}
|
| 22 |
+
}
|
| 23 |
+
}
|
csharp/BrowserTool/BrowserTool/BrowserTool.csproj
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
|
| 2 |
+
<PropertyGroup>
|
| 3 |
+
<OutputType>WinExe</OutputType>
|
| 4 |
+
<TargetFramework>net8.0-windows</TargetFramework>
|
| 5 |
+
<UseWPF>true</UseWPF>
|
| 6 |
+
<UseWindowsForms>true</UseWindowsForms>
|
| 7 |
+
<RootNamespace>BrowserTool</RootNamespace>
|
| 8 |
+
<AssemblyName>BrowserTool</AssemblyName>
|
| 9 |
+
<ApplicationIcon>Resources\app.ico</ApplicationIcon>
|
| 10 |
+
<Platforms>AnyCPU;x64</Platforms>
|
| 11 |
+
</PropertyGroup>
|
| 12 |
+
<ItemGroup>
|
| 13 |
+
<None Remove="Resources\app.ico" />
|
| 14 |
+
<None Remove="Resources\default_icon.png" />
|
| 15 |
+
</ItemGroup>
|
| 16 |
+
|
| 17 |
+
<ItemGroup>
|
| 18 |
+
<PackageReference Include="CefSharp.Wpf.NETCore" Version="141.0.110" />
|
| 19 |
+
<PackageReference Include="Hardcodet.NotifyIcon.Wpf" Version="2.0.1" />
|
| 20 |
+
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.2" />
|
| 21 |
+
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.2" />
|
| 22 |
+
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.2">
|
| 23 |
+
<PrivateAssets>all</PrivateAssets>
|
| 24 |
+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
| 25 |
+
</PackageReference>
|
| 26 |
+
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.2">
|
| 27 |
+
<PrivateAssets>all</PrivateAssets>
|
| 28 |
+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
| 29 |
+
</PackageReference>
|
| 30 |
+
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
| 31 |
+
<PackageReference Include="NLog" Version="5.2.8" />
|
| 32 |
+
<PackageReference Include="System.Drawing.Common" Version="8.0.0" />
|
| 33 |
+
</ItemGroup>
|
| 34 |
+
|
| 35 |
+
<ItemGroup>
|
| 36 |
+
<Resource Include="Resources\app.ico">
|
| 37 |
+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
| 38 |
+
</Resource>
|
| 39 |
+
</ItemGroup>
|
| 40 |
+
|
| 41 |
+
<ItemGroup>
|
| 42 |
+
<Resource Include="Resources\default_icon.png">
|
| 43 |
+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
| 44 |
+
</Resource>
|
| 45 |
+
</ItemGroup>
|
| 46 |
+
|
| 47 |
+
<ItemGroup>
|
| 48 |
+
<None Update="Resources\checkin_icon1.png">
|
| 49 |
+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
| 50 |
+
</None>
|
| 51 |
+
<None Update="Resources\checkin_icon2.png">
|
| 52 |
+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
| 53 |
+
</None>
|
| 54 |
+
<None Update="Resources\image_matcher.py">
|
| 55 |
+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
| 56 |
+
</None>
|
| 57 |
+
</ItemGroup>
|
| 58 |
+
|
| 59 |
+
<ItemGroup>
|
| 60 |
+
<None Update="NLog.config">
|
| 61 |
+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
| 62 |
+
</None>
|
| 63 |
+
</ItemGroup>
|
| 64 |
+
</Project>
|
csharp/BrowserTool/BrowserTool/ChangePasswordWindow.xaml
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<Window x:Class="BrowserTool.ChangePasswordWindow"
|
| 2 |
+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
| 3 |
+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
| 4 |
+
Title="修改启动密码" Height="300" Width="420" WindowStartupLocation="CenterScreen" ResizeMode="NoResize" WindowStyle="ToolWindow">
|
| 5 |
+
<Window.Resources>
|
| 6 |
+
<Style x:Key="ModernButtonStyle" TargetType="Button">
|
| 7 |
+
<Setter Property="Background" Value="#444"/>
|
| 8 |
+
<Setter Property="Foreground" Value="#FFF"/>
|
| 9 |
+
<Setter Property="BorderBrush" Value="#444"/>
|
| 10 |
+
<Setter Property="BorderThickness" Value="0"/>
|
| 11 |
+
<Setter Property="FontSize" Value="15"/>
|
| 12 |
+
<Setter Property="Height" Value="32"/>
|
| 13 |
+
<Setter Property="MinWidth" Value="72"/>
|
| 14 |
+
<Setter Property="Margin" Value="0,0,10,0"/>
|
| 15 |
+
<Setter Property="Cursor" Value="Hand"/>
|
| 16 |
+
<Setter Property="Template">
|
| 17 |
+
<Setter.Value>
|
| 18 |
+
<ControlTemplate TargetType="Button">
|
| 19 |
+
<Border Background="{TemplateBinding Background}" CornerRadius="6">
|
| 20 |
+
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
|
| 21 |
+
</Border>
|
| 22 |
+
<ControlTemplate.Triggers>
|
| 23 |
+
<Trigger Property="IsMouseOver" Value="True">
|
| 24 |
+
<Setter Property="Background" Value="#555"/>
|
| 25 |
+
</Trigger>
|
| 26 |
+
<Trigger Property="IsPressed" Value="True">
|
| 27 |
+
<Setter Property="Background" Value="#666"/>
|
| 28 |
+
</Trigger>
|
| 29 |
+
</ControlTemplate.Triggers>
|
| 30 |
+
</ControlTemplate>
|
| 31 |
+
</Setter.Value>
|
| 32 |
+
</Setter>
|
| 33 |
+
</Style>
|
| 34 |
+
</Window.Resources>
|
| 35 |
+
<ScrollViewer VerticalScrollBarVisibility="Auto">
|
| 36 |
+
<Grid Margin="20">
|
| 37 |
+
<Grid.RowDefinitions>
|
| 38 |
+
<RowDefinition Height="Auto"/>
|
| 39 |
+
<RowDefinition Height="Auto"/>
|
| 40 |
+
<RowDefinition Height="Auto"/>
|
| 41 |
+
<RowDefinition Height="Auto"/>
|
| 42 |
+
<RowDefinition Height="Auto"/>
|
| 43 |
+
<RowDefinition Height="Auto"/>
|
| 44 |
+
<RowDefinition Height="Auto"/>
|
| 45 |
+
<RowDefinition Height="*"/>
|
| 46 |
+
</Grid.RowDefinitions>
|
| 47 |
+
<TextBlock Text="请输入旧密码" Grid.Row="0"/>
|
| 48 |
+
<PasswordBox x:Name="txtOld" Grid.Row="1" Height="28" Margin="0,8,0,12"/>
|
| 49 |
+
<TextBlock Text="新密码" Grid.Row="2"/>
|
| 50 |
+
<PasswordBox x:Name="txtNew1" Grid.Row="3" Height="28" Margin="0,8,0,0"/>
|
| 51 |
+
<TextBlock Text="再次输入新密码" Grid.Row="4"/>
|
| 52 |
+
<PasswordBox x:Name="txtNew2" Grid.Row="5" Height="28" Margin="0,8,0,0"/>
|
| 53 |
+
<StackPanel Grid.Row="6" Orientation="Horizontal" HorizontalAlignment="Center" Margin="0,20,0,0">
|
| 54 |
+
<Button Content="确定" Width="80" Style="{StaticResource ModernButtonStyle}" Click="BtnOK_Click"/>
|
| 55 |
+
<Button Content="取消" Width="80" Style="{StaticResource ModernButtonStyle}" Margin="0" Click="BtnCancel_Click"/>
|
| 56 |
+
</StackPanel>
|
| 57 |
+
</Grid>
|
| 58 |
+
</ScrollViewer>
|
| 59 |
+
</Window>
|
csharp/BrowserTool/BrowserTool/ChangePasswordWindow.xaml.cs
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
using System.Windows;
|
| 2 |
+
using BrowserTool.Utils;
|
| 3 |
+
|
| 4 |
+
namespace BrowserTool
|
| 5 |
+
{
|
| 6 |
+
public partial class ChangePasswordWindow : Window
|
| 7 |
+
{
|
| 8 |
+
public ChangePasswordWindow()
|
| 9 |
+
{
|
| 10 |
+
InitializeComponent();
|
| 11 |
+
}
|
| 12 |
+
private void BtnOK_Click(object sender, RoutedEventArgs e)
|
| 13 |
+
{
|
| 14 |
+
if (!PasswordHelper.VerifyOrSetStartupPassword(txtOld.Password))
|
| 15 |
+
{
|
| 16 |
+
MessageBox.Show("旧密码错误!", "提示", MessageBoxButton.OK, MessageBoxImage.Error);
|
| 17 |
+
txtOld.Clear();
|
| 18 |
+
txtOld.Focus();
|
| 19 |
+
return;
|
| 20 |
+
}
|
| 21 |
+
if (string.IsNullOrWhiteSpace(txtNew1.Password) || txtNew1.Password != txtNew2.Password)
|
| 22 |
+
{
|
| 23 |
+
MessageBox.Show("两次输入的新密码不一致或为空!", "提示", MessageBoxButton.OK, MessageBoxImage.Error);
|
| 24 |
+
txtNew1.Clear();
|
| 25 |
+
txtNew2.Clear();
|
| 26 |
+
txtNew1.Focus();
|
| 27 |
+
return;
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
// 1. 用旧密码设置KeyManager
|
| 31 |
+
KeyManager.SetPassword(txtOld.Password);
|
| 32 |
+
var allGroups = Database.SiteConfig.GetAllGroups();
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
// 2. 用新密码设置KeyManager
|
| 36 |
+
KeyManager.SetPassword(txtNew1.Password);
|
| 37 |
+
foreach (var group in allGroups)
|
| 38 |
+
{
|
| 39 |
+
foreach (var site in group.Sites)
|
| 40 |
+
{
|
| 41 |
+
Database.SiteConfig.SaveSite(site);
|
| 42 |
+
}
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
// 3. 保存新启动密码
|
| 46 |
+
PasswordHelper.SaveStartupPassword(txtNew1.Password);
|
| 47 |
+
MessageBox.Show("密码修改成功!", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
// 4. 激活主窗口
|
| 51 |
+
if (Owner != null)
|
| 52 |
+
{
|
| 53 |
+
Owner.Show();
|
| 54 |
+
Owner.WindowState = WindowState.Normal;
|
| 55 |
+
Owner.Activate();
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
DialogResult = true;
|
| 59 |
+
Close();
|
| 60 |
+
//Application.Current.Shutdown();
|
| 61 |
+
}
|
| 62 |
+
private void BtnCancel_Click(object sender, RoutedEventArgs e)
|
| 63 |
+
{
|
| 64 |
+
DialogResult = false;
|
| 65 |
+
Close();
|
| 66 |
+
}
|
| 67 |
+
}
|
| 68 |
+
}
|
csharp/BrowserTool/BrowserTool/CheckInResultQueue.cs
ADDED
|
@@ -0,0 +1,182 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
using System;
|
| 2 |
+
using System.Collections.Concurrent;
|
| 3 |
+
using System.Threading;
|
| 4 |
+
using System.Threading.Tasks;
|
| 5 |
+
using BrowserTool.Utils;
|
| 6 |
+
using NLog;
|
| 7 |
+
|
| 8 |
+
namespace BrowserTool
|
| 9 |
+
{
|
| 10 |
+
/// <summary>
|
| 11 |
+
/// 打卡结果数据
|
| 12 |
+
/// </summary>
|
| 13 |
+
public class CheckInResult
|
| 14 |
+
{
|
| 15 |
+
/// <summary>
|
| 16 |
+
/// 是否成功
|
| 17 |
+
/// </summary>
|
| 18 |
+
public bool IsSuccess { get; set; }
|
| 19 |
+
|
| 20 |
+
/// <summary>
|
| 21 |
+
/// 页面URL
|
| 22 |
+
/// </summary>
|
| 23 |
+
public string Url { get; set; } = string.Empty;
|
| 24 |
+
|
| 25 |
+
/// <summary>
|
| 26 |
+
/// 检查时间
|
| 27 |
+
/// </summary>
|
| 28 |
+
public DateTime CheckTime { get; set; } = DateTime.Now;
|
| 29 |
+
|
| 30 |
+
/// <summary>
|
| 31 |
+
/// 备注信息
|
| 32 |
+
/// </summary>
|
| 33 |
+
public string Message { get; set; } = string.Empty;
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
/// <summary>
|
| 37 |
+
/// 打卡结果队列管理器
|
| 38 |
+
/// </summary>
|
| 39 |
+
public class CheckInResultQueue
|
| 40 |
+
{
|
| 41 |
+
private static readonly Logger _logger = LogManager.GetCurrentClassLogger();
|
| 42 |
+
private static readonly Lazy<CheckInResultQueue> _instance = new(() => new CheckInResultQueue());
|
| 43 |
+
|
| 44 |
+
/// <summary>
|
| 45 |
+
/// 单例实例
|
| 46 |
+
/// </summary>
|
| 47 |
+
public static CheckInResultQueue Instance => _instance.Value;
|
| 48 |
+
|
| 49 |
+
/// <summary>
|
| 50 |
+
/// 结果队列
|
| 51 |
+
/// </summary>
|
| 52 |
+
private readonly ConcurrentQueue<CheckInResult> _resultQueue = new();
|
| 53 |
+
|
| 54 |
+
/// <summary>
|
| 55 |
+
/// 等待结果的信号量
|
| 56 |
+
/// </summary>
|
| 57 |
+
private readonly SemaphoreSlim _resultSemaphore = new(0);
|
| 58 |
+
|
| 59 |
+
/// <summary>
|
| 60 |
+
/// 私有构造函数
|
| 61 |
+
/// </summary>
|
| 62 |
+
private CheckInResultQueue()
|
| 63 |
+
{
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
/// <summary>
|
| 67 |
+
/// 添加检查结果到队列
|
| 68 |
+
/// </summary>
|
| 69 |
+
/// <param name="result">检查结果</param>
|
| 70 |
+
public void EnqueueResult(CheckInResult result)
|
| 71 |
+
{
|
| 72 |
+
try
|
| 73 |
+
{
|
| 74 |
+
_resultQueue.Enqueue(result);
|
| 75 |
+
_resultSemaphore.Release(); // 通知等待的线程
|
| 76 |
+
_logger.Debug($"添加打卡结果到队列: 成功={result.IsSuccess}, URL={result.Url}, 消息={result.Message}");
|
| 77 |
+
}
|
| 78 |
+
catch (Exception ex)
|
| 79 |
+
{
|
| 80 |
+
_logger.Error("添加结果到队列时发生异常", ex);
|
| 81 |
+
}
|
| 82 |
+
}
|
| 83 |
+
|
| 84 |
+
/// <summary>
|
| 85 |
+
/// 从队列获取检查结果(带超时)
|
| 86 |
+
/// </summary>
|
| 87 |
+
/// <param name="timeoutMinutes">超时时间(分钟)</param>
|
| 88 |
+
/// <returns>检查结果,超时返回null</returns>
|
| 89 |
+
public async Task<CheckInResult?> DequeueResultAsync(int timeoutMinutes = 2)
|
| 90 |
+
{
|
| 91 |
+
try
|
| 92 |
+
{
|
| 93 |
+
_logger.Debug($"开始等待打卡结果,超时时间: {timeoutMinutes}分钟");
|
| 94 |
+
|
| 95 |
+
// 等待结果或超时
|
| 96 |
+
var timeout = TimeSpan.FromMinutes(timeoutMinutes);
|
| 97 |
+
bool hasResult = await _resultSemaphore.WaitAsync(timeout);
|
| 98 |
+
|
| 99 |
+
if (hasResult)
|
| 100 |
+
{
|
| 101 |
+
if (_resultQueue.TryDequeue(out CheckInResult? result))
|
| 102 |
+
{
|
| 103 |
+
_logger.Debug($"成功获取打卡结果: 成功={result.IsSuccess}, URL={result.Url}");
|
| 104 |
+
return result;
|
| 105 |
+
}
|
| 106 |
+
else
|
| 107 |
+
{
|
| 108 |
+
_logger.Warn("信号量被释放但队列为空");
|
| 109 |
+
return null;
|
| 110 |
+
}
|
| 111 |
+
}
|
| 112 |
+
else
|
| 113 |
+
{
|
| 114 |
+
_logger.Debug($"等待打卡结果超时({timeoutMinutes}分钟)");
|
| 115 |
+
return null;
|
| 116 |
+
}
|
| 117 |
+
}
|
| 118 |
+
catch (Exception ex)
|
| 119 |
+
{
|
| 120 |
+
_logger.Error("从队列获取结果时发生异常", ex);
|
| 121 |
+
return null;
|
| 122 |
+
}
|
| 123 |
+
}
|
| 124 |
+
|
| 125 |
+
/// <summary>
|
| 126 |
+
/// 清空队列
|
| 127 |
+
/// </summary>
|
| 128 |
+
public async Task ClearAsync()
|
| 129 |
+
{
|
| 130 |
+
try
|
| 131 |
+
{
|
| 132 |
+
while (_resultQueue.TryDequeue(out _))
|
| 133 |
+
{
|
| 134 |
+
// 清空队列
|
| 135 |
+
}
|
| 136 |
+
|
| 137 |
+
// 重置信号量
|
| 138 |
+
while (_resultSemaphore.CurrentCount > 0)
|
| 139 |
+
{
|
| 140 |
+
await _resultSemaphore.WaitAsync(0);
|
| 141 |
+
}
|
| 142 |
+
|
| 143 |
+
_logger.Debug("打卡结果队列已清空");
|
| 144 |
+
}
|
| 145 |
+
catch (Exception ex)
|
| 146 |
+
{
|
| 147 |
+
_logger.Error("清空队列时发生异常", ex);
|
| 148 |
+
}
|
| 149 |
+
}
|
| 150 |
+
|
| 151 |
+
/// <summary>
|
| 152 |
+
/// 清空队列(同步版本)
|
| 153 |
+
/// </summary>
|
| 154 |
+
public void Clear()
|
| 155 |
+
{
|
| 156 |
+
try
|
| 157 |
+
{
|
| 158 |
+
while (_resultQueue.TryDequeue(out _))
|
| 159 |
+
{
|
| 160 |
+
// 清空队列
|
| 161 |
+
}
|
| 162 |
+
|
| 163 |
+
// 重置信号量(同步方式)
|
| 164 |
+
while (_resultSemaphore.CurrentCount > 0)
|
| 165 |
+
{
|
| 166 |
+
_resultSemaphore.Wait(0);
|
| 167 |
+
}
|
| 168 |
+
|
| 169 |
+
_logger.Debug("打卡结果队列已清空");
|
| 170 |
+
}
|
| 171 |
+
catch (Exception ex)
|
| 172 |
+
{
|
| 173 |
+
_logger.Error("清空队列时发生异常", ex);
|
| 174 |
+
}
|
| 175 |
+
}
|
| 176 |
+
|
| 177 |
+
/// <summary>
|
| 178 |
+
/// 获取队列中的结果数量
|
| 179 |
+
/// </summary>
|
| 180 |
+
public int Count => _resultQueue.Count;
|
| 181 |
+
}
|
| 182 |
+
}
|
csharp/BrowserTool/BrowserTool/Controls/LoadingControl.xaml
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<UserControl x:Class="BrowserTool.Controls.LoadingControl"
|
| 2 |
+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
| 3 |
+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
| 4 |
+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
| 5 |
+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
| 6 |
+
xmlns:local="clr-namespace:BrowserTool.Controls"
|
| 7 |
+
xmlns:converters="clr-namespace:BrowserTool.Converters"
|
| 8 |
+
mc:Ignorable="d"
|
| 9 |
+
d:DesignHeight="100" d:DesignWidth="100">
|
| 10 |
+
<UserControl.Resources>
|
| 11 |
+
<converters:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
|
| 12 |
+
</UserControl.Resources>
|
| 13 |
+
<Grid>
|
| 14 |
+
<Border Background="#80000000" Visibility="{Binding IsLoading, RelativeSource={RelativeSource AncestorType=UserControl}, Converter={StaticResource BooleanToVisibilityConverter}}">
|
| 15 |
+
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
|
| 16 |
+
<Canvas Width="60" Height="60">
|
| 17 |
+
<!-- 6个圆点环形排列动画 -->
|
| 18 |
+
<Ellipse Width="10" Height="10" Fill="#2979FF" Canvas.Left="50" Canvas.Top="28">
|
| 19 |
+
<Ellipse.RenderTransform>
|
| 20 |
+
<RotateTransform x:Name="Dot1" CenterX="-20" CenterY="2"/>
|
| 21 |
+
</Ellipse.RenderTransform>
|
| 22 |
+
</Ellipse>
|
| 23 |
+
<Ellipse Width="10" Height="10" Fill="#2979FF" Canvas.Left="44" Canvas.Top="44">
|
| 24 |
+
<Ellipse.RenderTransform>
|
| 25 |
+
<RotateTransform x:Name="Dot2" CenterX="-14" CenterY="-14"/>
|
| 26 |
+
</Ellipse.RenderTransform>
|
| 27 |
+
</Ellipse>
|
| 28 |
+
<Ellipse Width="10" Height="10" Fill="#2979FF" Canvas.Left="28" Canvas.Top="50">
|
| 29 |
+
<Ellipse.RenderTransform>
|
| 30 |
+
<RotateTransform x:Name="Dot3" CenterX="2" CenterY="-20"/>
|
| 31 |
+
</Ellipse.RenderTransform>
|
| 32 |
+
</Ellipse>
|
| 33 |
+
<Ellipse Width="10" Height="10" Fill="#2979FF" Canvas.Left="12" Canvas.Top="44">
|
| 34 |
+
<Ellipse.RenderTransform>
|
| 35 |
+
<RotateTransform x:Name="Dot4" CenterX="18" CenterY="-14"/>
|
| 36 |
+
</Ellipse.RenderTransform>
|
| 37 |
+
</Ellipse>
|
| 38 |
+
<Ellipse Width="10" Height="10" Fill="#2979FF" Canvas.Left="6" Canvas.Top="28">
|
| 39 |
+
<Ellipse.RenderTransform>
|
| 40 |
+
<RotateTransform x:Name="Dot5" CenterX="24" CenterY="2"/>
|
| 41 |
+
</Ellipse.RenderTransform>
|
| 42 |
+
</Ellipse>
|
| 43 |
+
<Ellipse Width="10" Height="10" Fill="#2979FF" Canvas.Left="12" Canvas.Top="12">
|
| 44 |
+
<Ellipse.RenderTransform>
|
| 45 |
+
<RotateTransform x:Name="Dot6" CenterX="18" CenterY="18"/>
|
| 46 |
+
</Ellipse.RenderTransform>
|
| 47 |
+
</Ellipse>
|
| 48 |
+
<Canvas.Triggers>
|
| 49 |
+
<EventTrigger RoutedEvent="Loaded">
|
| 50 |
+
<BeginStoryboard>
|
| 51 |
+
<Storyboard RepeatBehavior="Forever">
|
| 52 |
+
<DoubleAnimation Storyboard.TargetName="Dot1" Storyboard.TargetProperty="Angle" From="0" To="360" Duration="0:0:1"/>
|
| 53 |
+
<DoubleAnimation Storyboard.TargetName="Dot2" Storyboard.TargetProperty="Angle" From="60" To="420" Duration="0:0:1"/>
|
| 54 |
+
<DoubleAnimation Storyboard.TargetName="Dot3" Storyboard.TargetProperty="Angle" From="120" To="480" Duration="0:0:1"/>
|
| 55 |
+
<DoubleAnimation Storyboard.TargetName="Dot4" Storyboard.TargetProperty="Angle" From="180" To="540" Duration="0:0:1"/>
|
| 56 |
+
<DoubleAnimation Storyboard.TargetName="Dot5" Storyboard.TargetProperty="Angle" From="240" To="600" Duration="0:0:1"/>
|
| 57 |
+
<DoubleAnimation Storyboard.TargetName="Dot6" Storyboard.TargetProperty="Angle" From="300" To="660" Duration="0:0:1"/>
|
| 58 |
+
</Storyboard>
|
| 59 |
+
</BeginStoryboard>
|
| 60 |
+
</EventTrigger>
|
| 61 |
+
</Canvas.Triggers>
|
| 62 |
+
</Canvas>
|
| 63 |
+
<TextBlock Text="加载中..." Foreground="White" HorizontalAlignment="Center" FontSize="16" Margin="0,10,0,0"/>
|
| 64 |
+
</StackPanel>
|
| 65 |
+
</Border>
|
| 66 |
+
</Grid>
|
| 67 |
+
</UserControl>
|
csharp/BrowserTool/BrowserTool/Controls/LoadingControl.xaml.cs
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
using System.Windows;
|
| 2 |
+
using System.Windows.Controls;
|
| 3 |
+
|
| 4 |
+
namespace BrowserTool.Controls
|
| 5 |
+
{
|
| 6 |
+
/// <summary>
|
| 7 |
+
/// LoadingControl.xaml 的交互逻辑
|
| 8 |
+
/// </summary>
|
| 9 |
+
public partial class LoadingControl : UserControl
|
| 10 |
+
{
|
| 11 |
+
public static readonly DependencyProperty IsLoadingProperty =
|
| 12 |
+
DependencyProperty.Register("IsLoading", typeof(bool), typeof(LoadingControl), new PropertyMetadata(false));
|
| 13 |
+
|
| 14 |
+
public bool IsLoading
|
| 15 |
+
{
|
| 16 |
+
get { return (bool)GetValue(IsLoadingProperty); }
|
| 17 |
+
set { SetValue(IsLoadingProperty, value); }
|
| 18 |
+
}
|
| 19 |
+
|
| 20 |
+
public LoadingControl()
|
| 21 |
+
{
|
| 22 |
+
InitializeComponent();
|
| 23 |
+
}
|
| 24 |
+
}
|
| 25 |
+
}
|
csharp/BrowserTool/BrowserTool/Controls/SearchBar.xaml
ADDED
|
@@ -0,0 +1,193 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<UserControl x:Class="BrowserTool.Controls.SearchBar"
|
| 2 |
+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
| 3 |
+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
| 4 |
+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
| 5 |
+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
| 6 |
+
mc:Ignorable="d"
|
| 7 |
+
d:DesignHeight="40" d:DesignWidth="350">
|
| 8 |
+
<Border Background="#2D2D30" BorderBrush="#3F3F46" BorderThickness="1" CornerRadius="3">
|
| 9 |
+
<Grid Height="40">
|
| 10 |
+
<Grid.ColumnDefinitions>
|
| 11 |
+
<ColumnDefinition Width="*"/>
|
| 12 |
+
<ColumnDefinition Width="Auto"/>
|
| 13 |
+
<ColumnDefinition Width="Auto"/>
|
| 14 |
+
<ColumnDefinition Width="Auto"/>
|
| 15 |
+
<ColumnDefinition Width="Auto"/>
|
| 16 |
+
<ColumnDefinition Width="Auto"/>
|
| 17 |
+
</Grid.ColumnDefinitions>
|
| 18 |
+
|
| 19 |
+
<!-- 搜索输入框 -->
|
| 20 |
+
<TextBox x:Name="SearchTextBox"
|
| 21 |
+
Grid.Column="0"
|
| 22 |
+
Margin="8,8,4,8"
|
| 23 |
+
Background="#1E1E1E"
|
| 24 |
+
Foreground="#FFFFFF"
|
| 25 |
+
BorderBrush="#555555"
|
| 26 |
+
BorderThickness="1"
|
| 27 |
+
Padding="6,4"
|
| 28 |
+
VerticalContentAlignment="Center"
|
| 29 |
+
FontSize="13"
|
| 30 |
+
KeyDown="SearchTextBox_KeyDown"
|
| 31 |
+
TextChanged="SearchTextBox_TextChanged"/>
|
| 32 |
+
|
| 33 |
+
<!-- 搜索结果计数 -->
|
| 34 |
+
<TextBlock x:Name="ResultCountText"
|
| 35 |
+
Grid.Column="1"
|
| 36 |
+
Margin="4,0,8,0"
|
| 37 |
+
VerticalAlignment="Center"
|
| 38 |
+
Foreground="#CCCCCC"
|
| 39 |
+
FontSize="12"
|
| 40 |
+
Text="0/0"/>
|
| 41 |
+
|
| 42 |
+
<!-- 上一个按钮 -->
|
| 43 |
+
<Button x:Name="PreviousButton"
|
| 44 |
+
Grid.Column="2"
|
| 45 |
+
Width="28" Height="28"
|
| 46 |
+
Margin="2"
|
| 47 |
+
Background="Transparent"
|
| 48 |
+
BorderThickness="0"
|
| 49 |
+
Click="PreviousButton_Click"
|
| 50 |
+
ToolTip="上一个 (Shift+Enter)">
|
| 51 |
+
<Button.Style>
|
| 52 |
+
<Style TargetType="Button">
|
| 53 |
+
<Setter Property="Background" Value="Transparent"/>
|
| 54 |
+
<Setter Property="Template">
|
| 55 |
+
<Setter.Value>
|
| 56 |
+
<ControlTemplate TargetType="Button">
|
| 57 |
+
<Border x:Name="Border" Background="Transparent" CornerRadius="2">
|
| 58 |
+
<TextBlock Text=""
|
| 59 |
+
FontFamily="Segoe MDL2 Assets"
|
| 60 |
+
FontSize="12"
|
| 61 |
+
Foreground="#CCCCCC"
|
| 62 |
+
HorizontalAlignment="Center"
|
| 63 |
+
VerticalAlignment="Center"/>
|
| 64 |
+
</Border>
|
| 65 |
+
<ControlTemplate.Triggers>
|
| 66 |
+
<Trigger Property="IsMouseOver" Value="True">
|
| 67 |
+
<Setter TargetName="Border" Property="Background" Value="#3F3F46"/>
|
| 68 |
+
</Trigger>
|
| 69 |
+
<Trigger Property="IsEnabled" Value="False">
|
| 70 |
+
<Setter Property="Opacity" Value="0.5"/>
|
| 71 |
+
</Trigger>
|
| 72 |
+
</ControlTemplate.Triggers>
|
| 73 |
+
</ControlTemplate>
|
| 74 |
+
</Setter.Value>
|
| 75 |
+
</Setter>
|
| 76 |
+
</Style>
|
| 77 |
+
</Button.Style>
|
| 78 |
+
</Button>
|
| 79 |
+
|
| 80 |
+
<!-- 下一个按钮 -->
|
| 81 |
+
<Button x:Name="NextButton"
|
| 82 |
+
Grid.Column="3"
|
| 83 |
+
Width="28" Height="28"
|
| 84 |
+
Margin="2"
|
| 85 |
+
Background="Transparent"
|
| 86 |
+
BorderThickness="0"
|
| 87 |
+
Click="NextButton_Click"
|
| 88 |
+
ToolTip="下一个 (Enter)">
|
| 89 |
+
<Button.Style>
|
| 90 |
+
<Style TargetType="Button">
|
| 91 |
+
<Setter Property="Background" Value="Transparent"/>
|
| 92 |
+
<Setter Property="Template">
|
| 93 |
+
<Setter.Value>
|
| 94 |
+
<ControlTemplate TargetType="Button">
|
| 95 |
+
<Border x:Name="Border" Background="Transparent" CornerRadius="2">
|
| 96 |
+
<TextBlock Text=""
|
| 97 |
+
FontFamily="Segoe MDL2 Assets"
|
| 98 |
+
FontSize="12"
|
| 99 |
+
Foreground="#CCCCCC"
|
| 100 |
+
HorizontalAlignment="Center"
|
| 101 |
+
VerticalAlignment="Center"/>
|
| 102 |
+
</Border>
|
| 103 |
+
<ControlTemplate.Triggers>
|
| 104 |
+
<Trigger Property="IsMouseOver" Value="True">
|
| 105 |
+
<Setter TargetName="Border" Property="Background" Value="#3F3F46"/>
|
| 106 |
+
</Trigger>
|
| 107 |
+
<Trigger Property="IsEnabled" Value="False">
|
| 108 |
+
<Setter Property="Opacity" Value="0.5"/>
|
| 109 |
+
</Trigger>
|
| 110 |
+
</ControlTemplate.Triggers>
|
| 111 |
+
</ControlTemplate>
|
| 112 |
+
</Setter.Value>
|
| 113 |
+
</Setter>
|
| 114 |
+
</Style>
|
| 115 |
+
</Button.Style>
|
| 116 |
+
</Button>
|
| 117 |
+
|
| 118 |
+
<!-- 区分大小写按钮 -->
|
| 119 |
+
<Button x:Name="MatchCaseButton"
|
| 120 |
+
Grid.Column="4"
|
| 121 |
+
Width="28" Height="28"
|
| 122 |
+
Margin="2"
|
| 123 |
+
Background="Transparent"
|
| 124 |
+
BorderThickness="0"
|
| 125 |
+
Click="MatchCaseButton_Click"
|
| 126 |
+
ToolTip="区分大小写">
|
| 127 |
+
<Button.Style>
|
| 128 |
+
<Style TargetType="Button">
|
| 129 |
+
<Setter Property="Background" Value="Transparent"/>
|
| 130 |
+
<Setter Property="Template">
|
| 131 |
+
<Setter.Value>
|
| 132 |
+
<ControlTemplate TargetType="Button">
|
| 133 |
+
<Border x:Name="Border" Background="Transparent" CornerRadius="2">
|
| 134 |
+
<TextBlock Text="Aa"
|
| 135 |
+
FontFamily="Segoe UI"
|
| 136 |
+
FontSize="11"
|
| 137 |
+
FontWeight="Bold"
|
| 138 |
+
Foreground="#CCCCCC"
|
| 139 |
+
HorizontalAlignment="Center"
|
| 140 |
+
VerticalAlignment="Center"/>
|
| 141 |
+
</Border>
|
| 142 |
+
<ControlTemplate.Triggers>
|
| 143 |
+
<Trigger Property="IsMouseOver" Value="True">
|
| 144 |
+
<Setter TargetName="Border" Property="Background" Value="#3F3F46"/>
|
| 145 |
+
</Trigger>
|
| 146 |
+
<DataTrigger Binding="{Binding IsMatchCase, RelativeSource={RelativeSource AncestorType=UserControl}}" Value="True">
|
| 147 |
+
<Setter TargetName="Border" Property="Background" Value="#0078D4"/>
|
| 148 |
+
</DataTrigger>
|
| 149 |
+
</ControlTemplate.Triggers>
|
| 150 |
+
</ControlTemplate>
|
| 151 |
+
</Setter.Value>
|
| 152 |
+
</Setter>
|
| 153 |
+
</Style>
|
| 154 |
+
</Button.Style>
|
| 155 |
+
</Button>
|
| 156 |
+
|
| 157 |
+
<!-- 关闭按钮 -->
|
| 158 |
+
<Button x:Name="CloseButton"
|
| 159 |
+
Grid.Column="5"
|
| 160 |
+
Width="28" Height="28"
|
| 161 |
+
Margin="2,2,8,2"
|
| 162 |
+
Background="Transparent"
|
| 163 |
+
BorderThickness="0"
|
| 164 |
+
Click="CloseButton_Click"
|
| 165 |
+
ToolTip="关闭 (Esc)">
|
| 166 |
+
<Button.Style>
|
| 167 |
+
<Style TargetType="Button">
|
| 168 |
+
<Setter Property="Background" Value="Transparent"/>
|
| 169 |
+
<Setter Property="Template">
|
| 170 |
+
<Setter.Value>
|
| 171 |
+
<ControlTemplate TargetType="Button">
|
| 172 |
+
<Border x:Name="Border" Background="Transparent" CornerRadius="2">
|
| 173 |
+
<TextBlock Text=""
|
| 174 |
+
FontFamily="Segoe MDL2 Assets"
|
| 175 |
+
FontSize="12"
|
| 176 |
+
Foreground="#CCCCCC"
|
| 177 |
+
HorizontalAlignment="Center"
|
| 178 |
+
VerticalAlignment="Center"/>
|
| 179 |
+
</Border>
|
| 180 |
+
<ControlTemplate.Triggers>
|
| 181 |
+
<Trigger Property="IsMouseOver" Value="True">
|
| 182 |
+
<Setter TargetName="Border" Property="Background" Value="#E81123"/>
|
| 183 |
+
</Trigger>
|
| 184 |
+
</ControlTemplate.Triggers>
|
| 185 |
+
</ControlTemplate>
|
| 186 |
+
</Setter.Value>
|
| 187 |
+
</Setter>
|
| 188 |
+
</Style>
|
| 189 |
+
</Button.Style>
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
using System;
|
| 2 |
+
using System.Windows;
|
| 3 |
+
using System.Windows.Controls;
|
| 4 |
+
using System.Windows.Input;
|
| 5 |
+
using CefSharp;
|
| 6 |
+
using CefSharp.Wpf;
|
| 7 |
+
|
| 8 |
+
namespace BrowserTool.Controls
|
| 9 |
+
{
|
| 10 |
+
/// <summary>
|
| 11 |
+
/// SearchBar.xaml 的交互逻辑
|
| 12 |
+
/// </summary>
|
| 13 |
+
public partial class SearchBar : UserControl
|
| 14 |
+
{
|
| 15 |
+
private ChromiumWebBrowser _browser;
|
| 16 |
+
private int _currentMatchIndex = 0;
|
| 17 |
+
private int _totalMatches = 0;
|
| 18 |
+
private bool _isMatchCase = false;
|
| 19 |
+
|
| 20 |
+
public event EventHandler CloseRequested;
|
| 21 |
+
|
| 22 |
+
public bool IsMatchCase
|
| 23 |
+
{
|
| 24 |
+
get { return _isMatchCase; }
|
| 25 |
+
set
|
| 26 |
+
{
|
| 27 |
+
_isMatchCase = value;
|
| 28 |
+
UpdateMatchCaseButtonStyle();
|
| 29 |
+
}
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
public SearchBar()
|
| 33 |
+
{
|
| 34 |
+
InitializeComponent();
|
| 35 |
+
UpdateUI();
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
public void SetBrowser(ChromiumWebBrowser browser)
|
| 39 |
+
{
|
| 40 |
+
_browser = browser;
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
public void FocusSearchBox()
|
| 44 |
+
{
|
| 45 |
+
SearchTextBox.Focus();
|
| 46 |
+
SearchTextBox.SelectAll();
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
private void SearchTextBox_TextChanged(object sender, TextChangedEventArgs e)
|
| 50 |
+
{
|
| 51 |
+
var searchText = SearchTextBox.Text;
|
| 52 |
+
|
| 53 |
+
if (string.IsNullOrEmpty(searchText))
|
| 54 |
+
{
|
| 55 |
+
StopFind();
|
| 56 |
+
return;
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
StartFind(searchText);
|
| 60 |
+
}
|
| 61 |
+
|
| 62 |
+
private void SearchTextBox_KeyDown(object sender, KeyEventArgs e)
|
| 63 |
+
{
|
| 64 |
+
switch (e.Key)
|
| 65 |
+
{
|
| 66 |
+
case Key.Enter:
|
| 67 |
+
if (Keyboard.Modifiers == ModifierKeys.Shift)
|
| 68 |
+
{
|
| 69 |
+
FindPrevious();
|
| 70 |
+
}
|
| 71 |
+
else
|
| 72 |
+
{
|
| 73 |
+
FindNext();
|
| 74 |
+
}
|
| 75 |
+
e.Handled = true;
|
| 76 |
+
break;
|
| 77 |
+
case Key.Escape:
|
| 78 |
+
CloseRequested?.Invoke(this, EventArgs.Empty);
|
| 79 |
+
e.Handled = true;
|
| 80 |
+
break;
|
| 81 |
+
}
|
| 82 |
+
}
|
| 83 |
+
|
| 84 |
+
private void PreviousButton_Click(object sender, RoutedEventArgs e)
|
| 85 |
+
{
|
| 86 |
+
FindPrevious();
|
| 87 |
+
}
|
| 88 |
+
|
| 89 |
+
private void NextButton_Click(object sender, RoutedEventArgs e)
|
| 90 |
+
{
|
| 91 |
+
FindNext();
|
| 92 |
+
}
|
| 93 |
+
|
| 94 |
+
private void MatchCaseButton_Click(object sender, RoutedEventArgs e)
|
| 95 |
+
{
|
| 96 |
+
IsMatchCase = !IsMatchCase;
|
| 97 |
+
|
| 98 |
+
// 重新搜索
|
| 99 |
+
var searchText = SearchTextBox.Text;
|
| 100 |
+
if (!string.IsNullOrEmpty(searchText))
|
| 101 |
+
{
|
| 102 |
+
StartFind(searchText);
|
| 103 |
+
}
|
| 104 |
+
}
|
| 105 |
+
|
| 106 |
+
private void CloseButton_Click(object sender, RoutedEventArgs e)
|
| 107 |
+
{
|
| 108 |
+
CloseRequested?.Invoke(this, EventArgs.Empty);
|
| 109 |
+
}
|
| 110 |
+
|
| 111 |
+
private void StartFind(string searchText)
|
| 112 |
+
{
|
| 113 |
+
if (_browser == null || !_browser.IsBrowserInitialized)
|
| 114 |
+
return;
|
| 115 |
+
|
| 116 |
+
try
|
| 117 |
+
{
|
| 118 |
+
_browser.Find(searchText, forward: true, matchCase: IsMatchCase, findNext: false);
|
| 119 |
+
|
| 120 |
+
// 简单的UI更新,显示搜索中状态
|
| 121 |
+
ResultCountText.Text = "搜索中...";
|
| 122 |
+
SearchTextBox.Background = System.Windows.Media.Brushes.DarkSlateGray;
|
| 123 |
+
|
| 124 |
+
// 启用按钮
|
| 125 |
+
PreviousButton.IsEnabled = true;
|
| 126 |
+
NextButton.IsEnabled = true;
|
| 127 |
+
}
|
| 128 |
+
catch (Exception ex)
|
| 129 |
+
{
|
| 130 |
+
ResultCountText.Text = "搜索失败";
|
| 131 |
+
SearchTextBox.Background = System.Windows.Media.Brushes.DarkRed;
|
| 132 |
+
}
|
| 133 |
+
}
|
| 134 |
+
|
| 135 |
+
private void FindNext()
|
| 136 |
+
{
|
| 137 |
+
var searchText = SearchTextBox.Text;
|
| 138 |
+
if (string.IsNullOrEmpty(searchText) || _browser == null || !_browser.IsBrowserInitialized)
|
| 139 |
+
return;
|
| 140 |
+
|
| 141 |
+
try
|
| 142 |
+
{
|
| 143 |
+
_browser.Find(searchText, forward: true, matchCase: IsMatchCase, findNext: true);
|
| 144 |
+
}
|
| 145 |
+
catch (Exception ex)
|
| 146 |
+
{
|
| 147 |
+
// 忽略错误
|
| 148 |
+
}
|
| 149 |
+
}
|
| 150 |
+
|
| 151 |
+
private void FindPrevious()
|
| 152 |
+
{
|
| 153 |
+
var searchText = SearchTextBox.Text;
|
| 154 |
+
if (string.IsNullOrEmpty(searchText) || _browser == null || !_browser.IsBrowserInitialized)
|
| 155 |
+
return;
|
| 156 |
+
|
| 157 |
+
try
|
| 158 |
+
{
|
| 159 |
+
_browser.Find(searchText, forward: false, matchCase: IsMatchCase, findNext: true);
|
| 160 |
+
}
|
| 161 |
+
catch (Exception ex)
|
| 162 |
+
{
|
| 163 |
+
// 忽略错误
|
| 164 |
+
}
|
| 165 |
+
}
|
| 166 |
+
|
| 167 |
+
private void StopFind()
|
| 168 |
+
{
|
| 169 |
+
if (_browser == null || !_browser.IsBrowserInitialized)
|
| 170 |
+
return;
|
| 171 |
+
|
| 172 |
+
try
|
| 173 |
+
{
|
| 174 |
+
_browser.StopFinding(clearSelection: true);
|
| 175 |
+
}
|
| 176 |
+
catch (Exception ex)
|
| 177 |
+
{
|
| 178 |
+
// 忽略错误
|
| 179 |
+
}
|
| 180 |
+
|
| 181 |
+
_currentMatchIndex = 0;
|
| 182 |
+
_totalMatches = 0;
|
| 183 |
+
UpdateUI();
|
| 184 |
+
}
|
| 185 |
+
|
| 186 |
+
private void UpdateUI()
|
| 187 |
+
{
|
| 188 |
+
if (_totalMatches > 0)
|
| 189 |
+
{
|
| 190 |
+
ResultCountText.Text = $"{_currentMatchIndex}/{_totalMatches}";
|
| 191 |
+
SearchTextBox.Background = System.Windows.Media.Brushes.DarkSlateGray;
|
| 192 |
+
}
|
| 193 |
+
else if (!string.IsNullOrEmpty(SearchTextBox.Text))
|
| 194 |
+
{
|
| 195 |
+
ResultCountText.Text = "0/0";
|
| 196 |
+
SearchTextBox.Background = System.Windows.Media.Brushes.DarkRed;
|
| 197 |
+
}
|
| 198 |
+
else
|
| 199 |
+
{
|
| 200 |
+
ResultCountText.Text = "0/0";
|
| 201 |
+
SearchTextBox.Background = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Color.FromRgb(30, 30, 30));
|
| 202 |
+
}
|
| 203 |
+
|
| 204 |
+
PreviousButton.IsEnabled = !string.IsNullOrEmpty(SearchTextBox.Text);
|
| 205 |
+
NextButton.IsEnabled = !string.IsNullOrEmpty(SearchTextBox.Text);
|
| 206 |
+
}
|
| 207 |
+
|
| 208 |
+
private void UpdateMatchCaseButtonStyle()
|
| 209 |
+
{
|
| 210 |
+
// 触发数据绑定更新
|
| 211 |
+
var binding = MatchCaseButton.GetBindingExpression(Button.StyleProperty);
|
| 212 |
+
binding?.UpdateTarget();
|
| 213 |
+
}
|
| 214 |
+
|
| 215 |
+
public void Clear()
|
| 216 |
+
{
|
| 217 |
+
SearchTextBox.Clear();
|
| 218 |
+
StopFind();
|
| 219 |
+
}
|
| 220 |
+
}
|
| 221 |
+
}
|
csharp/BrowserTool/BrowserTool/Converters/BooleanToVisibilityConverter.cs
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
using System;
|
| 2 |
+
using System.Globalization;
|
| 3 |
+
using System.Windows;
|
| 4 |
+
using System.Windows.Data;
|
| 5 |
+
|
| 6 |
+
namespace BrowserTool.Converters
|
| 7 |
+
{
|
| 8 |
+
public class BooleanToVisibilityConverter : IValueConverter
|
| 9 |
+
{
|
| 10 |
+
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
|
| 11 |
+
{
|
| 12 |
+
if (value is bool boolValue)
|
| 13 |
+
{
|
| 14 |
+
return boolValue ? Visibility.Visible : Visibility.Collapsed;
|
| 15 |
+
}
|
| 16 |
+
return Visibility.Collapsed;
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
|
| 20 |
+
{
|
| 21 |
+
if (value is Visibility visibility)
|
| 22 |
+
{
|
| 23 |
+
return visibility == Visibility.Visible;
|
| 24 |
+
}
|
| 25 |
+
return false;
|
| 26 |
+
}
|
| 27 |
+
}
|
| 28 |
+
}
|
csharp/BrowserTool/BrowserTool/Database/AppDbContext.cs
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
using Microsoft.EntityFrameworkCore;
|
| 2 |
+
using System;
|
| 3 |
+
|
| 4 |
+
namespace BrowserTool.Database
|
| 5 |
+
{
|
| 6 |
+
/// <summary>
|
| 7 |
+
/// 应用程序数据库上下文
|
| 8 |
+
/// </summary>
|
| 9 |
+
public class AppDbContext : DbContext
|
| 10 |
+
{
|
| 11 |
+
/// <summary>
|
| 12 |
+
/// 站点组集合
|
| 13 |
+
/// </summary>
|
| 14 |
+
public DbSet<Entities.SiteGroup> SiteGroups { get; set; }
|
| 15 |
+
|
| 16 |
+
/// <summary>
|
| 17 |
+
/// 站点项集合
|
| 18 |
+
/// </summary>
|
| 19 |
+
public DbSet<Entities.SiteItem> SiteItems { get; set; }
|
| 20 |
+
|
| 21 |
+
/// <summary>
|
| 22 |
+
/// 配置数据库连接
|
| 23 |
+
/// </summary>
|
| 24 |
+
/// <param name="optionsBuilder">数据库上下文选项构建器</param>
|
| 25 |
+
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
| 26 |
+
{
|
| 27 |
+
try
|
| 28 |
+
{
|
| 29 |
+
var dbPath = DatabaseInitializer.GetDbPath();
|
| 30 |
+
optionsBuilder.UseSqlite($"Data Source={dbPath}");
|
| 31 |
+
}
|
| 32 |
+
catch (Exception ex)
|
| 33 |
+
{
|
| 34 |
+
throw new Exception($"配置数据库连接时出错:{ex.Message}", ex);
|
| 35 |
+
}
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
/// <summary>
|
| 39 |
+
/// 配置实体模型
|
| 40 |
+
/// </summary>
|
| 41 |
+
/// <param name="modelBuilder">模型构建器</param>
|
| 42 |
+
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
| 43 |
+
{
|
| 44 |
+
try
|
| 45 |
+
{
|
| 46 |
+
// 配置 SiteGroup 实体
|
| 47 |
+
modelBuilder.Entity<Entities.SiteGroup>(entity =>
|
| 48 |
+
{
|
| 49 |
+
entity.HasKey(e => e.Id);
|
| 50 |
+
entity.Property(e => e.Name).IsRequired().HasMaxLength(100);
|
| 51 |
+
entity.Property(e => e.SortOrder).HasDefaultValue(0);
|
| 52 |
+
entity.Property(e => e.IsEnabled).HasDefaultValue(true);
|
| 53 |
+
entity.Property(e => e.IsDefaultExpanded).HasDefaultValue(false);
|
| 54 |
+
entity.Property(e => e.CreateTime).HasDefaultValueSql("CURRENT_TIMESTAMP");
|
| 55 |
+
entity.Property(e => e.UpdateTime).HasDefaultValueSql("CURRENT_TIMESTAMP");
|
| 56 |
+
});
|
| 57 |
+
|
| 58 |
+
// 配置 SiteItem 实体
|
| 59 |
+
modelBuilder.Entity<Entities.SiteItem>(entity =>
|
| 60 |
+
{
|
| 61 |
+
entity.HasKey(e => e.Id);
|
| 62 |
+
entity.Property(e => e.DisplayName).IsRequired().HasMaxLength(200);
|
| 63 |
+
entity.Property(e => e.Url).IsRequired().HasMaxLength(500);
|
| 64 |
+
entity.Property(e => e.SortOrder).HasDefaultValue(0);
|
| 65 |
+
entity.Property(e => e.Username).HasMaxLength(200);
|
| 66 |
+
entity.Property(e => e.Password).HasMaxLength(200);
|
| 67 |
+
entity.Property(e => e.CommonUsername).HasMaxLength(200);
|
| 68 |
+
entity.Property(e => e.CommonPassword).HasMaxLength(200);
|
| 69 |
+
entity.Property(e => e.UseCommonCredentials).HasDefaultValue(false);
|
| 70 |
+
entity.Property(e => e.AutoLogin).HasDefaultValue(false);
|
| 71 |
+
entity.Property(e => e.IsEnabled).HasDefaultValue(true);
|
| 72 |
+
entity.Property(e => e.Icon).HasMaxLength(1000);
|
| 73 |
+
entity.Property(e => e.Description).HasMaxLength(1000);
|
| 74 |
+
entity.Property(e => e.Tags).HasMaxLength(500);
|
| 75 |
+
entity.Property(e => e.CreateTime).HasDefaultValueSql("CURRENT_TIMESTAMP");
|
| 76 |
+
entity.Property(e => e.UpdateTime).HasDefaultValueSql("CURRENT_TIMESTAMP");
|
| 77 |
+
entity.Property(e => e.AccessCount).HasDefaultValue(0);
|
| 78 |
+
entity.Property(e => e.UsernameSelector).HasMaxLength(200);
|
| 79 |
+
entity.Property(e => e.PasswordSelector).HasMaxLength(200);
|
| 80 |
+
entity.Property(e => e.CaptchaSelector).HasMaxLength(200);
|
| 81 |
+
entity.Property(e => e.LoginButtonSelector).HasMaxLength(200);
|
| 82 |
+
entity.Property(e => e.LoginPageFeature).HasMaxLength(500);
|
| 83 |
+
entity.Property(e => e.CaptchaValue).HasMaxLength(200);
|
| 84 |
+
entity.Property(e => e.CaptchaMode).HasDefaultValue(0);
|
| 85 |
+
entity.Property(e => e.GoogleSecret).HasMaxLength(200);
|
| 86 |
+
|
| 87 |
+
// 配置外键关系
|
| 88 |
+
entity.HasOne(e => e.Group)
|
| 89 |
+
.WithMany(g => g.Sites)
|
| 90 |
+
.HasForeignKey(e => e.GroupId)
|
| 91 |
+
.OnDelete(DeleteBehavior.Cascade);
|
| 92 |
+
});
|
| 93 |
+
}
|
| 94 |
+
catch (Exception ex)
|
| 95 |
+
{
|
| 96 |
+
throw new Exception($"配置实体模型时出错:{ex.Message}", ex);
|
| 97 |
+
}
|
| 98 |
+
}
|
| 99 |
+
}
|
| 100 |
+
}
|
csharp/BrowserTool/BrowserTool/Database/ApplicationDbContext.cs
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
using BrowserTool.Database.Entities;
|
| 2 |
+
using Microsoft.EntityFrameworkCore;
|
| 3 |
+
using System;
|
| 4 |
+
using System.IO;
|
| 5 |
+
|
| 6 |
+
namespace BrowserTool.Database
|
| 7 |
+
{
|
| 8 |
+
public class ApplicationDbContext : DbContext
|
| 9 |
+
{
|
| 10 |
+
private static readonly string DbPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "sites.db");
|
| 11 |
+
|
| 12 |
+
public DbSet<SiteGroup> SiteGroups { get; set; }
|
| 13 |
+
public DbSet<SiteItem> SiteItems { get; set; }
|
| 14 |
+
|
| 15 |
+
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
| 16 |
+
{
|
| 17 |
+
optionsBuilder.UseSqlite($"Data Source={DbPath}");
|
| 18 |
+
}
|
| 19 |
+
|
| 20 |
+
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
| 21 |
+
{
|
| 22 |
+
base.OnModelCreating(modelBuilder);
|
| 23 |
+
|
| 24 |
+
// Configure SiteGroup
|
| 25 |
+
modelBuilder.Entity<SiteGroup>(entity =>
|
| 26 |
+
{
|
| 27 |
+
entity.HasKey(e => e.Id);
|
| 28 |
+
entity.Property(e => e.Name).IsRequired().HasMaxLength(100);
|
| 29 |
+
entity.Property(e => e.CreateTime).HasDefaultValueSql("CURRENT_TIMESTAMP");
|
| 30 |
+
entity.Property(e => e.UpdateTime).HasDefaultValueSql("CURRENT_TIMESTAMP");
|
| 31 |
+
entity.Property(e => e.IsEnabled).HasDefaultValue(true);
|
| 32 |
+
entity.Property(e => e.SortOrder).HasDefaultValue(0);
|
| 33 |
+
});
|
| 34 |
+
|
| 35 |
+
// Configure SiteItem
|
| 36 |
+
modelBuilder.Entity<SiteItem>(entity =>
|
| 37 |
+
{
|
| 38 |
+
entity.HasKey(e => e.Id);
|
| 39 |
+
entity.Property(e => e.DisplayName).IsRequired().HasMaxLength(200);
|
| 40 |
+
entity.Property(e => e.Url).IsRequired().HasMaxLength(500);
|
| 41 |
+
entity.Property(e => e.CreateTime).HasDefaultValueSql("CURRENT_TIMESTAMP");
|
| 42 |
+
entity.Property(e => e.UpdateTime).HasDefaultValueSql("CURRENT_TIMESTAMP");
|
| 43 |
+
entity.Property(e => e.IsEnabled).HasDefaultValue(true);
|
| 44 |
+
entity.Property(e => e.SortOrder).HasDefaultValue(0);
|
| 45 |
+
entity.Property(e => e.UseCommonCredentials).HasDefaultValue(false);
|
| 46 |
+
entity.Property(e => e.AutoLogin).HasDefaultValue(false);
|
| 47 |
+
entity.Property(e => e.AccessCount).HasDefaultValue(0);
|
| 48 |
+
entity.Property(e => e.CaptchaMode).HasDefaultValue(0);
|
| 49 |
+
|
| 50 |
+
// Configure relationship
|
| 51 |
+
entity.HasOne(e => e.Group)
|
| 52 |
+
.WithMany(g => g.Sites)
|
| 53 |
+
.HasForeignKey(e => e.GroupId)
|
| 54 |
+
.OnDelete(DeleteBehavior.Cascade);
|
| 55 |
+
});
|
| 56 |
+
}
|
| 57 |
+
}
|
| 58 |
+
}
|
csharp/BrowserTool/BrowserTool/Database/DatabaseInitializer.cs
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
using System;
|
| 2 |
+
using System.IO;
|
| 3 |
+
using System.Linq;
|
| 4 |
+
using Microsoft.EntityFrameworkCore;
|
| 5 |
+
|
| 6 |
+
namespace BrowserTool.Database
|
| 7 |
+
{
|
| 8 |
+
/// <summary>
|
| 9 |
+
/// 数据库初始化器
|
| 10 |
+
/// </summary>
|
| 11 |
+
public static class DatabaseInitializer
|
| 12 |
+
{
|
| 13 |
+
/// <summary>
|
| 14 |
+
/// 初始化数据库
|
| 15 |
+
/// </summary>
|
| 16 |
+
public static void Initialize()
|
| 17 |
+
{
|
| 18 |
+
// 确保数据库目录存在
|
| 19 |
+
var dbPath = GetDbPath();
|
| 20 |
+
var dbDirectory = Path.GetDirectoryName(dbPath);
|
| 21 |
+
if (!Directory.Exists(dbDirectory))
|
| 22 |
+
{
|
| 23 |
+
Directory.CreateDirectory(dbDirectory);
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
using (var context = new AppDbContext())
|
| 27 |
+
{
|
| 28 |
+
try
|
| 29 |
+
{
|
| 30 |
+
// 确保数据库存在
|
| 31 |
+
context.Database.EnsureCreated();
|
| 32 |
+
|
| 33 |
+
// 检查是否需要迁移
|
| 34 |
+
var pendingMigrations = context.Database.GetPendingMigrations().ToList();
|
| 35 |
+
if (pendingMigrations.Any())
|
| 36 |
+
{
|
| 37 |
+
context.Database.Migrate();
|
| 38 |
+
}
|
| 39 |
+
}
|
| 40 |
+
catch (Exception ex)
|
| 41 |
+
{
|
| 42 |
+
throw new Exception($"初始化数据库时出错:{ex.Message}", ex);
|
| 43 |
+
}
|
| 44 |
+
}
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
/// <summary>
|
| 48 |
+
/// 获取数据库文件路径
|
| 49 |
+
/// </summary>
|
| 50 |
+
/// <returns>数据库文件的完整路径</returns>
|
| 51 |
+
public static string GetDbPath()
|
| 52 |
+
{
|
| 53 |
+
try
|
| 54 |
+
{
|
| 55 |
+
// 获取应用程序目录
|
| 56 |
+
var appPath = AppDomain.CurrentDomain.BaseDirectory;
|
| 57 |
+
var dbPath = Path.Combine(appPath, "sites.db");
|
| 58 |
+
return dbPath;
|
| 59 |
+
}
|
| 60 |
+
catch (Exception ex)
|
| 61 |
+
{
|
| 62 |
+
throw new Exception($"获取数据库路径时出错:{ex.Message}", ex);
|
| 63 |
+
}
|
| 64 |
+
}
|
| 65 |
+
}
|
| 66 |
+
}
|
csharp/BrowserTool/BrowserTool/Database/Entities/SiteGroup.cs
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
using System;
|
| 2 |
+
using System.Collections.Generic;
|
| 3 |
+
using System.ComponentModel.DataAnnotations;
|
| 4 |
+
using System.ComponentModel.DataAnnotations.Schema;
|
| 5 |
+
|
| 6 |
+
namespace BrowserTool.Database.Entities
|
| 7 |
+
{
|
| 8 |
+
[Table("SiteGroups")]
|
| 9 |
+
public class SiteGroup
|
| 10 |
+
{
|
| 11 |
+
[Key]
|
| 12 |
+
public int Id { get; set; }
|
| 13 |
+
|
| 14 |
+
[Required]
|
| 15 |
+
[MaxLength(100)]
|
| 16 |
+
public string Name { get; set; }
|
| 17 |
+
|
| 18 |
+
public int SortOrder { get; set; }
|
| 19 |
+
|
| 20 |
+
public bool IsEnabled { get; set; }
|
| 21 |
+
|
| 22 |
+
/// <summary>
|
| 23 |
+
/// 是否默认展开(用于控制一级菜单的默认展开状态)
|
| 24 |
+
/// </summary>
|
| 25 |
+
public bool IsDefaultExpanded { get; set; } = false;
|
| 26 |
+
|
| 27 |
+
public DateTime CreateTime { get; set; }
|
| 28 |
+
|
| 29 |
+
public DateTime UpdateTime { get; set; }
|
| 30 |
+
|
| 31 |
+
// Navigation property
|
| 32 |
+
public virtual ICollection<SiteItem> Sites { get; set; } = new List<SiteItem>();
|
| 33 |
+
}
|
| 34 |
+
}
|
csharp/BrowserTool/BrowserTool/Database/Entities/SiteItem.cs
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
using System;
|
| 2 |
+
using System.ComponentModel.DataAnnotations;
|
| 3 |
+
using System.ComponentModel.DataAnnotations.Schema;
|
| 4 |
+
|
| 5 |
+
namespace BrowserTool.Database.Entities
|
| 6 |
+
{
|
| 7 |
+
[Table("SiteItems")]
|
| 8 |
+
public class SiteItem
|
| 9 |
+
{
|
| 10 |
+
[Key]
|
| 11 |
+
public int Id { get; set; }
|
| 12 |
+
|
| 13 |
+
[Required]
|
| 14 |
+
public int GroupId { get; set; }
|
| 15 |
+
|
| 16 |
+
[Required]
|
| 17 |
+
[MaxLength(200)]
|
| 18 |
+
public string DisplayName { get; set; }
|
| 19 |
+
|
| 20 |
+
[Required]
|
| 21 |
+
[MaxLength(500)]
|
| 22 |
+
public string Url { get; set; }
|
| 23 |
+
|
| 24 |
+
public int SortOrder { get; set; }
|
| 25 |
+
|
| 26 |
+
[MaxLength(200)]
|
| 27 |
+
public string Username { get; set; }
|
| 28 |
+
|
| 29 |
+
[MaxLength(200)]
|
| 30 |
+
public string Password { get; set; }
|
| 31 |
+
|
| 32 |
+
[MaxLength(200)]
|
| 33 |
+
public string CommonUsername { get; set; }
|
| 34 |
+
|
| 35 |
+
[MaxLength(200)]
|
| 36 |
+
public string CommonPassword { get; set; }
|
| 37 |
+
|
| 38 |
+
public bool UseCommonCredentials { get; set; }
|
| 39 |
+
|
| 40 |
+
public bool AutoLogin { get; set; }
|
| 41 |
+
|
| 42 |
+
public bool IsEnabled { get; set; }
|
| 43 |
+
|
| 44 |
+
[MaxLength(1000)]
|
| 45 |
+
public string Icon { get; set; }
|
| 46 |
+
|
| 47 |
+
[MaxLength(1000)]
|
| 48 |
+
public string Description { get; set; }
|
| 49 |
+
|
| 50 |
+
[MaxLength(500)]
|
| 51 |
+
public string Tags { get; set; }
|
| 52 |
+
|
| 53 |
+
public DateTime CreateTime { get; set; }
|
| 54 |
+
|
| 55 |
+
public DateTime UpdateTime { get; set; }
|
| 56 |
+
|
| 57 |
+
public DateTime? LastAccessTime { get; set; }
|
| 58 |
+
|
| 59 |
+
public int AccessCount { get; set; }
|
| 60 |
+
|
| 61 |
+
[MaxLength(200)]
|
| 62 |
+
public string UsernameSelector { get; set; }
|
| 63 |
+
|
| 64 |
+
[MaxLength(200)]
|
| 65 |
+
public string PasswordSelector { get; set; }
|
| 66 |
+
|
| 67 |
+
[MaxLength(200)]
|
| 68 |
+
public string CaptchaSelector { get; set; }
|
| 69 |
+
|
| 70 |
+
[MaxLength(200)]
|
| 71 |
+
public string LoginButtonSelector { get; set; }
|
| 72 |
+
|
| 73 |
+
[MaxLength(500)]
|
| 74 |
+
public string LoginPageFeature { get; set; }
|
| 75 |
+
|
| 76 |
+
[MaxLength(200)]
|
| 77 |
+
public string CaptchaValue { get; set; }
|
| 78 |
+
|
| 79 |
+
public int CaptchaMode { get; set; }
|
| 80 |
+
|
| 81 |
+
[MaxLength(200)]
|
| 82 |
+
public string GoogleSecret { get; set; }
|
| 83 |
+
|
| 84 |
+
// Navigation property
|
| 85 |
+
[ForeignKey("GroupId")]
|
| 86 |
+
public virtual SiteGroup Group { get; set; }
|
| 87 |
+
}
|
| 88 |
+
}
|
csharp/BrowserTool/BrowserTool/Database/Migrations/AddIsDefaultExpandedToSiteGroup.cs
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
using Microsoft.EntityFrameworkCore.Migrations;
|
| 2 |
+
|
| 3 |
+
namespace BrowserTool.Database.Migrations
|
| 4 |
+
{
|
| 5 |
+
public partial class AddIsDefaultExpandedToSiteGroup : Migration
|
| 6 |
+
{
|
| 7 |
+
protected override void Up(MigrationBuilder migrationBuilder)
|
| 8 |
+
{
|
| 9 |
+
migrationBuilder.AddColumn<bool>(
|
| 10 |
+
name: "IsDefaultExpanded",
|
| 11 |
+
table: "SiteGroups",
|
| 12 |
+
type: "INTEGER",
|
| 13 |
+
nullable: false,
|
| 14 |
+
defaultValue: false);
|
| 15 |
+
}
|
| 16 |
+
|
| 17 |
+
protected override void Down(MigrationBuilder migrationBuilder)
|
| 18 |
+
{
|
| 19 |
+
migrationBuilder.DropColumn(
|
| 20 |
+
name: "IsDefaultExpanded",
|
| 21 |
+
table: "SiteGroups");
|
| 22 |
+
}
|
| 23 |
+
}
|
| 24 |
+
}
|
csharp/BrowserTool/BrowserTool/Database/Migrations/InitialCreate.cs
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
using System;
|
| 2 |
+
using Microsoft.EntityFrameworkCore.Migrations;
|
| 3 |
+
|
| 4 |
+
namespace BrowserTool.Database.Migrations
|
| 5 |
+
{
|
| 6 |
+
public partial class InitialCreate : Migration
|
| 7 |
+
{
|
| 8 |
+
/// <summary>
|
| 9 |
+
/// 创建数据库表结构
|
| 10 |
+
/// </summary>
|
| 11 |
+
/// <param name="migrationBuilder">迁移构建器</param>
|
| 12 |
+
protected override void Up(MigrationBuilder migrationBuilder)
|
| 13 |
+
{
|
| 14 |
+
migrationBuilder.CreateTable(
|
| 15 |
+
name: "SiteGroups",
|
| 16 |
+
columns: table => new
|
| 17 |
+
{
|
| 18 |
+
Id = table.Column<int>(type: "INTEGER", nullable: false)
|
| 19 |
+
.Annotation("Sqlite:Autoincrement", true),
|
| 20 |
+
Name = table.Column<string>(type: "TEXT", maxLength: 100, nullable: false),
|
| 21 |
+
SortOrder = table.Column<int>(type: "INTEGER", nullable: false, defaultValue: 0),
|
| 22 |
+
IsEnabled = table.Column<bool>(type: "INTEGER", nullable: false, defaultValue: true),
|
| 23 |
+
CreateTime = table.Column<DateTime>(type: "TEXT", nullable: false, defaultValueSql: "CURRENT_TIMESTAMP"),
|
| 24 |
+
UpdateTime = table.Column<DateTime>(type: "TEXT", nullable: false, defaultValueSql: "CURRENT_TIMESTAMP")
|
| 25 |
+
},
|
| 26 |
+
constraints: table =>
|
| 27 |
+
{
|
| 28 |
+
table.PrimaryKey("PK_SiteGroups", x => x.Id);
|
| 29 |
+
});
|
| 30 |
+
|
| 31 |
+
migrationBuilder.CreateTable(
|
| 32 |
+
name: "SiteItems",
|
| 33 |
+
columns: table => new
|
| 34 |
+
{
|
| 35 |
+
Id = table.Column<int>(type: "INTEGER", nullable: false)
|
| 36 |
+
.Annotation("Sqlite:Autoincrement", true),
|
| 37 |
+
GroupId = table.Column<int>(type: "INTEGER", nullable: false),
|
| 38 |
+
DisplayName = table.Column<string>(type: "TEXT", maxLength: 200, nullable: false),
|
| 39 |
+
Url = table.Column<string>(type: "TEXT", maxLength: 500, nullable: false),
|
| 40 |
+
SortOrder = table.Column<int>(type: "INTEGER", nullable: false, defaultValue: 0),
|
| 41 |
+
Username = table.Column<string>(type: "TEXT", maxLength: 200, nullable: true),
|
| 42 |
+
Password = table.Column<string>(type: "TEXT", maxLength: 200, nullable: true),
|
| 43 |
+
CommonUsername = table.Column<string>(type: "TEXT", maxLength: 200, nullable: true),
|
| 44 |
+
CommonPassword = table.Column<string>(type: "TEXT", maxLength: 200, nullable: true),
|
| 45 |
+
UseCommonCredentials = table.Column<bool>(type: "INTEGER", nullable: false, defaultValue: false),
|
| 46 |
+
AutoLogin = table.Column<bool>(type: "INTEGER", nullable: false, defaultValue: false),
|
| 47 |
+
IsEnabled = table.Column<bool>(type: "INTEGER", nullable: false, defaultValue: true),
|
| 48 |
+
Icon = table.Column<string>(type: "TEXT", maxLength: 1000, nullable: true),
|
| 49 |
+
Description = table.Column<string>(type: "TEXT", maxLength: 1000, nullable: true),
|
| 50 |
+
Tags = table.Column<string>(type: "TEXT", maxLength: 500, nullable: true),
|
| 51 |
+
CreateTime = table.Column<DateTime>(type: "TEXT", nullable: false, defaultValueSql: "CURRENT_TIMESTAMP"),
|
| 52 |
+
UpdateTime = table.Column<DateTime>(type: "TEXT", nullable: false, defaultValueSql: "CURRENT_TIMESTAMP"),
|
| 53 |
+
LastAccessTime = table.Column<DateTime>(type: "TEXT", nullable: true),
|
| 54 |
+
AccessCount = table.Column<int>(type: "INTEGER", nullable: false, defaultValue: 0),
|
| 55 |
+
UsernameSelector = table.Column<string>(type: "TEXT", maxLength: 200, nullable: true),
|
| 56 |
+
PasswordSelector = table.Column<string>(type: "TEXT", maxLength: 200, nullable: true),
|
| 57 |
+
CaptchaSelector = table.Column<string>(type: "TEXT", maxLength: 200, nullable: true),
|
| 58 |
+
LoginButtonSelector = table.Column<string>(type: "TEXT", maxLength: 200, nullable: true),
|
| 59 |
+
LoginPageFeature = table.Column<string>(type: "TEXT", maxLength: 500, nullable: true),
|
| 60 |
+
CaptchaValue = table.Column<string>(type: "TEXT", maxLength: 200, nullable: true),
|
| 61 |
+
CaptchaMode = table.Column<int>(type: "INTEGER", nullable: false, defaultValue: 0),
|
| 62 |
+
GoogleSecret = table.Column<string>(type: "TEXT", maxLength: 200, nullable: true)
|
| 63 |
+
},
|
| 64 |
+
constraints: table =>
|
| 65 |
+
{
|
| 66 |
+
table.PrimaryKey("PK_SiteItems", x => x.Id);
|
| 67 |
+
table.ForeignKey(
|
| 68 |
+
name: "FK_SiteItems_SiteGroups_GroupId",
|
| 69 |
+
column: x => x.GroupId,
|
| 70 |
+
principalTable: "SiteGroups",
|
| 71 |
+
principalColumn: "Id",
|
| 72 |
+
onDelete: ReferentialAction.Cascade);
|
| 73 |
+
});
|
| 74 |
+
|
| 75 |
+
migrationBuilder.CreateIndex(
|
| 76 |
+
name: "IX_SiteItems_GroupId",
|
| 77 |
+
table: "SiteItems",
|
| 78 |
+
column: "GroupId");
|
| 79 |
+
|
| 80 |
+
migrationBuilder.CreateIndex(
|
| 81 |
+
name: "IX_SiteItems_SortOrder",
|
| 82 |
+
table: "SiteItems",
|
| 83 |
+
column: "SortOrder");
|
| 84 |
+
|
| 85 |
+
migrationBuilder.CreateIndex(
|
| 86 |
+
name: "IX_SiteGroups_SortOrder",
|
| 87 |
+
table: "SiteGroups",
|
| 88 |
+
column: "SortOrder");
|
| 89 |
+
}
|
| 90 |
+
|
| 91 |
+
/// <summary>
|
| 92 |
+
/// 删除数据库表结构
|
| 93 |
+
/// </summary>
|
| 94 |
+
/// <param name="migrationBuilder">迁移构建器</param>
|
| 95 |
+
protected override void Down(MigrationBuilder migrationBuilder)
|
| 96 |
+
{
|
| 97 |
+
migrationBuilder.DropTable(
|
| 98 |
+
name: "SiteItems");
|
| 99 |
+
|
| 100 |
+
migrationBuilder.DropTable(
|
| 101 |
+
name: "SiteGroups");
|
| 102 |
+
}
|
| 103 |
+
}
|
| 104 |
+
}
|
csharp/BrowserTool/BrowserTool/Database/SiteConfig.cs
ADDED
|
@@ -0,0 +1,553 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
using BrowserTool.Database.Entities;
|
| 2 |
+
using BrowserTool.Utils;
|
| 3 |
+
using Microsoft.EntityFrameworkCore;
|
| 4 |
+
using System;
|
| 5 |
+
using System.Collections.Generic;
|
| 6 |
+
using System.Linq;
|
| 7 |
+
|
| 8 |
+
namespace BrowserTool.Database
|
| 9 |
+
{
|
| 10 |
+
public static class SiteConfig
|
| 11 |
+
{
|
| 12 |
+
/// <summary>
|
| 13 |
+
/// 添加数据变更事件
|
| 14 |
+
/// </summary>
|
| 15 |
+
public static event EventHandler DataChanged;
|
| 16 |
+
|
| 17 |
+
/// <summary>
|
| 18 |
+
/// 触发数据变更事件的方法
|
| 19 |
+
/// </summary>
|
| 20 |
+
private static void OnDataChanged()
|
| 21 |
+
{
|
| 22 |
+
DataChanged?.Invoke(null, EventArgs.Empty);
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
/// <summary>
|
| 26 |
+
/// 解密网站敏感信息
|
| 27 |
+
/// </summary>
|
| 28 |
+
/// <param name="site">要解密的网站对象</param>
|
| 29 |
+
private static void DecryptSiteInfo(SiteItem site)
|
| 30 |
+
{
|
| 31 |
+
if (site == null) return;
|
| 32 |
+
|
| 33 |
+
try
|
| 34 |
+
{
|
| 35 |
+
// 检查DisplayName和Url是否已经是明文
|
| 36 |
+
bool isDisplayNameEncrypted = IsEncryptedString(site.DisplayName);
|
| 37 |
+
bool isUrlEncrypted = IsEncryptedString(site.Url);
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
if (!string.IsNullOrEmpty(site.Username))
|
| 41 |
+
{
|
| 42 |
+
try { site.Username = CryptoHelper.Decrypt(site.Username); }
|
| 43 |
+
catch (Exception ex) { System.Diagnostics.Debug.WriteLine($"解密Username失败: {ex}"); }
|
| 44 |
+
}
|
| 45 |
+
if (!string.IsNullOrEmpty(site.Password))
|
| 46 |
+
{
|
| 47 |
+
try { site.Password = CryptoHelper.Decrypt(site.Password); }
|
| 48 |
+
catch (Exception ex) { System.Diagnostics.Debug.WriteLine($"解密Password失败: {ex}"); }
|
| 49 |
+
}
|
| 50 |
+
if (!string.IsNullOrEmpty(site.CommonUsername))
|
| 51 |
+
{
|
| 52 |
+
try { site.CommonUsername = CryptoHelper.Decrypt(site.CommonUsername); }
|
| 53 |
+
catch (Exception ex) { System.Diagnostics.Debug.WriteLine($"解密CommonUsername失败: {ex}"); }
|
| 54 |
+
}
|
| 55 |
+
if (!string.IsNullOrEmpty(site.CommonPassword))
|
| 56 |
+
{
|
| 57 |
+
try { site.CommonPassword = CryptoHelper.Decrypt(site.CommonPassword); }
|
| 58 |
+
catch (Exception ex) { System.Diagnostics.Debug.WriteLine($"解密CommonPassword失败: {ex}"); }
|
| 59 |
+
}
|
| 60 |
+
if (!string.IsNullOrEmpty(site.GoogleSecret))
|
| 61 |
+
{
|
| 62 |
+
try { site.GoogleSecret = CryptoHelper.Decrypt(site.GoogleSecret); }
|
| 63 |
+
catch (Exception ex) { System.Diagnostics.Debug.WriteLine($"解密GoogleSecret失败: {ex}"); }
|
| 64 |
+
}
|
| 65 |
+
if (!string.IsNullOrEmpty(site.Url) && isUrlEncrypted)
|
| 66 |
+
{
|
| 67 |
+
try { site.Url = CryptoHelper.Decrypt(site.Url); }
|
| 68 |
+
catch (Exception ex) { System.Diagnostics.Debug.WriteLine($"解密Url失败: {ex}"); }
|
| 69 |
+
}
|
| 70 |
+
if (!string.IsNullOrEmpty(site.DisplayName) && isDisplayNameEncrypted)
|
| 71 |
+
{
|
| 72 |
+
try { site.DisplayName = CryptoHelper.Decrypt(site.DisplayName); }
|
| 73 |
+
catch (Exception ex) { System.Diagnostics.Debug.WriteLine($"解密DisplayName失败: {ex}"); }
|
| 74 |
+
}
|
| 75 |
+
}
|
| 76 |
+
catch (Exception ex)
|
| 77 |
+
{
|
| 78 |
+
System.Diagnostics.Debug.WriteLine($"解密站点信息时出错: {ex}");
|
| 79 |
+
}
|
| 80 |
+
}
|
| 81 |
+
|
| 82 |
+
private static bool IsEncryptedString(string str)
|
| 83 |
+
{
|
| 84 |
+
if (string.IsNullOrEmpty(str)) return false;
|
| 85 |
+
|
| 86 |
+
// 检查是否是有效的Base64字符串
|
| 87 |
+
if (!IsValidBase64String(str)) return false;
|
| 88 |
+
|
| 89 |
+
// 检查长度是否足够长(加密后的字符串通常较长)
|
| 90 |
+
if (str.Length < 16) return false;
|
| 91 |
+
|
| 92 |
+
return true;
|
| 93 |
+
}
|
| 94 |
+
|
| 95 |
+
private static bool IsValidBase64String(string str)
|
| 96 |
+
{
|
| 97 |
+
if (string.IsNullOrEmpty(str)) return false;
|
| 98 |
+
|
| 99 |
+
// 检查长度是否为4的倍数
|
| 100 |
+
if (str.Length % 4 != 0) return false;
|
| 101 |
+
|
| 102 |
+
// 检查是否只包含有效的Base64字符
|
| 103 |
+
foreach (char c in str)
|
| 104 |
+
{
|
| 105 |
+
if (!((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '+' || c == '/' || c == '='))
|
| 106 |
+
{
|
| 107 |
+
return false;
|
| 108 |
+
}
|
| 109 |
+
}
|
| 110 |
+
|
| 111 |
+
// 检查填充字符
|
| 112 |
+
int paddingCount = str.Count(c => c == '=');
|
| 113 |
+
if (paddingCount > 2) return false;
|
| 114 |
+
|
| 115 |
+
return true;
|
| 116 |
+
}
|
| 117 |
+
|
| 118 |
+
/// <summary>
|
| 119 |
+
/// 批量解密网站敏感信息
|
| 120 |
+
/// </summary>
|
| 121 |
+
/// <param name="sites">要解密的网站集合</param>
|
| 122 |
+
private static void DecryptSiteInfoBatch(ICollection<SiteItem> sites)
|
| 123 |
+
{
|
| 124 |
+
if (sites == null) return;
|
| 125 |
+
|
| 126 |
+
foreach (var site in sites)
|
| 127 |
+
{
|
| 128 |
+
DecryptSiteInfo(site);
|
| 129 |
+
}
|
| 130 |
+
}
|
| 131 |
+
|
| 132 |
+
/// <summary>
|
| 133 |
+
/// 获取所有启用的分���
|
| 134 |
+
/// </summary>
|
| 135 |
+
/// <returns>启用的分组列表</returns>
|
| 136 |
+
public static List<SiteGroup> GetGroups()
|
| 137 |
+
{
|
| 138 |
+
using (var context = new AppDbContext())
|
| 139 |
+
{
|
| 140 |
+
return context.SiteGroups
|
| 141 |
+
.Where(g => g.IsEnabled)
|
| 142 |
+
.OrderBy(g => g.SortOrder)
|
| 143 |
+
.ThenBy(g => g.Name)
|
| 144 |
+
.ToList();
|
| 145 |
+
}
|
| 146 |
+
}
|
| 147 |
+
|
| 148 |
+
/// <summary>
|
| 149 |
+
/// 根据分组ID获取该分组下的所有网站
|
| 150 |
+
/// </summary>
|
| 151 |
+
/// <param name="groupId">分组ID</param>
|
| 152 |
+
/// <returns>该分组下的网站列表</returns>
|
| 153 |
+
public static List<SiteItem> GetSitesByGroup(int groupId)
|
| 154 |
+
{
|
| 155 |
+
using (var context = new AppDbContext())
|
| 156 |
+
{
|
| 157 |
+
var sites = context.SiteItems
|
| 158 |
+
.Where(s => s.GroupId == groupId && s.IsEnabled)
|
| 159 |
+
.OrderBy(s => s.SortOrder)
|
| 160 |
+
.ThenBy(s => s.DisplayName)
|
| 161 |
+
.ToList();
|
| 162 |
+
|
| 163 |
+
// 解密敏感信息
|
| 164 |
+
DecryptSiteInfoBatch(sites);
|
| 165 |
+
|
| 166 |
+
return sites;
|
| 167 |
+
}
|
| 168 |
+
}
|
| 169 |
+
|
| 170 |
+
/// <summary>
|
| 171 |
+
/// 保存分组信息
|
| 172 |
+
/// </summary>
|
| 173 |
+
/// <param name="group">要保存的分组</param>
|
| 174 |
+
public static void SaveGroup(SiteGroup group)
|
| 175 |
+
{
|
| 176 |
+
using (var context = new AppDbContext())
|
| 177 |
+
{
|
| 178 |
+
if (group.Id == 0)
|
| 179 |
+
{
|
| 180 |
+
group.CreateTime = DateTime.Now;
|
| 181 |
+
group.UpdateTime = DateTime.Now;
|
| 182 |
+
context.SiteGroups.Add(group);
|
| 183 |
+
}
|
| 184 |
+
else
|
| 185 |
+
{
|
| 186 |
+
var existingGroup = context.SiteGroups.Find(group.Id);
|
| 187 |
+
if (existingGroup != null)
|
| 188 |
+
{
|
| 189 |
+
context.Entry(existingGroup).CurrentValues.SetValues(group);
|
| 190 |
+
existingGroup.UpdateTime = DateTime.Now;
|
| 191 |
+
}
|
| 192 |
+
}
|
| 193 |
+
context.SaveChanges();
|
| 194 |
+
OnDataChanged(); // 触发数据变更事件
|
| 195 |
+
}
|
| 196 |
+
}
|
| 197 |
+
|
| 198 |
+
/// <summary>
|
| 199 |
+
/// 保存网站信息
|
| 200 |
+
/// </summary>
|
| 201 |
+
/// <param name="site">要保存的网站</param>
|
| 202 |
+
public static void SaveSite(SiteItem site)
|
| 203 |
+
{
|
| 204 |
+
using (var context = new AppDbContext())
|
| 205 |
+
{
|
| 206 |
+
// 创建副本以避免修改原对象
|
| 207 |
+
var siteToSave = new SiteItem
|
| 208 |
+
{
|
| 209 |
+
Id = site.Id,
|
| 210 |
+
GroupId = site.GroupId,
|
| 211 |
+
DisplayName = site.DisplayName,
|
| 212 |
+
Username = site.Username,
|
| 213 |
+
Password = site.Password,
|
| 214 |
+
CommonUsername = site.CommonUsername,
|
| 215 |
+
CommonPassword = site.CommonPassword,
|
| 216 |
+
GoogleSecret = site.GoogleSecret,
|
| 217 |
+
Url = site.Url,
|
| 218 |
+
IsEnabled = site.IsEnabled,
|
| 219 |
+
SortOrder = site.SortOrder,
|
| 220 |
+
CreateTime = site.CreateTime,
|
| 221 |
+
UpdateTime = site.UpdateTime,
|
| 222 |
+
LastAccessTime = site.LastAccessTime,
|
| 223 |
+
Icon = site.Icon,
|
| 224 |
+
AccessCount = site.AccessCount
|
| 225 |
+
};
|
| 226 |
+
|
| 227 |
+
// 加密敏感信息
|
| 228 |
+
if (!string.IsNullOrEmpty(siteToSave.Username))
|
| 229 |
+
{
|
| 230 |
+
siteToSave.Username = CryptoHelper.Encrypt(siteToSave.Username);
|
| 231 |
+
}
|
| 232 |
+
if (!string.IsNullOrEmpty(siteToSave.Password))
|
| 233 |
+
{
|
| 234 |
+
siteToSave.Password = CryptoHelper.Encrypt(siteToSave.Password);
|
| 235 |
+
}
|
| 236 |
+
if (!string.IsNullOrEmpty(siteToSave.CommonUsername))
|
| 237 |
+
{
|
| 238 |
+
siteToSave.CommonUsername = CryptoHelper.Encrypt(siteToSave.CommonUsername);
|
| 239 |
+
}
|
| 240 |
+
if (!string.IsNullOrEmpty(siteToSave.CommonPassword))
|
| 241 |
+
{
|
| 242 |
+
siteToSave.CommonPassword = CryptoHelper.Encrypt(siteToSave.CommonPassword);
|
| 243 |
+
}
|
| 244 |
+
if (!string.IsNullOrEmpty(siteToSave.GoogleSecret))
|
| 245 |
+
{
|
| 246 |
+
siteToSave.GoogleSecret = CryptoHelper.Encrypt(siteToSave.GoogleSecret);
|
| 247 |
+
}
|
| 248 |
+
if (!string.IsNullOrEmpty(siteToSave.Url))
|
| 249 |
+
{
|
| 250 |
+
siteToSave.Url = CryptoHelper.Encrypt(siteToSave.Url);
|
| 251 |
+
}
|
| 252 |
+
if (!string.IsNullOrEmpty(siteToSave.DisplayName))
|
| 253 |
+
{
|
| 254 |
+
siteToSave.DisplayName = CryptoHelper.Encrypt(siteToSave.DisplayName);
|
| 255 |
+
}
|
| 256 |
+
|
| 257 |
+
if (siteToSave.Id == 0)
|
| 258 |
+
{
|
| 259 |
+
siteToSave.CreateTime = DateTime.Now;
|
| 260 |
+
siteToSave.UpdateTime = DateTime.Now;
|
| 261 |
+
context.SiteItems.Add(siteToSave);
|
| 262 |
+
}
|
| 263 |
+
else
|
| 264 |
+
{
|
| 265 |
+
var existingSite = context.SiteItems.Find(siteToSave.Id);
|
| 266 |
+
if (existingSite != null)
|
| 267 |
+
{
|
| 268 |
+
context.Entry(existingSite).CurrentValues.SetValues(siteToSave);
|
| 269 |
+
existingSite.UpdateTime = DateTime.Now;
|
| 270 |
+
}
|
| 271 |
+
}
|
| 272 |
+
context.SaveChanges();
|
| 273 |
+
OnDataChanged(); // 触发数据变更事件
|
| 274 |
+
}
|
| 275 |
+
}
|
| 276 |
+
|
| 277 |
+
/// <summary>
|
| 278 |
+
/// 删除指定ID的分组
|
| 279 |
+
/// </summary>
|
| 280 |
+
/// <param name="groupId">要删除的分组ID</param>
|
| 281 |
+
public static void DeleteGroup(int groupId)
|
| 282 |
+
{
|
| 283 |
+
using (var context = new AppDbContext())
|
| 284 |
+
{
|
| 285 |
+
var group = context.SiteGroups.Find(groupId);
|
| 286 |
+
if (group != null)
|
| 287 |
+
{
|
| 288 |
+
context.SiteGroups.Remove(group);
|
| 289 |
+
context.SaveChanges();
|
| 290 |
+
OnDataChanged(); // 触发数据变更事件
|
| 291 |
+
}
|
| 292 |
+
}
|
| 293 |
+
}
|
| 294 |
+
|
| 295 |
+
/// <summary>
|
| 296 |
+
/// 删除指定ID的网站
|
| 297 |
+
/// </summary>
|
| 298 |
+
/// <param name="siteId">要删除的网站ID</param>
|
| 299 |
+
public static void DeleteSite(int siteId)
|
| 300 |
+
{
|
| 301 |
+
using (var context = new AppDbContext())
|
| 302 |
+
{
|
| 303 |
+
var site = context.SiteItems.Find(siteId);
|
| 304 |
+
if (site != null)
|
| 305 |
+
{
|
| 306 |
+
context.SiteItems.Remove(site);
|
| 307 |
+
context.SaveChanges();
|
| 308 |
+
OnDataChanged(); // 触发数据变更事件
|
| 309 |
+
}
|
| 310 |
+
}
|
| 311 |
+
}
|
| 312 |
+
|
| 313 |
+
/// <summary>
|
| 314 |
+
/// 更新网站的访问信息
|
| 315 |
+
/// </summary>
|
| 316 |
+
/// <param name="siteId">要更新的网站ID</param>
|
| 317 |
+
public static void UpdateSiteAccess(int siteId)
|
| 318 |
+
{
|
| 319 |
+
using (var context = new AppDbContext())
|
| 320 |
+
{
|
| 321 |
+
var site = context.SiteItems.Find(siteId);
|
| 322 |
+
if (site != null)
|
| 323 |
+
{
|
| 324 |
+
site.LastAccessTime = DateTime.Now;
|
| 325 |
+
site.AccessCount++;
|
| 326 |
+
context.SaveChanges();
|
| 327 |
+
}
|
| 328 |
+
}
|
| 329 |
+
}
|
| 330 |
+
|
| 331 |
+
/// <summary>
|
| 332 |
+
/// 获取所有分组(包括未启用的)
|
| 333 |
+
/// </summary>
|
| 334 |
+
/// <returns>所有分组的列表</returns>
|
| 335 |
+
public static List<SiteGroup> GetAllGroups()
|
| 336 |
+
{
|
| 337 |
+
try
|
| 338 |
+
{
|
| 339 |
+
using (var context = new AppDbContext())
|
| 340 |
+
{
|
| 341 |
+
// 检查数据库连接
|
| 342 |
+
System.Diagnostics.Debug.WriteLine($"数据库路径: {DatabaseInitializer.GetDbPath()}");
|
| 343 |
+
|
| 344 |
+
// 检查是否有任何分组
|
| 345 |
+
var groupCount = context.SiteGroups.Count();
|
| 346 |
+
|
| 347 |
+
// 先获取所有分组,不使用Include
|
| 348 |
+
var groups = context.SiteGroups
|
| 349 |
+
.OrderBy(g => g.SortOrder)
|
| 350 |
+
.ThenBy(g => g.Name)
|
| 351 |
+
.ToList();
|
| 352 |
+
|
| 353 |
+
// 手动加载每个分组的站点并解密
|
| 354 |
+
foreach (var group in groups)
|
| 355 |
+
{
|
| 356 |
+
try
|
| 357 |
+
{
|
| 358 |
+
// 确保IsDefaultExpanded有有效值
|
| 359 |
+
if (group.IsDefaultExpanded != true && group.IsDefaultExpanded != false)
|
| 360 |
+
{
|
| 361 |
+
System.Diagnostics.Debug.WriteLine($"警告:分组 {group.Name} (ID: {group.Id}) 的IsDefaultExpanded值无效,设置为默认值false");
|
| 362 |
+
group.IsDefaultExpanded = false;
|
| 363 |
+
}
|
| 364 |
+
|
| 365 |
+
// 获取站点数据
|
| 366 |
+
var sites = context.SiteItems
|
| 367 |
+
.Where(s => s.GroupId == group.Id)
|
| 368 |
+
.OrderBy(s => s.SortOrder)
|
| 369 |
+
.ToList();
|
| 370 |
+
|
| 371 |
+
|
| 372 |
+
// 检查每个站点的数据
|
| 373 |
+
foreach (var site in sites)
|
| 374 |
+
{
|
| 375 |
+
try
|
| 376 |
+
{
|
| 377 |
+
|
| 378 |
+
// 确保所有必需字段都有有效值
|
| 379 |
+
if (string.IsNullOrEmpty(site.DisplayName))
|
| 380 |
+
{
|
| 381 |
+
System.Diagnostics.Debug.WriteLine($"警告:站点 ID={site.Id} 的DisplayName为空");
|
| 382 |
+
site.DisplayName = "未命名站点";
|
| 383 |
+
}
|
| 384 |
+
if (string.IsNullOrEmpty(site.Url))
|
| 385 |
+
{
|
| 386 |
+
System.Diagnostics.Debug.WriteLine($"警告:站点 ID={site.Id} 的Url为空");
|
| 387 |
+
site.Url = "about:blank";
|
| 388 |
+
}
|
| 389 |
+
}
|
| 390 |
+
catch (Exception ex)
|
| 391 |
+
{
|
| 392 |
+
System.Diagnostics.Debug.WriteLine($"处理站点 ID={site.Id} 时出错: {ex}");
|
| 393 |
+
}
|
| 394 |
+
}
|
| 395 |
+
|
| 396 |
+
group.Sites = sites;
|
| 397 |
+
|
| 398 |
+
// 解密站点敏感信息
|
| 399 |
+
if (group.Sites != null)
|
| 400 |
+
{
|
| 401 |
+
try
|
| 402 |
+
{
|
| 403 |
+
DecryptSiteInfoBatch(group.Sites);
|
| 404 |
+
}
|
| 405 |
+
catch (Exception ex)
|
| 406 |
+
{
|
| 407 |
+
System.Diagnostics.Debug.WriteLine($"解密分组 {group.Name} 的站点信息时出错: {ex}");
|
| 408 |
+
}
|
| 409 |
+
}
|
| 410 |
+
|
| 411 |
+
}
|
| 412 |
+
catch (Exception ex)
|
| 413 |
+
{
|
| 414 |
+
System.Diagnostics.Debug.WriteLine($"处理分组 {group.Name} (ID: {group.Id}) 时出错: {ex}");
|
| 415 |
+
throw;
|
| 416 |
+
}
|
| 417 |
+
}
|
| 418 |
+
|
| 419 |
+
return groups;
|
| 420 |
+
}
|
| 421 |
+
}
|
| 422 |
+
catch (Exception ex)
|
| 423 |
+
{
|
| 424 |
+
System.Diagnostics.Debug.WriteLine($"获取分组时出错: {ex}");
|
| 425 |
+
throw;
|
| 426 |
+
}
|
| 427 |
+
}
|
| 428 |
+
|
| 429 |
+
/// <summary>
|
| 430 |
+
/// 获取所有网站(包括未启用的)
|
| 431 |
+
/// </summary>
|
| 432 |
+
/// <returns>所有网站的列表</returns>
|
| 433 |
+
public static List<SiteItem> GetAllSites()
|
| 434 |
+
{
|
| 435 |
+
using (var context = new AppDbContext())
|
| 436 |
+
{
|
| 437 |
+
var sites = context.SiteItems
|
| 438 |
+
.OrderBy(s => s.SortOrder)
|
| 439 |
+
.ThenBy(s => s.DisplayName)
|
| 440 |
+
.ToList();
|
| 441 |
+
|
| 442 |
+
// 解密敏感信息
|
| 443 |
+
DecryptSiteInfoBatch(sites);
|
| 444 |
+
|
| 445 |
+
return sites;
|
| 446 |
+
}
|
| 447 |
+
}
|
| 448 |
+
|
| 449 |
+
/// <summary>
|
| 450 |
+
/// 初始化数据库
|
| 451 |
+
/// </summary>
|
| 452 |
+
public static void InitializeDatabase()
|
| 453 |
+
{
|
| 454 |
+
DatabaseInitializer.Initialize();
|
| 455 |
+
}
|
| 456 |
+
|
| 457 |
+
/// <summary>
|
| 458 |
+
/// 添加分组
|
| 459 |
+
/// </summary>
|
| 460 |
+
/// <param name="group">要添加的分组</param>
|
| 461 |
+
public static void AddGroup(SiteGroup group)
|
| 462 |
+
{
|
| 463 |
+
using (var context = new AppDbContext())
|
| 464 |
+
{
|
| 465 |
+
context.SiteGroups.Add(group);
|
| 466 |
+
context.SaveChanges();
|
| 467 |
+
OnDataChanged(); // 触发数据变更事件
|
| 468 |
+
}
|
| 469 |
+
}
|
| 470 |
+
|
| 471 |
+
/// <summary>
|
| 472 |
+
/// 更新分组
|
| 473 |
+
/// </summary>
|
| 474 |
+
/// <param name="group">要更新的分组</param>
|
| 475 |
+
public static void UpdateGroup(SiteGroup group)
|
| 476 |
+
{
|
| 477 |
+
using (var context = new AppDbContext())
|
| 478 |
+
{
|
| 479 |
+
context.SiteGroups.Update(group);
|
| 480 |
+
context.SaveChanges();
|
| 481 |
+
OnDataChanged(); // 触发数据变更事件
|
| 482 |
+
}
|
| 483 |
+
}
|
| 484 |
+
|
| 485 |
+
/// <summary>
|
| 486 |
+
/// 添加网站
|
| 487 |
+
/// </summary>
|
| 488 |
+
/// <param name="site">要添加的网站</param>
|
| 489 |
+
public static void AddSite(SiteItem site)
|
| 490 |
+
{
|
| 491 |
+
using (var context = new AppDbContext())
|
| 492 |
+
{
|
| 493 |
+
// 创建副本并加密敏感信息
|
| 494 |
+
var siteToAdd = new SiteItem
|
| 495 |
+
{
|
| 496 |
+
GroupId = site.GroupId,
|
| 497 |
+
DisplayName = !string.IsNullOrEmpty(site.DisplayName) ? CryptoHelper.Encrypt(site.DisplayName) : site.DisplayName,
|
| 498 |
+
Username = !string.IsNullOrEmpty(site.Username) ? CryptoHelper.Encrypt(site.Username) : site.Username,
|
| 499 |
+
Password = !string.IsNullOrEmpty(site.Password) ? CryptoHelper.Encrypt(site.Password) : site.Password,
|
| 500 |
+
CommonUsername = !string.IsNullOrEmpty(site.CommonUsername) ? CryptoHelper.Encrypt(site.CommonUsername) : site.CommonUsername,
|
| 501 |
+
CommonPassword = !string.IsNullOrEmpty(site.CommonPassword) ? CryptoHelper.Encrypt(site.CommonPassword) : site.CommonPassword,
|
| 502 |
+
GoogleSecret = !string.IsNullOrEmpty(site.GoogleSecret) ? CryptoHelper.Encrypt(site.GoogleSecret) : site.GoogleSecret,
|
| 503 |
+
Url = !string.IsNullOrEmpty(site.Url) ? CryptoHelper.Encrypt(site.Url) : site.Url,
|
| 504 |
+
IsEnabled = site.IsEnabled,
|
| 505 |
+
SortOrder = site.SortOrder,
|
| 506 |
+
CreateTime = DateTime.Now,
|
| 507 |
+
UpdateTime = DateTime.Now,
|
| 508 |
+
LastAccessTime = site.LastAccessTime,
|
| 509 |
+
AccessCount = site.AccessCount
|
| 510 |
+
};
|
| 511 |
+
|
| 512 |
+
context.SiteItems.Add(siteToAdd);
|
| 513 |
+
context.SaveChanges();
|
| 514 |
+
OnDataChanged(); // 触发数据��更事件
|
| 515 |
+
}
|
| 516 |
+
}
|
| 517 |
+
|
| 518 |
+
/// <summary>
|
| 519 |
+
/// 更新网站
|
| 520 |
+
/// </summary>
|
| 521 |
+
/// <param name="site">要更新的网站</param>
|
| 522 |
+
public static void UpdateSite(SiteItem site)
|
| 523 |
+
{
|
| 524 |
+
using (var context = new AppDbContext())
|
| 525 |
+
{
|
| 526 |
+
var existingSite = context.SiteItems.Find(site.Id);
|
| 527 |
+
if (existingSite != null)
|
| 528 |
+
{
|
| 529 |
+
// 更新非敏感字段
|
| 530 |
+
existingSite.GroupId = site.GroupId;
|
| 531 |
+
//existingSite.DisplayName = site.DisplayName;
|
| 532 |
+
existingSite.IsEnabled = site.IsEnabled;
|
| 533 |
+
existingSite.SortOrder = site.SortOrder;
|
| 534 |
+
existingSite.UpdateTime = DateTime.Now;
|
| 535 |
+
existingSite.LastAccessTime = site.LastAccessTime;
|
| 536 |
+
existingSite.AccessCount = site.AccessCount;
|
| 537 |
+
|
| 538 |
+
// 加密并更新敏感字段
|
| 539 |
+
existingSite.Username = !string.IsNullOrEmpty(site.Username) ? CryptoHelper.Encrypt(site.Username) : site.Username;
|
| 540 |
+
existingSite.Password = !string.IsNullOrEmpty(site.Password) ? CryptoHelper.Encrypt(site.Password) : site.Password;
|
| 541 |
+
existingSite.CommonUsername = !string.IsNullOrEmpty(site.CommonUsername) ? CryptoHelper.Encrypt(site.CommonUsername) : site.CommonUsername;
|
| 542 |
+
existingSite.CommonPassword = !string.IsNullOrEmpty(site.CommonPassword) ? CryptoHelper.Encrypt(site.CommonPassword) : site.CommonPassword;
|
| 543 |
+
existingSite.GoogleSecret = !string.IsNullOrEmpty(site.GoogleSecret) ? CryptoHelper.Encrypt(site.GoogleSecret) : site.GoogleSecret;
|
| 544 |
+
existingSite.Url = !string.IsNullOrEmpty(site.Url) ? CryptoHelper.Encrypt(site.Url) : site.Url;
|
| 545 |
+
existingSite.DisplayName = !string.IsNullOrEmpty(site.DisplayName) ? CryptoHelper.Encrypt(site.DisplayName) : site.DisplayName;
|
| 546 |
+
|
| 547 |
+
context.SaveChanges();
|
| 548 |
+
OnDataChanged(); // 触发数据变更事件
|
| 549 |
+
}
|
| 550 |
+
}
|
| 551 |
+
}
|
| 552 |
+
}
|
| 553 |
+
}
|
csharp/BrowserTool/BrowserTool/GroupEditDialog.xaml
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<Window x:Class="BrowserTool.GroupEditDialog"
|
| 2 |
+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
| 3 |
+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
| 4 |
+
Title="分组" Height="300" Width="400"
|
| 5 |
+
WindowStartupLocation="CenterScreen"
|
| 6 |
+
Background="#F5F5F5"
|
| 7 |
+
ResizeMode="NoResize">
|
| 8 |
+
|
| 9 |
+
<Window.Resources>
|
| 10 |
+
<!-- 按钮样式 -->
|
| 11 |
+
<Style TargetType="Button">
|
| 12 |
+
<Setter Property="Padding" Value="8,8"/>
|
| 13 |
+
<Setter Property="Margin" Value="5"/>
|
| 14 |
+
<Setter Property="Background" Value="#2196F3"/>
|
| 15 |
+
<Setter Property="Foreground" Value="White"/>
|
| 16 |
+
<Setter Property="BorderThickness" Value="0"/>
|
| 17 |
+
<Setter Property="Height" Value="36"/>
|
| 18 |
+
<Setter Property="MinWidth" Value="100"/>
|
| 19 |
+
<Setter Property="Template">
|
| 20 |
+
<Setter.Value>
|
| 21 |
+
<ControlTemplate TargetType="Button">
|
| 22 |
+
<Border Background="{TemplateBinding Background}"
|
| 23 |
+
BorderBrush="{TemplateBinding BorderBrush}"
|
| 24 |
+
BorderThickness="{TemplateBinding BorderThickness}"
|
| 25 |
+
CornerRadius="4">
|
| 26 |
+
<Grid>
|
| 27 |
+
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
|
| 28 |
+
</Grid>
|
| 29 |
+
</Border>
|
| 30 |
+
</ControlTemplate>
|
| 31 |
+
</Setter.Value>
|
| 32 |
+
</Setter>
|
| 33 |
+
<Style.Triggers>
|
| 34 |
+
<Trigger Property="IsMouseOver" Value="True">
|
| 35 |
+
<Setter Property="Background" Value="#1976D2"/>
|
| 36 |
+
</Trigger>
|
| 37 |
+
</Style.Triggers>
|
| 38 |
+
</Style>
|
| 39 |
+
|
| 40 |
+
<!-- 文本框样式 -->
|
| 41 |
+
<Style TargetType="TextBox">
|
| 42 |
+
<Setter Property="Padding" Value="8,6"/>
|
| 43 |
+
<Setter Property="BorderThickness" Value="0"/>
|
| 44 |
+
<Setter Property="Background" Value="Transparent"/>
|
| 45 |
+
<Setter Property="Height" Value="36"/>
|
| 46 |
+
<Setter Property="FontSize" Value="14"/>
|
| 47 |
+
<Setter Property="VerticalContentAlignment" Value="Center"/>
|
| 48 |
+
</Style>
|
| 49 |
+
|
| 50 |
+
<!-- 文本框容器样式 -->
|
| 51 |
+
<Style x:Key="TextBoxContainerStyle" TargetType="Border">
|
| 52 |
+
<Setter Property="Background" Value="White"/>
|
| 53 |
+
<Setter Property="BorderBrush" Value="#BDBDBD"/>
|
| 54 |
+
<Setter Property="BorderThickness" Value="1"/>
|
| 55 |
+
<Setter Property="CornerRadius" Value="4"/>
|
| 56 |
+
<Setter Property="Height" Value="60"/>
|
| 57 |
+
<Setter Property="Width" Value="260"/>
|
| 58 |
+
</Style>
|
| 59 |
+
</Window.Resources>
|
| 60 |
+
|
| 61 |
+
<Grid Margin="20">
|
| 62 |
+
<Grid.RowDefinitions>
|
| 63 |
+
<RowDefinition Height="Auto"/>
|
| 64 |
+
<RowDefinition Height="*"/>
|
| 65 |
+
<RowDefinition Height="Auto"/>
|
| 66 |
+
</Grid.RowDefinitions>
|
| 67 |
+
|
| 68 |
+
<!-- 标题 -->
|
| 69 |
+
<TextBlock Text="📁 分组信息"
|
| 70 |
+
FontSize="16"
|
| 71 |
+
FontWeight="SemiBold"
|
| 72 |
+
Margin="0,0,0,15"/>
|
| 73 |
+
|
| 74 |
+
<!-- 内容 -->
|
| 75 |
+
<Grid Grid.Row="1" Margin="0,0,0,10">
|
| 76 |
+
<Grid.RowDefinitions>
|
| 77 |
+
<RowDefinition Height="Auto"/>
|
| 78 |
+
<RowDefinition Height="Auto"/>
|
| 79 |
+
</Grid.RowDefinitions>
|
| 80 |
+
<Grid.ColumnDefinitions>
|
| 81 |
+
<ColumnDefinition Width="80"/>
|
| 82 |
+
<ColumnDefinition Width="*"/>
|
| 83 |
+
</Grid.ColumnDefinitions>
|
| 84 |
+
|
| 85 |
+
<!-- 分组名称 -->
|
| 86 |
+
<TextBlock Text="名称:" VerticalAlignment="Center" Margin="0,0,8,0" FontSize="14" Grid.Row="0" Grid.Column="0"/>
|
| 87 |
+
<Border CornerRadius="4" BorderBrush="#BDBDBD" BorderThickness="1" Background="White" Height="36" VerticalAlignment="Center" Grid.Row="0" Grid.Column="1" Margin="0,0,0,10">
|
| 88 |
+
<TextBox
|
| 89 |
+
Text="{Binding GroupName, UpdateSourceTrigger=PropertyChanged}"
|
| 90 |
+
BorderThickness="0"
|
| 91 |
+
Background="Transparent"
|
| 92 |
+
Height="36"
|
| 93 |
+
VerticalContentAlignment="Center"
|
| 94 |
+
FontSize="14"
|
| 95 |
+
Padding="8,6"
|
| 96 |
+
/>
|
| 97 |
+
</Border>
|
| 98 |
+
|
| 99 |
+
<!-- 是否默认展开 -->
|
| 100 |
+
<TextBlock Text="默认展开:" VerticalAlignment="Center" Margin="0,0,8,0" FontSize="14" Grid.Row="1" Grid.Column="0"/>
|
| 101 |
+
<CheckBox x:Name="chkDefaultExpanded"
|
| 102 |
+
Content="启动时默认展开此分组"
|
| 103 |
+
VerticalAlignment="Center"
|
| 104 |
+
FontSize="14"
|
| 105 |
+
Grid.Row="1" Grid.Column="1"/>
|
| 106 |
+
</Grid>
|
| 107 |
+
|
| 108 |
+
<!-- 底部按钮 -->
|
| 109 |
+
<StackPanel Grid.Row="2"
|
| 110 |
+
Orientation="Horizontal"
|
| 111 |
+
HorizontalAlignment="Right"
|
| 112 |
+
Margin="0,20,0,0">
|
| 113 |
+
<Button x:Name="btnOK"
|
| 114 |
+
Content="💾 确定"
|
| 115 |
+
Click="btnOK_Click"
|
| 116 |
+
Margin="0,0,16,0"/>
|
| 117 |
+
<Button x:Name="btnCancel"
|
| 118 |
+
Content="❌ 取消"
|
| 119 |
+
Click="btnCancel_Click"/>
|
| 120 |
+
</StackPanel>
|
| 121 |
+
</Grid>
|
| 122 |
+
</Window>
|
csharp/BrowserTool/BrowserTool/GroupEditDialog.xaml.cs
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
using System.Windows;
|
| 2 |
+
|
| 3 |
+
namespace BrowserTool
|
| 4 |
+
{
|
| 5 |
+
public partial class GroupEditDialog : Window
|
| 6 |
+
{
|
| 7 |
+
public string GroupName { get; set; }
|
| 8 |
+
public bool IsDefaultExpanded { get; set; }
|
| 9 |
+
|
| 10 |
+
public GroupEditDialog()
|
| 11 |
+
{
|
| 12 |
+
InitializeComponent();
|
| 13 |
+
DataContext = this;
|
| 14 |
+
}
|
| 15 |
+
|
| 16 |
+
public GroupEditDialog(string groupName, bool isDefaultExpanded = false) : this()
|
| 17 |
+
{
|
| 18 |
+
GroupName = groupName;
|
| 19 |
+
IsDefaultExpanded = isDefaultExpanded;
|
| 20 |
+
chkDefaultExpanded.IsChecked = isDefaultExpanded;
|
| 21 |
+
}
|
| 22 |
+
|
| 23 |
+
private void btnOK_Click(object sender, RoutedEventArgs e)
|
| 24 |
+
{
|
| 25 |
+
if (string.IsNullOrWhiteSpace(GroupName))
|
| 26 |
+
{
|
| 27 |
+
MessageBox.Show("请输入分组名称", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
|
| 28 |
+
return;
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
// 更新IsDefaultExpanded属性
|
| 32 |
+
IsDefaultExpanded = chkDefaultExpanded.IsChecked ?? false;
|
| 33 |
+
|
| 34 |
+
DialogResult = true;
|
| 35 |
+
Close();
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
private void btnCancel_Click(object sender, RoutedEventArgs e)
|
| 39 |
+
{
|
| 40 |
+
DialogResult = false;
|
| 41 |
+
Close();
|
| 42 |
+
}
|
| 43 |
+
}
|
| 44 |
+
}
|
csharp/BrowserTool/BrowserTool/IsTreeViewRootConverter.cs
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
using System;
|
| 2 |
+
using System.Globalization;
|
| 3 |
+
using System.Windows.Data;
|
| 4 |
+
using System.Windows.Controls;
|
| 5 |
+
using System.Windows;
|
| 6 |
+
|
| 7 |
+
namespace BrowserTool
|
| 8 |
+
{
|
| 9 |
+
public class IsTreeViewRootConverter : IValueConverter
|
| 10 |
+
{
|
| 11 |
+
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
|
| 12 |
+
{
|
| 13 |
+
var tvi = value as TreeViewItem;
|
| 14 |
+
if (tvi == null)
|
| 15 |
+
return System.Windows.Visibility.Collapsed;
|
| 16 |
+
var parent = System.Windows.Media.VisualTreeHelper.GetParent(tvi);
|
| 17 |
+
while (parent != null && !(parent is TreeViewItem) && !(parent is System.Windows.Controls.TreeView))
|
| 18 |
+
{
|
| 19 |
+
parent = System.Windows.Media.VisualTreeHelper.GetParent(parent);
|
| 20 |
+
}
|
| 21 |
+
if (parent is System.Windows.Controls.TreeView)
|
| 22 |
+
{
|
| 23 |
+
// 只有父级为TreeView才是根节点
|
| 24 |
+
return System.Windows.Visibility.Visible;
|
| 25 |
+
}
|
| 26 |
+
return System.Windows.Visibility.Collapsed;
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
|
| 30 |
+
{
|
| 31 |
+
throw new NotImplementedException();
|
| 32 |
+
}
|
| 33 |
+
}
|
| 34 |
+
}
|
csharp/BrowserTool/BrowserTool/LoginWindow.xaml
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<Window x:Class="BrowserTool.LoginWindow"
|
| 2 |
+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
| 3 |
+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
| 4 |
+
Title="登录" Height="190" Width="400"
|
| 5 |
+
WindowStartupLocation="CenterScreen"
|
| 6 |
+
Background="#F5F5F5"
|
| 7 |
+
ResizeMode="NoResize"
|
| 8 |
+
Icon="{StaticResource LockIcon}">
|
| 9 |
+
|
| 10 |
+
<Window.Resources>
|
| 11 |
+
<!-- 按钮样式 -->
|
| 12 |
+
<Style TargetType="Button">
|
| 13 |
+
<Setter Property="Padding" Value="15,8"/>
|
| 14 |
+
<Setter Property="Margin" Value="5"/>
|
| 15 |
+
<Setter Property="Background" Value="#2196F3"/>
|
| 16 |
+
<Setter Property="Foreground" Value="White"/>
|
| 17 |
+
<Setter Property="BorderThickness" Value="0"/>
|
| 18 |
+
<Setter Property="Height" Value="36"/>
|
| 19 |
+
<Setter Property="MinWidth" Value="100"/>
|
| 20 |
+
<Setter Property="Template">
|
| 21 |
+
<Setter.Value>
|
| 22 |
+
<ControlTemplate TargetType="Button">
|
| 23 |
+
<Border Background="{TemplateBinding Background}"
|
| 24 |
+
BorderBrush="{TemplateBinding BorderBrush}"
|
| 25 |
+
BorderThickness="{TemplateBinding BorderThickness}"
|
| 26 |
+
CornerRadius="4">
|
| 27 |
+
<Grid>
|
| 28 |
+
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
|
| 29 |
+
</Grid>
|
| 30 |
+
</Border>
|
| 31 |
+
</ControlTemplate>
|
| 32 |
+
</Setter.Value>
|
| 33 |
+
</Setter>
|
| 34 |
+
<Style.Triggers>
|
| 35 |
+
<Trigger Property="IsMouseOver" Value="True">
|
| 36 |
+
<Setter Property="Background" Value="#1976D2"/>
|
| 37 |
+
</Trigger>
|
| 38 |
+
</Style.Triggers>
|
| 39 |
+
</Style>
|
| 40 |
+
|
| 41 |
+
<!-- 密码框样式 -->
|
| 42 |
+
<Style TargetType="PasswordBox">
|
| 43 |
+
<Setter Property="Padding" Value="8,6"/>
|
| 44 |
+
<Setter Property="BorderThickness" Value="1"/>
|
| 45 |
+
<Setter Property="BorderBrush" Value="#BDBDBD"/>
|
| 46 |
+
<Setter Property="Background" Value="White"/>
|
| 47 |
+
<Setter Property="Height" Value="36"/>
|
| 48 |
+
<Setter Property="FontSize" Value="14"/>
|
| 49 |
+
<Setter Property="Template">
|
| 50 |
+
<Setter.Value>
|
| 51 |
+
<ControlTemplate TargetType="PasswordBox">
|
| 52 |
+
<Border Background="{TemplateBinding Background}"
|
| 53 |
+
BorderBrush="{TemplateBinding BorderBrush}"
|
| 54 |
+
BorderThickness="{TemplateBinding BorderThickness}"
|
| 55 |
+
CornerRadius="4">
|
| 56 |
+
<Grid>
|
| 57 |
+
<ScrollViewer x:Name="PART_ContentHost" Margin="36,0,0,0"/>
|
| 58 |
+
<Path Data="M12,17A2,2 0 0,0 14,15C14,13.89 13.1,13 12,13A2,2 0 0,0 10,15A2,2 0 0,0 12,17M18,8A2,2 0 0,1 20,10V20A2,2 0 0,1 18,22H6A2,2 0 0,1 4,20V10C4,8.89 4.9,8 6,8H7V6A5,5 0 0,1 12,1A5,5 0 0,1 17,6V8H18M12,3A3,3 0 0,0 9,6V8H15V6A3,3 0 0,0 12,3Z"
|
| 59 |
+
Fill="#757575"
|
| 60 |
+
Width="20"
|
| 61 |
+
Height="20"
|
| 62 |
+
Margin="8,0,0,0"
|
| 63 |
+
HorizontalAlignment="Left"
|
| 64 |
+
VerticalAlignment="Center"/>
|
| 65 |
+
</Grid>
|
| 66 |
+
</Border>
|
| 67 |
+
</ControlTemplate>
|
| 68 |
+
</Setter.Value>
|
| 69 |
+
</Setter>
|
| 70 |
+
</Style>
|
| 71 |
+
</Window.Resources>
|
| 72 |
+
|
| 73 |
+
<Grid Margin="20">
|
| 74 |
+
<Grid.RowDefinitions>
|
| 75 |
+
<RowDefinition Height="Auto"/>
|
| 76 |
+
<RowDefinition Height="Auto"/>
|
| 77 |
+
<RowDefinition Height="*"/>
|
| 78 |
+
</Grid.RowDefinitions>
|
| 79 |
+
|
| 80 |
+
<!-- 密码输入框 -->
|
| 81 |
+
<PasswordBox x:Name="txtPassword"
|
| 82 |
+
Grid.Row="0"
|
| 83 |
+
Margin="0,0,0,20"
|
| 84 |
+
KeyDown="txtPassword_KeyDown"/>
|
| 85 |
+
|
| 86 |
+
<!-- 错误提示 -->
|
| 87 |
+
<TextBlock x:Name="txtError"
|
| 88 |
+
Grid.Row="1"
|
| 89 |
+
Foreground="Red"
|
| 90 |
+
TextWrapping="Wrap"
|
| 91 |
+
Visibility="Collapsed"/>
|
| 92 |
+
|
| 93 |
+
<!-- 底部按钮 -->
|
| 94 |
+
<StackPanel Grid.Row="2"
|
| 95 |
+
Orientation="Horizontal"
|
| 96 |
+
HorizontalAlignment="Right"
|
| 97 |
+
VerticalAlignment="Bottom">
|
| 98 |
+
<Button x:Name="btnOK"
|
| 99 |
+
Content="确定"
|
| 100 |
+
Click="btnOK_Click"
|
| 101 |
+
Margin="0,0,16,0"/>
|
| 102 |
+
<Button x:Name="btnCancel"
|
| 103 |
+
Content="取消"
|
| 104 |
+
Click="btnCancel_Click"/>
|
| 105 |
+
</StackPanel>
|
| 106 |
+
</Grid>
|
| 107 |
+
</Window>
|
csharp/BrowserTool/BrowserTool/LoginWindow.xaml.cs
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
using System.Windows;
|
| 2 |
+
using System.Windows.Input;
|
| 3 |
+
using BrowserTool.Utils;
|
| 4 |
+
|
| 5 |
+
namespace BrowserTool
|
| 6 |
+
{
|
| 7 |
+
public partial class LoginWindow : Window
|
| 8 |
+
{
|
| 9 |
+
private const string DEFAULT_PASSWORD = "qwe123";
|
| 10 |
+
|
| 11 |
+
public LoginWindow()
|
| 12 |
+
{
|
| 13 |
+
InitializeComponent();
|
| 14 |
+
txtPassword.Focus();
|
| 15 |
+
}
|
| 16 |
+
|
| 17 |
+
private void btnOK_Click(object sender, RoutedEventArgs e)
|
| 18 |
+
{
|
| 19 |
+
ValidatePassword();
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
private void btnCancel_Click(object sender, RoutedEventArgs e)
|
| 23 |
+
{
|
| 24 |
+
DialogResult = false;
|
| 25 |
+
Close();
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
private void txtPassword_KeyDown(object sender, KeyEventArgs e)
|
| 29 |
+
{
|
| 30 |
+
if (e.Key == Key.Enter)
|
| 31 |
+
{
|
| 32 |
+
ValidatePassword();
|
| 33 |
+
}
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
private void ValidatePassword()
|
| 37 |
+
{
|
| 38 |
+
string password = txtPassword.Password;
|
| 39 |
+
|
| 40 |
+
if (string.IsNullOrWhiteSpace(password))
|
| 41 |
+
{
|
| 42 |
+
ShowError("密码不能为空!");
|
| 43 |
+
return;
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
if (PasswordHelper.VerifyOrSetStartupPassword(password))
|
| 47 |
+
{
|
| 48 |
+
KeyManager.SetPassword(password);
|
| 49 |
+
DialogResult = true;
|
| 50 |
+
Close();
|
| 51 |
+
}
|
| 52 |
+
else
|
| 53 |
+
{
|
| 54 |
+
ShowError("密码错误!");
|
| 55 |
+
txtPassword.Password = string.Empty;
|
| 56 |
+
txtPassword.Focus();
|
| 57 |
+
}
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
+
private void ShowError(string message)
|
| 61 |
+
{
|
| 62 |
+
txtError.Text = message;
|
| 63 |
+
txtError.Visibility = Visibility.Visible;
|
| 64 |
+
}
|
| 65 |
+
}
|
| 66 |
+
}
|
csharp/BrowserTool/BrowserTool/MainWindow.xaml
ADDED
|
@@ -0,0 +1,916 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<Window x:Class="BrowserTool.MainWindow"
|
| 2 |
+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
| 3 |
+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
| 4 |
+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
| 5 |
+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
| 6 |
+
xmlns:local="clr-namespace:BrowserTool"
|
| 7 |
+
xmlns:controls="clr-namespace:BrowserTool.Controls"
|
| 8 |
+
xmlns:browser="clr-namespace:BrowserTool.Browser"
|
| 9 |
+
xmlns:converters="clr-namespace:BrowserTool.Converters"
|
| 10 |
+
mc:Ignorable="d"
|
| 11 |
+
Title="Browser Tool" Height="700" Width="1200"
|
| 12 |
+
WindowStyle="None" AllowsTransparency="True" Background="#1e1e1e"
|
| 13 |
+
BorderThickness="0"
|
| 14 |
+
BorderBrush="Transparent"
|
| 15 |
+
Icon="Resources/app.ico"
|
| 16 |
+
ResizeMode="CanResize"
|
| 17 |
+
SizeChanged="MainWindow_SizeChanged"
|
| 18 |
+
KeyDown="MainWindow_KeyDown">
|
| 19 |
+
<WindowChrome.WindowChrome>
|
| 20 |
+
<WindowChrome CaptionHeight="0"
|
| 21 |
+
ResizeBorderThickness="5"
|
| 22 |
+
GlassFrameThickness="0"
|
| 23 |
+
CornerRadius="0"
|
| 24 |
+
UseAeroCaptionButtons="False"/>
|
| 25 |
+
</WindowChrome.WindowChrome>
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
<Window.Resources>
|
| 30 |
+
<local:IsTreeViewRootConverter x:Key="IsTreeViewRootConverter"/>
|
| 31 |
+
<!-- 布尔值转可见性转换器(用于界面元素的显示与隐藏) -->
|
| 32 |
+
<converters:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
|
| 33 |
+
|
| 34 |
+
<!-- 深色滚动条样式 -->
|
| 35 |
+
<Style x:Key="DarkScrollBarStyle" TargetType="ScrollBar">
|
| 36 |
+
<Setter Property="Background" Value="#2D2D30"/>
|
| 37 |
+
<Setter Property="BorderBrush" Value="#3F3F46"/>
|
| 38 |
+
<Setter Property="Foreground" Value="#686868"/>
|
| 39 |
+
<Setter Property="Width" Value="12"/>
|
| 40 |
+
<Setter Property="Template">
|
| 41 |
+
<Setter.Value>
|
| 42 |
+
<ControlTemplate TargetType="ScrollBar">
|
| 43 |
+
<Grid>
|
| 44 |
+
<Border Background="{TemplateBinding Background}"
|
| 45 |
+
BorderBrush="{TemplateBinding BorderBrush}"
|
| 46 |
+
BorderThickness="1"
|
| 47 |
+
CornerRadius="6"/>
|
| 48 |
+
<Track x:Name="PART_Track" IsDirectionReversed="True">
|
| 49 |
+
<Track.Thumb>
|
| 50 |
+
<Thumb Style="{DynamicResource DarkScrollBarThumbStyle}"/>
|
| 51 |
+
</Track.Thumb>
|
| 52 |
+
</Track>
|
| 53 |
+
</Grid>
|
| 54 |
+
</ControlTemplate>
|
| 55 |
+
</Setter.Value>
|
| 56 |
+
</Setter>
|
| 57 |
+
</Style>
|
| 58 |
+
|
| 59 |
+
<!-- 深色滚动条滑块样式 -->
|
| 60 |
+
<Style x:Key="DarkScrollBarThumbStyle" TargetType="Thumb">
|
| 61 |
+
<Setter Property="Background" Value="#686868"/>
|
| 62 |
+
<Setter Property="BorderBrush" Value="Transparent"/>
|
| 63 |
+
<Setter Property="Template">
|
| 64 |
+
<Setter.Value>
|
| 65 |
+
<ControlTemplate TargetType="Thumb">
|
| 66 |
+
<Border Background="{TemplateBinding Background}"
|
| 67 |
+
BorderBrush="{TemplateBinding BorderBrush}"
|
| 68 |
+
BorderThickness="0"
|
| 69 |
+
CornerRadius="6"/>
|
| 70 |
+
<ControlTemplate.Triggers>
|
| 71 |
+
<Trigger Property="IsMouseOver" Value="True">
|
| 72 |
+
<Setter Property="Background" Value="#9E9E9E"/>
|
| 73 |
+
</Trigger>
|
| 74 |
+
<Trigger Property="IsDragging" Value="True">
|
| 75 |
+
<Setter Property="Background" Value="#BFBFBF"/>
|
| 76 |
+
</Trigger>
|
| 77 |
+
</ControlTemplate.Triggers>
|
| 78 |
+
</ControlTemplate>
|
| 79 |
+
</Setter.Value>
|
| 80 |
+
</Setter>
|
| 81 |
+
</Style>
|
| 82 |
+
|
| 83 |
+
<!-- 深色ScrollViewer样式 -->
|
| 84 |
+
<Style x:Key="DarkScrollViewerStyle" TargetType="ScrollViewer">
|
| 85 |
+
<Setter Property="Template">
|
| 86 |
+
<Setter.Value>
|
| 87 |
+
<ControlTemplate TargetType="ScrollViewer">
|
| 88 |
+
<Grid>
|
| 89 |
+
<Grid.ColumnDefinitions>
|
| 90 |
+
<ColumnDefinition/>
|
| 91 |
+
<ColumnDefinition Width="Auto"/>
|
| 92 |
+
</Grid.ColumnDefinitions>
|
| 93 |
+
<Grid.RowDefinitions>
|
| 94 |
+
<RowDefinition/>
|
| 95 |
+
<RowDefinition Height="Auto"/>
|
| 96 |
+
</Grid.RowDefinitions>
|
| 97 |
+
<ScrollContentPresenter Grid.Column="0" Grid.Row="0"/>
|
| 98 |
+
<ScrollBar x:Name="PART_VerticalScrollBar"
|
| 99 |
+
Grid.Column="1" Grid.Row="0"
|
| 100 |
+
Style="{StaticResource DarkScrollBarStyle}"
|
| 101 |
+
Orientation="Vertical"
|
| 102 |
+
Value="{TemplateBinding VerticalOffset}"
|
| 103 |
+
Maximum="{TemplateBinding ScrollableHeight}"
|
| 104 |
+
ViewportSize="{TemplateBinding ViewportHeight}"
|
| 105 |
+
Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}"/>
|
| 106 |
+
<ScrollBar x:Name="PART_HorizontalScrollBar"
|
| 107 |
+
Grid.Column="0" Grid.Row="1"
|
| 108 |
+
Style="{StaticResource DarkScrollBarStyle}"
|
| 109 |
+
Orientation="Horizontal"
|
| 110 |
+
Value="{TemplateBinding HorizontalOffset}"
|
| 111 |
+
Maximum="{TemplateBinding ScrollableWidth}"
|
| 112 |
+
ViewportSize="{TemplateBinding ViewportWidth}"
|
| 113 |
+
Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}"/>
|
| 114 |
+
</Grid>
|
| 115 |
+
</ControlTemplate>
|
| 116 |
+
</Setter.Value>
|
| 117 |
+
</Setter>
|
| 118 |
+
</Style>
|
| 119 |
+
|
| 120 |
+
<!-- 抽屉开关按钮的自定义样式(汉堡菜单按钮) -->
|
| 121 |
+
<Style x:Key="DrawerToggleButtonStyle" TargetType="Button">
|
| 122 |
+
<Setter Property="Width" Value="32"/>
|
| 123 |
+
<Setter Property="Height" Value="32"/>
|
| 124 |
+
<Setter Property="Background" Value="Transparent"/>
|
| 125 |
+
<Setter Property="BorderThickness" Value="0"/>
|
| 126 |
+
<Setter Property="Cursor" Value="Hand"/>
|
| 127 |
+
<Setter Property="Template">
|
| 128 |
+
<Setter.Value>
|
| 129 |
+
<ControlTemplate TargetType="Button">
|
| 130 |
+
<Border Background="{TemplateBinding Background}"
|
| 131 |
+
BorderBrush="{TemplateBinding BorderBrush}"
|
| 132 |
+
BorderThickness="{TemplateBinding BorderThickness}"
|
| 133 |
+
CornerRadius="4">
|
| 134 |
+
<Grid>
|
| 135 |
+
<Path Data="M3,18H21V16H3V18M3,13H21V11H3V13M3,6V8H21V6H3Z"
|
| 136 |
+
Fill="#757575"
|
| 137 |
+
Width="24"
|
| 138 |
+
Height="24"/>
|
| 139 |
+
</Grid>
|
| 140 |
+
</Border>
|
| 141 |
+
</ControlTemplate>
|
| 142 |
+
</Setter.Value>
|
| 143 |
+
</Setter>
|
| 144 |
+
<Style.Triggers>
|
| 145 |
+
<Trigger Property="IsMouseOver" Value="True">
|
| 146 |
+
<Setter Property="Background" Value="#F5F5F5"/>
|
| 147 |
+
</Trigger>
|
| 148 |
+
</Style.Triggers>
|
| 149 |
+
</Style>
|
| 150 |
+
|
| 151 |
+
<!-- 侧边抽屉面板的自定义样式 -->
|
| 152 |
+
<Style x:Key="DrawerStyle" TargetType="Grid">
|
| 153 |
+
<Setter Property="Background" Value="White"/>
|
| 154 |
+
<Setter Property="Effect">
|
| 155 |
+
<Setter.Value>
|
| 156 |
+
<DropShadowEffect BlurRadius="10" ShadowDepth="1" Direction="270" Opacity="0.2"/>
|
| 157 |
+
</Setter.Value>
|
| 158 |
+
</Setter>
|
| 159 |
+
</Style>
|
| 160 |
+
|
| 161 |
+
<!-- 左侧树形菜单的自定义样式 -->
|
| 162 |
+
<Style x:Key="CustomTreeViewStyle" TargetType="TreeView">
|
| 163 |
+
<Setter Property="BorderThickness" Value="1"/>
|
| 164 |
+
<Setter Property="BorderBrush" Value="#3c3c3c"/>
|
| 165 |
+
<Setter Property="Background" Value="#252526"/>
|
| 166 |
+
<Setter Property="Foreground" Value="#FFFFFF"/>
|
| 167 |
+
<Setter Property="Template">
|
| 168 |
+
<Setter.Value>
|
| 169 |
+
<ControlTemplate TargetType="TreeView">
|
| 170 |
+
<Border Background="{TemplateBinding Background}"
|
| 171 |
+
BorderBrush="{TemplateBinding BorderBrush}"
|
| 172 |
+
BorderThickness="{TemplateBinding BorderThickness}"
|
| 173 |
+
CornerRadius="4">
|
| 174 |
+
<ScrollViewer Padding="{TemplateBinding Padding}" Focusable="False" Style="{StaticResource DarkScrollViewerStyle}">
|
| 175 |
+
<ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
|
| 176 |
+
</ScrollViewer>
|
| 177 |
+
</Border>
|
| 178 |
+
</ControlTemplate>
|
| 179 |
+
</Setter.Value>
|
| 180 |
+
</Setter>
|
| 181 |
+
</Style>
|
| 182 |
+
|
| 183 |
+
<!-- 树形菜单项的自定义样式,包含原生风格的展开/折叠按钮 -->
|
| 184 |
+
<!-- 展开/折叠按钮样式 -->
|
| 185 |
+
<Style x:Key="ExpandCollapseToggleStyle" TargetType="ToggleButton">
|
| 186 |
+
<Setter Property="Focusable" Value="False"/>
|
| 187 |
+
<Setter Property="Template">
|
| 188 |
+
<Setter.Value>
|
| 189 |
+
<ControlTemplate TargetType="ToggleButton">
|
| 190 |
+
<Border Background="Transparent">
|
| 191 |
+
<Path x:Name="ExpanderIcon"
|
| 192 |
+
Fill="Red" Stroke="Black" StrokeThickness="1"
|
| 193 |
+
Data="M 2 4 L 10 8 L 2 12 Z">
|
| 194 |
+
<Path.RenderTransform>
|
| 195 |
+
<RotateTransform Angle="0"/>
|
| 196 |
+
</Path.RenderTransform>
|
| 197 |
+
</Path>
|
| 198 |
+
</Border>
|
| 199 |
+
<ControlTemplate.Triggers>
|
| 200 |
+
<Trigger Property="IsChecked" Value="True">
|
| 201 |
+
<Setter TargetName="ExpanderIcon" Property="RenderTransform">
|
| 202 |
+
<Setter.Value>
|
| 203 |
+
<RotateTransform Angle="90"/>
|
| 204 |
+
</Setter.Value>
|
| 205 |
+
</Setter>
|
| 206 |
+
</Trigger>
|
| 207 |
+
</ControlTemplate.Triggers>
|
| 208 |
+
</ControlTemplate>
|
| 209 |
+
</Setter.Value>
|
| 210 |
+
</Setter>
|
| 211 |
+
</Style>
|
| 212 |
+
<!-- 自定义三角形展开/折叠按钮样式,适配深色主题 -->
|
| 213 |
+
<Style x:Key="TreeViewExpanderToggleStyle" TargetType="ToggleButton">
|
| 214 |
+
<Setter Property="Focusable" Value="False"/>
|
| 215 |
+
<Setter Property="Width" Value="16"/>
|
| 216 |
+
<Setter Property="Height" Value="16"/>
|
| 217 |
+
<Setter Property="Cursor" Value="Hand"/>
|
| 218 |
+
<Setter Property="Template">
|
| 219 |
+
<Setter.Value>
|
| 220 |
+
<ControlTemplate TargetType="ToggleButton">
|
| 221 |
+
<Grid>
|
| 222 |
+
<!-- 未展开时向右 -->
|
| 223 |
+
<Path x:Name="ExpandPath"
|
| 224 |
+
Data="M 6 4 L 10 8 L 6 12 Z"
|
| 225 |
+
Fill="#FFF" Stroke="#888" StrokeThickness="1"/>
|
| 226 |
+
</Grid>
|
| 227 |
+
<ControlTemplate.Triggers>
|
| 228 |
+
<!-- 展开时向下 -->
|
| 229 |
+
<Trigger Property="IsChecked" Value="True">
|
| 230 |
+
<Setter TargetName="ExpandPath" Property="Data" Value="M 4 6 L 12 6 L 8 10 Z"/>
|
| 231 |
+
</Trigger>
|
| 232 |
+
<Trigger Property="IsMouseOver" Value="True">
|
| 233 |
+
<Setter TargetName="ExpandPath" Property="Fill" Value="#4C8CFB"/>
|
| 234 |
+
<Setter TargetName="ExpandPath" Property="Stroke" Value="#4C8CFB"/>
|
| 235 |
+
</Trigger>
|
| 236 |
+
</ControlTemplate.Triggers>
|
| 237 |
+
</ControlTemplate>
|
| 238 |
+
</Setter.Value>
|
| 239 |
+
</Setter>
|
| 240 |
+
</Style>
|
| 241 |
+
<Style TargetType="TreeViewItem">
|
| 242 |
+
<Setter Property="OverridesDefaultStyle" Value="True"/>
|
| 243 |
+
<Setter Property="Foreground" Value="#ECECEC"/>
|
| 244 |
+
<Setter Property="FontSize" Value="15"/>
|
| 245 |
+
<Setter Property="Background" Value="Transparent"/>
|
| 246 |
+
<!--<Setter Property="Padding" Value="6,4,8,4"/>-->
|
| 247 |
+
<!--<Setter Property="Margin" Value="0,0.5,0,0.5"/>-->
|
| 248 |
+
<Setter Property="Cursor" Value="Arrow"/>
|
| 249 |
+
<!-- TreeViewItem 的上下间距由 Margin 控制,当前为 0,2,0,2 -->
|
| 250 |
+
<Setter Property="Template">
|
| 251 |
+
<Setter.Value>
|
| 252 |
+
<ControlTemplate TargetType="TreeViewItem">
|
| 253 |
+
<Grid>
|
| 254 |
+
<Grid.ColumnDefinitions>
|
| 255 |
+
<ColumnDefinition Width="Auto"/>
|
| 256 |
+
<ColumnDefinition Width="*"/>
|
| 257 |
+
</Grid.ColumnDefinitions>
|
| 258 |
+
<Grid.RowDefinitions>
|
| 259 |
+
<RowDefinition Height="32"/>
|
| 260 |
+
<RowDefinition/>
|
| 261 |
+
</Grid.RowDefinitions>
|
| 262 |
+
<!-- 展开/折叠按钮 -->
|
| 263 |
+
<ToggleButton x:Name="Expander"
|
| 264 |
+
Style="{StaticResource TreeViewExpanderToggleStyle}"
|
| 265 |
+
IsChecked="{Binding IsExpanded, RelativeSource={RelativeSource TemplatedParent}}"
|
| 266 |
+
Width="18" Height="18" Margin="8,0,4,0"
|
| 267 |
+
VerticalAlignment="Center"
|
| 268 |
+
Grid.Row="0" Grid.Column="0">
|
| 269 |
+
<ToggleButton.Visibility>
|
| 270 |
+
<Binding RelativeSource="{RelativeSource TemplatedParent}" Converter="{StaticResource IsTreeViewRootConverter}"/>
|
| 271 |
+
</ToggleButton.Visibility>
|
| 272 |
+
</ToggleButton>
|
| 273 |
+
<!-- 树节点头部内容区域(显示分组/网站名等) -->
|
| 274 |
+
<Border x:Name="Bd"
|
| 275 |
+
Grid.Column="1"
|
| 276 |
+
Background="{TemplateBinding Background}"
|
| 277 |
+
BorderBrush="{TemplateBinding BorderBrush}"
|
| 278 |
+
BorderThickness="{TemplateBinding BorderThickness}"
|
| 279 |
+
Padding="4,0,0,0"
|
| 280 |
+
Cursor="Hand">
|
| 281 |
+
<ContentPresenter x:Name="PART_Header"
|
| 282 |
+
ContentSource="Header"
|
| 283 |
+
VerticalAlignment="Center"
|
| 284 |
+
Margin="0,0,0,0"/>
|
| 285 |
+
</Border>
|
| 286 |
+
<!-- 子节点内容区域 -->
|
| 287 |
+
<ItemsPresenter Grid.Row="1" Grid.Column="1" Margin="20,0,0,0" x:Name="ItemsHost"/>
|
| 288 |
+
</Grid>
|
| 289 |
+
<ControlTemplate.Triggers>
|
| 290 |
+
<Trigger Property="IsSelected" Value="True">
|
| 291 |
+
<Setter TargetName="Bd" Property="Background" Value="#363636"/>
|
| 292 |
+
<Setter TargetName="Bd" Property="BorderBrush" Value="#4C8CFB"/>
|
| 293 |
+
</Trigger>
|
| 294 |
+
<Trigger Property="IsExpanded" Value="False">
|
| 295 |
+
<Setter Property="Visibility" TargetName="ItemsHost" Value="Collapsed"/>
|
| 296 |
+
</Trigger>
|
| 297 |
+
</ControlTemplate.Triggers>
|
| 298 |
+
</ControlTemplate>
|
| 299 |
+
</Setter.Value>
|
| 300 |
+
</Setter>
|
| 301 |
+
</Style>
|
| 302 |
+
|
| 303 |
+
<!-- 搜索框的自定义样式,带有圆角和搜索图标 -->
|
| 304 |
+
<Style x:Key="CustomSearchBoxStyle" TargetType="TextBox">
|
| 305 |
+
<Setter Property="Height" Value="32"/>
|
| 306 |
+
<Setter Property="VerticalContentAlignment" Value="Center"/>
|
| 307 |
+
<Setter Property="BorderThickness" Value="1"/>
|
| 308 |
+
<Setter Property="BorderBrush" Value="#3c3c3c"/>
|
| 309 |
+
<Setter Property="Background" Value="#2c2c2c"/>
|
| 310 |
+
<Setter Property="Foreground" Value="#FFFFFF"/>
|
| 311 |
+
<Setter Property="CaretBrush" Value="#FFFFFF"/>
|
| 312 |
+
<Setter Property="Template">
|
| 313 |
+
<Setter.Value>
|
| 314 |
+
<ControlTemplate TargetType="TextBox">
|
| 315 |
+
<Border Background="{TemplateBinding Background}"
|
| 316 |
+
BorderBrush="{TemplateBinding BorderBrush}"
|
| 317 |
+
BorderThickness="{TemplateBinding BorderThickness}"
|
| 318 |
+
CornerRadius="4">
|
| 319 |
+
<Grid>
|
| 320 |
+
<Grid.ColumnDefinitions>
|
| 321 |
+
<ColumnDefinition Width="Auto"/>
|
| 322 |
+
<ColumnDefinition Width="*"/>
|
| 323 |
+
</Grid.ColumnDefinitions>
|
| 324 |
+
<!-- 搜索图标(放在TextBox左侧) -->
|
| 325 |
+
<TextBlock Grid.Column="0" Text="" FontFamily="Segoe MDL2 Assets" FontSize="16"
|
| 326 |
+
VerticalAlignment="Center" Margin="8,0,5,0" Foreground="#888"/>
|
| 327 |
+
<!-- 输入内容区域 -->
|
| 328 |
+
<ScrollViewer x:Name="PART_ContentHost" Grid.Column="1"/>
|
| 329 |
+
</Grid>
|
| 330 |
+
</Border>
|
| 331 |
+
<ControlTemplate.Triggers>
|
| 332 |
+
<Trigger Property="IsMouseOver" Value="True">
|
| 333 |
+
<Setter Property="BorderBrush" Value="#555"/>
|
| 334 |
+
</Trigger>
|
| 335 |
+
<Trigger Property="IsKeyboardFocusWithin" Value="True">
|
| 336 |
+
<Setter Property="BorderBrush" Value="#007ACC"/>
|
| 337 |
+
</Trigger>
|
| 338 |
+
</ControlTemplate.Triggers>
|
| 339 |
+
</ControlTemplate>
|
| 340 |
+
</Setter.Value>
|
| 341 |
+
</Setter>
|
| 342 |
+
</Style>
|
| 343 |
+
|
| 344 |
+
<!-- 覆盖系统高亮颜色,适配深色主题 -->
|
| 345 |
+
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent"/>
|
| 346 |
+
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="#CCCCCC"/>
|
| 347 |
+
<SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="Transparent"/>
|
| 348 |
+
<SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightTextBrushKey}" Color="#CCCCCC"/>
|
| 349 |
+
|
| 350 |
+
<!-- TabItem样式 -->
|
| 351 |
+
<Style TargetType="TabItem">
|
| 352 |
+
<!-- 固定Tab宽度为160px -->
|
| 353 |
+
<Setter Property="Width" Value="140"/>
|
| 354 |
+
<Setter Property="Template">
|
| 355 |
+
<Setter.Value>
|
| 356 |
+
<ControlTemplate TargetType="TabItem">
|
| 357 |
+
<Border x:Name="Border"
|
| 358 |
+
BorderThickness="1,1,1,0"
|
| 359 |
+
BorderBrush="#3c3c3c"
|
| 360 |
+
Background="Transparent"
|
| 361 |
+
Margin="0,0,2,0"
|
| 362 |
+
Width="140">
|
| 363 |
+
<Grid>
|
| 364 |
+
<Grid.ColumnDefinitions>
|
| 365 |
+
<ColumnDefinition Width="*"/>
|
| 366 |
+
<ColumnDefinition Width="Auto"/>
|
| 367 |
+
</Grid.ColumnDefinitions>
|
| 368 |
+
<!-- 标题文本,支持截取显示 -->
|
| 369 |
+
<TextBlock Grid.Column="0"
|
| 370 |
+
Text="{Binding Header, RelativeSource={RelativeSource TemplatedParent}}"
|
| 371 |
+
Foreground="#FFFFFF"
|
| 372 |
+
Margin="8,4,4,4"
|
| 373 |
+
VerticalAlignment="Center"
|
| 374 |
+
TextTrimming="CharacterEllipsis"
|
| 375 |
+
ToolTip="{Binding Header, RelativeSource={RelativeSource TemplatedParent}}"/>
|
| 376 |
+
<!-- 关闭按钮 -->
|
| 377 |
+
<Button Grid.Column="1"
|
| 378 |
+
Width="16" Height="16"
|
| 379 |
+
Margin="0,0,8,0"
|
| 380 |
+
Click="CloseTabButton_Click"
|
| 381 |
+
Tag="{Binding RelativeSource={RelativeSource TemplatedParent}}"
|
| 382 |
+
Background="Transparent"
|
| 383 |
+
BorderThickness="0"
|
| 384 |
+
Padding="0"
|
| 385 |
+
Cursor="Hand"
|
| 386 |
+
VerticalAlignment="Center">
|
| 387 |
+
<Button.Style>
|
| 388 |
+
<Style TargetType="Button">
|
| 389 |
+
<Setter Property="Background" Value="Transparent"/>
|
| 390 |
+
<Setter Property="Cursor" Value="Hand"/>
|
| 391 |
+
<Setter Property="Template">
|
| 392 |
+
<Setter.Value>
|
| 393 |
+
<ControlTemplate TargetType="Button">
|
| 394 |
+
<Border x:Name="Border"
|
| 395 |
+
Background="Transparent"
|
| 396 |
+
CornerRadius="8">
|
| 397 |
+
<TextBlock Text=""
|
| 398 |
+
FontFamily="Segoe MDL2 Assets"
|
| 399 |
+
FontSize="10"
|
| 400 |
+
Foreground="#666"
|
| 401 |
+
HorizontalAlignment="Center"
|
| 402 |
+
VerticalAlignment="Center"/>
|
| 403 |
+
</Border>
|
| 404 |
+
<ControlTemplate.Triggers>
|
| 405 |
+
<Trigger Property="IsMouseOver" Value="True">
|
| 406 |
+
<Setter TargetName="Border" Property="Background" Value="#E0E0E0"/>
|
| 407 |
+
<Setter Property="Foreground" Value="#333"/>
|
| 408 |
+
</Trigger>
|
| 409 |
+
</ControlTemplate.Triggers>
|
| 410 |
+
</ControlTemplate>
|
| 411 |
+
</Setter.Value>
|
| 412 |
+
</Setter>
|
| 413 |
+
</Style>
|
| 414 |
+
</Button.Style>
|
| 415 |
+
</Button>
|
| 416 |
+
</Grid>
|
| 417 |
+
</Border>
|
| 418 |
+
<ControlTemplate.Triggers>
|
| 419 |
+
<!-- 选中状态:更突出的蓝色背景 -->
|
| 420 |
+
<Trigger Property="IsSelected" Value="True">
|
| 421 |
+
<Setter TargetName="Border" Property="Background" Value="#2979FF"/>
|
| 422 |
+
<Setter TargetName="Border" Property="BorderBrush" Value="#2979FF"/>
|
| 423 |
+
</Trigger>
|
| 424 |
+
<!-- 非选中状态的鼠标悬停:中等灰色背景 -->
|
| 425 |
+
<MultiTrigger>
|
| 426 |
+
<MultiTrigger.Conditions>
|
| 427 |
+
<Condition Property="IsMouseOver" Value="True"/>
|
| 428 |
+
<Condition Property="IsSelected" Value="False"/>
|
| 429 |
+
</MultiTrigger.Conditions>
|
| 430 |
+
<Setter TargetName="Border" Property="Background" Value="#3a3a3a"/>
|
| 431 |
+
</MultiTrigger>
|
| 432 |
+
<!-- 选中状态的鼠标悬停:更亮的蓝色背景 -->
|
| 433 |
+
<MultiTrigger>
|
| 434 |
+
<MultiTrigger.Conditions>
|
| 435 |
+
<Condition Property="IsMouseOver" Value="True"/>
|
| 436 |
+
<Condition Property="IsSelected" Value="True"/>
|
| 437 |
+
</MultiTrigger.Conditions>
|
| 438 |
+
<Setter TargetName="Border" Property="Background" Value="#4A90E2"/>
|
| 439 |
+
</MultiTrigger>
|
| 440 |
+
</ControlTemplate.Triggers>
|
| 441 |
+
</ControlTemplate>
|
| 442 |
+
</Setter.Value>
|
| 443 |
+
</Setter>
|
| 444 |
+
</Style>
|
| 445 |
+
|
| 446 |
+
</Window.Resources>
|
| 447 |
+
|
| 448 |
+
<Grid>
|
| 449 |
+
<Grid.RowDefinitions>
|
| 450 |
+
<RowDefinition Height="40"/>
|
| 451 |
+
<RowDefinition Height="*"/>
|
| 452 |
+
</Grid.RowDefinitions>
|
| 453 |
+
|
| 454 |
+
<!-- 顶部自定义标题栏(包含窗口控制按钮和Logo) -->
|
| 455 |
+
<DockPanel Grid.Row="0" Height="40" Background="#141414" Margin="0" MouseLeftButtonDown="TitleBar_MouseLeftButtonDown">
|
| 456 |
+
<!-- 左侧Logo或标题 -->
|
| 457 |
+
<StackPanel Orientation="Horizontal" DockPanel.Dock="Left" HorizontalAlignment="Left">
|
| 458 |
+
<TextBlock Text="Tool" Foreground="#EEE" FontSize="16" VerticalAlignment="Center" Margin="16,0,0,0"/>
|
| 459 |
+
<Button x:Name="DrawerToggleBtn" Width="40" Height="40" Margin="0,0,8,0" Background="Transparent" BorderBrush="Transparent" BorderThickness="0" Cursor="Hand" Click="DrawerToggleBtn_Click">
|
| 460 |
+
<Button.Style>
|
| 461 |
+
<Style TargetType="Button" BasedOn="{StaticResource {x:Type Button}}">
|
| 462 |
+
<!-- 设置按钮的默认背景色为透明 -->
|
| 463 |
+
<Setter Property="Background" Value="Transparent"/>
|
| 464 |
+
<!-- 定义按钮的自定义模板 -->
|
| 465 |
+
<Setter Property="Template">
|
| 466 |
+
<Setter.Value>
|
| 467 |
+
<ControlTemplate TargetType="Button">
|
| 468 |
+
<!-- 使用Grid作为根容器,可以叠加多个元素 -->
|
| 469 |
+
<Grid>
|
| 470 |
+
<!-- 背景层:用于显示鼠标悬停效果,x:Name="BackgroundBorder" 给Border命名,以便在触发器中引用 -->
|
| 471 |
+
<Border x:Name="BackgroundBorder" Background="Transparent"/>
|
| 472 |
+
<!-- 内容层 -->
|
| 473 |
+
<Viewbox Width="24" Height="24" Stretch="Uniform">
|
| 474 |
+
<Canvas Width="24" Height="24">
|
| 475 |
+
<Path StrokeThickness="2" Stroke="#AAA" StrokeStartLineCap="Round" StrokeEndLineCap="Round" Data="M4 7h16M4 12h16M4 17h16"/>
|
| 476 |
+
</Canvas>
|
| 477 |
+
</Viewbox>
|
| 478 |
+
</Grid>
|
| 479 |
+
<!-- 模板触发器:定义鼠标悬停等交互效果 -->
|
| 480 |
+
<ControlTemplate.Triggers>
|
| 481 |
+
<!-- 当鼠标悬停时,将背景色改为深灰色 -->
|
| 482 |
+
<Trigger Property="IsMouseOver" Value="True">
|
| 483 |
+
<Setter TargetName="BackgroundBorder" Property="Background" Value="#555"/>
|
| 484 |
+
</Trigger>
|
| 485 |
+
</ControlTemplate.Triggers>
|
| 486 |
+
</ControlTemplate>
|
| 487 |
+
</Setter.Value>
|
| 488 |
+
</Setter>
|
| 489 |
+
</Style>
|
| 490 |
+
</Button.Style>
|
| 491 |
+
</Button>
|
| 492 |
+
<!-- 测试环境按钮 -->
|
| 493 |
+
<Button x:Name="TestEnvButton" Width="40" Height="40" Margin="0,0,8,0" Background="Transparent" BorderBrush="Transparent" BorderThickness="0" Cursor="Hand" Click="TestEnvButton_Click" ToolTip="测试环境">
|
| 494 |
+
<Button.Style>
|
| 495 |
+
<Style TargetType="Button" BasedOn="{StaticResource {x:Type Button}}">
|
| 496 |
+
<Setter Property="Background" Value="Transparent"/>
|
| 497 |
+
<Setter Property="Template">
|
| 498 |
+
<Setter.Value>
|
| 499 |
+
<ControlTemplate TargetType="Button">
|
| 500 |
+
<Grid>
|
| 501 |
+
<Border x:Name="BackgroundBorder" Background="Transparent"/>
|
| 502 |
+
<Viewbox Width="20" Height="20" Stretch="Uniform">
|
| 503 |
+
<Canvas Width="24" Height="24">
|
| 504 |
+
<!-- 蜘蛛图标 -->
|
| 505 |
+
<!-- 蜘蛛身体 -->
|
| 506 |
+
<Ellipse Fill="#AAA" Canvas.Left="10" Canvas.Top="8" Width="4" Height="8"/>
|
| 507 |
+
<!-- 蜘蛛头部 -->
|
| 508 |
+
<Ellipse Fill="#AAA" Canvas.Left="10.5" Canvas.Top="6" Width="3" Height="3"/>
|
| 509 |
+
<!-- 蜘蛛腿 - 左侧 -->
|
| 510 |
+
<Path Fill="#AAA" Data="M8,9 L4,6 L3,7 L7,10 Z"/>
|
| 511 |
+
<Path Fill="#AAA" Data="M8,11 L3,9 L2,10 L7,12 Z"/>
|
| 512 |
+
<Path Fill="#AAA" Data="M8,13 L4,16 L5,17 L9,14 Z"/>
|
| 513 |
+
<Path Fill="#AAA" Data="M8,15 L3,18 L4,19 L9,16 Z"/>
|
| 514 |
+
<!-- 蜘蛛腿 - 右侧 -->
|
| 515 |
+
<Path Fill="#AAA" Data="M16,9 L20,6 L21,7 L17,10 Z"/>
|
| 516 |
+
<Path Fill="#AAA" Data="M16,11 L21,9 L22,10 L17,12 Z"/>
|
| 517 |
+
<Path Fill="#AAA" Data="M16,13 L20,16 L19,17 L15,14 Z"/>
|
| 518 |
+
<Path Fill="#AAA" Data="M16,15 L21,18 L20,19 L15,16 Z"/>
|
| 519 |
+
</Canvas>
|
| 520 |
+
</Viewbox>
|
| 521 |
+
</Grid>
|
| 522 |
+
<ControlTemplate.Triggers>
|
| 523 |
+
<Trigger Property="IsMouseOver" Value="True">
|
| 524 |
+
<Setter TargetName="BackgroundBorder" Property="Background" Value="#555"/>
|
| 525 |
+
</Trigger>
|
| 526 |
+
</ControlTemplate.Triggers>
|
| 527 |
+
</ControlTemplate>
|
| 528 |
+
</Setter.Value>
|
| 529 |
+
</Setter>
|
| 530 |
+
</Style>
|
| 531 |
+
</Button.Style>
|
| 532 |
+
</Button>
|
| 533 |
+
<!-- 手动打卡按钮 -->
|
| 534 |
+
<Button x:Name="ManualCheckInButton" Width="40" Height="40" Margin="0,0,8,0" Background="Transparent" BorderBrush="Transparent" BorderThickness="0" Cursor="Hand" Click="ManualCheckInButton_Click" ToolTip="手动打卡">
|
| 535 |
+
<Button.Style>
|
| 536 |
+
<Style TargetType="Button" BasedOn="{StaticResource {x:Type Button}}">
|
| 537 |
+
<Setter Property="Background" Value="Transparent"/>
|
| 538 |
+
<Setter Property="Template">
|
| 539 |
+
<Setter.Value>
|
| 540 |
+
<ControlTemplate TargetType="Button">
|
| 541 |
+
<Grid>
|
| 542 |
+
<Border x:Name="BackgroundBorder" Background="Transparent"/>
|
| 543 |
+
<Viewbox Width="20" Height="20" Stretch="Uniform">
|
| 544 |
+
<Canvas Width="24" Height="24">
|
| 545 |
+
<!-- 时钟图标 -->
|
| 546 |
+
<Ellipse Fill="Transparent" Stroke="#AAA" StrokeThickness="1.5" Canvas.Left="2" Canvas.Top="2" Width="20" Height="20"/>
|
| 547 |
+
<!-- 时钟指针 -->
|
| 548 |
+
<Path Stroke="#AAA" StrokeThickness="1.5" Data="M12,6 L12,12 L16,16"/>
|
| 549 |
+
<!-- 勾号 -->
|
| 550 |
+
<Path Stroke="#4CAF50" StrokeThickness="2" Data="M8,14 L10,16 L16,10"/>
|
| 551 |
+
</Canvas>
|
| 552 |
+
</Viewbox>
|
| 553 |
+
</Grid>
|
| 554 |
+
<ControlTemplate.Triggers>
|
| 555 |
+
<Trigger Property="IsMouseOver" Value="True">
|
| 556 |
+
<Setter TargetName="BackgroundBorder" Property="Background" Value="#555"/>
|
| 557 |
+
</Trigger>
|
| 558 |
+
</ControlTemplate.Triggers>
|
| 559 |
+
</ControlTemplate>
|
| 560 |
+
</Setter.Value>
|
| 561 |
+
</Setter>
|
| 562 |
+
</Style>
|
| 563 |
+
</Button.Style>
|
| 564 |
+
</Button>
|
| 565 |
+
|
| 566 |
+
</StackPanel>
|
| 567 |
+
<!-- 右上角窗口控制按钮区域(测试环境、最小化、最大化、关闭) -->
|
| 568 |
+
<StackPanel Orientation="Horizontal" DockPanel.Dock="Right" HorizontalAlignment="Right">
|
| 569 |
+
<!-- 最小化按钮 -->
|
| 570 |
+
<Button Width="40" Height="40" Background="Transparent" BorderBrush="Transparent" BorderThickness="0" Click="MinimizeButton_Click">
|
| 571 |
+
<Button.Style>
|
| 572 |
+
<Style TargetType="Button" BasedOn="{StaticResource {x:Type Button}}">
|
| 573 |
+
<!-- 设置按钮的默认背景色为透明 -->
|
| 574 |
+
<Setter Property="Background" Value="Transparent"/>
|
| 575 |
+
<!-- 定义按钮的自定义模板 -->
|
| 576 |
+
<Setter Property="Template">
|
| 577 |
+
<Setter.Value>
|
| 578 |
+
<ControlTemplate TargetType="Button">
|
| 579 |
+
<!-- 使用Grid作为根容器,可以叠加多个元素 -->
|
| 580 |
+
<Grid>
|
| 581 |
+
<!-- 背景层:用于显示鼠标悬停效果,x:Name="BackgroundBorder" 给Border命名,以便在触发器中引用 -->
|
| 582 |
+
<Border x:Name="BackgroundBorder" Background="Transparent"/>
|
| 583 |
+
<!-- 内容层 -->
|
| 584 |
+
<TextBlock Text="" FontFamily="Segoe MDL2 Assets" FontSize="16" Foreground="#AAA" HorizontalAlignment="Center" VerticalAlignment="Center"/>
|
| 585 |
+
</Grid>
|
| 586 |
+
<!-- 模板触发器:定义鼠标悬停等交互效果 -->
|
| 587 |
+
<ControlTemplate.Triggers>
|
| 588 |
+
<!-- 当鼠标悬停时,将背景色改为深灰色 -->
|
| 589 |
+
<Trigger Property="IsMouseOver" Value="True">
|
| 590 |
+
<Setter TargetName="BackgroundBorder" Property="Background" Value="#555"/>
|
| 591 |
+
</Trigger>
|
| 592 |
+
</ControlTemplate.Triggers>
|
| 593 |
+
</ControlTemplate>
|
| 594 |
+
</Setter.Value>
|
| 595 |
+
</Setter>
|
| 596 |
+
</Style>
|
| 597 |
+
</Button.Style>
|
| 598 |
+
</Button>
|
| 599 |
+
<!-- 最大化按钮 -->
|
| 600 |
+
<Button Width="40" Height="40" Background="Transparent" BorderBrush="Transparent" BorderThickness="0" Click="MaximizeButton_Click">
|
| 601 |
+
<Button.Style>
|
| 602 |
+
<Style TargetType="Button" BasedOn="{StaticResource {x:Type Button}}">
|
| 603 |
+
<!-- 设置按钮的默认背景色为透明 -->
|
| 604 |
+
<Setter Property="Background" Value="Transparent"/>
|
| 605 |
+
<!-- 定义按钮的自定义模板 -->
|
| 606 |
+
<Setter Property="Template">
|
| 607 |
+
<Setter.Value>
|
| 608 |
+
<ControlTemplate TargetType="Button">
|
| 609 |
+
<!-- 使用Grid作为根容器,可以叠加多个元素 -->
|
| 610 |
+
<Grid>
|
| 611 |
+
<!-- 背景层:用于显示鼠标悬停效果,x:Name="BackgroundBorder" 给Border命名,以便在触发器中引用 -->
|
| 612 |
+
<Border x:Name="BackgroundBorder" Background="Transparent"/>
|
| 613 |
+
<!-- 内容层 -->
|
| 614 |
+
<TextBlock Text="" FontFamily="Segoe MDL2 Assets" FontSize="16" Foreground="#AAA" HorizontalAlignment="Center" VerticalAlignment="Center"/>
|
| 615 |
+
</Grid>
|
| 616 |
+
<!-- 模板触发器:定义鼠标悬停等交互效果 -->
|
| 617 |
+
<ControlTemplate.Triggers>
|
| 618 |
+
<!-- 当鼠标悬停时,将背景色改为深灰色 -->
|
| 619 |
+
<Trigger Property="IsMouseOver" Value="True">
|
| 620 |
+
<Setter TargetName="BackgroundBorder" Property="Background" Value="#555"/>
|
| 621 |
+
</Trigger>
|
| 622 |
+
</ControlTemplate.Triggers>
|
| 623 |
+
</ControlTemplate>
|
| 624 |
+
</Setter.Value>
|
| 625 |
+
</Setter>
|
| 626 |
+
</Style>
|
| 627 |
+
</Button.Style>
|
| 628 |
+
</Button>
|
| 629 |
+
<!-- 关闭按钮 -->
|
| 630 |
+
<Button Width="40" Height="40" Click="CloseButton_Click">
|
| 631 |
+
<Button.Style>
|
| 632 |
+
<Style TargetType="Button">
|
| 633 |
+
<!-- 设置按钮的默认背景色为透明 -->
|
| 634 |
+
<Setter Property="Background" Value="Transparent"/>
|
| 635 |
+
|
| 636 |
+
<!-- 定义按钮的自定义模板 -->
|
| 637 |
+
<Setter Property="Template">
|
| 638 |
+
<Setter.Value>
|
| 639 |
+
<ControlTemplate TargetType="Button">
|
| 640 |
+
<!-- 使用Grid作为根容器,可以叠加多个元素 -->
|
| 641 |
+
<Grid>
|
| 642 |
+
<!-- 背景层:用于显示鼠标悬停效果,x:Name="BackgroundBorder" 给Border命名,以便在触发器中引用,默认背景为透明 -->
|
| 643 |
+
<Border x:Name="BackgroundBorder"
|
| 644 |
+
Background="Transparent"/>
|
| 645 |
+
|
| 646 |
+
<!-- 内容层:显示关闭图标,使用Segoe MDL2 Assets字体中的关闭图标,颜色为浅红色 -->
|
| 647 |
+
<TextBlock Text=""
|
| 648 |
+
FontFamily="Segoe MDL2 Assets"
|
| 649 |
+
FontSize="16"
|
| 650 |
+
Foreground="#F55"
|
| 651 |
+
HorizontalAlignment="Center"
|
| 652 |
+
VerticalAlignment="Center"/>
|
| 653 |
+
</Grid>
|
| 654 |
+
|
| 655 |
+
<!-- 模板触发器:定义鼠标悬停等交互效果 -->
|
| 656 |
+
<ControlTemplate.Triggers>
|
| 657 |
+
<!-- 当鼠标悬停时,将背景色改为深红色 -->
|
| 658 |
+
<Trigger Property="IsMouseOver" Value="True">
|
| 659 |
+
<Setter TargetName="BackgroundBorder" Property="Background" Value="#861b2d"/>
|
| 660 |
+
</Trigger>
|
| 661 |
+
</ControlTemplate.Triggers>
|
| 662 |
+
</ControlTemplate>
|
| 663 |
+
</Setter.Value>
|
| 664 |
+
</Setter>
|
| 665 |
+
</Style>
|
| 666 |
+
</Button.Style>
|
| 667 |
+
</Button>
|
| 668 |
+
</StackPanel>
|
| 669 |
+
</DockPanel>
|
| 670 |
+
|
| 671 |
+
<!-- 主内容区(包含左侧菜单和右侧TabControl) -->
|
| 672 |
+
<Grid Grid.Row="1" Background="#1e1e1e" Margin="0">
|
| 673 |
+
<Grid.ColumnDefinitions>
|
| 674 |
+
<ColumnDefinition Width="0"/>
|
| 675 |
+
<ColumnDefinition x:Name="DrawerCol" Width="200"/>
|
| 676 |
+
<ColumnDefinition Width="*"/>
|
| 677 |
+
</Grid.ColumnDefinitions>
|
| 678 |
+
|
| 679 |
+
<!-- 左侧菜单卡片区域 -->
|
| 680 |
+
<Border x:Name="DrawerBorder" Grid.Column="1" Margin="0,0,0,0"
|
| 681 |
+
Background="#252526" Padding="10"
|
| 682 |
+
VerticalAlignment="Stretch" HorizontalAlignment="Left" Width="200"
|
| 683 |
+
Panel.ZIndex="11">
|
| 684 |
+
<DockPanel x:Name="DrawerPanel" LastChildFill="True">
|
| 685 |
+
<!-- 搜索框,带圆角和图标 -->
|
| 686 |
+
<TextBox x:Name="SearchBox" TextChanged="SearchBox_TextChanged"
|
| 687 |
+
Style="{StaticResource CustomSearchBoxStyle}" Margin="10,5,10,5" DockPanel.Dock="Top"/>
|
| 688 |
+
<!-- 树形菜单,带图标 -->
|
| 689 |
+
<TreeView x:Name="MenuTree" Style="{StaticResource CustomTreeViewStyle}"
|
| 690 |
+
SelectedItemChanged="MenuTree_SelectedItemChanged">
|
| 691 |
+
</TreeView>
|
| 692 |
+
</DockPanel>
|
| 693 |
+
</Border>
|
| 694 |
+
|
| 695 |
+
<!-- 右侧TabControl区域 -->
|
| 696 |
+
<Grid Grid.Column="2" Margin="0">
|
| 697 |
+
<Grid.RowDefinitions>
|
| 698 |
+
<RowDefinition Height="Auto"/>
|
| 699 |
+
<RowDefinition Height="Auto"/>
|
| 700 |
+
<RowDefinition Height="*"/>
|
| 701 |
+
</Grid.RowDefinitions>
|
| 702 |
+
<!-- 地址栏区域(包含导航按钮、URL输入框、跳转按钮) -->
|
| 703 |
+
<Grid x:Name="UrlBar" Grid.Row="0" Margin="5,5,5,5" Visibility="Collapsed" Background="#3c3c3c">
|
| 704 |
+
<Grid.ColumnDefinitions>
|
| 705 |
+
<ColumnDefinition Width="Auto"/>
|
| 706 |
+
<ColumnDefinition Width="*"/>
|
| 707 |
+
<ColumnDefinition Width="Auto"/>
|
| 708 |
+
</Grid.ColumnDefinitions>
|
| 709 |
+
<!-- 导航按钮(后退、前进、刷新) -->
|
| 710 |
+
<StackPanel Grid.Column="0" Orientation="Horizontal" Margin="0,0,5,0">
|
| 711 |
+
<Button x:Name="BackButton" Width="32" Height="32" Margin="0,0,2,0"
|
| 712 |
+
Click="BackButton_Click" ToolTip="后退"
|
| 713 |
+
Background="Transparent" BorderThickness="0">
|
| 714 |
+
<TextBlock Text="" FontFamily="Segoe MDL2 Assets" FontSize="16" Foreground="#FFFFFF"/>
|
| 715 |
+
</Button>
|
| 716 |
+
<Button x:Name="ForwardButton" Width="32" Height="32" Margin="2,0,2,0"
|
| 717 |
+
Click="ForwardButton_Click" ToolTip="前进"
|
| 718 |
+
Background="Transparent" BorderThickness="0">
|
| 719 |
+
<TextBlock Text="" FontFamily="Segoe MDL2 Assets" FontSize="16" Foreground="#FFFFFF"/>
|
| 720 |
+
</Button>
|
| 721 |
+
<Button x:Name="RefreshButton" Width="32" Height="32" Margin="2,0,0,0"
|
| 722 |
+
Click="RefreshButton_Click" ToolTip="刷新"
|
| 723 |
+
Background="Transparent" BorderThickness="0">
|
| 724 |
+
<TextBlock Text="" FontFamily="Segoe MDL2 Assets" FontSize="16" Foreground="#FFFFFF"/>
|
| 725 |
+
</Button>
|
| 726 |
+
</StackPanel>
|
| 727 |
+
<!-- URL输入框 -->
|
| 728 |
+
<TextBox x:Name="UrlTextBox" Grid.Column="1" Height="32" Margin="0,0,5,0"
|
| 729 |
+
VerticalContentAlignment="Center" Padding="8,0"
|
| 730 |
+
Background="#2c2c2c" Foreground="#FFFFFF" CaretBrush="#FFFFFF"
|
| 731 |
+
BorderThickness="1" BorderBrush="#555555"
|
| 732 |
+
KeyDown="UrlTextBox_KeyDown"/>
|
| 733 |
+
<!-- 跳转按钮(根据地址栏内容跳转) -->
|
| 734 |
+
<Button x:Name="DevToolsButton" Grid.Column="2" Width="32" Height="32"
|
| 735 |
+
Click="DevToolsButton_Click" ToolTip="前往"
|
| 736 |
+
Background="Transparent" BorderThickness="0" Margin="2,0,0,0">
|
| 737 |
+
<TextBlock Text="" FontFamily="Segoe MDL2 Assets" FontSize="16" Foreground="#FFFFFF"/>
|
| 738 |
+
</Button>
|
| 739 |
+
</Grid>
|
| 740 |
+
|
| 741 |
+
<!-- 搜索栏区域 -->
|
| 742 |
+
<Grid Grid.Row="1" Margin="0" Panel.ZIndex="100">
|
| 743 |
+
<controls:SearchBar x:Name="PageSearchBar"
|
| 744 |
+
Visibility="Collapsed"
|
| 745 |
+
HorizontalAlignment="Right"
|
| 746 |
+
VerticalAlignment="Top"
|
| 747 |
+
Margin="0,5,20,0"
|
| 748 |
+
Width="350"
|
| 749 |
+
CloseRequested="PageSearchBar_CloseRequested"/>
|
| 750 |
+
</Grid>
|
| 751 |
+
|
| 752 |
+
<!-- 主TabControl控件,显示各个网页内容 -->
|
| 753 |
+
<TabControl x:Name="MainTabControl" Grid.Row="2"
|
| 754 |
+
BorderThickness="1,0,0,0" BorderBrush="#3c3c3c"
|
| 755 |
+
MouseRightButtonDown="MainTabControl_MouseRightButtonDown"
|
| 756 |
+
SelectionChanged="MainTabControl_SelectionChanged">
|
| 757 |
+
<TabControl.Style>
|
| 758 |
+
<Style TargetType="TabControl">
|
| 759 |
+
<Setter Property="Template">
|
| 760 |
+
<Setter.Value>
|
| 761 |
+
<ControlTemplate TargetType="TabControl">
|
| 762 |
+
<Grid>
|
| 763 |
+
<Grid.RowDefinitions>
|
| 764 |
+
<RowDefinition Height="Auto"/>
|
| 765 |
+
<RowDefinition Height="*"/>
|
| 766 |
+
</Grid.RowDefinitions>
|
| 767 |
+
<!-- 标签页头部区域 -->
|
| 768 |
+
<Border Grid.Row="0"
|
| 769 |
+
BorderThickness="{TemplateBinding BorderThickness}"
|
| 770 |
+
BorderBrush="#3c3c3c"
|
| 771 |
+
Background="#3c3c3c">
|
| 772 |
+
<TabPanel x:Name="HeaderPanel"
|
| 773 |
+
Panel.ZIndex="1"
|
| 774 |
+
Background="Transparent"
|
| 775 |
+
KeyboardNavigation.TabIndex="1"
|
| 776 |
+
IsItemsHost="True"/>
|
| 777 |
+
</Border>
|
| 778 |
+
<!-- 标签页内容区域 -->
|
| 779 |
+
<Border Grid.Row="1"
|
| 780 |
+
Background="#1e1e1e"
|
| 781 |
+
BorderThickness="{TemplateBinding BorderThickness}"
|
| 782 |
+
BorderBrush="#3c3c3c"
|
| 783 |
+
KeyboardNavigation.DirectionalNavigation="Contained"
|
| 784 |
+
KeyboardNavigation.TabIndex="2"
|
| 785 |
+
KeyboardNavigation.TabNavigation="Local">
|
| 786 |
+
<ContentPresenter x:Name="PART_SelectedContentHost"
|
| 787 |
+
Margin="{TemplateBinding Padding}"
|
| 788 |
+
ContentSource="SelectedContent"/>
|
| 789 |
+
</Border>
|
| 790 |
+
</Grid>
|
| 791 |
+
</ControlTemplate>
|
| 792 |
+
</Setter.Value>
|
| 793 |
+
</Setter>
|
| 794 |
+
</Style>
|
| 795 |
+
</TabControl.Style>
|
| 796 |
+
<TabControl.ItemContainerStyle>
|
| 797 |
+
<Style TargetType="TabItem">
|
| 798 |
+
<Setter Property="Template">
|
| 799 |
+
<Setter.Value>
|
| 800 |
+
<ControlTemplate TargetType="TabItem">
|
| 801 |
+
<Border x:Name="Border"
|
| 802 |
+
BorderThickness="1,1,1,0"
|
| 803 |
+
BorderBrush="#3c3c3c"
|
| 804 |
+
Background="Transparent"
|
| 805 |
+
Margin="0,0,2,0"
|
| 806 |
+
Width="140">
|
| 807 |
+
<Grid>
|
| 808 |
+
<Grid.ColumnDefinitions>
|
| 809 |
+
<ColumnDefinition Width="*"/>
|
| 810 |
+
<ColumnDefinition Width="Auto"/>
|
| 811 |
+
</Grid.ColumnDefinitions>
|
| 812 |
+
<!-- 标题文本,支持截取显示 -->
|
| 813 |
+
<TextBlock Grid.Column="0"
|
| 814 |
+
Text="{Binding Header, RelativeSource={RelativeSource TemplatedParent}}"
|
| 815 |
+
Foreground="#FFFFFF"
|
| 816 |
+
Margin="8,4,4,4"
|
| 817 |
+
VerticalAlignment="Center"
|
| 818 |
+
TextTrimming="CharacterEllipsis"
|
| 819 |
+
ToolTip="{Binding Header, RelativeSource={RelativeSource TemplatedParent}}"/>
|
| 820 |
+
<!-- 关闭按钮 -->
|
| 821 |
+
<Button Grid.Column="1"
|
| 822 |
+
Width="16" Height="16"
|
| 823 |
+
Margin="0,0,8,0"
|
| 824 |
+
Click="CloseTabButton_Click"
|
| 825 |
+
Tag="{Binding RelativeSource={RelativeSource TemplatedParent}}"
|
| 826 |
+
Background="Transparent"
|
| 827 |
+
BorderThickness="0"
|
| 828 |
+
Padding="0"
|
| 829 |
+
Cursor="Hand"
|
| 830 |
+
VerticalAlignment="Center">
|
| 831 |
+
<Button.Style>
|
| 832 |
+
<Style TargetType="Button">
|
| 833 |
+
<Setter Property="Background" Value="Transparent"/>
|
| 834 |
+
<Setter Property="Cursor" Value="Hand"/>
|
| 835 |
+
<Setter Property="Template">
|
| 836 |
+
<Setter.Value>
|
| 837 |
+
<ControlTemplate TargetType="Button">
|
| 838 |
+
<Border x:Name="Border"
|
| 839 |
+
Background="Transparent"
|
| 840 |
+
CornerRadius="8">
|
| 841 |
+
<TextBlock Text=""
|
| 842 |
+
FontFamily="Segoe MDL2 Assets"
|
| 843 |
+
FontSize="10"
|
| 844 |
+
Foreground="#666"
|
| 845 |
+
HorizontalAlignment="Center"
|
| 846 |
+
VerticalAlignment="Center"/>
|
| 847 |
+
</Border>
|
| 848 |
+
<ControlTemplate.Triggers>
|
| 849 |
+
<Trigger Property="IsMouseOver" Value="True">
|
| 850 |
+
<Setter TargetName="Border" Property="Background" Value="#E0E0E0"/>
|
| 851 |
+
<Setter Property="Foreground" Value="#333"/>
|
| 852 |
+
</Trigger>
|
| 853 |
+
</ControlTemplate.Triggers>
|
| 854 |
+
</ControlTemplate>
|
| 855 |
+
</Setter.Value>
|
| 856 |
+
</Setter>
|
| 857 |
+
</Style>
|
| 858 |
+
</Button.Style>
|
| 859 |
+
</Button>
|
| 860 |
+
</Grid>
|
| 861 |
+
</Border>
|
| 862 |
+
<ControlTemplate.Triggers>
|
| 863 |
+
<!-- 选中状态:更突出的蓝色背景 -->
|
| 864 |
+
<Trigger Property="IsSelected" Value="True">
|
| 865 |
+
<Setter TargetName="Border" Property="Background" Value="#2979FF"/>
|
| 866 |
+
<Setter TargetName="Border" Property="BorderBrush" Value="#2979FF"/>
|
| 867 |
+
</Trigger>
|
| 868 |
+
<!-- 非选中状态的鼠标悬停:中等灰色背景 -->
|
| 869 |
+
<MultiTrigger>
|
| 870 |
+
<MultiTrigger.Conditions>
|
| 871 |
+
<Condition Property="IsMouseOver" Value="True"/>
|
| 872 |
+
<Condition Property="IsSelected" Value="False"/>
|
| 873 |
+
</MultiTrigger.Conditions>
|
| 874 |
+
<Setter TargetName="Border" Property="Background" Value="#3a3a3a"/>
|
| 875 |
+
</MultiTrigger>
|
| 876 |
+
<!-- 选中状态的鼠标悬停:更亮的蓝色背景 -->
|
| 877 |
+
<MultiTrigger>
|
| 878 |
+
<MultiTrigger.Conditions>
|
| 879 |
+
<Condition Property="IsMouseOver" Value="True"/>
|
| 880 |
+
<Condition Property="IsSelected" Value="True"/>
|
| 881 |
+
</MultiTrigger.Conditions>
|
| 882 |
+
<Setter TargetName="Border" Property="Background" Value="#4A90E2"/>
|
| 883 |
+
</MultiTrigger>
|
| 884 |
+
</ControlTemplate.Triggers>
|
| 885 |
+
</ControlTemplate>
|
| 886 |
+
</Setter.Value>
|
| 887 |
+
</Setter>
|
| 888 |
+
</Style>
|
| 889 |
+
</TabControl.ItemContainerStyle>
|
| 890 |
+
<TabControl.ContextMenu>
|
| 891 |
+
<ContextMenu x:Name="TabContextMenu"
|
| 892 |
+
Placement="Custom"
|
| 893 |
+
CustomPopupPlacementCallback="ContextMenu_PlacementCallback">
|
| 894 |
+
<MenuItem Header="刷新" Click="RefreshTabMenuItem_Click"/>
|
| 895 |
+
<MenuItem Header="复制网址" Click="CopyUrlMenuItem_Click"/>
|
| 896 |
+
<MenuItem Header="复制标签页" Click="DuplicateTabMenuItem_Click"/>
|
| 897 |
+
<Separator/>
|
| 898 |
+
<MenuItem Header="粘贴并访问" Click="PasteAndGoMenuItem_Click"/>
|
| 899 |
+
<Separator/>
|
| 900 |
+
<MenuItem Header="关闭标签页" Click="CloseTabMenuItem_Click"/>
|
| 901 |
+
<MenuItem Header="关闭其他标签页" Click="CloseOtherTabsMenuItem_Click"/>
|
| 902 |
+
</ContextMenu>
|
| 903 |
+
</TabControl.ContextMenu>
|
| 904 |
+
<TabControl.ContentTemplate>
|
| 905 |
+
<DataTemplate>
|
| 906 |
+
<Grid>
|
| 907 |
+
<ContentPresenter Content="{Binding}"/>
|
| 908 |
+
<controls:LoadingControl IsLoading="{Binding IsLoading}"/>
|
| 909 |
+
</Grid>
|
| 910 |
+
</DataTemplate>
|
| 911 |
+
</TabControl.ContentTemplate>
|
| 912 |
+
</TabControl>
|
| 913 |
+
</Grid>
|
| 914 |
+
</Grid>
|
| 915 |
+
</Grid>
|
| 916 |
+
</Window>
|
csharp/BrowserTool/BrowserTool/MainWindow.xaml.cs
ADDED
|
@@ -0,0 +1,2413 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
using System;
|
| 2 |
+
using System.Collections.Generic;
|
| 3 |
+
using System.Collections.ObjectModel;
|
| 4 |
+
using System.ComponentModel;
|
| 5 |
+
using System.Configuration;
|
| 6 |
+
using System.Diagnostics;
|
| 7 |
+
using System.IO;
|
| 8 |
+
using System.Linq;
|
| 9 |
+
using System.Runtime.CompilerServices;
|
| 10 |
+
using System.Runtime.InteropServices;
|
| 11 |
+
using System.Security.Policy;
|
| 12 |
+
using System.Text;
|
| 13 |
+
using System.Text.RegularExpressions;
|
| 14 |
+
using System.Threading.Tasks;
|
| 15 |
+
using System.Windows;
|
| 16 |
+
using System.Windows.Controls;
|
| 17 |
+
using System.Windows.Controls.Primitives;
|
| 18 |
+
using System.Windows.Data;
|
| 19 |
+
using System.Windows.Documents;
|
| 20 |
+
using System.Windows.Input;
|
| 21 |
+
using System.Windows.Interop;
|
| 22 |
+
using System.Windows.Media;
|
| 23 |
+
using System.Windows.Media.Animation;
|
| 24 |
+
using System.Windows.Media.Imaging;
|
| 25 |
+
using System.Windows.Navigation;
|
| 26 |
+
using System.Windows.Shapes;
|
| 27 |
+
using System.Windows.Threading;
|
| 28 |
+
using BrowserTool.Browser;
|
| 29 |
+
using BrowserTool.Database;
|
| 30 |
+
using BrowserTool.Database.Entities;
|
| 31 |
+
using BrowserTool.Utils;
|
| 32 |
+
using CefSharp;
|
| 33 |
+
using CefSharp.Wpf;
|
| 34 |
+
using Hardcodet.Wpf.TaskbarNotification;
|
| 35 |
+
using NLog;
|
| 36 |
+
|
| 37 |
+
namespace BrowserTool;
|
| 38 |
+
|
| 39 |
+
/// <summary>
|
| 40 |
+
/// 标签页信息类,用于存储标签页的URL和ID
|
| 41 |
+
/// </summary>
|
| 42 |
+
public sealed record TabInfo
|
| 43 |
+
{
|
| 44 |
+
/// <summary>
|
| 45 |
+
/// 获取或设置当前URL
|
| 46 |
+
/// </summary>
|
| 47 |
+
public string Url { get; set; } = string.Empty;
|
| 48 |
+
|
| 49 |
+
/// <summary>
|
| 50 |
+
/// 获取或设置标签页唯一标识符
|
| 51 |
+
/// </summary>
|
| 52 |
+
public string TabId { get; set; } = string.Empty;
|
| 53 |
+
|
| 54 |
+
/// <summary>
|
| 55 |
+
/// 获取或设置原始URL,用于标识标签页来源的菜单项
|
| 56 |
+
/// </summary>
|
| 57 |
+
public string OriginalUrl { get; set; } = string.Empty;
|
| 58 |
+
|
| 59 |
+
/// <summary>
|
| 60 |
+
/// 获取或设置菜单项ID,用于唯一标识来源的二级菜单
|
| 61 |
+
/// </summary>
|
| 62 |
+
public int? MenuItemId { get; set; }
|
| 63 |
+
|
| 64 |
+
/// <summary>
|
| 65 |
+
/// 获取或设置菜单项标题,用于显示和调试
|
| 66 |
+
/// </summary>
|
| 67 |
+
public string MenuItemTitle { get; set; } = string.Empty;
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
+
/// <summary>
|
| 71 |
+
/// MainWindow.xaml 的交互逻辑类
|
| 72 |
+
/// </summary>
|
| 73 |
+
public partial class MainWindow : Window
|
| 74 |
+
{
|
| 75 |
+
#region 嵌套类型定义
|
| 76 |
+
|
| 77 |
+
/// <summary>
|
| 78 |
+
/// 菜单组数据结构
|
| 79 |
+
/// </summary>
|
| 80 |
+
public sealed record MenuGroup
|
| 81 |
+
{
|
| 82 |
+
/// <summary>
|
| 83 |
+
/// 获取或设置组名称
|
| 84 |
+
/// </summary>
|
| 85 |
+
public string GroupName { get; set; } = string.Empty;
|
| 86 |
+
|
| 87 |
+
/// <summary>
|
| 88 |
+
/// 获取或设置是否默认展开
|
| 89 |
+
/// </summary>
|
| 90 |
+
public bool IsDefaultExpanded { get; set; }
|
| 91 |
+
|
| 92 |
+
/// <summary>
|
| 93 |
+
/// 获取或设置菜单项列表
|
| 94 |
+
/// </summary>
|
| 95 |
+
public List<MenuItemData> Items { get; set; } = new();
|
| 96 |
+
}
|
| 97 |
+
|
| 98 |
+
/// <summary>
|
| 99 |
+
/// 菜单项数据结构
|
| 100 |
+
/// </summary>
|
| 101 |
+
public sealed record MenuItemData
|
| 102 |
+
{
|
| 103 |
+
/// <summary>
|
| 104 |
+
/// 获取或设置菜单项的数据库ID
|
| 105 |
+
/// </summary>
|
| 106 |
+
public int Id { get; set; }
|
| 107 |
+
|
| 108 |
+
/// <summary>
|
| 109 |
+
/// 获取或设置菜单项名称
|
| 110 |
+
/// </summary>
|
| 111 |
+
public string Name { get; set; } = string.Empty;
|
| 112 |
+
|
| 113 |
+
/// <summary>
|
| 114 |
+
/// 获取或设置菜单项URL
|
| 115 |
+
/// </summary>
|
| 116 |
+
public string Url { get; set; } = string.Empty;
|
| 117 |
+
|
| 118 |
+
/// <summary>
|
| 119 |
+
/// 获取或设置菜单项图标
|
| 120 |
+
/// </summary>
|
| 121 |
+
public string Icon { get; set; } = string.Empty;
|
| 122 |
+
}
|
| 123 |
+
|
| 124 |
+
/// <summary>
|
| 125 |
+
/// 浏览器上下文类,实现属性变更通知
|
| 126 |
+
/// </summary>
|
| 127 |
+
public sealed class BrowserContext : INotifyPropertyChanged
|
| 128 |
+
{
|
| 129 |
+
private bool _isLoading;
|
| 130 |
+
|
| 131 |
+
/// <summary>
|
| 132 |
+
/// 获取或设置是否正在加载
|
| 133 |
+
/// </summary>
|
| 134 |
+
public bool IsLoading
|
| 135 |
+
{
|
| 136 |
+
get => _isLoading;
|
| 137 |
+
set
|
| 138 |
+
{
|
| 139 |
+
if (_isLoading != value)
|
| 140 |
+
{
|
| 141 |
+
_isLoading = value;
|
| 142 |
+
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(IsLoading)));
|
| 143 |
+
}
|
| 144 |
+
}
|
| 145 |
+
}
|
| 146 |
+
|
| 147 |
+
/// <summary>
|
| 148 |
+
/// 属性变更通知事件
|
| 149 |
+
/// </summary>
|
| 150 |
+
public event PropertyChangedEventHandler? PropertyChanged;
|
| 151 |
+
}
|
| 152 |
+
|
| 153 |
+
|
| 154 |
+
|
| 155 |
+
#endregion
|
| 156 |
+
|
| 157 |
+
#region 字段和属性
|
| 158 |
+
|
| 159 |
+
/// <summary>
|
| 160 |
+
/// 菜单数据集合
|
| 161 |
+
/// </summary>
|
| 162 |
+
private readonly List<MenuGroup> _menuGroups = new();
|
| 163 |
+
|
| 164 |
+
/// <summary>
|
| 165 |
+
/// 抽屉是否打开
|
| 166 |
+
/// </summary>
|
| 167 |
+
private bool _isDrawerOpen = true;
|
| 168 |
+
|
| 169 |
+
/// <summary>
|
| 170 |
+
/// 最后的窗口状态
|
| 171 |
+
/// </summary>
|
| 172 |
+
private WindowState _lastWindowState = WindowState.Normal;
|
| 173 |
+
|
| 174 |
+
/// <summary>
|
| 175 |
+
/// 恢复边界
|
| 176 |
+
/// </summary>
|
| 177 |
+
private Rect _restoreBounds;
|
| 178 |
+
|
| 179 |
+
/// <summary>
|
| 180 |
+
/// 日志记录器
|
| 181 |
+
/// </summary>
|
| 182 |
+
private static readonly Logger _logger = LogManager.GetCurrentClassLogger();
|
| 183 |
+
|
| 184 |
+
/// <summary>
|
| 185 |
+
/// Windows消息常量
|
| 186 |
+
/// </summary>
|
| 187 |
+
private static readonly int WM_SHOWMAINWINDOW = RegisterWindowMessage("BrowserTool_ShowMainWindow");
|
| 188 |
+
|
| 189 |
+
/// <summary>
|
| 190 |
+
/// 获取抽屉是否打开状态
|
| 191 |
+
/// </summary>
|
| 192 |
+
public bool IsDrawerOpen => _isDrawerOpen;
|
| 193 |
+
|
| 194 |
+
#endregion
|
| 195 |
+
|
| 196 |
+
#region Windows API 声明
|
| 197 |
+
|
| 198 |
+
/// <summary>
|
| 199 |
+
/// 注册自定义Windows消息
|
| 200 |
+
/// </summary>
|
| 201 |
+
/// <param name="lpString">消息字符串</param>
|
| 202 |
+
/// <returns>消息ID</returns>
|
| 203 |
+
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
|
| 204 |
+
private static extern int RegisterWindowMessage(string lpString);
|
| 205 |
+
|
| 206 |
+
#endregion
|
| 207 |
+
|
| 208 |
+
#region 构造函数
|
| 209 |
+
|
| 210 |
+
/// <summary>
|
| 211 |
+
/// 初始化MainWindow的新实例
|
| 212 |
+
/// </summary>
|
| 213 |
+
public MainWindow()
|
| 214 |
+
{
|
| 215 |
+
try
|
| 216 |
+
{
|
| 217 |
+
InitializeComponent();
|
| 218 |
+
InitializeMainWindow();
|
| 219 |
+
}
|
| 220 |
+
catch (Exception ex)
|
| 221 |
+
{
|
| 222 |
+
_logger.Error("初始化主窗口时发生异常", ex);
|
| 223 |
+
Application.Current.Shutdown();
|
| 224 |
+
}
|
| 225 |
+
}
|
| 226 |
+
|
| 227 |
+
/// <summary>
|
| 228 |
+
/// 初始化主窗口设置
|
| 229 |
+
/// </summary>
|
| 230 |
+
private void InitializeMainWindow()
|
| 231 |
+
{
|
| 232 |
+
// 监听标签页切换
|
| 233 |
+
MainTabControl.SelectionChanged += MainTabControl_SelectionChanged;
|
| 234 |
+
|
| 235 |
+
// 窗口居中显示
|
| 236 |
+
WindowStartupLocation = WindowStartupLocation.CenterScreen;
|
| 237 |
+
|
| 238 |
+
// 订阅数据变更事件
|
| 239 |
+
SiteConfig.DataChanged += OnDataChanged;
|
| 240 |
+
|
| 241 |
+
// 添加Windows消息处理
|
| 242 |
+
SourceInitialized += MainWindow_SourceInitialized;
|
| 243 |
+
|
| 244 |
+
// 订阅登录状态变化事件
|
| 245 |
+
LoginManager.OnLoginStatusChanged += OnLoginStatusChanged;
|
| 246 |
+
|
| 247 |
+
// 设置ContextMenu的PlacementTarget
|
| 248 |
+
TabContextMenu.Opened += (s, e) =>
|
| 249 |
+
{
|
| 250 |
+
if (TabContextMenu.PlacementTarget == null)
|
| 251 |
+
{
|
| 252 |
+
TabContextMenu.PlacementTarget = MainTabControl;
|
| 253 |
+
}
|
| 254 |
+
};
|
| 255 |
+
|
| 256 |
+
// 根据配置控制手动打卡按钮的显示
|
| 257 |
+
bool showManualCheckInButton = true;
|
| 258 |
+
if (bool.TryParse(ConfigurationManager.AppSettings["ShowManualCheckInButton"], out bool configValue))
|
| 259 |
+
{
|
| 260 |
+
showManualCheckInButton = configValue;
|
| 261 |
+
}
|
| 262 |
+
ManualCheckInButton.Visibility = showManualCheckInButton ? Visibility.Visible : Visibility.Collapsed;
|
| 263 |
+
}
|
| 264 |
+
|
| 265 |
+
#endregion
|
| 266 |
+
|
| 267 |
+
#region Windows 消息处理
|
| 268 |
+
|
| 269 |
+
/// <summary>
|
| 270 |
+
/// 窗口源初始化事件处理器
|
| 271 |
+
/// </summary>
|
| 272 |
+
private void MainWindow_SourceInitialized(object sender, EventArgs e)
|
| 273 |
+
{
|
| 274 |
+
var source = HwndSource.FromHwnd(new WindowInteropHelper(this).Handle);
|
| 275 |
+
source.AddHook(WndProc);
|
| 276 |
+
}
|
| 277 |
+
|
| 278 |
+
/// <summary>
|
| 279 |
+
/// Windows消息处理程序
|
| 280 |
+
/// </summary>
|
| 281 |
+
/// <param name="hwnd">窗口句柄</param>
|
| 282 |
+
/// <param name="msg">消息类型</param>
|
| 283 |
+
/// <param name="wParam">消息参数</param>
|
| 284 |
+
/// <param name="lParam">消息参数</param>
|
| 285 |
+
/// <param name="handled">是否已处理</param>
|
| 286 |
+
/// <returns>处理结果</returns>
|
| 287 |
+
private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
|
| 288 |
+
{
|
| 289 |
+
if (msg == WM_SHOWMAINWINDOW)
|
| 290 |
+
{
|
| 291 |
+
_logger.Debug($"收到WM_SHOWMAINWINDOW消息: {msg}");
|
| 292 |
+
|
| 293 |
+
Dispatcher.BeginInvoke(new Action(() =>
|
| 294 |
+
{
|
| 295 |
+
var app = App.GetCurrentApp();
|
| 296 |
+
if (app is not null)
|
| 297 |
+
{
|
| 298 |
+
_logger.Debug("调用App.ShowMainWindow");
|
| 299 |
+
app.ShowMainWindow();
|
| 300 |
+
}
|
| 301 |
+
else
|
| 302 |
+
{
|
| 303 |
+
_logger.Debug("App实例为null");
|
| 304 |
+
}
|
| 305 |
+
}));
|
| 306 |
+
|
| 307 |
+
handled = true;
|
| 308 |
+
}
|
| 309 |
+
|
| 310 |
+
return IntPtr.Zero;
|
| 311 |
+
}
|
| 312 |
+
|
| 313 |
+
#endregion
|
| 314 |
+
|
| 315 |
+
#region 数据变更处理
|
| 316 |
+
|
| 317 |
+
/// <summary>
|
| 318 |
+
/// 数据变更事件处理器
|
| 319 |
+
/// </summary>
|
| 320 |
+
private void OnDataChanged(object sender, EventArgs e) =>
|
| 321 |
+
Dispatcher.Invoke(LoadMenuGroupsFromDb);
|
| 322 |
+
|
| 323 |
+
/// <summary>
|
| 324 |
+
/// 登录状态变化事件处理器
|
| 325 |
+
/// </summary>
|
| 326 |
+
/// <param name="isLoggedIn">是否已登录</param>
|
| 327 |
+
private void OnLoginStatusChanged(bool isLoggedIn)
|
| 328 |
+
{
|
| 329 |
+
Dispatcher.BeginInvoke(new Action(() =>
|
| 330 |
+
{
|
| 331 |
+
if (isLoggedIn)
|
| 332 |
+
{
|
| 333 |
+
RefreshMenuFromSettings();
|
| 334 |
+
}
|
| 335 |
+
}));
|
| 336 |
+
}
|
| 337 |
+
|
| 338 |
+
#endregion
|
| 339 |
+
|
| 340 |
+
#region 资源清理
|
| 341 |
+
|
| 342 |
+
/// <summary>
|
| 343 |
+
/// 窗口关闭时的清理工作
|
| 344 |
+
/// </summary>
|
| 345 |
+
protected override void OnClosed(EventArgs e)
|
| 346 |
+
{
|
| 347 |
+
base.OnClosed(e);
|
| 348 |
+
SiteConfig.DataChanged -= OnDataChanged;
|
| 349 |
+
}
|
| 350 |
+
|
| 351 |
+
#endregion
|
| 352 |
+
|
| 353 |
+
#region 菜单管理
|
| 354 |
+
|
| 355 |
+
/// <summary>
|
| 356 |
+
/// 从数据库加载菜单组数据
|
| 357 |
+
/// </summary>
|
| 358 |
+
private void LoadMenuGroupsFromDb()
|
| 359 |
+
{
|
| 360 |
+
try
|
| 361 |
+
{
|
| 362 |
+
SiteConfig.InitializeDatabase();
|
| 363 |
+
var allGroups = SiteConfig.GetAllGroups();
|
| 364 |
+
|
| 365 |
+
_logger.Debug($"加载到 {allGroups.Count} 个分组");
|
| 366 |
+
|
| 367 |
+
_menuGroups.Clear();
|
| 368 |
+
_menuGroups.AddRange(allGroups.Select(g => new MenuGroup
|
| 369 |
+
{
|
| 370 |
+
GroupName = g.Name,
|
| 371 |
+
IsDefaultExpanded = g.IsDefaultExpanded,
|
| 372 |
+
Items = g.Sites
|
| 373 |
+
.Where(s => s.IsEnabled)
|
| 374 |
+
.OrderBy(s => s.SortOrder)
|
| 375 |
+
.Select(s => new MenuItemData
|
| 376 |
+
{
|
| 377 |
+
Id = s.Id,
|
| 378 |
+
Name = s.DisplayName,
|
| 379 |
+
Url = s.Url,
|
| 380 |
+
Icon = s.Icon
|
| 381 |
+
}).ToList()
|
| 382 |
+
}));
|
| 383 |
+
|
| 384 |
+
RefreshMenuTreeUI();
|
| 385 |
+
}
|
| 386 |
+
catch (Exception ex)
|
| 387 |
+
{
|
| 388 |
+
_logger.Error("加载菜单数据时出错", ex);
|
| 389 |
+
MessageBox.Show($"加载菜单数据时出错:{ex.Message}", "错误",
|
| 390 |
+
MessageBoxButton.OK, MessageBoxImage.Error);
|
| 391 |
+
}
|
| 392 |
+
}
|
| 393 |
+
|
| 394 |
+
/// <summary>
|
| 395 |
+
/// 刷新菜单树UI
|
| 396 |
+
/// </summary>
|
| 397 |
+
private void RefreshMenuTreeUI()
|
| 398 |
+
{
|
| 399 |
+
if (MenuTree is null)
|
| 400 |
+
{
|
| 401 |
+
return;
|
| 402 |
+
}
|
| 403 |
+
|
| 404 |
+
MenuTree.Items.Clear();
|
| 405 |
+
|
| 406 |
+
foreach (var group in _menuGroups)
|
| 407 |
+
{
|
| 408 |
+
var groupItem = new TreeViewItem
|
| 409 |
+
{
|
| 410 |
+
Header = group.GroupName,
|
| 411 |
+
IsExpanded = group.IsDefaultExpanded
|
| 412 |
+
};
|
| 413 |
+
|
| 414 |
+
foreach (var item in group.Items)
|
| 415 |
+
{
|
| 416 |
+
var stackPanel = CreateMenuItemPanel(item);
|
| 417 |
+
var subItem = new TreeViewItem { Header = stackPanel, Tag = item };
|
| 418 |
+
groupItem.Items.Add(subItem);
|
| 419 |
+
}
|
| 420 |
+
|
| 421 |
+
MenuTree.Items.Add(groupItem);
|
| 422 |
+
}
|
| 423 |
+
|
| 424 |
+
_logger.Debug($"UI 更新完成,共添加 {MenuTree.Items.Count} 个分组");
|
| 425 |
+
}
|
| 426 |
+
|
| 427 |
+
/// <summary>
|
| 428 |
+
/// 创建菜单项面板
|
| 429 |
+
/// </summary>
|
| 430 |
+
/// <param name="item">菜单项数据</param>
|
| 431 |
+
/// <returns>菜单项面板</returns>
|
| 432 |
+
private StackPanel CreateMenuItemPanel(MenuItemData item)
|
| 433 |
+
{
|
| 434 |
+
var stackPanel = new StackPanel { Orientation = Orientation.Horizontal };
|
| 435 |
+
|
| 436 |
+
if (!string.IsNullOrEmpty(item.Icon))
|
| 437 |
+
{
|
| 438 |
+
var image = CreateMenuItemIcon(item.Icon);
|
| 439 |
+
if (image is not null)
|
| 440 |
+
{
|
| 441 |
+
stackPanel.Children.Add(image);
|
| 442 |
+
}
|
| 443 |
+
}
|
| 444 |
+
|
| 445 |
+
var textBlock = new TextBlock
|
| 446 |
+
{
|
| 447 |
+
Text = item.Name,
|
| 448 |
+
VerticalAlignment = VerticalAlignment.Center,
|
| 449 |
+
Foreground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#CCCCCC"))
|
| 450 |
+
};
|
| 451 |
+
|
| 452 |
+
stackPanel.Children.Add(textBlock);
|
| 453 |
+
return stackPanel;
|
| 454 |
+
}
|
| 455 |
+
|
| 456 |
+
/// <summary>
|
| 457 |
+
/// 创建菜单项图标
|
| 458 |
+
/// </summary>
|
| 459 |
+
/// <param name="iconPath">图标路径</param>
|
| 460 |
+
/// <returns>图像控件,如果创建失败则返回null</returns>
|
| 461 |
+
private Image? CreateMenuItemIcon(string iconPath)
|
| 462 |
+
{
|
| 463 |
+
try
|
| 464 |
+
{
|
| 465 |
+
var image = new Image
|
| 466 |
+
{
|
| 467 |
+
Width = 16,
|
| 468 |
+
Height = 16,
|
| 469 |
+
Margin = new Thickness(0, 0, 5, 0)
|
| 470 |
+
};
|
| 471 |
+
|
| 472 |
+
var bitmap = Utils.ImageCache.GetImage(iconPath);
|
| 473 |
+
if (bitmap is not null)
|
| 474 |
+
{
|
| 475 |
+
image.Source = bitmap;
|
| 476 |
+
return image;
|
| 477 |
+
}
|
| 478 |
+
}
|
| 479 |
+
catch (Exception ex)
|
| 480 |
+
{
|
| 481 |
+
_logger.Error("加载图标时出错", ex);
|
| 482 |
+
}
|
| 483 |
+
|
| 484 |
+
return null;
|
| 485 |
+
}
|
| 486 |
+
|
| 487 |
+
/// <summary>
|
| 488 |
+
/// 初始化左侧菜单树,支持可选过滤
|
| 489 |
+
/// </summary>
|
| 490 |
+
/// <param name="filter">过滤条件</param>
|
| 491 |
+
private void InitMenuTree(string filter = "")
|
| 492 |
+
{
|
| 493 |
+
MenuTree.Items.Clear();
|
| 494 |
+
|
| 495 |
+
foreach (var group in _menuGroups)
|
| 496 |
+
{
|
| 497 |
+
var filteredItems = string.IsNullOrWhiteSpace(filter)
|
| 498 |
+
? group.Items
|
| 499 |
+
: group.Items.Where(i => i.Name.Contains(filter, StringComparison.OrdinalIgnoreCase)).ToList();
|
| 500 |
+
|
| 501 |
+
if (filteredItems.Count == 0 && !string.IsNullOrWhiteSpace(filter))
|
| 502 |
+
{
|
| 503 |
+
continue;
|
| 504 |
+
}
|
| 505 |
+
|
| 506 |
+
var groupItem = new TreeViewItem
|
| 507 |
+
{
|
| 508 |
+
Header = group.GroupName,
|
| 509 |
+
IsExpanded = group.IsDefaultExpanded
|
| 510 |
+
};
|
| 511 |
+
|
| 512 |
+
foreach (var item in filteredItems)
|
| 513 |
+
{
|
| 514 |
+
var stackPanel = CreateMenuItemPanel(item);
|
| 515 |
+
var subItem = new TreeViewItem { Header = stackPanel, Tag = item };
|
| 516 |
+
groupItem.Items.Add(subItem);
|
| 517 |
+
}
|
| 518 |
+
|
| 519 |
+
MenuTree.Items.Add(groupItem);
|
| 520 |
+
}
|
| 521 |
+
}
|
| 522 |
+
|
| 523 |
+
/// <summary>
|
| 524 |
+
/// 刷新主窗口菜单,可以被设置窗口直接调用
|
| 525 |
+
/// </summary>
|
| 526 |
+
public void RefreshMenuFromSettings()
|
| 527 |
+
{
|
| 528 |
+
Dispatcher.BeginInvoke(new Action(() =>
|
| 529 |
+
{
|
| 530 |
+
try
|
| 531 |
+
{
|
| 532 |
+
_logger.Debug("[MainWindow.RefreshMenuFromSettings] 开始重新加载菜单");
|
| 533 |
+
LoadMenuGroupsFromDb();
|
| 534 |
+
MenuTree.Items.Refresh();
|
| 535 |
+
_logger.Debug("[MainWindow.RefreshMenuFromSettings] 菜单刷新完成");
|
| 536 |
+
}
|
| 537 |
+
catch (Exception ex)
|
| 538 |
+
{
|
| 539 |
+
_logger.Error("[MainWindow.RefreshMenuFromSettings] 刷新菜单时出错", ex);
|
| 540 |
+
MessageBox.Show($"刷新菜单时出错:{ex.Message}", "错误",
|
| 541 |
+
MessageBoxButton.OK, MessageBoxImage.Error);
|
| 542 |
+
}
|
| 543 |
+
}), DispatcherPriority.Render);
|
| 544 |
+
}
|
| 545 |
+
|
| 546 |
+
#endregion
|
| 547 |
+
|
| 548 |
+
#region 搜索功能
|
| 549 |
+
|
| 550 |
+
/// <summary>
|
| 551 |
+
/// 搜索框文本变更事件处理器
|
| 552 |
+
/// </summary>
|
| 553 |
+
private void SearchBox_TextChanged(object sender, TextChangedEventArgs e) =>
|
| 554 |
+
InitMenuTree(SearchBox.Text.Trim());
|
| 555 |
+
|
| 556 |
+
#endregion
|
| 557 |
+
|
| 558 |
+
#region 菜单事件处理
|
| 559 |
+
|
| 560 |
+
/// <summary>
|
| 561 |
+
/// 菜单项选择变更事件处理器
|
| 562 |
+
/// </summary>
|
| 563 |
+
private void MenuTree_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
|
| 564 |
+
{
|
| 565 |
+
if (MenuTree.SelectedItem is not TreeViewItem selected || selected.Tag is not MenuItemData data)
|
| 566 |
+
{
|
| 567 |
+
return;
|
| 568 |
+
}
|
| 569 |
+
|
| 570 |
+
var parentItem = selected.Parent as TreeViewItem;
|
| 571 |
+
var parentName = parentItem?.Header?.ToString() ?? "";
|
| 572 |
+
var combinedTitle = string.IsNullOrEmpty(parentName) ? data.Name : $"{parentName}-{data.Name}";
|
| 573 |
+
|
| 574 |
+
OpenUrlInTab(combinedTitle, data.Url, true, false, data.Id, data.Name);
|
| 575 |
+
}
|
| 576 |
+
|
| 577 |
+
/// <summary>
|
| 578 |
+
/// TreeView双击事件处理器,实现一级菜单的展开/收缩
|
| 579 |
+
/// </summary>
|
| 580 |
+
private void MenuTree_MouseDoubleClick(object sender, MouseButtonEventArgs e)
|
| 581 |
+
{
|
| 582 |
+
_logger.Debug("[双击事件] MouseDoubleClick 被触发");
|
| 583 |
+
|
| 584 |
+
var clickedItem = FindClickedTreeViewItem(e.OriginalSource as DependencyObject);
|
| 585 |
+
if (clickedItem is null)
|
| 586 |
+
{
|
| 587 |
+
_logger.Debug("[双击事件] 未找到TreeViewItem");
|
| 588 |
+
return;
|
| 589 |
+
}
|
| 590 |
+
|
| 591 |
+
var isTopLevel = IsTopLevelTreeViewItem(clickedItem);
|
| 592 |
+
_logger.Debug($"[双击检测] 项目: {clickedItem.Header}, 是否顶级: {isTopLevel}");
|
| 593 |
+
|
| 594 |
+
if (isTopLevel)
|
| 595 |
+
{
|
| 596 |
+
var currentState = clickedItem.IsExpanded;
|
| 597 |
+
_logger.Debug($"[一级菜单双击] {clickedItem.Header} - 当前展开状态: {currentState}");
|
| 598 |
+
|
| 599 |
+
clickedItem.IsExpanded = !currentState;
|
| 600 |
+
_logger.Debug($"[一级菜单双击] {clickedItem.Header} - 新展开状态: {clickedItem.IsExpanded}");
|
| 601 |
+
|
| 602 |
+
e.Handled = true;
|
| 603 |
+
}
|
| 604 |
+
else
|
| 605 |
+
{
|
| 606 |
+
_logger.Debug("[二级菜单双击] 忽略处理");
|
| 607 |
+
}
|
| 608 |
+
}
|
| 609 |
+
|
| 610 |
+
/// <summary>
|
| 611 |
+
/// 查找被点击的TreeViewItem
|
| 612 |
+
/// </summary>
|
| 613 |
+
/// <param name="hitTest">命中测试对象</param>
|
| 614 |
+
/// <returns>TreeViewItem或null</returns>
|
| 615 |
+
private static TreeViewItem? FindClickedTreeViewItem(DependencyObject? hitTest)
|
| 616 |
+
{
|
| 617 |
+
while (hitTest is not null)
|
| 618 |
+
{
|
| 619 |
+
if (hitTest is TreeViewItem treeViewItem)
|
| 620 |
+
{
|
| 621 |
+
return treeViewItem;
|
| 622 |
+
}
|
| 623 |
+
hitTest = VisualTreeHelper.GetParent(hitTest);
|
| 624 |
+
}
|
| 625 |
+
return null;
|
| 626 |
+
}
|
| 627 |
+
|
| 628 |
+
/// <summary>
|
| 629 |
+
/// 判断是否为顶级TreeViewItem
|
| 630 |
+
/// </summary>
|
| 631 |
+
/// <param name="item">TreeViewItem</param>
|
| 632 |
+
/// <returns>是否为顶级项</returns>
|
| 633 |
+
private bool IsTopLevelTreeViewItem(TreeViewItem item)
|
| 634 |
+
{
|
| 635 |
+
var parent = VisualTreeHelper.GetParent(item);
|
| 636 |
+
while (parent is not null)
|
| 637 |
+
{
|
| 638 |
+
if (parent == MenuTree)
|
| 639 |
+
{
|
| 640 |
+
return true;
|
| 641 |
+
}
|
| 642 |
+
if (parent is TreeViewItem)
|
| 643 |
+
{
|
| 644 |
+
return false;
|
| 645 |
+
}
|
| 646 |
+
parent = VisualTreeHelper.GetParent(parent);
|
| 647 |
+
}
|
| 648 |
+
return false;
|
| 649 |
+
}
|
| 650 |
+
|
| 651 |
+
#endregion
|
| 652 |
+
|
| 653 |
+
#region 标签页管理
|
| 654 |
+
|
| 655 |
+
/// <summary>
|
| 656 |
+
/// 在标签页中打开网址
|
| 657 |
+
/// </summary>
|
| 658 |
+
/// <param name="title">标签页标题</param>
|
| 659 |
+
/// <param name="url">要打开的URL</param>
|
| 660 |
+
/// <param name="keepOriginalTitle">是否保持原始标题</param>
|
| 661 |
+
/// <param name="forceReload">是否强制重新加载页面</param>
|
| 662 |
+
/// <param name="menuItemId">菜单项ID</param>
|
| 663 |
+
/// <param name="menuItemTitle">菜单项标题</param>
|
| 664 |
+
/// <param name="selectedNewTab">是否选择新标签页</param>
|
| 665 |
+
/// <returns>创建的标签页对象</returns>
|
| 666 |
+
public TabItem? OpenUrlInTab(string title, string url, bool keepOriginalTitle = false,
|
| 667 |
+
bool forceReload = false, int menuItemId = 0, string? menuItemTitle = null, bool selectedNewTab = true)
|
| 668 |
+
{
|
| 669 |
+
try
|
| 670 |
+
{
|
| 671 |
+
if (string.IsNullOrWhiteSpace(url))
|
| 672 |
+
{
|
| 673 |
+
_logger.Warn("OpenUrlInTab调用失败:URL为空");
|
| 674 |
+
return null;
|
| 675 |
+
}
|
| 676 |
+
|
| 677 |
+
title = string.IsNullOrWhiteSpace(title) ? "新标签页" : title;
|
| 678 |
+
url = NormalizeUrl(url);
|
| 679 |
+
|
| 680 |
+
_logger.Debug($"开始处理OpenUrlInTab - 标题: {title}, URL: {url}, 菜单ID: {menuItemId}, 强制重载: {forceReload}");
|
| 681 |
+
|
| 682 |
+
var existingTab = FindExistingTab(menuItemId, url);
|
| 683 |
+
if (existingTab is not null)
|
| 684 |
+
{
|
| 685 |
+
return HandleExistingTab(existingTab, url, title, forceReload, selectedNewTab, menuItemId);
|
| 686 |
+
}
|
| 687 |
+
|
| 688 |
+
_logger.Info($"创建新标签页 - 标题: {title}, URL: {url}, 菜单ID: {menuItemId}");
|
| 689 |
+
return CreateNewTab(title, url, keepOriginalTitle, menuItemId, menuItemTitle, selectedNewTab);
|
| 690 |
+
}
|
| 691 |
+
catch (Exception ex)
|
| 692 |
+
{
|
| 693 |
+
_logger.Error("OpenUrlInTab发生异常", ex);
|
| 694 |
+
MessageBox.Show($"打开标签页时发生错误: {ex.Message}", "错误",
|
| 695 |
+
MessageBoxButton.OK, MessageBoxImage.Error);
|
| 696 |
+
return null;
|
| 697 |
+
}
|
| 698 |
+
}
|
| 699 |
+
|
| 700 |
+
/// <summary>
|
| 701 |
+
/// 查找已存在的标签页
|
| 702 |
+
/// </summary>
|
| 703 |
+
/// <param name="menuItemId">菜单项ID</param>
|
| 704 |
+
/// <param name="url">URL</param>
|
| 705 |
+
/// <returns>已存在的标签页或null</returns>
|
| 706 |
+
private TabItem? FindExistingTab(int menuItemId, string url)
|
| 707 |
+
{
|
| 708 |
+
foreach (TabItem tab in MainTabControl.Items)
|
| 709 |
+
{
|
| 710 |
+
if (tab.Tag is not TabInfo tabInfo)
|
| 711 |
+
{
|
| 712 |
+
continue;
|
| 713 |
+
}
|
| 714 |
+
|
| 715 |
+
if (menuItemId > 0 && tabInfo.MenuItemId == menuItemId)
|
| 716 |
+
{
|
| 717 |
+
return tab;
|
| 718 |
+
}
|
| 719 |
+
|
| 720 |
+
if (menuItemId == 0 && tabInfo.OriginalUrl == url)
|
| 721 |
+
{
|
| 722 |
+
return tab;
|
| 723 |
+
}
|
| 724 |
+
}
|
| 725 |
+
return null;
|
| 726 |
+
}
|
| 727 |
+
|
| 728 |
+
/// <summary>
|
| 729 |
+
/// 处理已存在的标签页
|
| 730 |
+
/// </summary>
|
| 731 |
+
/// <param name="tab">已存在的标签页</param>
|
| 732 |
+
/// <param name="url">URL</param>
|
| 733 |
+
/// <param name="title">标题</param>
|
| 734 |
+
/// <param name="forceReload">是否强制重载</param>
|
| 735 |
+
/// <param name="selectedNewTab">是否选择新标签页</param>
|
| 736 |
+
/// <param name="menuItemId">菜单项ID</param>
|
| 737 |
+
/// <returns>处理后的标签页</returns>
|
| 738 |
+
private TabItem HandleExistingTab(TabItem tab, string url, string title, bool forceReload,
|
| 739 |
+
bool selectedNewTab, int menuItemId)
|
| 740 |
+
{
|
| 741 |
+
if (selectedNewTab)
|
| 742 |
+
{
|
| 743 |
+
MainTabControl.SelectedItem = tab;
|
| 744 |
+
}
|
| 745 |
+
|
| 746 |
+
if (tab.Tag is TabInfo tabInfo)
|
| 747 |
+
{
|
| 748 |
+
var urlChanged = tabInfo.OriginalUrl != url;
|
| 749 |
+
var needReload = forceReload || urlChanged;
|
| 750 |
+
|
| 751 |
+
if (needReload && tab.Content is ChromiumWebBrowser browser)
|
| 752 |
+
{
|
| 753 |
+
tabInfo.Url = url;
|
| 754 |
+
tabInfo.OriginalUrl = url;
|
| 755 |
+
LoadUrlSafely(browser, url);
|
| 756 |
+
_logger.Info($"标签页重新加载 - {title} - {url} (菜单ID: {menuItemId}, URL变化: {urlChanged})");
|
| 757 |
+
}
|
| 758 |
+
else
|
| 759 |
+
{
|
| 760 |
+
_logger.Debug($"标签页切换 - {title} - {url} (菜单ID: {menuItemId})");
|
| 761 |
+
}
|
| 762 |
+
}
|
| 763 |
+
|
| 764 |
+
return tab;
|
| 765 |
+
}
|
| 766 |
+
|
| 767 |
+
/// <summary>
|
| 768 |
+
/// 标准化URL格式
|
| 769 |
+
/// </summary>
|
| 770 |
+
/// <param name="url">原始URL</param>
|
| 771 |
+
/// <returns>标准化后的URL</returns>
|
| 772 |
+
private static string NormalizeUrl(string url)
|
| 773 |
+
{
|
| 774 |
+
if (string.IsNullOrWhiteSpace(url))
|
| 775 |
+
{
|
| 776 |
+
return url;
|
| 777 |
+
}
|
| 778 |
+
|
| 779 |
+
url = url.Trim();
|
| 780 |
+
|
| 781 |
+
if (!url.StartsWith("http://", StringComparison.OrdinalIgnoreCase) &&
|
| 782 |
+
!url.StartsWith("https://", StringComparison.OrdinalIgnoreCase) &&
|
| 783 |
+
!url.StartsWith("about:", StringComparison.OrdinalIgnoreCase) &&
|
| 784 |
+
!url.StartsWith("file://", StringComparison.OrdinalIgnoreCase))
|
| 785 |
+
{
|
| 786 |
+
url = "https://" + url;
|
| 787 |
+
}
|
| 788 |
+
|
| 789 |
+
return url;
|
| 790 |
+
}
|
| 791 |
+
|
| 792 |
+
/// <summary>
|
| 793 |
+
/// 安全地加载URL到浏览器
|
| 794 |
+
/// </summary>
|
| 795 |
+
/// <param name="browser">浏览器实例</param>
|
| 796 |
+
/// <param name="url">要加载的URL</param>
|
| 797 |
+
private void LoadUrlSafely(ChromiumWebBrowser browser, string url)
|
| 798 |
+
{
|
| 799 |
+
try
|
| 800 |
+
{
|
| 801 |
+
if (browser.IsBrowserInitialized)
|
| 802 |
+
{
|
| 803 |
+
browser.Load(url);
|
| 804 |
+
_logger.Debug($"直接加载URL: {url}");
|
| 805 |
+
}
|
| 806 |
+
else
|
| 807 |
+
{
|
| 808 |
+
DependencyPropertyChangedEventHandler? browserInitializedHandler = null;
|
| 809 |
+
browserInitializedHandler = (sender, args) =>
|
| 810 |
+
{
|
| 811 |
+
try
|
| 812 |
+
{
|
| 813 |
+
if (browser.IsBrowserInitialized)
|
| 814 |
+
{
|
| 815 |
+
browser.IsBrowserInitializedChanged -= browserInitializedHandler;
|
| 816 |
+
browser.Load(url);
|
| 817 |
+
_logger.Debug($"浏览器初始化完成后加载URL: {url}");
|
| 818 |
+
}
|
| 819 |
+
}
|
| 820 |
+
catch (Exception ex)
|
| 821 |
+
{
|
| 822 |
+
_logger.Error("延迟加载时发生异常", ex);
|
| 823 |
+
}
|
| 824 |
+
};
|
| 825 |
+
browser.IsBrowserInitializedChanged += browserInitializedHandler;
|
| 826 |
+
}
|
| 827 |
+
}
|
| 828 |
+
catch (Exception ex)
|
| 829 |
+
{
|
| 830 |
+
_logger.Error("加载URL时发生异常", ex);
|
| 831 |
+
throw;
|
| 832 |
+
}
|
| 833 |
+
}
|
| 834 |
+
|
| 835 |
+
/// <summary>
|
| 836 |
+
/// 确保浏览器正确加载URL
|
| 837 |
+
/// </summary>
|
| 838 |
+
/// <param name="browser">浏览器实例</param>
|
| 839 |
+
/// <param name="url">URL</param>
|
| 840 |
+
private void EnsureBrowserLoadsUrl(ChromiumWebBrowser browser, string url)
|
| 841 |
+
{
|
| 842 |
+
try
|
| 843 |
+
{
|
| 844 |
+
if (!browser.IsBrowserInitialized)
|
| 845 |
+
{
|
| 846 |
+
DependencyPropertyChangedEventHandler? browserInitializedHandler = null;
|
| 847 |
+
browserInitializedHandler = (sender, args) =>
|
| 848 |
+
{
|
| 849 |
+
try
|
| 850 |
+
{
|
| 851 |
+
if (browser.IsBrowserInitialized)
|
| 852 |
+
{
|
| 853 |
+
browser.IsBrowserInitializedChanged -= browserInitializedHandler;
|
| 854 |
+
browser.Load(url);
|
| 855 |
+
_logger.Debug($"浏览器初始化完成后加载URL: {url}");
|
| 856 |
+
}
|
| 857 |
+
}
|
| 858 |
+
catch (Exception ex)
|
| 859 |
+
{
|
| 860 |
+
_logger.Error("延迟加载时发生异常", ex);
|
| 861 |
+
}
|
| 862 |
+
};
|
| 863 |
+
browser.IsBrowserInitializedChanged += browserInitializedHandler;
|
| 864 |
+
}
|
| 865 |
+
else
|
| 866 |
+
{
|
| 867 |
+
browser.Load(url);
|
| 868 |
+
_logger.Debug($"直接加载URL: {url}");
|
| 869 |
+
}
|
| 870 |
+
}
|
| 871 |
+
catch (Exception ex)
|
| 872 |
+
{
|
| 873 |
+
_logger.Error("确保浏览器加载URL时发生异常", ex);
|
| 874 |
+
}
|
| 875 |
+
}
|
| 876 |
+
|
| 877 |
+
/// <summary>
|
| 878 |
+
/// 创建新标签页
|
| 879 |
+
/// </summary>
|
| 880 |
+
/// <param name="title">标题</param>
|
| 881 |
+
/// <param name="url">URL</param>
|
| 882 |
+
/// <param name="keepOriginalTitle">是否保持原始标题</param>
|
| 883 |
+
/// <param name="menuItemId">菜单项ID</param>
|
| 884 |
+
/// <param name="menuItemTitle">菜单项标题</param>
|
| 885 |
+
/// <param name="selectedNewTab">是否选择新标签页</param>
|
| 886 |
+
/// <returns>新创建的标签页</returns>
|
| 887 |
+
private TabItem CreateNewTab(string title, string url, bool keepOriginalTitle,
|
| 888 |
+
int menuItemId, string? menuItemTitle, bool selectedNewTab = true)
|
| 889 |
+
{
|
| 890 |
+
try
|
| 891 |
+
{
|
| 892 |
+
_logger.Debug($"创建新标签页 - URL: {url}");
|
| 893 |
+
|
| 894 |
+
var tabId = Guid.NewGuid().ToString();
|
| 895 |
+
var newBrowser = Browser.BrowserInstanceManager.Instance.GetBrowser("", tabId);
|
| 896 |
+
|
| 897 |
+
EnsureBrowserLoadsUrl(newBrowser, url);
|
| 898 |
+
|
| 899 |
+
var browserContext = new BrowserContext { IsLoading = true };
|
| 900 |
+
var newTabInfo = new TabInfo
|
| 901 |
+
{
|
| 902 |
+
Url = url,
|
| 903 |
+
TabId = tabId,
|
| 904 |
+
OriginalUrl = url,
|
| 905 |
+
MenuItemId = menuItemId,
|
| 906 |
+
MenuItemTitle = menuItemTitle ?? string.Empty
|
| 907 |
+
};
|
| 908 |
+
|
| 909 |
+
var tabItem = new TabItem
|
| 910 |
+
{
|
| 911 |
+
Header = title,
|
| 912 |
+
Tag = newTabInfo,
|
| 913 |
+
Content = newBrowser,
|
| 914 |
+
DataContext = browserContext
|
| 915 |
+
};
|
| 916 |
+
|
| 917 |
+
SetupBrowserEventHandlers(newBrowser, tabId, keepOriginalTitle, browserContext);
|
| 918 |
+
SetupAutoLogin(newBrowser, url);
|
| 919 |
+
SetupTabUnloadHandler(tabItem, newBrowser);
|
| 920 |
+
|
| 921 |
+
MainTabControl.Items.Add(tabItem);
|
| 922 |
+
if (selectedNewTab)
|
| 923 |
+
{
|
| 924 |
+
MainTabControl.SelectedItem = tabItem;
|
| 925 |
+
}
|
| 926 |
+
|
| 927 |
+
_logger.Debug($"新标签页创建完成 - 标题: {title}, URL: {url}");
|
| 928 |
+
return tabItem;
|
| 929 |
+
}
|
| 930 |
+
catch (Exception ex)
|
| 931 |
+
{
|
| 932 |
+
_logger.Error("创建新标签页时发生异常", ex);
|
| 933 |
+
throw;
|
| 934 |
+
}
|
| 935 |
+
}
|
| 936 |
+
|
| 937 |
+
/// <summary>
|
| 938 |
+
/// 设置浏览器事件处理器
|
| 939 |
+
/// </summary>
|
| 940 |
+
/// <param name="browser">浏览器实例</param>
|
| 941 |
+
/// <param name="tabId">标签页ID</param>
|
| 942 |
+
/// <param name="keepOriginalTitle">是否保持原始标题</param>
|
| 943 |
+
/// <param name="browserContext">浏览器上下文</param>
|
| 944 |
+
private void SetupBrowserEventHandlers(ChromiumWebBrowser browser, string tabId,
|
| 945 |
+
bool keepOriginalTitle, BrowserContext browserContext)
|
| 946 |
+
{
|
| 947 |
+
EventHandler<FrameLoadEndEventArgs>? titleUpdateHandler = null;
|
| 948 |
+
titleUpdateHandler = (sender, args) =>
|
| 949 |
+
{
|
| 950 |
+
if (keepOriginalTitle || !args.Frame.IsMain)
|
| 951 |
+
{
|
| 952 |
+
return;
|
| 953 |
+
}
|
| 954 |
+
|
| 955 |
+
args.Frame.EvaluateScriptAsync("document.title").ContinueWith(t =>
|
| 956 |
+
{
|
| 957 |
+
if (!t.IsFaulted && t.Result.Success && t.Result.Result is not null)
|
| 958 |
+
{
|
| 959 |
+
var pageTitle = t.Result.Result.ToString();
|
| 960 |
+
if (!string.IsNullOrWhiteSpace(pageTitle))
|
| 961 |
+
{
|
| 962 |
+
UpdateTabTitle(tabId, pageTitle);
|
| 963 |
+
}
|
| 964 |
+
}
|
| 965 |
+
});
|
| 966 |
+
};
|
| 967 |
+
|
| 968 |
+
browser.FrameLoadEnd += titleUpdateHandler;
|
| 969 |
+
|
| 970 |
+
EventHandler<LoadingStateChangedEventArgs>? loadingStateChangedHandler = null;
|
| 971 |
+
loadingStateChangedHandler = (sender, args) =>
|
| 972 |
+
{
|
| 973 |
+
Application.Current.Dispatcher.InvokeAsync(() =>
|
| 974 |
+
{
|
| 975 |
+
if (browserContext is not null)
|
| 976 |
+
{
|
| 977 |
+
browserContext.IsLoading = args.IsLoading;
|
| 978 |
+
}
|
| 979 |
+
}, DispatcherPriority.Background);
|
| 980 |
+
|
| 981 |
+
// 页面加载完成后检查是否为打卡页面
|
| 982 |
+
if (!args.IsLoading)
|
| 983 |
+
{
|
| 984 |
+
Task.Run(async () =>
|
| 985 |
+
{
|
| 986 |
+
await Task.Delay(2000); // 等待页面稳定
|
| 987 |
+
await CheckAndEnqueueCheckInResult(browser);
|
| 988 |
+
});
|
| 989 |
+
}
|
| 990 |
+
};
|
| 991 |
+
|
| 992 |
+
browser.LoadingStateChanged += loadingStateChangedHandler;
|
| 993 |
+
|
| 994 |
+
StoreBrowserEventHandlers(browser, titleUpdateHandler, loadingStateChangedHandler);
|
| 995 |
+
}
|
| 996 |
+
|
| 997 |
+
/// <summary>
|
| 998 |
+
/// 更新标签页标题
|
| 999 |
+
/// </summary>
|
| 1000 |
+
/// <param name="tabId">标签页ID</param>
|
| 1001 |
+
/// <param name="pageTitle">页面标题</param>
|
| 1002 |
+
private void UpdateTabTitle(string tabId, string pageTitle)
|
| 1003 |
+
{
|
| 1004 |
+
Dispatcher.BeginInvoke(new Action(() =>
|
| 1005 |
+
{
|
| 1006 |
+
foreach (TabItem tab in MainTabControl.Items)
|
| 1007 |
+
{
|
| 1008 |
+
if (tab.Tag is TabInfo info && info.TabId == tabId)
|
| 1009 |
+
{
|
| 1010 |
+
tab.Header = pageTitle;
|
| 1011 |
+
_logger.Debug("标签页标题已更新");
|
| 1012 |
+
break;
|
| 1013 |
+
}
|
| 1014 |
+
}
|
| 1015 |
+
}));
|
| 1016 |
+
}
|
| 1017 |
+
|
| 1018 |
+
/// <summary>
|
| 1019 |
+
/// 存储浏览器事件处理器引用
|
| 1020 |
+
/// </summary>
|
| 1021 |
+
/// <param name="browser">浏览器实例</param>
|
| 1022 |
+
/// <param name="titleUpdateHandler">标题更新处理器</param>
|
| 1023 |
+
/// <param name="loadingStateChangedHandler">加载状态变更处理器</param>
|
| 1024 |
+
private static void StoreBrowserEventHandlers(ChromiumWebBrowser browser,
|
| 1025 |
+
EventHandler<FrameLoadEndEventArgs> titleUpdateHandler,
|
| 1026 |
+
EventHandler<LoadingStateChangedEventArgs> loadingStateChangedHandler)
|
| 1027 |
+
{
|
| 1028 |
+
browser.Tag ??= new Dictionary<string, object>();
|
| 1029 |
+
|
| 1030 |
+
if (browser.Tag is Dictionary<string, object> browserTags)
|
| 1031 |
+
{
|
| 1032 |
+
browserTags["titleUpdateHandler"] = titleUpdateHandler;
|
| 1033 |
+
browserTags["loadingStateChangedHandler"] = loadingStateChangedHandler;
|
| 1034 |
+
}
|
| 1035 |
+
}
|
| 1036 |
+
|
| 1037 |
+
/// <summary>
|
| 1038 |
+
/// 设置自动登录
|
| 1039 |
+
/// </summary>
|
| 1040 |
+
/// <param name="browser">浏览器实例</param>
|
| 1041 |
+
/// <param name="url">URL</param>
|
| 1042 |
+
private void SetupAutoLogin(ChromiumWebBrowser browser, string url)
|
| 1043 |
+
{
|
| 1044 |
+
var siteItem = FindSiteItemByUrl(url);
|
| 1045 |
+
if (siteItem is null || !siteItem.AutoLogin)
|
| 1046 |
+
{
|
| 1047 |
+
return;
|
| 1048 |
+
}
|
| 1049 |
+
|
| 1050 |
+
EventHandler<FrameLoadEndEventArgs>? frameLoadEndHandler = null;
|
| 1051 |
+
frameLoadEndHandler = (sender, args) =>
|
| 1052 |
+
{
|
| 1053 |
+
if (args.Frame.IsMain)
|
| 1054 |
+
{
|
| 1055 |
+
ExecuteAutoLoginScript(args.Frame, siteItem);
|
| 1056 |
+
browser.FrameLoadEnd -= frameLoadEndHandler;
|
| 1057 |
+
}
|
| 1058 |
+
};
|
| 1059 |
+
|
| 1060 |
+
browser.FrameLoadEnd += frameLoadEndHandler;
|
| 1061 |
+
}
|
| 1062 |
+
|
| 1063 |
+
/// <summary>
|
| 1064 |
+
/// 设置标签页卸载事件处理器
|
| 1065 |
+
/// </summary>
|
| 1066 |
+
/// <param name="tabItem">标签页</param>
|
| 1067 |
+
/// <param name="browser">浏览器实例</param>
|
| 1068 |
+
private void SetupTabUnloadHandler(TabItem tabItem, ChromiumWebBrowser browser)
|
| 1069 |
+
{
|
| 1070 |
+
tabItem.Unloaded += (sender, e) =>
|
| 1071 |
+
{
|
| 1072 |
+
try
|
| 1073 |
+
{
|
| 1074 |
+
_logger.Debug("标签页卸载开始");
|
| 1075 |
+
|
| 1076 |
+
CleanupBrowserEventHandlers(browser);
|
| 1077 |
+
|
| 1078 |
+
if (sender is TabItem tab && tab.Tag is TabInfo info)
|
| 1079 |
+
{
|
| 1080 |
+
Browser.BrowserInstanceManager.Instance.ReleaseBrowser(info.TabId);
|
| 1081 |
+
_logger.Debug($"浏览器实例已释放 - TabId: {info.TabId}");
|
| 1082 |
+
}
|
| 1083 |
+
|
| 1084 |
+
_logger.Debug("标签页卸载完成");
|
| 1085 |
+
}
|
| 1086 |
+
catch (Exception ex)
|
| 1087 |
+
{
|
| 1088 |
+
_logger.Error("标签页卸载时发生异常", ex);
|
| 1089 |
+
}
|
| 1090 |
+
};
|
| 1091 |
+
}
|
| 1092 |
+
|
| 1093 |
+
/// <summary>
|
| 1094 |
+
/// 清理浏览器事件处理器
|
| 1095 |
+
/// </summary>
|
| 1096 |
+
/// <param name="browser">浏览器实例</param>
|
| 1097 |
+
private static void CleanupBrowserEventHandlers(ChromiumWebBrowser browser)
|
| 1098 |
+
{
|
| 1099 |
+
if (browser.Tag is not Dictionary<string, object> browserTags)
|
| 1100 |
+
{
|
| 1101 |
+
return;
|
| 1102 |
+
}
|
| 1103 |
+
|
| 1104 |
+
if (browserTags.TryGetValue("titleUpdateHandler", out var titleHandler) &&
|
| 1105 |
+
titleHandler is EventHandler<FrameLoadEndEventArgs> titleUpdateHandler)
|
| 1106 |
+
{
|
| 1107 |
+
browser.FrameLoadEnd -= titleUpdateHandler;
|
| 1108 |
+
}
|
| 1109 |
+
|
| 1110 |
+
if (browserTags.TryGetValue("loadingStateChangedHandler", out var loadingHandler) &&
|
| 1111 |
+
loadingHandler is EventHandler<LoadingStateChangedEventArgs> loadingStateChangedHandler)
|
| 1112 |
+
{
|
| 1113 |
+
browser.LoadingStateChanged -= loadingStateChangedHandler;
|
| 1114 |
+
}
|
| 1115 |
+
|
| 1116 |
+
browserTags.Clear();
|
| 1117 |
+
}
|
| 1118 |
+
|
| 1119 |
+
/// <summary>
|
| 1120 |
+
/// 执行自动登录脚本
|
| 1121 |
+
/// </summary>
|
| 1122 |
+
/// <param name="frame">浏览器框架</param>
|
| 1123 |
+
/// <param name="siteItem">站点配置项</param>
|
| 1124 |
+
private static void ExecuteAutoLoginScript(IFrame frame, SiteItem siteItem)
|
| 1125 |
+
{
|
| 1126 |
+
var username = siteItem.UseCommonCredentials ? siteItem.CommonUsername : siteItem.Username;
|
| 1127 |
+
var password = siteItem.UseCommonCredentials ? siteItem.CommonPassword : siteItem.Password;
|
| 1128 |
+
var usernameSelector = string.IsNullOrWhiteSpace(siteItem.UsernameSelector)
|
| 1129 |
+
? "input[type=email],input[type=text],input[name*=user],input[name*=email],input[name*=login]"
|
| 1130 |
+
: siteItem.UsernameSelector;
|
| 1131 |
+
var passwordSelector = string.IsNullOrWhiteSpace(siteItem.PasswordSelector)
|
| 1132 |
+
? "input[type=password]"
|
| 1133 |
+
: siteItem.PasswordSelector;
|
| 1134 |
+
var captchaSelector = siteItem.CaptchaSelector;
|
| 1135 |
+
var loginButtonSelector = string.IsNullOrWhiteSpace(siteItem.LoginButtonSelector)
|
| 1136 |
+
? "button[type=submit],input[type=submit]"
|
| 1137 |
+
: siteItem.LoginButtonSelector;
|
| 1138 |
+
var loginPageFeature = siteItem.LoginPageFeature;
|
| 1139 |
+
var captchaValue = siteItem.CaptchaValue ?? "";
|
| 1140 |
+
|
| 1141 |
+
if (siteItem.CaptchaMode == 1 && !string.IsNullOrWhiteSpace(siteItem.GoogleSecret))
|
| 1142 |
+
{
|
| 1143 |
+
captchaValue = BrowserTool.Utils.GoogleAuthenticator.GenerateCode(siteItem.GoogleSecret);
|
| 1144 |
+
}
|
| 1145 |
+
|
| 1146 |
+
var featureCheck = string.IsNullOrWhiteSpace(loginPageFeature)
|
| 1147 |
+
? ""
|
| 1148 |
+
: $"if(!document.querySelector('{loginPageFeature}')) return;";
|
| 1149 |
+
var captchaJs = string.IsNullOrWhiteSpace(captchaSelector)
|
| 1150 |
+
? ""
|
| 1151 |
+
: $"var captchaInput = document.querySelector('{captchaSelector}'); if(captchaInput) captchaInput.value = '{captchaValue.Replace("'", "\\'")}';";
|
| 1152 |
+
|
| 1153 |
+
var js = $@"
|
| 1154 |
+
(function() {{
|
| 1155 |
+
{featureCheck}
|
| 1156 |
+
var userInput = document.querySelector('{usernameSelector}');
|
| 1157 |
+
var passInput = document.querySelector('{passwordSelector}');
|
| 1158 |
+
{captchaJs}
|
| 1159 |
+
if(userInput) userInput.value = '{username?.Replace("'", "\\'") ?? ""}';
|
| 1160 |
+
if(passInput) passInput.value = '{password?.Replace("'", "\\'") ?? ""}';
|
| 1161 |
+
var form = userInput ? userInput.form : (passInput ? passInput.form : null);
|
| 1162 |
+
if(form) {{
|
| 1163 |
+
form.submit();
|
| 1164 |
+
}} else {{
|
| 1165 |
+
var btn = document.querySelector('{loginButtonSelector}');
|
| 1166 |
+
if(btn) btn.click();
|
| 1167 |
+
}}
|
| 1168 |
+
}})();
|
| 1169 |
+
";
|
| 1170 |
+
|
| 1171 |
+
frame.ExecuteJavaScriptAsync(js);
|
| 1172 |
+
}
|
| 1173 |
+
|
| 1174 |
+
/// <summary>
|
| 1175 |
+
/// 根据URL查找站点配置项
|
| 1176 |
+
/// </summary>
|
| 1177 |
+
/// <param name="url">URL</param>
|
| 1178 |
+
/// <returns>站点配置项或null</returns>
|
| 1179 |
+
private static SiteItem? FindSiteItemByUrl(string url)
|
| 1180 |
+
{
|
| 1181 |
+
var allGroups = BrowserTool.Database.SiteConfig.GetAllGroups();
|
| 1182 |
+
return allGroups.SelectMany(group => group.Sites)
|
| 1183 |
+
.FirstOrDefault(site => site.Url == url);
|
| 1184 |
+
}
|
| 1185 |
+
|
| 1186 |
+
/// <summary>
|
| 1187 |
+
/// 清除TreeView的选中状态
|
| 1188 |
+
/// </summary>
|
| 1189 |
+
private void ClearTreeViewSelection()
|
| 1190 |
+
{
|
| 1191 |
+
foreach (TreeViewItem item in MenuTree.Items)
|
| 1192 |
+
{
|
| 1193 |
+
item.IsSelected = false;
|
| 1194 |
+
foreach (TreeViewItem child in item.Items)
|
| 1195 |
+
{
|
| 1196 |
+
child.IsSelected = false;
|
| 1197 |
+
}
|
| 1198 |
+
}
|
| 1199 |
+
}
|
| 1200 |
+
|
| 1201 |
+
/// <summary>
|
| 1202 |
+
/// 关闭指定的标签页
|
| 1203 |
+
/// </summary>
|
| 1204 |
+
/// <param name="tabItem">要关闭的标签页</param>
|
| 1205 |
+
public void CloseTab(TabItem? tabItem)
|
| 1206 |
+
{
|
| 1207 |
+
if (tabItem is not null)
|
| 1208 |
+
{
|
| 1209 |
+
MainTabControl.Items.Remove(tabItem);
|
| 1210 |
+
ClearTreeViewSelection();
|
| 1211 |
+
}
|
| 1212 |
+
}
|
| 1213 |
+
|
| 1214 |
+
/// <summary>
|
| 1215 |
+
/// 关闭当前标签页
|
| 1216 |
+
/// </summary>
|
| 1217 |
+
private void CloseCurrentTab()
|
| 1218 |
+
{
|
| 1219 |
+
if (MainTabControl.SelectedItem is TabItem tab)
|
| 1220 |
+
{
|
| 1221 |
+
CloseTab(tab);
|
| 1222 |
+
}
|
| 1223 |
+
}
|
| 1224 |
+
|
| 1225 |
+
#endregion
|
| 1226 |
+
|
| 1227 |
+
#region 键盘事件处理
|
| 1228 |
+
|
| 1229 |
+
/// <summary>
|
| 1230 |
+
/// 主窗口键盘事件处理器
|
| 1231 |
+
/// </summary>
|
| 1232 |
+
private void MainWindow_KeyDown(object sender, KeyEventArgs e)
|
| 1233 |
+
{
|
| 1234 |
+
try
|
| 1235 |
+
{
|
| 1236 |
+
switch (e.Key)
|
| 1237 |
+
{
|
| 1238 |
+
case Key.F12:
|
| 1239 |
+
HandleF12KeyPress();
|
| 1240 |
+
break;
|
| 1241 |
+
|
| 1242 |
+
case Key.F when Keyboard.Modifiers == ModifierKeys.Control:
|
| 1243 |
+
ShowSearchBar();
|
| 1244 |
+
e.Handled = true;
|
| 1245 |
+
break;
|
| 1246 |
+
|
| 1247 |
+
case Key.W when Keyboard.Modifiers == ModifierKeys.Control:
|
| 1248 |
+
CloseCurrentTab();
|
| 1249 |
+
e.Handled = true;
|
| 1250 |
+
break;
|
| 1251 |
+
|
| 1252 |
+
case Key.Escape when PageSearchBar.Visibility == Visibility.Visible:
|
| 1253 |
+
HideSearchBar();
|
| 1254 |
+
e.Handled = true;
|
| 1255 |
+
break;
|
| 1256 |
+
}
|
| 1257 |
+
}
|
| 1258 |
+
catch (Exception ex)
|
| 1259 |
+
{
|
| 1260 |
+
_logger.Error("键盘事件处理错误", ex);
|
| 1261 |
+
}
|
| 1262 |
+
}
|
| 1263 |
+
|
| 1264 |
+
/// <summary>
|
| 1265 |
+
/// 处理F12按键(���发者工具)
|
| 1266 |
+
/// </summary>
|
| 1267 |
+
private void HandleF12KeyPress()
|
| 1268 |
+
{
|
| 1269 |
+
if (MainTabControl.SelectedItem is TabItem tab && tab.Content is ChromiumWebBrowser currentBrowser)
|
| 1270 |
+
{
|
| 1271 |
+
CefHelper.ShowDevTools(currentBrowser);
|
| 1272 |
+
}
|
| 1273 |
+
}
|
| 1274 |
+
|
| 1275 |
+
#endregion
|
| 1276 |
+
|
| 1277 |
+
#region 搜索栏功能
|
| 1278 |
+
|
| 1279 |
+
/// <summary>
|
| 1280 |
+
/// 显示搜索栏
|
| 1281 |
+
/// </summary>
|
| 1282 |
+
private void ShowSearchBar()
|
| 1283 |
+
{
|
| 1284 |
+
try
|
| 1285 |
+
{
|
| 1286 |
+
var currentTab = MainTabControl.SelectedItem as TabItem;
|
| 1287 |
+
if (currentTab?.Content is ChromiumWebBrowser browser)
|
| 1288 |
+
{
|
| 1289 |
+
PageSearchBar.SetBrowser(browser);
|
| 1290 |
+
PageSearchBar.Visibility = Visibility.Visible;
|
| 1291 |
+
PageSearchBar.FocusSearchBox();
|
| 1292 |
+
}
|
| 1293 |
+
}
|
| 1294 |
+
catch (Exception ex)
|
| 1295 |
+
{
|
| 1296 |
+
_logger.Error("显示搜索栏错误", ex);
|
| 1297 |
+
}
|
| 1298 |
+
}
|
| 1299 |
+
|
| 1300 |
+
/// <summary>
|
| 1301 |
+
/// 隐藏搜索栏
|
| 1302 |
+
/// </summary>
|
| 1303 |
+
private void HideSearchBar()
|
| 1304 |
+
{
|
| 1305 |
+
try
|
| 1306 |
+
{
|
| 1307 |
+
PageSearchBar.Visibility = Visibility.Collapsed;
|
| 1308 |
+
PageSearchBar.Clear();
|
| 1309 |
+
|
| 1310 |
+
var currentTab = MainTabControl.SelectedItem as TabItem;
|
| 1311 |
+
if (currentTab?.Content is ChromiumWebBrowser browser)
|
| 1312 |
+
{
|
| 1313 |
+
browser.Focus();
|
| 1314 |
+
}
|
| 1315 |
+
}
|
| 1316 |
+
catch (Exception ex)
|
| 1317 |
+
{
|
| 1318 |
+
_logger.Error("隐藏搜索栏错误", ex);
|
| 1319 |
+
}
|
| 1320 |
+
}
|
| 1321 |
+
|
| 1322 |
+
/// <summary>
|
| 1323 |
+
/// 搜索栏关闭请求事件处理器
|
| 1324 |
+
/// </summary>
|
| 1325 |
+
private void PageSearchBar_CloseRequested(object sender, EventArgs e) => HideSearchBar();
|
| 1326 |
+
|
| 1327 |
+
#endregion
|
| 1328 |
+
|
| 1329 |
+
#region UI控件事件处理
|
| 1330 |
+
|
| 1331 |
+
/// <summary>
|
| 1332 |
+
/// 测试环境按钮点击事件处理器
|
| 1333 |
+
/// </summary>
|
| 1334 |
+
private void TestEnvButton_Click(object sender, RoutedEventArgs e)
|
| 1335 |
+
{
|
| 1336 |
+
var testUrl = System.Configuration.ConfigurationManager.AppSettings["TestEnvironmentUrl"];
|
| 1337 |
+
|
| 1338 |
+
if (!string.IsNullOrEmpty(testUrl))
|
| 1339 |
+
{
|
| 1340 |
+
OpenUrlInTab("测试环境", testUrl, true);
|
| 1341 |
+
}
|
| 1342 |
+
else
|
| 1343 |
+
{
|
| 1344 |
+
MessageBox.Show("测试环境URL未配置,请在App.config中设置TestEnvironmentUrl", "配置错误",
|
| 1345 |
+
MessageBoxButton.OK, MessageBoxImage.Warning);
|
| 1346 |
+
}
|
| 1347 |
+
}
|
| 1348 |
+
|
| 1349 |
+
/// <summary>
|
| 1350 |
+
/// 手动打卡按钮点击事件处理器
|
| 1351 |
+
/// </summary>
|
| 1352 |
+
private async void ManualCheckInButton_Click(object sender, RoutedEventArgs e)
|
| 1353 |
+
{
|
| 1354 |
+
try
|
| 1355 |
+
{
|
| 1356 |
+
_logger.Debug("手动打卡按钮被点击");
|
| 1357 |
+
|
| 1358 |
+
await App.GetAutoCheckInSimulator().ExecuteManualCheckIn();
|
| 1359 |
+
}
|
| 1360 |
+
catch (Exception ex)
|
| 1361 |
+
{
|
| 1362 |
+
_logger.Error("手动打卡时发生异常", ex);
|
| 1363 |
+
MessageBox.Show($"手动打卡失败:{ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
|
| 1364 |
+
}
|
| 1365 |
+
}
|
| 1366 |
+
|
| 1367 |
+
/// <summary>
|
| 1368 |
+
/// 抽屉开关按钮点击事件处理器
|
| 1369 |
+
/// </summary>
|
| 1370 |
+
private void DrawerToggleBtn_Click(object sender, RoutedEventArgs e)
|
| 1371 |
+
{
|
| 1372 |
+
ToggleDrawer();
|
| 1373 |
+
NotifyBrowserLayoutChange();
|
| 1374 |
+
}
|
| 1375 |
+
|
| 1376 |
+
/// <summary>
|
| 1377 |
+
/// 切换抽屉状态
|
| 1378 |
+
/// </summary>
|
| 1379 |
+
private void ToggleDrawer()
|
| 1380 |
+
{
|
| 1381 |
+
if (_isDrawerOpen)
|
| 1382 |
+
{
|
| 1383 |
+
DrawerCol.Width = new GridLength(0);
|
| 1384 |
+
DrawerBorder.Visibility = Visibility.Collapsed;
|
| 1385 |
+
}
|
| 1386 |
+
else
|
| 1387 |
+
{
|
| 1388 |
+
DrawerCol.Width = new GridLength(200);
|
| 1389 |
+
DrawerBorder.Visibility = Visibility.Visible;
|
| 1390 |
+
}
|
| 1391 |
+
_isDrawerOpen = !_isDrawerOpen;
|
| 1392 |
+
}
|
| 1393 |
+
|
| 1394 |
+
/// <summary>
|
| 1395 |
+
/// 通知浏览器布局变化
|
| 1396 |
+
/// </summary>
|
| 1397 |
+
private void NotifyBrowserLayoutChange()
|
| 1398 |
+
{
|
| 1399 |
+
try
|
| 1400 |
+
{
|
| 1401 |
+
if (MainTabControl?.Items.Count == 0)
|
| 1402 |
+
{
|
| 1403 |
+
return;
|
| 1404 |
+
}
|
| 1405 |
+
|
| 1406 |
+
var selectedItem = MainTabControl.SelectedItem as TabItem;
|
| 1407 |
+
if (selectedItem is null || !MainTabControl.Items.Contains(selectedItem))
|
| 1408 |
+
{
|
| 1409 |
+
if (MainTabControl.Items.Count > 0)
|
| 1410 |
+
{
|
| 1411 |
+
MainTabControl.SelectedIndex = 0;
|
| 1412 |
+
}
|
| 1413 |
+
return;
|
| 1414 |
+
}
|
| 1415 |
+
|
| 1416 |
+
if (selectedItem.Content is ChromiumWebBrowser browser && !browser.IsDisposed)
|
| 1417 |
+
{
|
| 1418 |
+
var browserInstance = browser.GetBrowser();
|
| 1419 |
+
if (browserInstance is not null && !browserInstance.IsDisposed)
|
| 1420 |
+
{
|
| 1421 |
+
var host = browserInstance.GetHost();
|
| 1422 |
+
if (host is not null && !host.IsDisposed)
|
| 1423 |
+
{
|
| 1424 |
+
host.NotifyMoveOrResizeStarted();
|
| 1425 |
+
host.NotifyScreenInfoChanged();
|
| 1426 |
+
host.WasResized();
|
| 1427 |
+
}
|
| 1428 |
+
}
|
| 1429 |
+
}
|
| 1430 |
+
}
|
| 1431 |
+
catch (ObjectDisposedException ex)
|
| 1432 |
+
{
|
| 1433 |
+
_logger.Error("浏览器对象已释放", ex);
|
| 1434 |
+
}
|
| 1435 |
+
catch (Exception ex)
|
| 1436 |
+
{
|
| 1437 |
+
_logger.Error("通知浏览器布局变化���出错", ex);
|
| 1438 |
+
}
|
| 1439 |
+
}
|
| 1440 |
+
|
| 1441 |
+
/// <summary>
|
| 1442 |
+
/// 后退按钮点击事件处理器
|
| 1443 |
+
/// </summary>
|
| 1444 |
+
private void BackButton_Click(object sender, RoutedEventArgs e)
|
| 1445 |
+
{
|
| 1446 |
+
if (MainTabControl.SelectedItem is TabItem tabItem &&
|
| 1447 |
+
tabItem.Content is ChromiumWebBrowser browser && browser.CanGoBack)
|
| 1448 |
+
{
|
| 1449 |
+
browser.Back();
|
| 1450 |
+
}
|
| 1451 |
+
}
|
| 1452 |
+
|
| 1453 |
+
/// <summary>
|
| 1454 |
+
/// 前进按钮点击事件处理器
|
| 1455 |
+
/// </summary>
|
| 1456 |
+
private void ForwardButton_Click(object sender, RoutedEventArgs e)
|
| 1457 |
+
{
|
| 1458 |
+
if (MainTabControl.SelectedItem is TabItem tabItem &&
|
| 1459 |
+
tabItem.Content is ChromiumWebBrowser browser && browser.CanGoForward)
|
| 1460 |
+
{
|
| 1461 |
+
browser.Forward();
|
| 1462 |
+
}
|
| 1463 |
+
}
|
| 1464 |
+
|
| 1465 |
+
/// <summary>
|
| 1466 |
+
/// 刷新按钮点击事件处理器
|
| 1467 |
+
/// </summary>
|
| 1468 |
+
private void RefreshButton_Click(object sender, RoutedEventArgs e)
|
| 1469 |
+
{
|
| 1470 |
+
if (MainTabControl.SelectedItem is TabItem tabItem &&
|
| 1471 |
+
tabItem.Content is ChromiumWebBrowser browser)
|
| 1472 |
+
{
|
| 1473 |
+
browser.Reload();
|
| 1474 |
+
}
|
| 1475 |
+
}
|
| 1476 |
+
|
| 1477 |
+
/// <summary>
|
| 1478 |
+
/// URL输入框键盘事件处理器
|
| 1479 |
+
/// </summary>
|
| 1480 |
+
private void UrlTextBox_KeyDown(object sender, KeyEventArgs e)
|
| 1481 |
+
{
|
| 1482 |
+
if (e.Key != Key.Enter || MainTabControl.SelectedItem is not TabItem tabItem ||
|
| 1483 |
+
tabItem.Content is not ChromiumWebBrowser browser)
|
| 1484 |
+
{
|
| 1485 |
+
return;
|
| 1486 |
+
}
|
| 1487 |
+
|
| 1488 |
+
var url = UrlTextBox.Text;
|
| 1489 |
+
if (!url.StartsWith("http://") && !url.StartsWith("https://"))
|
| 1490 |
+
{
|
| 1491 |
+
url = "https://" + url;
|
| 1492 |
+
}
|
| 1493 |
+
browser.LoadUrl(url);
|
| 1494 |
+
}
|
| 1495 |
+
|
| 1496 |
+
/// <summary>
|
| 1497 |
+
/// 开发者工具按钮点击事件处理器
|
| 1498 |
+
/// </summary>
|
| 1499 |
+
private void DevToolsButton_Click(object sender, RoutedEventArgs e)
|
| 1500 |
+
{
|
| 1501 |
+
if (MainTabControl.SelectedItem is not TabItem tabItem ||
|
| 1502 |
+
tabItem.Content is not ChromiumWebBrowser browser)
|
| 1503 |
+
{
|
| 1504 |
+
return;
|
| 1505 |
+
}
|
| 1506 |
+
|
| 1507 |
+
var url = UrlTextBox.Text;
|
| 1508 |
+
if (!string.IsNullOrWhiteSpace(url))
|
| 1509 |
+
{
|
| 1510 |
+
if (!url.StartsWith("http://") && !url.StartsWith("https://"))
|
| 1511 |
+
{
|
| 1512 |
+
url = "https://" + url;
|
| 1513 |
+
}
|
| 1514 |
+
browser.LoadUrl(url);
|
| 1515 |
+
}
|
| 1516 |
+
}
|
| 1517 |
+
|
| 1518 |
+
/// <summary>
|
| 1519 |
+
/// 切换地址栏的显示/隐藏状态
|
| 1520 |
+
/// </summary>
|
| 1521 |
+
public void ToggleUrlBar()
|
| 1522 |
+
{
|
| 1523 |
+
if (this.FindName("UrlBar") is not Grid urlBar)
|
| 1524 |
+
{
|
| 1525 |
+
return;
|
| 1526 |
+
}
|
| 1527 |
+
|
| 1528 |
+
urlBar.Visibility = urlBar.Visibility == Visibility.Visible
|
| 1529 |
+
? Visibility.Collapsed
|
| 1530 |
+
: Visibility.Visible;
|
| 1531 |
+
|
| 1532 |
+
if (urlBar.Visibility == Visibility.Visible &&
|
| 1533 |
+
MainTabControl.SelectedItem is TabItem tabItem &&
|
| 1534 |
+
tabItem.Content is ChromiumWebBrowser browser)
|
| 1535 |
+
{
|
| 1536 |
+
UrlTextBox.Text = browser.Address;
|
| 1537 |
+
}
|
| 1538 |
+
}
|
| 1539 |
+
|
| 1540 |
+
/// <summary>
|
| 1541 |
+
/// 标签页选择变更事件处理器
|
| 1542 |
+
/// </summary>
|
| 1543 |
+
private void MainTabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
|
| 1544 |
+
{
|
| 1545 |
+
try
|
| 1546 |
+
{
|
| 1547 |
+
ClearTreeViewSelection();
|
| 1548 |
+
|
| 1549 |
+
if (PageSearchBar.Visibility == Visibility.Visible)
|
| 1550 |
+
{
|
| 1551 |
+
var currentTab = MainTabControl.SelectedItem as TabItem;
|
| 1552 |
+
if (currentTab?.Content is ChromiumWebBrowser browser)
|
| 1553 |
+
{
|
| 1554 |
+
PageSearchBar.SetBrowser(browser);
|
| 1555 |
+
}
|
| 1556 |
+
else
|
| 1557 |
+
{
|
| 1558 |
+
HideSearchBar();
|
| 1559 |
+
}
|
| 1560 |
+
}
|
| 1561 |
+
|
| 1562 |
+
if (MainTabControl.SelectedItem is TabItem tabItem &&
|
| 1563 |
+
tabItem.Content is ChromiumWebBrowser selectedBrowser)
|
| 1564 |
+
{
|
| 1565 |
+
UrlTextBox.Text = selectedBrowser.Address;
|
| 1566 |
+
}
|
| 1567 |
+
}
|
| 1568 |
+
catch (Exception ex)
|
| 1569 |
+
{
|
| 1570 |
+
_logger.Error("标签页切换错误", ex);
|
| 1571 |
+
}
|
| 1572 |
+
}
|
| 1573 |
+
|
| 1574 |
+
#endregion
|
| 1575 |
+
|
| 1576 |
+
#region 右键菜单处理
|
| 1577 |
+
|
| 1578 |
+
/// <summary>
|
| 1579 |
+
/// 标签页右键按下事件处理器
|
| 1580 |
+
/// </summary>
|
| 1581 |
+
private void MainTabControl_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
|
| 1582 |
+
{
|
| 1583 |
+
var tabItem = GetTabItemAtPosition(e.GetPosition(MainTabControl));
|
| 1584 |
+
if (tabItem is not null)
|
| 1585 |
+
{
|
| 1586 |
+
MainTabControl.SelectedItem = tabItem;
|
| 1587 |
+
}
|
| 1588 |
+
}
|
| 1589 |
+
|
| 1590 |
+
/// <summary>
|
| 1591 |
+
/// 获取指定位置的标签页
|
| 1592 |
+
/// </summary>
|
| 1593 |
+
/// <param name="position">位置</param>
|
| 1594 |
+
/// <returns>标签页或null</returns>
|
| 1595 |
+
private TabItem? GetTabItemAtPosition(Point position)
|
| 1596 |
+
{
|
| 1597 |
+
var result = VisualTreeHelper.HitTest(MainTabControl, position);
|
| 1598 |
+
if (result is null)
|
| 1599 |
+
{
|
| 1600 |
+
return null;
|
| 1601 |
+
}
|
| 1602 |
+
|
| 1603 |
+
var element = result.VisualHit;
|
| 1604 |
+
while (element is not null && element is not TabItem)
|
| 1605 |
+
{
|
| 1606 |
+
element = VisualTreeHelper.GetParent(element);
|
| 1607 |
+
}
|
| 1608 |
+
return element as TabItem;
|
| 1609 |
+
}
|
| 1610 |
+
|
| 1611 |
+
/// <summary>
|
| 1612 |
+
/// 关闭标签页菜单项点击事件处理器
|
| 1613 |
+
/// </summary>
|
| 1614 |
+
private void CloseTabMenuItem_Click(object sender, RoutedEventArgs e)
|
| 1615 |
+
{
|
| 1616 |
+
if (MainTabControl.SelectedItem is not TabItem tabItem)
|
| 1617 |
+
{
|
| 1618 |
+
return;
|
| 1619 |
+
}
|
| 1620 |
+
|
| 1621 |
+
if (tabItem.Tag is TabInfo tabInfo)
|
| 1622 |
+
{
|
| 1623 |
+
Browser.BrowserInstanceManager.Instance.ReleaseBrowser(tabInfo.TabId);
|
| 1624 |
+
}
|
| 1625 |
+
|
| 1626 |
+
MainTabControl.Items.Remove(tabItem);
|
| 1627 |
+
ClearTreeViewSelection();
|
| 1628 |
+
}
|
| 1629 |
+
|
| 1630 |
+
/// <summary>
|
| 1631 |
+
/// 关闭其他标签页菜单项点击事件处理器
|
| 1632 |
+
/// </summary>
|
| 1633 |
+
private void CloseOtherTabsMenuItem_Click(object sender, RoutedEventArgs e)
|
| 1634 |
+
{
|
| 1635 |
+
if (MainTabControl.SelectedItem is not TabItem selectedTab)
|
| 1636 |
+
{
|
| 1637 |
+
return;
|
| 1638 |
+
}
|
| 1639 |
+
|
| 1640 |
+
var tabsToRemove = MainTabControl.Items.Cast<TabItem>()
|
| 1641 |
+
.Where(tab => tab != selectedTab)
|
| 1642 |
+
.ToList();
|
| 1643 |
+
|
| 1644 |
+
foreach (var tab in tabsToRemove)
|
| 1645 |
+
{
|
| 1646 |
+
MainTabControl.Items.Remove(tab);
|
| 1647 |
+
}
|
| 1648 |
+
}
|
| 1649 |
+
|
| 1650 |
+
/// <summary>
|
| 1651 |
+
/// 关闭右侧标签页菜单项点击事件处理器
|
| 1652 |
+
/// </summary>
|
| 1653 |
+
private void CloseRightTabsMenuItem_Click(object sender, RoutedEventArgs e)
|
| 1654 |
+
{
|
| 1655 |
+
if (MainTabControl.SelectedItem is not TabItem selectedTab)
|
| 1656 |
+
{
|
| 1657 |
+
return;
|
| 1658 |
+
}
|
| 1659 |
+
|
| 1660 |
+
var selectedIndex = MainTabControl.Items.IndexOf(selectedTab);
|
| 1661 |
+
var tabsToRemove = MainTabControl.Items.Cast<TabItem>()
|
| 1662 |
+
.Where((tab, index) => index > selectedIndex)
|
| 1663 |
+
.ToList();
|
| 1664 |
+
|
| 1665 |
+
foreach (var tab in tabsToRemove)
|
| 1666 |
+
{
|
| 1667 |
+
MainTabControl.Items.Remove(tab);
|
| 1668 |
+
}
|
| 1669 |
+
}
|
| 1670 |
+
|
| 1671 |
+
/// <summary>
|
| 1672 |
+
/// 刷新标签页菜单项点击事件处理器
|
| 1673 |
+
/// </summary>
|
| 1674 |
+
private void RefreshTabMenuItem_Click(object sender, RoutedEventArgs e)
|
| 1675 |
+
{
|
| 1676 |
+
if (MainTabControl.SelectedItem is TabItem tabItem &&
|
| 1677 |
+
tabItem.Content is ChromiumWebBrowser browser)
|
| 1678 |
+
{
|
| 1679 |
+
browser.Reload();
|
| 1680 |
+
}
|
| 1681 |
+
}
|
| 1682 |
+
|
| 1683 |
+
/// <summary>
|
| 1684 |
+
/// 复制URL菜单项点击事件处理器
|
| 1685 |
+
/// </summary>
|
| 1686 |
+
private void CopyUrlMenuItem_Click(object sender, RoutedEventArgs e)
|
| 1687 |
+
{
|
| 1688 |
+
if (MainTabControl.SelectedItem is not TabItem tabItem)
|
| 1689 |
+
{
|
| 1690 |
+
return;
|
| 1691 |
+
}
|
| 1692 |
+
|
| 1693 |
+
var textToCopy = tabItem.Content switch
|
| 1694 |
+
{
|
| 1695 |
+
ChromiumWebBrowser browser => browser.Address,
|
| 1696 |
+
_ when tabItem.Tag is TabInfo tabInfo => tabInfo.Url,
|
| 1697 |
+
_ => null
|
| 1698 |
+
};
|
| 1699 |
+
|
| 1700 |
+
if (!string.IsNullOrEmpty(textToCopy))
|
| 1701 |
+
{
|
| 1702 |
+
Clipboard.SetText(textToCopy);
|
| 1703 |
+
}
|
| 1704 |
+
}
|
| 1705 |
+
|
| 1706 |
+
/// <summary>
|
| 1707 |
+
/// 复制标题菜单项点击事件处理器
|
| 1708 |
+
/// </summary>
|
| 1709 |
+
private void CopyTitleMenuItem_Click(object sender, RoutedEventArgs e)
|
| 1710 |
+
{
|
| 1711 |
+
if (MainTabControl.SelectedItem is TabItem tabItem)
|
| 1712 |
+
{
|
| 1713 |
+
Clipboard.SetText(tabItem.Header.ToString());
|
| 1714 |
+
}
|
| 1715 |
+
}
|
| 1716 |
+
|
| 1717 |
+
/// <summary>
|
| 1718 |
+
/// 复制选中内容菜单项点击事件处理器
|
| 1719 |
+
/// </summary>
|
| 1720 |
+
private void CopyMenuItem_Click(object sender, RoutedEventArgs e)
|
| 1721 |
+
{
|
| 1722 |
+
if (MainTabControl.SelectedItem is TabItem tabItem &&
|
| 1723 |
+
tabItem.Content is ChromiumWebBrowser browser)
|
| 1724 |
+
{
|
| 1725 |
+
browser.Copy();
|
| 1726 |
+
}
|
| 1727 |
+
}
|
| 1728 |
+
|
| 1729 |
+
/// <summary>
|
| 1730 |
+
/// 粘贴菜单项点击事件处理器
|
| 1731 |
+
/// </summary>
|
| 1732 |
+
private void PasteMenuItem_Click(object sender, RoutedEventArgs e)
|
| 1733 |
+
{
|
| 1734 |
+
if (MainTabControl.SelectedItem is TabItem tabItem &&
|
| 1735 |
+
tabItem.Content is ChromiumWebBrowser browser)
|
| 1736 |
+
{
|
| 1737 |
+
browser.Paste();
|
| 1738 |
+
}
|
| 1739 |
+
}
|
| 1740 |
+
|
| 1741 |
+
/// <summary>
|
| 1742 |
+
/// 粘贴并访问菜单项点击事件处理器
|
| 1743 |
+
/// </summary>
|
| 1744 |
+
private void PasteAndGoMenuItem_Click(object sender, RoutedEventArgs e)
|
| 1745 |
+
{
|
| 1746 |
+
if (MainTabControl.SelectedItem is not TabItem tabItem ||
|
| 1747 |
+
tabItem.Content is not ChromiumWebBrowser browser)
|
| 1748 |
+
{
|
| 1749 |
+
return;
|
| 1750 |
+
}
|
| 1751 |
+
|
| 1752 |
+
var url = Clipboard.GetText();
|
| 1753 |
+
if (string.IsNullOrWhiteSpace(url))
|
| 1754 |
+
{
|
| 1755 |
+
return;
|
| 1756 |
+
}
|
| 1757 |
+
|
| 1758 |
+
if (!url.StartsWith("http://") && !url.StartsWith("https://"))
|
| 1759 |
+
{
|
| 1760 |
+
url = "https://" + url;
|
| 1761 |
+
}
|
| 1762 |
+
browser.LoadUrl(url);
|
| 1763 |
+
}
|
| 1764 |
+
|
| 1765 |
+
/// <summary>
|
| 1766 |
+
/// 复制标签页菜单项点击事件处理器
|
| 1767 |
+
/// </summary>
|
| 1768 |
+
private void DuplicateTabMenuItem_Click(object sender, RoutedEventArgs e)
|
| 1769 |
+
{
|
| 1770 |
+
if (MainTabControl.SelectedItem is not TabItem tabItem)
|
| 1771 |
+
{
|
| 1772 |
+
return;
|
| 1773 |
+
}
|
| 1774 |
+
|
| 1775 |
+
var (baseTitle, url) = GetTabDuplicationInfo(tabItem);
|
| 1776 |
+
var newTitle = GenerateUniqueTabTitle(baseTitle);
|
| 1777 |
+
|
| 1778 |
+
var newBrowser = new ChromiumWebBrowser(url)
|
| 1779 |
+
{
|
| 1780 |
+
DownloadHandler = new CefDownloadHandler(),
|
| 1781 |
+
MenuHandler = new CefMenuHandler(),
|
| 1782 |
+
LifeSpanHandler = new Browser.CefLifeSpanHandler()
|
| 1783 |
+
};
|
| 1784 |
+
|
| 1785 |
+
var newTabItem = new TabItem
|
| 1786 |
+
{
|
| 1787 |
+
Header = newTitle,
|
| 1788 |
+
Tag = GetTabTag(tabItem, url),
|
| 1789 |
+
Content = newBrowser
|
| 1790 |
+
};
|
| 1791 |
+
|
| 1792 |
+
MainTabControl.Items.Add(newTabItem);
|
| 1793 |
+
MainTabControl.SelectedItem = newTabItem;
|
| 1794 |
+
}
|
| 1795 |
+
|
| 1796 |
+
/// <summary>
|
| 1797 |
+
/// 获取标签页复制信息
|
| 1798 |
+
/// </summary>
|
| 1799 |
+
/// <param name="tabItem">标签页</param>
|
| 1800 |
+
/// <returns>基础标题和URL</returns>
|
| 1801 |
+
private static (string baseTitle, string url) GetTabDuplicationInfo(TabItem tabItem)
|
| 1802 |
+
{
|
| 1803 |
+
var baseTitle = Regex.Replace(tabItem.Header.ToString(), @"\d+$", "").Trim();
|
| 1804 |
+
|
| 1805 |
+
var url = tabItem.Content switch
|
| 1806 |
+
{
|
| 1807 |
+
ChromiumWebBrowser browser => browser.Address,
|
| 1808 |
+
_ when tabItem.Tag is TabInfo tabInfo => tabInfo.Url,
|
| 1809 |
+
_ => string.Empty
|
| 1810 |
+
};
|
| 1811 |
+
|
| 1812 |
+
return (baseTitle, url);
|
| 1813 |
+
}
|
| 1814 |
+
|
| 1815 |
+
/// <summary>
|
| 1816 |
+
/// 生成唯一的标签页标题
|
| 1817 |
+
/// </summary>
|
| 1818 |
+
/// <param name="baseTitle">基础标题</param>
|
| 1819 |
+
/// <returns>唯一标题</returns>
|
| 1820 |
+
private string GenerateUniqueTabTitle(string baseTitle)
|
| 1821 |
+
{
|
| 1822 |
+
var existingTabs = MainTabControl.Items.Cast<TabItem>()
|
| 1823 |
+
.Where(t => t.Header.ToString().StartsWith(baseTitle))
|
| 1824 |
+
.ToList();
|
| 1825 |
+
|
| 1826 |
+
var newNumber = 1;
|
| 1827 |
+
if (existingTabs.Any())
|
| 1828 |
+
{
|
| 1829 |
+
var numbers = existingTabs
|
| 1830 |
+
.Select(t => t.Header.ToString())
|
| 1831 |
+
.Select(title => Regex.Match(title, @"\d+$"))
|
| 1832 |
+
.Where(m => m.Success)
|
| 1833 |
+
.Select(m => int.Parse(m.Value))
|
| 1834 |
+
.ToList();
|
| 1835 |
+
|
| 1836 |
+
if (numbers.Any())
|
| 1837 |
+
{
|
| 1838 |
+
newNumber = numbers.Max() + 1;
|
| 1839 |
+
}
|
| 1840 |
+
}
|
| 1841 |
+
|
| 1842 |
+
return $"{baseTitle}{newNumber}";
|
| 1843 |
+
}
|
| 1844 |
+
|
| 1845 |
+
/// <summary>
|
| 1846 |
+
/// 获取标签页标记
|
| 1847 |
+
/// </summary>
|
| 1848 |
+
/// <param name="originalTab">原始标签页</param>
|
| 1849 |
+
/// <param name="url">URL</param>
|
| 1850 |
+
/// <returns>标签页标记</returns>
|
| 1851 |
+
private static object GetTabTag(TabItem originalTab, string url)
|
| 1852 |
+
{
|
| 1853 |
+
return originalTab.Tag switch
|
| 1854 |
+
{
|
| 1855 |
+
TabInfo tabInfo => tabInfo,
|
| 1856 |
+
_ => url
|
| 1857 |
+
};
|
| 1858 |
+
}
|
| 1859 |
+
|
| 1860 |
+
/// <summary>
|
| 1861 |
+
/// 在新标签页中打开菜单项点击事件处理器
|
| 1862 |
+
/// </summary>
|
| 1863 |
+
private void OpenInNewTabMenuItem_Click(object sender, RoutedEventArgs e)
|
| 1864 |
+
{
|
| 1865 |
+
if (MainTabControl.SelectedItem is not TabItem tabItem)
|
| 1866 |
+
{
|
| 1867 |
+
return;
|
| 1868 |
+
}
|
| 1869 |
+
|
| 1870 |
+
var (title, url) = GetTabOpenInfo(tabItem);
|
| 1871 |
+
if (!string.IsNullOrEmpty(url))
|
| 1872 |
+
{
|
| 1873 |
+
OpenUrlInTab(title, url);
|
| 1874 |
+
}
|
| 1875 |
+
}
|
| 1876 |
+
|
| 1877 |
+
/// <summary>
|
| 1878 |
+
/// 获取标签页打开信息
|
| 1879 |
+
/// </summary>
|
| 1880 |
+
/// <param name="tabItem">标签页</param>
|
| 1881 |
+
/// <returns>标题和URL</returns>
|
| 1882 |
+
private static (string title, string url) GetTabOpenInfo(TabItem tabItem)
|
| 1883 |
+
{
|
| 1884 |
+
var title = tabItem.Header.ToString();
|
| 1885 |
+
|
| 1886 |
+
var url = tabItem.Content switch
|
| 1887 |
+
{
|
| 1888 |
+
ChromiumWebBrowser browser => browser.Address,
|
| 1889 |
+
_ when tabItem.Tag is TabInfo tabInfo => tabInfo.Url,
|
| 1890 |
+
_ => string.Empty
|
| 1891 |
+
};
|
| 1892 |
+
|
| 1893 |
+
return (title, url);
|
| 1894 |
+
}
|
| 1895 |
+
|
| 1896 |
+
#endregion
|
| 1897 |
+
|
| 1898 |
+
#region 窗口控制
|
| 1899 |
+
|
| 1900 |
+
/// <summary>
|
| 1901 |
+
/// 最小化按钮点击事件处理器
|
| 1902 |
+
/// </summary>
|
| 1903 |
+
private void MinimizeButton_Click(object sender, RoutedEventArgs e) =>
|
| 1904 |
+
WindowState = WindowState.Minimized;
|
| 1905 |
+
|
| 1906 |
+
/// <summary>
|
| 1907 |
+
/// 最小化按钮点击事件处理器(备用)
|
| 1908 |
+
/// </summary>
|
| 1909 |
+
private void btnMinimize_Click(object sender, RoutedEventArgs e) =>
|
| 1910 |
+
WindowState = WindowState.Minimized;
|
| 1911 |
+
|
| 1912 |
+
/// <summary>
|
| 1913 |
+
/// 最大化按钮点击事件处理器
|
| 1914 |
+
/// </summary>
|
| 1915 |
+
private void MaximizeButton_Click(object sender, RoutedEventArgs e)
|
| 1916 |
+
{
|
| 1917 |
+
if (WindowState == WindowState.Maximized)
|
| 1918 |
+
{
|
| 1919 |
+
WindowState = WindowState.Normal;
|
| 1920 |
+
}
|
| 1921 |
+
else
|
| 1922 |
+
{
|
| 1923 |
+
WindowState = WindowState.Maximized;
|
| 1924 |
+
MaxHeight = SystemParameters.MaximizedPrimaryScreenHeight;
|
| 1925 |
+
MaxWidth = SystemParameters.MaximizedPrimaryScreenWidth;
|
| 1926 |
+
}
|
| 1927 |
+
}
|
| 1928 |
+
|
| 1929 |
+
/// <summary>
|
| 1930 |
+
/// 关闭按钮点击事件处理器
|
| 1931 |
+
/// </summary>
|
| 1932 |
+
private void CloseButton_Click(object sender, RoutedEventArgs e) => Close();
|
| 1933 |
+
|
| 1934 |
+
/// <summary>
|
| 1935 |
+
/// 标题栏鼠标左键按下事件处理器 - 支持拖拽和双击最大化/还原
|
| 1936 |
+
/// </summary>
|
| 1937 |
+
private void TitleBar_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
|
| 1938 |
+
{
|
| 1939 |
+
if (e.ClickCount == 2)
|
| 1940 |
+
{
|
| 1941 |
+
MaximizeButton_Click(sender, e);
|
| 1942 |
+
}
|
| 1943 |
+
else if (e.ButtonState == MouseButtonState.Pressed)
|
| 1944 |
+
{
|
| 1945 |
+
DragMove();
|
| 1946 |
+
}
|
| 1947 |
+
}
|
| 1948 |
+
|
| 1949 |
+
/// <summary>
|
| 1950 |
+
/// 窗口关闭事件处理 - 最小化到托盘而不是退出
|
| 1951 |
+
/// </summary>
|
| 1952 |
+
protected override void OnClosing(CancelEventArgs e)
|
| 1953 |
+
{
|
| 1954 |
+
e.Cancel = true;
|
| 1955 |
+
Hide();
|
| 1956 |
+
|
| 1957 |
+
if (Application.Current.Resources["TrayIcon"] is TaskbarIcon trayIcon)
|
| 1958 |
+
{
|
| 1959 |
+
trayIcon.ShowBalloonTip("Browser Tool", "程序已最小化到系统托盘", BalloonIcon.Info);
|
| 1960 |
+
}
|
| 1961 |
+
}
|
| 1962 |
+
|
| 1963 |
+
/// <summary>
|
| 1964 |
+
/// 主窗体大小改变事件处理器
|
| 1965 |
+
/// </summary>
|
| 1966 |
+
private void MainWindow_SizeChanged(object sender, SizeChangedEventArgs e)
|
| 1967 |
+
{
|
| 1968 |
+
Dispatcher.BeginInvoke(new Action(() =>
|
| 1969 |
+
{
|
| 1970 |
+
try
|
| 1971 |
+
{
|
| 1972 |
+
foreach (TabItem tab in MainTabControl.Items)
|
| 1973 |
+
{
|
| 1974 |
+
if (tab.Content is not ChromiumWebBrowser browser)
|
| 1975 |
+
{
|
| 1976 |
+
continue;
|
| 1977 |
+
}
|
| 1978 |
+
|
| 1979 |
+
browser.InvalidateVisual();
|
| 1980 |
+
browser.UpdateLayout();
|
| 1981 |
+
|
| 1982 |
+
if (!browser.IsLoading && browser.CanExecuteJavascriptInMainFrame)
|
| 1983 |
+
{
|
| 1984 |
+
browser.ExecuteScriptAsync("if(window.dispatchEvent) { window.dispatchEvent(new Event('resize')); }");
|
| 1985 |
+
}
|
| 1986 |
+
}
|
| 1987 |
+
|
| 1988 |
+
_logger.Debug($"[窗口大小改变] 新尺寸: {e.NewSize.Width}x{e.NewSize.Height}");
|
| 1989 |
+
}
|
| 1990 |
+
catch (Exception ex)
|
| 1991 |
+
{
|
| 1992 |
+
_logger.Error("窗口大小改变处理错误", ex);
|
| 1993 |
+
}
|
| 1994 |
+
}), DispatcherPriority.Background);
|
| 1995 |
+
}
|
| 1996 |
+
|
| 1997 |
+
/// <summary>
|
| 1998 |
+
/// 关闭标签页按钮点击事件处理器
|
| 1999 |
+
/// </summary>
|
| 2000 |
+
private void CloseTabButton_Click(object sender, RoutedEventArgs e)
|
| 2001 |
+
{
|
| 2002 |
+
try
|
| 2003 |
+
{
|
| 2004 |
+
if (sender is not Button button || button.Tag is not TabItem tabItem ||
|
| 2005 |
+
!MainTabControl.Items.Contains(tabItem))
|
| 2006 |
+
{
|
| 2007 |
+
return;
|
| 2008 |
+
}
|
| 2009 |
+
|
| 2010 |
+
if (MainTabControl.SelectedItem == tabItem && MainTabControl.Items.Count > 1)
|
| 2011 |
+
{
|
| 2012 |
+
var index = MainTabControl.Items.IndexOf(tabItem);
|
| 2013 |
+
MainTabControl.SelectedIndex = index > 0 ? index - 1 : index + 1;
|
| 2014 |
+
}
|
| 2015 |
+
|
| 2016 |
+
if (tabItem.Content is ChromiumWebBrowser browser)
|
| 2017 |
+
{
|
| 2018 |
+
browser.Dispose();
|
| 2019 |
+
}
|
| 2020 |
+
|
| 2021 |
+
MainTabControl.Items.Remove(tabItem);
|
| 2022 |
+
ClearTreeViewSelection();
|
| 2023 |
+
}
|
| 2024 |
+
catch (Exception ex)
|
| 2025 |
+
{
|
| 2026 |
+
_logger.Error("关闭标签页错误", ex);
|
| 2027 |
+
}
|
| 2028 |
+
}
|
| 2029 |
+
|
| 2030 |
+
#endregion
|
| 2031 |
+
|
| 2032 |
+
#region 设置相关
|
| 2033 |
+
|
| 2034 |
+
/// <summary>
|
| 2035 |
+
/// 设置按钮点击事件处理器
|
| 2036 |
+
/// </summary>
|
| 2037 |
+
private void btnSettings_Click(object sender, RoutedEventArgs e)
|
| 2038 |
+
{
|
| 2039 |
+
var settingsWindow = new SettingsWindow(this);
|
| 2040 |
+
|
| 2041 |
+
_logger.Debug("[MainWindow.btnSettings_Click] 准备订阅 SettingsSaved 事件");
|
| 2042 |
+
settingsWindow.SettingsSaved += SettingsWindow_SettingsSaved;
|
| 2043 |
+
|
| 2044 |
+
settingsWindow.ShowDialog();
|
| 2045 |
+
|
| 2046 |
+
_logger.Debug("[MainWindow.btnSettings_Click] 设置窗口已关闭,准备刷新菜单");
|
| 2047 |
+
RefreshMenuFromSettings();
|
| 2048 |
+
}
|
| 2049 |
+
|
| 2050 |
+
/// <summary>
|
| 2051 |
+
/// 设置保存事件处理器
|
| 2052 |
+
/// </summary>
|
| 2053 |
+
private void SettingsWindow_SettingsSaved(object sender, EventArgs args)
|
| 2054 |
+
{
|
| 2055 |
+
_logger.Debug("[MainWindow] SettingsSaved 事件被触发,准备刷新菜单");
|
| 2056 |
+
|
| 2057 |
+
Dispatcher.BeginInvoke(new Action(() =>
|
| 2058 |
+
{
|
| 2059 |
+
try
|
| 2060 |
+
{
|
| 2061 |
+
_logger.Debug("[MainWindow] 开始重新加载菜单");
|
| 2062 |
+
LoadMenuGroupsFromDb();
|
| 2063 |
+
MenuTree.Items.Refresh();
|
| 2064 |
+
_logger.Debug("[MainWindow] 菜单刷新完成");
|
| 2065 |
+
}
|
| 2066 |
+
catch (Exception ex)
|
| 2067 |
+
{
|
| 2068 |
+
_logger.Error("[MainWindow] 刷新菜单时出错", ex);
|
| 2069 |
+
MessageBox.Show($"刷新菜单时出错:{ex.Message}", "错误",
|
| 2070 |
+
MessageBoxButton.OK, MessageBoxImage.Error);
|
| 2071 |
+
}
|
| 2072 |
+
}), DispatcherPriority.Render);
|
| 2073 |
+
}
|
| 2074 |
+
|
| 2075 |
+
#endregion
|
| 2076 |
+
|
| 2077 |
+
#region 上下文菜单位置回调
|
| 2078 |
+
|
| 2079 |
+
/// <summary>
|
| 2080 |
+
/// 上下文菜单位置回调
|
| 2081 |
+
/// </summary>
|
| 2082 |
+
/// <param name="popupSize">弹出框大小</param>
|
| 2083 |
+
/// <param name="targetSize">目标大小</param>
|
| 2084 |
+
/// <param name="offset">偏移量</param>
|
| 2085 |
+
/// <returns>自定义弹出框位置数组</returns>
|
| 2086 |
+
private CustomPopupPlacement[] ContextMenu_PlacementCallback(Size popupSize, Size targetSize, Point offset)
|
| 2087 |
+
{
|
| 2088 |
+
var mousePosition = Mouse.GetPosition(MainTabControl);
|
| 2089 |
+
mousePosition = MainTabControl.PointToScreen(mousePosition);
|
| 2090 |
+
|
| 2091 |
+
var placementPoint = new Point(mousePosition.X, mousePosition.Y);
|
| 2092 |
+
|
| 2093 |
+
if (_isDrawerOpen)
|
| 2094 |
+
{
|
| 2095 |
+
placementPoint.X -= DrawerCol.Width.Value;
|
| 2096 |
+
}
|
| 2097 |
+
|
| 2098 |
+
return new[]
|
| 2099 |
+
{
|
| 2100 |
+
new CustomPopupPlacement(placementPoint, PopupPrimaryAxis.Horizontal)
|
| 2101 |
+
};
|
| 2102 |
+
}
|
| 2103 |
+
|
| 2104 |
+
#endregion
|
| 2105 |
+
|
| 2106 |
+
#region 辅助方法
|
| 2107 |
+
|
| 2108 |
+
/// <summary>
|
| 2109 |
+
/// 获取当前标签页的浏览器控件
|
| 2110 |
+
/// </summary>
|
| 2111 |
+
/// <returns>当前浏览器控件或null</returns>
|
| 2112 |
+
private ChromiumWebBrowser? GetCurrentBrowser()
|
| 2113 |
+
{
|
| 2114 |
+
if (MainTabControl.SelectedItem is not TabItem selectedTab)
|
| 2115 |
+
{
|
| 2116 |
+
return null;
|
| 2117 |
+
}
|
| 2118 |
+
|
| 2119 |
+
return selectedTab.Content switch
|
| 2120 |
+
{
|
| 2121 |
+
ChromiumWebBrowser browser => browser,
|
| 2122 |
+
ContentPresenter contentPresenter when contentPresenter.Content is Grid grid =>
|
| 2123 |
+
grid.Children.OfType<ChromiumWebBrowser>().FirstOrDefault(),
|
| 2124 |
+
_ => null
|
| 2125 |
+
};
|
| 2126 |
+
}
|
| 2127 |
+
|
| 2128 |
+
/// <summary>
|
| 2129 |
+
/// 更新URL输入框内容
|
| 2130 |
+
/// </summary>
|
| 2131 |
+
/// <param name="url">新的URL</param>
|
| 2132 |
+
public void UpdateUrl(string url)
|
| 2133 |
+
{
|
| 2134 |
+
if (MainTabControl.SelectedItem is not TabItem selectedTab)
|
| 2135 |
+
{
|
| 2136 |
+
return;
|
| 2137 |
+
}
|
| 2138 |
+
|
| 2139 |
+
switch (selectedTab.Content)
|
| 2140 |
+
{
|
| 2141 |
+
case ContentPresenter contentPresenter when contentPresenter.Content is Grid grid:
|
| 2142 |
+
{
|
| 2143 |
+
var urlTextBox = grid.FindName("UrlTextBox") as TextBox;
|
| 2144 |
+
if (urlTextBox is not null)
|
| 2145 |
+
{
|
| 2146 |
+
urlTextBox.Text = url;
|
| 2147 |
+
}
|
| 2148 |
+
break;
|
| 2149 |
+
}
|
| 2150 |
+
}
|
| 2151 |
+
}
|
| 2152 |
+
|
| 2153 |
+
#endregion
|
| 2154 |
+
|
| 2155 |
+
#region 自动打卡相关
|
| 2156 |
+
|
| 2157 |
+
/// <summary>
|
| 2158 |
+
/// 检查页面并将打卡结果加入队列
|
| 2159 |
+
/// </summary>
|
| 2160 |
+
/// <param name="browser">浏览器控件</param>
|
| 2161 |
+
private async Task CheckAndEnqueueCheckInResult(ChromiumWebBrowser browser)
|
| 2162 |
+
{
|
| 2163 |
+
try
|
| 2164 |
+
{
|
| 2165 |
+
await Dispatcher.InvokeAsync(async () =>
|
| 2166 |
+
{
|
| 2167 |
+
string currentUrl = browser.Address ?? "";
|
| 2168 |
+
_logger.Debug($"检查页面打卡结果: {currentUrl}");
|
| 2169 |
+
|
| 2170 |
+
// 检查域名是否为打卡相关域名
|
| 2171 |
+
List<string> _checkInDomains = ConfigurationManager.AppSettings["CheckInDomains"]?
|
| 2172 |
+
.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
|
| 2173 |
+
.Select(d => d.Trim())
|
| 2174 |
+
.ToList() ?? new List<string>();
|
| 2175 |
+
|
| 2176 |
+
bool isDomainMatch = _checkInDomains.Any(domain => currentUrl.Contains(domain));
|
| 2177 |
+
if (!isDomainMatch)
|
| 2178 |
+
{
|
| 2179 |
+
_logger.Debug($"当前域名不在打卡域名列表中: {currentUrl}");
|
| 2180 |
+
return;
|
| 2181 |
+
}
|
| 2182 |
+
|
| 2183 |
+
_logger.Debug("域名匹配,开始检查页面内容");
|
| 2184 |
+
|
| 2185 |
+
// 等待页面稳定(给页面一些时间完成加载和渲染)
|
| 2186 |
+
await Task.Delay(2000);
|
| 2187 |
+
|
| 2188 |
+
// 再次检查浏览器状态
|
| 2189 |
+
if (browser.IsDisposed)
|
| 2190 |
+
{
|
| 2191 |
+
_logger.Debug("浏览器已被释放,跳过检查");
|
| 2192 |
+
return;
|
| 2193 |
+
}
|
| 2194 |
+
|
| 2195 |
+
// 检查页面内容是否包含成功标识
|
| 2196 |
+
bool hasSuccessContent = await CheckPageContentForSuccess(browser);
|
| 2197 |
+
|
| 2198 |
+
// 创建检查结果并加入队列
|
| 2199 |
+
var result = new CheckInResult
|
| 2200 |
+
{
|
| 2201 |
+
IsSuccess = hasSuccessContent,
|
| 2202 |
+
Url = currentUrl,
|
| 2203 |
+
CheckTime = DateTime.Now,
|
| 2204 |
+
Message = hasSuccessContent ? "检测到打卡成功" : "未检测到打卡成功标识"
|
| 2205 |
+
};
|
| 2206 |
+
|
| 2207 |
+
CheckInResultQueue.Instance.EnqueueResult(result);
|
| 2208 |
+
_logger.Debug($"打卡结果已加入队列: 成功={hasSuccessContent}, URL={currentUrl}");
|
| 2209 |
+
});
|
| 2210 |
+
}
|
| 2211 |
+
catch (Exception ex)
|
| 2212 |
+
{
|
| 2213 |
+
_logger.Error("检查并入队打卡结果时发生异常", ex);
|
| 2214 |
+
}
|
| 2215 |
+
}
|
| 2216 |
+
|
| 2217 |
+
/// <summary>
|
| 2218 |
+
/// 检查页面内容是否包含成功标识
|
| 2219 |
+
/// </summary>
|
| 2220 |
+
/// <param name="browser">浏览器控件</param>
|
| 2221 |
+
/// <returns>是否包含成功标识</returns>
|
| 2222 |
+
private async Task<bool> CheckPageContentForSuccess(ChromiumWebBrowser browser)
|
| 2223 |
+
{
|
| 2224 |
+
try
|
| 2225 |
+
{
|
| 2226 |
+
// 首先使用简单的方法检查URL和基本信息
|
| 2227 |
+
string url = browser.Address ?? "";
|
| 2228 |
+
_logger.Debug($"当前页面URL: {url}");
|
| 2229 |
+
|
| 2230 |
+
//// 先检查URL是否包含成功标识(最快的方法)
|
| 2231 |
+
//if (CheckUrlForSuccess(url))
|
| 2232 |
+
//{
|
| 2233 |
+
// _logger.Debug("URL包含成功标识,直接返回成功");
|
| 2234 |
+
// return true;
|
| 2235 |
+
//}
|
| 2236 |
+
|
| 2237 |
+
// 检查浏览器状态
|
| 2238 |
+
if (browser.IsDisposed)
|
| 2239 |
+
{
|
| 2240 |
+
_logger.Debug("浏览器已被释放,使用URL判断");
|
| 2241 |
+
return CheckUrlForSuccess(url);
|
| 2242 |
+
}
|
| 2243 |
+
|
| 2244 |
+
var cefBrowser = browser.GetBrowser();
|
| 2245 |
+
if (cefBrowser == null || cefBrowser.IsDisposed)
|
| 2246 |
+
{
|
| 2247 |
+
_logger.Debug("CEF浏览器实例无效,使用URL判断");
|
| 2248 |
+
return CheckUrlForSuccess(url);
|
| 2249 |
+
}
|
| 2250 |
+
|
| 2251 |
+
// 检查浏览器是否正在加载
|
| 2252 |
+
if (browser.IsLoading)
|
| 2253 |
+
{
|
| 2254 |
+
_logger.Debug("浏览器正在加载,等待加载完成...");
|
| 2255 |
+
// 等待最多3秒让页面加载完成
|
| 2256 |
+
int waitCount = 0;
|
| 2257 |
+
while (browser.IsLoading && waitCount < 6)
|
| 2258 |
+
{
|
| 2259 |
+
await Task.Delay(500);
|
| 2260 |
+
waitCount++;
|
| 2261 |
+
}
|
| 2262 |
+
|
| 2263 |
+
if (browser.IsLoading)
|
| 2264 |
+
{
|
| 2265 |
+
_logger.Debug("页面仍在加载,使用URL判断");
|
| 2266 |
+
return CheckUrlForSuccess(url);
|
| 2267 |
+
}
|
| 2268 |
+
}
|
| 2269 |
+
|
| 2270 |
+
_logger.Debug($"浏览器状态检查通过,BrowserId: {cefBrowser.Identifier}");
|
| 2271 |
+
|
| 2272 |
+
// 使用多种方法获取页面内容
|
| 2273 |
+
string bodyText = "";
|
| 2274 |
+
string titleText = "";
|
| 2275 |
+
|
| 2276 |
+
// 方法1: 尝试使用扩展方法(减少超时时间到5秒)
|
| 2277 |
+
try
|
| 2278 |
+
{
|
| 2279 |
+
_logger.Debug("开始使用扩展方法获取页面内容...");
|
| 2280 |
+
|
| 2281 |
+
var sourceTask = browser.GetSourceAsync();
|
| 2282 |
+
var textTask = browser.GetTextAsync();
|
| 2283 |
+
|
| 2284 |
+
// 减少超时时间到5秒
|
| 2285 |
+
var timeoutTask = Task.Delay(5000);
|
| 2286 |
+
var completedTask = await Task.WhenAny(
|
| 2287 |
+
Task.WhenAll(sourceTask, textTask),
|
| 2288 |
+
timeoutTask
|
| 2289 |
+
);
|
| 2290 |
+
|
| 2291 |
+
if (completedTask != timeoutTask)
|
| 2292 |
+
{
|
| 2293 |
+
var source = await sourceTask;
|
| 2294 |
+
var text = await textTask;
|
| 2295 |
+
|
| 2296 |
+
// 从HTML源码中提取标题
|
| 2297 |
+
var titleMatch = System.Text.RegularExpressions.Regex.Match(source, @"<title[^>]*>([^<]*)</title>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
|
| 2298 |
+
titleText = titleMatch.Success ? titleMatch.Groups[1].Value.Trim() : "";
|
| 2299 |
+
|
| 2300 |
+
// 使用页面文本内容,限制长度
|
| 2301 |
+
bodyText = text.Length > 1000 ? text.Substring(0, 1000) : text;
|
| 2302 |
+
|
| 2303 |
+
_logger.Debug($"扩展方法获取成功 - 标题: {titleText}, 内容长度: {bodyText.Length}");
|
| 2304 |
+
}
|
| 2305 |
+
else
|
| 2306 |
+
{
|
| 2307 |
+
_logger.Debug("扩展方法获取超时,尝试备用方法");
|
| 2308 |
+
// 继续尝试其他方法
|
| 2309 |
+
}
|
| 2310 |
+
}
|
| 2311 |
+
catch (Exception ex)
|
| 2312 |
+
{
|
| 2313 |
+
_logger.Error($"扩展方法获取时发生异常: {ex.Message}");
|
| 2314 |
+
// 继续尝试其他方法
|
| 2315 |
+
}
|
| 2316 |
+
|
| 2317 |
+
// 方法2: 如果扩展方法失败,尝试获取页面标题
|
| 2318 |
+
if (string.IsNullOrEmpty(titleText))
|
| 2319 |
+
{
|
| 2320 |
+
try
|
| 2321 |
+
{
|
| 2322 |
+
_logger.Debug("尝试直接获取页面标题...");
|
| 2323 |
+
var mainFrame = cefBrowser.MainFrame;
|
| 2324 |
+
if (mainFrame != null && !mainFrame.IsDisposed)
|
| 2325 |
+
{
|
| 2326 |
+
// 使用主框架的URL作为备用
|
| 2327 |
+
var frameUrl = mainFrame.Url ?? "";
|
| 2328 |
+
if (!string.IsNullOrEmpty(frameUrl) && frameUrl != url)
|
| 2329 |
+
{
|
| 2330 |
+
_logger.Debug($"使用主框架URL: {frameUrl}");
|
| 2331 |
+
if (CheckUrlForSuccess(frameUrl))
|
| 2332 |
+
{
|
| 2333 |
+
return true;
|
| 2334 |
+
}
|
| 2335 |
+
}
|
| 2336 |
+
}
|
| 2337 |
+
}
|
| 2338 |
+
catch (Exception ex)
|
| 2339 |
+
{
|
| 2340 |
+
_logger.Error($"获取主框架信息时发生异常: {ex.Message}");
|
| 2341 |
+
}
|
| 2342 |
+
}
|
| 2343 |
+
|
| 2344 |
+
// 方法3: 检查浏览器标题(如果可用)
|
| 2345 |
+
if (string.IsNullOrEmpty(titleText))
|
| 2346 |
+
{
|
| 2347 |
+
try
|
| 2348 |
+
{
|
| 2349 |
+
// 尝试从浏览器获取标题
|
| 2350 |
+
var browserHost = cefBrowser.GetHost();
|
| 2351 |
+
if (browserHost != null && !browserHost.IsDisposed)
|
| 2352 |
+
{
|
| 2353 |
+
// 这里可以添加更多的检查逻辑
|
| 2354 |
+
_logger.Debug("浏览器主机可用,但无法获取内容");
|
| 2355 |
+
}
|
| 2356 |
+
}
|
| 2357 |
+
catch (Exception ex)
|
| 2358 |
+
{
|
| 2359 |
+
_logger.Error($"获取浏览器主机信息时发生异常: {ex.Message}");
|
| 2360 |
+
}
|
| 2361 |
+
}
|
| 2362 |
+
|
| 2363 |
+
// 如果所有方法都失败,使用URL判断
|
| 2364 |
+
if (string.IsNullOrEmpty(titleText) && string.IsNullOrEmpty(bodyText))
|
| 2365 |
+
{
|
| 2366 |
+
_logger.Debug("所有内容获取方法都失败,使用URL判断");
|
| 2367 |
+
return CheckUrlForSuccess(url);
|
| 2368 |
+
}
|
| 2369 |
+
|
| 2370 |
+
// 检查是否包含打卡成功的关键字
|
| 2371 |
+
string[] successKeywords = { "打卡成功", "签到成功", "考勤成功", "上班打卡成功", "下班打卡成功", "check-in successful", "attendance successful", "签到完成", "打卡完成", "google" };
|
| 2372 |
+
|
| 2373 |
+
bool hasSuccessKeyword = successKeywords.Any(keyword =>
|
| 2374 |
+
bodyText.Contains(keyword, StringComparison.OrdinalIgnoreCase) ||
|
| 2375 |
+
titleText.Contains(keyword, StringComparison.OrdinalIgnoreCase) ||
|
| 2376 |
+
url.Contains(keyword, StringComparison.OrdinalIgnoreCase));
|
| 2377 |
+
|
| 2378 |
+
if (hasSuccessKeyword)
|
| 2379 |
+
{
|
| 2380 |
+
_logger.Debug("页面包含成功标识");
|
| 2381 |
+
return true;
|
| 2382 |
+
}
|
| 2383 |
+
|
| 2384 |
+
_logger.Debug("页面不包含成功标识");
|
| 2385 |
+
return false;
|
| 2386 |
+
}
|
| 2387 |
+
catch (Exception ex)
|
| 2388 |
+
{
|
| 2389 |
+
_logger.Error("检查页面内容时发生异常", ex);
|
| 2390 |
+
return CheckUrlForSuccess(browser.Address ?? "");
|
| 2391 |
+
}
|
| 2392 |
+
}
|
| 2393 |
+
|
| 2394 |
+
/// <summary>
|
| 2395 |
+
/// 基于URL检查是否成功(备用方法)
|
| 2396 |
+
/// </summary>
|
| 2397 |
+
/// <param name="url">页面URL</param>
|
| 2398 |
+
/// <returns>是否包含成功标识</returns>
|
| 2399 |
+
private bool CheckUrlForSuccess(string url)
|
| 2400 |
+
{
|
| 2401 |
+
if (string.IsNullOrEmpty(url))
|
| 2402 |
+
return false;
|
| 2403 |
+
|
| 2404 |
+
string[] urlSuccessKeywords = { "success", "complete", "done", "成功", "完成", "google" };
|
| 2405 |
+
bool hasSuccessInUrl = urlSuccessKeywords.Any(keyword => url.Contains(keyword, StringComparison.OrdinalIgnoreCase));
|
| 2406 |
+
|
| 2407 |
+
_logger.Debug($"URL成功检查结果: {hasSuccessInUrl} (URL: {url})");
|
| 2408 |
+
return hasSuccessInUrl;
|
| 2409 |
+
}
|
| 2410 |
+
|
| 2411 |
+
#endregion
|
| 2412 |
+
}
|
| 2413 |
+
|
csharp/BrowserTool/BrowserTool/NLog.config
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<?xml version="1.0" encoding="utf-8" ?>
|
| 2 |
+
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
|
| 3 |
+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
| 4 |
+
autoReload="true"
|
| 5 |
+
throwExceptions="false">
|
| 6 |
+
|
| 7 |
+
<targets>
|
| 8 |
+
<!-- 调试器目标 - 用于在VS2022中显示日志 -->
|
| 9 |
+
<target name="debugger" xsi:type="Debugger"
|
| 10 |
+
layout="${time}|${level:uppercase=true}|${logger}|${message} ${exception:format=tostring}" />
|
| 11 |
+
|
| 12 |
+
<!-- 文件目标 -->
|
| 13 |
+
<target name="logfile" xsi:type="File"
|
| 14 |
+
fileName="${basedir}/logs/${shortdate}.log"
|
| 15 |
+
layout="${longdate}|${level:uppercase=true}|${logger}|${message} ${exception:format=tostring}"
|
| 16 |
+
archiveFileName="${basedir}/logs/archive/log.{#}.txt"
|
| 17 |
+
archiveNumbering="Date"
|
| 18 |
+
archiveEvery="Day"
|
| 19 |
+
archiveDateFormat="yyyyMMdd"
|
| 20 |
+
maxArchiveFiles="30"
|
| 21 |
+
concurrentWrites="true"
|
| 22 |
+
keepFileOpen="false" />
|
| 23 |
+
|
| 24 |
+
<!-- 控制台目标 -->
|
| 25 |
+
<target xsi:type="ColoredConsole" name="console"
|
| 26 |
+
layout="${longdate}|${level:uppercase=true}|${logger}|${message} ${exception:format=tostring}">
|
| 27 |
+
<highlight-row condition="level == LogLevel.Debug" foregroundColor="DarkGray" />
|
| 28 |
+
<highlight-row condition="level == LogLevel.Info" foregroundColor="White" />
|
| 29 |
+
<highlight-row condition="level == LogLevel.Warn" foregroundColor="Yellow" />
|
| 30 |
+
<highlight-row condition="level == LogLevel.Error" foregroundColor="Red" />
|
| 31 |
+
<highlight-row condition="level == LogLevel.Fatal" foregroundColor="Red" backgroundColor="White" />
|
| 32 |
+
</target>
|
| 33 |
+
</targets>
|
| 34 |
+
|
| 35 |
+
<rules>
|
| 36 |
+
<!-- 所有日志级别都输出到调试器和文件 -->
|
| 37 |
+
<logger name="*" minlevel="Debug" writeTo="debugger,logfile,console" />
|
| 38 |
+
</rules>
|
| 39 |
+
</nlog>
|
csharp/BrowserTool/BrowserTool/Properties/AssemblyInfo.cs
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
using System.Reflection;
|
| 2 |
+
using System.Resources;
|
| 3 |
+
using System.Runtime.CompilerServices;
|
| 4 |
+
using System.Runtime.InteropServices;
|
| 5 |
+
using System.Windows;
|
| 6 |
+
|
| 7 |
+
// 有关程序集的一般信息由以下
|
| 8 |
+
// 控制。更改这些特性值可修改
|
| 9 |
+
// 与程序集关联的信息。
|
| 10 |
+
// [assembly: AssemblyTitle("BrowserTool")]
|
| 11 |
+
// [assembly: AssemblyDescription("")]
|
| 12 |
+
// [assembly: AssemblyConfiguration("")]
|
| 13 |
+
// [assembly: AssemblyCompany("")]
|
| 14 |
+
// [assembly: AssemblyProduct("BrowserTool")]
|
| 15 |
+
// [assembly: AssemblyCopyright("Copyright © 2025")]
|
| 16 |
+
// [assembly: AssemblyTrademark("")]
|
| 17 |
+
// [assembly: AssemblyCulture("")]
|
| 18 |
+
|
| 19 |
+
// 将 ComVisible 设置为 false 会使此程序集中的类型
|
| 20 |
+
//对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型
|
| 21 |
+
//请将此类型的 ComVisible 特性设置为 true。
|
| 22 |
+
[assembly: ComVisible(false)]
|
| 23 |
+
|
| 24 |
+
//若要开始生成可本地化的应用程序,请设置
|
| 25 |
+
//.csproj 文件中的 <UICulture>CultureYouAreCodingWith</UICulture>
|
| 26 |
+
//在 <PropertyGroup> 中。例如,如果你使用的是美国英语。
|
| 27 |
+
//使用的是美国英语,请将 <UICulture> 设置为 en-US。 然后取消
|
| 28 |
+
//对以下 NeutralResourceLanguage 特性的注释。 更新
|
| 29 |
+
//以下行中的"en-US"以匹配项目文件中的 UICulture 设置。
|
| 30 |
+
|
| 31 |
+
//[assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.Satellite)]
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
[assembly: ThemeInfo(
|
| 35 |
+
ResourceDictionaryLocation.None, //主题特定资源词典所处位置
|
| 36 |
+
//(未在页面中找到资源时使用,
|
| 37 |
+
//或应用程序资源字典中找到时使用)
|
| 38 |
+
ResourceDictionaryLocation.SourceAssembly //常规资源词典所处位置
|
| 39 |
+
//(未在页面中找到资源时使用,
|
| 40 |
+
//、应用程序或任何主题专用资源字典中找到时使用)
|
| 41 |
+
)]
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
// 程序集的版本信息由下列四个值组成:
|
| 45 |
+
//
|
| 46 |
+
// 主版本
|
| 47 |
+
// 次版本
|
| 48 |
+
// 生成号
|
| 49 |
+
// 修订号
|
| 50 |
+
//
|
| 51 |
+
// [assembly: AssemblyVersion("1.0.0.0")]
|
| 52 |
+
// [assembly: AssemblyFileVersion("1.0.0.0")]
|
csharp/BrowserTool/BrowserTool/Properties/Resources.Designer.cs
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
//------------------------------------------------------------------------------
|
| 2 |
+
// <auto-generated>
|
| 3 |
+
// 此代码由工具生成。
|
| 4 |
+
// 运行时版本: 4.0.30319.42000
|
| 5 |
+
//
|
| 6 |
+
// 对此文件的更改可能导致不正确的行为,如果
|
| 7 |
+
// 重新生成代码,则所做更改将丢失。
|
| 8 |
+
// </auto-generated>
|
| 9 |
+
//------------------------------------------------------------------------------
|
| 10 |
+
|
| 11 |
+
namespace BrowserTool.Properties
|
| 12 |
+
{
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
/// <summary>
|
| 16 |
+
/// 强类型资源类,用于查找本地化字符串等。
|
| 17 |
+
/// </summary>
|
| 18 |
+
// 此类是由 StronglyTypedResourceBuilder
|
| 19 |
+
// 类通过类似于 ResGen 或 Visual Studio 的工具自动生成的。
|
| 20 |
+
// 若要添加或移除成员,请编辑 .ResX 文件,然后重新运行 ResGen
|
| 21 |
+
// (以 /str 作为命令选项),或重新生成 VS 项目。
|
| 22 |
+
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")]
|
| 23 |
+
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
|
| 24 |
+
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
|
| 25 |
+
internal class Resources
|
| 26 |
+
{
|
| 27 |
+
|
| 28 |
+
private static global::System.Resources.ResourceManager resourceMan;
|
| 29 |
+
|
| 30 |
+
private static global::System.Globalization.CultureInfo resourceCulture;
|
| 31 |
+
|
| 32 |
+
[global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
|
| 33 |
+
internal Resources()
|
| 34 |
+
{
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
/// <summary>
|
| 38 |
+
/// 返回此类使用的缓存 ResourceManager 实例。
|
| 39 |
+
/// </summary>
|
| 40 |
+
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
|
| 41 |
+
internal static global::System.Resources.ResourceManager ResourceManager
|
| 42 |
+
{
|
| 43 |
+
get
|
| 44 |
+
{
|
| 45 |
+
if ((resourceMan == null))
|
| 46 |
+
{
|
| 47 |
+
global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("BrowserTool.Properties.Resources", typeof(Resources).Assembly);
|
| 48 |
+
resourceMan = temp;
|
| 49 |
+
}
|
| 50 |
+
return resourceMan;
|
| 51 |
+
}
|
| 52 |
+
}
|
| 53 |
+
|
| 54 |
+
/// <summary>
|
| 55 |
+
/// 重写当前线程的 CurrentUICulture 属性,对
|
| 56 |
+
/// 使用此强类型资源类的所有资源查找执行重写。
|
| 57 |
+
/// </summary>
|
| 58 |
+
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
|
| 59 |
+
internal static global::System.Globalization.CultureInfo Culture
|
| 60 |
+
{
|
| 61 |
+
get
|
| 62 |
+
{
|
| 63 |
+
return resourceCulture;
|
| 64 |
+
}
|
| 65 |
+
set
|
| 66 |
+
{
|
| 67 |
+
resourceCulture = value;
|
| 68 |
+
}
|
| 69 |
+
}
|
| 70 |
+
}
|
| 71 |
+
}
|
csharp/BrowserTool/BrowserTool/Properties/Resources.resx
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<?xml version="1.0" encoding="utf-8"?>
|
| 2 |
+
<root>
|
| 3 |
+
<!--
|
| 4 |
+
Microsoft ResX Schema
|
| 5 |
+
|
| 6 |
+
Version 2.0
|
| 7 |
+
|
| 8 |
+
The primary goals of this format is to allow a simple XML format
|
| 9 |
+
that is mostly human readable. The generation and parsing of the
|
| 10 |
+
various data types are done through the TypeConverter classes
|
| 11 |
+
associated with the data types.
|
| 12 |
+
|
| 13 |
+
Example:
|
| 14 |
+
|
| 15 |
+
... ado.net/XML headers & schema ...
|
| 16 |
+
<resheader name="resmimetype">text/microsoft-resx</resheader>
|
| 17 |
+
<resheader name="version">2.0</resheader>
|
| 18 |
+
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
|
| 19 |
+
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
|
| 20 |
+
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
|
| 21 |
+
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
|
| 22 |
+
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
|
| 23 |
+
<value>[base64 mime encoded serialized .NET Framework object]</value>
|
| 24 |
+
</data>
|
| 25 |
+
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
|
| 26 |
+
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
|
| 27 |
+
<comment>This is a comment</comment>
|
| 28 |
+
</data>
|
| 29 |
+
|
| 30 |
+
There are any number of "resheader" rows that contain simple
|
| 31 |
+
name/value pairs.
|
| 32 |
+
|
| 33 |
+
Each data row contains a name, and value. The row also contains a
|
| 34 |
+
type or mimetype. Type corresponds to a .NET class that support
|
| 35 |
+
text/value conversion through the TypeConverter architecture.
|
| 36 |
+
Classes that don't support this are serialized and stored with the
|
| 37 |
+
mimetype set.
|
| 38 |
+
|
| 39 |
+
The mimetype is used for serialized objects, and tells the
|
| 40 |
+
ResXResourceReader how to depersist the object. This is currently not
|
| 41 |
+
extensible. For a given mimetype the value must be set accordingly:
|
| 42 |
+
|
| 43 |
+
Note - application/x-microsoft.net.object.binary.base64 is the format
|
| 44 |
+
that the ResXResourceWriter will generate, however the reader can
|
| 45 |
+
read any of the formats listed below.
|
| 46 |
+
|
| 47 |
+
mimetype: application/x-microsoft.net.object.binary.base64
|
| 48 |
+
value : The object must be serialized with
|
| 49 |
+
: System.Serialization.Formatters.Binary.BinaryFormatter
|
| 50 |
+
: and then encoded with base64 encoding.
|
| 51 |
+
|
| 52 |
+
mimetype: application/x-microsoft.net.object.soap.base64
|
| 53 |
+
value : The object must be serialized with
|
| 54 |
+
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
|
| 55 |
+
: and then encoded with base64 encoding.
|
| 56 |
+
|
| 57 |
+
mimetype: application/x-microsoft.net.object.bytearray.base64
|
| 58 |
+
value : The object must be serialized into a byte array
|
| 59 |
+
: using a System.ComponentModel.TypeConverter
|
| 60 |
+
: and then encoded with base64 encoding.
|
| 61 |
+
-->
|
| 62 |
+
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
|
| 63 |
+
<xsd:element name="root" msdata:IsDataSet="true">
|
| 64 |
+
<xsd:complexType>
|
| 65 |
+
<xsd:choice maxOccurs="unbounded">
|
| 66 |
+
<xsd:element name="metadata">
|
| 67 |
+
<xsd:complexType>
|
| 68 |
+
<xsd:sequence>
|
| 69 |
+
<xsd:element name="value" type="xsd:string" minOccurs="0" />
|
| 70 |
+
</xsd:sequence>
|
| 71 |
+
<xsd:attribute name="name" type="xsd:string" />
|
| 72 |
+
<xsd:attribute name="type" type="xsd:string" />
|
| 73 |
+
<xsd:attribute name="mimetype" type="xsd:string" />
|
| 74 |
+
</xsd:complexType>
|
| 75 |
+
</xsd:element>
|
| 76 |
+
<xsd:element name="assembly">
|
| 77 |
+
<xsd:complexType>
|
| 78 |
+
<xsd:attribute name="alias" type="xsd:string" />
|
| 79 |
+
<xsd:attribute name="name" type="xsd:string" />
|
| 80 |
+
</xsd:complexType>
|
| 81 |
+
</xsd:element>
|
| 82 |
+
<xsd:element name="data">
|
| 83 |
+
<xsd:complexType>
|
| 84 |
+
<xsd:sequence>
|
| 85 |
+
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
|
| 86 |
+
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
|
| 87 |
+
</xsd:sequence>
|
| 88 |
+
<xsd:attribute name="name" type="xsd:string" msdata:Ordinal="1" />
|
| 89 |
+
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
|
| 90 |
+
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
|
| 91 |
+
</xsd:complexType>
|
| 92 |
+
</xsd:element>
|
| 93 |
+
<xsd:element name="resheader">
|
| 94 |
+
<xsd:complexType>
|
| 95 |
+
<xsd:sequence>
|
| 96 |
+
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
|
| 97 |
+
</xsd:sequence>
|
| 98 |
+
<xsd:attribute name="name" type="xsd:string" use="required" />
|
| 99 |
+
</xsd:complexType>
|
| 100 |
+
</xsd:element>
|
| 101 |
+
</xsd:choice>
|
| 102 |
+
</xsd:complexType>
|
| 103 |
+
</xsd:element>
|
| 104 |
+
</xsd:schema>
|
| 105 |
+
<resheader name="resmimetype">
|
| 106 |
+
<value>text/microsoft-resx</value>
|
| 107 |
+
</resheader>
|
| 108 |
+
<resheader name="version">
|
| 109 |
+
<value>2.0</value>
|
| 110 |
+
</resheader>
|
| 111 |
+
<resheader name="reader">
|
| 112 |
+
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
|
| 113 |
+
</resheader>
|
| 114 |
+
<resheader name="writer">
|
| 115 |
+
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
|
| 116 |
+
</resheader>
|
| 117 |
+
</root>
|