Skip to content
This repository has been archived by the owner on Jul 16, 2024. It is now read-only.

Commit

Permalink
Add hydrateAutomaticSize
Browse files Browse the repository at this point in the history
  • Loading branch information
evaera committed Jul 2, 2022
1 parent d0b6de6 commit 2ded2ab
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
26 changes: 25 additions & 1 deletion src/automaticSize.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
local RunService = game:GetService("RunService")
local CollectionService = game:GetService("CollectionService")
local function applyLayout(container, layout, options)
local axis = options.axis or Enum.AutomaticSize.XY
local maxSize = options.maxSize or Vector2.new(math.huge, math.huge)
Expand Down Expand Up @@ -97,17 +99,39 @@ local defaultOptions = {}
@within Plasma
@function automaticSize
@param container GuiObject -- The instance to apply automatic sizing to.
@param options { axis: Enum.AutomaticSize, maxSize: Vector2} | nil
@param options { axis: Enum.AutomaticSize, maxSize: Vector2 | UDim2} | nil
@tag utilities
Applies padding-aware automatic size to the given GUI instance. This function sets up events to listen to further changes, so
should only be called once per object.
Also supports ScrollingFrames by correctly clamping actual and canvas sizes.
:::note
Automatic sizing cannot be applied on the server because clients have differing screen sizes.
If this function is called from the server, it instead configures the instance to be compatible with the
[Plasma.hydrateAutomaticSize] function, adding the CollectionService tag and other attributes.
You must also call `hydrateAutomaticSize` once on the client for this to work.
:::
]=]
local function automaticSize(container, options)
options = options or defaultOptions

if RunService:IsServer() then
if options.maxSize then
container:SetAttribute("maxSize", options.maxSize)
end
if options.axis then
container:SetAttribute("axis", options.axis.Name)
end

CollectionService:AddTag(container, "PlasmaAutomaticSize")

return
end

local layout = container:FindFirstChildWhichIsA("UIGridStyleLayout")

applyLayout(container, layout, options)
Expand Down
32 changes: 32 additions & 0 deletions src/hydrateAutomaticSize.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
local CollectionService = game:GetService("CollectionService")
local automaticSize = require(script.Parent.automaticSize)

local function hydrate(instance)
local axisName = instance:GetAttribute("axis")

automaticSize(instance, {
maxSize = instance:GetAttribute("maxSize"),
axis = if axisName then Enum.AutomaticSize[axisName] else nil,
})
end

--[=[
Applies automatic sizing to any current or future instances in the DataModel that are tagged with
`"PlasmaAutomaticSize"`. Attributes `axis` (string) and `maxSize` (UDim2 or Vector2) are allowed.
@within Plasma
@tag utilities
@client
@return RBXScriptConnection
]=]
local function hydrateAutomaticSize()
for _, instance in CollectionService:GetTagged("PlasmaAutomaticSize") do
hydrate(instance)
end

return CollectionService:GetInstanceAddedSignal("PlasmaAutomaticSize"):Connect(function(instance)
task.defer(hydrate, instance) -- instance added signal fires before children are added
end)
end

return hydrateAutomaticSize
1 change: 1 addition & 0 deletions src/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ return {
setStyle = Style.set,

automaticSize = require(script.automaticSize),
hydrateAutomaticSize = require(script.hydrateAutomaticSize),
create = require(script.create),

window = require(script.widgets.window),
Expand Down

0 comments on commit 2ded2ab

Please sign in to comment.