OSX Application: how to make window maximized?
You can "zoom" a window to the max available space by using NSScreen's visibleFrame
as the target frame. Let's say window
is your NSWindow IBOutlet:
if let screen = NSScreen.mainScreen() {
window.setFrame(screen.visibleFrame, display: true, animate: true)
}
For example, in the AppDelegate.swift:
import Cocoa
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
@IBOutlet weak var window: NSWindow!
func applicationDidFinishLaunching(aNotification: NSNotification) {
if let screen = NSScreen.mainScreen() {
window.setFrame(screen.visibleFrame, display: true, animate: true)
}
}
NSWindow maximized?
Maybe take a look at isZoomed
Mac OS X Dock: How to programmatically maximize my application with the application's main icon, and not the minimized icon?
Need to implement applicationShouldHandleReopen in order to open minimized windows from the "main" icon
- (BOOL)applicationShouldHandleReopen:(NSApplication *) __unused theApplication hasVisibleWindows:(BOOL)flag
{
if (!flag){
[[self window] makeKeyAndOrderFront:self];
}
return YES;
}
How can I minimize/maximize windows in macOS with the Cocoa API from a Python script?
There are probably different ways to do it, out of which one is by enumerating the running applications and next is enumerating the windows inside the application.
I will show the app approach here
from AppKit import NSApplication, NSApp, NSWorkspace
from Quartz import kCGWindowListOptionOnScreenOnly, kCGNullWindowID, CGWindowListCopyWindowInfo
workspace = NSWorkspace.sharedWorkspace()
activeApps = workspace.runningApplications()
for app in activeApps:
if app.isActive():
options = kCGWindowListOptionOnScreenOnly
windowList = CGWindowListCopyWindowInfo(options,
kCGNullWindowID)
for window in windowList:
if window['kCGWindowOwnerName'] == app.localizedName():
print(window.getKeys_)
break
break
This will find the current focused app, you can change the logic based on titles or whatever you want
After the app
is found. You can minimize it using
app.hide()
And you can show the app again using
from Cocoa import NSApplicationActivateIgnoringOtherApps, NSApplicationActivateAllWindows
app.unhide()
app.activateWithOptions_(NSApplicationActivateIgnoringOtherApps)
# or
app.activateWithOptions_(NSApplicationActivateIgnoringOtherApps | NSApplicationActivateAllWindows)
Lot of threads I had to refer to get to this solution
OS X: Move window from Python
How to list all windows from all workspaces in Python on Mac?
How to get Window reference (CGWindow, NSWindow or WindowRef) from CGWindowID in Swift?
"No such file: 'requirements.txt' error" while installing Quartz module
How to build Apple's Son of grab example?
How to get another application window's title, position and size in Mac OS without Accessibility API?
Get the title of the current active Window/Document in Mac OS X
-[NSRunningApplication activateWithOptions:] not working
How to start an app in the foreground on Mac OS X with Python?
set NSWindow focused
Activate a window using its Window ID
Fullscreen to Maximize on MacOS using JS
Try using currentWindow.isFullScreen()
instead of currentWindow.isMaximized()
.
You're using currentWindow.isMaximized()
to check if you're full screened when you should use currentWindow.isFullScreen()
instead.
See the docs for: currentWindow.isFullScreen()
Old answer:
I had problems creating my own traffic lights too, but it turns out Electron can do this for you with the titleBarStyle
option.
To use it:
var winObj = new BrowserWindow({
titleBarStyle: 'hidden',
});
This will create some native traffic lights in the top right window corner.
Here are different values for the titleBarStyle
option:
default
- Results in the standard gray opaque Mac title bar.
hidden
(Used in the example).
- Results in a hidden title bar and a full size content window, yet the title bar still has the standard window controls ("traffic lights") in the top left.
hiddenInset
- Results in a hidden title bar with an alternative look where the traffic light buttons are slightly more inset from the window edge.
Have a play around with it and see what works for you.
See the docs for the BrowserWindow
options (search for titleBarStyle
).
Enable fullscreen for floating window in macOS app
I got it to work by setting collectionBehavior
:
NSApplication.shared.windows.forEach { window in
window.collectionBehavior = [.fullScreenPrimary]
window.level = .floating
}
I found a similar SO question, they also set some properties (not the level
) of the window, and fullscreen gets disabled.
So is it that if collectionBehavior
is not specified for an NSWindow, then fullscreen is enabled as long as certain NSWindow properties (e.g. level
) are not explicitly set?
Create a fixed position window in Mac Application development
It's possible - but I wouldn't recommend it, as it could be a bad user experience.
The solution is simple, though: set the .isMovable
property of the window to false
.
For example, for a window
IBOutlet, setting:
window.isMovable = false
in applicationDidFinishLaunching
works well.
You can also, for example, use window.setFrameOrigin(_ point: NSPoint)
to first set it at a fixed position.
How to customize enlarging a window on a mac pro?
The functionality you're looking for is known as "zooming" the window. It used to be that the green button did that, but these days it enters full-screen mode instead. If you hold down the Option key when you click the green button, it reverts to the old zoom operation. You can also use the Zoom item of the Window menu. You can also configure things so that double-clicking on the title bar zooms the window, on the Dock pane of System Preferences.
Related Topics
iOS Facebook Sdk: Login Doesn't Return Email Despite Permissions Granted
Swift 4 Decode Simple Root Level JSON Value
Swiftui List Empty State View/Modifier
Can't Hook Up an Outlet Collection in Xcode 6 Using Storyboard
Constant Speed Orbit Around Point with Sknode
Swift, Auto Resize Custom Table View Cells
Why Should Not Directly Extend Uiview or Uiviewcontroller
Swift - Avaudioplayer, Sound Doesn't Play Correctly
Arkit - Viewport Size VS Real Screen Resolution
Swift Error Handling for Methods That Do Not Throw
Mutable Binding in Swiftui Live Preview
Error: Use of Unresolved Identifier 'Process'
Bool Being Seen as Int When Using Anyobject
How to Check the Network Speed Using Swift
Conditionally Use View in Swiftui
Swiftui: How to Implement a Custom Init With @Binding Variables