Skip to content

Commit d0b6ac2

Browse files
committed
Avoid more nyis
1 parent 0eeed81 commit d0b6ac2

File tree

3 files changed

+60
-44
lines changed

3 files changed

+60
-44
lines changed

src/plugin/layout.lua

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,14 @@ function Layout:refreshView(window)
7676
return ctx.computedLayout
7777
end
7878

79+
local function hasMouseUp(e) ---@param e Element
80+
return e.onmouseup ~= nil
81+
end
82+
83+
local function hasMouseDownOrClick(e) ---@param e Element
84+
return e.onmousedown ~= nil or e.onclick ~= nil
85+
end
86+
7987
---@param event Event
8088
---@param handler EventHandler
8189
---@return Message?
@@ -90,7 +98,7 @@ function Layout:event(event, handler)
9098

9199
local anyWithMouseDown = false
92100
for el, _ in pairs(hoveredElements) do
93-
if el.onmousedown then
101+
if el.onmousedown or el.onclick then
94102
anyWithMouseDown = true
95103
break
96104
end
@@ -111,20 +119,21 @@ function Layout:event(event, handler)
111119
end
112120
elseif event.name == "mousePress" then
113121
local ctx = self.contexts[event.window]
114-
local info = findElementAtPosition(ctx.ui, ctx.computedLayout, event.x, event.y, 0, 0, function(el)
115-
return el.onmousedown ~= nil
116-
end)
122+
local info = findElementAtPosition(ctx.ui, ctx.computedLayout, event.x, event.y, 0, 0, hasMouseDownOrClick)
117123

118124
if info then
125+
if info.element.onclick then
126+
return info.element.onclick
127+
end
128+
119129
local relX = event.x - info.absX
120130
local relY = event.y - info.absY
131+
121132
return info.element.onmousedown(relX, relY, info.layout.width, info.layout.height)
122133
end
123134
elseif event.name == "mouseRelease" then
124135
local ctx = self.contexts[event.window]
125-
local info = findElementAtPosition(ctx.ui, ctx.computedLayout, event.x, event.y, 0, 0, function(el)
126-
return el.onmouseup ~= nil
127-
end)
136+
local info = findElementAtPosition(ctx.ui, ctx.computedLayout, event.x, event.y, 0, 0, hasMouseUp)
128137

129138
if info then
130139
local relX = event.x - info.absX

src/plugin/ui.lua

Lines changed: 43 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,41 @@ local function convertZ(z)
2020
return 1 - math.min(z or 0, 100000) / 1000000
2121
end
2222

23+
---@param z number
24+
---@param windowWidth number
25+
---@param windowHeight number
26+
---@param vertices number[]
27+
---@param indices number[]
28+
local function addBorderQuad(bx, by, bw, bh, color, z, windowWidth, windowHeight, vertices, indices)
29+
if bw <= 0 or bh <= 0 then
30+
return
31+
end
32+
33+
local baseIdx = #vertices / 10
34+
local left = toNDC(bx, windowWidth)
35+
local right = toNDC(bx + bw, windowWidth)
36+
local top = -toNDC(by, windowHeight)
37+
local bottom = -toNDC(by + bh, windowHeight)
38+
39+
-- stylua: ignore
40+
for _, v in ipairs({
41+
left, top, convertZ(z + 1), color.r, color.g, color.b, color.a, 0, 0, 0,
42+
right, top, convertZ(z + 1), color.r, color.g, color.b, color.a, 0, 0, 0,
43+
right, bottom, convertZ(z + 1), color.r, color.g, color.b, color.a, 0, 0, 0,
44+
left, bottom, convertZ(z + 1), color.r, color.g, color.b, color.a, 0, 0, 0,
45+
}) do
46+
table.insert(vertices, v)
47+
end
48+
49+
-- stylua: ignore
50+
for _, idx in ipairs({
51+
baseIdx, baseIdx + 1, baseIdx + 2,
52+
baseIdx, baseIdx + 2, baseIdx + 3,
53+
}) do
54+
table.insert(indices, idx)
55+
end
56+
end
57+
2358
---@param layout ComputedLayout
2459
local function generateLayoutQuads(layout, parentX, parentY, vertices, indices, windowWidth, windowHeight)
2560
local x = (parentX or 0) + (layout.x or 0)
@@ -74,54 +109,28 @@ local function generateLayoutQuads(layout, parentX, parentY, vertices, indices,
74109
local borderLeft = layout.border.left
75110
local borderRight = layout.border.right
76111

77-
local function addBorderQuad(bx, by, bw, bh, color)
78-
if bw <= 0 or bh <= 0 then
79-
return
80-
end
81-
82-
local baseIdx = #vertices / 10
83-
local left = toNDC(bx, windowWidth)
84-
local right = toNDC(bx + bw, windowWidth)
85-
local top = -toNDC(by, windowHeight)
86-
local bottom = -toNDC(by + bh, windowHeight)
87-
88-
-- stylua: ignore
89-
for _, v in ipairs({
90-
left, top, convertZ(z + 1), color.r, color.g, color.b, color.a, 0, 0, 0,
91-
right, top, convertZ(z + 1), color.r, color.g, color.b, color.a, 0, 0, 0,
92-
right, bottom, convertZ(z + 1), color.r, color.g, color.b, color.a, 0, 0, 0,
93-
left, bottom, convertZ(z + 1), color.r, color.g, color.b, color.a, 0, 0, 0,
94-
}) do
95-
table.insert(vertices, v)
96-
end
97-
98-
-- stylua: ignore
99-
for _, idx in ipairs({
100-
baseIdx, baseIdx + 1, baseIdx + 2,
101-
baseIdx, baseIdx + 2, baseIdx + 3,
102-
}) do
103-
table.insert(indices, idx)
104-
end
105-
end
106-
107112
-- Top border
108113
if borderTop and borderTop.width and borderTop.width > 0 and borderTop.style ~= "none" then
109-
addBorderQuad(x, y, width, borderTop.width, borderTop.color)
114+
addBorderQuad(x, y, width, borderTop.width, borderTop.color, z, windowWidth, windowHeight, vertices, indices)
110115
end
111116

112117
-- Bottom border
113118
if borderBottom and borderBottom.width and borderBottom.width > 0 and borderBottom.style ~= "none" then
114-
addBorderQuad(x, y + height - borderBottom.width, width, borderBottom.width, borderBottom.color)
119+
addBorderQuad(x, y + height - borderBottom.width, width, z, borderBottom.width, borderBottom.color,
120+
windowWidth,
121+
windowHeight, vertices, indices)
115122
end
116123

117124
-- Left border
118125
if borderLeft and borderLeft.width and borderLeft.width > 0 and borderLeft.style ~= "none" then
119-
addBorderQuad(x, y, borderLeft.width, height, borderLeft.color)
126+
addBorderQuad(x, y, borderLeft.width, height, borderLeft.color, z, windowWidth, windowHeight, vertices,
127+
indices)
120128
end
121129

122130
-- Right border
123131
if borderRight and borderRight.width and borderRight.width > 0 and borderRight.style ~= "none" then
124-
addBorderQuad(x + width - borderRight.width, y, borderRight.width, height, borderRight.color)
132+
addBorderQuad(x + width - borderRight.width, y, borderRight.width, height, borderRight.color, z, windowWidth,
133+
windowHeight, vertices, indices)
125134
end
126135
end
127136

src/ui/element.lua

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,7 @@ end
7373
---@generic T
7474
---@param message T
7575
function Element:onClick(message)
76-
self.onmousedown = function()
77-
return message
78-
end
76+
self.onclick = message
7977
return self
8078
end
8179

0 commit comments

Comments
 (0)