Just wrote my first gadget.

I have been doing software development professionally and personally so I decided to check out what this new gadget thing for Vista is. Well I finished up my first gadget which is just a simple combination of javascript and css. The link is as follows for the zip file that contains the gadget: http://www.anthonyw.net/SystemInfo.gadget.zip

Additionally I have done some things with enumerating CPUs so that if anyone makes a call to System.Machine.CPUs they can process all of them. The following code is easy to follow.

<script type="text/javascript">

var compName = "Your computer is: " + System.Environment.getEnvironmentVariable("COMPUTERNAME") + "\n";
var availMem = "Available memory: " + System.Machine.availableMemory + "mb\n";
var procType = "Processor Type: " + System.Machine.processorArchitecture + "\n";
var cpuObj = System.Machine.CPUs;
var cpuCount = System.Machine.CPUs.count;
var cpuStats = "Processor count: " + cpuCount + "\n";
var cpuTemp = "";
var procStat = "";
var intervalID;

function getInfo()
{
intervalID = setInterval(refresh, 1000);
}

function refresh()
{
availMem = "Available memory: " + System.Machine.availableMemory + "mb\n"
for(var i = 0; i < cpuCount; i++)
{
cpuTemp = cpuObj.item(i);
var utilization = cpuTemp.usagePercentage;
procStat = procStat + "CPU " + (i+1) + ": " + Math.round(utilization) + "%\n";
}

gadgetContent.innerText = compName + availMem + procType + cpuStats + procStat;
procStat = "";
}

</script>
</head>

<body onload="getInfo()">
<div id="gadgetContent"></div>
</body>




Answer this question

Just wrote my first gadget.

  • alphaone

    Well I have only coded in javascript for about a period of 4 hours now since yesterday.  Is thare a set of commands such that i can do a boolean test   Persay isFocused

     

    I acutally found an answer, the pages is located here:

    http://blogs.msdn.com/sidebar/archive/2006/08/18/706495.aspx - Brian T's page.



  • MuscleHead

    Oh, that's bad, we want those documented and used! Thanks for mentioning that they went away, we'll get them back up soon.

    Brian


  • ar_pad

    If anyone is looking for an example of how to use the above documented features, this is how I decided to implement it in my gadget:

    <script type="text/javascript">

    var compName = null; //computer name
    var availMem = null; //available memory
    var procType = null; //processor type
    var cpuObj = null; // cpuObjects
    var cpuCount = null; // number of processors
    var cpuStats = null; // count string
    var cpuTemp = null; // temp variable
    var procStat = null; // processor stats
    var intervalID = null; // timer id
    var visible = null;

    function initialize()
    {
    //do some initialization stuff
    compName = "Your computer is: " + System.Environment.getEnvironmentVariable("COMPUTERNAME") + "\n";
    availMem = "Available memory: " + System.Machine.availableMemory + "mb\n";
    procType = "Processor Type: " + System.Machine.processorArchitecture + "\n";
    cpuObj = System.Machine.CPUs;
    cpuCount = System.Machine.CPUs.count;
    cpuStats = "Processor count: " + cpuCount + "\n";
    cpuTemp = "";
    procStat = "";

    for(var i = 0; i < cpuCount; i++)
    {
    cpuTemp = cpuObj.item(i);
    var utilization = cpuTemp.usagePercentage;
    procStat = procStat + "CPU " + (i+1) + ": " + Math.round(utilization) + "%\n";
    }

    gadgetContent.innerText = compName + availMem + procType + cpuStats + procStat;
    procStat = "";

    System.Gadget.visibilityChanged = isVisible;
    intervalID = setInterval(update, 1000);

    }

    function isVisible()
    {
    if (System.Gadget.visible)
    {
    visible = true;
    }
    else
    {
    visible = false;
    }
    }

    function update()
    {
    if (visible)
    {
    // perform updates here
    isDirty = false;
    availMem = "Available memory: " + System.Machine.availableMemory + "mb\n"
    for(var i = 0; i < cpuCount; i++)
    {
    cpuTemp = cpuObj.item(i);
    var utilization = cpuTemp.usagePercentage;
    procStat = procStat + "CPU " + (i+1) + ": " + Math.round(utilization) + "%\n";
    }

    gadgetContent.innerText = compName + availMem + procType + cpuStats + procStat;
    procStat = "";
    isDirty = true;
    }
    }



  • programmer01

    You're still firing a timer and running a little javascript every second; try this:

    window.setTimeout(refresh, 1000);

    var timerRunning = true;

    function refresh()

    {

    timerRunning = false;

    if (!System.Gadget.visible)

    {

    return;

    }

    // refresh your data and UI here

    window.setTimeout(refresh, 1000);

    timerRunning = true;

    }

    System.Gadget.visibilityChanged = function ()

    {

    if (System.Gadget.visible && !timerRunning)

    {

    window.setTimeout(refresh, 1000);

    }

    }


  • Grant Holliday

    You need to use two undocumented Gadget functions! They were there during the beta, but seem to have disappeared from the MSDN documentation recently. I've recreated them below for you.


    visibilityChanged Event


    This event is triggered when the gadget visibilty has changed, and will run the specified script code when the event occurs.

    Syntax

    Event property System.Gadget.visibilityChanged = handler

    Event Handler Parameters

    handler Required. A function containing code to run when a gadget visibility changes.




    visible Property


    Called from within a gadget as a means to determine whether or not the gadget is visible.

    Syntax

    [ bIsVisible = ] System.Gadget.visible

    Possible Values

    bIsVisible The visible status of the gadget, stored as a Boolean value. Returns true if visible, and false if hidden.

    The property is read-only. The property has no default value.

    Example

    System.Gadget.visibilityChanged = areWeVisible;

    function areWeVisible()
    {
    if (System.Gadget.visible)
    {
    //Gadget now visible
    } else {
    //Gadget now hidden
    }
    }

  • DavidR100

    Okay I see the logic it just wasn't clear before, the logic for "function()" is if the window is visible and the timer wasn't running run it now. But the change in visibility is dependent upon the refresh function at least running once to make sure that if the gadget is visible we do our updating clear the timer and reset it to a new one otherwise if the window is not visible do nothing.

    Now since this polls every second it def did eat cpu cycles in the background but that has now changed.

    Thanks


  • Ariadne22

    Its a good start! One suggestion - stop polling every second if your gadget isn't visible, and start again when it becomes visible again.
  • Just wrote my first gadget.