tsb_common_page.lua 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. --[[
  2. @module common_ui
  3. @summary 公共UI组件
  4. @version 1.0
  5. @date 2026.03.05
  6. @author 李一玮
  7. @usage
  8. 本文件提供公共UI组件功能,如电量显示等。
  9. ]]
  10. local common_ui = {}
  11. local time1_id = nil
  12. local show_charge_info = false -- 控制显示哪个文本
  13. local switch_counter = 0 -- 计数器,用于控制文本切换频率
  14. local shutdown_msgbox = nil -- 跟踪关机弹窗状态
  15. -- 在现有标题栏添加电量显示
  16. -- @param title_bar 标题栏容器对象
  17. -- @return battery_label 电量标签对象,用于后续更新
  18. function common_ui.add_battery_display(parent)
  19. -- 电量图标
  20. airui.image({
  21. parent = parent,
  22. x = 410,
  23. y = 5,
  24. w = 40,
  25. h = 20,
  26. src = "/luadb/dian3.png",
  27. zoom = 200,
  28. })
  29. -- 电量值标签
  30. local battery_label = airui.label({
  31. parent = parent,
  32. text = "70%",
  33. x = 445,
  34. y = 8,
  35. w = 35,
  36. h = 20,
  37. font_size = 14,
  38. color = 0xFFFFFF,
  39. })
  40. -- 信号图标
  41. airui.image({
  42. parent = parent,
  43. x = 355,
  44. y = 5,
  45. w = 40,
  46. h = 20,
  47. src = "/luadb/4g3.png",
  48. zoom = 160,
  49. })
  50. -- 信号值标签
  51. local battery_label = airui.label({
  52. parent = parent,
  53. text = "4G",
  54. x = 390,
  55. y = 8,
  56. w = 45,
  57. h = 20,
  58. font_size = 14,
  59. color = 0xFFFFFF,
  60. })
  61. -- WIFI图标
  62. airui.image({
  63. parent = parent,
  64. x = 330,
  65. y = 5,
  66. w = 40,
  67. h = 20,
  68. src = "/luadb/wifi3.png",
  69. zoom = 150,
  70. })
  71. return battery_label
  72. end
  73. function common_ui.create_back_button(parent, cleanup_func)
  74. -- local back_btn = airui.button({
  75. -- parent = parent,
  76. -- x = 10,
  77. -- y = 3,
  78. -- w = 45,
  79. -- h = 25,
  80. -- text = "返回",
  81. -- on_click = function()
  82. -- -- 调用当前页面的清理函数
  83. -- if cleanup_func then
  84. -- cleanup_func()
  85. -- end
  86. -- -- 返回上一页
  87. -- go_back()
  88. -- end
  89. -- })
  90. local back_img = airui.image({
  91. parent = parent,
  92. x = 5,
  93. y = 4,
  94. w = 20,
  95. h = 25,
  96. src = "/luadb/back1.png",
  97. zoom = 256,
  98. opacity = 255,
  99. on_click = function()
  100. -- 调用当前页面的清理函数
  101. if cleanup_func then
  102. cleanup_func()
  103. end
  104. -- 返回上一页
  105. go_back()
  106. end
  107. })
  108. local back_label = airui.label({
  109. parent = parent,
  110. text = "返回",
  111. x = 22,
  112. y = 9,
  113. w = 45,
  114. h = 20,
  115. font_size = 15,
  116. color = 0xFFFFFF,
  117. on_click = function()
  118. -- 调用当前页面的清理函数
  119. if cleanup_func then
  120. cleanup_func()
  121. end
  122. -- 返回上一页
  123. go_back()
  124. end
  125. })
  126. -- local home_btn = airui.button({
  127. -- parent = parent,
  128. -- x = 60,
  129. -- y = 3,
  130. -- w = 45,
  131. -- h = 25,
  132. -- text = "首页",
  133. -- on_click = function()
  134. -- if cleanup_func then
  135. -- cleanup_func()
  136. -- end
  137. -- go_home()
  138. -- end
  139. -- })
  140. local home_img = airui.image({
  141. parent = parent,
  142. x = 65,
  143. y = 4,
  144. w = 20,
  145. h = 25,
  146. src = "/luadb/home2.png",
  147. zoom = 140,
  148. opacity = 255,
  149. on_click = function()
  150. if cleanup_func then
  151. cleanup_func()
  152. end
  153. go_home()
  154. end
  155. })
  156. local home_label = airui.label({
  157. parent = parent,
  158. text = "首页",
  159. x = 87,
  160. y = 9,
  161. w = 45,
  162. h = 20,
  163. font_size = 15,
  164. color = 0xFFFFFF,
  165. on_click = function()
  166. if cleanup_func then
  167. cleanup_func()
  168. end
  169. go_home()
  170. end
  171. })
  172. return back_img
  173. end
  174. function common_ui.add_background_png(parent)
  175. local background_img = airui.image({
  176. parent = parent,
  177. x = 0,
  178. y = 0,
  179. w = 480,
  180. h = 290,
  181. src = "/luadb/1.png",
  182. zoom = 256,
  183. opacity = 255
  184. })
  185. return background_img
  186. end
  187. -- 创建底部信息栏
  188. -- @param main_container 主容器对象
  189. -- @return status_bar 底部信息栏容器对象
  190. function common_ui.create_status_bar(parent)
  191. -- 底部信息
  192. local status_bar = airui.container({
  193. parent = parent,
  194. x = 0,
  195. y = 300,
  196. w = 480,
  197. h = 20,
  198. color = 0xDFEAFF,
  199. })
  200. local version_charge_msg = airui.label({
  201. parent = status_bar,
  202. text = "TSB-3.0 v1.0.0",
  203. x = 0,
  204. y = 0,
  205. w = 180,
  206. h = 20,
  207. font_size = 14,
  208. })
  209. local shutdown_image = airui.image({
  210. parent = status_bar,
  211. x = 422,
  212. y = 0,
  213. w = 30,
  214. h = 20,
  215. src = "/luadb/shutdown.png",
  216. zoom = 200,
  217. opacity = 255,
  218. on_click = function(self)
  219. common_ui.show_shutdown_confirm(status_bar)
  220. end
  221. })
  222. local shutdwon_label = airui.label({
  223. parent = status_bar,
  224. text = "关机",
  225. x = 445,
  226. y = 2,
  227. w = 30,
  228. h = 18,
  229. font_size = 14,
  230. on_click = function(self)
  231. common_ui.show_shutdown_confirm(status_bar)
  232. end
  233. })
  234. local dynamic_label = airui.label({
  235. parent = status_bar,
  236. text = os.date("%Y-%m-%d %H:%M:%S"),
  237. x = 170,
  238. y = 0,
  239. w = 180,
  240. h = 20,
  241. font_size = 14,
  242. })
  243. -- 先停止已存在的定时器,避免多个定时器同时运行
  244. if time1_id then
  245. sys.timerStop(time1_id)
  246. time1_id = nil
  247. end
  248. -- 启动新的定时器
  249. time1_id = sys.timerLoopStart(function()
  250. local current_time = os.date("%Y-%m-%d %H:%M:%S")
  251. -- 检查dynamic_label是否仍然存在
  252. if dynamic_label then
  253. dynamic_label:set_text(current_time)
  254. else
  255. -- 如果标签不存在,停止定时器
  256. if time1_id then
  257. sys.timerStop(time1_id)
  258. time1_id = nil
  259. end
  260. end
  261. -- 每两次定时切换一次文本(即1800ms)
  262. if version_charge_msg then
  263. switch_counter = switch_counter + 1
  264. if switch_counter >= 2 then
  265. show_charge_info = not show_charge_info -- 切换状态
  266. if show_charge_info then
  267. version_charge_msg:set_text("充电中。。充电功率:7.5W")
  268. else
  269. version_charge_msg:set_text("TSB-3.0 v1.0.0")
  270. end
  271. switch_counter = 0 -- 重置计数器
  272. end
  273. end
  274. end, 900) -- 每900ms更新一次时间
  275. return status_bar
  276. end
  277. -- 显示关机确认弹窗
  278. -- @param parent 父容器对象
  279. function common_ui.show_shutdown_confirm(parent)
  280. -- 检查是否已经存在关机弹窗
  281. if shutdown_msgbox then
  282. log.info("shutdown", "关机弹窗已存在,不重复创建")
  283. return
  284. end
  285. shutdown_msgbox = airui.msgbox({
  286. title = "系统提示",
  287. text = "是否确认关机?",
  288. buttons = { "否", "是" },
  289. on_action = function(self, label)
  290. if label == "是" then
  291. -- 执行关机操作
  292. log.info("shutdown", "用户确认关机")
  293. pm.shutdown()
  294. else
  295. -- 取消关机操作
  296. log.info("shutdown", "用户取消关机")
  297. end
  298. self:hide()
  299. -- 重置弹窗状态
  300. shutdown_msgbox = nil
  301. end
  302. })
  303. shutdown_msgbox:show()
  304. end
  305. -- 清理函数,用于停止定时器
  306. function common_ui.cleanup()
  307. if time1_id then
  308. sys.timerStop(time1_id)
  309. time1_id = nil
  310. end
  311. end
  312. return common_ui