cLib.DContextMenu
Styled context menu with submenus and dividers.
Quick Function
cLib.ShowContextMenu(options, x, y)cLib.ShowContextMenu({
{text = "Edit", icon = "icon16/pencil.png", callback = function() Edit() end},
{text = "Duplicate", icon = "icon16/page_copy.png", callback = function() Duplicate() end},
{divider = true},
{text = "Delete", icon = "icon16/cross.png", callback = function() Delete() end},
})Option Properties
| Property | Type | Description |
|---|---|---|
text | string | Menu item text |
icon | string | Icon path (optional) |
callback | function | Click handler |
divider | boolean | Separator line |
disabled | boolean | Gray out item |
submenu | table | Submenu options |
With Submenus
cLib.ShowContextMenu({
{text = "File", submenu = {
{text = "New", icon = "icon16/page_add.png", callback = function() New() end},
{text = "Open", icon = "icon16/folder.png", callback = function() Open() end},
{text = "Save", icon = "icon16/disk.png", callback = function() Save() end},
}},
{text = "Edit", submenu = {
{text = "Cut", callback = function() Cut() end},
{text = "Copy", callback = function() Copy() end},
{text = "Paste", callback = function() Paste() end},
}},
{divider = true},
{text = "Settings", icon = "icon16/cog.png", callback = function() Settings() end},
})Right-Click on Panel
local panel = vgui.Create("DPanel", parent)
panel.OnMousePressed = function(self, code)
if code == MOUSE_RIGHT then
cLib.ShowContextMenu({
{text = "Option 1", callback = function() end},
{text = "Option 2", callback = function() end},
})
end
endRight-Click on List Row
list.OnRowRightClick = function(self, index, row)
local item = row.itemData
cLib.ShowContextMenu({
{text = "View Details", icon = "icon16/zoom.png", callback = function()
ViewItem(item)
end},
{text = "Edit", icon = "icon16/pencil.png", callback = function()
EditItem(item)
end},
{text = "Duplicate", icon = "icon16/page_copy.png", callback = function()
DuplicateItem(item)
end},
{divider = true},
{text = "Move to...", submenu = {
{text = "Favorites", callback = function() MoveToFavorites(item) end},
{text = "Archive", callback = function() MoveToArchive(item) end},
}},
{divider = true},
{text = "Delete", icon = "icon16/cross.png", callback = function()
cLib.Confirm("Delete", "Delete this item?", function()
DeleteItem(item)
end)
end},
})
endDisabled Items
local canEdit = HasPermission("edit")
local canDelete = HasPermission("delete")
cLib.ShowContextMenu({
{text = "View", callback = function() View() end},
{text = "Edit", callback = function() Edit() end, disabled = not canEdit},
{divider = true},
{text = "Delete", callback = function() Delete() end, disabled = not canDelete},
})