Widget affecting screen positioning

Posted on
  • After a good deal of testing I have come to the conclusion that either I am handling widgets incorrectly in the app I am working on, or at least one widget (RAM Widget in this particular case) is misbehaving. The problem can be demonstrated with this simple code snippet:

    g.clear();
    Bangle.loadWidgets();
    Bangle.drawWidgets();
    g.drawString("hello world",64,64);

    The string "hello world" appears on the display somewhat to the left of the expected 64,64. If I comment out the Widgets lines, it appears at 64,64 just fine. Battery Level Widget (with percentage) and Bluetooth Widget don't cause this problem. So is it my code, or is it RAM Widget, or possibly even the firmware?

  • Most likely the RAM widget is changing the text alignment. Best practice is to call g.reset() before you start drawing in order to reset the graphics object to a known state.

  • As @NebbishHacker says:

    g.clear();
    Bangle.loadWidgets();
    Bangle.drawWidgets();
    g.reset(); // <--- here
    g.drawString("hello world",64,64);
    

    Widgets (or anything) may change text/colors/etc, so it's good practice to call .reset() in a function before you render to put everything back into a known state.

  • Thanks, and sure enough, I found a thread from a couple of months ago where the same problem was described and the g.reset() solution was advised, but isn't this actually a race condition? If a widget can call bangle.drawWidgets() itself, wouldn't I really need to call g.reset() before every single instance of g.drawString() etc., and even then, it would only maximise my chances of winning the "race"?

  • but isn't this actually a race condition?

    It's actually fine in JavaScript. JS is itself normally only single-threaded, so if you're inside a function, no other JS is going to be able to run and mess things up for you.

    It's just when you first start your function. For example if you do:

    setInterval(function() {
     // here
    }, 1000);
    

    Then you have no idea what would have run before your function started.

  • That appears to work fine, thanks. I also need to g.reset() at the start of any text-displaying functions which are triggered by button presses.

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Widget affecting screen positioning

Posted by Avatar for skm @skm

Actions