Shared Toolbar Plugins

How can I make a shared toolbar for my plugins?

Example:

You simply add another plugin button line

local toolBar = plugin:CreateToolbar("Toolbar Name")
local pluginButton = toolBar:CreateButton("", "Title", "rbxassetid://")
local pluginButton2 = toolBar:CreateButton("", "Title2", "rbxassetid://")

image

No what I mean is to different plugins like how GapFill and ResizeAlign are different plugins but share the same toolbar.

Yea, I realized when I just posted it.
However, I looked at the plugin’s code, and this is what they seem to do:
Looks a bit complicated though

Set up
local On, Off;

-- create the plugin and toolbar, and connect them to the On/Off activation functions
plugin.Deactivation:Connect(function()
	Off()
end)

local sharedToolbarSettings = {} :: createSharedToolbar.SharedToolbarSettings
sharedToolbarSettings.CombinerName = "GeomToolsToolbar"
sharedToolbarSettings.ToolbarName = "GeomTools"
sharedToolbarSettings.ButtonName = "GapFill"
sharedToolbarSettings.ButtonIcon = "rbxassetid://4521972465"
sharedToolbarSettings.ButtonTooltip = "Generate geometry filling the space between two selected part edges."
sharedToolbarSettings.ClickedFn = function()
	if on then
		deactivatingEvent:Fire()
		Off()
	elseif loaded then
		On()
	end
end
createSharedToolbar(plugin, sharedToolbarSettings)

-- Run when the popup is activated.
function On()
	plugin:Activate(true)
	sharedToolbarSettings.Button:SetActive(true)
	on = true
	mouse = plugin:GetMouse(true)
	table.insert(mouseCnList, mouse.Button1Down:connect(function()
		MouseDown()
	end))
	table.insert(mouseCnList, mouse.Button1Up:connect(function()
		MouseUp()
	end))
	table.insert(mouseCnList, mouse.Move:connect(function()
		MouseMove()
	end))
	table.insert(mouseCnList, mouse.Idle:connect(function()
		MouseIdle()
	end))
	table.insert(mouseCnList, mouse.KeyDown:connect(function()
		KeyDown()
	end))
	--
	Selected()
end

-- Run when the popup is deactivated.
function Off()
	draggerHandler:disable()
	sharedToolbarSettings.Button:SetActive(false)
	on = false
	for i, cn in pairs(mouseCnList) do
		cn:disconnect()
		mouseCnList[i] = nil
	end
	--
	Deselected()
end
From a module called 'create-shared-toolbar'

local CoreGui = game:GetService("CoreGui")

export type SharedToolbarSettings = {
	ButtonName: string,
	ButtonIcon: string,
	ButtonTooltip: string,
	ToolbarName: string,
	CombinerName: string,
	ClickedFn: () -> (),
	Button: PluginToolbarButton?,
}

return function(plugin: Plugin, set: SharedToolbarSettings)
	local combiner = CoreGui:FindFirstChild(set.CombinerName)
	if not combiner then
		combiner = Instance.new("ObjectValue")
		combiner.Name = set.CombinerName
		combiner.Parent = CoreGui
	end
	local owner = combiner:FindFirstChild("Owner")
	if not owner then
		owner = Instance.new("ObjectValue")
		owner.Name = "Owner"
		owner.Parent = combiner
	end

	local buttonCn;
	local function createButton(toolbar: PluginToolbar)
		if buttonCn then
			buttonCn:Disconnect()
		end
		local buttonRef = combiner:FindFirstChild(set.ButtonName)
		if not buttonRef then
			buttonRef = Instance.new("ObjectValue")
			buttonRef.Name = set.ButtonName
			buttonRef.Value = toolbar:CreateButton(set.ButtonName, set.ButtonTooltip, set.ButtonIcon)
			buttonRef.Value.Name = plugin.Name .. "_" .. set.ButtonName
			buttonRef.Parent = combiner
		end
		buttonCn = buttonRef.Value.Click:Connect(set.ClickedFn)
		set.Button = buttonRef.Value
	end

	-- Initial toolbar handling... create or use
	do
		local toolbar = combiner.Value
		if not toolbar then
			toolbar = plugin:CreateToolbar(set.ToolbarName)
			combiner.Value = toolbar
			owner.Value = plugin
		end
		createButton(toolbar)
	end

	-- Handle unloading plugins
	local ownerChangedConnection = owner:GetPropertyChangedSignal("Value"):Connect(function()
		task.delay(0.5, function()
			if not owner.Value then
				local toolbar = plugin:CreateToolbar(set.ToolbarName)
				toolbar.Name = plugin.Name .. "_Toolbar"
				combiner.Value = toolbar
				owner.Value = plugin
			elseif combiner.Value then
				createButton(combiner.Value)
			end
		end)
	end)

	-- Handle our own unloading
	local unloadConnection;
	unloadConnection = plugin.Unloading:Connect(function()
		unloadConnection:Disconnect()
		ownerChangedConnection:Disconnect()
		if buttonCn then
			buttonCn:Disconnect()
		end
		if owner.Value == plugin then
			-- Clear the button refs
			for _, ch in combiner:GetChildren() do
				if ch ~= owner then
					ch:Destroy()
				end
			end
			combiner.Value = nil
			owner.Value = nil
		end
	end)
end

1 Like

Yeah, I also saw that I was just seeing if there is a easier way of making a shared toolbar.

stravant/createSharedToolbar: Roblox utility for creating a Toolbar shared between multiple plugins

1 Like