Prevent multiple script UI to be launched

User avatar
Movalex
Fusioneer
Posts: 125
Joined: Fri Nov 03, 2017 5:36 am
Answers: 2
Been thanked: 32 times
Contact:

Prevent multiple script UI to be launched

#1

Post by Movalex » Tue Jun 18, 2019 11:06 pm

Hi!
1) Is it possible to get to know if script UI is already launched and do not show the same window second time? For instance, we can launch multiple Reactor UI or About window, which is probably has to be avoided.
2) How would you create a button that both launch additional disp:AddWindow UI and also close it? Like, when you toggle 'About window' to show about information and also to close it?
This is what I'm looking for to implement:
Image
by PeterLoveday » Wed Jun 19, 2019 1:47 am
If you're doing it all from within one script, then you don't really need to go to the trouble of querying globally for windows by ID:

Code: Select all

ui = app.UIManager

disp = bmd.UIDispatcher(ui)



local win = disp:AddWindow({

ID = "WindowTest_Main",

WindowTitle = "Window Test",


ui:VGroup {
ui:Button{
ID = 'LaunchPrefs',
Text = 'prefs',
Flat = true,
Checkable = false,
},
},

})

local prefs_dlg = nil

function toggle_prefs()
if prefs_dlg then
prefs_dlg:Hide()
prefs_dlg = nil
-- collectgarbage()
else
launch_prefs()
end
end


function launch_prefs()
prefs_dlg = disp:AddWindow({
ID = 'TBPrefs',
TargetID = 'TBPrefs',
-- WindowTitle = 'Toolbar16 Prefs',
WindowFlags = {SplashScreen = true, NoDropShadowWindowHint = true, WindowStaysOnTopHint = false},
Geometry = {300, 200, 200, 100},
ui:VGroup{
ID = 'prefs',
ui:HGroup{
ui:Button{
ID = 'FlushData',
Text = 'Flush',
},
},
ui:HGroup{
ui:Button{
ID = 'PrefClose',
Text = 'close',
Flat = true,
},
},
},
})

function prefs_dlg.On.PrefClose.Clicked(ev)
toggle_prefs()
end

prefs_dlg:Show()
end

function win.On.LaunchPrefs.Clicked(ev)
toggle_prefs()
end


function win.On.WindowTest_Main.Close(ev)
disp:ExitLoop()
end


win:Show()

disp:RunLoop()
Go to full post

User avatar
PeterLoveday
Fusioneer
Posts: 155
Joined: Sun Sep 14, 2014 6:09 pm
Answers: 6
Been thanked: 17 times

Re: Prevent multiple script UI to be launched

#2

Post by PeterLoveday » Wed Jun 19, 2019 12:10 am

Something like this should give you some pointers:
Code: [Select all] [Expand/Collapse] [Download] (WindowTest.lua)
  1. ui = app.UIManager
  2. disp = bmd.UIDispatcher(ui)
  3.  
  4. -- If we already have a window open, do not open another, activate it and return
  5. local mywin = ui:FindWindow("WindowTest_Main")
  6. if mywin then
  7.     mywin:Raise()
  8.     mywin:ActivateWindow()
  9.  
  10.     -- Uncomment to toggle, by sending a close event
  11.     --mywin:Close()
  12.     return
  13. end
  14.  
  15. local win = disp:AddWindow({
  16.     ID = "WindowTest_Main",
  17.     WindowTitle = "Window Test",
  18.  
  19.     ui:VGroup {
  20.         ui:Label {
  21.             ID = "Label",
  22.             Text = "Window Test",
  23.             ReadOnly = true,
  24.             Alignment = {
  25.                 AlignHCenter = true,
  26.                 AlignVCenter = true,
  27.             },
  28.         },
  29.     },
  30. })
  31.  
  32. function win.On.WindowTest_Main.Close(ev)
  33.     disp:ExitLoop()
  34. end
  35.  
  36. win:Show()
  37. disp:RunLoop()

This would allow what would be needed for the mentioned changes to Reactor... however, in the case shown by the image, wouldn't it be running in the same script, and so could just check a variable?

User avatar
Movalex
Fusioneer
Posts: 125
Joined: Fri Nov 03, 2017 5:36 am
Answers: 2
Been thanked: 32 times
Contact:

Re: Prevent multiple script UI to be launched

#3

Post by Movalex » Wed Jun 19, 2019 12:48 am

Wow, thanks!
But how should I delete window object after I close the window?
Next time I press the button, mywin variable will always be true, since it is not deleted automatically.

User avatar
PeterLoveday
Fusioneer
Posts: 155
Joined: Sun Sep 14, 2014 6:09 pm
Answers: 6
Been thanked: 17 times

Re: Prevent multiple script UI to be launched

#4

Post by PeterLoveday » Wed Jun 19, 2019 12:53 am

It should be being deleted when the script exits.

If you're not seeing this, then try setting the vars (mywin and win) to nil, then collectgarbage(), and see if that forces it.

User avatar
Movalex
Fusioneer
Posts: 125
Joined: Fri Nov 03, 2017 5:36 am
Answers: 2
Been thanked: 32 times
Contact:

Re: Prevent multiple script UI to be launched

#5

Post by Movalex » Wed Jun 19, 2019 1:32 am

In my case I want to launch another UI from the existing one. So we are not closing the main UI, therefore mywin will not be garbagecollected.
Here's briefly what I'm talking about:
  1. ui = app.UIManager
  2.  
  3. disp = bmd.UIDispatcher(ui)
  4.  
  5.  
  6.  
  7. local win = disp:AddWindow({
  8.  
  9.     ID = "WindowTest_Main",
  10.  
  11.     WindowTitle = "Window Test",
  12.  
  13.  
  14.     ui:VGroup {
  15.         ui:Button{
  16.             ID = 'LaunchPrefs',
  17.             Text = 'prefs',
  18.             Flat = true,
  19.             Checkable = false,
  20.         },
  21.     },
  22.  
  23. })
  24.  
  25. -- If we already have a window open, do not open another, activate it and return
  26.  
  27.  
  28. function lauch_prefs()
  29.     dlg = disp:AddWindow({
  30.         ID = 'TBPrefs',
  31.         TargetID = 'TBPrefs',
  32.         -- WindowTitle = 'Toolbar16 Prefs',
  33.         WindowFlags = {SplashScreen = true, NoDropShadowWindowHint = true, WindowStaysOnTopHint = false},
  34.         Geometry = {300, 200, 200, 100},
  35.             ui:VGroup{
  36.             ID = 'prefs',
  37.                 ui:HGroup{
  38.                     ui:Button{
  39.                         ID = 'FlushData',
  40.                         Text = 'Flush',
  41.                     },
  42.                 },
  43.                 ui:HGroup{
  44.                     ui:Button{
  45.                         ID = 'PrefClose',
  46.                         Text = 'close',
  47.                         Flat = true,
  48.                     },
  49.                 },
  50.             },
  51.         })
  52.     function dlg.On.PrefClose.Clicked(ev)
  53.         disp:ExitLoop()
  54.         collectgarbage()
  55.     end
  56.     dlg:Show()
  57.     disp:RunLoop()
  58.     dlg:Hide()
  59.  
  60. end
  61.  
  62.  
  63. function win.On.LaunchPrefs.Clicked(ev)
  64.     local mywin = ui:FindWindow("TBPrefs")
  65.     if mywin then
  66.         print(mywin)
  67.         mywin:Raise()
  68.         mywin:ActivateWindow()
  69.         -- Uncomment to toggle, by sending a close event
  70.         --mywin:Close()
  71.         return
  72.     else
  73.         lauch_prefs()
  74.     end
  75.  
  76. end
  77.  
  78.  
  79.  
  80. function win.On.WindowTest_Main.Close(ev)
  81.     disp:ExitLoop()
  82. end
  83.  
  84.  
  85. win:Show()
  86.  
  87. disp:RunLoop()

User avatar
PeterLoveday
Fusioneer
Posts: 155
Joined: Sun Sep 14, 2014 6:09 pm
Answers: 6
Been thanked: 17 times

Re: Prevent multiple script UI to be launched

#6

Post by PeterLoveday » Wed Jun 19, 2019 1:47 am

If you're doing it all from within one script, then you don't really need to go to the trouble of querying globally for windows by ID:
  1. ui = app.UIManager
  2.  
  3. disp = bmd.UIDispatcher(ui)
  4.  
  5.  
  6.  
  7. local win = disp:AddWindow({
  8.  
  9.     ID = "WindowTest_Main",
  10.  
  11.     WindowTitle = "Window Test",
  12.  
  13.  
  14.     ui:VGroup {
  15.         ui:Button{
  16.             ID = 'LaunchPrefs',
  17.             Text = 'prefs',
  18.             Flat = true,
  19.             Checkable = false,
  20.         },
  21.     },
  22.  
  23. })
  24.  
  25. local prefs_dlg = nil
  26.  
  27. function toggle_prefs()
  28.     if prefs_dlg then
  29.         prefs_dlg:Hide()
  30.         prefs_dlg = nil
  31.         -- collectgarbage()
  32.     else
  33.         launch_prefs()
  34.     end
  35. end
  36.  
  37.  
  38. function launch_prefs()
  39.     prefs_dlg = disp:AddWindow({
  40.         ID = 'TBPrefs',
  41.         TargetID = 'TBPrefs',
  42.         -- WindowTitle = 'Toolbar16 Prefs',
  43.         WindowFlags = {SplashScreen = true, NoDropShadowWindowHint = true, WindowStaysOnTopHint = false},
  44.         Geometry = {300, 200, 200, 100},
  45.             ui:VGroup{
  46.             ID = 'prefs',
  47.                 ui:HGroup{
  48.                     ui:Button{
  49.                         ID = 'FlushData',
  50.                         Text = 'Flush',
  51.                     },
  52.                 },
  53.                 ui:HGroup{
  54.                     ui:Button{
  55.                         ID = 'PrefClose',
  56.                         Text = 'close',
  57.                         Flat = true,
  58.                     },
  59.                 },
  60.             },
  61.         })
  62.  
  63.     function prefs_dlg.On.PrefClose.Clicked(ev)
  64.         toggle_prefs()
  65.     end
  66.  
  67.     prefs_dlg:Show()
  68. end
  69.  
  70. function win.On.LaunchPrefs.Clicked(ev)
  71.     toggle_prefs()
  72. end
  73.  
  74.  
  75. function win.On.WindowTest_Main.Close(ev)
  76.     disp:ExitLoop()
  77. end
  78.  
  79.  
  80. win:Show()
  81.  
  82. disp:RunLoop()