Spiderman-3

<HTML>
<HEAD>
<TITLE>Test</TITLE>
</HEAD>
<STYLE>
body{
width: 130px;
height: 76px;
margin: 0px;
}
div{
font-family: 'Segoe UI';
font-size: 15px;
color: white;
}
</STYLE>
<script type="text/javascript" language="javascript">
var txt = "";
var visChgTO = null;

System.Gadget.visibilityChanged=checkVisibility;

function checkVisibility(){
clearTimeout(visChgTO);
visChgTO = null;
var isVisible = System.Gadget.visible;

if (isVisible){
txt = txt + "v";
}else{
txt = txt + "n";
}

visChgTO = setTimeout('txtSpace()',500);
}

function txtSpace(){
txt = txt + " ";
lbl.innerHTML = txt;
}
</script>
<BODY>
<g:background id="bg" src="bg.png" style="width:130px;height:76px"/>
<div id="lbl" style="position:absolute;top:8px;left:15px;width:110px"></div>
</BODY>
</HTML>


The Event fires:
visible - not visible - visible @ Install of the Gadget
not visible - not visible @ hide Sidebar
not visible - not visible - visible @ show Sidebar

Multiple different Events for a single Action, that's a dirty Event System.
I think we must clean it with a timeout, see the Example Code.

Only the last Event from the multiple Events is the right Event.



Re: Sidebar Gadget Development visibilityChanged

Jonathan Abbott

This is normal behaviour, onDock/onUndock can also fire multiple times. I'm sure that at one point this was documented on MSDN.




Re: Sidebar Gadget Development visibilityChanged

Spiderman-3

Hi Jonathan,

i think that's dirty and not good.

Why first fires two "not visible" Events and than the right "visible" Event, when I click "Show Sidebar"

Each Gadget received the "visibilityChanged" Event, and do a Action.
This can result in Problems, when the Action will save Settings.

Greets
Spiderman




Re: Sidebar Gadget Development visibilityChanged

Jonathan Abbott

It's not ideal!

It's not normally an issue as it happens so quickly, but if needed you can work around it by checking if the state has actually changed, and using a timer event to trap misfires. It's a bit of a botch though:


Code Snippet

System.Gadget.visibilityChanged = checkVisibility;
var visibilityState = System.Gadget.visible;
var visibilityChangeEvent;

function checkVisibility() {
//visibility has changed, catch multiple misfires
try{window.clearTimeout(visibilityChangeEvent)} catch(err) {}
visibilityChangeEvent = window.setTimeout(checkVisibilityChanged, 300);
}

function checkVisibilityChanged() {
if (!System.Gadget.visible == visibilityState)
{
visibilityState = System.Gadget.visible;
// visilibity changed
}
}






Re: Sidebar Gadget Development visibilityChanged

Spiderman-3

@ Jonathan

Hi,

the Code works fine, after I fill in the missing "i" in function checkVisibility().

Next Problem, the Hide Desktop Icon, also Keys WIN + D, hide/show the Sidebar, but no Event is firing.

Bug #1001

Greets
Spiderman





Re: Sidebar Gadget Development visibilityChanged

Jonathan Abbott

Missing "i" corrected

The event also doesn't fire if the screen the Gadget is on, is taken over by a full screen DirectX app (ie games).

I think it only fires when the Gadged is hidden whilst docked on the Sidebar, the screen saver kicks in, or the screen goes into power save. Don't quote me on that though.

Microsoft have never documented what actually fires the event, although Brian Teutsch (from the Sidebar dev team) did hint at it in this thread.