From 780371f64427326b0465b3d8387517ce6edb9623 Mon Sep 17 00:00:00 2001 From: Luke Date: Sat, 4 Jun 2022 14:42:35 +0200 Subject: [PATCH] feat(interface/context): onExit function Allows defining a function in the menu that must be called onExit which is going to execute every time the user exits the context menu with ESC --- resource/interface/client/context.lua | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/resource/interface/client/context.lua b/resource/interface/client/context.lua index 1de0d9a..d7ed9c0 100644 --- a/resource/interface/client/context.lua +++ b/resource/interface/client/context.lua @@ -1,6 +1,14 @@ local contextMenus = {} local openContextMenu = nil +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) + if not cb then SendNUIMessage({action = 'hideContext'}) end + openContextMenu = nil +end + function lib.showContext(id) if not contextMenus[id] then return error('No context menu of such id found.') end local data = contextMenus[id] @@ -29,11 +37,7 @@ end function lib.getOpenContextMenu() return openContextMenu end -function lib.hideContext() - SetNuiFocus(false, false) - SendNUIMessage({action = 'hideContext'}) - openContextMenu = nil -end +function lib.hideContext(onExit) return closeContext(nil, nil, onExit) end RegisterNUICallback('openContext', function(id, cb) cb(1) @@ -58,8 +62,4 @@ RegisterNUICallback('clickContext', function(id, cb) }) end) -RegisterNUICallback('closeContext', function(_, cb) - cb(1) - SetNuiFocus(false, false) - openContextMenu = nil -end) +RegisterNUICallback('closeContext', closeContext)