|
| 1 | +// |
| 2 | +// ContextMenu.m |
| 3 | +// reactnativeuimenu |
| 4 | +// |
| 5 | +// Created by Matthew Iannucci on 10/6/19. |
| 6 | +// Copyright © 2019 Facebook. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +#import "ContextMenuView.h" |
| 10 | +#import <React/UIView+React.h> |
| 11 | + |
| 12 | +@implementation ContextMenuView { |
| 13 | + BOOL cancelled; |
| 14 | +} |
| 15 | + |
| 16 | +- (void)insertReactSubview:(UIView *)subview atIndex:(NSInteger)atIndex |
| 17 | +{ |
| 18 | + [super insertReactSubview:subview atIndex:atIndex]; |
| 19 | + if (@available(iOS 13.0, *)) { |
| 20 | + UIContextMenuInteraction* contextInteraction = [[UIContextMenuInteraction alloc] initWithDelegate:self]; |
| 21 | + |
| 22 | + [subview addInteraction:contextInteraction]; |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +- (void)removeReactSubview:(UIView *)subview |
| 27 | +{ |
| 28 | + [super removeReactSubview:subview]; |
| 29 | +} |
| 30 | + |
| 31 | +- (void)didUpdateReactSubviews |
| 32 | +{ |
| 33 | + [super didUpdateReactSubviews]; |
| 34 | +} |
| 35 | + |
| 36 | +- (void)layoutSubviews |
| 37 | +{ |
| 38 | + [super layoutSubviews]; |
| 39 | +} |
| 40 | + |
| 41 | +- (nullable UIContextMenuConfiguration *)contextMenuInteraction:(nonnull UIContextMenuInteraction *)interaction configurationForMenuAtLocation:(CGPoint)location API_AVAILABLE(ios(13.0)){ |
| 42 | + |
| 43 | + return [UIContextMenuConfiguration configurationWithIdentifier:nil previewProvider:nil actionProvider:^UIMenu * _Nullable(NSArray<UIMenuElement *> * _Nonnull suggestedActions) { |
| 44 | + NSMutableArray* actions = [[NSMutableArray alloc] init]; |
| 45 | + |
| 46 | + for (ContextMenuAction* thisAction in self.actions) { |
| 47 | + UIAction* actionMenuItem = [UIAction actionWithTitle:thisAction.title.capitalizedString image:[UIImage systemImageNamed:thisAction.systemIcon] identifier:nil handler:^(__kindof UIAction * _Nonnull action) { |
| 48 | + if (self.onPress != nil) { |
| 49 | + self->cancelled = false; |
| 50 | + self.onPress(@{@"name": thisAction.title}); |
| 51 | + } |
| 52 | + }]; |
| 53 | + |
| 54 | + [actions addObject:actionMenuItem]; |
| 55 | + } |
| 56 | + |
| 57 | + return [UIMenu menuWithTitle:self.title children:actions]; |
| 58 | + }]; |
| 59 | +} |
| 60 | + |
| 61 | +- (void)contextMenuInteraction:(UIContextMenuInteraction *)interaction willDisplayMenuForConfiguration:(UIContextMenuConfiguration *)configuration animator:(id<UIContextMenuInteractionAnimating>)animator API_AVAILABLE(ios(13.0)){ |
| 62 | + cancelled = true; |
| 63 | +} |
| 64 | + |
| 65 | +- (void)contextMenuInteraction:(UIContextMenuInteraction *)interaction |
| 66 | +willEndForConfiguration:(UIContextMenuConfiguration *)configuration |
| 67 | + animator:(id<UIContextMenuInteractionAnimating>)animator API_AVAILABLE(ios(13.0)) API_AVAILABLE(ios(13.0)){ |
| 68 | + |
| 69 | + if (cancelled && self.onCancel) { |
| 70 | + self.onCancel(@{}); |
| 71 | + } |
| 72 | + |
| 73 | +} |
| 74 | + |
| 75 | +@end |
0 commit comments