refactor(client/interface): nui focus tweaks

Add helper functions to set nui focus and restore previous input state.
Ensure focus is reset before triggering responses (promises/callbacks).
This commit is contained in:
Linden
2023-03-01 07:25:28 +11:00
parent 284b563483
commit 3dd996c38b
4 changed files with 33 additions and 40 deletions

View File

@@ -1,10 +1,4 @@
local alert = nil local alert = nil
local keepInput = IsNuiFocusKeepingInput()
local function resetFocus()
SetNuiFocus(false, false)
SetNuiFocusKeepInput(keepInput)
end
---@class AlertDialogProps ---@class AlertDialogProps
---@field header string; ---@field header string;
@@ -19,10 +13,9 @@ end
---@return 'cancel' | 'confirm' | nil ---@return 'cancel' | 'confirm' | nil
function lib.alertDialog(data) function lib.alertDialog(data)
if alert then return end if alert then return end
alert = promise.new() alert = promise.new()
lib.setNuiFocus(false)
keepInput = IsNuiFocusKeepingInput()
SetNuiFocus(true, true) SetNuiFocus(true, true)
SetNuiFocusKeepInput(false) SetNuiFocusKeepInput(false)
SendNUIMessage({ SendNUIMessage({
@@ -35,20 +28,24 @@ end
function lib.closeAlertDialog() function lib.closeAlertDialog()
if not alert then return end if not alert then return end
alert:resolve(nil)
alert = nil lib.resetNuiFocus()
resetFocus()
SendNUIMessage({ SendNUIMessage({
action = 'closeAlertDialog' action = 'closeAlertDialog'
}) })
alert:resolve(nil)
alert = nil
end end
RegisterNUICallback('closeAlert', function(data, cb) RegisterNUICallback('closeAlert', function(data, cb)
cb(1) cb(1)
resetFocus() lib.resetNuiFocus()
local promise = alert local promise = alert
alert = nil alert = nil
promise:resolve(data) promise:resolve(data)
end) end)

View File

@@ -1,6 +1,5 @@
local contextMenus = {} local contextMenus = {}
local openContextMenu = nil local openContextMenu = nil
local keepInput = IsNuiFocusKeepingInput()
---@class ContextMenuItem ---@class ContextMenuItem
---@field title? string ---@field title? string
@@ -28,16 +27,12 @@ local keepInput = IsNuiFocusKeepingInput()
---@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
resetFocus() lib.resetNuiFocus()
if (cb or onExit) and contextMenus[openContextMenu].onExit then contextMenus[openContextMenu].onExit() end
if not cb then SendNUIMessage({ action = 'hideContext' }) end if not cb then SendNUIMessage({ action = 'hideContext' }) end
@@ -47,12 +42,11 @@ end
---@param id string ---@param id string
function lib.showContext(id) 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) lib.setNuiFocus(false)
SetNuiFocusKeepInput(false)
SendNuiMessage(json.encode({ SendNuiMessage(json.encode({
action = 'showContext', action = 'showContext',
@@ -104,7 +98,7 @@ RegisterNUICallback('clickContext', function(id, cb)
openContextMenu = nil openContextMenu = nil
resetFocus() lib.resetNuiFocus()
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

View File

@@ -0,0 +1,12 @@
local keepInput = IsNuiFocusKeepingInput()
function lib.setNuiFocus(allowInput, disableCursor)
keepInput = IsNuiFocusKeepingInput()
SetNuiFocus(true, not disableCursor)
SetNuiFocusKeepInput(allowInput)
end
function lib.resetNuiFocus()
SetNuiFocus(false, false)
SetNuiFocusKeepInput(keepInput)
end

View File

@@ -2,7 +2,6 @@
local registeredMenus = {} local registeredMenus = {}
---@type MenuProps | nil ---@type MenuProps | nil
local openMenu local openMenu
local keepInput = IsNuiFocusKeepingInput()
---@alias MenuPosition 'top-left' | 'top-right' | 'bottom-left' | 'bottom-right' ---@alias MenuPosition 'top-left' | 'top-right' | 'bottom-left' | 'bottom-right'
---@alias MenuChangeFunction fun(selected: number, scrollIndex?: number, args?: any, checked?: boolean) ---@alias MenuChangeFunction fun(selected: number, scrollIndex?: number, args?: any, checked?: boolean)
@@ -65,13 +64,9 @@ function lib.showMenu(id, startIndex)
end end
openMenu = menu openMenu = menu
keepInput = IsNuiFocusKeepingInput()
if not menu.disableInput then lib.setNuiFocus(not menu.disableInput, false)
SetNuiFocusKeepInput(true)
end
SetNuiFocus(true, false)
SendNUIMessage({ SendNUIMessage({
action = 'setMenu', action = 'setMenu',
data = { data = {
@@ -84,24 +79,19 @@ function lib.showMenu(id, startIndex)
}) })
end end
local function resetFocus()
SetNuiFocus(false, false)
SetNuiFocusKeepInput(keepInput)
end
---@param onExit boolean? ---@param onExit boolean?
function lib.hideMenu(onExit) function lib.hideMenu(onExit)
local menu = openMenu local menu = openMenu
openMenu = nil
if not menu then return end if not menu then return end
openMenu = nil lib.resetNuiFocus()
if onExit and menu.onClose then if onExit and menu.onClose then
menu.onClose() menu.onClose()
end end
resetFocus()
SendNUIMessage({ SendNUIMessage({
action = 'closeMenu' action = 'closeMenu'
}) })
@@ -135,7 +125,7 @@ RegisterNUICallback('confirmSelected', function(data, cb)
if not menu then return end if not menu then return end
if menu.options[data[1]].close ~= false then if menu.options[data[1]].close ~= false then
resetFocus() lib.resetNuiFocus()
openMenu = nil openMenu = nil
end end
@@ -191,7 +181,7 @@ end)
RegisterNUICallback('closeMenu', function(data, cb) RegisterNUICallback('closeMenu', function(data, cb)
cb(1) cb(1)
resetFocus() lib.resetNuiFocus()
local menu = openMenu local menu = openMenu