Visitar URL original
[ENH]: Support "window_move_event" in canvas.mpl_connect · Issue #25351 · matplotlib/matplotlib · GitHub
The Wayback Machine - https://web.archive.org/web/20230301185000/https://github.com/matplotlib/matplotlib/issues/25351
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ENH]: Support "window_move_event" in canvas.mpl_connect #25351

Open
alanbernstein opened this issue Feb 28, 2023 · 4 comments
Open

[ENH]: Support "window_move_event" in canvas.mpl_connect #25351

alanbernstein opened this issue Feb 28, 2023 · 4 comments

Comments

@alanbernstein
Copy link
Contributor

alanbernstein commented Feb 28, 2023 •

Problem

  • I'm always frustrated when I want a figure window to appear with specific geometry, because I have to either move it manually every time, figure out the geometry manually (and update it every time I rearrange windows), or write some code to print the geometry and then copy that into my code (every time I rearrange windows).
  • I would like it if figure window geometry was persisted across multiple runs of the same script, because I could move the window around manually one time, and then on every successive run of the script it would appear with that same geometry.
  • Here is a prototype implementation: https://gist.github.com/alanbernstein/a178fa96b355cc69ea249b688d9dd3a0

Proposed solution

I have created a quick prototype wrapper class PersistentFigure which serves as a rough proof of concept. Essentially, it simply creates a figure, and connects a callback to resize_event, which writes the geometry tuple to a JSON file, as a dict value, keyed with something like hostname-filename-functionname-figuretitle. This accomplishes most of what I want, with one problem: there is no callback event for moving a window, without resizing it. With this addition, my PersistentFigure would do everything I want it to.

I realize there are some likely issues with this, including:

  • This request verges on asking Matplotlib to serve as a window manager, which is, generally speaking, out of scope of the project. This is why I'm not asking for the actual feature - persistent figure window geometry - to be implemented within Matplotlib.
  • Matplotlib may not generally be aware of the figure window position, so providing this event could be problematic. If it really can't be done at all, regardless of the backend/context, I can accept that. However, if it is possible in some contexts and not others, I would appreciate a little information about that.
  • I'm not sure how to deal with multiple monitors, or different configurations of monitor setups on the same machine.
  • The above prototype works in Linux with default (as far as I remember) Matplotlib settings. It does not work on Mac, but may work with a different backend/implementation. I hope this wouldn't be a major blocker, but rather a system-dependent caveat.

If there are any good alternative ways to accomplish my overall goal, I would love to learn more about them.

@tacaswell
Copy link
Member

My knee-jerk threshold question on this is if the underlying UI frameworks (Qt, gtk, wx, tk, osx) give us a signal (on all of the platforms) we can listen to to be notified when the window is moved. If yes for all, this is probably non-controversial if someone does the work. If no for some we would have to have a think about if we want to give up feature parity. If none of them provide this hook I suspect this is a no-go.

A only slightly manual work around would be to make _save_geometry (or something that manages a bunch of these managers) public and have a button or keybinding (which you could also set up in the init) to manually snap shot the location (which means you can differentiate "I moved this because I wanted to" and "I moved this because my finger slipped on the trackpad")).

Keeping a global registry of figure location is well outside of Matplotlib's scope and should be managed by the application.

Mostly joking, have you considered using qtile (https://docs.qtile.org/en/latest/manual/install/)?


Unsolicited API comments: pass the Figure instance into your class (rather than calling plt.figure) or sub-class Figure and pass it as a class into plt.figure(..., FigureClass=StickyFigure).

@alanbernstein
Copy link
Contributor Author

alanbernstein commented Mar 1, 2023 •

My knee-jerk threshold question on this is if the underlying UI frameworks (Qt, gtk, wx, tk, osx) give us a signal (on all of the platforms) we can listen to to be notified when the window is moved.

Yes, exactly. It seems like an obviously missing callback, since only half of the geometry tuple is covered, so I wondered if there was a known reason this signal isn't included. It makes sense that application windows need to know their size but not their position, so maybe UI frameworks tend not to provide the position? I thought you might have some idea offhand; if not I can look into it (at some point).

If yes for all, this is probably non-controversial if someone does the work.

Is that "all" of the frameworks, or all valid combinations of (framework, platform)?

If no for some we would have to have a think about if we want to give up feature parity.

Are there any official guidelines on how this is normally approached? It seems that this is already somewhat of an issue (e.g. https://stackoverflow.com/questions/7449585/how-do-you-set-the-absolute-position-of-figure-windows-with-matplotlib).

A only slightly manual work around ...

This occurred to me as well, I just need to look up how to do buttons. This might be more realistic, at least short term. If the position callback approach is feasible, I would love to contribute it eventually. I realized that this problem has been nagging at me subconsciously for years, and I have to imagine it would be useful for others too. I also realize anyone using Jupyter/Spyder/PyCharm/etc has a slick solution already, but I imagine there is still room for the callback to be useful.

I appreciate any API comments from you; my intent with that prototype was to make the usage as concise as possible, without paying much attention to the Right Way to do it.

I'm open to any suggestions - I love the idea of tiling window managers, and ideally I would like to get in the habit of using one. i3 is on my todo list, but I will consider qtile as well. Do you use it effectively with Matplotlib? Frankly, I have become frustrated with the feature update/retirement cycle of Linux desktop environments. I now tend to use stock Ubuntu, to minimize the amount of effort I need to put into fixing customizations as they break.

Thanks for your input!

@tacaswell
Copy link
Member

Is that "all" of the frameworks, or all valid combinations of (framework, platform)?

all combinations, but the frameworks are pretty good about feature parity across platforms.

Do you use it effectively with Matplotlib?

yes, with a cut out that figures (well windows with the name 'Figure {N}') do not get tiled. It has a very rich set of rules for automatically placing windows based on program + window name so you might be able to get the persistent placement through it without any magic on the Matplotlib side (other than maybe systematically naming your figures).

@anntzer
Copy link
Contributor

anntzer commented Mar 1, 2023

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants