[vlc-commits] macosx: add custom view to show images

Felix Paul Kühne git at videolan.org
Mon Apr 29 19:26:47 CEST 2019


vlc | branch: master | Felix Paul Kühne <felix at feepk.net> | Sun Apr 21 20:13:31 2019 +0200| [2f12c304ac13d01d059ebbc61a138f6640b79d2e] | committer: Felix Paul Kühne

macosx: add custom view to show images

This custom class allows a plethora of content gravities like its UIKit equivaltent

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

 .../package/macosx/VLC.xcodeproj/project.pbxproj   |   6 ++
 modules/gui/macosx/Makefile.am                     |   2 +
 modules/gui/macosx/views/VLCImageView.h            |  45 +++++++++
 modules/gui/macosx/views/VLCImageView.m            | 108 +++++++++++++++++++++
 po/POTFILES.in                                     |   2 +
 5 files changed, 163 insertions(+)

diff --git a/extras/package/macosx/VLC.xcodeproj/project.pbxproj b/extras/package/macosx/VLC.xcodeproj/project.pbxproj
index bba52781d4..23632d473a 100644
--- a/extras/package/macosx/VLC.xcodeproj/project.pbxproj
+++ b/extras/package/macosx/VLC.xcodeproj/project.pbxproj
@@ -153,6 +153,7 @@
 		7DFBDCAE2269ED0C00B700A5 /* VLCLibraryDataSource.m in Sources */ = {isa = PBXBuildFile; fileRef = 7DFBDCAD2269ED0C00B700A5 /* VLCLibraryDataSource.m */; };
 		7DFBDCB1226A518400B700A5 /* VLCLibraryMenuController.m in Sources */ = {isa = PBXBuildFile; fileRef = 7DFBDCB0226A518400B700A5 /* VLCLibraryMenuController.m */; };
 		7DFBDCB4226CD00900B700A5 /* VLCLibraryDataTypes.m in Sources */ = {isa = PBXBuildFile; fileRef = 7DFBDCB3226CD00900B700A5 /* VLCLibraryDataTypes.m */; };
+		7DFBDCB7226CDFD600B700A5 /* VLCImageView.m in Sources */ = {isa = PBXBuildFile; fileRef = 7DFBDCB6226CDFD600B700A5 /* VLCImageView.m */; };
 /* End PBXBuildFile section */
 
 /* Begin PBXFileReference section */
@@ -518,6 +519,8 @@
 		7DFBDCB0226A518400B700A5 /* VLCLibraryMenuController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = VLCLibraryMenuController.m; sourceTree = "<group>"; };
 		7DFBDCB2226CD00900B700A5 /* VLCLibraryDataTypes.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = VLCLibraryDataTypes.h; sourceTree = "<group>"; };
 		7DFBDCB3226CD00900B700A5 /* VLCLibraryDataTypes.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = VLCLibraryDataTypes.m; sourceTree = "<group>"; };
+		7DFBDCB5226CDFD600B700A5 /* VLCImageView.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = VLCImageView.h; sourceTree = "<group>"; };
+		7DFBDCB6226CDFD600B700A5 /* VLCImageView.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = VLCImageView.m; sourceTree = "<group>"; };
 		8E49720006417F6800370C9F /* VLCInformationWindowController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = VLCInformationWindowController.h; sourceTree = "<group>"; };
 		8E49720106417F6800370C9F /* VLCInformationWindowController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = VLCInformationWindowController.m; sourceTree = "<group>"; };
 		8E55FB7F0459B0FD00FB3317 /* VLCOutput.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = VLCOutput.h; sourceTree = "<group>"; };
@@ -778,6 +781,8 @@
 				6B2EFC621F281A0900F3C0EA /* VLCVolumeSliderCell.m */,
 				1C1C61FF1F8260A90052DD4F /* VLCWrappableTextField.h */,
 				1C1C62001F8260A90052DD4F /* VLCWrappableTextField.m */,
+				7DFBDCB5226CDFD600B700A5 /* VLCImageView.h */,
+				7DFBDCB6226CDFD600B700A5 /* VLCImageView.m */,
 			);
 			path = views;
 			sourceTree = "<group>";
@@ -1558,6 +1563,7 @@
 				1C31138E1E508C6900D4DD76 /* VLCAboutWindowController.m in Sources */,
 				1C3113901E508C6900D4DD76 /* VLCHelpWindowController.m in Sources */,
 				1C3113921E508C6900D4DD76 /* VLCAddonListItem.m in Sources */,
+				7DFBDCB7226CDFD600B700A5 /* VLCImageView.m in Sources */,
 				7DE9C7DD220728420089108F /* VLCPlayerController.m in Sources */,
 				1C3113941E508C6900D4DD76 /* VLCAddonsWindowController.m in Sources */,
 				7D2E0EDE20CD206F0033A221 /* VLCVideoWindowCommon.m in Sources */,
diff --git a/modules/gui/macosx/Makefile.am b/modules/gui/macosx/Makefile.am
index 501fa05eea..5a0a57574d 100644
--- a/modules/gui/macosx/Makefile.am
+++ b/modules/gui/macosx/Makefile.am
@@ -153,6 +153,8 @@ libmacosx_plugin_la_SOURCES = \
 	gui/macosx/views/VLCHUDTableView.m \
 	gui/macosx/views/VLCImageButton.h \
 	gui/macosx/views/VLCImageButton.m \
+	gui/macosx/views/VLCImageView.h \
+	gui/macosx/views/VLCImageView.m \
 	gui/macosx/views/VLCScrollingClipView.h \
 	gui/macosx/views/VLCScrollingClipView.m \
 	gui/macosx/views/VLCSlider.h \
diff --git a/modules/gui/macosx/views/VLCImageView.h b/modules/gui/macosx/views/VLCImageView.h
new file mode 100644
index 0000000000..46e73f95c1
--- /dev/null
+++ b/modules/gui/macosx/views/VLCImageView.h
@@ -0,0 +1,45 @@
+/*****************************************************************************
+ * VLCImageView.h: MacOS X interface module
+ *****************************************************************************
+ * Copyright (C) 2019 VLC authors and VideoLAN
+ *
+ * Authors: Felix Paul Kühne <fkuehne # videolan -dot- org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ *****************************************************************************/
+
+#import <Cocoa/Cocoa.h>
+
+typedef NS_ENUM(NSInteger, VLCImageViewContentGravity) {
+    VLCImageViewContentGravityCenter,
+    VLCImageViewContentGravityTop,
+    VLCImageViewContentGravityBottom,
+    VLCImageViewContentGravityLeft,
+    VLCImageViewContentGravityRight,
+    VLCImageViewContentGravityTopLeft,
+    VLCImageViewContentGravityTopRight,
+    VLCImageViewContentGravityBottomLeft,
+    VLCImageViewContentGravityBottomRight,
+    VLCImageViewContentGravityResize,
+    VLCImageViewContentGravityResizeAspect,
+    VLCImageViewContentGravityResizeAspectFill,
+};
+
+ at interface VLCImageView : NSView
+
+ at property (readwrite, retain, nonatomic, nullable) NSImage *image;
+ at property (readwrite) VLCImageViewContentGravity contentGravity;
+
+ at end
diff --git a/modules/gui/macosx/views/VLCImageView.m b/modules/gui/macosx/views/VLCImageView.m
new file mode 100644
index 0000000000..e3bd78698a
--- /dev/null
+++ b/modules/gui/macosx/views/VLCImageView.m
@@ -0,0 +1,108 @@
+/*****************************************************************************
+ * VLCImageView.m: MacOS X interface module
+ *****************************************************************************
+ * Copyright (C) 2019 VLC authors and VideoLAN
+ *
+ * Authors: Felix Paul Kühne <fkuehne # videolan -dot- org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ *****************************************************************************/
+
+#import "VLCImageView.h"
+
+ at implementation VLCImageView
+
+- (instancetype)initWithFrame:(NSRect)frameRect
+{
+    self = [super initWithFrame:frameRect];
+    if (self) {
+        [self setupLayer];
+    }
+    return self;
+}
+
+- (instancetype)initWithCoder:(NSCoder *)decoder
+{
+    self = [super initWithCoder:decoder];
+    if (self) {
+        [self setupLayer];
+    }
+    return self;
+}
+
+- (void)setupLayer
+{
+    self.layer = [[CALayer alloc] init];
+    self.contentGravity = VLCImageViewContentGravityResizeAspectFill;
+    self.wantsLayer = YES;
+}
+
+- (void)setImage:(NSImage *)image
+{
+    _image = image;
+    CGFloat desiredScaleFactor = [self.window backingScaleFactor];
+    CGFloat actualScaleFactor = [image recommendedLayerContentsScale:desiredScaleFactor];
+
+    id layerContents = [image layerContentsForContentsScale:actualScaleFactor];
+
+    [self setCAContentGravity:_contentGravity];
+    [self.layer setContents:layerContents];
+    [self.layer setContentsScale:actualScaleFactor];
+}
+
+- (void)setCAContentGravity:(VLCImageViewContentGravity)contentGravity
+{
+    switch (contentGravity) {
+        case VLCImageViewContentGravityCenter:
+            self.layer.contentsGravity = kCAGravityCenter;
+            break;
+        case VLCImageViewContentGravityTop:
+            self.layer.contentsGravity = kCAGravityTop;
+            break;
+        case VLCImageViewContentGravityBottom:
+            self.layer.contentsGravity = kCAGravityBottom;
+            break;
+        case VLCImageViewContentGravityLeft:
+            self.layer.contentsGravity = kCAGravityLeft;
+            break;
+        case VLCImageViewContentGravityRight:
+            self.layer.contentsGravity = kCAGravityRight;
+            break;
+        case VLCImageViewContentGravityTopLeft:
+            self.layer.contentsGravity = kCAGravityTopLeft;
+            break;
+        case VLCImageViewContentGravityTopRight:
+            self.layer.contentsGravity = kCAGravityTopRight;
+            break;
+        case VLCImageViewContentGravityBottomLeft:
+            self.layer.contentsGravity = kCAGravityBottomLeft;
+            break;
+        case VLCImageViewContentGravityBottomRight:
+            self.layer.contentsGravity = kCAGravityBottomRight;
+            break;
+        case VLCImageViewContentGravityResize:
+            self.layer.contentsGravity = kCAGravityResize;
+            break;
+        case VLCImageViewContentGravityResizeAspect:
+            self.layer.contentsGravity = kCAGravityResizeAspect;
+            break;
+        case VLCImageViewContentGravityResizeAspectFill:
+        default:
+            self.layer.contentsGravity = kCAGravityResizeAspectFill;
+            break;
+    }
+}
+
+ at end
diff --git a/po/POTFILES.in b/po/POTFILES.in
index 86bbe53ce6..495eb3cb3f 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -569,6 +569,8 @@ modules/gui/macosx/views/VLCHUDTableView.h
 modules/gui/macosx/views/VLCHUDTableView.m
 modules/gui/macosx/views/VLCImageButton.h
 modules/gui/macosx/views/VLCImageButton.m
+modules/gui/macosx/views/VLCImageView.h
+modules/gui/macosx/views/VLCImageView.m
 modules/gui/macosx/views/VLCScrollingClipView.h
 modules/gui/macosx/views/VLCScrollingClipView.m
 modules/gui/macosx/views/VLCSlider.h



More information about the vlc-commits mailing list