Skip to content

Instantly share code, notes, and snippets.

@mvyasu
Last active August 7, 2023 03:23
Show Gist options
  • Select an option

  • Save mvyasu/d6f3e67841a49f1438ae93f02b755f15 to your computer and use it in GitHub Desktop.

Select an option

Save mvyasu/d6f3e67841a49f1438ae93f02b755f15 to your computer and use it in GitHub Desktop.
A simple studio theme provider that can be used for plugins made with Fusion
--!strict
local Plugin = script:FindFirstAncestorWhichIsA("Plugin")
local Packages = script.Parent.Parent.Packages
local Fusion = require(Packages.Fusion)
local Studio = settings().Studio
local Computed = Fusion.Computed
local Value = Fusion.Value
local currentFontFamily = Font.fromEnum(Enum.Font.Gotham).Family
local currentStudioTheme = Studio.Theme :: StudioTheme
local themeProvider = {
Theme = Value(currentStudioTheme),
IsDark = Value(true),
}
local function unwrap(x: Fusion.CanBeState<any>, useDependency: boolean?): any
if typeof(x)=="table" and x.type=="State" then
return x:get(useDependency)
end
return x
end
type studioStyleGuideColor = Fusion.CanBeState<Enum.StudioStyleGuideColor>
type studioStyleGuideModifier = Fusion.CanBeState<Enum.StudioStyleGuideModifier>
function themeProvider:GetColor(providedColor: studioStyleGuideColor, providedModifier: studioStyleGuideModifier?): Fusion.Computed<Color3>
return Computed(function()
local currentTheme = unwrap(self.Theme)
local currentColor = unwrap(providedColor)
local currentModifier = unwrap(providedModifier)
return currentTheme:GetColor(currentColor, currentModifier)
end)
end
type fontWeight = Fusion.CanBeState<Enum.FontWeight>
type fontStyle = Fusion.CanBeState<Enum.FontStyle>
function themeProvider:GetFont(providedWeight: fontWeight?, providedStyle: fontStyle?): Fusion.Computed<Font>
return Computed(function()
local currentFontWeight = unwrap(providedWeight) or Enum.FontWeight.Regular
local currentFontStyle = unwrap(providedStyle) or Enum.FontStyle.Normal
return Font.new(currentFontFamily, currentFontWeight, currentFontStyle)
end)
end
local function updateTheme()
currentStudioTheme = Studio.Theme :: StudioTheme
themeProvider.Theme:set(currentStudioTheme)
local _, _, v = currentStudioTheme:GetColor(Enum.StudioStyleGuideColor.MainBackground):ToHSV()
themeProvider.IsDark:set(v<=0.6)
end
do --updates the theme color values when necessary
local themeChangedConnection = Studio.ThemeChanged:Connect(updateTheme)
updateTheme()
Plugin.Unloading:Once(function()
themeChangedConnection:Disconnect()
end)
end
return themeProvider
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment