How to implement custom cut/copy/paste

aallen8455aallen8455 Aaron AllenUSMember ✭✭
edited September 11 in Xamarin.Mac

I want to have custom cut/copy/paste actions for a particular NSView. This view has items the user can select and should be able to perform copy/paste on.
From what I've seen, creating a method with [Export(":copy")] attribute should be enough to connect to the Copy item in the edit menu, however it's not working, Copy is greyed out.
Any idea how to do this?

Edit:
The correct tag to use is [Action("copy:")]. My question is now how to signal when copy or paste actions can be performed, i.e. when an item is selected.

I found this obj-c example but don't know how to implement it in Xamarin:

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
    BOOL retValue = NO;
    ColorTile *theTile = [self colorTileForOrigin:currentSelection];

    if (action == @selector(paste:) )
        retValue = (theTile == nil) &&
             [[UIPasteboard generalPasteboard] containsPasteboardTypes:
             [NSArray arrayWithObject:ColorTileUTI]];
    else if ( action == @selector(cut:) || action == @selector(copy:) )
        retValue = (theTile != nil);
    else
        retValue = [super canPerformAction:action withSender:sender];
    return retValue;
}

Best Answer

  • aallen8455aallen8455 Aaron Allen USMember ✭✭
    Accepted Answer

    I figured it out, the view needs a method with [Action("validateMenuItem:")] tag, for example:

        [Action("validateMenuItem:")]
        public bool ValidateMenuItem(NSMenuItem item)
        {
            string actionName = item.Action.Name;
    
            if (actionName == "cut:" || actionName == "copy:")
            {
                ...
            }
            else if (actionName == "paste:")
            {
                ...
            }
    
            return true;
        }
    

Answers

  • aallen8455aallen8455 Aaron Allen USMember ✭✭
    Accepted Answer

    I figured it out, the view needs a method with [Action("validateMenuItem:")] tag, for example:

        [Action("validateMenuItem:")]
        public bool ValidateMenuItem(NSMenuItem item)
        {
            string actionName = item.Action.Name;
    
            if (actionName == "cut:" || actionName == "copy:")
            {
                ...
            }
            else if (actionName == "paste:")
            {
                ...
            }
    
            return true;
        }
    
Sign In or Register to comment.