|
| 1 | +# core.ui.application |
| 2 | + |
| 3 | +This module provides the main application container for the terminal UI system. You can inherit from `application` to implement your own terminal UI applications. |
| 4 | + |
| 5 | +::: tip TIP |
| 6 | +To use this module, you need to import it first: `import("core.ui.application")` |
| 7 | +::: |
| 8 | + |
| 9 | +::: tip NOTE |
| 10 | +The UI module is primarily used for xmake's internal `xmake f --menu` menu-based visual configuration. It provides basic UI components that can also be used by users to implement their own terminal UIs. |
| 11 | +::: |
| 12 | + |
| 13 | +The application module uses inheritance. You create your own application instance and customize its behavior: |
| 14 | + |
| 15 | +```lua |
| 16 | +import("core.ui.application") |
| 17 | + |
| 18 | +local app = application() |
| 19 | + |
| 20 | +function app:init() |
| 21 | + -- Your initialization code |
| 22 | +end |
| 23 | +``` |
| 24 | + |
| 25 | +## application:new |
| 26 | + |
| 27 | +- Create a new application instance |
| 28 | + |
| 29 | +#### Function Prototype |
| 30 | + |
| 31 | +::: tip API |
| 32 | +```lua |
| 33 | +local app = application() |
| 34 | +``` |
| 35 | +::: |
| 36 | + |
| 37 | +#### Parameter Description |
| 38 | + |
| 39 | +No parameters for constructor |
| 40 | + |
| 41 | +#### Return Value |
| 42 | + |
| 43 | +| Type | Description | |
| 44 | +|------|-------------| |
| 45 | +| application | Returns a new application instance | |
| 46 | + |
| 47 | +#### Usage |
| 48 | + |
| 49 | +Create an application instance that you can customize: |
| 50 | + |
| 51 | +```lua |
| 52 | +import("core.ui.application") |
| 53 | + |
| 54 | +local demo = application() |
| 55 | +``` |
| 56 | + |
| 57 | +## application:init |
| 58 | + |
| 59 | +- Initialize the application |
| 60 | + |
| 61 | +#### Function Prototype |
| 62 | + |
| 63 | +::: tip API |
| 64 | +```lua |
| 65 | +application:init(name: <string>, argv?: <table>) |
| 66 | +``` |
| 67 | +::: |
| 68 | + |
| 69 | +#### Parameter Description |
| 70 | + |
| 71 | +| Parameter | Description | |
| 72 | +|-----------|-------------| |
| 73 | +| name | Required. Application name string | |
| 74 | +| argv | Optional. Command line arguments table | |
| 75 | + |
| 76 | +#### Return Value |
| 77 | + |
| 78 | +No return value |
| 79 | + |
| 80 | +#### Usage |
| 81 | + |
| 82 | +Initialize your custom application. This is called automatically by `run()`: |
| 83 | + |
| 84 | +```lua |
| 85 | +function demo:init() |
| 86 | + application.init(self, "myapp") |
| 87 | + self:background_set("blue") |
| 88 | + |
| 89 | + -- Add your UI components here |
| 90 | + self:insert(self:main_dialog()) |
| 91 | +end |
| 92 | +``` |
| 93 | + |
| 94 | +## application:run |
| 95 | + |
| 96 | +- Run the application and start the event loop |
| 97 | + |
| 98 | +#### Function Prototype |
| 99 | + |
| 100 | +::: tip API |
| 101 | +```lua |
| 102 | +application:run(...) |
| 103 | +``` |
| 104 | +::: |
| 105 | + |
| 106 | +#### Parameter Description |
| 107 | + |
| 108 | +| Parameter | Description | |
| 109 | +|-----------|-------------| |
| 110 | +| ... | Optional. Additional arguments | |
| 111 | + |
| 112 | +#### Return Value |
| 113 | + |
| 114 | +No return value |
| 115 | + |
| 116 | +#### Usage |
| 117 | + |
| 118 | +Start your application. This initializes the UI, calls `init()`, and starts the event loop: |
| 119 | + |
| 120 | +```lua |
| 121 | +local demo = application() |
| 122 | + |
| 123 | +function demo:init() |
| 124 | + application.init(self, "demo") |
| 125 | + self:background_set("blue") |
| 126 | + self:insert(self:main_dialog()) |
| 127 | +end |
| 128 | + |
| 129 | +-- Start the application |
| 130 | +demo:run() |
| 131 | +``` |
| 132 | + |
| 133 | +## application:background_set |
| 134 | + |
| 135 | +- Set the application background color |
| 136 | + |
| 137 | +#### Function Prototype |
| 138 | + |
| 139 | +::: tip API |
| 140 | +```lua |
| 141 | +application:background_set(color: <string>) |
| 142 | +``` |
| 143 | +::: |
| 144 | + |
| 145 | +#### Parameter Description |
| 146 | + |
| 147 | +| Parameter | Description | |
| 148 | +|-----------|-------------| |
| 149 | +| color | Required. Color name (e.g., "blue", "red") | |
| 150 | + |
| 151 | +#### Return Value |
| 152 | + |
| 153 | +No return value |
| 154 | + |
| 155 | +#### Usage |
| 156 | + |
| 157 | +Set the background color for your application: |
| 158 | + |
| 159 | +```lua |
| 160 | +self:background_set("blue") |
| 161 | +``` |
| 162 | + |
| 163 | +## application:insert |
| 164 | + |
| 165 | +- Insert a view into the application |
| 166 | + |
| 167 | +#### Function Prototype |
| 168 | + |
| 169 | +::: tip API |
| 170 | +```lua |
| 171 | +application:insert(view: <view>, opt?: <table>) |
| 172 | +``` |
| 173 | +::: |
| 174 | + |
| 175 | +#### Parameter Description |
| 176 | + |
| 177 | +| Parameter | Description | |
| 178 | +|-----------|-------------| |
| 179 | +| view | Required. View to insert | |
| 180 | +| opt | Optional. Options table, supports: `{centerx = true, centery = true}` | |
| 181 | + |
| 182 | +#### Return Value |
| 183 | + |
| 184 | +No return value |
| 185 | + |
| 186 | +#### Usage |
| 187 | + |
| 188 | +Add views to your application with optional positioning: |
| 189 | + |
| 190 | +```lua |
| 191 | +-- Add a dialog |
| 192 | +self:insert(self:main_dialog()) |
| 193 | + |
| 194 | +-- Add a centered dialog |
| 195 | +self:insert(self:input_dialog(), {centerx = true, centery = true}) |
| 196 | +``` |
| 197 | + |
| 198 | +## application:on_resize |
| 199 | + |
| 200 | +- Handle window resize events |
| 201 | + |
| 202 | +#### Function Prototype |
| 203 | + |
| 204 | +::: tip API |
| 205 | +```lua |
| 206 | +application:on_resize() |
| 207 | +``` |
| 208 | +::: |
| 209 | + |
| 210 | +#### Parameter Description |
| 211 | + |
| 212 | +No parameters |
| 213 | + |
| 214 | +#### Return Value |
| 215 | + |
| 216 | +No return value |
| 217 | + |
| 218 | +#### Usage |
| 219 | + |
| 220 | +Override this method to handle resize events and update your UI layout: |
| 221 | + |
| 222 | +```lua |
| 223 | +function demo:on_resize() |
| 224 | + self:main_dialog():bounds_set(rect{1, 1, self:width() - 1, self:height() - 1}) |
| 225 | + self:center(self:input_dialog(), {centerx = true, centery = true}) |
| 226 | + application.on_resize(self) |
| 227 | +end |
| 228 | +``` |
| 229 | + |
| 230 | +## application:menubar |
| 231 | + |
| 232 | +- Get the application's menu bar |
| 233 | + |
| 234 | +#### Function Prototype |
| 235 | + |
| 236 | +::: tip API |
| 237 | +```lua |
| 238 | +application:menubar() |
| 239 | +``` |
| 240 | +::: |
| 241 | + |
| 242 | +#### Parameter Description |
| 243 | + |
| 244 | +No parameters |
| 245 | + |
| 246 | +#### Return Value |
| 247 | + |
| 248 | +| Type | Description | |
| 249 | +|------|-------------| |
| 250 | +| menubar | Returns the menu bar instance | |
| 251 | + |
| 252 | +#### Usage |
| 253 | + |
| 254 | +Access the menu bar component: |
| 255 | + |
| 256 | +```lua |
| 257 | +local menubar = app:menubar() |
| 258 | +``` |
| 259 | + |
| 260 | +## application:desktop |
| 261 | + |
| 262 | +- Get the application's desktop area |
| 263 | + |
| 264 | +#### Function Prototype |
| 265 | + |
| 266 | +::: tip API |
| 267 | +```lua |
| 268 | +application:desktop() |
| 269 | +``` |
| 270 | +::: |
| 271 | + |
| 272 | +#### Parameter Description |
| 273 | + |
| 274 | +No parameters |
| 275 | + |
| 276 | +#### Return Value |
| 277 | + |
| 278 | +| Type | Description | |
| 279 | +|------|-------------| |
| 280 | +| desktop | Returns the desktop instance | |
| 281 | + |
| 282 | +#### Usage |
| 283 | + |
| 284 | +Access the desktop area for placing views: |
| 285 | + |
| 286 | +```lua |
| 287 | +local desktop = app:desktop() |
| 288 | +``` |
| 289 | + |
| 290 | +## application:statusbar |
| 291 | + |
| 292 | +- Get the application's status bar |
| 293 | + |
| 294 | +#### Function Prototype |
| 295 | + |
| 296 | +::: tip API |
| 297 | +```lua |
| 298 | +application:statusbar() |
| 299 | +``` |
| 300 | +::: |
| 301 | + |
| 302 | +#### Parameter Description |
| 303 | + |
| 304 | +No parameters |
| 305 | + |
| 306 | +#### Return Value |
| 307 | + |
| 308 | +| Type | Description | |
| 309 | +|------|-------------| |
| 310 | +| statusbar | Returns the status bar instance | |
| 311 | + |
| 312 | +#### Usage |
| 313 | + |
| 314 | +Access the status bar component: |
| 315 | + |
| 316 | +```lua |
| 317 | +local statusbar = app:statusbar() |
| 318 | +statusbar:text_set("Ready") |
| 319 | +``` |
| 320 | + |
0 commit comments