Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using undo manager with mac catalyst

I'm working on adding a mac build target to an iOS application. I have the basics working and would like to implement undo/redo functionality.

In a traditional AppKit application you get this for free. When you create a new application, the prepopulated main menu has the Edit item and Undo and Redo under it. The view controller has an undoManager, you just registerUndo on it (preferably setActionName as well) and everything works. Hotkeys, menu item title changes and state changes (disable redo when at the top of the stack etc.) all work out of the box.

Adding a catalyst build target to an iOS project also creates a default menu with a top level Edit menu and Undo/Redo menu items. These do not seem to adopt the built-in functionality. Do I really need to manually recreate all that is free with AppKit or is there something I'm missing?

like image 408
whistler Avatar asked Sep 07 '25 03:09

whistler


2 Answers

Registering undo on NSWindow.undoManager works for me. But I had to use a hidden/private API to access the NSWindow instance using Dynamic library:

let nsWindow = Dynamic.NSApplication.sharedApplication.delegate.hostWindowForUIWindow(view.window)
let undoManager: UndoManager? = nsWindow.undoManager
like image 66
Hejazi Avatar answered Sep 09 '25 06:09

Hejazi


I was able to get this working in my Catalyst app only by calling becomeFirstResponder() in viewDidAppear and no sooner. I was originally calling it in viewDidLoad and also tried viewWillAppear, neither of which correctly register my undo manager with macOS.

Relevant view controller sample code:

class MyViewController: UIViewController {
    override var undoManager: UndoManager? {
        return myCustomUndoManager
    }

    override var canBecomeFirstResponder: Bool {
        true
    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        
        becomeFirstResponder()
    }
}
like image 28
Joey C. Avatar answered Sep 09 '25 07:09

Joey C.