Commands refactor (#257)

* remove unecessary check

* move command registration to core/commands
This commit is contained in:
Takase 2021-06-08 05:00:29 +08:00 committed by GitHub
parent 130b29438a
commit 0fd1fa8872
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 36 additions and 40 deletions

View File

@ -1,13 +1,7 @@
local core = require "core"
local command = require "core.command"
local CommandView = require "core.commandview"
local function has_commandview()
return core.active_view:is(CommandView)
end
command.add(has_commandview, {
command.add("core.commandview", {
["command:submit"] = function()
core.active_view:submit()
end,

View File

@ -0,0 +1,34 @@
local core = require "core"
local command = require "core.command"
command.add("core.nagview", {
["dialog:previous-entry"] = function()
local v = core.active_view
local hover = v.hovered_item or 1
v:change_hovered(hover == 1 and #v.options or hover - 1)
end,
["dialog:next-entry"] = function()
local v = core.active_view
local hover = v.hovered_item or 1
v:change_hovered(hover == #v.options and 1 or hover + 1)
end,
["dialog:select-yes"] = function()
local v = core.active_view
if v ~= core.nag_view then return end
v:change_hovered(findindex(v.options, "default_yes"))
command.perform "dialog:select"
end,
["dialog:select-no"] = function()
local v = core.active_view
if v ~= core.nag_view then return end
v:change_hovered(findindex(v.options, "default_no"))
command.perform "dialog:select"
end,
["dialog:select"] = function()
local v = core.active_view
if v.hovered_item then
v.on_selected(v.options[v.hovered_item])
v:next()
end
end
})

View File

@ -212,36 +212,4 @@ function NagView:show(title, message, options, on_select)
if #self.queue > 0 and not self.title then self:next() end
end
command.add(NagView, {
["dialog:previous-entry"] = function()
local v = core.active_view
local hover = v.hovered_item or 1
v:change_hovered(hover == 1 and #v.options or hover - 1)
end,
["dialog:next-entry"] = function()
local v = core.active_view
local hover = v.hovered_item or 1
v:change_hovered(hover == #v.options and 1 or hover + 1)
end,
["dialog:select-yes"] = function()
local v = core.active_view
if v ~= core.nag_view then return end
v:change_hovered(findindex(v.options, "default_yes"))
command.perform "dialog:select"
end,
["dialog:select-no"] = function()
local v = core.active_view
if v ~= core.nag_view then return end
v:change_hovered(findindex(v.options, "default_no"))
command.perform "dialog:select"
end,
["dialog:select"] = function()
local v = core.active_view
if v.hovered_item then
v.on_selected(v.options[v.hovered_item])
v:next()
end
end,
})
return NagView
return NagView