refactor(resource/interface): formatting and type definitions

This commit is contained in:
Linden
2022-09-18 09:06:32 +10:00
parent cb645ae345
commit 6254ba9ba2
8 changed files with 388 additions and 265 deletions

View File

@@ -1,5 +1,13 @@
local alert = nil 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) function lib.alertDialog(data)
if alert then return end if alert then return end
alert = promise.new() alert = promise.new()

View File

@@ -1,3 +1,4 @@
---@param value string
function lib.setClipboard(value) function lib.setClipboard(value)
SendNUIMessage({ SendNUIMessage({
action = 'setClipboard', action = 'setClipboard',

View File

@@ -1,6 +1,30 @@
local contextMenus = {} local contextMenus = {}
local openContextMenu = nil 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) 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 if (cb or onExit) and contextMenus[openContextMenu].onExit then contextMenus[openContextMenu].onExit() end
@@ -9,8 +33,9 @@ local function closeContext(_, cb, onExit)
openContextMenu = nil openContextMenu = nil
end end
---@param id string
function lib.showContext(id) 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] local data = contextMenus[id]
openContextMenu = id openContextMenu = id
SetNuiFocus(true, true) SetNuiFocus(true, true)
@@ -25,6 +50,7 @@ function lib.showContext(id)
}, { sort_keys = true })) }, { sort_keys = true }))
end end
---@param context ContextMenuProps | ContextMenuProps[]
function lib.registerContext(context) function lib.registerContext(context)
for k, v in pairs(context) do for k, v in pairs(context) do
if type(k) == 'number' then if type(k) == 'number' then
@@ -36,9 +62,11 @@ function lib.registerContext(context)
end end
end end
---@return string?
function lib.getOpenContextMenu() return openContextMenu end 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) RegisterNUICallback('openContext', function(id, cb)
cb(1) cb(1)

View File

@@ -1,5 +1,22 @@
local input local input
---@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
---@param heading string
---@param rows string[] | InputDialogRowProps[]
---@return string[] | number[] | boolean[] | nil
function lib.inputDialog(heading, rows) function lib.inputDialog(heading, rows)
if input then return end if input then return end
input = promise.new() input = promise.new()
@@ -7,9 +24,8 @@ function lib.inputDialog(heading, rows)
-- Backwards compat with string tables -- Backwards compat with string tables
for i = 1, #rows do for i = 1, #rows do
if type(rows[i]) == 'string' then if type(rows[i]) == 'string' then
rows[i] = {type = 'input', label = rows[i]} rows[i] = { type = 'input', label = rows[i] --[[@as string]] }
end end
end end
SetNuiFocus(true, true) SetNuiFocus(true, true)

View File

@@ -2,19 +2,44 @@ local registeredMenus = {}
local openMenu = nil local openMenu = nil
local keepInput = IsNuiFocusKeepingInput() local keepInput = IsNuiFocusKeepingInput()
---@alias MenuPosition 'top-left' | 'top-right' | 'bottom-left' | 'bottom-right';
---@alias MenuChangeFunction fun(selected: number, scrollIndex?: number, args?: any)
---@class MenuOptions
---@field label string
---@field icon? string
---@field values? string[]
---@field description? string
---@field defaultIndex? number
---@field args? any
---@class MenuProps
---@field id string
---@field title string
---@field options MenuOptions[]
---@field position? MenuPosition
---@field disableInput? boolean
---@field onClose? fun()
---@field onSelected? MenuChangeFunction
---@field onSideScroll? MenuChangeFunction
---@param data MenuProps
---@param cb? MenuChangeFunction
function lib.registerMenu(data, cb) function lib.registerMenu(data, cb)
if not data.id then return error('No menu id was provided.') end if not data.id then error('No menu id was provided.') end
if not data.title then return error('No menu title was provided.') end if not data.title then error('No menu title was provided.') end
if not data.options then return error('No menu options were provided.') end if not data.options then error('No menu options were provided.') end
data.cb = cb data.cb = cb
registeredMenus[data.id] = data registeredMenus[data.id] = data
end end
---@param id string
---@param startIndex? number
function lib.showMenu(id, startIndex) function lib.showMenu(id, startIndex)
local menu = registeredMenus[id] local menu = registeredMenus[id]
if not menu then if not menu then
return error(('No menu with id %s was found'):format(id)) error(('No menu with id %s was found'):format(id))
end end
if not openMenu then if not openMenu then
@@ -56,6 +81,7 @@ local function resetFocus()
SetNuiFocusKeepInput(keepInput) SetNuiFocusKeepInput(keepInput)
end end
---@param onExit boolean?
function lib.hideMenu(onExit) function lib.hideMenu(onExit)
local menu = openMenu local menu = openMenu
openMenu = nil openMenu = nil
@@ -70,15 +96,19 @@ function lib.hideMenu(onExit)
}) })
end end
---@param id string
---@param options MenuOptions | MenuOptions[]
---@param index? number
function lib.setMenuOptions(id, options, index) function lib.setMenuOptions(id, options, index)
if index then if index then
registeredMenus[id].options[index] = options registeredMenus[id].options[index] = options
else else
if not options[1] then return error('Invalid override format used, expected table of options.') end if not options[1] then error('Invalid override format used, expected table of options.') end
registeredMenus[id].options = options registeredMenus[id].options = options
end end
end end
---@return string?
function lib.getOpenMenu() return openMenu end function lib.getOpenMenu() return openMenu end
RegisterNUICallback('confirmSelected', function(data, cb) RegisterNUICallback('confirmSelected', function(data, cb)

View File

@@ -1,18 +1,18 @@
--[[```lua ---@alias NotificationPosition 'top' | 'top-right' | 'top-left' | 'bottom' | 'bottom-right' | 'bottom-left'
{ ---@alias NotificationType 'inform' | 'error' | 'success'
id?: string
title?: string
description: string
duration?: number
position?: 'top' | 'top-right' | 'top-left' | 'bottom' | 'bottom-right' | 'bottom-left'
style?: table
icon?: string
iconColor?: string
}
```]]
-- Custom notifications with a lot of allowed styling ---@class NotifyProps
---@param data table ---@field id? string
---@field title? string
---@field description? string
---@field duration? number
---@field position? NotificationPosition
---@field type? NotificationType
---@field style? string
---@field icon? string;
---@field iconColor? string;
---@param data NotifyProps
function lib.notify(data) function lib.notify(data)
SendNUIMessage({ SendNUIMessage({
action = 'customNotify', action = 'customNotify',
@@ -20,7 +20,15 @@ function lib.notify(data)
}) })
end end
-- Default Chakra UI notifications ---@class DefaultNotifyProps
---@field title? string
---@field description? string
---@field duration? number
---@field position? NotificationPosition
---@field status? 'info' | 'warning' | 'success' | 'error'
---@field id? number
---@param data DefaultNotifyProps
function lib.defaultNotify(data) function lib.defaultNotify(data)
SendNUIMessage({ SendNUIMessage({
action = 'notify', action = 'notify',

View File

@@ -3,6 +3,25 @@ local DisableControlAction = DisableControlAction
local DisablePlayerFiring = DisablePlayerFiring local DisablePlayerFiring = DisablePlayerFiring
local playerState = LocalPlayer.state local playerState = LocalPlayer.state
---@class ProgressPropProps
---@field model string
---@field bone? number
---@field pos vector3
---@field rot vector3
---@class ProgressProps
---@field label? string
---@field duration number
---@field position? 'middle' | 'bottom'
---@field useWhileDead? boolean
---@field allowRagdoll? boolean
---@field allowCuffed? boolean
---@field allowFalling? boolean
---@field canCancel? boolean
---@field anim? { dict?: string, clip: string, flag?: number, blendIn?: number, blendOut?: number, duration?: number, playbackRate?: number, lockX?: boolean, lockY?: boolean, lockZ?: boolean, scenario?: string, playEnter?: boolean }
---@field prop? ProgressPropProps | ProgressPropProps[]
---@field disable? { move?: boolean, car?: boolean, combat?: boolean, mouse?: boolean }
local function createProp(prop) local function createProp(prop)
lib.requestModel(prop.model) lib.requestModel(prop.model)
local coords = GetEntityCoords(cache.ped) local coords = GetEntityCoords(cache.ped)
@@ -112,6 +131,8 @@ local function startProgress(data)
return true return true
end end
---@param data ProgressProps
---@return boolean?
function lib.progressBar(data) function lib.progressBar(data)
while progress ~= nil do Wait(100) end while progress ~= nil do Wait(100) end
@@ -128,6 +149,8 @@ function lib.progressBar(data)
end end
end end
---@param data ProgressProps
---@return boolean?
function lib.progressCircle(data) function lib.progressCircle(data)
while progress ~= nil do Wait(100) end while progress ~= nil do Wait(100) end
@@ -155,6 +178,7 @@ function lib.cancelProgress()
progress = false progress = false
end end
---@return boolean
function lib.progressActive() function lib.progressActive()
return progress and true return progress and true
end end

View File

@@ -1,3 +1,11 @@
---@class TextUIOptions
---@field position? 'right-center' | 'left-center' | 'top-center';
---@field icon? string;
---@field iconColor? string;
---@field style? string;
---@param text string
---@param options TextUIOptions
function lib.showTextUI(text, options) function lib.showTextUI(text, options)
if not options then options = {} end if not options then options = {} end
options.text = text options.text = text