py5coding / py5generator

Meta-programming project that creates the py5 library code.
https://py5coding.org/
GNU General Public License v3.0
52 stars 13 forks source link

Remember last sketch window position #60

Closed tabreturn closed 2 years ago

tabreturn commented 2 years ago

With the Processing editor, you can run a sketch, then move the display window about; py5, of course, does this too.

But, with Processing, rerunning the sketch positions it where you last closed the display window; the py5 display window always defaults to the center of the screen.

One can set the window position explicitly with set_location(), but might there be a way for py5 to recall the last coordinates?

tabreturn commented 2 years ago

I'm thinking that the editor might handle this (Thonny, in this case). So, run_sketch could accept additional x-y coords for the display window ... but then how does py5 send a message 'back' to Thonny with that info ...

hx2A commented 2 years ago

So I've been running py5 Sketches for almost 2 years go (I started in March of 2020) and just about every time I've moved the Sketch window to a new location after it opens up. I never thought about this until you asked about it. I agree, it would be nice if the Sketch window would "remember" its last location and open up new windows in the new location. This is how most other applications work. I wonder if this is managed by the OS or by each individual application.

In any case, here is what I know about Sketch window locations.

run_sketch() supports arguments to set the window location because Processing does. If you look at the third example, it is using them to set the window location on display number 1. (note that means the first monitor and not the DISPLAY=:1 variety)

If you look at the Processing code comments they specifically mention this is used to set the window location by the editor. So in the case of Processing, window location is in fact actively managed by the PDE.

I know little about Processing code outside of the core libraries. But I was able to find the --location param mentioned in the Runner class. In the code one finds editor.getSketchLocation() and editor.setSketchLocation(new Point(left, top)) and some other stuff that suggests that the Sketch is sending messages to the PDE, perhaps over a socket. It makes sense that it would do this because the PApplet's stdout and stderr also appear in the PDE window. There needs to be some communication.

More investigation is needed here. Are py5 Sketches currently attempting to report window locations to a socket that nobody is listening to? First we need to find the code in the Sketch that is reporting the window location and how that communication channel works. Then we need to get the Thonny plugin to receive those messages. Currently the run_sketch command line utility does not accept window location parameters but it would be easy for me add that so they get passed along to the run_sketch() call.

hx2A commented 2 years ago

All of this work is done and will now remember the screen location of the last Sketch. The run_sketch command line utility does what it needs to do to allow Thonny to provide the same screen location functionality for users.

Processing uses the --external and --location command line arguments to instruct a Sketch to report window positions and to set the initial window location. To use both in py5 via the run_sketch command, do something like this:

run_sketch --py5_options external location=0,0

The window positions will be outputted to stderr and will be preceded with MOVE. The PDE must be intercepting these messages and removing them from stderr before displaying stderr to the user in the output pane. If it is not easy for you to do the same in Thonny let me know; py5 could easily write the window positions to a socket instead.