Skip to content

Commit 9488005

Browse files
committed
Simplify attaching GUI components
Specify callbacks directly in attach function. This greatly improves readability.
1 parent e561765 commit 9488005

File tree

6 files changed

+50
-37
lines changed

6 files changed

+50
-37
lines changed

gui/gui.lua

+35-27
Original file line numberDiff line numberDiff line change
@@ -50,32 +50,35 @@ local function start_test(item)
5050

5151
window { title = item.fullpath }
5252

53-
local text_area = attach_textarea(
54-
gui,
55-
{ x = 0, y = 97, width = width, height = 103 }
56-
)
57-
5853
local function find_lua_file_in_text(text)
5954
return text:match("[^ ]*%.lua:%d+")
6055
end
6156

62-
function text_area:is_link(text)
63-
return text != nil and find_lua_file_in_text(text) != nil
64-
end
65-
66-
function text_area:link_click(text)
67-
if text == nil then return end
68-
69-
local file = find_lua_file_in_text(text)
70-
if file != nil then
71-
file = file:gsub(":", "#")
72-
-- TODO if file is already open in text editor then this
73-
-- command does not go to the specific line number.
74-
-- Please note though, that in case of an unhandled error,
75-
-- Picotron also opens the text editor in the same way.
76-
create_process("/system/util/open.lua", { argv = { file } })
77-
end
78-
end
57+
local text_area = attach_textarea(
58+
gui,
59+
{
60+
x = 0,
61+
y = 97,
62+
width = width,
63+
height = 103,
64+
is_link = function(text)
65+
return text != nil and find_lua_file_in_text(text) != nil
66+
end,
67+
link_click = function(text)
68+
if text == nil then return end
69+
70+
local file = find_lua_file_in_text(text)
71+
if file != nil then
72+
file = file:gsub(":", "#")
73+
-- TODO if file is already open in text editor then this
74+
-- command does not go to the specific line number.
75+
-- Please note though, that in case of an unhandled error,
76+
-- Picotron also opens the text editor in the same way.
77+
create_process("/system/util/open.lua", { argv = { file } })
78+
end
79+
end
80+
}
81+
)
7982

8083
test_summary = attach_test_summary(
8184
gui,
@@ -92,12 +95,17 @@ local function start_test(item)
9295

9396
lights = attach_lights(
9497
gui,
95-
{ x = 8, y = 115, width = 264, height = 78 }
98+
{
99+
x = 8,
100+
y = 115,
101+
width = 264,
102+
height = 78,
103+
select = function(selected_test)
104+
select_test(selected_test)
105+
test_tree:select_child(selected_test)
106+
end
107+
}
96108
)
97-
function lights:select(selected_test)
98-
select_test(selected_test)
99-
test_tree:select_child(selected_test)
100-
end
101109

102110
test_tree = attach_tree(
103111
gui,

gui/lights.lua

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
-- lights is a gui component showing lights of different color :)
55

6+
---@param el {x:number,y:number,width:number,height:number,select:function}
67
function attach_lights(parent, el)
78
local lights <const> = {}
89
local lights_max = 0
@@ -40,7 +41,7 @@ function attach_lights(parent, el)
4041
function el:click(msg)
4142
local light = light_at_cursor_pointer(msg)
4243
if light != nil then
43-
el:select(light)
44+
el.select(light)
4445
end
4546
end
4647

gui/test_summary.lua

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
-- (c) 2024 Jacek Olszak
22
-- This code is licensed under MIT license (see LICENSE for details)
33

4+
---@param el {x:number,y:number,width:number,height:number}
45
function attach_test_summary(parent, el)
56
local succeeded, failed = 0, 0
67

gui/test_toolbar.lua

+8-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
-- (c) 2024 Jacek Olszak
22
-- This code is licensed under MIT license (see LICENSE for details)
33

4+
---@param el {x:number,y:number,width:number,height:number,start_test:function,stop_test:function,is_running:function}
45
function attach_toolbar(parent, el)
56
local toolbar_color <const> = 6
67
local disabled_color <const> = 13
@@ -14,13 +15,13 @@ function attach_toolbar(parent, el)
1415

1516
local run_btn <const> = el:attach_button { x = 6, y = 4, width = 10 }
1617
function run_btn:click()
17-
if not el:is_running() then
18-
el:start_test()
18+
if not el.is_running() then
19+
el.start_test()
1920
end
2021
end
2122

2223
function run_btn:update()
23-
if el:is_running() then
24+
if el.is_running() then
2425
self.cursor = nil
2526
else
2627
self.cursor = "pointer"
@@ -29,7 +30,7 @@ function attach_toolbar(parent, el)
2930

3031
function run_btn:draw()
3132
local col = disabled_color
32-
if not el:is_running() then
33+
if not el.is_running() then
3334
col = enabled_color
3435
end
3536
pal(icon_color, col)
@@ -39,11 +40,11 @@ function attach_toolbar(parent, el)
3940

4041
local stop_btn <const> = el:attach_button { x = 22, y = 4, width = 10 }
4142
function stop_btn:click()
42-
el:stop_test()
43+
el.stop_test()
4344
end
4445

4546
function stop_btn:update()
46-
if not el:is_running() then
47+
if not el.is_running() then
4748
self.cursor = nil
4849
else
4950
self.cursor = "pointer"
@@ -52,7 +53,7 @@ function attach_toolbar(parent, el)
5253

5354
function stop_btn:draw()
5455
local col = disabled_color
55-
if el:is_running() then
56+
if el.is_running() then
5657
col = enabled_color
5758
end
5859
pal(icon_color, col)

gui/textarea.lua

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
-- (c) 2024 Jacek Olszak
22
-- This code is licensed under MIT license (see LICENSE for details)
33

4+
---@param el {x:number,y:number,width:number,height:number,is_link:function,link_click:function}
45
function attach_textarea(parent, el)
56
local line_height <const> = 10
67

@@ -17,7 +18,7 @@ function attach_textarea(parent, el)
1718
end
1819

1920
local function is_link(text)
20-
return el.is_link != nil and el:is_link(text)
21+
return el.is_link != nil and el.is_link(text)
2122
end
2223

2324
function text_area:update(msg)
@@ -33,7 +34,7 @@ function attach_textarea(parent, el)
3334
function text_area:click(msg)
3435
local text = text_at_mouse_position(msg)
3536
if is_link(text) and el.link_click != nil then
36-
el:link_click(text)
37+
el.link_click(text)
3738
end
3839
end
3940

gui/tree.lua

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
-- (c) 2024 Jacek Olszak
33
-- This code is licensed under MIT license (see LICENSE for details)
44

5+
---@param el {x:number,y:number,width:number,height:number,select:function}
56
function attach_tree(parent_el, el)
67
local bg_color <const> = 7
78
local fg_color <const> = 13

0 commit comments

Comments
 (0)