Skip to content

基于自定义规则的番剧采集APP,支持流媒体在线观看,支持弹幕,支持实时超分辨率。

feat: show shortcuts dialog on first launch (#1915)

Wong Wing Chun9ce11a9dbadb
diff --git a/lib/pages/init_page.dart b/lib/pages/init_page.dart
index 9f4c9479..b2d4f1e2 100644
--- a/lib/pages/init_page.dart
+++ b/lib/pages/init_page.dart
@@ -16,6 +16,7 @@ import 'package:kazumi/bean/settings/theme_provider.dart';
import 'package:kazumi/shaders/shaders_controller.dart';
import 'package:kazumi/pages/download/download_controller.dart';
import 'package:kazumi/utils/background_download_service.dart';
+import 'package:kazumi/utils/windows_shortcut.dart';
class InitPage extends StatefulWidget {
const InitPage({super.key});
@@ -55,6 +56,7 @@ class _InitPageState extends State<InitPage> {
await _checkRunningOnX11();
await _pluginInit();
+ await _showShortcutDialog();
_startDefaultPage();
// delay to ensure that the default page is fully loaded
@@ -193,6 +195,35 @@ class _InitPageState extends State<InitPage> {
}
}
+ Future<void> _showShortcutDialog() async {
+ if (!Platform.isWindows) return;
+ if (setting.get(SettingBoxKey.shortcutDialogShown, defaultValue: false)) return;
+
+ final create = await KazumiDialog.show<bool>(
+ clickMaskDismiss: false,
+ builder: (context) => AlertDialog(
+ title: const Text('创建桌面快捷方式'),
+ content: const Text('是否在桌面创建 Kazumi 的快捷方式?'),
+ actions: [
+ TextButton(
+ onPressed: () => KazumiDialog.dismiss(popWith: false),
+ child: Text('暂不创建', style: TextStyle(color: Theme.of(context).colorScheme.outline)),
+ ),
+ TextButton(
+ onPressed: () => KazumiDialog.dismiss(popWith: true),
+ child: const Text('创建'),
+ ),
+ ],
+ ),
+ );
+
+ await setting.put(SettingBoxKey.shortcutDialogShown, true);
+ if (create ?? false) {
+ final success = await WindowsShortcut.createDesktopShortcut();
+ KazumiDialog.showToast(message: success ? '桌面快捷方式已创建' : '桌面快捷方式创建失败');
+ }
+ }
+
Future<void> _pluginInit() async {
String statementsText = '';
try {
diff --git a/lib/utils/storage.dart b/lib/utils/storage.dart
index aff72d20..87dd43e1 100644
--- a/lib/utils/storage.dart
+++ b/lib/utils/storage.dart
@@ -340,5 +340,6 @@ class SettingBoxKey {
showRating = 'showRating',
downloadParallelEpisodes = 'downloadParallelEpisodes',
downloadParallelSegments = 'downloadParallelSegments',
- downloadDanmaku = 'downloadDanmaku';
+ downloadDanmaku = 'downloadDanmaku',
+ shortcutDialogShown = 'shortcutDialogShown';
}
diff --git a/lib/utils/windows_shortcut.dart b/lib/utils/windows_shortcut.dart
new file mode 100644
index 00000000..36cd729b
--- /dev/null
+++ b/lib/utils/windows_shortcut.dart
@@ -0,0 +1,17 @@
+import 'dart:io';
+import 'package:flutter/foundation.dart';
+import 'package:flutter/services.dart';
+
+class WindowsShortcut {
+ static const _channel = MethodChannel('com.predidit.kazumi/shortcut');
+
+ static Future<bool> createDesktopShortcut() async {
+ if (!Platform.isWindows) return false;
+ try {
+ return await _channel.invokeMethod<bool>('createDesktopShortcut') ?? false;
+ } catch (e) {
+ debugPrint('Failed to create desktop shortcut: $e');
+ return false;
+ }
+ }
+}
\ No newline at end of file
diff --git a/windows/runner/CMakeLists.txt b/windows/runner/CMakeLists.txt
index 50de5963..eec604b8 100644
--- a/windows/runner/CMakeLists.txt
+++ b/windows/runner/CMakeLists.txt
@@ -12,6 +12,7 @@ add_executable(${BINARY_NAME} WIN32
"utils.cpp"
"external_player_utils.cpp"
"fullscreen_utils.cpp"
+ "shortcut_utils.cpp"
"win32_window.cpp"
"${FLUTTER_MANAGED_DIR}/generated_plugin_registrant.cc"
"Runner.rc"
diff --git a/windows/runner/flutter_window.cpp b/windows/runner/flutter_window.cpp
index c2ef7ef9..f16880f9 100644
--- a/windows/runner/flutter_window.cpp
+++ b/windows/runner/flutter_window.cpp
@@ -1,6 +1,7 @@
#include "flutter_window.h"
#include "fullscreen_utils.h"
#include "external_player_utils.h"
+#include "shortcut_utils.h"
#include <optional>
#include <flutter/method_channel.h>
@@ -50,6 +51,9 @@ bool FlutterWindow::OnCreate() {
// Register Storage MethodChannel
RegisterStorageChannel();
+ // Register Shortcut MethodChannel
+ RegisterShortcutChannel();
+
return true;
}
@@ -151,3 +155,24 @@ void FlutterWindow::RegisterStorageChannel() {
}
});
}
+
+// Shortcut MethodChannel setup
+void FlutterWindow::RegisterShortcutChannel() {
+ auto channel = std::make_unique<flutter::MethodChannel<flutter::EncodableValue>>(
+ flutter_controller_->engine()->messenger(), "com.predidit.kazumi/shortcut",
+ &flutter::StandardMethodCodec::GetInstance());
+
+ channel->SetMethodCallHandler([](const auto& call, auto result) {
+ if (call.method_name() != "createDesktopShortcut") {
+ result->NotImplemented();
+ return;
+ }
+
+ bool success = ShortcutUtils::CreateDesktopShortcut(L"Kazumi", L"Kazumi - Anime Player");
+ if (success) {
+ result->Success(flutter::EncodableValue(true));
+ } else {
+ result->Error("Failed", "Failed to create desktop shortcut");
+ }
+ });
+}
diff --git a/windows/runner/flutter_window.h b/windows/runner/flutter_window.h
index 81a932c9..f523a885 100644
--- a/windows/runner/flutter_window.h
+++ b/windows/runner/flutter_window.h
@@ -34,6 +34,9 @@ class FlutterWindow : public Win32Window {
// Register Storage MethodChannel
void RegisterStorageChannel();
+
+ // Register Shortcut MethodChannel
+ void RegisterShortcutChannel();
};
#endif // RUNNER_FLUTTER_WINDOW_H_
diff --git a/windows/runner/shortcut_utils.cpp b/windows/runner/shortcut_utils.cpp
new file mode 100644
index 00000000..192256b7
--- /dev/null
+++ b/windows/runner/shortcut_utils.cpp
@@ -0,0 +1,67 @@
+// shortcut_utils.cpp - Windows desktop shortcut utilities
+
+#include "shortcut_utils.h"
+
+#include <shobjidl.h>
+#include <shlobj.h>
+#include <propkey.h>
+#include <propvarutil.h>
+#include <appmodel.h>
+
+bool ShortcutUtils::CreateDesktopShortcut(const std::wstring& shortcutName, const std::wstring& description) {
+ wchar_t desktopPath[MAX_PATH];
+ if (SHGetFolderPathW(nullptr, CSIDL_DESKTOP, nullptr, 0, desktopPath) != S_OK) return false;
+
+ std::wstring shortcutPath = std::wstring(desktopPath) + L"\\" + shortcutName + L".lnk";
+ if (GetFileAttributesW(shortcutPath.c_str()) != INVALID_FILE_ATTRIBUTES) return true;
+
+ // COM is already initialized in main.cpp, do not re-initialize
+ IShellLinkW* pShellLink = nullptr;
+ HRESULT hr = CoCreateInstance(CLSID_ShellLink, nullptr, CLSCTX_INPROC_SERVER, IID_IShellLinkW, (void**)&pShellLink);
+ if (FAILED(hr)) return false;
+
+ pShellLink->SetDescription(description.c_str());
+
+ // Check if running as MSIX package
+ UINT32 length = 0;
+ std::wstring aumid;
+ if (GetCurrentPackageFamilyName(&length, nullptr) == ERROR_INSUFFICIENT_BUFFER) {
+ aumid.resize(length);
+ if (GetCurrentPackageFamilyName(&length, &aumid[0]) == ERROR_SUCCESS) {
+ if (!aumid.empty() && aumid.back() == L'\0') aumid.pop_back();
+ aumid += L"!kazumi";
+ }
+ }
+
+ bool success = false;
+ IPersistFile* pPersistFile = nullptr;
+
+ if (!aumid.empty()) {
+ // MSIX: use shell:AppsFolder\AUMID
+ pShellLink->SetPath((L"shell:AppsFolder\\" + aumid).c_str());
+
+ IPropertyStore* pPropertyStore = nullptr;
+ if (SUCCEEDED(pShellLink->QueryInterface(IID_IPropertyStore, (void**)&pPropertyStore))) {
+ PROPVARIANT propVar;
+ if (SUCCEEDED(InitPropVariantFromString(aumid.c_str(), &propVar))) {
+ pPropertyStore->SetValue(PKEY_AppUserModel_ID, propVar);
+ PropVariantClear(&propVar);
+ }
+ pPropertyStore->Commit();
+ pPropertyStore->Release();
+ }
+ } else {
+ // Portable: use executable path
+ wchar_t exePath[MAX_PATH];
+ GetModuleFileNameW(nullptr, exePath, MAX_PATH);
+ pShellLink->SetPath(exePath);
+ }
+
+ if (SUCCEEDED(pShellLink->QueryInterface(IID_IPersistFile, (void**)&pPersistFile))) {
+ success = SUCCEEDED(pPersistFile->Save(shortcutPath.c_str(), TRUE));
+ pPersistFile->Release();
+ }
+
+ pShellLink->Release();
+ return success;
+}
\ No newline at end of file
diff --git a/windows/runner/shortcut_utils.h b/windows/runner/shortcut_utils.h
new file mode 100644
index 00000000..7418723a
--- /dev/null
+++ b/windows/runner/shortcut_utils.h
@@ -0,0 +1,13 @@
+// shortcut_utils.h - Windows desktop shortcut utilities
+
+#ifndef SHORTCUT_UTILS_H_
+#define SHORTCUT_UTILS_H_
+
+#include <string>
+
+class ShortcutUtils {
+ public:
+ static bool CreateDesktopShortcut(const std::wstring& shortcutName, const std::wstring& description);
+};
+
+#endif // SHORTCUT_UTILS_H_
\ No newline at end of file