fix(client/context): reset focus

Should resolve #170 but ceebs testing
This commit is contained in:
Linden
2022-12-13 08:37:56 +11:00
parent da9b55fa84
commit b50c0b7df8

View File

@@ -1,5 +1,6 @@
local contextMenus = {}
local openContextMenu = nil
local keepInput = IsNuiFocusKeepingInput()
---@class ContextMenuItem
---@field title? string
@@ -27,11 +28,19 @@ local openContextMenu = nil
---@field canClose? boolean
---@field options { [string]: ContextMenuItem } | ContextMenuArrayItem[]
local function resetFocus()
SetNuiFocus(false, false)
SetNuiFocusKeepInput(keepInput)
end
local function closeContext(_, cb, onExit)
if cb then cb(1) end
if (cb or onExit) and contextMenus[openContextMenu].onExit then contextMenus[openContextMenu].onExit() end
SetNuiFocus(false, false)
resetFocus()
if not cb then SendNUIMessage({ action = 'hideContext' }) end
openContextMenu = nil
end
@@ -40,7 +49,11 @@ function lib.showContext(id)
if not contextMenus[id] then error('No context menu of such id found.') end
local data = contextMenus[id]
openContextMenu = id
keepInput = IsNuiFocusKeepingInput()
SetNuiFocus(true, true)
SetNuiFocusKeepInput(false)
SendNuiMessage(json.encode({
action = 'showContext',
data = {
@@ -78,21 +91,31 @@ end)
RegisterNUICallback('clickContext', function(id, cb)
cb(1)
if math.type(tonumber(id)) == 'float' then
id = math.tointeger(id)
elseif tonumber(id) then
id += 1
end
local data = contextMenus[openContextMenu].options[id]
if not data.event and not data.serverEvent and not data.onSelect then return end
openContextMenu = nil
SetNuiFocus(false, false)
resetFocus()
if data.onSelect then data.onSelect(data.args) end
if data.event then TriggerEvent(data.event, data.args) end
if data.serverEvent then TriggerServerEvent(data.serverEvent, data.args) end
SendNUIMessage({
action = 'hideContext'
})
end)
RegisterNUICallback('closeContext', closeContext)
RegisterNUICallback('closeContext', closeContext)