win_page.lua 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. --[[
  2. @module win_page
  3. @summary 窗口组件演示页面
  4. @version 1.0
  5. @date 2026.01.30
  6. @author 江访
  7. @usage 本文件是窗口组件的演示页面,展示窗口的各种用法。
  8. ]]
  9. local win_page = {}
  10. ----------------------------------------------------------------
  11. -- 页面UI元素
  12. ----------------------------------------------------------------
  13. local main_container = nil
  14. local current_window = nil
  15. -- 开关状态
  16. local switch_states = {
  17. show_title = true, -- 显示标题
  18. change_style = false, -- 更改窗口样式
  19. add_components = false, -- 增加组件
  20. add_cancel_btn = false, -- 增加取消按钮
  21. add_multi_level = false -- 增加多级按钮
  22. }
  23. -- 开关组件引用
  24. local title_switch = nil
  25. local style_switch = nil
  26. local components_switch = nil
  27. local cancel_switch = nil
  28. local multi_switch = nil
  29. ----------------------------------------------------------------
  30. -- 创建基本窗口
  31. ----------------------------------------------------------------
  32. local function create_basic_window()
  33. if current_window then
  34. current_window:close()
  35. end
  36. local window_height = 280
  37. if switch_states.add_components then
  38. window_height = window_height + 40
  39. end
  40. if switch_states.add_multi_level then
  41. window_height = window_height + 40
  42. end
  43. local window_config = {
  44. parent = main_container,
  45. x = 40,
  46. y = 100,
  47. w = 240,
  48. h = window_height,
  49. close_btn = true,
  50. auto_center = false,
  51. -- on_close 在 v1.0.3 无效,v1.0.4 修复
  52. on_close = function()
  53. log.info("win", "窗口被关闭")
  54. current_window = nil
  55. end
  56. }
  57. if switch_states.show_title then
  58. window_config.title = "基本窗口"
  59. else
  60. window_config.title = ""
  61. end
  62. if switch_states.change_style then
  63. window_config.style = { radius = 10, border_width = 2 }
  64. end
  65. current_window = airui.win(window_config)
  66. local content_y = 20
  67. local content_label = airui.label({
  68. text = "这是一个基本窗口",
  69. x = 20,
  70. y = content_y,
  71. w = 160,
  72. h = 30,
  73. color = 0x333333,
  74. font_size = 14,
  75. })
  76. current_window:add_content(content_label)
  77. content_y = content_y + 40
  78. if switch_states.add_components then
  79. local sample_switch = airui.switch({
  80. x = 20,
  81. y = content_y,
  82. w = 60,
  83. h = 30,
  84. checked = true,
  85. on_change = function(self)
  86. log.info("win", "窗口内开关: " .. tostring(self:get_state()))
  87. end
  88. })
  89. current_window:add_content(sample_switch)
  90. current_window:add_content(airui.label({
  91. text = "示例开关",
  92. x = 90,
  93. y = content_y + 5,
  94. w = 80,
  95. h = 20,
  96. color = 0x333333,
  97. font_size = 12,
  98. }))
  99. content_y = content_y + 40
  100. end
  101. if switch_states.add_multi_level then
  102. local multi_btn = airui.button({
  103. text = "点击提示",
  104. x = 60,
  105. y = content_y,
  106. w = 80,
  107. h = 40,
  108. on_click = function(self)
  109. local msg = airui.msgbox({
  110. text = "这是二级提示消息",
  111. buttons = { "确定" },
  112. timeout = 2000,
  113. on_action = function(self, label)
  114. self:hide()
  115. end
  116. })
  117. msg:show()
  118. log.info("win", "多级按钮被点击")
  119. end
  120. })
  121. current_window:add_content(multi_btn)
  122. content_y = content_y + 40
  123. end
  124. local button_y = window_height - 160
  125. if switch_states.add_cancel_btn then
  126. local ok_btn = airui.button({
  127. text = "确定",
  128. x = 20,
  129. y = button_y,
  130. w = 80,
  131. h = 40,
  132. on_click = function(self)
  133. local msg = airui.msgbox({
  134. text = "点击了确定按钮",
  135. buttons = { "确定" },
  136. timeout = 1500,
  137. on_action = function(self, label)
  138. self:hide()
  139. end
  140. })
  141. msg:show()
  142. log.info("win", "确定按钮被点击")
  143. current_window:close()
  144. end
  145. })
  146. current_window:add_content(ok_btn)
  147. local cancel_btn = airui.button({
  148. text = "取消",
  149. x = 110,
  150. y = button_y,
  151. w = 80,
  152. h = 40,
  153. on_click = function(self)
  154. local msg = airui.msgbox({
  155. text = "点击了取消按钮",
  156. buttons = { "确定" },
  157. timeout = 1500,
  158. on_action = function(self, label)
  159. self:hide()
  160. end
  161. })
  162. msg:show()
  163. log.info("win", "取消按钮被点击")
  164. current_window:close()
  165. end
  166. })
  167. current_window:add_content(cancel_btn)
  168. else
  169. local ok_btn = airui.button({
  170. text = "确定",
  171. x = 60,
  172. y = button_y,
  173. w = 80,
  174. h = 40,
  175. on_click = function(self)
  176. local msg = airui.msgbox({
  177. text = "点击了确定按钮",
  178. buttons = { "确定" },
  179. timeout = 1500,
  180. on_action = function(self, label)
  181. self:hide()
  182. end
  183. })
  184. msg:show()
  185. log.info("win", "确定按钮被点击")
  186. current_window:close()
  187. end
  188. })
  189. current_window:add_content(ok_btn)
  190. end
  191. end
  192. ----------------------------------------------------------------
  193. -- 创建UI
  194. ----------------------------------------------------------------
  195. function win_page.create_ui()
  196. main_container = airui.container({
  197. parent = airui.screen,
  198. x = 0,
  199. y = 0,
  200. w = 320,
  201. h = 480,
  202. color = 0xF5F5F5,
  203. })
  204. -- 标题栏
  205. local title_bar = airui.container({
  206. parent = main_container,
  207. x = 0,
  208. y = 0,
  209. w = 320,
  210. h = 50,
  211. color = 0x009688,
  212. })
  213. airui.label({
  214. parent = title_bar,
  215. text = "窗口组件演示",
  216. x = 10,
  217. y = 15,
  218. w = 200,
  219. h = 20,
  220. font_size = 16,
  221. color = 0xFFFFFF,
  222. })
  223. -- 返回按钮
  224. local back_btn = airui.button({
  225. parent = title_bar,
  226. x = 250,
  227. y = 10,
  228. w = 60,
  229. h = 30,
  230. text = "返回",
  231. on_click = function(self)
  232. go_back()
  233. end
  234. })
  235. -- 滚动容器
  236. local scroll_container = airui.container({
  237. parent = main_container,
  238. x = 0,
  239. y = 60,
  240. w = 320,
  241. h = 370,
  242. color = 0xF5F5F5,
  243. })
  244. local current_y = 10
  245. -- 说明标签
  246. airui.label({
  247. parent = scroll_container,
  248. text = "开关控制窗口特性",
  249. x = 10,
  250. y = current_y,
  251. w = 300,
  252. h = 25,
  253. color = 0x333333,
  254. font_size = 14,
  255. })
  256. current_y = current_y + 30
  257. --------------------------------------------------------------------
  258. -- 开关控制区域
  259. --------------------------------------------------------------------
  260. airui.label({
  261. parent = scroll_container,
  262. text = "显示标题:",
  263. x = 20,
  264. y = current_y,
  265. w = 100,
  266. h = 30,
  267. color = 0x333333,
  268. font_size = 12,
  269. })
  270. title_switch = airui.switch({
  271. parent = scroll_container,
  272. x = 120,
  273. y = current_y,
  274. w = 60,
  275. h = 30,
  276. checked = switch_states.show_title,
  277. on_change = function(self)
  278. switch_states.show_title = self:get_state()
  279. log.info("win", "显示标题开关: " .. tostring(self:get_state()))
  280. end
  281. })
  282. current_y = current_y + 40
  283. airui.label({
  284. parent = scroll_container,
  285. text = "更改窗口样式:",
  286. x = 20,
  287. y = current_y,
  288. w = 100,
  289. h = 30,
  290. color = 0x333333,
  291. font_size = 12,
  292. })
  293. style_switch = airui.switch({
  294. parent = scroll_container,
  295. x = 120,
  296. y = current_y,
  297. w = 60,
  298. h = 30,
  299. checked = switch_states.change_style,
  300. on_change = function(self)
  301. switch_states.change_style = self:get_state()
  302. log.info("win", "更改窗口样式开关: " .. tostring(self:get_state()))
  303. end
  304. })
  305. current_y = current_y + 40
  306. airui.label({
  307. parent = scroll_container,
  308. text = "增加组件:",
  309. x = 20,
  310. y = current_y,
  311. w = 100,
  312. h = 30,
  313. color = 0x333333,
  314. font_size = 12,
  315. })
  316. components_switch = airui.switch({
  317. parent = scroll_container,
  318. x = 120,
  319. y = current_y,
  320. w = 60,
  321. h = 30,
  322. checked = switch_states.add_components,
  323. on_change = function(self)
  324. switch_states.add_components = self:get_state()
  325. log.info("win", "增加组件开关: " .. tostring(self:get_state()))
  326. end
  327. })
  328. current_y = current_y + 40
  329. airui.label({
  330. parent = scroll_container,
  331. text = "增加取消按钮:",
  332. x = 20,
  333. y = current_y,
  334. w = 100,
  335. h = 30,
  336. color = 0x333333,
  337. font_size = 12,
  338. })
  339. cancel_switch = airui.switch({
  340. parent = scroll_container,
  341. x = 120,
  342. y = current_y,
  343. w = 60,
  344. h = 30,
  345. checked = switch_states.add_cancel_btn,
  346. on_change = function(self)
  347. switch_states.add_cancel_btn = self:get_state()
  348. log.info("win", "增加取消按钮开关: " .. tostring(self:get_state()))
  349. end
  350. })
  351. current_y = current_y + 40
  352. airui.label({
  353. parent = scroll_container,
  354. text = "增加多级按钮:",
  355. x = 20,
  356. y = current_y,
  357. w = 100,
  358. h = 30,
  359. color = 0x333333,
  360. font_size = 12,
  361. })
  362. multi_switch = airui.switch({
  363. parent = scroll_container,
  364. x = 120,
  365. y = current_y,
  366. w = 60,
  367. h = 30,
  368. checked = switch_states.add_multi_level,
  369. on_change = function(self)
  370. switch_states.add_multi_level = self:get_state()
  371. log.info("win", "增加多级按钮开关: " .. tostring(self:get_state()))
  372. end
  373. })
  374. current_y = current_y + 50
  375. --------------------------------------------------------------------
  376. -- 控制按钮
  377. --------------------------------------------------------------------
  378. local show_window_btn = airui.button({
  379. parent = scroll_container,
  380. x = 40,
  381. y = current_y,
  382. w = 240,
  383. h = 50,
  384. text = "显示窗口",
  385. on_click = function(self)
  386. create_basic_window()
  387. end
  388. })
  389. current_y = current_y + 70
  390. local close_window_btn = airui.button({
  391. parent = scroll_container,
  392. x = 40,
  393. y = current_y,
  394. w = 100,
  395. h = 50,
  396. text = "关闭窗口",
  397. on_click = function(self)
  398. if current_window then
  399. current_window:close()
  400. current_window = nil
  401. else
  402. local msg = airui.msgbox({
  403. text = "没有打开的窗口",
  404. buttons = { "确定" },
  405. timeout = 1500,
  406. on_action = function(self, label)
  407. self:hide()
  408. end
  409. })
  410. msg:show()
  411. end
  412. end
  413. })
  414. local reset_btn = airui.button({
  415. parent = scroll_container,
  416. x = 180,
  417. y = current_y,
  418. w = 100,
  419. h = 50,
  420. text = "重置所有开关",
  421. on_click = function(self)
  422. switch_states = {
  423. show_title = true,
  424. change_style = false,
  425. add_components = false,
  426. add_cancel_btn = false,
  427. add_multi_level = false
  428. }
  429. -- v1.0.3 支持,低于V1.0.3版本使用会死机
  430. title_switch:set_state(switch_states.show_title)
  431. style_switch:set_state(switch_states.change_style)
  432. components_switch:set_state(switch_states.add_components)
  433. cancel_switch:set_state(switch_states.add_cancel_btn)
  434. multi_switch:set_state(switch_states.add_multi_level)
  435. local msg = airui.msgbox({
  436. text = "所有开关已重置",
  437. buttons = { "确定" },
  438. timeout = 1500,
  439. on_action = function(self, label)
  440. log.info("win", "所有开关已重置")
  441. self:hide()
  442. end
  443. })
  444. msg:show()
  445. end
  446. })
  447. -- 底部信息
  448. airui.label({
  449. parent = main_container,
  450. text = "提示: 使用开关控制窗口特性",
  451. x = 10,
  452. y = 440,
  453. w = 300,
  454. h = 20,
  455. color = 0x666666,
  456. font_size = 12,
  457. })
  458. end
  459. ----------------------------------------------------------------
  460. -- 初始化页面
  461. ----------------------------------------------------------------
  462. function win_page.init(params)
  463. win_page.create_ui()
  464. end
  465. ----------------------------------------------------------------
  466. -- 清理页面
  467. ----------------------------------------------------------------
  468. function win_page.cleanup()
  469. if current_window then
  470. current_window:close()
  471. current_window = nil
  472. end
  473. if main_container then
  474. main_container:destroy()
  475. main_container = nil
  476. end
  477. title_switch = nil
  478. style_switch = nil
  479. components_switch = nil
  480. cancel_switch = nil
  481. multi_switch = nil
  482. end
  483. return win_page