KMBlankView.m 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. //
  2. // KMBlankView.m
  3. // PDF Reader Pro Edition
  4. //
  5. // Created by zhipeng jiang on 2020/12/23.
  6. //
  7. #import "KMBlankView.h"
  8. @interface KMBlankView ()
  9. @property (nonatomic, strong) NSButton *button;
  10. @end
  11. @implementation KMBlankView
  12. -(void)awakeFromNib {
  13. [super awakeFromNib];
  14. self.titleLabel.font = [NSFont systemFontOfSize:14 weight:NSFontWeightSemibold];
  15. self.secondTitleLabel.font = [NSFont systemFontOfSize:14];
  16. self.titleLabel.stringValue = @"";
  17. self.secondTitleLabel.stringValue = @"";
  18. [self addTrackingArea];
  19. self.wantsLayer = YES;
  20. [self.customView addSubview:self.button];
  21. [self registerForDraggedTypes:[NSArray arrayWithObjects:NSFilenamesPboardType, nil]];
  22. }
  23. - (void)layout {
  24. [super layout];
  25. self.button.frame = self.imageView.frame;
  26. }
  27. #pragma mark -
  28. #pragma mark - Drag
  29. - (void)draggingExited:(nullable id <NSDraggingInfo>)sender {}
  30. - (NSDragOperation)draggingEntered:(id<NSDraggingInfo>)sender {
  31. NSPasteboard *pboard = [sender draggingPasteboard];
  32. BOOL isCanDrag = NO;
  33. NSDragOperation result = NSDragOperationNone;
  34. if ([pboard availableTypeFromArray:[NSArray arrayWithObject:NSFilenamesPboardType]])
  35. {
  36. NSArray *fileNames = [pboard propertyListForType:NSFilenamesPboardType];
  37. for (NSString* path in fileNames) {
  38. if ([self.allowedFileTypes containsObject:path.pathExtension.lowercaseString]) {
  39. isCanDrag = YES;
  40. } else {
  41. isCanDrag = NO;
  42. break;
  43. }
  44. }
  45. }
  46. self.layer.borderColor = [NSColor clearColor].CGColor;
  47. if (isCanDrag) {
  48. result = NSDragOperationCopy;
  49. }
  50. return result;
  51. }
  52. - (BOOL)prepareForDragOperation:(id<NSDraggingInfo>)sender
  53. {
  54. NSPasteboard *pboard = [sender draggingPasteboard];
  55. if ([pboard availableTypeFromArray:[NSArray arrayWithObject:NSFilenamesPboardType]]) {
  56. NSMutableArray *fileNames = [pboard propertyListForType:NSFilenamesPboardType];
  57. if (self.dragSuccessBlock) {
  58. self.dragSuccessBlock(fileNames);
  59. }
  60. }
  61. return YES;
  62. }
  63. #pragma mark -
  64. #pragma mark - mouse Methods
  65. - (void)addTrackingArea {
  66. NSTrackingArea *trackingArea = [[NSTrackingArea alloc] initWithRect:self.imageView.bounds options:NSTrackingMouseEnteredAndExited | NSTrackingActiveAlways owner:self userInfo:nil];
  67. [self.imageView addTrackingArea:trackingArea];
  68. }
  69. - (void)mouseEntered:(NSEvent *)event {
  70. [super mouseEntered:event];
  71. if (self.mouseActionCallBack) {
  72. self.mouseActionCallBack(KMBlankViewMouseEventType_MouseEnter);
  73. }
  74. }
  75. - (void)mouseExited:(NSEvent *)event {
  76. [super mouseExited:event];
  77. if (self.mouseActionCallBack) {
  78. self.mouseActionCallBack(KMBlankViewMouseEventType_MouseExit);
  79. }
  80. }
  81. //- (void)mouseDown:(NSEvent *)event {
  82. // [super mouseDown:event];
  83. // NSPoint point = [event locationInWindow];
  84. // point = [self convertPoint:point toView:self.imageView];
  85. // if (self.mouseActionCallBack && CGRectContainsPoint(self.imageView.bounds, point)) {
  86. // self.mouseActionCallBack(KMBlankViewMouseEventType_MouseDown);
  87. // }
  88. //}
  89. - (void)mouseUp:(NSEvent *)event {
  90. [super mouseUp:event];
  91. NSPoint point = [event locationInWindow];
  92. point = [self convertPoint:point toView:self.imageView];
  93. if (self.mouseActionCallBack && CGRectContainsPoint(self.imageView.bounds, point)) {
  94. self.mouseActionCallBack(KMBlankViewMouseEventType_MouseUp);
  95. }
  96. }
  97. #pragma mark -
  98. #pragma mark - User Actions
  99. - (void)buttonDidClick {
  100. if (self.mouseActionCallBack) {
  101. self.mouseActionCallBack(KMBlankViewMouseEventType_MouseDown);
  102. }
  103. }
  104. #pragma mark -
  105. #pragma mark - Getting
  106. - (NSButton *)button {
  107. if (!_button) {
  108. _button = [[NSButton alloc] init];
  109. _button.bordered = false;
  110. _button.title = @"";
  111. _button.target = self;
  112. _button.action = @selector(buttonDidClick);
  113. }
  114. return _button;
  115. }
  116. @end