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

Feature request: add support for window zoom #181

Closed
jfchevrette opened this issue Jan 6, 2015 · 11 comments
Closed

Feature request: add support for window zoom #181

jfchevrette opened this issue Jan 6, 2015 · 11 comments

Comments

@jfchevrette
Copy link
Contributor

When using hs.window:maximize() the window will be resized to fill the screen w/o the dock. It is my understanding that hs does this manually by setting the window position/size as opposed to using OS capabilities.

It would be useful to add window zoom functionnality using OS X zoom capabilities (when an app/window supports it). Then we can also support "unzooming" so that the window previous position/size is restored.

The AppKit zoom method is available in OS X 10.0 and later and should be all that's needed to toggle between zoomed/unzoomed. If the window does not support zooming, a beep sound is triggered by the OS.

https://developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSWindow_Class/index.html#//apple_ref/occ/instm/NSWindow/zoom:

@cmsj cmsj closed this as completed in f91a490 Jan 6, 2015
@mengelbrecht
Copy link
Contributor

@cmsj We implemented it nearly simultaneously... 👍

@cmsj
Copy link
Member

cmsj commented Jan 6, 2015

@mgee haha, sorry!

@mengelbrecht
Copy link
Contributor

Nevermind ;-)

@jfchevrette
Copy link
Contributor Author

commit f91a490 does not implement the behavior I was expecting from this feature request. (the code was eventually refectored under the window_pressbutton function)

Clicking kAXZoomButtonAttribute on OSX < 10.10 it calls toggleZoom and on OSX 10.10 it calls toggleFullScreen unless option is pressed then toggleZoom is called. This has the effect that toggleZoom has a different behavior depending on your OS X version. No ideal IMO.

To remedy that, I believe the proper implementation for hs.window:toggleZoom() would be to call NSWindow 's zoom: method as opposed to simply clicking the button kAXZoomButtonAttribute. This would at least be consistent accross OS X versions.

@cmsj cmsj reopened this Jan 10, 2015
@cmsj
Copy link
Member

cmsj commented Jan 10, 2015

reopening, but note that we don't have access to the nswindow object, just the accessibility API

@jfchevrette
Copy link
Contributor Author

would it be possible to fake a keypress on option while clicking kAXZoomButtonAttribute?

In OSX > 10.9 clicking the zoom button toggle the zoom stateand option+click toggle the maximized state which was the previous default in OSX < 10.9

hs.window:toggleZoom(), clicks kAXZoomButtonAttribute
hs.window:toggleMaximized(), clicks kAXZoomButtonAttribute /w option

@cmsj
Copy link
Member

cmsj commented Jan 11, 2015

faking keypresses gets very complex, because we can't be sure what the user is pressing at the time, so they might end up confusing things. Also I'm not sure if the accessibility layer would even care about it.

I think a better option here would be to wrap this up at the Lua layer and store the current frame of a window, call :maximize() on it, note that that has happened, then when called again, restore the original frame.

@jfchevrette
Copy link
Contributor Author

I ended up implementing it in lua

hs.hotkey.bind(mash, 'RETURN', function() 
  local win = hs.window.focusedWindow()
  local frame = win:frame()
  local id = win:id()

  -- init table to save window state
  savedwin = savedwin or {}
  savedwin[id] = savedwin[id] or {}

  if (savedwin[id].maximized == nil or savedwin[id].maximized == false) then
    savedwin[id].frame = frame
    savedwin[id].maximized = true
    win:maximize()
  else
    savedwin[id].maximized = false
    win:setFrame(savedwin[id].frame)
  end
end)

@cmsj
Copy link
Member

cmsj commented Jan 13, 2015

@jfchevrette looks good. One thing I might suggest is that in the else section, you actually do savedwin[id] = nil, otherwise you're going to slowly leak references to windows that don't exist anymore.
(that leak is still possible if you close windows without unzooming them, so the most complete solution here would use hs.uielement to catch an event of the window closing, and nil it out in savedwin, but we're talking about very small amounts of memory here, so that may be more work than it's worth)

I'm going to close out this issue for now, but I do think we should have some generic abstraction in hs.window for snapshotting the frame and restoring it later, very much like how your savedwin code works.

@cmsj cmsj closed this as completed Jan 13, 2015
@tmandry
Copy link
Contributor

tmandry commented Jan 14, 2015

Sorry to comment on a closed issue, but I'd like to point out that zoom is not the same as maximize. In many Apple applications, clicking the zoom button doesn't make the window fill the screen, but makes the window large enough to fit whatever the content is (e.g. in Preview, it makes it big enough to fit the page).

Of course, I have no idea how to activate this if they changed what the meaning of "zoom" is in the API...

@cmsj
Copy link
Member

cmsj commented Jan 14, 2015

@tmandry yes, the zoom button is weird and inconsistent (and thus, imho, useless), which is why we didn't support it for a long time, but we do now, so if people want to use it, they can :)

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

No branches or pull requests

4 participants