From 88ac96242f0d36e53876ece9f90baf776616f0bc Mon Sep 17 00:00:00 2001 From: jacqueline Date: Wed, 3 Jul 2024 10:43:54 +1000 Subject: Load fonts asynchronously on a bg task This saves a second or two from bootup; AFAICT this *mostly* reclaims the dynamic fonts boot time regression. --- lua/font.lua | 36 +++++++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) (limited to 'lua/font.lua') diff --git a/lua/font.lua b/lua/font.lua index 7afa1e01..c76602ec 100644 --- a/lua/font.lua +++ b/lua/font.lua @@ -1,6 +1,36 @@ local lvgl = require("lvgl") -return { - fusion_12 = lvgl.Font("//lua/fonts/fusion12"), - fusion_10 = lvgl.Font("//lua/fonts/fusion10"), +local fonts = {} +local fonts_priv = { + has_invoked_cb = false, + cb = nil, } + +function fonts_priv.has_loaded_all() + return fonts.fusion_12 and fonts.fusion_10 +end + +function fonts_priv.invoke_cb() + if fonts_priv.has_invoked_cb or not fonts_priv.cb then return end + if not fonts_priv.has_loaded_all() then return end + fonts_priv.has_invoked_cb = true + fonts_priv.cb() +end + +lvgl.Font("//lua/fonts/fusion12", function(font) + fonts.fusion_12 = font + fonts_priv.invoke_cb() +end) + +lvgl.Font("//lua/fonts/fusion10", function(font) + fonts.fusion_10 = font + fonts_priv.invoke_cb() +end) + +function fonts.on_loaded(cb) + fonts_priv.cb = cb + fonts_priv.has_invoked_cb = false + fonts_priv.invoke_cb() +end + +return fonts -- cgit v1.2.3