Losing the handle to my web dialog on onOpenModel


#1

I have a bit of a problem with losing the connection to my web dialog. My web dialog is using a SelectionObserver to track what is being selected.

However, when I open a new model… I can no longer get the handle to the dialog window.

So here in this code…

class MySelectionObserver < Sketchup::SelectionObserver
		def myDialog dialog
			@gp = dialog
		end
		
		def onSelectionBulkChange(selection)
			if @gp.dialog.visible?
				@gp.update()
			end
		end
		
		def onSelectionCleared(selection)
			if @gp.dialog.visible?
				@gp.update()
			end
		end
	end

	class MyAppObserver < Sketchup::AppObserver
	   		
		def onOpenModel(model)
			if @gp != nil
				// This block is where I would reinitialize my web dialog, however it never gets called as @gp is always nil....
			else 
				puts "can't find it... the web dialog is now unresponsive..."
			end
		end
	end
	Sketchup.add_observer(MyAppObserver.new)

Thoughts on how I can maintain a persistent handle to a web dialog with a SelectionObserver?


#2

Nintari,

The code sample you’ve listed does not show when you attach the SelectionObserver (MySelectionObserver).

Hence, I’m not really sure where the issue is.

Regardless, you need to re-attach it anytime the onOpenModel or onNewModel events fire in the AppObserver. Something like –

Sketchup.active_model.selection.add_observer(obs_sel)

where obs_sel is the instance of the MySelectionObserver class.

Greg


#3

Ah sorry-- yes I attach it in the initialize function of my web dialog:

select = MySelectionObserver.new
Sketchup.active_model.selection.add_observer(select)
select.myDialog(self)

So when I initialize the web dialog, it’s storing the handle for the dialog into @gp. However it seems to clear it when I open a new file.


#4

Nintari,

Without testing some code myself…

[quote=“nintari, post:3, topic:17483”]```ruby
select = MySelectionObserver.new
Sketchup.active_model.selection.add_observer(select)
select.myDialog(self)


You've listed a local variable named 'select'.  If it's not an instance variable (@select), when the active model changes, the previous active model goes out of scope, and it's reference to 'select' goes away.

Does it work if you make 'select' into an instance variable, as in '@select'?

Of course, I assume you're re-attaching it to the new model, as I mentioned previously...

Greg

#5

Thanks for your help–I figured it out. Essentially I had to find the right place to find the variable to be re-attached to the model. I had to get it from the point where I spawned the web dialog rather than in the initialization of said web dialog.