1 /*****************************************************************************
2 * VLCScrollingClipView.m: NSClipView subclass that automatically scrolls
3 *****************************************************************************
4 * Copyright (C) 2015 VLC authors and VideoLAN
7 * Author: Marvin Scholz <epirat07@gmail.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 #import "VLCScrollingClipView.h"
26 @implementation VLCScrollingClipView {
28 NSTimeInterval startInterval;
31 - (void)startScrolling {
32 // Start our timer unless running
34 scrollTimer = [NSTimer scheduledTimerWithTimeInterval: 1/6
36 selector:@selector(scrollTick:)
42 - (void)stopScrolling {
43 // Stop timer unless stopped
45 [scrollTimer invalidate];
50 - (void)resetScrolling {
52 [self scrollToPoint:NSMakePoint(0.0, 0.0)];
53 if (_parentScrollView) {
54 [_parentScrollView reflectScrolledClipView:self];
58 - (void)scrollWheel:(NSEvent *)theEvent {
59 // Stop auto-scrolling if the user scrolls
61 [super scrollWheel:theEvent];
64 - (void)scrollTick:(NSTimer *)timer {
65 CGFloat maxHeight = self.documentRect.size.height;
66 CGFloat scrollPosition = self.documentVisibleRect.origin.y;
69 if (scrollPosition == 0.0 && !startInterval) {
70 startInterval = [NSDate timeIntervalSinceReferenceDate] + 2.0;
73 if ([NSDate timeIntervalSinceReferenceDate] >= startInterval) {
74 // Reset if we are past the end
75 if (scrollPosition > maxHeight) {
76 [self resetScrolling];
80 // Scroll the view a bit on each tick
81 [self scrollToPoint:NSMakePoint(0.0, scrollPosition + 0.5)];
83 // Update the parent ScrollView
84 if (_parentScrollView) {
85 [_parentScrollView reflectScrolledClipView:self];