summaryrefslogtreecommitdiff
path: root/tests/ui/dialog.lua
blob: b532bd460428367538613bfba9a708e95a104561 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
--!A cross-platform build utility based on Lua
--
-- Licensed to the Apache Software Foundation (ASF) under one
-- or more contributor license agreements.  See the NOTICE file
-- distributed with this work for additional information
-- regarding copyright ownership.  The ASF licenses this file
-- to you under the Apache License, Version 2.0 (the
-- "License"); you may not use this file except in compliance
-- with the License.  You may obtain a copy of the License at
--
--     http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-2020, Xmake Open Source Community.
--
-- @author      ruki
-- @file        dialog.lua
--

-- imports
import("core.ui.log")
import("core.ui.rect")
import("core.ui.view")
import("core.ui.label")
import("core.ui.event")
import("core.ui.boxdialog")
import("core.ui.textdialog")
import("core.ui.inputdialog")
import("core.ui.application")

-- the demo application
local demo = application()

-- init demo
function demo:init()

    -- init name
    application.init(self, "demo")

    -- init background
    self:background_set("blue")

    -- init main dialog
    self:insert(self:dialog_main())

    -- init input dialog
    self:insert(self:dialog_input(), {centerx = true, centery = true})

    -- init tips dialog
    self:insert(self:dialog_tips(), {centerx = true, centery = true})
end

-- main dialog
function demo:dialog_main()
    local dialog_main = self._DIALOG_MAIN
    if not dialog_main then
        dialog_main = boxdialog:new("dialog.main", rect {1, 1, self:width() - 1, self:height() - 1}, "main dialog")
        dialog_main:text():text_set("The project focuses on making development and building easier and provides many features (.e.g package, install, plugin, macro, action, option, task ...), so that any developer can quickly pick it up and enjoy the productivity boost when developing and building project.")
        dialog_main:button_add("tips", "< Tips >", function (v) self:view("dialog.tips"):show(true, {focused = true}) end)
        dialog_main:button_add("input", "< Input >", function (v) self:view("dialog.input"):show(true, {focused = true}) end)
        dialog_main:button_add("help", "< Help >", function (v) self:insert(self:dialog_help()) end)
        dialog_main:button_add("quit", "< Quit >", "cm_quit")
        self._DIALOG_MAIN = dialog_main
    end
    return dialog_main
end

-- help dialog
function demo:dialog_help()
    local dialog_help = self._DIALOG_HELP
    if not dialog_help then
        dialog_help = textdialog:new("dialog.help", rect {1, 1, self:width() - 1, self:height() - 1}, "README")
        dialog_help:option_set("scrollable", true)
        local helptext = nil
        local file = io.open("./LICENSE.md", 'r')
        if file then
            helptext = file:read("*a")
            file:close()
        end
        if helptext then
            dialog_help:text():text_set(helptext)
        end
        dialog_help:button_add("exit", "< Exit >", function (v) self:remove(dialog_help) end)
        self._DIALOG_HELP = dialog_help
    end
    return dialog_help
end

-- input dialog
function demo:dialog_input()
    local dialog_input = self._DIALOG_INPUT
    if not dialog_input then
        dialog_input = inputdialog:new("dialog.input", rect {0, 0, 50, 8}):background_set(self:dialog_main():frame():background())
        dialog_input:frame():background_set("cyan")
        dialog_input:text():text_set("please input text:"):textattr_set("red")
        dialog_input:button_add("no", "< No >", function (v) dialog_input:show(false) end)
        dialog_input:button_add("yes", "< Yes >", function (v)
                                                      self:dialog_main():text():text_set(dialog_input:textedit():text())
                                                      dialog_input:show(false)
                                                  end)
        dialog_input:show(false)
        self._DIALOG_INPUT = dialog_input
    end
    return dialog_input
end

-- tips dialog
function demo:dialog_tips()
    local dialog_tips = self._DIALOG_TIPS
    if not dialog_tips then
        dialog_tips = textdialog:new("dialog.tips", rect {0, 0, 50, 8}):background_set(self:dialog_main():frame():background())
        dialog_tips:frame():background_set("cyan")
        dialog_tips:text():text_set("hello ltui! (https://tboox.org)\nA cross-platform terminal ui library based on Lua"):textattr_set("red")
        dialog_tips:button_add("yes", "< Yes >", function (v) dialog_tips:show(false) end)
        dialog_tips:button_add("no", "< No >", function (v) dialog_tips:show(false) end)
        self._DIALOG_TIPS = dialog_tips
    end
    return dialog_tips
end

-- on resize
function demo:on_resize()
    self:dialog_main():bounds_set(rect {1, 1, self:width() - 1, self:height() - 1})
    self:dialog_help():bounds_set(rect {1, 1, self:width() - 1, self:height() - 1})
    self:center(self:dialog_input(), {centerx = true, centery = true})
    self:center(self:dialog_tips(), {centerx = true, centery = true})
    application.on_resize(self)
end

-- main entry
function main(...)
    demo:run(...)
end