SwiftUI multiple windows closing will cause crash - swift

I am creating a second window in my SwiftUI app for Mac.
This is how I call my second window.
let window = NSWindow(contentRect: NSRect(x: 20, y: 20, width: 480, height: 300), styleMask: [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView], backing: .buffered, defer: false)
window.center()
window.setFrameAutosaveName("Add Person")
window.contentView = NSHostingView(rootView: AddPerson())
window.makeKeyAndOrderFront(nil)
This works fine. However, when I try to close that window, the app is crashing.
I am calling this, to close the window.
NSApplication.shared.keyWindow?.close()
I think there is a problem with opening two windows. Is my opening call correct?
Edit: I need to set the window.number when I am creating that window. How can I set that? I haven't found anything.

Well, I don't think it is because of window itself (I tested your case on Xcode 11.3 with just simple text & button content and it works), but due to some content and/or active objects (managers, etc.)
Anyway, you can try instead of force close (as in provided snapshot) to close via action
NSApp.keyWindow?.performClose(nil)
it does the same as clicking close button on window's titlebar.
Update: store below window as member, not a local variable
let window = NSWindow(contentRect: NSRect(x: 20, y: 20, width: 480, height: 300), styleMask: [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView], backing: .buffered, defer: false)
like main window
class AppDelegate: NSObject, NSApplicationDelegate {
var window: NSWindow! // << default, main window
var window2: NSWindow! // << other window (as example)
... // somewhere below
window2 = NSWindow(contentRect: NSRect(x: 20, y: 20, width: 480, height: 300), styleMask: [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView], backing: .buffered, defer: false)

Related

Make NSWindow remain "float" when space changes in swift

I currently have:
let window = NSWindow(contentRect: NSRect(x: 300, y: 300, width: 200, height: 200), styleMask: [.borderless], backing: .buffered, defer: true)
window.backgroundColor = NSColor.green
window.level = .floating
window.collectionBehavior = [.stationary, .canJoinAllSpaces, .fullScreenAuxiliary]
window.makeKeyAndOrderFront(nil)
Current behaviour:
When space changes (moving between full screen windows) the window moves relative to the desktop/space .
Expected behaviour:
I want the window to behave much like the notifications window in macOS, where the window "floats" above the screen even when active space changes.

SwiftUI - cannot find type 'UIApplicationDelegate' in scope

I am new to MacOS development and Swift so sorry if this is a basic/stupid question.
I am getting this error:
Cannot find type 'UIApplicationDelegate' in scope
I have created an AppDelegate.swift file to try to define a default window size for my app. I have this code:
import Foundation
import SwiftUI
class AppDelegate: NSObject, UIApplicationDelegate {
window = NSWindow(
contentRect: NSRect(x: 0, y: 0, width: 480, height: 300),
styleMask: [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView],
backing: .buffered, defer: false)
window.center()
window.setFrameAutosaveName("Main Window")
window.contentView = NSHostingView(rootView: ContentView())
window.makeKeyAndOrderFront(nil)
}
I'm not really sure what to do here. Is there something I need to import?
Thnak you for your help.
import FBSDKCoreKit. This will help u :)

Variable reassignment causes "Message sent to deallocated instance"

I had code similar to this which was causing the error "-[NSWindow release]: message sent to deallocated instance":
var w = NSWindow(contentRect: NSMakeRect(100, 100, 500, 500), styleMask: .closable, backing: NSWindow.BackingStoreType.buffered, defer: false)
w.title = "Window"
w.makeKeyAndOrderFront(nil)
//w.isReleasedWhenClosed = false
w.close()
w = NSWindow(contentRect: NSMakeRect(100, 100, 500, 500), styleMask: .closable, backing: NSWindow.BackingStoreType.buffered, defer: false)
When I added the commented line it worked fine.
I just wanted to know if this is expected behaviour with the way Swift does memory management?
It was quite unexpected that the variable reassignment could be causing this problem.

How to convert a Swift UI View to a NSImage

I am trying to use a SwiftUI View as a NSCursor on MacOS.
Using SwiftUI I am constructing a view that I then convert to an NSView using NSHostingView. Now I am trying to convert that to a NSImage via a NSBitmapImageRep. For some reason I always get an empty png when I inspect the variables while setting breakpoints.
(For now the setup of the cursor is done in the AppDelegate because I am currently just trying to get this to work.)
class AppDelegate: NSObject, NSApplicationDelegate {
var window: NSWindow!
func applicationDidFinishLaunching(_ aNotification: Notification) {
// Create the SwiftUI view that provides the window contents.
let contentView = ContentView()
let contentRect = NSRect(x: 0, y: 0, width: 480, height: 480)
// Create the window and set the content view.
window = NSWindow(
contentRect: contentRect,
styleMask: [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView],
backing: .buffered, defer: false)
window.center()
window.setFrameAutosaveName("Main Window")
window.contentView = NSHostingView(rootView: contentView)
window.makeKeyAndOrderFront(nil)
let myNSView = NSHostingView(rootView: ContentView()).bitmapImageRepForCachingDisplay(in: contentRect)!
NSHostingView(rootView: ContentView()).cacheDisplay(in: contentRect, to: myNSView)
let myNSImage = NSImage(size: myNSView.size)
myNSImage.addRepresentation(myNSView)
self.window.disableCursorRects()
// var myName = NSImage.Name("ColorRing")
// var myCursorImg = NSImage(named: myName)!
//
// var myCursor = NSCursor(image: myCursorImg, hotSpot: NSPoint(x: 10, y: 10))
var myCursor = NSCursor(image: myNSImage, hotSpot: NSPoint(x: 0, y: 0))
myCursor.set()
}
func applicationWillTerminate(_ aNotification: Notification) {
// Insert code here to tear down your application
}
}
When executing the part that is commented out, I get the mouse cursor from an external png file.
When executing the code shown above I always get an empty mouse cursor with the size 480x480.
In debug mode I can see, that myNSView is an empty png.
I am pretty sure I misunderstand the documentation and therefore misuse cacheDisplay(in:to:) and bitmapImageRepForCachingDisplay(in:).
The documentation tells me to use the NSBitmapImageRep from bitmapImageRepForCachingDisplay(in:) in cacheDisplay(in:to:). But for some reason I simply can not get this to work.
Does anyone have an idea what I am doing wrong?
To answer my own question:
It seems like the view that one wants to convert to an NSImage needs to be layed out inside of a window. Otherwise it probably does not know how large the view needs to be inside the rect.
The following code works for me:
func applicationDidFinishLaunching(_ aNotification: Notification) {
// Create the SwiftUI view that provides the window contents.
let contentView = ContentView()
let contentRect = NSRect(x: 0, y: 0, width: 480, height: 480)
// Create the window and set the content view.
window = NSWindow(
contentRect: contentRect,
styleMask: [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView],
backing: .buffered, defer: false)
window.center()
window.setFrameAutosaveName("Main Window")
window.contentView = NSHostingView(rootView: contentView)
window.makeKeyAndOrderFront(nil)
// CHANGED ----------
let newWindow = NSWindow(
contentRect: contentRect,
styleMask: [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView],
backing: .buffered, defer: false)
newWindow.contentView = NSHostingView(rootView: contentView)
let myNSBitMapRep = newWindow.contentView!.bitmapImageRepForCachingDisplay(in: contentRect)!
newWindow.contentView!.cacheDisplay(in: contentRect, to: myNSBitMapRep)
// CHANGED ----------
let myNSImage = NSImage(size: myNSBitMapRep.size)
myNSImage.addRepresentation(myNSBitMapRep)
self.window.disableCursorRects()
var myCursor = NSCursor(image: myNSImage, hotSpot: NSPoint(x: 0, y: 0))
myCursor.set()
}
Only lines between the two //CHANGED ----- comments have been changed for a working solution. I am simply embedding the view inside a window before applying the same methods as before.
Hope this helps some people in the future.

Create a new window with NSWindow

I'd like to create a new window programmatically. I have the following code, and it builds, but my window doesn't show up. The only way I can make it visible is by adding it as a child window to the default 'window'. How can I make 'win' be an independent window?
#IBOutlet var window: NSWindow
func applicationDidFinishLaunching(aNotification: NSNotification?) {
var win = NSWindow(contentRect: NSMakeRect(100, 100, 600, 200),
styleMask: NSResizableWindowMask,
backing: NSBackingStoreType.Buffered, defer: true)
window.addChildWindow(win, ordered:NSWindowOrderingMode.Above)
}
What about adding:
win.makeKeyAndOrderFront(win)
For me on OSX (not iOS) using Swift and writing in vim
let win = NSWindow(contentRect: NSMakeRect(100, 100, 600, 200),
styleMask: NSResizableWindowMask,
backing: NSBackingStoreType.buffered, defer: true)
win.makeKeyAndOrderFront(win)
pops up a window
You also need a NSWindowController to display the window:
let window = NSWindow(...)
let controller = NSWindowController(window: window)
controller.showWindow(self)