Skip to content

Commit 5386fa1

Browse files
committed
新增 插件配置项目支持按钮
新增 手动检查更新按钮
1 parent 0c951c2 commit 5386fa1

File tree

9 files changed

+464
-353
lines changed

9 files changed

+464
-353
lines changed

Core.Window/ApplicationService.cs

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
using System.IO;
22
using Avalonia;
3+
using Avalonia.Controls.Notifications;
34
using Core.SDKs.Services;
45
using Core.Services;
56
using Core.Services.Config;
7+
using Core.Utils;
8+
using KitopiaAvalonia.Services;
9+
using Microsoft.Extensions.DependencyInjection;
610
using Microsoft.Win32;
711
using PluginCore;
812
using Serilog;
@@ -134,4 +138,81 @@ public bool ChangeAutoStart(bool autoStart)
134138

135139
return true;
136140
}
141+
142+
public async Task CheckUpdate()
143+
{
144+
var gitHubUpdateService = ServiceManager.Services.GetService<GitHubUpdateService>();
145+
var (hasUpdate, latestVersion, downloadUrl, releaseNotes) = await gitHubUpdateService!.CheckForUpdatesAsync();
146+
if (hasUpdate && !string.IsNullOrEmpty(downloadUrl))
147+
{
148+
Log.Information($"发现新版本:{latestVersion}");
149+
var dialog = new DialogContent()
150+
{
151+
Title = $"Kitopia更新 - 发现新版本 {latestVersion}",
152+
Content = $"发现新版本 {latestVersion},是否前往下载?\n\n更新内容:\n{releaseNotes ?? "无更新说明"}",
153+
PrimaryButtonText = "下载并更新",
154+
SecondaryButtonText = "取消",
155+
PrimaryAction = async () =>
156+
{
157+
try
158+
{
159+
var toastService = ServiceManager.Services.GetService<IToastService>();
160+
var tempPath = Path.Combine(Path.GetTempPath(), $"Kitopia_{latestVersion}_Installer.exe");
161+
162+
using var client = new HttpClient();
163+
using var response = await client.GetAsync(downloadUrl, HttpCompletionOption.ResponseHeadersRead);
164+
response.EnsureSuccessStatusCode();
165+
166+
var totalBytes = response.Content.Headers.ContentLength ?? -1L;
167+
var canReportProgress = totalBytes != -1;
168+
169+
await using var contentStream = await response.Content.ReadAsStreamAsync();
170+
var fileStream = new FileStream(tempPath, FileMode.Create, FileAccess.Write, FileShare.None, 8192, true);
171+
172+
var buffer = new byte[8192];
173+
long totalRead = 0;
174+
int bytesRead;
175+
var lastProgress = -1;
176+
177+
toastService!.Show("更新", "开始下载更新...");
178+
179+
while ((bytesRead = await contentStream.ReadAsync(buffer, 0, buffer.Length)) > 0)
180+
{
181+
await fileStream.WriteAsync(buffer, 0, bytesRead);
182+
totalRead += bytesRead;
183+
184+
if (canReportProgress)
185+
{
186+
var progress = (int)((double)totalRead / totalBytes * 100);
187+
if (progress > lastProgress && progress % 10 == 0) // Report every 10%
188+
{
189+
lastProgress = progress;
190+
toastService.Show("更新", $"下载进度: {progress}%");
191+
}
192+
}
193+
}
194+
await fileStream.DisposeAsync();
195+
toastService.Show("更新", "下载完成,正在启动安装程序...");
196+
await Task.Delay(1000);
197+
// Close application and start installer
198+
ServiceManager.Services.GetService<ISearchItemTool>()!.OpenFile(tempPath);
199+
await Task.Delay(2000);
200+
Environment.Exit(0);
201+
}
202+
catch (Exception ex)
203+
{
204+
Log.Error(ex, "更新失败");
205+
ServiceManager.Services.GetService<IToastService>()!.Show("更新失败", $"下载出错: {ex.Message}", NotificationType.Error);
206+
}
207+
}
208+
};
209+
await ((IContentDialog)ServiceManager.Services!.GetService(typeof(IContentDialog))!).ShowDialogAsync(null,
210+
dialog);
211+
}
212+
else
213+
{
214+
var toastService = ServiceManager.Services.GetService<IToastService>();
215+
toastService.Show("更新", "无更新", NotificationType.Information);
216+
}
217+
}
137218
}

KitopiaAvalonia/Services/GitHubUpdateService.cs renamed to Core.Window/GitHubUpdateService.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Net.Http.Headers;
55
using System.Reflection;
66
using System.Threading.Tasks;
7+
using Core.Services.Plugin;
78
using Newtonsoft.Json.Linq;
89
using Serilog;
910

@@ -19,11 +20,9 @@ public class GitHubUpdateService
1920
{
2021
try
2122
{
22-
using var client = new HttpClient();
23-
client.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue(UserAgent, "1.0"));
24-
23+
2524
var url = $"https://update.kitopia.top/repos/{Owner}/{Repo}/releases";
26-
var response = await client.GetAsync(url);
25+
var response = await PluginManager._httpClient.GetAsync(url);
2726

2827
if (!response.IsSuccessStatusCode)
2928
{

Core.Window/ToastService.cs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public void Show(string header, string text, NotificationType notificationType =
5757
windowToastManager.Position = NotificationPosition.BottomRight;
5858
Interlocked.Add(ref counter, 1);
5959
windowToastManager!.Show(
60-
new Notification("Kitopia", $"{header}{text}"),
60+
new Notification($"Kitopia{header}", text),
6161
showIcon: true,
6262
showClose: true,
6363
onClose: () =>
@@ -70,11 +70,21 @@ public void Show(string header, string text, NotificationType notificationType =
7070
}
7171
else
7272
{
73-
var windowToastManager = WindowToastManager.TryGetToastManager(windowFromIntPtr, out var manager)
74-
? manager
75-
: new WindowToastManager(windowFromIntPtr);
73+
// var windowToastManager = WindowToastManager.TryGetToastManager(windowFromIntPtr, out var manager)
74+
// ? manager
75+
// : new WindowToastManager(windowFromIntPtr);
76+
// windowToastManager!.Show(
77+
// new Toast($"{header} {text}"),
78+
// showIcon: true,
79+
// showClose: true,
80+
// type: notificationType);
81+
var windowToastManager =
82+
WindowNotificationManager.TryGetNotificationManager(windowFromIntPtr, out var manager)
83+
? manager
84+
: new WindowNotificationManager(windowFromIntPtr);
85+
windowToastManager.Position = NotificationPosition.BottomRight;
7686
windowToastManager!.Show(
77-
new Toast($"{header}{text}"),
87+
new Notification($"Kitopia{header}", text),
7888
showIcon: true,
7989
showClose: true,
8090
type: notificationType);

Core/Services/Config/KitopiaConfig.cs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public class KitopiaConfig : ConfigBase
3131
public bool canReadClipboard = true;
3232

3333
[ConfigFieldCategory("搜索框")]
34-
[ConfigField("搜索框快捷键", "显示搜索框快捷键", 0xF4B8, ConfigFieldType.快捷键, action: "searchHotKeyAction")]
34+
[ConfigField("搜索框快捷键", "显示搜索框快捷键", 0xF4B8, ConfigFieldType.快捷键, actionName: "searchHotKeyAction")]
3535
public HotKeyModel searchHotKey = new()
3636
{
3737
IsEnabled = true,
@@ -95,7 +95,7 @@ public class KitopiaConfig : ConfigBase
9595
[ConfigFieldCategory("鼠标快捷操作")] [ConfigField("允许对鼠标进行捕获", "允许对鼠标进行捕获(禁用后鼠标快捷键无效)", 0xE61C, ConfigFieldType.布尔)]
9696
public bool mouseCapture = true;
9797

98-
[ConfigField("鼠标快捷键", "激活鼠标快捷菜单快捷键", 0xF4B8, ConfigFieldType.快捷键, action: "mouseHotkeyAction")]
98+
[ConfigField("鼠标快捷键", "激活鼠标快捷菜单快捷键", 0xF4B8, ConfigFieldType.快捷键, actionName: "mouseHotkeyAction")]
9999
public HotKeyModel mouseHotkey = new()
100100
{
101101
IsEnabled = true,
@@ -111,13 +111,14 @@ public class KitopiaConfig : ConfigBase
111111
public List<string> mouseQuickItems = new();
112112

113113

114-
[ConfigFieldCategory("截图")] [ConfigField("截图直接复制到剪贴板", "截图直接复制到剪贴板,不显示工具栏", 0xE61C, ConfigFieldType.布尔)]
114+
[ConfigFieldCategory("截图")]
115+
[ConfigField("截图直接复制到剪贴板", "截图直接复制到剪贴板,不显示工具栏", 0xE61C, ConfigFieldType.布尔)]
115116
public bool 截图直接复制到剪贴板 = false;
116117

117-
[ConfigField("截图方法", "使用特定的截图方法,某些情况下截图失败请尝试切换", 0xE61C, ConfigFieldType.自定义选项, action: "截图方法列表")]
118+
[ConfigField("截图方法", "使用特定的截图方法,某些情况下截图失败请尝试切换", 0xE61C, ConfigFieldType.自定义选项, actionName: "截图方法列表")]
118119
public string 截图方法 = "WGC";
119120

120-
[ConfigField("截图快捷键", "修改截图快捷键", 0xF4B8, ConfigFieldType.快捷键, action: "screenShotHotKeyAction")]
121+
[ConfigField("截图快捷键", "修改截图快捷键", 0xF4B8, ConfigFieldType.快捷键, actionName: "screenShotHotKeyAction")]
121122
public HotKeyModel screenShotHotKey = new()
122123
{
123124
IsEnabled = true,
@@ -126,6 +127,13 @@ public class KitopiaConfig : ConfigBase
126127
IsSelectShift = false, SelectKey = EKey.Q
127128
};
128129

130+
[ConfigFieldCategory("更多")]
131+
[ConfigField("检查更新", "立即检查更新", 0xE974, ConfigFieldType.按钮,actionName: "检查更新")]
132+
public async Task CheckUpdate()
133+
{
134+
await ServiceManager.Services.GetService<IApplicationService>()!.CheckUpdate();
135+
}
136+
129137
public override void BeforeLoad()
130138
{
131139
invokes.Add("screenShotHotKeyAction", new Action<HotKeyModel>(e =>

Core/Services/IApplicationService.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ public interface IApplicationService
77
public bool ChangeAutoStart(bool autoStart);
88
public void Restart();
99
public void Stop();
10+
public Task CheckUpdate();
1011
}

0 commit comments

Comments
 (0)