Use a snapshot view for context menu previews.

This commit is contained in:
Doug 2022-02-01 14:54:28 +00:00 committed by Doug
parent 351c18cfc6
commit f63d0c4101
3 changed files with 20 additions and 21 deletions

View file

@ -16,23 +16,21 @@
import UIKit
/// A view controller that provides a preview of a room for use in context menus.
/// A view controller that provides a preview for use in context menus.
/// The preview will display a snapshot of whichever view is passed into the init.
@objcMembers
class RoomPreviewViewController: UIViewController {
// MARK: - Constants
private enum Constants {
static let size = CGSize(width: 80, height: 115)
}
class ContextMenuSnapshotPreviewViewController: UIViewController {
// MARK: - Private
private var cellData: MXKCellData
private let snapshotView: UIView?
// MARK: - Setup
init(cellData: MXKCellData) {
self.cellData = cellData
/// Creates a new preview by snapshotting the supplied view.
/// - Parameter view: The view to use as a preview.
init(view: UIView) {
self.snapshotView = view.snapshotView(afterScreenUpdates: false)
super.init(nibName: nil, bundle: nil)
}
@ -40,16 +38,15 @@ class RoomPreviewViewController: UIViewController {
fatalError("init(coder:) has not been implemented")
}
// MARK: - Lifecyle
// MARK: - Lifecycle
override func viewDidLoad() {
super.viewDidLoad()
let cell = RoomCollectionViewCell(frame: CGRect(origin: .zero, size: Constants.size))
cell.render(cellData)
view.vc_addSubViewMatchingParent(cell)
guard let snapshotView = snapshotView else { return }
view.vc_addSubViewMatchingParent(snapshotView)
preferredContentSize = Constants.size
preferredContentSize = snapshotView.bounds.size
}
}

View file

@ -673,15 +673,19 @@
- (UIContextMenuConfiguration *)collectionView:(UICollectionView *)collectionView contextMenuConfigurationForItemAtIndexPath:(NSIndexPath *)indexPath point:(CGPoint)point API_AVAILABLE(ios(13.0))
{
id<MXKRecentCellDataStoring> cellData = [recentsDataSource cellDataAtIndexPath:[NSIndexPath indexPathForRow:indexPath.item inSection:collectionView.tag]];
UIView *cell = [collectionView cellForItemAtIndexPath:indexPath];
MXRoom *room = [self.dataSource getRoomAtIndexPath:[NSIndexPath indexPathForRow:indexPath.row inSection:collectionView.tag]];
NSString *roomId = room.roomId;
MXWeakify(self);
MXWeakify(room);
return [UIContextMenuConfiguration configurationWithIdentifier:roomId previewProvider:^UIViewController * _Nullable {
// Add a preview using the cell's data to prevent the avatar and displayname from changing with a room list update.
return [[RoomPreviewViewController alloc] initWithCellData:cellData];
return [[ContextMenuSnapshotPreviewViewController alloc] initWithView:cell];
} actionProvider:^UIMenu * _Nullable(NSArray<UIMenuElement *> * _Nonnull suggestedActions) {
MXWeakify(self);
MXStrongifyAndReturnValueIfNil(room, nil);
BOOL isDirect = room.isDirect;
UIAction *directChatAction = [UIAction actionWithTitle:isDirect ? VectorL10n.homeContextMenuMakeRoom : VectorL10n.homeContextMenuMakeDm
@ -736,9 +740,8 @@
}];
BOOL isLowPriority = (currentTag && [kMXRoomTagLowPriority isEqualToString:currentTag.name]);
UIImage *lowPriorityImage = [UIImage systemImageNamed:isLowPriority ? @"arrow.up" : @"arrow.down"];
UIAction *lowPriorityAction = [UIAction actionWithTitle:isLowPriority ? VectorL10n.homeContextMenuNormalPriority : VectorL10n.homeContextMenuLowPriority
image:lowPriorityImage
image:[UIImage systemImageNamed:isLowPriority ? @"arrow.up" : @"arrow.down"]
identifier:nil
handler:^(__kindof UIAction * _Nonnull action) {
MXStrongifyAndReturnIfNil(self);

View file

@ -50,7 +50,6 @@
#import "PlainRoomTimelineCellProvider.h"
#import "BubbleRoomTimelineCellProvider.h"
#import "RoomSelectedStickerBubbleCell.h"
#import "RoomCollectionViewCell.h"
// MatrixKit common imports, shared with all targets
#import "MatrixKit-Bridging-Header.h"