mjolnir icon indicating copy to clipboard operation
mjolnir copied to clipboard

Add screen:movetoscreen() and screen:findbyresolution()

Open ravasthi opened this issue 12 years ago • 8 comments

Hi,

I'm currently a slate user and am looking to switch away to a maintained project like Hydra. The thing I'm missing the most is a way to save and restore multi-monitor layouts, and was planning on writing an extension to do just that. However, in order to make that possible, I'd need two things:

  • A way to find screens by resolution
  • A way to move a window to a specific screen

So, I'd love if Hydra could do something like that…please?

Thanks!

ravasthi avatar Jul 27 '14 17:07 ravasthi

@ravasthi The first one is as easy as wrapping NSDeviceResolution (and possibly NSDeviceIsScreen but when is it ever a printer?), and will definitely be added. The second one is less clear: what would it mean to "move a window to a screen"? The user might want it to be proportionate, i.e. the window's x,y,w,h would be translated into percentages on the current screen and then new absolute coordinates would be translated to the new screen based on these percentages, which might changes its size. Or the user might want it to keep its size and just move it to the same absolute position relative to 0,0 of that screen. Or they might want it moved to the percentage-based x,y of the new screen. Or something else entirely. So I don't know how such a method should work. Maybe there should be 3 methods for this. I don't like the idea of putting all 3 behaviors in one method and switching on them by parameters, since they're fundamentally different actions.

sdegutis avatar Jul 27 '14 18:07 sdegutis

@sdegutis Good points. I defer to you on how many methods there should be, but here's the behavior I think might make sense when moving a window to a screen:

  • If the window could fit in the new screen, i.e. is smaller than or equal to the screen's size without dock or menu, don't resize and move straight to the same absolute position relative to 0,0 (is that top left?) of that screen. If, however, that absolute position would cause the window to be partially offscreen, move the window such that it would be entirely visible.
  • If one of the window's dimensions is bigger than the corresponding screen dimension without dock or menu, move it to 0 on that axis and set the dimension equal to the screen's dimension.
  • To extend that line of thinking, if the window doesn't fit in the screen in either dimension, i.e. is bigger than the screen's size without dock or menu, move it to 0,0 on the new screen and maximize it.

I think that's good default behavior. That said, the multi-monitor extension I would write would probably do a move immediately followed by a resize anyway, so technically speaking I don't need the move to screen functionality to be any smarter than making sure that some clickable area of the top of the window is visible on the new screen so that I can grab and move it manually if needed.

What do you think?

ravasthi avatar Jul 27 '14 18:07 ravasthi

Hey guys.

A lot of this can be done already, with out any modification to Hydra. Check out my config. I use it to manage my hex-monitor setup at work. All monitors are the same size though, so I didn't bother to accommodate for differing resolutions.

jhgg avatar Jul 27 '14 18:07 jhgg

@jhgg Can you briefly summarize how your config solves this, if it's not too much trouble?

sdegutis avatar Jul 27 '14 19:07 sdegutis

Not at all.

For one, my config makes a few very specific assumptions of the environment it's in.

  1. There are no 2x/reitna displays attached to the computer. (As I don't have any)
  2. All the monitors are of the same resolution.
  3. All the monitors are positioned in a 3x2 grid.

So, when my config starts, it does monitor detection, to figure out which monitor is in what position. The positions are like the grid below:

| 4 | 5 | 6 |
| 1 | 2 | 3 |

(Which corresponds to each physical monitor) desktop

Monitor Auto Discovery

So, the function autodiscover_monitors is called which basically takes all the screens, and grabs their full frames using screen:frame_including_dock_and_menu(). Since all my monitors are 1x scale, The w and h parts of the frame are the monitor's resolution. Basically, how it works, is:

  1. It finds the "primary" screen, which is the screen that's at x, y = 0, 0. It uses this as as the reference screen for resolution (which means it assumes that all other screens are of the same resolution).
  2. Then it finds the screens on the first row, by looking for monitors on the same Y dimension (the first iteration y = 0), and then sorting them by their X component (which gets monitors [1, 2, 3]).
  3. It subtracts the height of the the reference monitor from the Y dimension it's looking for monitors on and finds the upper row of monitors.

Moving Windows

Now that it's found all my monitors, and mapped them to indexes 1...6, another module comes into play to bind each monitor to a key on the numpad dynamically, modules/monitors.lua:init_module is where this happens.

It uses utils/position.lua to provide generic functions which takes the screen's frame_without_dock_and_menu() to spit out a frame that is used to position the window either left, full, or right. (there's a lot more positions it can calculate, see the file for more details).

Now the tricky one really is moving a window to another monitor, and keeping the same size (handled here). For this, I use the function dimension:relative_window_position(win). This is called on the dimension object (which is created in monitor.autodiscover_monitors() to translate the window to the same position on the other monitor.

That's pretty much it...

jhgg avatar Jul 27 '14 21:07 jhgg

nice explanation - you should add it to your repo README.md :)

muescha avatar Jul 27 '14 22:07 muescha

I plan to document it a bit more... whenever I get the chance.

jhgg avatar Jul 27 '14 22:07 jhgg

Moved to mjolnir-io/mjolnir.core.screen#2.

sdegutis avatar Aug 27 '14 17:08 sdegutis