Newer
Older
/*****************************************************************************
* VLCLibraryHomeViewBaseCarouselContainerView.m: MacOS X interface module
*****************************************************************************
* Copyright (C) 2024 VLC authors and VideoLAN
*
* Authors: Claudio Cambra <developer@claudiocambra.com>
*
* 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 "VLCLibraryHomeViewBaseCarouselContainerView.h"

Claudio Cambra
committed
#import "extensions/NSFont+VLCAdditions.h"
#import "extensions/NSString+Helpers.h"
#import "library/VLCLibraryDataTypes.h"
#import "library/VLCLibraryCarouselViewItemView.h"
#import "library/VLCLibraryUIUnits.h"
@interface VLCLibraryHomeViewBaseCarouselContainerView ()
{
CGFloat _itemHeight;
}

Claudio Cambra
committed

Claudio Cambra
committed
@property (readonly) NSLayoutConstraint *heightConstraint;

Claudio Cambra
committed
@property (readwrite) VLCLibraryCarouselViewItemView *selectedItemView;
@end
@implementation VLCLibraryHomeViewBaseCarouselContainerView
@synthesize constraintsWithSuperview = _constraintsWithSuperview;

Claudio Cambra
committed
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
- (instancetype)init
{
self = [super init];
if (self) {
[self setup];
}
return self;
}
- (instancetype)initWithFrame:(NSRect)frameRect
{
self = [super initWithFrame:frameRect];
if (self) {
[self setup];
}
return self;
}
- (instancetype)initWithCoder:(NSCoder *)coder
{
self = [super initWithCoder:coder];
if (self) {
[self setup];
}
return self;
}
- (void)awakeFromNib
{
[super awakeFromNib];
[self setup];
}
- (void)setup
{
[self setupView];
}
- (void)setupView
{
_titleView = [[NSTextField alloc] init];
self.titleView.font = NSFont.VLClibrarySectionHeaderFont;
self.titleView.textColor = NSColor.headerTextColor;
self.titleView.selectable = NO;
self.titleView.bordered = NO;
self.titleView.drawsBackground = NO;
[self addSubview:self.titleView];
self.titleView.translatesAutoresizingMaskIntoConstraints = NO;
[NSLayoutConstraint activateConstraints:@[
[self.leadingAnchor constraintEqualToAnchor:self.titleView.leadingAnchor],
[self.trailingAnchor constraintEqualToAnchor:self.titleView.trailingAnchor],
[self.topAnchor constraintEqualToAnchor:self.titleView.topAnchor],
]];
_carouselView = [[iCarousel alloc] initWithFrame:self.bounds];
self.carouselView.delegate = self;
[self addSubview:self.carouselView];
self.carouselView.translatesAutoresizingMaskIntoConstraints = NO;
[NSLayoutConstraint activateConstraints:@[
[self.leadingAnchor constraintEqualToAnchor:self.carouselView.leadingAnchor],
[self.trailingAnchor constraintEqualToAnchor:self.carouselView.trailingAnchor],
[self.titleView.bottomAnchor constraintEqualToAnchor:self.carouselView.topAnchor], // titleView bottom
[self.bottomAnchor constraintEqualToAnchor:self.carouselView.bottomAnchor]
]];

Claudio Cambra
committed
_itemHeight = VLCLibraryUIUnits.carouselViewItemViewHeight;

Claudio Cambra
committed
[self updateCarouselViewHeight];
[self updateCarouselOffset];

Claudio Cambra
committed
self.carouselView.autoscroll = -.05;

Claudio Cambra
committed
}
- (void)updateCarouselViewHeight
{

Claudio Cambra
committed
const CGFloat viewHeight = self.titleView.frame.size.height +
VLCLibraryUIUnits.largeSpacing * 2 +

Claudio Cambra
committed
if (self.heightConstraint == nil) {
_heightConstraint = [self.carouselView.heightAnchor constraintEqualToConstant:viewHeight];
self.heightConstraint.active = YES;
} else {
self.heightConstraint.constant = viewHeight;
}

Claudio Cambra
committed
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
}
- (void)updateCarouselOffset
{
const CGFloat widthToFirstItemCenter = self.frame.size.width / 2;
const CGFloat leadingPadding = VLCLibraryUIUnits.largeSpacing;
const CGFloat itemWidth = self.carouselView.itemWidth;
const CGFloat horizontalOffset = (-(widthToFirstItemCenter - itemWidth / 2)) + leadingPadding;
self.carouselView.contentOffset = NSMakeSize(horizontalOffset, 0);
}
- (void)resizeWithOldSuperviewSize:(NSSize)oldSize
{
[super resizeWithOldSuperviewSize:oldSize];
[self updateCarouselOffset];
}
- (void)presentLibraryItem:(id<VLCMediaLibraryItemProtocol>)libraryItem
{
if (libraryItem == nil) {
return;
}
NSIndexPath * const itemIndexPath = [self.dataSource indexPathForLibraryItem:libraryItem];
if (itemIndexPath == nil) {
return;
}
const NSInteger itemIndex = itemIndexPath.item;
[self.carouselView scrollToItemAtIndex:itemIndex animated:YES];
}
- (CGFloat)itemHeight
{
return _itemHeight;
}
- (void)setItemHeight:(CGFloat)itemHeight
{
if (itemHeight == self.itemHeight) {
return;
}
_itemHeight = itemHeight;
[self updateCarouselViewHeight];
}

Claudio Cambra
committed
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
// pragma mark - iCarousel delegate methods
- (CGFloat)carousel:(iCarousel *)carousel
valueForOption:(iCarouselOption)option
withDefault:(CGFloat)value
{
switch (option) {
case iCarouselOptionSpacing:
{
// iCarousel calculates spacing as a multiplier on the item width.
// So a spacing of 1.2 means the item's width will grow to 1.2x its
// width, with the extra width as spacing.
//
// Because of this... interesting approach to spacing, we calculate
// the constant VLC-wide spacing relative to the width of the carousel's
// itemWidth.
const CGFloat itemWidth = carousel.itemWidth;
const CGFloat bothSidesSpacing = VLCLibraryUIUnits.mediumSpacing * 2;
const CGFloat desiredWidthWithSpacing = itemWidth + bothSidesSpacing;
const CGFloat desiredMultiple = desiredWidthWithSpacing / itemWidth;
return desiredMultiple;
}
case iCarouselOptionWrap:
return YES;
default:
return value;
}
}
- (void)carouselCurrentItemIndexDidChange:(iCarousel *)carousel
{
NSView * const currentItemView = carousel.currentItemView;
if (currentItemView == nil) {
return;
}
VLCLibraryCarouselViewItemView * const carouselItemView = (VLCLibraryCarouselViewItemView *)currentItemView;
NSAssert(carouselItemView != nil, @"Expected carousel item view to be non-nil!");
self.selectedItemView.selected = NO;
carouselItemView.selected = YES;
self.selectedItemView = carouselItemView;
}
- (void)carousel:(iCarousel *)carousel didSelectItemAtIndex:(NSInteger)index
{
VLCLibraryCarouselViewItemView * const carouselItemView = (VLCLibraryCarouselViewItemView *)[carousel itemViewAtIndex:index];
NSAssert(carouselItemView != nil, @"Expected carousel item view to be non-nil!");
[carouselItemView playRepresentedItem];
}