Dealing with multiple gui windows in supercollider
Let’s say one has a data class MyCircle and a gui Class MyCircleGui which displays controls for this class. The gui is constructed by calling aMyCircle.edit;
MyCircle {
…
edit { ^MyCircleGui(this) }
}
Now, if I call .edit again on a MyCircle I don’t want it to construct a new gui window if there is one already open: one must keep track of which windows are open. My solution to this problem was to have an IdentityDictionary as a class variable where I add the window of a MyCircleGui when the class is created and delete it when the window is closed.
MyCircle {
classvar <>instances;
*new{ |aMyCircle|
instances = instances ?? {IdentityDictionary.new};
if(instances.keys.includes(aMyCircle).not){
var view,window,flow;
window = Window(…)
// add guis to window here.
window.front;
window.onClose_{instances.removeAt(aMyCircle)}; instances.put(aMyCircle,window);
}
{ instances[aMyCircle].front }
}
*closeAll{
instances.do{ |window| if(window.isClosed.not){window.close} }
}
…
edit { ^MyCircleGui(this) }
}
This takes care of the open windows, and if I want to close all the current open windows I just call MyCircleGui.closeAll.
Tags: coding, supercollider