123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191 |
- //
- // SKProgressController.m
- // Skim
- //
- // Created by Christiaan Hofman on 9/16/07.
- /*
- This software is Copyright (c) 2007-2018
- Christiaan Hofman. All rights reserved.
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- - Redistributions of source code must retain the above copyright
- notice, this list of conditions and the following disclaimer.
- - Redistributions in binary form must reproduce the above copyright
- notice, this list of conditions and the following disclaimer in
- the documentation and/or other materials provided with the
- distribution.
- - Neither the name of Christiaan Hofman nor the names of any
- contributors may be used to endorse or promote products derived
- from this software without specific prior written permission.
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
- OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
- #import "SKProgressController.h"
- @interface SKProgressController_ProgressIndicator : NSProgressIndicator
- @end
- @implementation SKProgressController_ProgressIndicator
- - (instancetype)init {
- if (self = [super init]) {
-
- }
- return self;
- }
- - (void)drawRect:(NSRect)dirtyRect {
- NSColor *color = [NSColor colorWithRed:206/255.f green:208/255.f blue:212/255.f alpha:1.f];
- [color setStroke];
- [color setFill];
- NSRect lineRect = NSMakeRect(1.5, (NSHeight(self.bounds)-2.4)*0.5, NSWidth(self.bounds)-2, 2.4);
- NSBezierPath *linePath = [NSBezierPath bezierPathWithRoundedRect:lineRect xRadius:0 yRadius:0];
- [linePath fill];
- [linePath stroke];
-
- [super drawRect:dirtyRect];
- }
- @end
- @implementation SKProgressController
- @synthesize progressBar, progressField;
- @dynamic message, indeterminate, maxValue, doubleValue;
- - (void)dealloc {}
- - (NSString *)windowNibName {
- return @"ProgressSheet";
- }
- - (instancetype)init {
- if (self = [super init]) {
- self.showClose = YES;
- }
- return self;
- }
- - (void)windowDidLoad {
- [progressBar setUsesThreadedAnimation:YES];
-
- self.window.appearance = [NSAppearance appearanceNamed:NSAppearanceNameAqua];
-
- NSButton *button = [[NSButton alloc] init];
- [self.window.contentView addSubview:button];
-
- CGFloat width = NSWidth(self.window.contentView.frame);
- CGFloat height = NSWidth(self.window.contentView.frame);
- CGSize size = CGSizeMake(20, 20);
- CGFloat buttonY = 28;
- CGFloat buttonX = width-size.width-10;
- button.frame = NSMakeRect(buttonX, buttonY, size.width, size.height);
- button.autoresizingMask = NSViewMinXMargin | NSViewMaxYMargin;
- button.bordered = NO;
- button.image = [NSImage imageNamed:@"KMImageNameWhiteClose"];
- button.target = self;
- button.action = @selector(buttonAction:);
-
- button.hidden = !self.showClose;
- }
- - (NSProgressIndicator *)progressBar {
- [self window];
- return progressBar;
- }
- - (NSString *)message {
- [self window];
- return [progressField stringValue];
- }
- - (void)setMessage:(NSString *)newMessage {
- [self window];
- [progressField setStringValue:newMessage];
- [[self window] setTitle:newMessage];
- }
- - (BOOL)isIndeterminate {
- return [[self progressBar] isIndeterminate];
- }
- - (void)setIndeterminate:(BOOL)flag {
- [[self progressBar] setIndeterminate:flag];
- }
- - (double)maxValue {
- return [[self progressBar] maxValue];
- }
- - (void)setMaxValue:(double)newMaximum {
- [[self progressBar] setMaxValue:newMaximum];
- [[self progressBar] setDoubleValue:0.0];
- }
- - (double)doubleValue {
- return [[self progressBar] doubleValue];
- }
- - (void)setDoubleValue:(double)doubleValue {
- dispatch_async(dispatch_get_main_queue(), ^{
- [[self progressBar] setDoubleValue:doubleValue];
- [[self progressBar] displayIfNeeded];
- });
- }
- - (void)incrementBy:(double)delta {
- [[self progressBar] incrementBy:delta];
- [[self progressBar] displayIfNeeded];
- }
- - (void)buttonAction:(NSButton *)sender {
- if (self.closeBlock) {
- self.closeBlock();
- }
-
- [self dismissSheet:nil];
- }
- - (void)beginSheetModalForWindow:(NSWindow *)window completionHandler:(void (^)(NSInteger result))handler {
- [[self progressBar] startAnimation:self];
- // [(SKApplication *)NSApp setUserAttentionDisabled:YES];
- // [super beginSheetModalForWindow:window completionHandler:handler];
- [NSApp beginSheet:[self window]
- modalForWindow:window
- modalDelegate:self
- didEndSelector:@selector(didEndSheet:returnCode:contextInfo:)
- contextInfo:handler ? CFBridgingRetain(handler) : NULL];
-
- // [(SKApplication *)NSApp setUserAttentionDisabled:NO];
- }
- - (IBAction)dismissSheet:(id)sender {
- [[self progressBar] stopAnimation:self];
- // [super dismissSheet:sender];
- if (@available(macOS 10.13, *)) {
- [NSApp endSheet:[self window] returnCode:[sender tag]];
- } else {
- [NSApp endSheet:[self window]];
- }
- [[self window] orderOut:self];
- }
- @end
|