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