From 8cef26e415252d4b6558ba5f8836717886fc9137 Mon Sep 17 00:00:00 2001 From: MrSandor <89403726+MrSandor@users.noreply.github.com> Date: Thu, 5 Mar 2026 19:02:26 +0100 Subject: [PATCH] Fix NUI focus race when selecting option that opens another UI --- client/main.lua | 88 +++++++++++++++++++++++++++---------------------- 1 file changed, 49 insertions(+), 39 deletions(-) diff --git a/client/main.lua b/client/main.lua index 2d6ad7a..0d0e35d 100644 --- a/client/main.lua +++ b/client/main.lua @@ -610,58 +610,68 @@ RegisterNUICallback('select', function(data, cb) local zone = data[3] and nearbyZones[data[3]] ---@type OxTargetOption? - local option = zone and zone.options[data[2]] or options[data[1]][data[2]] + local option = (zone and zone.options[data[2]]) or (options[data[1]] and options[data[1]][data[2]]) - if option then - local maxDistance = option.distance or 7 - if currentTarget.distance and currentTarget.distance > maxDistance then - return + if not option then + if closeOnSelect and IsNuiFocused() then + state.setActive(false) + SetNuiFocus(false, false) + SetNuiFocusKeepInput(false) end + return + end - if option.openMenu then - local menuDepth = #menuHistory - - if option.name == 'builtin:goback' then - option.menuName = option.openMenu - option.openMenu = menuHistory[menuDepth] - - if menuDepth > 0 then - menuHistory[menuDepth] = nil - end - else - menuHistory[menuDepth + 1] = currentMenu - end + local maxDistance = option.distance or 7 + if currentTarget.distance and currentTarget.distance > maxDistance then + return + end - menuChanged = true - currentMenu = option.openMenu ~= 'home' and option.openMenu or nil + if option.openMenu then + local menuDepth = #menuHistory - options:wipe() + if option.name == 'builtin:goback' then + option.menuName = option.openMenu + option.openMenu = menuHistory[menuDepth] - if currentTarget.isSelf then - options:setSelf() - elseif currentTarget.entity and currentTarget.entity > 0 and currentTarget.entityModel then - options:set(currentTarget.entity, currentTarget.entityType, currentTarget.entityModel) + if menuDepth > 0 then + menuHistory[menuDepth] = nil end + else + menuHistory[menuDepth + 1] = currentMenu end - currentTarget.zone = zone?.id - - if option.onSelect then - option.onSelect(option.qtarget and currentTarget.entity or getResponse(option)) - elseif option.export then - exports[option.resource or zone.resource][option.export](nil, getResponse(option)) - elseif option.event then - TriggerEvent(option.event, getResponse(option)) - elseif option.serverEvent then - TriggerServerEvent(option.serverEvent, getResponse(option, true)) - elseif option.command then - ExecuteCommand(option.command) + menuChanged = true + currentMenu = option.openMenu ~= 'home' and option.openMenu or nil + + options:wipe() + + if currentTarget.isSelf then + options:setSelf() + elseif currentTarget.entity and currentTarget.entity > 0 and currentTarget.entityModel then + options:set(currentTarget.entity, currentTarget.entityType, currentTarget.entityModel) end - if option.menuName == 'home' then return end + return end - if closeOnSelect and not option?.openMenu and IsNuiFocused() then + currentTarget.zone = zone and zone.id or nil + + if closeOnSelect and IsNuiFocused() then state.setActive(false) + SetNuiFocus(false, false) + SetNuiFocusKeepInput(false) + Wait(0) + end + + if option.onSelect then + option.onSelect(option.qtarget and currentTarget.entity or getResponse(option)) + elseif option.export then + exports[option.resource or (zone and zone.resource)][option.export](nil, getResponse(option)) + elseif option.event then + TriggerEvent(option.event, getResponse(option)) + elseif option.serverEvent then + TriggerServerEvent(option.serverEvent, getResponse(option, true)) + elseif option.command then + ExecuteCommand(option.command) end end)