SKProgressController.m 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. //
  2. // SKProgressController.m
  3. // Skim
  4. //
  5. // Created by Christiaan Hofman on 9/16/07.
  6. /*
  7. This software is Copyright (c) 2007-2018
  8. Christiaan Hofman. All rights reserved.
  9. Redistribution and use in source and binary forms, with or without
  10. modification, are permitted provided that the following conditions
  11. are met:
  12. - Redistributions of source code must retain the above copyright
  13. notice, this list of conditions and the following disclaimer.
  14. - Redistributions in binary form must reproduce the above copyright
  15. notice, this list of conditions and the following disclaimer in
  16. the documentation and/or other materials provided with the
  17. distribution.
  18. - Neither the name of Christiaan Hofman nor the names of any
  19. contributors may be used to endorse or promote products derived
  20. from this software without specific prior written permission.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. */
  33. #import "SKProgressController.h"
  34. @interface SKProgressController_ProgressIndicator : NSProgressIndicator
  35. @end
  36. @implementation SKProgressController_ProgressIndicator
  37. - (instancetype)init {
  38. if (self = [super init]) {
  39. }
  40. return self;
  41. }
  42. - (void)drawRect:(NSRect)dirtyRect {
  43. NSColor *color = [NSColor colorWithRed:206/255.f green:208/255.f blue:212/255.f alpha:1.f];
  44. [color setStroke];
  45. [color setFill];
  46. NSRect lineRect = NSMakeRect(1.5, (NSHeight(self.bounds)-2.4)*0.5, NSWidth(self.bounds)-2, 2.4);
  47. NSBezierPath *linePath = [NSBezierPath bezierPathWithRoundedRect:lineRect xRadius:0 yRadius:0];
  48. [linePath fill];
  49. [linePath stroke];
  50. [super drawRect:dirtyRect];
  51. }
  52. @end
  53. @implementation SKProgressController
  54. @synthesize progressBar, progressField;
  55. @dynamic message, indeterminate, maxValue, doubleValue;
  56. - (void)dealloc {}
  57. - (NSString *)windowNibName {
  58. return @"ProgressSheet";
  59. }
  60. - (instancetype)init {
  61. if (self = [super init]) {
  62. self.showClose = YES;
  63. }
  64. return self;
  65. }
  66. - (void)windowDidLoad {
  67. [progressBar setUsesThreadedAnimation:YES];
  68. self.window.appearance = [NSAppearance appearanceNamed:NSAppearanceNameAqua];
  69. NSButton *button = [[NSButton alloc] init];
  70. [self.window.contentView addSubview:button];
  71. CGFloat width = NSWidth(self.window.contentView.frame);
  72. CGFloat height = NSWidth(self.window.contentView.frame);
  73. CGSize size = CGSizeMake(20, 20);
  74. CGFloat buttonY = 28;
  75. CGFloat buttonX = width-size.width-10;
  76. button.frame = NSMakeRect(buttonX, buttonY, size.width, size.height);
  77. button.autoresizingMask = NSViewMinXMargin | NSViewMaxYMargin;
  78. button.bordered = NO;
  79. button.image = [NSImage imageNamed:@"KMImageNameWhiteClose"];
  80. button.target = self;
  81. button.action = @selector(buttonAction:);
  82. button.hidden = !self.showClose;
  83. }
  84. - (NSProgressIndicator *)progressBar {
  85. [self window];
  86. return progressBar;
  87. }
  88. - (NSString *)message {
  89. [self window];
  90. return [progressField stringValue];
  91. }
  92. - (void)setMessage:(NSString *)newMessage {
  93. [self window];
  94. [progressField setStringValue:newMessage];
  95. [[self window] setTitle:newMessage];
  96. }
  97. - (BOOL)isIndeterminate {
  98. return [[self progressBar] isIndeterminate];
  99. }
  100. - (void)setIndeterminate:(BOOL)flag {
  101. [[self progressBar] setIndeterminate:flag];
  102. }
  103. - (double)maxValue {
  104. return [[self progressBar] maxValue];
  105. }
  106. - (void)setMaxValue:(double)newMaximum {
  107. [[self progressBar] setMaxValue:newMaximum];
  108. [[self progressBar] setDoubleValue:0.0];
  109. }
  110. - (double)doubleValue {
  111. return [[self progressBar] doubleValue];
  112. }
  113. - (void)setDoubleValue:(double)doubleValue {
  114. dispatch_async(dispatch_get_main_queue(), ^{
  115. [[self progressBar] setDoubleValue:doubleValue];
  116. [[self progressBar] displayIfNeeded];
  117. });
  118. }
  119. - (void)incrementBy:(double)delta {
  120. [[self progressBar] incrementBy:delta];
  121. [[self progressBar] displayIfNeeded];
  122. }
  123. - (void)buttonAction:(NSButton *)sender {
  124. if (self.closeBlock) {
  125. self.closeBlock();
  126. }
  127. [self dismissSheet:nil];
  128. }
  129. - (void)beginSheetModalForWindow:(NSWindow *)window completionHandler:(void (^)(NSInteger result))handler {
  130. [[self progressBar] startAnimation:self];
  131. // [(SKApplication *)NSApp setUserAttentionDisabled:YES];
  132. // [super beginSheetModalForWindow:window completionHandler:handler];
  133. [NSApp beginSheet:[self window]
  134. modalForWindow:window
  135. modalDelegate:self
  136. didEndSelector:@selector(didEndSheet:returnCode:contextInfo:)
  137. contextInfo:handler ? CFBridgingRetain(handler) : NULL];
  138. // [(SKApplication *)NSApp setUserAttentionDisabled:NO];
  139. }
  140. - (IBAction)dismissSheet:(id)sender {
  141. [[self progressBar] stopAnimation:self];
  142. // [super dismissSheet:sender];
  143. if (@available(macOS 10.13, *)) {
  144. [NSApp endSheet:[self window] returnCode:[sender tag]];
  145. } else {
  146. [NSApp endSheet:[self window]];
  147. }
  148. [[self window] orderOut:self];
  149. }
  150. @end