Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Combo popup size and position #91

Merged
merged 8 commits into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/WidgetTypes.lua
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,8 @@ export type Combo = ParentWidget & {
index: State<any>,
isOpened: State<boolean>,
},

UIListLayout: UIListLayout,
} & Opened & Closed & Clicked & Hovered

-- Plot
Expand Down
6 changes: 6 additions & 0 deletions lib/demoWindow.lua
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,12 @@ return function(Iris: Types.Iris)

Iris.ComboArray({ "Using ComboArray" }, { index = "No Selection" }, { "Red", "Green", "Blue" })

local heightTestArray = {}
for i = 1, 50 do
table.insert(heightTestArray, tostring(i))
end
Iris.ComboArray({ "Height Test" }, { index = "1" }, heightTestArray)

local sharedComboIndex2 = Iris.State("7 AM")

Iris.Combo({ "Combo with Inner widgets" }, { index = sharedComboIndex2 })
Expand Down
34 changes: 26 additions & 8 deletions lib/widgets/Combo.lua
Original file line number Diff line number Diff line change
Expand Up @@ -130,34 +130,52 @@ return function(Iris: Types.Internal, widgets: Types.WidgetUtility)
local AnyOpenedCombo: boolean = false
local ComboOpenedTick: number = -1
local OpenedCombo: Types.Combo? = nil
local CachedContentSize: number = 0

local function UpdateChildContainerTransform(thisWidget: Types.Combo)
local Combo = thisWidget.Instance :: Frame
local PreviewContainer = Combo.PreviewContainer :: TextButton
local ChildContainer = thisWidget.ChildContainer :: ScrollingFrame

ChildContainer.Size = UDim2.fromOffset(PreviewContainer.AbsoluteSize.X, 0)

local previewPosition: Vector2 = PreviewContainer.AbsolutePosition - widgets.GuiOffset
local previewSize: Vector2 = PreviewContainer.AbsoluteSize
local containerSize: Vector2 = ChildContainer.AbsoluteSize
local borderSize: number = Iris._config.PopupBorderSize
local screenSize: Vector2 = ChildContainer.Parent.AbsoluteSize

local absoluteContentSize = thisWidget.UIListLayout.AbsoluteContentSize.Y
CachedContentSize = absoluteContentSize

local contentsSize: number = absoluteContentSize + 2 * Iris._config.WindowPadding.Y

local x: number = previewPosition.X
local y: number
local y: number = previewPosition.Y + previewSize.Y + borderSize
local anchor: Vector2 = Vector2.zero
local distanceToScreen: number = screenSize.Y - y

if previewPosition.Y + containerSize.Y > screenSize.Y then
-- Only extend upwards if we cannot fully extend downwards, and we are on the bottom half of the screen.
-- i.e. there is more space upwards than there is downwards.
if contentsSize > distanceToScreen and y > (screenSize.Y / 2) then
y = previewPosition.Y - borderSize
anchor = Vector2.yAxis
else
y = previewPosition.Y + previewSize.Y + borderSize
distanceToScreen = y -- from 0 to the current position
end

ChildContainer.AnchorPoint = anchor
ChildContainer.Position = UDim2.fromOffset(x, y)

local height = math.min(contentsSize, distanceToScreen)
ChildContainer.Size = UDim2.fromOffset(PreviewContainer.AbsoluteSize.X, height)
end

table.insert(Iris._postCycleCallbacks, function()
if AnyOpenedCombo and OpenedCombo then
local contentSize = OpenedCombo.UIListLayout.AbsoluteContentSize.Y
if contentSize ~= CachedContentSize then
UpdateChildContainerTransform(OpenedCombo)
end
end
end)

local function UpdateComboState(input: InputObject)
if not Iris._started then
return
Expand Down Expand Up @@ -340,7 +358,6 @@ return function(Iris: Types.Internal, widgets: Types.WidgetUtility)

local ChildContainer: ScrollingFrame = Instance.new("ScrollingFrame")
ChildContainer.Name = "ComboContainer"
ChildContainer.AutomaticSize = Enum.AutomaticSize.Y
ChildContainer.BackgroundColor3 = Iris._config.PopupBgColor
ChildContainer.BackgroundTransparency = Iris._config.PopupBgTransparency
ChildContainer.BorderSizePixel = 0
Expand Down Expand Up @@ -374,6 +391,7 @@ return function(Iris: Types.Internal, widgets: Types.WidgetUtility)
ChildContainer.Parent = RootPopupScreenGui

thisWidget.ChildContainer = ChildContainer
thisWidget.UIListLayout = ChildContainerUIListLayout
return Combo
end,
Update = function(thisWidget: Types.Combo)
Expand Down
Loading