fix(settings): screen brightness setting only applies to the reader page, closes #2717 (#2720)

This commit is contained in:
Huang Xin
2025-12-15 13:04:29 +08:00
committed by GitHub
parent 0bd6a217ae
commit 7063d62b13
4 changed files with 43 additions and 23 deletions
@@ -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) {
@@ -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")
}
@@ -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') {
+1 -15
View File
@@ -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;