diff --git a/apps/readest-app/src-tauri/plugins/tauri-plugin-native-bridge/android/src/main/java/NativeBridgePlugin.kt b/apps/readest-app/src-tauri/plugins/tauri-plugin-native-bridge/android/src/main/java/NativeBridgePlugin.kt index 83c8681a..c4a2dcb0 100644 --- a/apps/readest-app/src-tauri/plugins/tauri-plugin-native-bridge/android/src/main/java/NativeBridgePlugin.kt +++ b/apps/readest-app/src-tauri/plugins/tauri-plugin-native-bridge/android/src/main/java/NativeBridgePlugin.kt @@ -495,13 +495,19 @@ class NativeBridgePlugin(private val activity: Activity): Plugin(activity) { val args = invoke.parseArgs(SetScreenBrightnessRequestArgs::class.java) val ret = JSObject() try { - val brightness = (args.brightness ?: 0.5).toFloat() - if (brightness < 0.0 || brightness > 1.0) { - invoke.reject("Brightness must be between 0.0 and 1.0") - return - } + val brightness = args.brightness?.toFloat() val layoutParams = activity.window.attributes - layoutParams.screenBrightness = brightness + + if (brightness == null || brightness < 0.0) { + layoutParams.screenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE + } else { + if (brightness > 1.0) { + invoke.reject("Brightness must be between 0.0 and 1.0, or null to use system brightness") + return + } + layoutParams.screenBrightness = brightness + } + activity.window.attributes = layoutParams ret.put("success", true) } catch (e: Exception) { diff --git a/apps/readest-app/src-tauri/plugins/tauri-plugin-native-bridge/ios/Sources/NativeBridgePlugin.swift b/apps/readest-app/src-tauri/plugins/tauri-plugin-native-bridge/ios/Sources/NativeBridgePlugin.swift index ba4fc6d4..4bffe764 100644 --- a/apps/readest-app/src-tauri/plugins/tauri-plugin-native-bridge/ios/Sources/NativeBridgePlugin.swift +++ b/apps/readest-app/src-tauri/plugins/tauri-plugin-native-bridge/ios/Sources/NativeBridgePlugin.swift @@ -812,7 +812,13 @@ class NativeBridgePlugin: Plugin { let brightness = args.brightness ?? 0.5 - if brightness < 0.0 || brightness > 1.0 { + if brightness < 0.0 { + // Revert to system brightness - iOS doesn't have a direct "system brightness" setting + // We will restore the brightness that was set before the app modified it + return invoke.resolve(["success": true]) + } + + if brightness > 1.0 { return invoke.reject("Brightness must be between 0.0 and 1.0") } diff --git a/apps/readest-app/src/app/reader/components/Reader.tsx b/apps/readest-app/src/app/reader/components/Reader.tsx index dcd31dcc..3cf85b4c 100644 --- a/apps/readest-app/src/app/reader/components/Reader.tsx +++ b/apps/readest-app/src/app/reader/components/Reader.tsx @@ -52,9 +52,10 @@ Z-Index Layering Guide: const Reader: React.FC<{ ids?: string }> = ({ ids }) => { const router = useRouter(); const { appService } = useEnv(); - const { hoveredBookKey, getView } = useReaderStore(); const { settings } = useSettingsStore(); const { sideBarBookKey } = useSidebarStore(); + const { hoveredBookKey, getView } = useReaderStore(); + const { getScreenBrightness, setScreenBrightness } = useDeviceControlStore(); const { isSideBarVisible, getIsSideBarVisible, setSideBarVisible } = useSidebarStore(); const { isNotebookVisible, getIsNotebookVisible, setNotebookVisible } = useNotebookStore(); const { isDarkMode, systemUIAlwaysHidden, isRoundedWindow } = useThemeStore(); @@ -74,6 +75,27 @@ const Reader: React.FC<{ ids?: string }> = ({ ids }) => { initDayjs(getLocale()); }, []); + useEffect(() => { + const brightness = settings.screenBrightness; + const autoBrightness = settings.autoScreenBrightness; + if (appService?.hasScreenBrightness && !autoBrightness && brightness >= 0) { + setScreenBrightness(brightness / 100); + } + let previousBrightness = -1; + if (appService?.isIOSApp) { + getScreenBrightness().then((b) => { + previousBrightness = b; + }); + } + + return () => { + if (appService?.hasScreenBrightness && !autoBrightness) { + setScreenBrightness(previousBrightness); + } + }; + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [appService]); + const handleKeyDown = (event: CustomEvent) => { const view = getView(sideBarBookKey!); if (event.detail.keyName === 'Back') { diff --git a/apps/readest-app/src/components/Providers.tsx b/apps/readest-app/src/components/Providers.tsx index 01bebb41..fd89c3fb 100644 --- a/apps/readest-app/src/components/Providers.tsx +++ b/apps/readest-app/src/components/Providers.tsx @@ -10,7 +10,6 @@ import { CSPostHogProvider } from '@/context/PHContext'; import { SyncProvider } from '@/context/SyncContext'; import { initSystemThemeListener, loadDataTheme } from '@/store/themeStore'; import { useSettingsStore } from '@/store/settingsStore'; -import { useDeviceControlStore } from '@/store/deviceStore'; import { useSafeAreaInsets } from '@/hooks/useSafeAreaInsets'; import { useDefaultIconSize } from '@/hooks/useResponsiveSize'; import { useBackgroundTexture } from '@/hooks/useBackgroundTexture'; @@ -21,7 +20,6 @@ import { getDirFromUILanguage } from '@/utils/rtl'; const Providers = ({ children }: { children: React.ReactNode }) => { const { envConfig, appService } = useEnv(); const { applyUILanguage } = useSettingsStore(); - const { setScreenBrightness } = useDeviceControlStore(); const { applyBackgroundTexture } = useBackgroundTexture(); const { applyEinkMode } = useEinkMode(); const iconSize = useDefaultIconSize(); @@ -54,25 +52,13 @@ const Providers = ({ children }: { children: React.ReactNode }) => { appService.loadSettings().then((settings) => { const globalViewSettings = settings.globalViewSettings; applyUILanguage(globalViewSettings.uiLanguage); - const brightness = settings.screenBrightness; - const autoBrightness = settings.autoScreenBrightness; - if (appService.hasScreenBrightness && !autoBrightness && brightness >= 0) { - setScreenBrightness(brightness / 100); - } applyBackgroundTexture(envConfig, globalViewSettings); if (globalViewSettings.isEink) { applyEinkMode(true); } }); } - }, [ - envConfig, - appService, - applyUILanguage, - setScreenBrightness, - applyBackgroundTexture, - applyEinkMode, - ]); + }, [envConfig, appService, applyUILanguage, applyBackgroundTexture, applyEinkMode]); // Make sure appService is available in all children components if (!appService) return;