123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- //
- // KMBlankView.m
- // PDF Reader Pro Edition
- //
- // Created by zhipeng jiang on 2020/12/23.
- //
- #import "KMBlankView.h"
- @interface KMBlankView ()
- @property (nonatomic, strong) NSButton *button;
- @end
- @implementation KMBlankView
- -(void)awakeFromNib {
- [super awakeFromNib];
- self.titleLabel.font = [NSFont systemFontOfSize:14 weight:NSFontWeightSemibold];
- self.secondTitleLabel.font = [NSFont systemFontOfSize:14];
-
- self.titleLabel.stringValue = @"";
- self.secondTitleLabel.stringValue = @"";
- [self addTrackingArea];
- self.wantsLayer = YES;
-
- [self.customView addSubview:self.button];
-
- [self registerForDraggedTypes:[NSArray arrayWithObjects:NSFilenamesPboardType, nil]];
- }
- - (void)layout {
- [super layout];
-
- self.button.frame = self.imageView.frame;
- }
- #pragma mark -
- #pragma mark - Drag
- - (void)draggingExited:(nullable id <NSDraggingInfo>)sender {}
- - (NSDragOperation)draggingEntered:(id<NSDraggingInfo>)sender {
- NSPasteboard *pboard = [sender draggingPasteboard];
- BOOL isCanDrag = NO;
- NSDragOperation result = NSDragOperationNone;
- if ([pboard availableTypeFromArray:[NSArray arrayWithObject:NSFilenamesPboardType]])
- {
- NSArray *fileNames = [pboard propertyListForType:NSFilenamesPboardType];
- for (NSString* path in fileNames) {
- if ([self.allowedFileTypes containsObject:path.pathExtension.lowercaseString]) {
- isCanDrag = YES;
- } else {
- isCanDrag = NO;
- break;
- }
- }
- }
- self.layer.borderColor = [NSColor clearColor].CGColor;
- if (isCanDrag) {
-
- result = NSDragOperationCopy;
- }
- return result;
- }
- - (BOOL)prepareForDragOperation:(id<NSDraggingInfo>)sender
- {
- NSPasteboard *pboard = [sender draggingPasteboard];
- if ([pboard availableTypeFromArray:[NSArray arrayWithObject:NSFilenamesPboardType]]) {
- NSMutableArray *fileNames = [pboard propertyListForType:NSFilenamesPboardType];
- if (self.dragSuccessBlock) {
- self.dragSuccessBlock(fileNames);
- }
- }
- return YES;
- }
- #pragma mark -
- #pragma mark - mouse Methods
- - (void)addTrackingArea {
- NSTrackingArea *trackingArea = [[NSTrackingArea alloc] initWithRect:self.imageView.bounds options:NSTrackingMouseEnteredAndExited | NSTrackingActiveAlways owner:self userInfo:nil];
- [self.imageView addTrackingArea:trackingArea];
- }
- - (void)mouseEntered:(NSEvent *)event {
- [super mouseEntered:event];
- if (self.mouseActionCallBack) {
- self.mouseActionCallBack(KMBlankViewMouseEventType_MouseEnter);
- }
- }
- - (void)mouseExited:(NSEvent *)event {
- [super mouseExited:event];
-
- if (self.mouseActionCallBack) {
- self.mouseActionCallBack(KMBlankViewMouseEventType_MouseExit);
- }
- }
- //- (void)mouseDown:(NSEvent *)event {
- // [super mouseDown:event];
- // NSPoint point = [event locationInWindow];
- // point = [self convertPoint:point toView:self.imageView];
- // if (self.mouseActionCallBack && CGRectContainsPoint(self.imageView.bounds, point)) {
- // self.mouseActionCallBack(KMBlankViewMouseEventType_MouseDown);
- // }
- //}
- - (void)mouseUp:(NSEvent *)event {
- [super mouseUp:event];
- NSPoint point = [event locationInWindow];
- point = [self convertPoint:point toView:self.imageView];
- if (self.mouseActionCallBack && CGRectContainsPoint(self.imageView.bounds, point)) {
- self.mouseActionCallBack(KMBlankViewMouseEventType_MouseUp);
- }
- }
- #pragma mark -
- #pragma mark - User Actions
- - (void)buttonDidClick {
- if (self.mouseActionCallBack) {
- self.mouseActionCallBack(KMBlankViewMouseEventType_MouseDown);
- }
- }
- #pragma mark -
- #pragma mark - Getting
- - (NSButton *)button {
- if (!_button) {
- _button = [[NSButton alloc] init];
- _button.bordered = false;
- _button.title = @"";
- _button.target = self;
- _button.action = @selector(buttonDidClick);
- }
- return _button;
- }
- @end
|