Skip to content
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

Merged
merged 12 commits into from
Nov 7, 2024

Conversation

eymar
Copy link
Member

@eymar eymar commented Nov 5, 2024

Fixes https://youtrack.jetbrains.com/issue/CMP-7565/Add-a-new-experimental-web-specific-API-to-preload-fonts-and-images-resources

Add a new experimental web-specific API to preload fonts and images:

@Composable
fun preloadFont(
    resource: FontResource,
    weight: FontWeight = FontWeight.Normal,
    style: FontStyle = FontStyle.Normal
): State<Font?>

@Composable
fun preloadImageBitmap(
    resource: DrawableResource,
): State<ImageBitmap?>

@Composable
fun preloadImageVector(
    resource: DrawableResource,
): State<ImageVector?>

Using this methods in advance, it's possible to avoid FOUT (flash of unstyled text), or flickering of images/icons.

Usage example:

val font1 by preloadFont(Res.font.Workbench_Regular)
val font2 by preloadFont(Res.font.font_awesome, FontWeight.Normal, FontStyle.Normal)

UseResources() // Main App that uses the above fonts

if (font1 != null && font2 != null) {
    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")
}

Release Notes

Features - Resources

  • Add new API to preload and cache font and image resources on web targets: preloadFont, preloadImageBitmap, preloadImageVector

@eymar eymar requested a review from terrakok November 5, 2024 16:02
Copy link
Member

@terrakok terrakok left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wait changes

@eymar eymar requested a review from Schahen November 6, 2024 11:21
@eymar eymar changed the title A draft for resource cache pre-initialization API on web Add a new experimental web-specific API to preload fonts and images resources Nov 6, 2024
Copy link
Member

@terrakok terrakok left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of the "nice" I recommend to use a "heavy" word in resource names in the doc

@eymar eymar merged commit b23f990 into master Nov 7, 2024
@eymar eymar deleted the ok/resource_font_preloading_to_cache branch November 7, 2024 14:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants