Skip to content

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

fix(window): defer title bar control changes until restart

Prediditc4ac0e2965c4
diff --git a/lib/bean/appbar/sys_app_bar.dart b/lib/bean/appbar/sys_app_bar.dart
index 8d799a54..f673e03e 100644
--- a/lib/bean/appbar/sys_app_bar.dart
+++ b/lib/bean/appbar/sys_app_bar.dart
@@ -3,7 +3,7 @@ import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_modular/flutter_modular.dart';
import 'package:kazumi/bean/widget/embedded_native_control_area.dart';
-import 'package:kazumi/services/storage/storage.dart';
+import 'package:kazumi/services/platform/desktop_window_config.dart';
import 'package:window_manager/window_manager.dart';
import 'package:kazumi/utils/device.dart';
@@ -25,15 +25,12 @@ class SysAppBar extends StatelessWidget implements PreferredSizeWidget {
this.needTopOffset = true,
});
- bool get _showWindowButton =>
- GStorage.getSetting(SettingsKeys.showWindowButton);
-
@override
Widget build(BuildContext context) {
final desktop = isDesktop();
final appBarActions = <Widget>[...?actions];
if (desktop) {
- if (!_showWindowButton) {
+ if (!DesktopWindowConfig.showWindowButton) {
appBarActions.add(CloseButton(onPressed: () => windowManager.close()));
}
appBarActions.add(const SizedBox(width: 8));
@@ -89,7 +86,10 @@ class SysAppBar extends StatelessWidget implements PreferredSizeWidget {
@override
Size get preferredSize {
// Reserve space for native macOS window controls.
- final topOffset = Platform.isMacOS && needTopOffset && _showWindowButton
+ final topOffset =
+ Platform.isMacOS &&
+ needTopOffset &&
+ DesktopWindowConfig.showWindowButton
? 22.0
: 0.0;
return Size.fromHeight((toolbarHeight ?? kToolbarHeight) + topOffset);
diff --git a/lib/bean/widget/embedded_native_control_area.dart b/lib/bean/widget/embedded_native_control_area.dart
index 83e2ef4d..13ca4798 100644
--- a/lib/bean/widget/embedded_native_control_area.dart
+++ b/lib/bean/widget/embedded_native_control_area.dart
@@ -1,11 +1,9 @@
import 'dart:io';
import 'package:flutter/material.dart';
-import 'package:kazumi/services/storage/storage.dart';
+import 'package:kazumi/services/platform/desktop_window_config.dart';
-class EmbeddedNativeControlArea extends StatefulWidget {
- /// The widget won't draw anything, just a placeholder for native window control.
- /// It only works on macOS at the moment.
- /// windows and linux have no way to embed native window control into flutter view.
+/// Reserves space above content for native macOS window controls.
+class EmbeddedNativeControlArea extends StatelessWidget {
const EmbeddedNativeControlArea({
super.key,
required this.child,
@@ -15,32 +13,15 @@ class EmbeddedNativeControlArea extends StatefulWidget {
final Widget child;
final bool requireOffset;
- @override
- State<StatefulWidget> createState() => _EmbeddedNativeControlAreaState();
-}
-
-class _EmbeddedNativeControlAreaState extends State<EmbeddedNativeControlArea> {
- bool showWindowButton = GStorage.getSetting(SettingsKeys.showWindowButton);
-
- EdgeInsets get getInsets {
- if (!showWindowButton) {
- return EdgeInsets.zero;
- }
- if (!widget.requireOffset) {
- return EdgeInsets.zero;
- }
- if (Platform.isMacOS) {
- return const EdgeInsets.only(top: 22);
- } else {
- return EdgeInsets.zero;
- }
- }
-
@override
Widget build(BuildContext context) {
+ final needsOffset =
+ Platform.isMacOS &&
+ requireOffset &&
+ DesktopWindowConfig.showWindowButton;
return Padding(
- padding: getInsets,
- child: widget.child,
+ padding: needsOffset ? const EdgeInsets.only(top: 22) : EdgeInsets.zero,
+ child: child,
);
}
}
diff --git a/lib/main.dart b/lib/main.dart
index d4857f06..0e2ebdf0 100644
--- a/lib/main.dart
+++ b/lib/main.dart
@@ -17,6 +17,7 @@ import 'package:window_manager/window_manager.dart';
import 'package:kazumi/pages/error/storage_error_page.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:kazumi/utils/device.dart';
+import 'package:kazumi/services/platform/desktop_window_config.dart';
import 'package:kazumi/services/platform/webview_feature_service.dart';
import 'package:kazumi/bean/dialog/dialog_helper.dart';
import 'package:kazumi/navigation.dart';
@@ -43,7 +44,6 @@ void main() async {
await Hive.initFlutter(hivePath);
await GStorage.init();
} catch (e) {
- // Log the error for debugging (if logger is available)
debugPrint('Storage initialization failed: $e');
if (isDesktop()) {
@@ -68,16 +68,15 @@ void main() async {
}));
return;
}
- bool showWindowButton =
- await GStorage.getSetting(SettingsKeys.showWindowButton);
+ final showWindowButton = DesktopWindowConfig.showWindowButton;
if (isDesktop()) {
await windowManager.ensureInitialized();
final lowResolution = await isLowResolution();
- WindowOptions windowOptions = WindowOptions(
+ final windowOptions = WindowOptions(
size: lowResolution ? const Size(840, 600) : const Size(1280, 860),
center: true,
skipTaskbar: false,
- // macOS always hide title bar regardless of showWindowButton setting
+ // macOS embeds native buttons in the Flutter view.
titleBarStyle: (Platform.isMacOS || !showWindowButton)
? TitleBarStyle.hidden
: TitleBarStyle.normal,
diff --git a/lib/pages/info/info_page.dart b/lib/pages/info/info_page.dart
index 6b7441bf..06ac268f 100644
--- a/lib/pages/info/info_page.dart
+++ b/lib/pages/info/info_page.dart
@@ -20,6 +20,7 @@ import 'package:kazumi/pages/info/info_tabview.dart';
import 'package:kazumi/pages/info/rating_review_dialog.dart';
import 'package:kazumi/pages/info/source_sheet.dart';
import 'package:kazumi/services/logging/logger.dart';
+import 'package:kazumi/services/platform/desktop_window_config.dart';
import 'package:kazumi/services/storage/storage.dart';
import 'package:kazumi/utils/device.dart';
@@ -307,8 +308,8 @@ class _InfoPageState extends State<InfoPage>
@override
Widget build(BuildContext context) {
- final bool showWindowButton =
- GStorage.getSetting(SettingsKeys.showWindowButton);
+ final showWindowButton = DesktopWindowConfig.showWindowButton;
+ final topOffset = Platform.isMacOS && showWindowButton ? 22.0 : 0.0;
return Scaffold(
body: NestedScrollView(
headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
@@ -352,26 +353,19 @@ class _InfoPageState extends State<InfoPage>
bangumiItem: infoController.bangumiItem,
),
),
- if (!showWindowButton && isDesktop())
+ if (isDesktop() && !showWindowButton)
CloseButton(onPressed: () => windowManager.close()),
SizedBox(width: 8),
],
- toolbarHeight: (Platform.isMacOS && showWindowButton)
- ? kToolbarHeight + 22
- : kToolbarHeight,
+ toolbarHeight: kToolbarHeight + topOffset,
stretch: true,
centerTitle: false,
- expandedHeight: (Platform.isMacOS && showWindowButton)
- ? 308 + kTextTabBarHeight + kToolbarHeight + 22
- : 308 + kTextTabBarHeight + kToolbarHeight,
- collapsedHeight: (Platform.isMacOS && showWindowButton)
- ? kTextTabBarHeight +
- kToolbarHeight +
- MediaQuery.paddingOf(context).top +
- 22
- : kTextTabBarHeight +
- kToolbarHeight +
- MediaQuery.paddingOf(context).top,
+ expandedHeight:
+ 308 + kTextTabBarHeight + kToolbarHeight + topOffset,
+ collapsedHeight: kTextTabBarHeight +
+ kToolbarHeight +
+ MediaQuery.paddingOf(context).top +
+ topOffset,
flexibleSpace: FlexibleSpaceBar(
collapseMode: CollapseMode.pin,
background: Observer(builder: (context) {
diff --git a/lib/pages/popular/popular_page.dart b/lib/pages/popular/popular_page.dart
index 155402b3..29ce7cdc 100644
--- a/lib/pages/popular/popular_page.dart
+++ b/lib/pages/popular/popular_page.dart
@@ -10,7 +10,7 @@ import 'package:kazumi/utils/constants.dart';
import 'package:flutter_mobx/flutter_mobx.dart';
import 'package:window_manager/window_manager.dart';
import 'package:kazumi/services/logging/logger.dart';
-import 'package:kazumi/services/storage/storage.dart';
+import 'package:kazumi/services/platform/desktop_window_config.dart';
import 'package:kazumi/bean/appbar/drag_to_move_bar.dart' as dtb;
import 'package:kazumi/utils/device.dart';
@@ -67,10 +67,6 @@ class _PopularPageState extends State<PopularPage> {
}
}
- bool showWindowButton() {
- return GStorage.getSetting(SettingsKeys.showWindowButton);
- }
-
@override
Widget build(BuildContext context) {
return Scaffold(
@@ -231,33 +227,25 @@ class _PopularPageState extends State<PopularPage> {
}
List<Widget> buildActions() {
- final actions = <Widget>[
+ return <Widget>[
if (MediaQuery.of(context).orientation == Orientation.portrait)
IconButton(
tooltip: '搜索',
onPressed: () => context.pushNamed('/search/'),
icon: const Icon(Icons.search),
),
- ];
- actions.add(
IconButton(
tooltip: '历史记录',
onPressed: () => context.pushNamed('/settings/history/'),
icon: const Icon(Icons.history),
),
- );
- if (isDesktop()) {
- if (!showWindowButton()) {
- actions.add(
- IconButton(
- tooltip: '退出',
- onPressed: () => windowManager.close(),
- icon: const Icon(Icons.close),
- ),
- );
- }
- }
- return actions;
+ if (isDesktop() && !DesktopWindowConfig.showWindowButton)
+ IconButton(
+ tooltip: '退出',
+ onPressed: () => windowManager.close(),
+ icon: const Icon(Icons.close),
+ ),
+ ];
}
Future<void> showTagMenu() async {
diff --git a/lib/services/platform/desktop_window_config.dart b/lib/services/platform/desktop_window_config.dart
new file mode 100644
index 00000000..abd728a4
--- /dev/null
+++ b/lib/services/platform/desktop_window_config.dart
@@ -0,0 +1,8 @@
+import 'package:kazumi/services/storage/storage.dart';
+
+abstract final class DesktopWindowConfig {
+ /// Captured during window setup; preference changes apply on the next launch.
+ static final bool showWindowButton = GStorage.getSetting(
+ SettingsKeys.showWindowButton,
+ );
+}