function Icon.setMenu(l,G)for G,l in pairs(l.menuIcons)do l:leave()end if type(G)=="\116\097\098\108\101"then for G,E in pairs(G)do E:join(l,"\109\101\110\117",true)end end l:_updateMenu()return l end function Icon._getMenuDirection(G)local l=(G:get("\109\101\110\117\068\105\114\101\099\116\105\111\110")or"\095\078\073\076"):lower()local E=(G:get("\097\108\105\103\110\109\101\110\116")or"\095\078\073\076"):lower()if l~="\108\101\102\116"and l~="\114\105\103\104\116"then l=E=="\108\101\102\116"and"\114\105\103\104\116"or"\108\101\102\116"end return l end function Icon._updateMenu(G)local l={maxIconsBeforeScroll=G:get("\109\101\110\117\077\097\120\073\099\111\110\115\066\101\102\111\114\101\083\099\114\111\108\108")or"\095\078\073\076",direction=G:get("\109\101\110\117\068\105\114\101\099\116\105\111\110")or"\095\078\073\076",iconAlignment=G:get("\097\108\105\103\110\109\101\110\116")or"\095\078\073\076",scrollBarThickness=G:get("\109\101\110\117\083\099\114\111\108\108\066\097\114\084\104\105\099\107\110\101\115\115")or"\095\078\073\076"}for G,l in pairs(l)do if l=="\095\078\073\076"then return end end local E=IconController[l.iconAlignment.."\071\097\112"]local Y=G.instances.menuContainer local B=G.instances.menuFrame local p=G.instances.menuList local J=#G.menuIcons local t=G:_getMenuDirection()local A=J>l.maxIconsBeforeScroll and l.maxIconsBeforeScroll or J local K=-E local Z=0 local C=0 local S=t=="\114\105\103\104\116"and function(G,l)return G:get("\111\114\100\101\114")<l:get("\111\114\100\101\114")end or function(G,l)return G:get("\111\114\100\101\114")>l:get("\111\114\100\101\114")end table.sort(G.menuIcons,S)for l=1,J,1 do local Y=G.menuIcons[l]local B=Y:get("\105\099\111\110\083\105\122\101")local p=B.X.Offset+E if l<=A then Z=Z+p end if l==A and l~=J then Z=Z-2 end K=K+p local t=B.Y.Offset if t>C then C=t end local S=G.menuIcons[l-1]local D=G.menuIcons[l+1]Y.instances.iconButton.NextSelectionRight=S and S.instances.iconButton Y.instances.iconButton.NextSelectionLeft=D and D.instances.iconButton end local D=A==J and 0 or K+E G:set("\109\101\110\117\067\097\110\118\097\115\083\105\122\101",UDim2.new(0,D,0,0))G:set("\109\101\110\117\083\105\122\101",UDim2.new(0,Z,0,(C+l.scrollBarThickness)+3))local f={left={containerAnchorPoint=Vector2.new(1,0);containerPosition=UDim2.new(0,-4,0,0);canvasPosition=Vector2.new(D,0)},right={containerAnchorPoint=Vector2.new(0,0),containerPosition=UDim2.new(1,E-2,0,0);canvasPosition=Vector2.new(0,0)}}local z=f[t]Y.AnchorPoint=z.containerAnchorPoint Y.Position=z.containerPosition B.CanvasPosition=z.canvasPosition G._menuCanvasPos=z.canvasPosition p.Padding=UDim.new(0,E)end function Icon._menuIgnoreClipping(G)G:_ignoreClipping("\109\101\110\117")end
-- Menus
function Icon:setMenu(arrayOfIcons)
	-- Reset any previous icons
	for i, otherIcon in pairs(self.menuIcons) do
		otherIcon:leave()
	end
	-- Apply new icons
	if type(arrayOfIcons) == "table" then
		for i, otherIcon in pairs(arrayOfIcons) do
			otherIcon:join(self, "menu", true)
		end
	end
	-- Update menu
	self:_updateMenu()
	return self
end

function Icon:_getMenuDirection()
	local direction = (self:get("menuDirection") or "_NIL"):lower()
	local alignment = (self:get("alignment") or "_NIL"):lower()
	if direction ~= "left" and direction ~= "right" then
		direction = (alignment == "left" and "right") or "left"
	end
	return direction
end

function Icon:_updateMenu()
	local values = {
		maxIconsBeforeScroll = self:get("menuMaxIconsBeforeScroll") or "_NIL",
		direction = self:get("menuDirection") or "_NIL",
		iconAlignment = self:get("alignment") or "_NIL",
		scrollBarThickness = self:get("menuScrollBarThickness") or "_NIL",
	}
	for k, v in pairs(values) do
		if v == "_NIL" then
			return
		end
	end

	local XPadding = IconController[values.iconAlignment .. "Gap"] --12
	local menuContainer = self.instances.menuContainer
	local menuFrame = self.instances.menuFrame
	local menuList = self.instances.menuList
	local totalIcons = #self.menuIcons

	local direction = self:_getMenuDirection()
	local lastVisibleIconIndex = (totalIcons > values.maxIconsBeforeScroll and values.maxIconsBeforeScroll)
		or totalIcons
	local newCanvasSizeX = -XPadding
	local newFrameSizeX = 0
	local newMinHeight = 0
	local sortFunc = (direction == "right" and function(a, b)
		return a:get("order") < b:get("order")
	end) or function(a, b)
		return a:get("order") > b:get("order")
	end
	table.sort(self.menuIcons, sortFunc)
	for i = 1, totalIcons do
		local otherIcon = self.menuIcons[i]
		local otherIconSize = otherIcon:get("iconSize")
		local increment = otherIconSize.X.Offset + XPadding
		if i <= lastVisibleIconIndex then
			newFrameSizeX = newFrameSizeX + increment
		end
		if i == lastVisibleIconIndex and i ~= totalIcons then
			newFrameSizeX = newFrameSizeX - 2 --(increment/4)
		end
		newCanvasSizeX = newCanvasSizeX + increment
		local otherIconHeight = otherIconSize.Y.Offset
		if otherIconHeight > newMinHeight then
			newMinHeight = otherIconHeight
		end
		-- This ensures the menu is navigated fully and correctly with a controller
		local prevIcon = self.menuIcons[i - 1]
		local nextIcon = self.menuIcons[i + 1]
		otherIcon.instances.iconButton.NextSelectionRight = prevIcon and prevIcon.instances.iconButton
		otherIcon.instances.iconButton.NextSelectionLeft = nextIcon and nextIcon.instances.iconButton
	end

	local canvasSize = (lastVisibleIconIndex == totalIcons and 0) or newCanvasSizeX + XPadding
	self:set("menuCanvasSize", UDim2.new(0, canvasSize, 0, 0))
	self:set("menuSize", UDim2.new(0, newFrameSizeX, 0, newMinHeight + values.scrollBarThickness + 3))

	-- Set direction
	local directionDetails = {
		left = {
			containerAnchorPoint = Vector2.new(1, 0),
			containerPosition = UDim2.new(0, -4, 0, 0),
			canvasPosition = Vector2.new(canvasSize, 0),
		},
		right = {
			containerAnchorPoint = Vector2.new(0, 0),
			containerPosition = UDim2.new(1, XPadding - 2, 0, 0),
			canvasPosition = Vector2.new(0, 0),
		},
	}
	local directionDetail = directionDetails[direction]
	menuContainer.AnchorPoint = directionDetail.containerAnchorPoint
	menuContainer.Position = directionDetail.containerPosition
	menuFrame.CanvasPosition = directionDetail.canvasPosition
	self._menuCanvasPos = directionDetail.canvasPosition

	menuList.Padding = UDim.new(0, XPadding)
end

function Icon:_menuIgnoreClipping()
	self:_ignoreClipping("menu")
end
