Centering the current glyph in tab

I updated my script “Edit Next Glyph” and found that centering the current glyph in the tab did not work reliably. Strangely, it turned out that a simple print() at the right time fixes the problem:

Can anyone shed some light on this? Why is this necessary? Is there a more elegant way of triggering whatever print() seems to trigger? Thanks!

What is happening inside the if newPosition == 0: condition when the print() is not there? Or is the entire condition something you had to add?

Removing or commenting out only the print() does not work as the Python interpreter will complain, of course, so to test this you need to remove the entire condition.

The condition is not necessary for the fix but it helps narrow down the problem. In other words, the glyph is not centred correctly if the text cursor is at the beginning of the text. Or, in fact, at the last glyph: freemix-glyphsapp/Edit Previous Glyph.py at 4bec24ad48d2fc0776f1c8a6a6a63d8401a29480 · justanotherfoundry/freemix-glyphsapp · GitHub

A print() triggers an update of the UI (to allow the macro window to udpate). That might trigger some internal updates.

Thanks.

Is there a more elegant (or “official”) way of triggering this? Or is it a bug in Glyphs?

Have you tried Glyphs.redraw()?
Maybe not what Georg will suggest, but worth a shot ¯_(ツ)_/¯

Anyway sounds like a buggy behaviour to me too.

Thanks but no luck. I tried tab.redraw() as well but it does not help.

When I run the snippet from the macro window, or your scripts, it works for me.

Of course it does.

The question is, does the centering still work if you remove the print()?

I did remove the print().

This is what it looks like on my computer:

The video doesn’t seem to work.

Hmmm… works in Firefox but not in Safari. I simply pasted a link to a Dropbox file.

Let’s see whether this one works:

Something is funny with the forum software. Even the Dropbox link with dl=1 is not preserved as a link. Something is trying to be too smart but it fails. Can I prevent it from trying to embed the video?

Next attempt

First video works fine for me, in Chrome (Android, macOS). Second one doesn’t.

Now I understand. The problem is only why the first position. And only really visible at certain zoom sizes.

I had a look. It seems that Glyphs is trying to keep the selected layer visible when in Text mode and scrolls it into place. That interferes with your attempt to center it. As long as the newly selected layer is not touching the border of the view, Glyphs doesn’t do anything. Zoom out so that all glyphs fit and a bit more. Then the centering should work.
So when you are in selection/edit more, the script should work just fine.

Calling .redraw is not actually redrawing, it just markes the view that it needs to be redrawn in the next drawing cycle. That is how macOS works. Calling print() deals with the runloop to get an extra redraw cycle.