Skip to content

Commit

Permalink
refactor(resource/interface): formatting and type definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
thelindat committed Sep 17, 2022
1 parent cb645ae commit 6254ba9
Show file tree
Hide file tree
Showing 8 changed files with 414 additions and 291 deletions.
10 changes: 9 additions & 1 deletion resource/interface/client/alert.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
local alert = nil

---@class AlertDialogProps
---@field header string;
---@field content string;
---@field centered? boolean?;
---@field cancel? boolean?;

---@param data AlertDialogProps
---@return 'cancel' | 'confirm' | nil
function lib.alertDialog(data)
if alert then return end
alert = promise.new()
Expand All @@ -21,4 +29,4 @@ RegisterNUICallback('closeAlert', function(data, cb)
promise:resolve(data)
end)

RegisterNetEvent('ox_lib:alertDialog', lib.alertDialog)
RegisterNetEvent('ox_lib:alertDialog', lib.alertDialog)
11 changes: 6 additions & 5 deletions resource/interface/client/clipboard.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---@param value string
function lib.setClipboard(value)
SendNUIMessage({
action = 'setClipboard',
data = value
})
end
SendNUIMessage({
action = 'setClipboard',
data = value
})
end
36 changes: 32 additions & 4 deletions resource/interface/client/context.lua
Original file line number Diff line number Diff line change
@@ -1,16 +1,41 @@
local contextMenus = {}
local openContextMenu = nil

---@class ContextMenuItem
---@field title? string
---@field menu? string
---@field icon? string
---@field iconColor? string
---@field onSelect? fun(args: any)
---@field arrow? boolean
---@field description? string
---@field metadata? string | { [string]: any } | string[]
---@field event? string
---@field serverEvent? string
---@field args? any

---@class ContextMenuArrayItem : ContextMenuItem
---@field title string

---@class ContextMenuProps
---@field id string
---@field title string
---@field menu? string
---@field onExit? fun()
---@field canClose? boolean
---@field options { [string]: ContextMenuItem } | ContextMenuArrayItem[]

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
if not cb then SendNUIMessage({ action = 'hideContext' }) end
openContextMenu = nil
end

---@param id string
function lib.showContext(id)
if not contextMenus[id] then return 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]
openContextMenu = id
SetNuiFocus(true, true)
Expand All @@ -25,6 +50,7 @@ function lib.showContext(id)
}, { sort_keys = true }))
end

---@param context ContextMenuProps | ContextMenuProps[]
function lib.registerContext(context)
for k, v in pairs(context) do
if type(k) == 'number' then
Expand All @@ -36,9 +62,11 @@ function lib.registerContext(context)
end
end

---@return string?
function lib.getOpenContextMenu() return openContextMenu end

function lib.hideContext(onExit) return closeContext(nil, nil, onExit) end
---@param onExit boolean?
function lib.hideContext(onExit) closeContext(nil, nil, onExit) end

RegisterNUICallback('openContext', function(id, cb)
cb(1)
Expand All @@ -64,4 +92,4 @@ RegisterNUICallback('clickContext', function(id, cb)
})
end)

RegisterNUICallback('closeContext', closeContext)
RegisterNUICallback('closeContext', closeContext)
64 changes: 40 additions & 24 deletions resource/interface/client/input.lua
Original file line number Diff line number Diff line change
@@ -1,33 +1,49 @@
local input

function lib.inputDialog(heading, rows)
if input then return end
input = promise.new()
---@class InputDialogRowProps
---@field type 'input' | 'number' | 'checkbox' | 'select' | 'slider'
---@field label string
---@field options? { value: string, label: string, default?: string }[]
---@field password? boolean
---@field icon? string
---@field iconColor? string
---@field placeholder? string
---@field default? string | number
---@field checked? boolean
---@field min? number
---@field max? number
---@field step? number

-- Backwards compat with string tables
for i = 1, #rows do
if type(rows[i]) == 'string' then
rows[i] = {type = 'input', label = rows[i]}
end
---@param heading string
---@param rows string[] | InputDialogRowProps[]
---@return string[] | number[] | boolean[] | nil
function lib.inputDialog(heading, rows)
if input then return end
input = promise.new()

end
-- Backwards compat with string tables
for i = 1, #rows do
if type(rows[i]) == 'string' then
rows[i] = { type = 'input', label = rows[i] --[[@as string]] }
end
end

SetNuiFocus(true, true)
SendNUIMessage({
action = 'openDialog',
data = {
heading = heading,
rows = rows
}
})
SetNuiFocus(true, true)
SendNUIMessage({
action = 'openDialog',
data = {
heading = heading,
rows = rows
}
})

return Citizen.Await(input)
return Citizen.Await(input)
end

RegisterNUICallback('inputData', function(data, cb)
cb(1)
SetNuiFocus(false, false)
local promise = input
input = nil
promise:resolve(data)
end)
cb(1)
SetNuiFocus(false, false)
local promise = input
input = nil
promise:resolve(data)
end)
Loading

0 comments on commit 6254ba9

Please sign in to comment.