-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgui.lua
More file actions
164 lines (148 loc) · 5.8 KB
/
gui.lua
File metadata and controls
164 lines (148 loc) · 5.8 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
-- Minimal desktop GUI for DaniX POC
local kernel = require("src.api.kernel")
local term_app = "DaniX.terminal_app"
local desktop = {}
local hasGuiH, GuiH = pcall(require, "GuiH")
local useGuiH = hasGuiH and GuiH
local function draw_box(x, y, w, h, bg, fg, label)
local old_bg = term.getBackgroundColor()
local old_fg = term.getTextColor()
local old_cursor = {term.getCursorPos()}
term.setBackgroundColor(bg)
term.setTextColor(fg)
for yy = y, y + h - 1 do
term.setCursorPos(x, yy)
term.write(string.rep(" ", w))
end
if label then
local label_y = y + math.floor((h - 1) / 2)
local label_x = x + math.floor((w - string.len(label)) / 2)
term.setCursorPos(label_x, label_y)
term.write(label)
end
term.setBackgroundColor(old_bg)
term.setTextColor(old_fg)
term.setCursorPos(old_cursor[1], old_cursor[2])
end
local function term_draw(username)
local w, h = term.getSize()
term.setBackgroundColor(colors.blue)
term.clear()
for y = 1, h do
term.setCursorPos(1, y)
term.write(string.rep(" ", w))
end
term.setBackgroundColor(colors.black)
term.setTextColor(colors.white)
term.setCursorPos(2,2)
term.write("DaniX")
term.setCursorPos(2,3)
term.write("User: " .. tostring(username))
-- Draw Terminal icon box (place relative to size)
local icon_w, icon_h = 16, 7
local w2, h2 = term.getSize()
local icon_x = math.max(4, math.floor(w2 / 6))
local icon_y = math.max(6, math.floor(h2 / 4))
draw_box(icon_x, icon_y, icon_w, icon_h, colors.lightGray, colors.black, "Terminal")
end
local function draw_desktop(username)
if useGuiH then
local ok, gui = pcall(function() return GuiH.new(term.current()) end)
if ok and gui then
if type(gui.clear) == "function" then pcall(function() gui:clear() end) end
if type(gui.print) == "function" then
pcall(function()
gui:print(2,2, "DaniX")
gui:print(2,3, "User: " .. tostring(username))
end)
-- fallback drawing of icon using term if gui doesn't support buttons
term_draw(username)
return
end
end
end
-- Fallback to terminal drawing
term_draw(username)
end
function desktop.start(username, isAdmin)
draw_desktop(username)
local function run_desktop(username)
-- Try Basalt first
local ok, basalt = pcall(require, "basalt")
if ok and basalt then
local main = basalt.getMainFrame()
if main then
if main.removeChildren then
pcall(function() main:removeChildren() end)
elseif main.clear then
pcall(function() main:clear() end)
end
if main.setBackground then pcall(function() main:setBackground(colors.black) end) end
end
local top = main:addPanel()
:setPosition(1,1)
:setSize(main:getSize())
local title = top:addLabel()
:setPosition(2,1)
:setText("DaniX Desktop - user: " .. tostring(username))
local termBtn = top:addButton()
:setPosition(2,3)
:setText("Terminal")
:onClick(function()
local ok, res = pcall(function()
return require("DaniX.terminal_app").open(username)
end)
if not ok then
-- show transient message
local msg = top:addLabel():setPosition(2,5):setText("Terminal failed: " .. tostring(res))
os.sleep(2)
msg:remove()
end
end)
local logout = top:addButton()
:setPosition(12,3)
:setText("Logout")
:onClick(function()
basalt.stop()
end)
basalt.run()
return
end
-- Fallback text-mode desktop
term.setBackgroundColor(colors.blue)
term.clear()
term.setTextColor(colors.white)
term.setCursorPos(1,1)
term.write("DaniX Desktop - user: " .. tostring(username))
term.setCursorPos(1,3)
term.write("Press T to open Terminal, L to logout")
while true do
local ev = { os.pullEventRaw() }
if ev[1] == "char" then
local ch = ev[2]
if ch == "t" or ch == "T" then
-- spawn terminal app
local ok, res = pcall(function()
return require("DaniX.terminal_app").open(username)
end)
if not ok then
-- show on-screen error
term.setCursorPos(1,5)
term.setTextColor(colors.red)
term.write("Terminal failed: " .. tostring(res))
term.setTextColor(colors.white)
end
elseif ch == "l" or ch == "L" then
-- logout
return
end
elseif ev[1] == "mouse_click" then
-- placeholder for clicking desktop icons later
elseif ev[1] == "terminate" then
error("terminate")
end
end
end
run_desktop(username)
end
return desktop