-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathbutton.lua
More file actions
95 lines (77 loc) · 2.05 KB
/
button.lua
File metadata and controls
95 lines (77 loc) · 2.05 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
local Paragraph = require("nui-components.paragraph")
local event = require("nui.utils.autocmd").event
local fn = require("nui-components.utils.fn")
---@class NuiComponents.Button: NuiComponents.Paragraph
---@overload fun(_, props: table<string, any>, popup_options: NuiPopupOptions): NuiComponents.Button
---@field super NuiComponents.Paragraph
local Button = Paragraph:extend("Button")
function Button:init(props, popup_options)
local lines = fn.default_to(props.label, "")
props.label = nil
Button.super.init(
self,
fn.merge({
on_press = fn.ignore,
press_key = { "<CR>", "<Space>" },
lines = lines,
is_active = false,
truncate = true,
}, props),
popup_options
)
end
function Button:prop_types()
return fn.merge(Button.super.prop_types(self), {
on_press = "function",
press_key = { "table", "string" },
is_active = { "boolean", "nil" },
global_press_key = { "table", "string", "nil" },
})
end
function Button:mappings()
local props = self:get_props()
local on_press = function()
props.on_press(self)
end
local mappings = {
{ mode = { "n" }, key = props.press_key, handler = on_press },
}
if props.global_press_key then
table.insert(mappings, {
global = true,
mode = { "n", "i", "v" },
key = props.global_press_key,
handler = on_press,
})
end
return mappings
end
function Button:events()
local handler = vim.schedule_wrap(function()
self:if_mounted(function()
self:on_update()
end)
end)
return {
{
event = event.BufEnter,
handler = handler,
},
{
event = event.BufLeave,
handler = handler,
},
}
end
function Button:get_lines()
local props = self:get_props()
local lines = Button.super.get_lines(self)
if props.prepare_lines then
return props.prepare_lines(lines, self)
end
local is_focused = self:is_focused()
local hl_group = self:hl_group(props.is_active and "Active" or (is_focused and "Focused" or ""))
self:set_hl_group(lines, hl_group)
return lines
end
return Button