Detect when widget has been closed

Hello there.

I am currently working on a plugin. For what I am working on I am trying to detect when the user closes the widget however I can’t seem to find a function that represents this, or am I missing something? I am currently focusing on this api reference on WidgetPluginGUI which I think has a typo in the title.

The reason I am trying to do this is so I can detect when the user closes the Widget and then my script will destroy it so that it can be created again when needed.

Thank you in advance for any help. ICrann

2 Likes

Check the state of the widget like this:

  local WidgetGui = plugin:CreateDockWidgetPluginGui("TestWidget", widgetInfo)
  if WidgetGui.Enabled == false then end

You can check whether it is enabled or not, there’s no event though, unless you use Changed maybe?

Bind it to the function checking whether it’s enabled.

1 Like

Unfortunately this does not work, I tried it with a simple print statement and it never got printed.

You should be able to via :GetPropertyChangedSignal, but I’m not entirely sure.

You can probably do this:

WidgetGui:GetPropertyChangedSignal("Enabled"):Connect(function()

end)

Initial issue might be because the if enabled ... then statement ran instantly as the code did, not when you actually closed it.

1 Like

This should work for your case

 widget:GetPropertyChangedSignal("Enabled"):Connect(function()
 if widget.Enabled == false then
    print("Widget closed") 
    end
 end)

Just detect when the Enabled property changes, if it equates to false then it means the widget was closed.

2 Likes

The proper way to do it is:

Widget:BindToClose(function() 
   Widget.Enabled = false 
   print("Widget closed")
end)
4 Likes