VapProgressHUD.m 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Tencent is pleased to support the open source community by making vap available.
  2. //
  3. // Copyright (C) 2020 THL A29 Limited, a Tencent company. All rights reserved.
  4. //
  5. // Licensed under the MIT License (the "License"); you may not use this file except in
  6. // compliance with the License. You may obtain a copy of the License at
  7. //
  8. // http://opensource.org/licenses/MIT
  9. //
  10. // Unless required by applicable law or agreed to in writing, software distributed under the License is
  11. // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
  12. // either express or implied. See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #import "VapProgressHUD.h"
  15. @interface VapProgressHUD ()
  16. @property (nonatomic, strong) NSProgressIndicator *progressIndicator;
  17. @end
  18. @implementation VapProgressHUD
  19. - (void)mouseDown:(NSEvent *)event {
  20. }
  21. - (void)rightMouseDown:(NSEvent *)event {
  22. }
  23. - (void)drawRect:(NSRect)dirtyRect {
  24. [super drawRect:dirtyRect];
  25. // Drawing code here.
  26. }
  27. - (instancetype)initWithView:(NSView *)view {
  28. if (self = [super initWithFrame:view.bounds]) {
  29. self.wantsLayer = YES;
  30. self.layer.backgroundColor = [NSColor colorWithRed:0 green:0 blue:0 alpha:0.7].CGColor;
  31. [self setNeedsDisplay:YES];
  32. NSProgressIndicator *progressIndicator = [[NSProgressIndicator alloc] initWithFrame:view.bounds];
  33. progressIndicator.indeterminate = NO;
  34. progressIndicator.bezeled = YES;
  35. progressIndicator.controlSize = NSControlSizeRegular;
  36. progressIndicator.doubleValue = 0;
  37. progressIndicator.style = NSProgressIndicatorStyleBar;
  38. progressIndicator.displayedWhenStopped = NO;
  39. [self addSubview:progressIndicator];
  40. self.progressIndicator = progressIndicator;
  41. CGFloat labelWidth = 100;
  42. CGFloat labelHeight = 30;
  43. NSText *progressLabel = [[NSText alloc] initWithFrame:CGRectMake((progressIndicator.frame.size.width - labelWidth) / 2, (progressIndicator.frame.size.height - labelHeight) / 2 - 40, labelWidth, labelHeight)];
  44. progressLabel.string = @"处理中...";
  45. progressLabel.backgroundColor = NSColor.clearColor;
  46. progressLabel.font = [NSFont fontWithName:@"Helvetica-Bold" size:20];
  47. progressLabel.alignment = NSTextAlignmentCenter;
  48. [self addSubview:progressLabel];
  49. }
  50. return self;
  51. }
  52. + (instancetype)showHUDToSuperView:(NSView *)superView {
  53. VapProgressHUD *hud = [[self alloc] initWithView:superView];
  54. [superView addSubview:hud];
  55. [hud show];
  56. return hud;
  57. }
  58. - (void)show {
  59. [self.progressIndicator startAnimation:nil];
  60. }
  61. - (void)hide {
  62. [self.progressIndicator stopAnimation:nil];
  63. [self removeFromSuperview];
  64. }
  65. - (void)setCurrentValue:(CGFloat)currentValue {
  66. self.progressIndicator.doubleValue = currentValue;
  67. }
  68. @end