--[[ @module common_ui @summary 公共UI组件 @version 1.0 @date 2026.03.05 @author 李一玮 @usage 本文件提供公共UI组件功能,如电量显示等。 ]] local common_ui = {} local time1_id = nil local show_charge_info = false -- 控制显示哪个文本 local switch_counter = 0 -- 计数器,用于控制文本切换频率 local shutdown_msgbox = nil -- 跟踪关机弹窗状态 -- 在现有标题栏添加电量显示 -- @param title_bar 标题栏容器对象 -- @return battery_label 电量标签对象,用于后续更新 function common_ui.add_battery_display(parent) -- 电量图标 airui.image({ parent = parent, x = 410, y = 5, w = 40, h = 20, src = "/luadb/dian3.png", zoom = 200, }) -- 电量值标签 local battery_label = airui.label({ parent = parent, text = "70%", x = 445, y = 8, w = 35, h = 20, font_size = 14, color = 0xFFFFFF, }) -- 信号图标 airui.image({ parent = parent, x = 355, y = 5, w = 40, h = 20, src = "/luadb/4g3.png", zoom = 160, }) -- 信号值标签 local battery_label = airui.label({ parent = parent, text = "4G", x = 390, y = 8, w = 45, h = 20, font_size = 14, color = 0xFFFFFF, }) -- WIFI图标 airui.image({ parent = parent, x = 330, y = 5, w = 40, h = 20, src = "/luadb/wifi3.png", zoom = 150, }) return battery_label end function common_ui.create_back_button(parent, cleanup_func) -- local back_btn = airui.button({ -- parent = parent, -- x = 10, -- y = 3, -- w = 45, -- h = 25, -- text = "返回", -- on_click = function() -- -- 调用当前页面的清理函数 -- if cleanup_func then -- cleanup_func() -- end -- -- 返回上一页 -- go_back() -- end -- }) local back_img = airui.image({ parent = parent, x = 5, y = 4, w = 20, h = 25, src = "/luadb/back1.png", zoom = 256, opacity = 255, on_click = function() -- 调用当前页面的清理函数 if cleanup_func then cleanup_func() end -- 返回上一页 go_back() end }) local back_label = airui.label({ parent = parent, text = "返回", x = 22, y = 9, w = 45, h = 20, font_size = 15, color = 0xFFFFFF, on_click = function() -- 调用当前页面的清理函数 if cleanup_func then cleanup_func() end -- 返回上一页 go_back() end }) -- local home_btn = airui.button({ -- parent = parent, -- x = 60, -- y = 3, -- w = 45, -- h = 25, -- text = "首页", -- on_click = function() -- if cleanup_func then -- cleanup_func() -- end -- go_home() -- end -- }) local home_img = airui.image({ parent = parent, x = 65, y = 4, w = 20, h = 25, src = "/luadb/home2.png", zoom = 140, opacity = 255, on_click = function() if cleanup_func then cleanup_func() end go_home() end }) local home_label = airui.label({ parent = parent, text = "首页", x = 87, y = 9, w = 45, h = 20, font_size = 15, color = 0xFFFFFF, on_click = function() if cleanup_func then cleanup_func() end go_home() end }) return back_img end function common_ui.add_background_png(parent) local background_img = airui.image({ parent = parent, x = 0, y = 0, w = 480, h = 290, src = "/luadb/1.png", zoom = 256, opacity = 255 }) return background_img end -- 创建底部信息栏 -- @param main_container 主容器对象 -- @return status_bar 底部信息栏容器对象 function common_ui.create_status_bar(parent) -- 底部信息 local status_bar = airui.container({ parent = parent, x = 0, y = 300, w = 480, h = 20, color = 0xDFEAFF, }) local version_charge_msg = airui.label({ parent = status_bar, text = "TSB-3.0 v1.0.0", x = 0, y = 0, w = 180, h = 20, font_size = 14, }) local shutdown_image = airui.image({ parent = status_bar, x = 422, y = 0, w = 30, h = 20, src = "/luadb/shutdown.png", zoom = 200, opacity = 255, on_click = function(self) common_ui.show_shutdown_confirm(status_bar) end }) local shutdwon_label = airui.label({ parent = status_bar, text = "关机", x = 445, y = 2, w = 30, h = 18, font_size = 14, on_click = function(self) common_ui.show_shutdown_confirm(status_bar) end }) local dynamic_label = airui.label({ parent = status_bar, text = os.date("%Y-%m-%d %H:%M:%S"), x = 170, y = 0, w = 180, h = 20, font_size = 14, }) -- 先停止已存在的定时器,避免多个定时器同时运行 if time1_id then sys.timerStop(time1_id) time1_id = nil end -- 启动新的定时器 time1_id = sys.timerLoopStart(function() local current_time = os.date("%Y-%m-%d %H:%M:%S") -- 检查dynamic_label是否仍然存在 if dynamic_label then dynamic_label:set_text(current_time) else -- 如果标签不存在,停止定时器 if time1_id then sys.timerStop(time1_id) time1_id = nil end end -- 每两次定时切换一次文本(即1800ms) if version_charge_msg then switch_counter = switch_counter + 1 if switch_counter >= 2 then show_charge_info = not show_charge_info -- 切换状态 if show_charge_info then version_charge_msg:set_text("充电中。。充电功率:7.5W") else version_charge_msg:set_text("TSB-3.0 v1.0.0") end switch_counter = 0 -- 重置计数器 end end end, 900) -- 每900ms更新一次时间 return status_bar end -- 显示关机确认弹窗 -- @param parent 父容器对象 function common_ui.show_shutdown_confirm(parent) -- 检查是否已经存在关机弹窗 if shutdown_msgbox then log.info("shutdown", "关机弹窗已存在,不重复创建") return end shutdown_msgbox = airui.msgbox({ title = "系统提示", text = "是否确认关机?", buttons = { "否", "是" }, on_action = function(self, label) if label == "是" then -- 执行关机操作 log.info("shutdown", "用户确认关机") pm.shutdown() else -- 取消关机操作 log.info("shutdown", "用户取消关机") end self:hide() -- 重置弹窗状态 shutdown_msgbox = nil end }) shutdown_msgbox:show() end -- 清理函数,用于停止定时器 function common_ui.cleanup() if time1_id then sys.timerStop(time1_id) time1_id = nil end end return common_ui