-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor: Centralize Window Configuration and Enhance Fullscreen Hand…
…ling - Removed `WindowConfiguration` data class and its associated logic. - Introduced `PlatformWindow` to manage window-related states, including `isUndecoratedFullscreen` and `isLandscape`. - Centralized fullscreen state management in `PlatformWindow` across Android and desktop. - Improved fullscreen detection and handling by leveraging `PlatformWindow`. - Modified `WindowsWindowUtils` to use `isWindowsUndecoratedFullscreen`. - Adjusted `WindowsWindowFrame` to utilize `isUndecoratedFullscreen`. - Updated `EpisodePage` and `AniDesktop` to use `PlatformWindow` for fullscreen checks. - Added landscape mode support to `PlatformWindow`. - Updated UI components to use `PlatformWindow` properties. - Enhanced Android's `PlatformWindow` to handle insets and configuration changes. - Added `Platform` property in desktop's `PlatformWindow`. - Updated preview composition to use `PlatformWindow`. - Update `isSystemInFullscreen` and `isInLandscapeMode` to get state from `PlatformWindow`.
- Loading branch information
Showing
17 changed files
with
160 additions
and
221 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 84 additions & 1 deletion
85
app/shared/ui-foundation/src/androidMain/kotlin/platform/PlatformWindow.android.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,89 @@ | ||
package me.him188.ani.app.platform | ||
|
||
import android.app.Activity | ||
import android.content.ComponentCallbacks | ||
import android.content.res.Configuration | ||
import android.os.Build | ||
import android.view.View | ||
import android.view.WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.setValue | ||
import androidx.core.view.ViewCompat | ||
import androidx.core.view.WindowInsetsCompat | ||
|
||
/** | ||
* PC 上的 Window. Android 上没有 | ||
*/ | ||
actual class PlatformWindow | ||
actual class PlatformWindow(private val context: Context) : AutoCloseable { | ||
private val activity = context.findActivity() | ||
private val decorView = activity?.window?.decorView | ||
|
||
private var _isLandscape: Boolean by mutableStateOf(isLandscape(context.resources.configuration)) | ||
actual val isLandscape: Boolean get() = _isLandscape | ||
|
||
private var _isUndecoratedFullscreen: Boolean by mutableStateOf(isInFullscreenMode(context)) | ||
actual val isUndecoratedFullscreen: Boolean get() = _isUndecoratedFullscreen | ||
|
||
private val insetListener = View.OnApplyWindowInsetsListener { _, insets -> | ||
@Suppress("DEPRECATION") | ||
val isFullscreenNow = when { | ||
Build.VERSION.SDK_INT >= Build.VERSION_CODES.R -> !insets.isVisible(android.view.WindowInsets.Type.systemBars()) | ||
else -> insets.systemWindowInsetTop == 0 | ||
} | ||
if (isFullscreenNow != _isUndecoratedFullscreen) { | ||
_isUndecoratedFullscreen = isFullscreenNow | ||
} | ||
|
||
insets | ||
} | ||
|
||
private val configurationListener = object : ComponentCallbacks { | ||
override fun onLowMemory() { | ||
} | ||
|
||
override fun onConfigurationChanged(newConfig: Configuration) { | ||
_isLandscape = isLandscape(newConfig) | ||
} | ||
} | ||
|
||
init { | ||
|
||
//register window inset listener | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { | ||
decorView?.setOnApplyWindowInsetsListener(insetListener) | ||
} else if (decorView != null) { | ||
ViewCompat.setOnApplyWindowInsetsListener(decorView) { v, insets -> | ||
val toWindowInsets = insets.toWindowInsets()!! | ||
insetListener.onApplyWindowInsets(v, toWindowInsets) | ||
WindowInsetsCompat.toWindowInsetsCompat(toWindowInsets) | ||
} | ||
} | ||
|
||
//register resource change listener | ||
context.registerComponentCallbacks(configurationListener) | ||
} | ||
|
||
override fun close() { | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { | ||
decorView?.setOnApplyWindowInsetsListener(null) | ||
} else if (decorView != null) { | ||
ViewCompat.setOnApplyWindowInsetsListener(decorView, null) | ||
} | ||
context.unregisterComponentCallbacks(configurationListener) | ||
} | ||
} | ||
|
||
@Suppress("DEPRECATION") | ||
private fun isInFullscreenMode(context: Context): Boolean { | ||
val window = (context as? Activity)?.window ?: return false | ||
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { | ||
val insetsController = window.insetsController | ||
insetsController != null && insetsController.systemBarsBehavior == BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE | ||
} else { | ||
val decorView = window.decorView | ||
(decorView.systemUiVisibility and View.SYSTEM_UI_FLAG_FULLSCREEN) != 0 | ||
} | ||
} | ||
|
||
private fun isLandscape(configuration: Configuration) = configuration.orientation == Configuration.ORIENTATION_LANDSCAPE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 0 additions & 100 deletions
100
.../ui-foundation/src/androidMain/kotlin/ui/foundation/window/WindowConfiguration.android.kt
This file was deleted.
Oops, something went wrong.
9 changes: 7 additions & 2 deletions
9
app/shared/ui-foundation/src/commonMain/kotlin/platform/PlatformWindow.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,11 @@ | ||
package me.him188.ani.app.platform | ||
|
||
/** | ||
* PC 上的 Window. Android 上没有 | ||
* 公共的 Window 抽象 | ||
* @property isUndecoratedFullscreen 当前窗口是否处于全屏模式 | ||
* @property isLandscape 当前窗口是否处于横屏模式,PC 上一定处于横屏模式,Android 从 configuration 里读 | ||
*/ | ||
expect class PlatformWindow | ||
expect class PlatformWindow { | ||
val isUndecoratedFullscreen: Boolean | ||
val isLandscape: Boolean | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.