-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathui.ps1
263 lines (221 loc) · 8.21 KB
/
ui.ps1
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
Add-Type -AssemblyName PresentationFramework
Add-Type -AssemblyName PresentationCore
Add-Type -AssemblyName WindowsBase
Add-Type -AssemblyName System.Windows.Forms
Add-Type -Assembly System.Drawing
. .\spotify-c.ps1
. .\spotify-api.ps1
# # 1. 从文件中读取加密的密码
# $encryptedPassword = Get-Content -Path ".\pwd.txt"
# # 2. 将字符串转换为SecureString
# $securePassword = ConvertTo-SecureString -String $encryptedPassword
# # 3. 将加密的密码解密
# $accessToken = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($securePassword))
# # 输出解密后的密码
# Write-Output "Decrypted Password: $accessToken"
# 1. 从password.json 读取token和刷新token
$password = Get-Content -Path ".\password.json" | ConvertFrom-Json
$global:accessToken = $password.token
$reflushToken = $password.reflushToken
# 当前播放状态
$status = $false
# Write-Output $password.token
# Write-Output $password.reflushToken
# Write-Output $password | ConvertTo-Json
# 更新accessToken的示例
function UpdateAccessToken([ref]$accessToken) {
$token = update-Token -RefreshToken $reflushToken -ClientId $clientID -ClientSecret $clientSecret
if ($null -ne $token) {
$accessToken.Value = $token
}
}
# 获取主屏幕
$primaryScreen = [Windows.Forms.Screen]::PrimaryScreen
$screenWidth = $primaryScreen.Bounds.Width
$screenHeight = $primaryScreen.Bounds.Height
# 创建窗口
$window = New-Object Windows.Window
$window.ResizeMode = 'NoResize'
$window.WindowStyle = 'None'
$window.WindowStartupLocation = 'Manual'
# 设置窗口不在任务栏中显示
$window.ShowInTaskbar = $false
# 设置窗口一直置于顶层
$window.Topmost = $true
# 设置窗口透明
$window.AllowsTransparency = $true
# 设置窗口大小
$window.Width = 300
$window.Height = 80
# # 创建一个边框效果(Rectangle)
# $border = New-Object Windows.Shapes.Rectangle
# $border.Width = $window.Width
# $border.Height = $window.Height
# $border.Stroke = [Windows.Media.Brushes]::White # 设置边框颜色
# $border.StrokeThickness = 2 # 设置边框宽度
# 创建按钮1
$buttonPlay = New-Object Windows.Controls.Button
$buttonPlay.Content = "⏮️"
$buttonPlay.Add_Click({
# TODO: 添加播放逻辑
$code = Spotify_C -action p -accessToken $global:accessToken
switch ($code) {
401 {
$token = update-Token -RefreshToken $reflushToken -ClientId $clientID -ClientSecret $clientSecret
if ($token -eq $null) {
}
else {
UpdateAccessToken -accessToken ([ref]$global:accessToken)
$code = Spotify_C -action p -accessToken $global:accessToken
}
}
200 {
UpdateText -s ([ref]$status)
}
400 {
# 弹窗提醒
}
Default {}
}
})
# $icon = [System.Drawing.Icon]::ExtractAssociatedIcon("C:\Users\lwdut\Documents\autoStart\spotify-control\prev_icon.png")
# $bitmap = $icon.ToBitmap()
# # 转换 Bitmap 为 BitmapImage
# $bitmapStream = New-Object System.IO.MemoryStream
# $bitmap.Save($bitmapStream, [System.Drawing.Imaging.ImageFormat]::Png)
# $bitmapImage = New-Object System.Windows.Media.Imaging.BitmapImage
# $bitmapImage.BeginInit()
# $bitmapImage.StreamSource = $bitmapStream
# $bitmapImage.EndInit()
# # 创建 ImageBrush
# $imageBrush = New-Object System.Windows.Media.ImageBrush
# $imageBrush.ImageSource = $bitmapImage
# $buttonPlay.Background = $imageBrush
# $buttonPlay.Background = [System.Drawing.Image]::FromFile("C:\Users\lwdut\Documents\autoStart\spotify-control\prev_icon.png") # 将图标转换为位图并设置为按钮的背景图像
# $button.BackgroundImageLayout = "Center"
# $buttonPlay.Image = [System.Drawing.Icon]::ExtractAssociatedIcon("C:\Users\lwdut\Documents\autoStart\spotify-control\prev_icon.png").ToBitmap()
# 创建按钮2
$buttonPause = New-Object Windows.Controls.Button
$buttonPause.Content = "⏸️"
$buttonPause.Add_Click({
$action = "c"
if ($status) {
$action = "u"
}
# Write-Host $status
# Write-Host $action
$code = Spotify_C -action $action -accessToken $global:accessToken
switch ($code) {
401 {
$token = update-Token -RefreshToken $reflushToken -ClientId $clientID -ClientSecret $clientSecret
if ($token -eq $null) {
}
else {
# $accessToken = $token
UpdateAccessToken -accessToken ([ref]$global:accessToken)
$code = Spotify_C -action $action -accessToken $global:accessToken
}
}
200 {
UpdateText -s ([ref]$status)
}
400 {
# 弹窗提醒
}
Default {}
}
})
# 创建按钮3
$buttonStop = New-Object Windows.Controls.Button
$buttonStop.Content = "⏭️"
$buttonStop.Add_Click({
$code = Spotify_C -action n -accessToken $global:accessToken
switch ($code) {
401 {
$token = update-Token -RefreshToken $reflushToken -ClientId $clientID -ClientSecret $clientSecret
if ($token -eq $null) {
}
else {
# $accessToken = $token
UpdateAccessToken -accessToken ([ref]$global:accessToken)
$code = Spotify_C -action n -accessToken $global:accessToken
}
}
200 {
UpdateText -s ([ref]$status)
}
400 {
# 弹窗提醒
}
Default {}
}
})
# 创建一个堆栈面板,用于放置按钮
$stackPanel = New-Object Windows.Controls.StackPanel
$stackPanel.Children.Add($buttonPlay)
$stackPanel.Children.Add($buttonPause)
$stackPanel.Children.Add($buttonStop)
# 设置堆栈面板的对齐方式为居中
$stackPanel.VerticalAlignment = 'Center'
$stackPanel.HorizontalAlignment = 'Center'
$stackPanel.Orientation = 'Horizontal'
# 添加文本内容
$textBlock = New-Object Windows.Controls.TextBlock
$textBlock.Text = '待机中……'
$textBlock.LineHeight = 6.0
$textBlock.Foreground = [Windows.Media.Brushes]::White
$textBlock.FontSize = 12
# Function to update text based on play status
function UpdateText([ref]$s) {
$resp = Get-Play-Status -token $global:accessToken
if ($null -eq $resp) {
UpdateAccessToken -accessToken ([ref]$global:accessToken)
$resp = Get-Play-Status -token $global:accessToken
}
$s.Value = $resp.is_playing
if ($resp.is_playing){
$buttonPause.Content = "⏸️"
$textBlock.Text = $resp.name
# $textBlock.Text = "jjjjewqdkljshilked2wqolikjiolkewjoikjhniojhdne2iowhjnkhnkw"
}else{
$buttonPause.Content ='▶️'
$textBlock.Text = "待机中……"
}
}
UpdateText -s ([ref]$status)
# Create a DispatcherTimer
$timer = New-Object Windows.Threading.DispatcherTimer
$timer.Interval = [TimeSpan]::FromSeconds(50)
$timer.Add_Tick({
UpdateText -s ([ref]$status)
})
# Start the timer
$timer.Start()
# Start-ThreadJob -ScriptBlock {
# $resp = Get-Play-Status -token $accessToken
# $textBlock.Text = $resp.item.name
# } -ArgumentList $accessToken
# 创建父堆栈面板
$stackPanelP = New-Object Windows.Controls.StackPanel
$stackPanelP.Children.Add($textBlock)
$stackPanelP.Children.Add($stackPanel)
$stackPanelP.Background = [Windows.Media.Brushes]::Transparent
$textBlock.TextAlignment = "Center"
# $textBlock.Foreground = [Windows.Media.Brushes]::White
# 设置透明背景色
$window.Background = [Windows.Media.Brushes]::Transparent
# 设置窗口内容为堆栈面板
$window.Content = $stackPanelP
# # 设置窗口内容为边框效果和堆栈面板
# $window.Content = $border
# $window.Content.AddChild($stackPanelP)
# 设置窗口背景色
# $window.Background = [Windows.Media.Brushes]::Blue
# 获取窗口宽度和高度
$windowWidth = $window.Width
$windowHeight = $window.Height
# 设置窗口位置为右下角
$window.Left = $screenWidth - $windowWidth - 10
$window.Top = $screenHeight - $windowHeight - 10
# 显示窗口
$window.ShowDialog()