BLACK FRIDAY: Save 50% on all my Swift books and bundles! >>

SOLVED: Copy/Paste issue

Forums > macOS

So, I have an app where I have replaced the copy/paste menu item with custom code. I need to be able to use copy/paste to copy and paste a custom object, so I call to functions in my view model:

            Button("Copy") {
                mainViewModel?.copyToPasteBoard()
            }
            .keyboardShortcut("c", modifiers: .command)
            .disabled(!hasSelection)

            Button("Paste") {
                mainViewModel?.pasteFromPasteboard()
            }
            .keyboardShortcut("v", modifiers: .command)
            .disabled(!hasSelection)

This works great.

However, I also have a popup sheet that allows me to edit my data. Nothing special in there, just some TextFields:

                TextField("", text: $vm.text)
                    .themedFont(for: .nodeEditTitle)
                    .padding(.bottom)

The problem I don't know how to address is, when editing the data, I can press <ctrl>c and <ctrl>v to copy and paste data in the TextField, but it's the menu that is triggered and that,in turn, calls my view model to copy and paste the object. I can stop the view model doing the copy and paste by detecting whether the edit sheet is displayed, but the copy/paste isn't passed on to the TextField in the sheet.

What I need to be able to do is direct <ctrl>c and <ctrl>v to the TextField on the sheet and to ignore the menu. I don't know how to do that.

Thanks Steve.

   

As ever, not long after posting this, I come up with an obvious solution... Change the code such that, when I'm editing an item, i don't override the default menus:

    public var body: some Commands {
        if mainViewModel?.editItem != nil {
            EmptyCommands()
        } else {
            CommandGroup(replacing: .pasteboard) {
                Button("Edit Item") {

It feels like a bit of a hack rather than a solution, but it's a start.

1      

Save 50% in my WWDC sale.

SAVE 50% All our books and bundles are half price for Black Friday, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.