Progress bar and dockable panels

Find out why the . goes before the /

Moderator: Paul Tuersley

Post Reply
Simma
Posts: 98
Joined: June 8th, 2010, 2:57 pm

I added a progress bar to a script, and everything seemed to work as expected, running it as a Window. However, as I tried to run in as a dockable panel I get an error saying I can't call update(). Am I missing something obvious here or can this not be done? Attached is an example script.

Code: Select all

function tmpScript(thisObj){
	
	function tmpScript_mainUI(thisObj) {

        var pal = (thisObj instanceof Panel) ? thisObj : new Window("palette", "tmp", undefined);
		if(pal != null){
			var res =
			"Group { \
			orientation: 'column', \
			alignment: ['left', 'top'], \
			one: Group { \
				orientation: 'row', \
				progressBar: Progressbar{text:'Progressbar Name', minvalue:0, maxvalue:100},\
				button: Button {text: 'Button', size: ['120', '25']}, \
				} \
			}";
			
			pal.grp = pal.add(res);
			pal.layout.layout(true);
			pal.layout.resize()
			pal.resizing = pal.onResize = function () {this.layout.resize()};
			
			pal.grp.one.button.onClick = function () {
				example(pal);
				}
			
			return pal;
			
			} // if (pal != null) {
		
		} // function tmpScript_mainUI(thisObj) {
	
	function example(pal){
		
		var min = 0;
		var max = 1000;
		
		pal.grp.one.progressBar.minvalue = min;
		pal.grp.one.progressBar.maxvalue = max;
		
		for(var i = min; i<=max;i++){
			pal.grp.one.progressBar.value = i;
			pal.update();
			}
		alert("Done! I will now reset the progress bar.");
		
		pal.grp.one.progressBar.value = 0
		pal.update();
		
		} // function example(pal){
	
	// Run main UI.
	var myPalette = tmpScript_mainUI(thisObj);
	if (myPalette != null && myPalette instanceof Window) {
		myPalette.show()
		myPalette.show();
		}

	} // function tmpScript(thisObj){

// Run tmpScript.
tmpScript(this);
Simma
Posts: 98
Joined: June 8th, 2010, 2:57 pm

I still haven't found a solution for this. Maybe someone knows of an example script that could point me in the right direction?
chriskelley
Posts: 4
Joined: August 1st, 2012, 5:22 pm

Hey Simma -

The unexpected behavior is happening because the Window object has an update() method, but the Panel object does not. So when you run as a Window, everything is fine - but when you load your script as a dockable Panel, you lose the update() method.

The good news is that you don't need to use update() at all when running as a Panel. You can test this in your script by changing your loop to this:

Code: Select all

for(var i = min; i<=max;i++){
         pal.grp.one.progressBar.value = i;
         //pal.update();
         $.sleep(1); // Wait a little between each loop to show the progress bar changing
}
If you need to run your script in both situations, you could prototype a dummy update() method into the Panel object to avoid the error, by putting this at the top of your script:

Code: Select all

Panel.prototype.update = function() {};


Alternatively you could test for what type of object you are in and handle each case individually

Code: Select all

if( pal instanceof Window ){
 pal.update();
}

Good Luck!
Chris Kelley
Simma
Posts: 98
Joined: June 8th, 2010, 2:57 pm

Thanks for your reply Chris!

I've tested you suggestion and it does indeed work. However, the progress bar isn't updating for each value, it jumps from min to max value as it's finished. Is this the expected behavior? I've tried longer sleeps, but it had no effect. I'm running CC on Windows.

Cheers.
Post Reply