forked from Fei-Away/Codex-Dream-Skin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtray-dream-skin.ps1
More file actions
238 lines (219 loc) · 10.3 KB
/
Copy pathtray-dream-skin.ps1
File metadata and controls
238 lines (219 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
[CmdletBinding()]
param([int]$Port = 9335)
$ErrorActionPreference = 'Stop'
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
Add-Type -AssemblyName Microsoft.VisualBasic
. (Join-Path $PSScriptRoot 'common-windows.ps1')
. (Join-Path $PSScriptRoot 'theme-windows.ps1')
Assert-DreamSkinPort -Port $Port
$SkillRoot = Split-Path -Parent $PSScriptRoot
$StateRoot = Join-Path $env:LOCALAPPDATA 'CodexDreamSkin'
$paths = Initialize-DreamSkinThemeStore -SkillRoot $SkillRoot -StateRoot $StateRoot
$powershell = (Get-Command powershell.exe -ErrorAction Stop).Source
$startScript = Join-Path $PSScriptRoot 'start-dream-skin.ps1'
$restoreScript = Join-Path $PSScriptRoot 'restore-dream-skin.ps1'
$sid = [System.Security.Principal.WindowsIdentity]::GetCurrent().User.Value
$mutex = [System.Threading.Mutex]::new($false, "Local\CodexDreamSkin.$sid.Tray")
$acquired = $false
try {
try { $acquired = $mutex.WaitOne(0) } catch [System.Threading.AbandonedMutexException] { $acquired = $true }
if (-not $acquired) { exit 0 }
$notify = [System.Windows.Forms.NotifyIcon]::new()
$notify.Icon = [System.Drawing.SystemIcons]::Application
$notify.Text = 'Codex Dream Skin'
$notify.Visible = $true
$menu = [System.Windows.Forms.ContextMenuStrip]::new()
$notify.ContextMenuStrip = $menu
function Show-DreamSkinTrayError {
param([string]$Message)
[void][System.Windows.Forms.MessageBox]::Show(
$Message,
'Codex Dream Skin',
[System.Windows.Forms.MessageBoxButtons]::OK,
[System.Windows.Forms.MessageBoxIcon]::Error
)
}
function Start-DreamSkinPowerShell {
param([Parameter(Mandatory = $true)][string]$Script, [string[]]$Arguments = @())
$scriptToken = ConvertTo-DreamSkinProcessArgument -Value $Script
$argumentLine = '-NoProfile -ExecutionPolicy Bypass -File ' + $scriptToken
if ($Arguments.Count -gt 0) { $argumentLine += ' ' + ($Arguments -join ' ') }
Start-Process -FilePath $powershell -ArgumentList $argumentLine | Out-Null
}
function Add-DreamSkinTrayItem {
param(
[Parameter(Mandatory = $true)][AllowEmptyCollection()][System.Windows.Forms.ToolStripItemCollection]$Items,
[Parameter(Mandatory = $true)][string]$Text,
[AllowNull()][scriptblock]$Action,
[bool]$Enabled = $true
)
$item = [System.Windows.Forms.ToolStripMenuItem]::new($Text)
$item.Enabled = $Enabled
if ($null -ne $Action) {
$item.add_Click({
try { & $Action } catch { Show-DreamSkinTrayError -Message $_.Exception.Message }
}.GetNewClosure())
}
[void]$Items.Add($item)
return $item
}
function Show-DreamSkinOverlayOpacityDialog {
param([Parameter(Mandatory = $true)][int]$InitialPercent)
$form = [System.Windows.Forms.Form]::new()
$form.Text = '设置 Codex 遮罩强度'
$form.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen
$form.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedDialog
$form.ClientSize = [System.Drawing.Size]::new(420, 170)
$form.MaximizeBox = $false
$form.MinimizeBox = $false
$form.ShowInTaskbar = $false
$description = [System.Windows.Forms.Label]::new()
$description.Text = '统一调整顶部栏、侧栏和内容区的遮罩强度。'
$description.AutoSize = $true
$description.Location = [System.Drawing.Point]::new(18, 15)
$slider = [System.Windows.Forms.TrackBar]::new()
$slider.Minimum = 0
$slider.Maximum = 100
$slider.TickFrequency = 10
$slider.SmallChange = 1
$slider.LargeChange = 10
$slider.Value = [Math]::Max(0, [Math]::Min(100, $InitialPercent))
$slider.Location = [System.Drawing.Point]::new(18, 42)
$slider.Size = [System.Drawing.Size]::new(384, 45)
$valueLabel = [System.Windows.Forms.Label]::new()
$valueLabel.Text = "遮罩强度:$($slider.Value)%"
$valueLabel.AutoSize = $true
$valueLabel.Location = [System.Drawing.Point]::new(18, 92)
$slider.add_ValueChanged({ $valueLabel.Text = "遮罩强度:$($slider.Value)%" }.GetNewClosure())
$ok = [System.Windows.Forms.Button]::new()
$ok.Text = '确定'
$ok.DialogResult = [System.Windows.Forms.DialogResult]::OK
$ok.Location = [System.Drawing.Point]::new(238, 126)
$ok.Size = [System.Drawing.Size]::new(78, 28)
$cancel = [System.Windows.Forms.Button]::new()
$cancel.Text = '取消'
$cancel.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$cancel.Location = [System.Drawing.Point]::new(324, 126)
$cancel.Size = [System.Drawing.Size]::new(78, 28)
$form.AcceptButton = $ok
$form.CancelButton = $cancel
foreach ($control in @($description, $slider, $valueLabel, $ok, $cancel)) {
[void]$form.Controls.Add($control)
}
try {
if ($form.ShowDialog() -eq [System.Windows.Forms.DialogResult]::OK) { return [int]$slider.Value }
return $null
} finally {
$form.Dispose()
}
}
function Rebuild-DreamSkinTrayMenu {
$menu.Items.Clear()
$paused = Test-DreamSkinPaused -StateRoot $StateRoot
$state = $null
try { $state = Read-DreamSkinState -Path $paths.State } catch {}
$active = $null
try { $active = Read-DreamSkinTheme -ThemeDirectory $paths.Active -SkipImageMetadata } catch {}
$status = if ($paused) { '状态:已暂停' } elseif ($state) { '状态:运行中' } else { '状态:未运行' }
if ($null -ne $active -and $null -ne $active.Theme -and $active.Theme.name) {
$status += " · $($active.Theme.name)"
}
$null = Add-DreamSkinTrayItem -Items $menu.Items -Text $status -Action $null -Enabled $false
[void]$menu.Items.Add([System.Windows.Forms.ToolStripSeparator]::new())
$null = Add-DreamSkinTrayItem -Items $menu.Items -Text '应用或重新应用' -Action {
Set-DreamSkinPaused -Paused $false -StateRoot $StateRoot | Out-Null
Start-DreamSkinPowerShell -Script $startScript -Arguments @('-Port', "$Port", '-PromptRestart')
}
$pauseText = if ($paused) { '继续显示皮肤' } else { '暂停皮肤' }
$nextPaused = -not $paused
$pauseAction = {
Set-DreamSkinPaused -Paused $nextPaused -StateRoot $StateRoot | Out-Null
}.GetNewClosure()
$null = Add-DreamSkinTrayItem -Items $menu.Items -Text $pauseText -Action $pauseAction
$null = Add-DreamSkinTrayItem -Items $menu.Items -Text '更换背景图' -Action {
$dialog = [System.Windows.Forms.OpenFileDialog]::new()
$dialog.Title = '选择 Codex Dream Skin 背景图'
$dialog.Filter = 'Image files|*.png;*.jpg;*.jpeg;*.webp|All files|*.*'
$dialog.Multiselect = $false
try {
if ($dialog.ShowDialog() -eq [System.Windows.Forms.DialogResult]::OK) {
$null = Set-DreamSkinActiveTheme -ImagePath $dialog.FileName -Theme $null -StateRoot $StateRoot
Set-DreamSkinPaused -Paused $false -StateRoot $StateRoot | Out-Null
$notify.ShowBalloonTip(1800, 'Codex Dream Skin', '背景图已更新。', [System.Windows.Forms.ToolTipIcon]::Info)
}
} finally {
$dialog.Dispose()
}
}
$overlayPercent = [int][Math]::Round((Get-DreamSkinOverlayOpacity -StateRoot $StateRoot) * 100)
$overlayAction = {
$selectedPercent = Show-DreamSkinOverlayOpacityDialog -InitialPercent $overlayPercent
if ($null -eq $selectedPercent) { return }
$null = Set-DreamSkinOverlayOpacity -Opacity ($selectedPercent / 100.0) -StateRoot $StateRoot
$notify.ShowBalloonTip(
1800,
'Codex Dream Skin',
"遮罩强度已更新为 $selectedPercent%。",
[System.Windows.Forms.ToolTipIcon]::Info
)
}.GetNewClosure()
$null = Add-DreamSkinTrayItem -Items $menu.Items `
-Text "设置遮罩强度($overlayPercent%)" -Action $overlayAction
$null = Add-DreamSkinTrayItem -Items $menu.Items -Text '保存当前主题' -Action {
$name = [Microsoft.VisualBasic.Interaction]::InputBox('输入主题名称:', '保存 Codex Dream Skin 主题', '')
if ($name.Trim()) {
$saved = Save-DreamSkinCurrentTheme -Name $name -StateRoot $StateRoot
$notify.ShowBalloonTip(1800, 'Codex Dream Skin', "已保存:$($saved.Theme.name)", [System.Windows.Forms.ToolTipIcon]::Info)
}
}
$savedMenu = [System.Windows.Forms.ToolStripMenuItem]::new('已保存主题')
$savedThemes = @(Get-DreamSkinSavedThemes -StateRoot $StateRoot -SkipImageMetadata)
if ($savedThemes.Count -eq 0) {
$empty = [System.Windows.Forms.ToolStripMenuItem]::new('暂无已保存主题')
$empty.Enabled = $false
[void]$savedMenu.DropDownItems.Add($empty)
} else {
foreach ($saved in $savedThemes) {
$savedPath = $saved.Path
$savedName = $saved.Name
$savedAction = {
$null = Use-DreamSkinSavedTheme -ThemeDirectory $savedPath -StateRoot $StateRoot
Set-DreamSkinPaused -Paused $false -StateRoot $StateRoot | Out-Null
$notify.ShowBalloonTip(1800, 'Codex Dream Skin', "已应用:$savedName", [System.Windows.Forms.ToolTipIcon]::Info)
}.GetNewClosure()
$null = Add-DreamSkinTrayItem -Items $savedMenu.DropDownItems -Text $savedName -Action $savedAction
}
}
[void]$menu.Items.Add($savedMenu)
$null = Add-DreamSkinTrayItem -Items $menu.Items -Text '打开图片文件夹' -Action {
Start-Process -FilePath explorer.exe -ArgumentList @($paths.Images) | Out-Null
}
[void]$menu.Items.Add([System.Windows.Forms.ToolStripSeparator]::new())
$null = Add-DreamSkinTrayItem -Items $menu.Items -Text '完全恢复 Codex' -Action {
Start-DreamSkinPowerShell -Script $restoreScript -Arguments @(
'-Port', "$Port", '-RestoreBaseTheme', '-PromptRestart'
)
$notify.Visible = $false
[System.Windows.Forms.Application]::Exit()
}
$null = Add-DreamSkinTrayItem -Items $menu.Items -Text '退出托盘' -Action {
$notify.Visible = $false
[System.Windows.Forms.Application]::Exit()
}
}
$menu.add_Opening({ Rebuild-DreamSkinTrayMenu })
$notify.add_DoubleClick({
try {
Set-DreamSkinPaused -Paused $false -StateRoot $StateRoot | Out-Null
Start-DreamSkinPowerShell -Script $startScript -Arguments @('-Port', "$Port", '-PromptRestart')
} catch {
Show-DreamSkinTrayError -Message $_.Exception.Message
}
})
[System.Windows.Forms.Application]::Run()
} finally {
if ($null -ne $notify) { $notify.Dispose() }
if ($acquired) { try { $mutex.ReleaseMutex() } catch {} }
$mutex.Dispose()
}