From b50c0b7df863a3d98b7586e07d7dcd18e16a9abb Mon Sep 17 00:00:00 2001 From: Linden <65407488+thelindat@users.noreply.github.com> Date: Tue, 13 Dec 2022 08:37:56 +1100 Subject: [PATCH] fix(client/context): reset focus Should resolve #170 but ceebs testing --- resource/interface/client/context.lua | 29 ++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/resource/interface/client/context.lua b/resource/interface/client/context.lua index db87649..2115efe 100644 --- a/resource/interface/client/context.lua +++ b/resource/interface/client/context.lua @@ -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) \ No newline at end of file +RegisterNUICallback('closeContext', closeContext) + + +