| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344 |
- --[[
- @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
|