Skip to content

Commit

Permalink
Allow ContextMenuItem to be opened on left click
Browse files Browse the repository at this point in the history
close #55
  • Loading branch information
antoniotorresm authored and coding-jackalope committed Oct 12, 2020
1 parent afda783 commit a4d9546
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 8 deletions.
12 changes: 8 additions & 4 deletions API.lua
Original file line number Diff line number Diff line change
Expand Up @@ -750,11 +750,13 @@ end
Slab.EndContextMenu()
end
Button: [Number] The mouse button to use for opening up this context menu.
Return: [Boolean] Returns true if the user right clicks on the previous item call. EndContextMenu must be called in order for
this to function properly.
--]]
function Slab.BeginContextMenuItem()
return Menu.BeginContextMenu({IsItem = true})
function Slab.BeginContextMenuItem(Button)
return Menu.BeginContextMenu({IsItem = true, Button = Button})
end

--[[
Expand All @@ -764,11 +766,13 @@ end
of a window's widget calls so that Slab can catch any BeginContextMenuItem calls before this call. If this function returns true,
EndContextMenu must be called.
Button: [Number] The mouse button to use for opening up this context menu.
Return: [Boolean] Returns true if the user right clicks anywhere within the window. EndContextMenu must be called in order for this
to function properly.
--]]
function Slab.BeginContextMenuWindow()
return Menu.BeginContextMenu({IsWindow = true})
function Slab.BeginContextMenuWindow(Button)
return Menu.BeginContextMenu({IsWindow = true, Button = Button})
end

--[[
Expand Down
2 changes: 1 addition & 1 deletion Internal/UI/Dialog.lua
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ function Dialog.FileDialog(Options)
Options.AllowMultiSelect = false
Options.Title = "Save File"
elseif Options.Type == 'opendirectory' then
Title = "Open Directory"
Options.Title = "Open Directory"
end
end

Expand Down
5 changes: 4 additions & 1 deletion Internal/UI/Menu.lua
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,9 @@ end

function Menu.BeginContextMenu(Options)
Options = Options == nil and {} or Options
Options.IsItem = Options.IsItem == nil and false or Options.IsItem
Options.IsWindow = Options.IsWindow == nil and false or Options.IsWindow
Options.Button = Options.Button == nil and 2 or Options.Button

local BaseId = nil
local Id = nil
Expand Down Expand Up @@ -314,7 +317,7 @@ function Menu.BeginContextMenu(Options)
end

local IsOpening = false
if not Window.IsObstructedAtMouse() and Window.IsMouseHovered() and Mouse.IsClicked(2) then
if not Window.IsObstructedAtMouse() and Window.IsMouseHovered() and Mouse.IsClicked(Options.Button) then
local IsValidWindow = Options.IsWindow and Window.GetHotItem() == nil
local IsValidItem = Options.IsItem

Expand Down
13 changes: 11 additions & 2 deletions SlabTest.lua
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,8 @@ local DrawMenus_CheckBox = false
local DrawMenus_ComboBox = {"Apple", "Banana", "Pear", "Orange", "Lemon"}
local DrawMenus_ComboBox_Selected = "Apple"

local function DrawContextMenuItem(Label)
if Slab.BeginContextMenuItem() then
local function DrawContextMenuItem(Label, Button)
if Slab.BeginContextMenuItem(Button) then
for I = 1, 5, 1 do
local MenuLabel = Label .. " Option " .. I
if Slab.MenuItem(MenuLabel) then
Expand Down Expand Up @@ -303,6 +303,15 @@ local function DrawMenus()
end
DrawContextMenuItem("Combo Box")

Slab.NewLine()
Slab.Textf(
"Context menu items are usually opened with the right mouse button. This can be changed for context menus to be a differen " ..
"mouse button. The button below will open a context menu using the left mouse button.")

Slab.NewLine()
Slab.Button("Left Mouse")
DrawContextMenuItem("Left Mouse Button", 1)

Slab.NewLine()
Slab.Separator()

Expand Down

0 comments on commit a4d9546

Please sign in to comment.