-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Add a new experimental web-specific API to preload fonts and images resources #5159
Open
eymar
wants to merge
11
commits into
master
Choose a base branch
from
ok/resource_font_preloading_to_cache
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+320
−7
Open
Changes from 6 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
f0cfc71
A draft for resource cache pre-initialization API on web
eymar 8259857
A draft for resource cache pre-initialization API on web
eymar 7317aa8
add dark theme resources to compose resources demo
eymar c627113
add a test for rememberResourceState to not ignore current ResourceEn…
eymar c5d80f1
add testPreloadFont
eymar 1eb1ade
Add documentation
eymar 1484074
revert unintended change
eymar 7151c14
doc improvements
eymar a73ca18
fix a typo in doc
eymar 42ccc1c
doc improvement
eymar 4925a65
more doc improvements
eymar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Binary file added
BIN
+10.3 KB
...resources/demo/shared/src/commonMain/composeResources/drawable-dark/compose.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+164 KB
...nts/resources/demo/shared/src/commonMain/composeResources/font-dark/Workbench-Regular.ttf
Binary file not shown.
Binary file added
BIN
+6.92 MB
components/resources/demo/shared/src/commonMain/composeResources/font/NotoColorEmoji.ttf
Binary file not shown.
48 changes: 46 additions & 2 deletions
48
components/resources/demo/shared/src/wasmJsMain/kotlin/main.wasm.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,15 +1,59 @@ | ||
import androidx.compose.ui.ExperimentalComposeUiApi | ||
import androidx.compose.foundation.background | ||
import androidx.compose.foundation.clickable | ||
import androidx.compose.foundation.layout.Box | ||
import androidx.compose.foundation.layout.fillMaxSize | ||
import androidx.compose.material3.CircularProgressIndicator | ||
import androidx.compose.runtime.* | ||
import androidx.compose.ui.* | ||
import androidx.compose.ui.graphics.Color | ||
import androidx.compose.ui.platform.LocalFontFamilyResolver | ||
import androidx.compose.ui.text.font.FontFamily | ||
import androidx.compose.ui.text.font.FontStyle | ||
import androidx.compose.ui.text.font.FontWeight | ||
import androidx.compose.ui.window.CanvasBasedWindow | ||
import components.resources.demo.shared.generated.resources.* | ||
import components.resources.demo.shared.generated.resources.NotoColorEmoji | ||
import components.resources.demo.shared.generated.resources.Res | ||
import components.resources.demo.shared.generated.resources.Workbench_Regular | ||
import components.resources.demo.shared.generated.resources.font_awesome | ||
import org.jetbrains.compose.resources.ExperimentalResourceApi | ||
import org.jetbrains.compose.resources.configureWebResources | ||
import org.jetbrains.compose.resources.demo.shared.UseResources | ||
import org.jetbrains.compose.resources.preloadFont | ||
|
||
@OptIn(ExperimentalComposeUiApi::class) | ||
@OptIn(ExperimentalComposeUiApi::class, ExperimentalResourceApi::class, InternalComposeUiApi::class) | ||
fun main() { | ||
configureWebResources { | ||
// Not necessary - It's the same as the default. We add it here just to present this feature. | ||
resourcePathMapping { path -> "./$path" } | ||
} | ||
CanvasBasedWindow("Resources demo + K/Wasm") { | ||
println("Theme = ${LocalSystemTheme.current}") | ||
Res.allFontResources.values.forEach { preloadFont(it) } | ||
val font1 by preloadFont(Res.font.Workbench_Regular) | ||
val font2 by preloadFont(Res.font.font_awesome, FontWeight.Normal, FontStyle.Normal) | ||
val emojiFont = preloadFont(Res.font.NotoColorEmoji).value | ||
var fontsFallbackInitialiazed by remember { mutableStateOf(false) } | ||
|
||
UseResources() | ||
|
||
if (font1 != null && font2 != null && emojiFont != null && fontsFallbackInitialiazed) { | ||
println("Fonts are ready") | ||
} else { | ||
Box(modifier = Modifier.fillMaxSize().background(Color.White.copy(alpha = 0.8f)).clickable { }) { | ||
CircularProgressIndicator(modifier = Modifier.align(Alignment.Center)) | ||
} | ||
println("Fonts are not ready yet") | ||
} | ||
|
||
|
||
val fontFamilyResolver = LocalFontFamilyResolver.current | ||
LaunchedEffect(fontFamilyResolver, emojiFont) { | ||
if (emojiFont != null) { | ||
// we have an emoji on Strings tab | ||
fontFamilyResolver.preload(FontFamily(listOf(emojiFont))) | ||
fontsFallbackInitialiazed = true | ||
} | ||
} | ||
} | ||
} |
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
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
77 changes: 77 additions & 0 deletions
77
...rces/library/src/webTest/kotlin/org/jetbrains/compose/resources/TestResourcePreloading.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 |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package org.jetbrains.compose.resources | ||
|
||
import androidx.compose.runtime.CompositionLocalProvider | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.setValue | ||
import androidx.compose.ui.test.ExperimentalTestApi | ||
import androidx.compose.ui.test.runComposeUiTest | ||
import androidx.compose.ui.text.font.Font | ||
import kotlinx.coroutines.CancellableContinuation | ||
import kotlinx.coroutines.suspendCancellableCoroutine | ||
import kotlin.io.encoding.Base64 | ||
import kotlin.io.encoding.ExperimentalEncodingApi | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
import kotlin.test.assertNotEquals | ||
|
||
@OptIn(ExperimentalTestApi::class, ExperimentalEncodingApi::class) | ||
class TestResourcePreloading { | ||
|
||
@Test | ||
fun testPreloadFont() = runComposeUiTest { | ||
var loadContinuation: CancellableContinuation<ByteArray>? = null | ||
|
||
val resLoader = object : ResourceReader { | ||
override suspend fun read(path: String): ByteArray { | ||
return suspendCancellableCoroutine { | ||
loadContinuation = it | ||
} | ||
} | ||
|
||
override suspend fun readPart(path: String, offset: Long, size: Long): ByteArray { | ||
TODO("Not yet implemented") | ||
} | ||
|
||
override fun getUri(path: String): String { | ||
TODO("Not yet implemented") | ||
} | ||
} | ||
|
||
var font: Font? = null | ||
var font2: Font? = null | ||
var condition by mutableStateOf(false) | ||
|
||
|
||
setContent { | ||
CompositionLocalProvider( | ||
LocalComposeEnvironment provides TestComposeEnvironment, | ||
LocalResourceReader provides resLoader | ||
) { | ||
font = preloadFont(TestFontResource("sometestfont")).value | ||
|
||
if (condition) { | ||
println("Herere!! - $font") | ||
font2 = Font(TestFontResource("sometestfont")) | ||
} | ||
} | ||
} | ||
waitForIdle() | ||
assertEquals(null, font) | ||
assertEquals(null, font2) | ||
|
||
assertNotEquals(null, loadContinuation) | ||
loadContinuation!!.resumeWith(Result.success(Base64.decode(emptyFontBase64))) | ||
terrakok marked this conversation as resolved.
Show resolved
Hide resolved
|
||
loadContinuation = null | ||
|
||
waitForIdle() | ||
assertNotEquals(null, font) | ||
assertEquals(null, font2) // condition was false for now, so font2 should be not initialized | ||
|
||
condition = true | ||
waitForIdle() | ||
assertNotEquals(null, font) | ||
assertEquals(font, font2, "font2 is expected to be loaded from cache") | ||
assertEquals(null, loadContinuation, "expected no more ResourceReader usages") | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
println is kinda debug style. do we need it yet?