[vlc-commits] macosx/drag & drop view: modernize code

Felix Paul Kühne git at videolan.org
Wed May 29 15:38:48 CEST 2019


vlc | branch: master | Felix Paul Kühne <felix at feepk.net> | Wed May 29 15:17:55 2019 +0200| [6609dce76e9c4d1cd4dd7da5f344e2611ffa8251] | committer: Felix Paul Kühne

macosx/drag & drop view: modernize code

Use CALayer instead of drawRect to show the focus ring and use an explicit instead of an implicit protocol for the handler

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=6609dce76e9c4d1cd4dd7da5f344e2611ffa8251
---

 modules/gui/macosx/views/VLCDragDropView.h         |  7 ++-
 modules/gui/macosx/views/VLCDragDropView.m         | 60 ++++++++++------------
 .../VLCConvertAndSaveWindowController.m            |  7 ++-
 3 files changed, 36 insertions(+), 38 deletions(-)

diff --git a/modules/gui/macosx/views/VLCDragDropView.h b/modules/gui/macosx/views/VLCDragDropView.h
index 045979cdd6..02ff245f4f 100644
--- a/modules/gui/macosx/views/VLCDragDropView.h
+++ b/modules/gui/macosx/views/VLCDragDropView.h
@@ -34,9 +34,14 @@ NS_ASSUME_NONNULL_BEGIN
 
 @end
 
+ at protocol VLCDragDropTarget
+ at required
+- (BOOL)handlePasteBoardFromDragSession:(NSPasteboard *)aPasteboard;
+ at end
+
 @interface VLCDragDropView : NSView
 
- at property (nonatomic, assign) id dropHandler;
+ at property (nonatomic, assign) id<VLCDragDropTarget> dropTarget;
 @property (nonatomic, assign) BOOL drawBorder;
 
 - (void)enablePlaylistItems;
diff --git a/modules/gui/macosx/views/VLCDragDropView.m b/modules/gui/macosx/views/VLCDragDropView.m
index e73b35d322..5daa1f5e77 100644
--- a/modules/gui/macosx/views/VLCDragDropView.m
+++ b/modules/gui/macosx/views/VLCDragDropView.m
@@ -33,12 +33,6 @@
 
 @end
 
- at interface VLCDragDropView()
-{
-    bool b_activeDragAndDrop;
-}
- at end
-
 @implementation VLCDragDropView
 
 - (id)initWithFrame:(NSRect)frame
@@ -48,7 +42,6 @@
         // default value
         [self setDrawBorder:YES];
     }
-
     return self;
 }
 
@@ -70,30 +63,36 @@
 - (void)awakeFromNib
 {
     [self registerForDraggedTypes:[NSArray arrayWithObject:NSFilenamesPboardType]];
+    self.wantsLayer = YES;
+    self.layer.cornerRadius = 5.;
+    [self updateBorderColor];
+}
+
+- (void)updateBorderColor
+{
+    self.layer.borderColor = [NSColor selectedControlColor].CGColor;
 }
 
 - (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
 {
     if ((NSDragOperationGeneric & [sender draggingSourceOperationMask]) == NSDragOperationGeneric) {
-        b_activeDragAndDrop = YES;
-        [self setNeedsDisplay:YES];
-
+        if (self.drawBorder) {
+            self.layer.borderWidth = 5.;
+        }
         return NSDragOperationCopy;
     }
 
     return NSDragOperationNone;
 }
 
-- (void)draggingEnded:(id < NSDraggingInfo >)sender
+- (void)draggingEnded:(id <NSDraggingInfo>)sender
 {
-    b_activeDragAndDrop = NO;
-    [self setNeedsDisplay:YES];
+    self.layer.borderWidth = 0.;
 }
 
-- (void)draggingExited:(id < NSDraggingInfo >)sender
+- (void)draggingExited:(id <NSDraggingInfo>)sender
 {
-    b_activeDragAndDrop = NO;
-    [self setNeedsDisplay:YES];
+    self.layer.borderWidth = 0.;
 }
 
 - (BOOL)prepareForDragOperation:(id <NSDraggingInfo>)sender
@@ -103,33 +102,28 @@
 
 - (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
 {
-    BOOL b_returned = NO;
+    BOOL returnValue = NO;
+    NSPasteboard *pasteBoard = [sender draggingPasteboard];
+    if (!pasteBoard) {
+        return NO;
+    }
 
-    if (_dropHandler && [_dropHandler respondsToSelector:@selector(performDragOperation:)])
-        b_returned = [_dropHandler performDragOperation:sender];
-    // default
-    // FIXME: implement drag and drop _on_ new playlist
-    //        b_returned = [[[VLCMain sharedInstance] playlist] performDragOperation:sender];
+    if (_dropTarget) {
+        returnValue = [_dropTarget handlePasteBoardFromDragSession:pasteBoard];
+    }
 
     [self setNeedsDisplay:YES];
-    return b_returned;
+    return returnValue;
 }
 
 - (void)concludeDragOperation:(id <NSDraggingInfo>)sender
 {
-    [self setNeedsDisplay:YES];
+    self.layer.borderWidth = 0.;
 }
 
-- (void)drawRect:(NSRect)dirtyRect
+- (void)viewDidChangeEffectiveAppearance
 {
-    if ([self drawBorder] && b_activeDragAndDrop) {
-        NSRect frameRect = [self bounds];
-
-        [[NSColor selectedControlColor] set];
-        NSFrameRectWithWidthUsingOperation(frameRect, 2., NSCompositeSourceOver);
-    }
-
-    [super drawRect:dirtyRect];
+    [self updateBorderColor];
 }
 
 @end
diff --git a/modules/gui/macosx/windows/convertandsave/VLCConvertAndSaveWindowController.m b/modules/gui/macosx/windows/convertandsave/VLCConvertAndSaveWindowController.m
index 8d2ddcbc85..1cf8bf93f1 100644
--- a/modules/gui/macosx/windows/convertandsave/VLCConvertAndSaveWindowController.m
+++ b/modules/gui/macosx/windows/convertandsave/VLCConvertAndSaveWindowController.m
@@ -51,7 +51,7 @@
 #define ASF 12
 /* 13-15 are present, but not set */
 
- at interface VLCConvertAndSaveWindowController()
+ at interface VLCConvertAndSaveWindowController() <VLCDragDropTarget>
 {
     NSArray *_videoCodecs;
     NSArray *_audioCodecs;
@@ -255,7 +255,7 @@
 
     // setup drop view
     [_dropBox enablePlaylistItems];
-    [_dropBox setDropHandler: self];
+    [_dropBox setDropTarget:self];
 
     [self resetCustomizationSheetBasedOnProfile:[self.profileValueList firstObject]];
 }
@@ -615,9 +615,8 @@
 #pragma mark -
 #pragma mark User interaction - misc
 
-- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
+- (BOOL)handlePasteBoardFromDragSession:(NSPasteboard *)paste
 {
-    NSPasteboard *paste = [sender draggingPasteboard];
     NSArray *types = [NSArray arrayWithObjects:NSFilenamesPboardType, @"VLCPlaylistItemPboardType", nil];
     NSString *desired_type = [paste availableTypeFromArray: types];
     NSData *carried_data = [paste dataForType: desired_type];



More information about the vlc-commits mailing list