maxsokolov / TableKit

Type-safe declarative table views.
MIT License
706 stars 74 forks source link

Context menu improvements #101

Open ddanilyuk opened 3 years ago

ddanilyuk commented 3 years ago

Hi! I really like your framework and I found a case where it doesn't quite work. I use it for creating chat and I needed to create a context menu for the message bubble. But in this version of the framework this cannot be done. I add some methods from UITableViewDelegate to select view from cell which will be used by the context menu.

Added methods from UITableViewDelegate:

Some code example how to use new functionality:

let row = TableRow<MessageTableViewCell>(item: message)
  .on(.showContextMenu) { item -> UIContextMenuConfiguration in

      // Here you need to path indexPath to identifier.
      return UIContextMenuConfiguration(identifier: item.indexPath as NSIndexPath, previewProvider: nil) { _ in

          let copyAction = UIAction(title: R.string.chat.copy(), image: R.image.messageCopy())
          let deleteAction = UIAction(title: R.string.chat.delete(), image: R.image.messageDelete(), attributes: .destructive) 
          return UIMenu(children: [copyAction, deleteAction])
      }
  }
  // !New methods!
  .on(.previewForHighlightingContextMenu) { item -> UITargetedPreview in

      // Choosing view to use in contextMenu
      let view = cell.messageBackgroundView ?? UIView()

      // Choosing parameters
      let parameters = UIPreviewParameters()
      parameters.backgroundColor = .clear
      parameters.visiblePath = UIBezierPath(roundedRect: view.bounds, cornerRadius: 10))

      return UITargetedPreview(view: view, parameters: parameters)
  }
  .on(.previewForDismissingContextMenu) { item -> UITargetedPreview in

      let view = cell.messageBackgroundView ?? UIView()

      // Choosing parameters
      let parameters = UIPreviewParameters()
      parameters.backgroundColor = .clear
      parameters.visiblePath = UIBezierPath(roundedRect: view.bounds, cornerRadius: 10))

      return UITargetedPreview(view: view, parameters: parameters)
  }