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


