mirror of
https://github.com/CommunityOx/ox_lib.git
synced 2026-08-17 06:56:03 +01:00
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:
@@ -1,10 +1,4 @@
|
||||
local alert = nil
|
||||
local keepInput = IsNuiFocusKeepingInput()
|
||||
|
||||
local function resetFocus()
|
||||
SetNuiFocus(false, false)
|
||||
SetNuiFocusKeepInput(keepInput)
|
||||
end
|
||||
|
||||
---@class AlertDialogProps
|
||||
---@field header string;
|
||||
@@ -19,10 +13,9 @@ end
|
||||
---@return 'cancel' | 'confirm' | nil
|
||||
function lib.alertDialog(data)
|
||||
if alert then return end
|
||||
|
||||
alert = promise.new()
|
||||
|
||||
keepInput = IsNuiFocusKeepingInput()
|
||||
|
||||
lib.setNuiFocus(false)
|
||||
SetNuiFocus(true, true)
|
||||
SetNuiFocusKeepInput(false)
|
||||
SendNUIMessage({
|
||||
@@ -35,20 +28,24 @@ end
|
||||
|
||||
function lib.closeAlertDialog()
|
||||
if not alert then return end
|
||||
alert:resolve(nil)
|
||||
alert = nil
|
||||
resetFocus()
|
||||
|
||||
lib.resetNuiFocus()
|
||||
SendNUIMessage({
|
||||
action = 'closeAlertDialog'
|
||||
})
|
||||
alert:resolve(nil)
|
||||
|
||||
alert = nil
|
||||
end
|
||||
|
||||
|
||||
RegisterNUICallback('closeAlert', function(data, cb)
|
||||
cb(1)
|
||||
resetFocus()
|
||||
lib.resetNuiFocus()
|
||||
|
||||
local promise = alert
|
||||
alert = nil
|
||||
|
||||
promise:resolve(data)
|
||||
end)
|
||||
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
local contextMenus = {}
|
||||
local openContextMenu = nil
|
||||
local keepInput = IsNuiFocusKeepingInput()
|
||||
|
||||
---@class ContextMenuItem
|
||||
---@field title? string
|
||||
@@ -28,16 +27,12 @@ local keepInput = IsNuiFocusKeepingInput()
|
||||
---@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
|
||||
|
||||
resetFocus()
|
||||
lib.resetNuiFocus()
|
||||
|
||||
if (cb or onExit) and contextMenus[openContextMenu].onExit then contextMenus[openContextMenu].onExit() end
|
||||
|
||||
if not cb then SendNUIMessage({ action = 'hideContext' }) end
|
||||
|
||||
@@ -47,12 +42,11 @@ end
|
||||
---@param id string
|
||||
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)
|
||||
lib.setNuiFocus(false)
|
||||
|
||||
SendNuiMessage(json.encode({
|
||||
action = 'showContext',
|
||||
@@ -104,7 +98,7 @@ RegisterNUICallback('clickContext', function(id, cb)
|
||||
|
||||
openContextMenu = nil
|
||||
|
||||
resetFocus()
|
||||
lib.resetNuiFocus()
|
||||
|
||||
if data.onSelect then data.onSelect(data.args) end
|
||||
if data.event then TriggerEvent(data.event, data.args) end
|
||||
|
||||
12
resource/interface/client/main.lua
Normal file
12
resource/interface/client/main.lua
Normal 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
|
||||
@@ -2,7 +2,6 @@
|
||||
local registeredMenus = {}
|
||||
---@type MenuProps | nil
|
||||
local openMenu
|
||||
local keepInput = IsNuiFocusKeepingInput()
|
||||
|
||||
---@alias MenuPosition 'top-left' | 'top-right' | 'bottom-left' | 'bottom-right'
|
||||
---@alias MenuChangeFunction fun(selected: number, scrollIndex?: number, args?: any, checked?: boolean)
|
||||
@@ -65,13 +64,9 @@ function lib.showMenu(id, startIndex)
|
||||
end
|
||||
|
||||
openMenu = menu
|
||||
keepInput = IsNuiFocusKeepingInput()
|
||||
|
||||
if not menu.disableInput then
|
||||
SetNuiFocusKeepInput(true)
|
||||
end
|
||||
lib.setNuiFocus(not menu.disableInput, false)
|
||||
|
||||
SetNuiFocus(true, false)
|
||||
SendNUIMessage({
|
||||
action = 'setMenu',
|
||||
data = {
|
||||
@@ -84,24 +79,19 @@ function lib.showMenu(id, startIndex)
|
||||
})
|
||||
end
|
||||
|
||||
local function resetFocus()
|
||||
SetNuiFocus(false, false)
|
||||
SetNuiFocusKeepInput(keepInput)
|
||||
end
|
||||
|
||||
---@param onExit boolean?
|
||||
function lib.hideMenu(onExit)
|
||||
local menu = openMenu
|
||||
openMenu = nil
|
||||
|
||||
if not menu then return end
|
||||
|
||||
openMenu = nil
|
||||
lib.resetNuiFocus()
|
||||
|
||||
if onExit and menu.onClose then
|
||||
menu.onClose()
|
||||
end
|
||||
|
||||
resetFocus()
|
||||
SendNUIMessage({
|
||||
action = 'closeMenu'
|
||||
})
|
||||
@@ -135,7 +125,7 @@ RegisterNUICallback('confirmSelected', function(data, cb)
|
||||
if not menu then return end
|
||||
|
||||
if menu.options[data[1]].close ~= false then
|
||||
resetFocus()
|
||||
lib.resetNuiFocus()
|
||||
openMenu = nil
|
||||
end
|
||||
|
||||
@@ -191,7 +181,7 @@ end)
|
||||
|
||||
RegisterNUICallback('closeMenu', function(data, cb)
|
||||
cb(1)
|
||||
resetFocus()
|
||||
lib.resetNuiFocus()
|
||||
|
||||
local menu = openMenu
|
||||
|
||||
|
||||
Reference in New Issue
Block a user