| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529 |
- --[[
- @module win_page
- @summary 窗口组件演示页面
- @version 1.0
- @date 2026.01.30
- @author 江访
- @usage 本文件是窗口组件的演示页面,展示窗口的各种用法。
- ]]
- local win_page = {}
- ----------------------------------------------------------------
- -- 页面UI元素
- ----------------------------------------------------------------
- local main_container = nil
- local current_window = nil
- -- 开关状态
- local switch_states = {
- show_title = true, -- 显示标题
- change_style = false, -- 更改窗口样式
- add_components = false, -- 增加组件
- add_cancel_btn = false, -- 增加取消按钮
- add_multi_level = false -- 增加多级按钮
- }
- -- 开关组件引用
- local title_switch = nil
- local style_switch = nil
- local components_switch = nil
- local cancel_switch = nil
- local multi_switch = nil
- ----------------------------------------------------------------
- -- 创建基本窗口
- ----------------------------------------------------------------
- local function create_basic_window()
- if current_window then
- current_window:close()
- end
- local window_height = 280
- if switch_states.add_components then
- window_height = window_height + 40
- end
- if switch_states.add_multi_level then
- window_height = window_height + 40
- end
- local window_config = {
- parent = main_container,
- x = 40,
- y = 100,
- w = 240,
- h = window_height,
- close_btn = true,
- auto_center = false,
- -- on_close 在 v1.0.3 无效,v1.0.4 修复
- on_close = function()
- log.info("win", "窗口被关闭")
- current_window = nil
- end
- }
- if switch_states.show_title then
- window_config.title = "基本窗口"
- else
- window_config.title = ""
- end
- if switch_states.change_style then
- window_config.style = { radius = 10, border_width = 2 }
- end
- current_window = airui.win(window_config)
- local content_y = 20
- local content_label = airui.label({
- text = "这是一个基本窗口",
- x = 20,
- y = content_y,
- w = 160,
- h = 30,
- color = 0x333333,
- font_size = 14,
- })
- current_window:add_content(content_label)
- content_y = content_y + 40
- if switch_states.add_components then
- local sample_switch = airui.switch({
- x = 20,
- y = content_y,
- w = 60,
- h = 30,
- checked = true,
- on_change = function(self)
- log.info("win", "窗口内开关: " .. tostring(self:get_state()))
- end
- })
- current_window:add_content(sample_switch)
- current_window:add_content(airui.label({
- text = "示例开关",
- x = 90,
- y = content_y + 5,
- w = 80,
- h = 20,
- color = 0x333333,
- font_size = 12,
- }))
- content_y = content_y + 40
- end
- if switch_states.add_multi_level then
- local multi_btn = airui.button({
- text = "点击提示",
- x = 60,
- y = content_y,
- w = 80,
- h = 40,
- on_click = function(self)
- local msg = airui.msgbox({
- text = "这是二级提示消息",
- buttons = { "确定" },
- timeout = 2000,
- on_action = function(self, label)
- self:hide()
- end
- })
- msg:show()
- log.info("win", "多级按钮被点击")
- end
- })
- current_window:add_content(multi_btn)
- content_y = content_y + 40
- end
- local button_y = window_height - 160
- if switch_states.add_cancel_btn then
- local ok_btn = airui.button({
- text = "确定",
- x = 20,
- y = button_y,
- w = 80,
- h = 40,
- on_click = function(self)
- local msg = airui.msgbox({
- text = "点击了确定按钮",
- buttons = { "确定" },
- timeout = 1500,
- on_action = function(self, label)
- self:hide()
- end
- })
- msg:show()
- log.info("win", "确定按钮被点击")
- current_window:close()
- end
- })
- current_window:add_content(ok_btn)
- local cancel_btn = airui.button({
- text = "取消",
- x = 110,
- y = button_y,
- w = 80,
- h = 40,
- on_click = function(self)
- local msg = airui.msgbox({
- text = "点击了取消按钮",
- buttons = { "确定" },
- timeout = 1500,
- on_action = function(self, label)
- self:hide()
- end
- })
- msg:show()
- log.info("win", "取消按钮被点击")
- current_window:close()
- end
- })
- current_window:add_content(cancel_btn)
- else
- local ok_btn = airui.button({
- text = "确定",
- x = 60,
- y = button_y,
- w = 80,
- h = 40,
- on_click = function(self)
- local msg = airui.msgbox({
- text = "点击了确定按钮",
- buttons = { "确定" },
- timeout = 1500,
- on_action = function(self, label)
- self:hide()
- end
- })
- msg:show()
- log.info("win", "确定按钮被点击")
- current_window:close()
- end
- })
- current_window:add_content(ok_btn)
- end
- end
- ----------------------------------------------------------------
- -- 创建UI
- ----------------------------------------------------------------
- function win_page.create_ui()
- main_container = airui.container({
- parent = airui.screen,
- x = 0,
- y = 0,
- w = 320,
- h = 480,
- color = 0xF5F5F5,
- })
- -- 标题栏
- local title_bar = airui.container({
- parent = main_container,
- x = 0,
- y = 0,
- w = 320,
- h = 50,
- color = 0x009688,
- })
- airui.label({
- parent = title_bar,
- text = "窗口组件演示",
- x = 10,
- y = 15,
- w = 200,
- h = 20,
- font_size = 16,
- color = 0xFFFFFF,
- })
- -- 返回按钮
- local back_btn = airui.button({
- parent = title_bar,
- x = 250,
- y = 10,
- w = 60,
- h = 30,
- text = "返回",
- on_click = function(self)
- go_back()
- end
- })
- -- 滚动容器
- local scroll_container = airui.container({
- parent = main_container,
- x = 0,
- y = 60,
- w = 320,
- h = 370,
- color = 0xF5F5F5,
- })
- local current_y = 10
- -- 说明标签
- airui.label({
- parent = scroll_container,
- text = "开关控制窗口特性",
- x = 10,
- y = current_y,
- w = 300,
- h = 25,
- color = 0x333333,
- font_size = 14,
- })
- current_y = current_y + 30
- --------------------------------------------------------------------
- -- 开关控制区域
- --------------------------------------------------------------------
- airui.label({
- parent = scroll_container,
- text = "显示标题:",
- x = 20,
- y = current_y,
- w = 100,
- h = 30,
- color = 0x333333,
- font_size = 12,
- })
- title_switch = airui.switch({
- parent = scroll_container,
- x = 120,
- y = current_y,
- w = 60,
- h = 30,
- checked = switch_states.show_title,
- on_change = function(self)
- switch_states.show_title = self:get_state()
- log.info("win", "显示标题开关: " .. tostring(self:get_state()))
- end
- })
- current_y = current_y + 40
- airui.label({
- parent = scroll_container,
- text = "更改窗口样式:",
- x = 20,
- y = current_y,
- w = 100,
- h = 30,
- color = 0x333333,
- font_size = 12,
- })
- style_switch = airui.switch({
- parent = scroll_container,
- x = 120,
- y = current_y,
- w = 60,
- h = 30,
- checked = switch_states.change_style,
- on_change = function(self)
- switch_states.change_style = self:get_state()
- log.info("win", "更改窗口样式开关: " .. tostring(self:get_state()))
- end
- })
- current_y = current_y + 40
- airui.label({
- parent = scroll_container,
- text = "增加组件:",
- x = 20,
- y = current_y,
- w = 100,
- h = 30,
- color = 0x333333,
- font_size = 12,
- })
- components_switch = airui.switch({
- parent = scroll_container,
- x = 120,
- y = current_y,
- w = 60,
- h = 30,
- checked = switch_states.add_components,
- on_change = function(self)
- switch_states.add_components = self:get_state()
- log.info("win", "增加组件开关: " .. tostring(self:get_state()))
- end
- })
- current_y = current_y + 40
- airui.label({
- parent = scroll_container,
- text = "增加取消按钮:",
- x = 20,
- y = current_y,
- w = 100,
- h = 30,
- color = 0x333333,
- font_size = 12,
- })
- cancel_switch = airui.switch({
- parent = scroll_container,
- x = 120,
- y = current_y,
- w = 60,
- h = 30,
- checked = switch_states.add_cancel_btn,
- on_change = function(self)
- switch_states.add_cancel_btn = self:get_state()
- log.info("win", "增加取消按钮开关: " .. tostring(self:get_state()))
- end
- })
- current_y = current_y + 40
- airui.label({
- parent = scroll_container,
- text = "增加多级按钮:",
- x = 20,
- y = current_y,
- w = 100,
- h = 30,
- color = 0x333333,
- font_size = 12,
- })
- multi_switch = airui.switch({
- parent = scroll_container,
- x = 120,
- y = current_y,
- w = 60,
- h = 30,
- checked = switch_states.add_multi_level,
- on_change = function(self)
- switch_states.add_multi_level = self:get_state()
- log.info("win", "增加多级按钮开关: " .. tostring(self:get_state()))
- end
- })
- current_y = current_y + 50
- --------------------------------------------------------------------
- -- 控制按钮
- --------------------------------------------------------------------
- local show_window_btn = airui.button({
- parent = scroll_container,
- x = 40,
- y = current_y,
- w = 240,
- h = 50,
- text = "显示窗口",
- on_click = function(self)
- create_basic_window()
- end
- })
- current_y = current_y + 70
- local close_window_btn = airui.button({
- parent = scroll_container,
- x = 40,
- y = current_y,
- w = 100,
- h = 50,
- text = "关闭窗口",
- on_click = function(self)
- if current_window then
- current_window:close()
- current_window = nil
- else
- local msg = airui.msgbox({
- text = "没有打开的窗口",
- buttons = { "确定" },
- timeout = 1500,
- on_action = function(self, label)
- self:hide()
- end
- })
- msg:show()
- end
- end
- })
- local reset_btn = airui.button({
- parent = scroll_container,
- x = 180,
- y = current_y,
- w = 100,
- h = 50,
- text = "重置所有开关",
- on_click = function(self)
- switch_states = {
- show_title = true,
- change_style = false,
- add_components = false,
- add_cancel_btn = false,
- add_multi_level = false
- }
- -- v1.0.3 支持,低于V1.0.3版本使用会死机
- title_switch:set_state(switch_states.show_title)
- style_switch:set_state(switch_states.change_style)
- components_switch:set_state(switch_states.add_components)
- cancel_switch:set_state(switch_states.add_cancel_btn)
- multi_switch:set_state(switch_states.add_multi_level)
- local msg = airui.msgbox({
- text = "所有开关已重置",
- buttons = { "确定" },
- timeout = 1500,
- on_action = function(self, label)
- log.info("win", "所有开关已重置")
- self:hide()
- end
- })
- msg:show()
- end
- })
- -- 底部信息
- airui.label({
- parent = main_container,
- text = "提示: 使用开关控制窗口特性",
- x = 10,
- y = 440,
- w = 300,
- h = 20,
- color = 0x666666,
- font_size = 12,
- })
- end
- ----------------------------------------------------------------
- -- 初始化页面
- ----------------------------------------------------------------
- function win_page.init(params)
- win_page.create_ui()
- end
- ----------------------------------------------------------------
- -- 清理页面
- ----------------------------------------------------------------
- function win_page.cleanup()
- if current_window then
- current_window:close()
- current_window = nil
- end
- if main_container then
- main_container:destroy()
- main_container = nil
- end
- title_switch = nil
- style_switch = nil
- components_switch = nil
- cancel_switch = nil
- multi_switch = nil
- end
- return win_page
|