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
---@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()

View File

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

View File

@@ -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)
@@ -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
@@ -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)

View File

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

View File

@@ -2,19 +2,44 @@ local registeredMenus = {}
local openMenu = nil
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)
if not data.id then return error('No menu id was provided.') end
if not data.title then return error('No menu title was provided.') end
if not data.options then return error('No menu options were provided.') end
if not data.id then error('No menu id was provided.') end
if not data.title then error('No menu title was provided.') end
if not data.options then error('No menu options were provided.') end
data.cb = cb
registeredMenus[data.id] = data
end
---@param id string
---@param startIndex? number
function lib.showMenu(id, startIndex)
local menu = registeredMenus[id]
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
if not openMenu then
@@ -56,6 +81,7 @@ local function resetFocus()
SetNuiFocusKeepInput(keepInput)
end
---@param onExit boolean?
function lib.hideMenu(onExit)
local menu = openMenu
openMenu = nil
@@ -70,15 +96,19 @@ function lib.hideMenu(onExit)
})
end
---@param id string
---@param options MenuOptions | MenuOptions[]
---@param index? number
function lib.setMenuOptions(id, options, index)
if index then
registeredMenus[id].options[index] = options
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
end
end
---@return string?
function lib.getOpenMenu() return openMenu end
RegisterNUICallback('confirmSelected', function(data, cb)

View File

@@ -1,18 +1,18 @@
--[[```lua
{
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
}
```]]
---@alias NotificationPosition 'top' | 'top-right' | 'top-left' | 'bottom' | 'bottom-right' | 'bottom-left'
---@alias NotificationType 'inform' | 'error' | 'success'
-- Custom notifications with a lot of allowed styling
---@param data table
---@class NotifyProps
---@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)
SendNUIMessage({
action = 'customNotify',
@@ -20,7 +20,15 @@ function lib.notify(data)
})
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)
SendNUIMessage({
action = 'notify',

View File

@@ -3,6 +3,25 @@ local DisableControlAction = DisableControlAction
local DisablePlayerFiring = DisablePlayerFiring
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)
lib.requestModel(prop.model)
local coords = GetEntityCoords(cache.ped)
@@ -112,6 +131,8 @@ local function startProgress(data)
return true
end
---@param data ProgressProps
---@return boolean?
function lib.progressBar(data)
while progress ~= nil do Wait(100) end
@@ -128,6 +149,8 @@ function lib.progressBar(data)
end
end
---@param data ProgressProps
---@return boolean?
function lib.progressCircle(data)
while progress ~= nil do Wait(100) end
@@ -155,6 +178,7 @@ function lib.cancelProgress()
progress = false
end
---@return boolean
function lib.progressActive()
return progress and true
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)
if not options then options = {} end
options.text = text