Skip to content

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

add native fullscreen implements for windows (#119)

Predidit6027a3f3f3da
diff --git a/.vscode/settings.json b/.vscode/settings.json
index 9ddf6b28..b88620d1 100644
--- a/.vscode/settings.json
+++ b/.vscode/settings.json
@@ -1,3 +1,6 @@
{
- "cmake.ignoreCMakeListsMissing": true
+ "cmake.ignoreCMakeListsMissing": true,
+ "files.associations": {
+ "xiosbase": "cpp"
+ }
}
\ No newline at end of file
diff --git a/lib/pages/player/player_item.dart b/lib/pages/player/player_item.dart
index e9f73dfa..7de19ac5 100644
--- a/lib/pages/player/player_item.dart
+++ b/lib/pages/player/player_item.dart
@@ -271,7 +271,7 @@ class _PlayerItemState extends State<PlayerItem>
void onBackPressed(BuildContext context) async {
if (videoPageController.androidFullscreen) {
try {
- await videoPageController.exitFullScreen();
+ await Utils.exitFullScreen();
videoPageController.androidFullscreen = false;
danmakuController.clear();
return;
@@ -300,9 +300,9 @@ class _PlayerItemState extends State<PlayerItem>
try {
danmakuController.onClear();
} catch (_) {}
- videoPageController.exitFullScreen();
+ Utils.exitFullScreen();
} else {
- videoPageController.enterFullScreen();
+ Utils.enterFullScreen();
}
videoPageController.androidFullscreen =
!videoPageController.androidFullscreen;
@@ -698,7 +698,7 @@ class _PlayerItemState extends State<PlayerItem>
try {
danmakuController.onClear();
} catch (_) {}
- videoPageController.exitFullScreen();
+ Utils.exitFullScreen();
videoPageController.androidFullscreen =
!videoPageController.androidFullscreen;
} else {
diff --git a/lib/pages/video/video_controller.dart b/lib/pages/video/video_controller.dart
index 2e50fd5f..4d978664 100644
--- a/lib/pages/video/video_controller.dart
+++ b/lib/pages/video/video_controller.dart
@@ -70,89 +70,5 @@ abstract class _VideoPageController with Store {
final webviewItemController = Modular.get<WebviewItemController>();
await webviewItemController.loadUrl(urlItem, offset: offset);
}
-
- Future<void> enterFullScreen({bool lockOrientation = true}) async {
- if (Platform.isWindows || Platform.isLinux || Platform.isMacOS) {
- await windowManager.setFullScreen(true);
- return;
- }
- await SystemChrome.setEnabledSystemUIMode(
- SystemUiMode.immersiveSticky,
- );
- if (!lockOrientation) {
- return;
- }
- if (Platform.isAndroid) {
- bool isInMultiWindowMode = await Utils.isInMultiWindowMode();
- if (isInMultiWindowMode) {
- return;
- }
- }
- await landScape();
- }
-
- //退出全屏显示
- Future<void> exitFullScreen({bool lockOrientation = true}) async {
- if (Platform.isWindows || Platform.isLinux || Platform.isMacOS) {
- await windowManager.setFullScreen(false);
- }
- late SystemUiMode mode = SystemUiMode.edgeToEdge;
- try {
- if (Platform.isAndroid || Platform.isIOS) {
- if (Platform.isAndroid &&
- (await DeviceInfoPlugin().androidInfo).version.sdkInt < 29) {
- mode = SystemUiMode.manual;
- }
- await SystemChrome.setEnabledSystemUIMode(
- mode,
- overlays: SystemUiOverlay.values,
- );
- if (Utils.isCompact() && lockOrientation) {
- if (Platform.isAndroid) {
- bool isInMultiWindowMode = await Utils.isInMultiWindowMode();
- if (isInMultiWindowMode) {
- return;
- }
- }
- verticalScreen();
- }
- }
- } catch (exception, stacktrace) {
- KazumiLogger()
- .log(Level.error, exception.toString(), stackTrace: stacktrace);
- }
- }
-
- //横屏
- Future<void> landScape() async {
- dynamic document;
- try {
- if (kIsWeb) {
- await document.documentElement?.requestFullscreen();
- } else if (Platform.isAndroid || Platform.isIOS) {
- await SystemChrome.setPreferredOrientations(
- [
- DeviceOrientation.landscapeLeft,
- DeviceOrientation.landscapeRight,
- ],
- );
- }
- } catch (exception, stacktrace) {
- KazumiLogger()
- .log(Level.error, exception.toString(), stackTrace: stacktrace);
- }
- }
-
- //竖屏
- Future<void> verticalScreen() async {
- await SystemChrome.setPreferredOrientations([
- DeviceOrientation.portraitUp,
- ]);
- }
-
- // 解除屏幕旋转限制
- Future<void> unlockScreenRotation() async {
- await SystemChrome.setPreferredOrientations([]);
- }
}
diff --git a/lib/pages/video/video_page.dart b/lib/pages/video/video_page.dart
index 3aec4a3b..5b6aafd0 100644
--- a/lib/pages/video/video_page.dart
+++ b/lib/pages/video/video_page.dart
@@ -16,6 +16,7 @@ import 'package:kazumi/utils/storage.dart';
import 'package:kazumi/utils/utils.dart';
import 'package:flutter/services.dart';
import 'package:wakelock_plus/wakelock_plus.dart';
+import 'package:kazumi/utils/utils.dart';
class VideoPage extends StatefulWidget {
const VideoPage({super.key});
@@ -83,7 +84,7 @@ class _VideoPageState extends State<VideoPage>
playerController.mediaPlayer.dispose();
} catch (_) {}
WakelockPlus.disable();
- videoPageController.unlockScreenRotation();
+ Utils.unlockScreenRotation();
super.dispose();
}
@@ -94,11 +95,11 @@ class _VideoPageState extends State<VideoPage>
if (!Utils.isTablet() && !Utils.isDesktop()) {
if (orientation == Orientation.landscape &&
!videoPageController.androidFullscreen) {
- videoPageController.enterFullScreen(lockOrientation: false);
+ Utils.enterFullScreen(lockOrientation: false);
videoPageController.androidFullscreen = true;
} else if (orientation == Orientation.portrait &&
videoPageController.androidFullscreen) {
- videoPageController.exitFullScreen(lockOrientation: false);
+ Utils.exitFullScreen(lockOrientation: false);
videoPageController.androidFullscreen = false;
}
}
@@ -208,7 +209,7 @@ class _VideoPageState extends State<VideoPage>
icon: const Icon(Icons.arrow_back, color: Colors.white),
onPressed: () {
if (videoPageController.androidFullscreen == true) {
- videoPageController.exitFullScreen();
+ Utils.exitFullScreen();
videoPageController.androidFullscreen = false;
return;
}
diff --git a/lib/pages/webview/webview_controller_impel/webview_controller_impel.dart b/lib/pages/webview/webview_controller_impel/webview_controller_impel.dart
index d43266ce..c23be2a7 100644
--- a/lib/pages/webview/webview_controller_impel/webview_controller_impel.dart
+++ b/lib/pages/webview/webview_controller_impel/webview_controller_impel.dart
@@ -72,11 +72,11 @@ class WebviewItemControllerImpel extends WebviewItemController {
debugPrint('FullscreenJS桥收到的消息为 ${message.message}');
if (message.message == 'enteredFullscreen') {
videoPageController.androidFullscreen = true;
- videoPageController.enterFullScreen();
+ Utils.enterFullScreen();
}
if (message.message == 'exitedFullscreen') {
videoPageController.androidFullscreen = false;
- videoPageController.exitFullScreen();
+ Utils.exitFullScreen();
}
});
await webviewController.loadRequest(Uri.parse(url));
diff --git a/lib/utils/utils.dart b/lib/utils/utils.dart
index 6ca9064d..bea8d840 100644
--- a/lib/utils/utils.dart
+++ b/lib/utils/utils.dart
@@ -12,6 +12,11 @@ import 'package:path_provider/path_provider.dart';
import 'package:kazumi/request/api.dart';
import 'package:screen_pixel/screen_pixel.dart';
import 'package:kazumi/utils/constans.dart';
+import 'package:logger/logger.dart';
+import 'package:device_info_plus/device_info_plus.dart';
+import 'package:kazumi/utils/logger.dart';
+import 'package:flutter/foundation.dart';
+import 'package:window_manager/window_manager.dart';
class Utils {
static final Random random = Random();
@@ -458,7 +463,8 @@ class Utils {
if (Platform.isAndroid) {
const platform = MethodChannel('com.predidit.kazumi/intent');
try {
- final bool result = await platform.invokeMethod('checkIfInMultiWindowMode');
+ final bool result =
+ await platform.invokeMethod('checkIfInMultiWindowMode');
return result;
} on PlatformException catch (e) {
print("Failed to check multi window mode: '${e.message}'.");
@@ -467,4 +473,118 @@ class Utils {
}
return false;
}
+
+ static enterWindowsFullscreen() async {
+ if (Platform.isWindows) {
+ const platform = MethodChannel('com.predidit.kazumi/intent');
+ try {
+ await platform.invokeMethod('enterFullscreen');
+ } on PlatformException catch (e) {
+ print("Failed to enter native window mode: '${e.message}'.");
+ }
+ }
+ }
+
+ static exitWindowsFullscreen() async {
+ if (Platform.isWindows) {
+ const platform = MethodChannel('com.predidit.kazumi/intent');
+ try {
+ await platform.invokeMethod('exitFullscreen');
+ } on PlatformException catch (e) {
+ print("Failed to exit native window mode: '${e.message}'.");
+ }
+ }
+ }
+
+ // 进入全屏显示
+ static enterFullScreen({bool lockOrientation = true}) async {
+ if (Platform.isWindows) {
+ await enterWindowsFullscreen();
+ return;
+ }
+ if (Platform.isLinux || Platform.isMacOS) {
+ await windowManager.setFullScreen(true);
+ return;
+ }
+ await SystemChrome.setEnabledSystemUIMode(
+ SystemUiMode.immersiveSticky,
+ );
+ if (!lockOrientation) {
+ return;
+ }
+ if (Platform.isAndroid) {
+ bool isInMultiWindowMode = await Utils.isInMultiWindowMode();
+ if (isInMultiWindowMode) {
+ return;
+ }
+ }
+ await landScape();
+ }
+
+ //退出全屏显示
+ static Future<void> exitFullScreen({bool lockOrientation = true}) async {
+ if (Platform.isWindows) {
+ await exitWindowsFullscreen();
+ }
+ if (Platform.isLinux || Platform.isMacOS) {
+ await windowManager.setFullScreen(false);
+ }
+ late SystemUiMode mode = SystemUiMode.edgeToEdge;
+ try {
+ if (Platform.isAndroid || Platform.isIOS) {
+ if (Platform.isAndroid &&
+ (await DeviceInfoPlugin().androidInfo).version.sdkInt < 29) {
+ mode = SystemUiMode.manual;
+ }
+ await SystemChrome.setEnabledSystemUIMode(
+ mode,
+ overlays: SystemUiOverlay.values,
+ );
+ if (Utils.isCompact() && lockOrientation) {
+ if (Platform.isAndroid) {
+ bool isInMultiWindowMode = await Utils.isInMultiWindowMode();
+ if (isInMultiWindowMode) {
+ return;
+ }
+ }
+ verticalScreen();
+ }
+ }
+ } catch (exception, stacktrace) {
+ KazumiLogger()
+ .log(Level.error, exception.toString(), stackTrace: stacktrace);
+ }
+ }
+
+ //横屏
+ static Future<void> landScape() async {
+ dynamic document;
+ try {
+ if (kIsWeb) {
+ await document.documentElement?.requestFullscreen();
+ } else if (Platform.isAndroid || Platform.isIOS) {
+ await SystemChrome.setPreferredOrientations(
+ [
+ DeviceOrientation.landscapeLeft,
+ DeviceOrientation.landscapeRight,
+ ],
+ );
+ }
+ } catch (exception, stacktrace) {
+ KazumiLogger()
+ .log(Level.error, exception.toString(), stackTrace: stacktrace);
+ }
+ }
+
+ //竖屏
+ static Future<void> verticalScreen() async {
+ await SystemChrome.setPreferredOrientations([
+ DeviceOrientation.portraitUp,
+ ]);
+ }
+
+ // 解除屏幕旋转限制
+ static Future<void> unlockScreenRotation() async {
+ await SystemChrome.setPreferredOrientations([]);
+ }
}
diff --git a/windows/runner/CMakeLists.txt b/windows/runner/CMakeLists.txt
index 394917c0..a1e7c661 100644
--- a/windows/runner/CMakeLists.txt
+++ b/windows/runner/CMakeLists.txt
@@ -10,6 +10,7 @@ add_executable(${BINARY_NAME} WIN32
"flutter_window.cpp"
"main.cpp"
"utils.cpp"
+ "fullscreen_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 955ee303..10c825c6 100644
--- a/windows/runner/flutter_window.cpp
+++ b/windows/runner/flutter_window.cpp
@@ -1,6 +1,11 @@
#include "flutter_window.h"
+#include "fullscreen_utils.h"
#include <optional>
+#include <flutter/method_channel.h>
+#include <flutter/standard_method_codec.h>
+#include <flutter/plugin_registrar_windows.h>
+#include <windows.h>
#include "flutter/generated_plugin_registrant.h"
@@ -36,6 +41,9 @@ bool FlutterWindow::OnCreate() {
// window is shown. It is a no-op if the first frame hasn't completed yet.
flutter_controller_->ForceRedraw();
+ // Register Intent MethodChannel
+ RegisterIntentChannel();
+
return true;
}
@@ -69,3 +77,23 @@ FlutterWindow::MessageHandler(HWND hwnd, UINT const message,
return Win32Window::MessageHandler(hwnd, message, wparam, lparam);
}
+
+// Intent MethodChannel setup
+void FlutterWindow::RegisterIntentChannel() {
+ auto window_channel =
+ std::make_unique<flutter::MethodChannel<flutter::EncodableValue>>(
+ flutter_controller_->engine()->messenger(), "com.predidit.kazumi/intent",
+ &flutter::StandardMethodCodec::GetInstance());
+
+ window_channel->SetMethodCallHandler([this](const auto& call, auto result) {
+ if (call.method_name().compare("enterFullscreen") == 0) {
+ FullscreenUtils::EnterNativeFullscreen(GetHandle());
+ result->Success();
+ } else if (call.method_name().compare("exitFullscreen") == 0) {
+ FullscreenUtils::ExitNativeFullscreen(GetHandle());
+ result->Success();
+ } else {
+ result->NotImplemented();
+ }
+ });
+}
diff --git a/windows/runner/flutter_window.h b/windows/runner/flutter_window.h
index 6da0652f..071da165 100644
--- a/windows/runner/flutter_window.h
+++ b/windows/runner/flutter_window.h
@@ -28,6 +28,9 @@ class FlutterWindow : public Win32Window {
// The Flutter instance hosted by this window.
std::unique_ptr<flutter::FlutterViewController> flutter_controller_;
+
+ // Register Intent MethodChannel
+ void RegisterIntentChannel();
};
#endif // RUNNER_FLUTTER_WINDOW_H_
diff --git a/windows/runner/fullscreen_utils.cpp b/windows/runner/fullscreen_utils.cpp
new file mode 100644
index 00000000..157783d9
--- /dev/null
+++ b/windows/runner/fullscreen_utils.cpp
@@ -0,0 +1,81 @@
+// This file is a part of media_kit
+// (https://github.com/media-kit/media-kit).
+//
+// Copyright © 2021 & onwards, Hitesh Kumar Saini <[email protected]>.
+// All rights reserved.
+// Use of this source code is governed by MIT license that can be found in the
+// LICENSE file.
+
+#include "fullscreen_utils.h"
+
+void FullscreenUtils::EnterNativeFullscreen(HWND window) {
+ if (fullscreen_) {
+ return;
+ }
+ fullscreen_ = true;
+
+ // The primary idea here is to revolve around |WS_OVERLAPPEDWINDOW| &
+ // detect/set fullscreen based on it. In the window procedure, this is
+ // separately handled. If there is no |WS_OVERLAPPEDWINDOW| style on the
+ // window i.e. in fullscreen, then no area is left for |WM_NCHITTEST|,
+ // accordingly client area is also expanded to fill whole monitor using
+ // |WM_NCCALCSIZE|.
+
+ auto style = ::GetWindowLongPtr(window, GWL_STYLE);
+ if (style & WS_OVERLAPPEDWINDOW) {
+ auto monitor = MONITORINFO{};
+ auto placement = WINDOWPLACEMENT{};
+ monitor.cbSize = sizeof(MONITORINFO);
+ placement.length = sizeof(WINDOWPLACEMENT);
+ ::GetWindowPlacement(window, &placement);
+ rect_before_fullscreen_ = RECT{
+ placement.rcNormalPosition.left,
+ placement.rcNormalPosition.top,
+ placement.rcNormalPosition.right,
+ placement.rcNormalPosition.bottom,
+ };
+ ::GetMonitorInfo(::MonitorFromWindow(window, MONITOR_DEFAULTTONEAREST),
+ &monitor);
+ ::SetWindowLongPtr(window, GWL_STYLE, style & ~WS_OVERLAPPEDWINDOW);
+ ::SetWindowPos(window, HWND_TOP, monitor.rcMonitor.left,
+ monitor.rcMonitor.top, monitor.rcMonitor.right - monitor.rcMonitor.left,
+ monitor.rcMonitor.bottom - monitor.rcMonitor.top,
+ SWP_NOOWNERZORDER | SWP_FRAMECHANGED);
+ }
+}
+
+void FullscreenUtils::ExitNativeFullscreen(HWND window) {
+ if (!fullscreen_) {
+ return;
+ }
+ fullscreen_ = false;
+
+ auto style = ::GetWindowLongPtr(window, GWL_STYLE);
+ if (!(style & WS_OVERLAPPEDWINDOW)) {
+ ::SetWindowLongPtr(window, GWL_STYLE, style | WS_OVERLAPPEDWINDOW);
+ if (::IsZoomed(window)) {
+ // Refresh the parent window.
+ ::SetWindowPos(window, nullptr, 0, 0, 0, 0,
+ SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER |
+ SWP_FRAMECHANGED);
+ auto rect = RECT{};
+ ::GetClientRect(window, &rect);
+ auto flutter_view =
+ ::FindWindowEx(window, nullptr, kFlutterViewWindowClassName, nullptr);
+ ::SetWindowPos(flutter_view, nullptr, rect.left, rect.top,
+ rect.right - rect.left, rect.bottom - rect.top,
+ SWP_NOACTIVATE | SWP_NOZORDER);
+ } else {
+ ::SetWindowPos(
+ window, nullptr, rect_before_fullscreen_.left,
+ rect_before_fullscreen_.top,
+ rect_before_fullscreen_.right - rect_before_fullscreen_.left,
+ rect_before_fullscreen_.bottom - rect_before_fullscreen_.top,
+ SWP_NOACTIVATE | SWP_NOZORDER);
+ }
+ }
+}
+
+bool FullscreenUtils::fullscreen_ = false;
+
+RECT FullscreenUtils::rect_before_fullscreen_ = RECT{};
diff --git a/windows/runner/fullscreen_utils.h b/windows/runner/fullscreen_utils.h
new file mode 100644
index 00000000..bb6db70c
--- /dev/null
+++ b/windows/runner/fullscreen_utils.h
@@ -0,0 +1,29 @@
+// This file is a part of media_kit
+// (https://github.com/media-kit/media-kit).
+//
+// Copyright © 2021 & onwards, Hitesh Kumar Saini <[email protected]>.
+// All rights reserved.
+// Use of this source code is governed by MIT license that can be found in the
+// LICENSE file.
+
+#ifndef UTILS_H_
+#define UTILS_H_
+
+#include <cstdint>
+
+#include <Windows.h>
+
+class FullscreenUtils {
+ public:
+ static void EnterNativeFullscreen(HWND window);
+
+ static void ExitNativeFullscreen(HWND window);
+
+ private:
+ static constexpr auto kFlutterViewWindowClassName = L"FLUTTERVIEW";
+
+ static bool fullscreen_;
+ static RECT rect_before_fullscreen_;
+};
+
+#endif // UTILS_H_