Skip to content

Commit

Permalink
new: migrate app_links
Browse files Browse the repository at this point in the history
  • Loading branch information
lollipopkit committed May 7, 2024
1 parent 9f6a698 commit 97ad12e
Show file tree
Hide file tree
Showing 14 changed files with 121 additions and 64 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Please don't use it in production environment, and don't use it for important da
- Text / Image / Audio chat. (Web only support text)
- Share chat by picture.
- Render code block / latex formula.
- Uni-Link, eg: `lk-gptbox://chat/new?msg=hello` (except Linux)
- Url Scheme, eg: `lk-gptbox://chat/new?msg=hello`
- All platforms support.
- Sync with WebDAV / iCloud.

Expand Down
2 changes: 1 addition & 1 deletion README_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
-[ChatGPT Next Web 备份](https://github.com/ChatGPTNextWeb/ChatGPT-Next-Web) / [OpenAI导出文件](https://chat.openai.com) 恢复
- 文本 / 图片 / 音频聊天(网页版仅支持文本)
- 以图片形式分享聊天
- Uni-Link,例如:`lk-gptbox://chat/new?msg=你好` (Linux 除外)
- Url Scheme,例如:`lk-gptbox://chat/new?msg=你好`
- 与 WebDAV / iCloud 同步
- 全平台支持
- 渲染 代码块 / LaTeX 公式
Expand Down
28 changes: 28 additions & 0 deletions lib/core/util/platform/win.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import 'dart:io';

import 'package:flutter_chatgpt/core/util/platform/base.dart';
import 'package:win32_registry/win32_registry.dart';

abstract final class Win32 {
static Future<void> registerProtocol(String scheme) async {
if (isWindows) return;

String appPath = Platform.resolvedExecutable;
String protocolRegKey = 'Software\\Classes\\$scheme';
RegistryValue protocolRegValue = const RegistryValue(
'URL Protocol',
RegistryValueType.string,
'',
);
String protocolCmdRegKey = 'shell\\open\\command';
RegistryValue protocolCmdRegValue = RegistryValue(
'',
RegistryValueType.string,
'"$appPath" "%1"',
);

final regKey = Registry.currentUser.createKey(protocolRegKey);
regKey.createValue(protocolRegValue);
regKey.createKey(protocolCmdRegKey).createValue(protocolCmdRegValue);
}
}
4 changes: 2 additions & 2 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import 'package:flutter_chatgpt/core/build_mode.dart';
import 'package:flutter_chatgpt/core/logger.dart';
import 'package:flutter_chatgpt/core/util/datetime.dart';
import 'package:flutter_chatgpt/core/util/platform/base.dart';
import 'package:flutter_chatgpt/core/util/platform/win.dart';
import 'package:flutter_chatgpt/core/util/sync/base.dart';
import 'package:flutter_chatgpt/data/model/chat/config.dart';
import 'package:flutter_chatgpt/data/model/chat/history.dart';
Expand All @@ -20,7 +21,6 @@ import 'package:flutter_chatgpt/data/store/all.dart';
import 'package:flutter_chatgpt/view/widget/appbar.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:logging/logging.dart';
import 'package:uni_links_desktop/uni_links_desktop.dart';
import 'package:window_manager/window_manager.dart';

Future<void> main() async {
Expand Down Expand Up @@ -65,7 +65,7 @@ Future<void> _initApp() async {
SyncService.sync(force: true);

if (isWindows) {
registerProtocol('lk-gptbox');
Win32.registerProtocol('lk-gptbox');
}

Paths.createAll();
Expand Down
11 changes: 6 additions & 5 deletions lib/view/page/home/home.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'dart:collection';
import 'dart:io';

import 'package:after_layout/after_layout.dart';
import 'package:app_links/app_links.dart';
import 'package:dart_openai/dart_openai.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
Expand Down Expand Up @@ -58,7 +59,6 @@ import 'package:screenshot/screenshot.dart';
import 'package:share_plus/share_plus.dart';
import 'package:shortid/shortid.dart';
import 'package:tiktoken/tiktoken.dart';
import 'package:uni_links/uni_links.dart';

part 'chat.dart';
part 'history.dart';
Expand Down Expand Up @@ -89,6 +89,7 @@ class HomePage extends StatefulWidget {
class _HomePageState extends State<HomePage>
with AfterLayoutMixin<HomePage>, TickerProviderStateMixin {
Timer? _refreshTimeTimer;
final _appLink = AppLinks();

@override
void initState() {
Expand All @@ -100,7 +101,7 @@ class _HomePageState extends State<HomePage>
if (mounted) _timeRN.build();
},
);
_initUniLinks();
_initUrlScheme();
AudioCard.listenAudioPlayer();
}

Expand Down Expand Up @@ -233,13 +234,13 @@ class _HomePageState extends State<HomePage>
}
}

Future<void> _initUniLinks() async {
Future<void> _initUrlScheme() async {
if (isWeb) {
final uri = await getInitialUri();
final uri = await _appLink.getInitialLink();
if (uri == null) return;
AppLink.handle(context, uri);
} else {
uriLinkStream.listen((Uri? uri) {
_appLink.uriLinkStream.listen((Uri? uri) {
if (uri == null) return;
if (!mounted) return;
AppLink.handle(context, uri);
Expand Down
4 changes: 4 additions & 0 deletions linux/flutter/generated_plugin_registrant.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include <audioplayers_linux/audioplayers_linux_plugin.h>
#include <file_selector_linux/file_selector_plugin.h>
#include <gtk/gtk_plugin.h>
#include <screen_retriever/screen_retriever_plugin.h>
#include <url_launcher_linux/url_launcher_plugin.h>
#include <window_manager/window_manager_plugin.h>
Expand All @@ -19,6 +20,9 @@ void fl_register_plugins(FlPluginRegistry* registry) {
g_autoptr(FlPluginRegistrar) file_selector_linux_registrar =
fl_plugin_registry_get_registrar_for_plugin(registry, "FileSelectorPlugin");
file_selector_plugin_register_with_registrar(file_selector_linux_registrar);
g_autoptr(FlPluginRegistrar) gtk_registrar =
fl_plugin_registry_get_registrar_for_plugin(registry, "GtkPlugin");
gtk_plugin_register_with_registrar(gtk_registrar);
g_autoptr(FlPluginRegistrar) screen_retriever_registrar =
fl_plugin_registry_get_registrar_for_plugin(registry, "ScreenRetrieverPlugin");
screen_retriever_plugin_register_with_registrar(screen_retriever_registrar);
Expand Down
1 change: 1 addition & 0 deletions linux/flutter/generated_plugins.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
list(APPEND FLUTTER_PLUGIN_LIST
audioplayers_linux
file_selector_linux
gtk
screen_retriever
url_launcher_linux
window_manager
Expand Down
12 changes: 10 additions & 2 deletions linux/my_application.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ G_DEFINE_TYPE(MyApplication, my_application, GTK_TYPE_APPLICATION)
// Implements GApplication::activate.
static void my_application_activate(GApplication* application) {
MyApplication* self = MY_APPLICATION(application);

// For registering URL schemes
GList* windows = gtk_application_get_windows(GTK_APPLICATION(application));
if (windows) {
gtk_window_present(GTK_WINDOW(windows->data));
return;
}

GtkWindow* window =
GTK_WINDOW(gtk_application_window_new(GTK_APPLICATION(application)));

Expand Down Expand Up @@ -78,7 +86,7 @@ static gboolean my_application_local_command_line(GApplication* application, gch
g_application_activate(application);
*exit_status = 0;

return TRUE;
return FALSE;
}

// Implements GObject::dispose.
Expand All @@ -99,6 +107,6 @@ static void my_application_init(MyApplication* self) {}
MyApplication* my_application_new() {
return MY_APPLICATION(g_object_new(my_application_get_type(),
"application-id", APPLICATION_ID,
"flags", G_APPLICATION_NON_UNIQUE,
"flags", G_APPLICATION_HANDLES_COMMAND_LINE | G_APPLICATION_HANDLES_OPEN,
nullptr));
}
4 changes: 2 additions & 2 deletions macos/Flutter/GeneratedPluginRegistrant.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,24 @@
import FlutterMacOS
import Foundation

import app_links
import audioplayers_darwin
import file_selector_macos
import icloud_storage
import path_provider_foundation
import screen_retriever
import share_plus
import uni_links_desktop
import url_launcher_macos
import window_manager

func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
AppLinksMacosPlugin.register(with: registry.registrar(forPlugin: "AppLinksMacosPlugin"))
AudioplayersDarwinPlugin.register(with: registry.registrar(forPlugin: "AudioplayersDarwinPlugin"))
FileSelectorPlugin.register(with: registry.registrar(forPlugin: "FileSelectorPlugin"))
IcloudStoragePlugin.register(with: registry.registrar(forPlugin: "IcloudStoragePlugin"))
PathProviderPlugin.register(with: registry.registrar(forPlugin: "PathProviderPlugin"))
ScreenRetrieverPlugin.register(with: registry.registrar(forPlugin: "ScreenRetrieverPlugin"))
SharePlusMacosPlugin.register(with: registry.registrar(forPlugin: "SharePlusMacosPlugin"))
UniLinksDesktopPlugin.register(with: registry.registrar(forPlugin: "UniLinksDesktopPlugin"))
UrlLauncherPlugin.register(with: registry.registrar(forPlugin: "UrlLauncherPlugin"))
WindowManagerPlugin.register(with: registry.registrar(forPlugin: "WindowManagerPlugin"))
}
62 changes: 23 additions & 39 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.0.2"
app_links:
dependency: "direct main"
description:
name: app_links
sha256: "1c2b9e9c56d80d17610bcbd111b37187875c5d0ded8654caa1bda14ea753d001"
url: "https://pub.dev"
source: hosted
version: "6.0.1"
archive:
dependency: transitive
description:
Expand Down Expand Up @@ -333,10 +341,10 @@ packages:
dependency: transitive
description:
name: ffi
sha256: "7bf0adc28a23d395f19f3f1eb21dd7cfd1dd9f8e1c50051c069122e6853bc878"
sha256: "493f37e7df1804778ff3a53bd691d8692ddf69702cf4c1c1096a2e41b4779e21"
url: "https://pub.dev"
source: hosted
version: "2.1.0"
version: "2.1.2"
file:
dependency: transitive
description:
Expand Down Expand Up @@ -510,6 +518,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.3.1"
gtk:
dependency: transitive
description:
name: gtk
sha256: e8ce9ca4b1df106e4d72dad201d345ea1a036cc12c360f1a7d5a758f78ffa42c
url: "https://pub.dev"
source: hosted
version: "2.1.0"
highlight:
dependency: transitive
description:
Expand Down Expand Up @@ -1115,38 +1131,6 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.3.2"
uni_links:
dependency: "direct main"
description:
name: uni_links
sha256: "051098acfc9e26a9fde03b487bef5d3d228ca8f67693480c6f33fd4fbb8e2b6e"
url: "https://pub.dev"
source: hosted
version: "0.5.1"
uni_links_desktop:
dependency: "direct main"
description:
name: uni_links_desktop
sha256: "692de81efc32ef72df56d428902afb5216d5f9e43d71c7b315d360acd7a1e115"
url: "https://pub.dev"
source: hosted
version: "0.1.7"
uni_links_platform_interface:
dependency: transitive
description:
name: uni_links_platform_interface
sha256: "929cf1a71b59e3b7c2d8a2605a9cf7e0b125b13bc858e55083d88c62722d4507"
url: "https://pub.dev"
source: hosted
version: "1.0.0"
uni_links_web:
dependency: transitive
description:
name: uni_links_web
sha256: "7539db908e25f67de2438e33cc1020b30ab94e66720b5677ba6763b25f6394df"
url: "https://pub.dev"
source: hosted
version: "0.1.0"
universal_html:
dependency: "direct main"
description:
Expand Down Expand Up @@ -1312,18 +1296,18 @@ packages:
dependency: transitive
description:
name: win32
sha256: b0f37db61ba2f2e9b7a78a1caece0052564d1bc70668156cf3a29d676fe4e574
sha256: "0eaf06e3446824099858367950a813472af675116bf63f008a4c2a75ae13e9cb"
url: "https://pub.dev"
source: hosted
version: "5.1.1"
version: "5.5.0"
win32_registry:
dependency: transitive
dependency: "direct main"
description:
name: win32_registry
sha256: "41fd8a189940d8696b1b810efb9abcf60827b6cbfab90b0c43e8439e3a39d85a"
sha256: "10589e0d7f4e053f2c61023a31c9ce01146656a70b7b7f0828c0b46d7da2a9bb"
url: "https://pub.dev"
source: hosted
version: "1.1.2"
version: "1.1.3"
window_manager:
dependency: "direct main"
description:
Expand Down
4 changes: 2 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ dependencies:
screenshot: ^2.3.0
after_layout: ^1.2.0
# flutter_math_fork: ^0.7.2
uni_links: ^0.5.1
uni_links_desktop: ^0.1.7
app_links: ^6.0.1
image_picker: ^1.0.7
audioplayers: ^6.0.0
cross_file: ^0.3.4
shortid: ^0.1.2
icons_plus: ^5.0.0
tiktoken: ^1.0.3
win32_registry: ^1.1.3

dev_dependencies:
flutter_test:
Expand Down
6 changes: 3 additions & 3 deletions windows/flutter/generated_plugin_registrant.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@

#include "generated_plugin_registrant.h"

#include <app_links/app_links_plugin_c_api.h>
#include <audioplayers_windows/audioplayers_windows_plugin.h>
#include <file_selector_windows/file_selector_windows.h>
#include <screen_retriever/screen_retriever_plugin.h>
#include <share_plus/share_plus_windows_plugin_c_api.h>
#include <uni_links_desktop/uni_links_desktop_plugin.h>
#include <url_launcher_windows/url_launcher_windows.h>
#include <window_manager/window_manager_plugin.h>

void RegisterPlugins(flutter::PluginRegistry* registry) {
AppLinksPluginCApiRegisterWithRegistrar(
registry->GetRegistrarForPlugin("AppLinksPluginCApi"));
AudioplayersWindowsPluginRegisterWithRegistrar(
registry->GetRegistrarForPlugin("AudioplayersWindowsPlugin"));
FileSelectorWindowsRegisterWithRegistrar(
Expand All @@ -23,8 +25,6 @@ void RegisterPlugins(flutter::PluginRegistry* registry) {
registry->GetRegistrarForPlugin("ScreenRetrieverPlugin"));
SharePlusWindowsPluginCApiRegisterWithRegistrar(
registry->GetRegistrarForPlugin("SharePlusWindowsPluginCApi"));
UniLinksDesktopPluginRegisterWithRegistrar(
registry->GetRegistrarForPlugin("UniLinksDesktopPlugin"));
UrlLauncherWindowsRegisterWithRegistrar(
registry->GetRegistrarForPlugin("UrlLauncherWindows"));
WindowManagerPluginRegisterWithRegistrar(
Expand Down
2 changes: 1 addition & 1 deletion windows/flutter/generated_plugins.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
#

list(APPEND FLUTTER_PLUGIN_LIST
app_links
audioplayers_windows
file_selector_windows
screen_retriever
share_plus
uni_links_desktop
url_launcher_windows
window_manager
)
Expand Down
Loading

0 comments on commit 97ad12e

Please sign in to comment.