From 7a22da68e6df2e1248beb6086ed461c5152c040d Mon Sep 17 00:00:00 2001 From: masonschafercodes <57614646+masonschafercodes@users.noreply.github.com> Date: Wed, 8 Feb 2023 04:05:30 -0500 Subject: [PATCH] feat(web/context): mouse enter and mouse leave events (#218) * feat(web/context): mouse enter and mouse leave events * chore(web/context): condense into one function for onHover * feat(resource/context): cache last hovered option --- package/client/resource/interface/context.ts | 1 + resource/interface/client/context.lua | 54 ++++++++++++++++--- .../menu/context/components/ContextButton.tsx | 32 +++++------ 3 files changed, 64 insertions(+), 23 deletions(-) diff --git a/package/client/resource/interface/context.ts b/package/client/resource/interface/context.ts index 0a00054..2be0f59 100644 --- a/package/client/resource/interface/context.ts +++ b/package/client/resource/interface/context.ts @@ -6,6 +6,7 @@ interface ContextMenuItem { icon?: IconName | [IconPrefix, IconName]; iconColor?: string; onSelect?: (args: any) => void; + onHover?: (hoverState: boolean, args: any) => void; arrow?: boolean; description?: string; metadata?: string | { [key: string]: any } | string[]; diff --git a/resource/interface/client/context.lua b/resource/interface/client/context.lua index 2115efe..ca09e5a 100644 --- a/resource/interface/client/context.lua +++ b/resource/interface/client/context.lua @@ -1,4 +1,5 @@ local contextMenus = {} +local lastHoveredContextItem = nil local openContextMenu = nil local keepInput = IsNuiFocusKeepingInput() @@ -8,6 +9,7 @@ local keepInput = IsNuiFocusKeepingInput() ---@field icon? string ---@field iconColor? string ---@field onSelect? fun(args: any) +---@field onHover? fun(hoverState: boolean, args: any) ---@field arrow? boolean ---@field description? string ---@field metadata? string | { [string]: any } | string[] @@ -41,9 +43,25 @@ local function closeContext(_, cb, onExit) if not cb then SendNUIMessage({ action = 'hideContext' }) end + if lastHoveredContextItem then + local data = contextMenus[openContextMenu].options[lastHoveredContextItem] + if data.onHover then data.onHover(false, data.args) end + lastHoveredContextItem = nil + end + openContextMenu = nil end +local function checkID(id) + if math.type(tonumber(id)) == 'float' then + id = math.tointeger(id) + elseif tonumber(id) then + id += 1 + end + + return id +end + ---@param id string function lib.showContext(id) if not contextMenus[id] then error('No context menu of such id found.') end @@ -89,14 +107,11 @@ RegisterNUICallback('openContext', function(data, cb) lib.showContext(data.id) 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 id = checkID(id) local data = contextMenus[openContextMenu].options[id] @@ -110,12 +125,35 @@ RegisterNUICallback('clickContext', function(id, cb) if data.event then TriggerEvent(data.event, data.args) end if data.serverEvent then TriggerServerEvent(data.serverEvent, data.args) end + if lastHoveredContextItem then + local data = contextMenus[openContextMenu].options[lastHoveredContextItem] + if data.onHover then data.onHover(false, data.args) end + lastHoveredContextItem = nil + end + SendNUIMessage({ action = 'hideContext' }) end) +RegisterNUICallback('onHover', function(data, cb) + cb(1) + + local id = checkID(data.id) + local hoverState = data.hoverState + + local data = contextMenus[openContextMenu].options[id] + + if not data.onHover then return end + + if data.onHover and hoverState == true then + lastHoveredContextItem = id + data.onHover(true, data.args) + end + if data.onHover and hoverState == false then + lastHoveredContextItem = nil + data.onHover(false, data.args) + end +end) + RegisterNUICallback('closeContext', closeContext) - - - diff --git a/web/src/features/menu/context/components/ContextButton.tsx b/web/src/features/menu/context/components/ContextButton.tsx index 6680727..ba02ffa 100644 --- a/web/src/features/menu/context/components/ContextButton.tsx +++ b/web/src/features/menu/context/components/ContextButton.tsx @@ -8,10 +8,6 @@ const openMenu = (id: string | undefined) => { fetchNui('openContext', { id: id, back: false }); }; -const clickContext = (id: string) => { - fetchNui('clickContext', id); -}; - const useStyles = createStyles((theme, params: { disabled?: boolean }) => ({ inner: { justifyContent: 'flex-start', @@ -51,7 +47,11 @@ const ContextButton: React.FC<{