]> khamidou — GitHub mirrors - electron.git/commitdiff
mac: Implement SetProgressBar API.
authorHaojian Wu <hokein.wu@gmail.com>
Wed, 17 Sep 2014 07:58:08 +0000 (15:58 +0800)
committerHaojian Wu <hokein.wu@gmail.com>
Thu, 18 Sep 2014 02:20:55 +0000 (10:20 +0800)
atom/browser/native_window_mac.h
atom/browser/native_window_mac.mm

index 0f3e26f9a387909fe92bae6ce91231a89d98f52d..ecdabc800cd5cfcf86646781562a6a916085d9ca 100644 (file)
@@ -65,6 +65,7 @@ class NativeWindowMac : public NativeWindow {
   virtual bool IsDocumentEdited() OVERRIDE;
   virtual bool HasModalDialog() OVERRIDE;
   virtual gfx::NativeWindow GetNativeWindow() OVERRIDE;
+  virtual void SetProgressBar(double progress) OVERRIDE;
 
   // Returns true if |point| in local Cocoa coordinate system falls within
   // the draggable region.
index 587dcc602ed393aa23662eba21393e7da0d37280..e9881eb03a0513c4023d10d4ce3ac22fe8c84c28 100644 (file)
@@ -193,6 +193,39 @@ static const CGFloat kAtomWindowCornerRadius = 4.0;
 
 @end
 
+@interface AtomProgressBar : NSProgressIndicator
+@end
+
+@implementation AtomProgressBar
+
+- (void)drawRect:(NSRect)dirtyRect {
+  if (self.style != NSProgressIndicatorBarStyle)
+    return;
+  // Draw edges of rounded rect.
+  NSRect rect = NSInsetRect([self bounds], 1.0, 1.0);
+       CGFloat radius = rect.size.height / 2;
+       NSBezierPath* bezier_path = [NSBezierPath bezierPathWithRoundedRect:rect xRadius:radius yRadius:radius];
+       [bezier_path setLineWidth:2.0];
+       [[NSColor grayColor] set];
+       [bezier_path stroke];
+
+  // Fill the rounded rect.
+       rect = NSInsetRect(rect, 2.0, 2.0);
+       radius = rect.size.height / 2;
+       bezier_path = [NSBezierPath bezierPathWithRoundedRect:rect xRadius:radius yRadius:radius];
+       [bezier_path setLineWidth:1.0];
+       [bezier_path addClip];
+
+  // Calculate the progress width.
+       rect.size.width = floor(rect.size.width * ([self doubleValue] / [self maxValue]));
+
+  // Fill the progress bar with color blue.
+  [[NSColor colorWithSRGBRed:0.2 green:0.6 blue:1 alpha:1] set];
+  NSRectFill(rect);
+}
+
+@end
+
 namespace atom {
 
 NativeWindowMac::NativeWindowMac(content::WebContents* web_contents,
@@ -517,6 +550,39 @@ gfx::NativeWindow NativeWindowMac::GetNativeWindow() {
   return window_;
 }
 
+void NativeWindowMac::SetProgressBar(double progress) {
+  NSDockTile* dock_tile = [NSApp dockTile];
+
+  // For the first time API invoked, we need to create a ContentView in DockTile.
+  if (dock_tile.contentView == NULL) {
+    NSImageView* image_view = [[NSImageView alloc] init];
+    [image_view setImage:[NSApp applicationIconImage]];
+    [dock_tile setContentView:image_view];
+
+    NSProgressIndicator* progress_indicator = [[AtomProgressBar alloc]
+        initWithFrame:NSMakeRect(0.0f, 0.0f, dock_tile.size.width, 15.0)];
+    [progress_indicator setStyle:NSProgressIndicatorBarStyle];
+    [progress_indicator setIndeterminate:NO];
+    [progress_indicator setBezeled:YES];
+    [progress_indicator setMinValue:0];
+    [progress_indicator setMaxValue:1];
+    [progress_indicator setHidden:NO];
+    [image_view addSubview:progress_indicator];
+  }
+
+  NSProgressIndicator* progress_indicator =
+      static_cast<NSProgressIndicator*>([[[dock_tile contentView] subviews]
+           objectAtIndex:0]);
+  if (progress < 0) {
+    [progress_indicator setHidden:YES];
+  } else if (progress > 1) {
+    [progress_indicator setIndeterminate:YES];
+  } else {
+    [progress_indicator setDoubleValue:progress];
+  }
+  [dock_tile display];
+}
+
 bool NativeWindowMac::IsWithinDraggableRegion(NSPoint point) const {
   if (!draggable_region_)
     return false;