Skip to content

Commit 7871234

Browse files
committed
Refactor documentation and examples for Display, Frame, Program, SideNav, TabControl, and Tree elements
1 parent 69a0254 commit 7871234

6 files changed

Lines changed: 247 additions & 13 deletions

File tree

src/elements/Display.lua

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,7 @@ local colorHex = require("libraries/colorHex")
66
---@configDescription The Display is a special element which uses the CC Window API which you can use.
77
---@configDefault false
88

9-
--- A specialized element that provides direct access to ComputerCraft's Window API.
10-
--- It acts as a canvas where you can use standard CC terminal operations, making it ideal for:
11-
--- - Integration with existing CC programs and APIs
12-
--- - Custom drawing operations
13-
--- - Terminal emulation
14-
--- - Complex text manipulation
15-
--- The Display maintains its own terminal buffer and can be manipulated using familiar CC terminal methods.
9+
--- A specialized element that provides direct access to ComputerCraft's Window API. It acts as a canvas where you can use standard CC terminal operations.
1610
--- @usage [[
1711
--- -- Create a display for a custom terminal
1812
--- local display = main:addDisplay()

src/elements/Frame.lua

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,18 +117,15 @@ end
117117
--- @return boolean handled Whether the event was handled
118118
--- @protected
119119
function Frame:mouse_drag(button, x, y)
120-
if self:hasState("clicked") and self.dragging then
120+
if self.dragging then
121121
local newX = x - self.dragStartX
122122
local newY = y - self.dragStartY
123123

124124
self.set("x", newX)
125125
self.set("y", newY)
126126
return true
127127
end
128-
if not self.dragging then
129-
return Container.mouse_drag(self, button, x, y)
130-
end
131-
return false
128+
return Container.mouse_drag(self, button, x, y)
132129
end
133130

134131
--- @shortDescription Calculates the total height of all children elements

src/elements/Program.lua

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,60 @@ local errorManager = require("errorManager")
66

77
--- This is the program class. It provides a program that runs in a window.
88
---@class Program : VisualElement
9+
---@run [[
10+
--- local basalt = require("basalt")
11+
---
12+
--- local main = basalt.getMainFrame()
13+
---
14+
--- local execPath = "rom/programs/fun/worm.lua"
15+
---
16+
--- local btn = main:addButton({
17+
--- x = 5,
18+
--- y = 2,
19+
--- width = 20,
20+
--- height = 3,
21+
--- text = "Run Worm",
22+
--- }):onClick(function()
23+
--- local frame = main:addFrame({
24+
--- x = 2,
25+
--- y = 2,
26+
--- width = 28,
27+
--- height = 10,
28+
--- title = "Worm Program",
29+
--- draggable = true,
30+
--- })
31+
--- :setDraggingMap({{x=1, y=1, width=27, height=1}})
32+
--- :onFocus(function(self)
33+
--- self:prioritize()
34+
--- end)
35+
--- local program = frame:addProgram({
36+
--- x = 1,
37+
--- y = 2,
38+
--- width = 28,
39+
--- height = 9,
40+
--- })
41+
--- program:execute(execPath)
42+
--- frame:addLabel({
43+
--- x = 2,
44+
--- y = 1,
45+
--- text = "Worm",
46+
--- foreground = colors.lightBlue
47+
--- })
48+
--- frame:addButton({
49+
--- x = frame.get("width"),
50+
--- y = 1,
51+
--- width = 1,
52+
--- height = 1,
53+
--- text = "X",
54+
--- background = colors.red,
55+
--- foreground = colors.white
56+
--- }):onClick(function()
57+
--- frame:destroy()
58+
--- end)
59+
--- end)
60+
---
61+
--- basalt.run()
62+
--- ]]
963
local Program = setmetatable({}, VisualElement)
1064
Program.__index = Program
1165

src/elements/SideNav.lua

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,100 @@ local tHex = require("libraries/colorHex")
55
---@configDescription A SideNav element that provides sidebar navigation with multiple content areas.
66

77
--- The SideNav is a container that provides sidebar navigation functionality
8+
--- @run [[
9+
--- local basalt = require("basalt")
10+
--- local main = basalt.getMainFrame()
11+
---
12+
--- -- Create a simple SideNav
13+
--- local sideNav = main:addSideNav({
14+
--- x = 1,
15+
--- y = 1,
16+
--- sidebarWidth = 12,
17+
--- width = 48
18+
--- })
19+
---
20+
--- -- Tab 1: Home
21+
--- local homeTab = sideNav:newTab("Home")
22+
---
23+
--- homeTab:addLabel({
24+
--- x = 2,
25+
--- y = 2,
26+
--- text = "Welcome!",
27+
--- foreground = colors.yellow
28+
--- })
29+
---
30+
--- homeTab:addLabel({
31+
--- x = 2,
32+
--- y = 4,
33+
--- text = "This is a simple",
34+
--- foreground = colors.white
35+
--- })
36+
---
37+
--- homeTab:addLabel({
38+
--- x = 2,
39+
--- y = 5,
40+
--- text = "SideNav example.",
41+
--- foreground = colors.white
42+
--- })
43+
---
44+
--- -- Tab 2: Counter
45+
--- local counterTab = sideNav:newTab("Counter")
46+
---
47+
--- local counterLabel = counterTab:addLabel({
48+
--- x = 2,
49+
--- y = 2,
50+
--- text = "Count: 0",
51+
--- foreground = colors.lime
52+
--- })
53+
---
54+
--- local count = 0
55+
--- counterTab:addButton({
56+
--- x = 2,
57+
--- y = 4,
58+
--- width = 12,
59+
--- height = 3,
60+
--- text = "Click Me",
61+
--- background = colors.blue
62+
--- })
63+
--- :setBackgroundState("clicked", colors.lightBlue)
64+
--- :onClick(function()
65+
--- count = count + 1
66+
--- counterLabel:setText("Count: " .. count)
67+
--- end)
68+
---
69+
--- -- Tab 3: Info
70+
--- local infoTab = sideNav:newTab("Info")
71+
---
72+
--- infoTab:addLabel({
73+
--- x = 2,
74+
--- y = 2,
75+
--- text = "SideNav Features:",
76+
--- foreground = colors.orange
77+
--- })
78+
---
79+
--- infoTab:addLabel({
80+
--- x = 2,
81+
--- y = 4,
82+
--- text = "- Multiple tabs",
83+
--- foreground = colors.gray
84+
--- })
85+
---
86+
--- infoTab:addLabel({
87+
--- x = 2,
88+
--- y = 5,
89+
--- text = "- Easy navigation",
90+
--- foreground = colors.gray
91+
--- })
92+
---
93+
--- infoTab:addLabel({
94+
--- x = 2,
95+
--- y = 6,
96+
--- text = "- Content per tab",
97+
--- foreground = colors.gray
98+
--- })
99+
---
100+
--- basalt.run()
101+
--- ]]
8102
---@class SideNav : Container
9103
local SideNav = setmetatable({}, Container)
10104
SideNav.__index = SideNav

src/elements/TabControl.lua

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,101 @@ local log = require("log")
66
---@configDescription A TabControl element that provides tabbed interface with multiple content areas.
77

88
--- The TabControl is a container that provides tabbed interface functionality
9+
--- @run [[
10+
--- local basalt = require("basalt")
11+
12+
--- local main = basalt.getMainFrame()
13+
---
14+
--- -- Create a simple TabControl
15+
--- local tabControl = main:addTabControl({
16+
--- x = 2,
17+
--- y = 2,
18+
--- width = 46,
19+
--- height = 15,
20+
--- })
21+
---
22+
--- -- Tab 1: Home
23+
--- local homeTab = tabControl:newTab("Home")
24+
---
25+
--- homeTab:addLabel({
26+
--- x = 2,
27+
--- y = 2,
28+
--- text = "Welcome!",
29+
--- foreground = colors.yellow
30+
--- })
31+
---
32+
--- homeTab:addLabel({
33+
--- x = 2,
34+
--- y = 4,
35+
--- text = "This is a TabControl",
36+
--- foreground = colors.white
37+
--- })
38+
---
39+
--- homeTab:addLabel({
40+
--- x = 2,
41+
--- y = 5,
42+
--- text = "example with tabs.",
43+
--- foreground = colors.white
44+
--- })
45+
---
46+
--- -- Tab 2: Counter
47+
--- local counterTab = tabControl:newTab("Counter")
48+
---
49+
--- local counterLabel = counterTab:addLabel({
50+
--- x = 2,
51+
--- y = 2,
52+
--- text = "Count: 0",
53+
--- foreground = colors.lime
54+
--- })
55+
---
56+
--- local count = 0
57+
--- counterTab:addButton({
58+
--- x = 2,
59+
--- y = 4,
60+
--- width = 12,
61+
--- height = 3,
62+
--- text = "Click Me",
63+
--- background = colors.blue
64+
--- })
65+
--- :setBackgroundState("clicked", colors.lightBlue)
66+
--- :onClick(function()
67+
--- count = count + 1
68+
--- counterLabel:setText("Count: " .. count)
69+
--- end)
70+
---
71+
--- -- Tab 3: Info
72+
--- local infoTab = tabControl:newTab("Info")
73+
---
74+
--- infoTab:addLabel({
75+
--- x = 2,
76+
--- y = 2,
77+
--- text = "TabControl Features:",
78+
--- foreground = colors.orange
79+
--- })
80+
---
81+
--- infoTab:addLabel({
82+
--- x = 2,
83+
--- y = 4,
84+
--- text = "- Horizontal tabs",
85+
--- foreground = colors.gray
86+
--- })
87+
---
88+
--- infoTab:addLabel({
89+
--- x = 2,
90+
--- y = 5,
91+
--- text = "- Easy navigation",
92+
--- foreground = colors.gray
93+
--- })
94+
---
95+
--- infoTab:addLabel({
96+
--- x = 2,
97+
--- y = 6,
98+
--- text = "- Content per tab",
99+
--- foreground = colors.gray
100+
--- })
101+
---
102+
--- basalt.run()
103+
--- ]]
9104
---@class TabControl : Container
10105
local TabControl = setmetatable({}, Container)
11106
TabControl.__index = TabControl

src/elements/Tree.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ end
1818

1919
--- This is the tree class. It provides a hierarchical view of nodes that can be expanded and collapsed, with support for selection and scrolling.
2020
--- @run [[
21-
--- local basaltg = require("basalt")
21+
--- local basalt = require("basalt")
2222
--- local main = basalt.getMainFrame()
2323
---
2424
--- local fileTree = main:addTree()

0 commit comments

Comments
 (0)