Skip to content

Commit 050010b

Browse files
committed
fix: resolve search hang and Linux WebView issues
1 parent 4c58120 commit 050010b

File tree

3 files changed

+58
-13
lines changed

3 files changed

+58
-13
lines changed

lib/repositories/dictionary.dart

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ class Mdict {
111111
bool hasJs = false;
112112

113113
late HttpServer? server;
114-
late int port;
114+
int port = 0;
115115

116116
static const maxBatchSize = 50000;
117117
static const maxLoadingCount = 5000;
@@ -127,6 +127,7 @@ class Mdict {
127127
title = reader.header["Title"] ?? basename(path);
128128

129129
await dictionaryListDao.add(path, title);
130+
isLoading = false;
130131
}
131132

132133
Future<void> close() async {
@@ -183,6 +184,12 @@ class Mdict {
183184
if (type == "mdx") {
184185
isLoading = false;
185186
}
187+
}, onError: (e) {
188+
if (type == "mdx") {
189+
isLoading = false;
190+
}
191+
talker.error("Failed to import cache for $id ($type): $e");
192+
cacheFile.delete().catchError((_) => cacheFile);
186193
});
187194

188195
return true;
@@ -230,17 +237,17 @@ class Mdict {
230237

231238
Future<void> initDictReaders() async {
232239
if (!await hitCache(id, "mdx", reader)) {
233-
reader.initDict(readHeader: false);
234240
reader.setOnRecordBlockInfoRead(saveCache(id, "mdx", reader));
241+
reader.initDict(readHeader: false);
235242
}
236243

237244
final mddFile = File("$path.mdd");
238245
if (await mddFile.exists()) {
239246
final reader = DictReader(mddFile.path);
240247

241248
if (!await hitCache(id, "mdd", reader)) {
242-
reader.initDict();
243249
reader.setOnRecordBlockInfoRead(saveCache(id, "mdd", reader));
250+
reader.initDict();
244251
} else {
245252
reader.initDict(readKeys: false, readRecordBlockInfo: false);
246253
}
@@ -254,8 +261,8 @@ class Mdict {
254261
final reader = DictReader(mddFile.path);
255262

256263
if (!await hitCache(id, "$i.mdd", reader)) {
257-
reader.initDict();
258264
reader.setOnRecordBlockInfoRead(saveCache(id, "$i.mdd", reader));
265+
reader.initDict();
259266
} else {
260267
reader.initDict(readKeys: false, readRecordBlockInfo: false);
261268
}
@@ -528,7 +535,7 @@ class Mdict {
528535

529536
Future<void> _startServer() async {
530537
try {
531-
server = await HttpServer.bind("localhost", 0);
538+
server = await HttpServer.bind(InternetAddress.loopbackIPv4, 0);
532539
port = server!.port;
533540
talker.info("HTTP server started on port $port");
534541

lib/ui/core/word_display/webview_widgets.dart

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import "dart:convert";
2+
import "dart:io";
23

34
import "package:ciyue/core/app_globals.dart";
45
import "package:ciyue/repositories/dictionary.dart";
@@ -236,21 +237,58 @@ class WebviewWindows extends StatelessWidget {
236237
@override
237238
Widget build(BuildContext context) {
238239
final port = dictManager.dicts[dictId]!.port;
239-
final url = "http://localhost:$port/";
240+
241+
if (port == 0) {
242+
return const Center(child: CircularProgressIndicator());
243+
}
244+
245+
final url = "http://127.0.0.1:$port/";
240246

241247
final Uint8List postData = Uint8List.fromList(
242248
utf8.encode(json.encode({"content": content})),
243249
);
250+
251+
final isLightTheme = settings.themeMode == ThemeMode.light ||
252+
settings.themeMode == ThemeMode.system &&
253+
MediaQuery.of(context).platformBrightness == Brightness.light;
254+
255+
final webviewSettings = InAppWebViewSettings(
256+
useWideViewPort: false,
257+
algorithmicDarkeningAllowed: !isLightTheme,
258+
resourceCustomSchemes: ["entry", "sound"],
259+
transparentBackground: true,
260+
);
261+
262+
if (Platform.isLinux) {
263+
return InAppWebView(
264+
initialSettings: webviewSettings,
265+
initialUrlRequest: URLRequest(
266+
url: WebUri(url),
267+
method: "POST",
268+
body: postData,
269+
),
270+
initialData: InAppWebViewInitialData(
271+
data: content,
272+
baseUrl: WebUri(url),
273+
),
274+
onLoadResourceWithCustomScheme:
275+
onLoadResourceWithCustomSchemeWarpper(dictId),
276+
shouldOverrideUrlLoading:
277+
shouldOverrideUrlLoadingWarpper(dictId, context),
278+
);
279+
}
280+
244281
return FutureBuilder(
245282
future: WebViewEnvironment.create(
246283
settings: WebViewEnvironmentSettings(
247284
userDataFolder: windowsWebview2Directory,
248285
),
249286
),
250287
builder: (context, snapshot) {
251-
if (snapshot.hasData) {
288+
if (snapshot.hasData || snapshot.hasError) {
252289
return InAppWebView(
253290
webViewEnvironment: snapshot.data,
291+
initialSettings: webviewSettings,
254292
initialUrlRequest: URLRequest(
255293
url: WebUri(url),
256294
method: "POST",

pubspec.lock

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1512,26 +1512,26 @@ packages:
15121512
dependency: "direct dev"
15131513
description:
15141514
name: test
1515-
sha256: "77cc98ea27006c84e71a7356cf3daf9ddbde2d91d84f77dbfe64cf0e4d9611ae"
1515+
sha256: "54c516bbb7cee2754d327ad4fca637f78abfc3cbcc5ace83b3eda117e42cd71a"
15161516
url: "https://pub.dev"
15171517
source: hosted
1518-
version: "1.28.0"
1518+
version: "1.29.0"
15191519
test_api:
15201520
dependency: transitive
15211521
description:
15221522
name: test_api
1523-
sha256: "19a78f63e83d3a61f00826d09bc2f60e191bf3504183c001262be6ac75589fb8"
1523+
sha256: "93167629bfc610f71560ab9312acdda4959de4df6fac7492c89ff0d3886f6636"
15241524
url: "https://pub.dev"
15251525
source: hosted
1526-
version: "0.7.8"
1526+
version: "0.7.9"
15271527
test_core:
15281528
dependency: transitive
15291529
description:
15301530
name: test_core
1531-
sha256: f1072617a6657e5fc09662e721307f7fb009b4ed89b19f47175d11d5254a62d4
1531+
sha256: "394f07d21f0f2255ec9e3989f21e54d3c7dc0e6e9dbce160e5a9c1a6be0e2943"
15321532
url: "https://pub.dev"
15331533
source: hosted
1534-
version: "0.6.14"
1534+
version: "0.6.15"
15351535
timezone:
15361536
dependency: transitive
15371537
description:

0 commit comments

Comments
 (0)