function Icon.setMenu(b,M)for M,b in pairs(b.menuIcons)do b:leave()end if type(M)=="\116\097\098\108\101"then for M,Q in pairs(M)do Q:join(b,"\109\101\110\117",true)end end b:_updateMenu()return b end function Icon._getMenuDirection(M)local b=(M:get("\109\101\110\117\068\105\114\101\099\116\105\111\110")or"\095\078\073\076"):lower()local Q=(M:get("\097\108\105\103\110\109\101\110\116")or"\095\078\073\076"):lower()if b~="\108\101\102\116"and b~="\114\105\103\104\116"then b=Q=="\108\101\102\116"and"\114\105\103\104\116"or"\108\101\102\116"end return b end function Icon._updateMenu(M)local b={maxIconsBeforeScroll=M: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=M:get("\109\101\110\117\068\105\114\101\099\116\105\111\110")or"\095\078\073\076";iconAlignment=M:get("\097\108\105\103\110\109\101\110\116")or"\095\078\073\076";scrollBarThickness=M: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 M,b in pairs(b)do if b=="\095\078\073\076"then return end end local Q=IconController[b.iconAlignment.."\071\097\112"]local a=M.instances.menuContainer local V=M.instances.menuFrame local F=M.instances.menuList local L=#M.menuIcons local x=M:_getMenuDirection()local f=L>b.maxIconsBeforeScroll and b.maxIconsBeforeScroll or L local O=-Q local I=0 local H=0 local U=x=="\114\105\103\104\116"and function(M,b)return M:get("\111\114\100\101\114")<b:get("\111\114\100\101\114")end or function(M,b)return M:get("\111\114\100\101\114")>b:get("\111\114\100\101\114")end table.sort(M.menuIcons,U)for b=1,L,1 do local a=M.menuIcons[b]local V=a:get("\105\099\111\110\083\105\122\101")local F=V.X.Offset+Q if b<=f then I=I+F end if b==f and b~=L then I=I-2 end O=O+F local x=V.Y.Offset if x>H then H=x end local U=M.menuIcons[b-1]local P=M.menuIcons[b+1]a.instances.iconButton.NextSelectionRight=U and U.instances.iconButton a.instances.iconButton.NextSelectionLeft=P and P.instances.iconButton end local P=f==L and 0 or O+Q M:set("\109\101\110\117\067\097\110\118\097\115\083\105\122\101",UDim2.new(0,P,0,0))M:set("\109\101\110\117\083\105\122\101",UDim2.new(0,I,0,(H+b.scrollBarThickness)+3))local i={left={containerAnchorPoint=Vector2.new(1,0),containerPosition=UDim2.new(0,-4,0,0);canvasPosition=Vector2.new(P,0)};right={containerAnchorPoint=Vector2.new(0,0);containerPosition=UDim2.new(1,Q-2,0,0);canvasPosition=Vector2.new(0,0)}}local q=i[x]a.AnchorPoint=q.containerAnchorPoint a.Position=q.containerPosition V.CanvasPosition=q.canvasPosition M._menuCanvasPos=q.canvasPosition F.Padding=UDim.new(0,Q)end function Icon._menuIgnoreClipping(M)M:_ignoreClipping("\109\101\110\117")end

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


