-
Notifications
You must be signed in to change notification settings - Fork 154
[web] Reuse same mediaQuery listener logic both for resize and theme listener #3231
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
dcd426a
6035835
8ea22ee
2bc6ec6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| /* | ||
| * Copyright 2026 The Android Open Source Project | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package androidx.compose.ui.window | ||
|
|
||
| import kotlin.js.js | ||
| import kotlinx.browser.window | ||
| import org.w3c.dom.MediaQueryList | ||
| import org.w3c.dom.MediaQueryListEvent | ||
| import org.w3c.dom.events.Event | ||
|
|
||
| internal abstract class MediaQueryListener(private val query: String) { | ||
| private val media: MediaQueryList by lazy { | ||
| window.matchMedia(query) | ||
| } | ||
|
|
||
| abstract fun onChange(matches: Boolean) | ||
|
|
||
| private val listener: (Event) -> Unit = { event -> | ||
| event as MediaQueryListEvent | ||
| onChange(event.matches) | ||
| } | ||
|
|
||
| fun dispose() { | ||
| try { | ||
| media.removeEventListener("change", listener) | ||
| } catch (t : Throwable) { | ||
| media.removeListener(listener) | ||
| } | ||
| } | ||
|
|
||
| init { | ||
| if (isMatchMediaSupported()) { | ||
| try { | ||
| media.addEventListener("change", listener) | ||
| } catch (t: Throwable) { | ||
| media.addListener(listener) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| fun matches(): Boolean? { | ||
| return when { | ||
| !isMatchMediaSupported() -> null | ||
| else -> media.matches | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // supported by all browsers since 2015 | ||
| // https://developer.mozilla.org/en-US/docs/Web/API/Window/matchMedia | ||
| // Changed from `@JsFun` annotation because in 2.2.20 it's marked as not available on LV = 2.0 | ||
| // TODO: Cannot add opt-in with LV = 2.0 due to https://youtrack.jetbrains.com/issue/KT-79716 | ||
| private fun isMatchMediaSupported(): Boolean = js("window.matchMedia != undefined") | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,68 +18,35 @@ package androidx.compose.ui.window | |
|
|
||
| import androidx.compose.runtime.State | ||
| import androidx.compose.runtime.mutableStateOf | ||
| import kotlin.js.js | ||
| import kotlinx.browser.window | ||
| import org.jetbrains.skiko.SystemTheme | ||
| import org.w3c.dom.MediaQueryList | ||
| import org.w3c.dom.MediaQueryListEvent | ||
| import org.w3c.dom.Window | ||
| import org.w3c.dom.events.Event | ||
|
|
||
| internal interface SystemThemeObserver { | ||
| val currentSystemTheme: State<SystemTheme> | ||
|
|
||
| fun dispose() | ||
| } | ||
|
|
||
| internal class SystemThemeObserverImpl(window : Window) : SystemThemeObserver { | ||
|
|
||
| private class SystemThemeObserverImpl : SystemThemeObserver { | ||
| override val currentSystemTheme: State<SystemTheme> | ||
| get() = _currentSystemTheme | ||
|
|
||
| private val media: MediaQueryList by lazy { | ||
| window.matchMedia("(prefers-color-scheme: dark)") | ||
| private val mediaQueryListener: MediaQueryListener = object : MediaQueryListener("(prefers-color-scheme: dark)") { | ||
| override fun onChange(matches: Boolean) { | ||
| _currentSystemTheme.value = if (matches) SystemTheme.DARK else SystemTheme.LIGHT | ||
| } | ||
| } | ||
|
|
||
| private val _currentSystemTheme = mutableStateOf( | ||
| when { | ||
| !isMatchMediaSupported() -> SystemTheme.UNKNOWN | ||
| media.matches -> SystemTheme.DARK | ||
| else -> SystemTheme.LIGHT | ||
| when(mediaQueryListener.matches()) { | ||
| true -> SystemTheme.DARK | ||
| false -> SystemTheme.LIGHT | ||
| else -> SystemTheme.UNKNOWN | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's a bit confusing to see a third branch when first 2 branches already cover true and false. What do you think about creating a value class for the returned value? For example: internal value class MediaQuery private constuctor(private val matchResult: Int) {
companion object {
val NOT_SUPPORTED = MediaQuery(-1)
val NO_MATCH = MediaQuery(0)
val MATCH = MediaQuery(1)
}
}then your when becomes:
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Or maybe it's not the responsibility of Then
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I absolutely don't mind having dedicated status if it's not overkill - Boolean? though is more or less an idiom for yes/no/we-don't-know but sure, performance-wise we won't lose anything and may be it will be clearer |
||
| } | ||
| ) | ||
|
|
||
| private val listener: (Event) -> Unit = { event -> | ||
| _currentSystemTheme.value = if ((event as MediaQueryListEvent).matches) | ||
| SystemTheme.DARK else SystemTheme.LIGHT | ||
| } | ||
|
|
||
| override fun dispose() { | ||
| if (isMatchMediaSupported()) { | ||
| try { | ||
| media.removeEventListener("change", listener) | ||
| } catch (t : Throwable) { | ||
| media.removeListener(listener) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| init { | ||
| if (isMatchMediaSupported()) { | ||
| try { | ||
| media.addEventListener("change", listener) | ||
| } catch (t: Throwable) { | ||
| media.addListener(listener) | ||
| } | ||
| } | ||
| mediaQueryListener.dispose() | ||
| } | ||
| } | ||
|
|
||
| internal fun getSystemThemeObserver(): SystemThemeObserver = | ||
| SystemThemeObserverImpl(window) | ||
|
|
||
| // supported by all browsers since 2015 | ||
| // https://developer.mozilla.org/en-US/docs/Web/API/Window/matchMedia | ||
| // Changed from `@JsFun` annotation because in 2.2.20 it's marked as not available on LV = 2.0 | ||
| // TODO: Cannot add opt-in with LV = 2.0 due to https://youtrack.jetbrains.com/issue/KT-79716 | ||
| private fun isMatchMediaSupported(): Boolean = js("window.matchMedia != undefined") | ||
| internal fun getSystemThemeObserver(): SystemThemeObserver = SystemThemeObserverImpl() | ||
Uh oh!
There was an error while loading. Please reload this page.