summaryrefslogtreecommitdiffstats
path: root/chrome/browser/cocoa/theme_install_bubble_view.mm
blob: 82c0e49083cf303f6f1761e2f2e0844051e7a16a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#import <Cocoa/Cocoa.h>

#import "chrome/browser/cocoa/theme_install_bubble_view.h"

#include "app/l10n_util_mac.h"
#include "base/scoped_nsobject.h"
#include "grit/generated_resources.h"

namespace {

// The alpha of the bubble.
static const float kBubbleAlpha = 0.75;

// The roundedness of the edges of our bubble.
static const int kBubbleCornerRadius = 4;

// Padding around text in popup box.
static const int kTextHorizPadding = 90;
static const int kTextVertPadding = 45;

// Point size of the text in the box.
static const int kLoadingTextSize = 24;

}

// static
ThemeInstallBubbleView* ThemeInstallBubbleView::view_ = NULL;

// The Cocoa view to draw a gray rounded rect with "Loading..." in it.
@interface ThemeInstallBubbleViewCocoa : NSView {
 @private
  scoped_nsobject<NSAttributedString> message_;

  NSRect grayRect_;
  NSRect textRect_;
}

- (id)init;

// The size of the gray rect that will be drawn.
- (NSSize)preferredSize;
// Forces size calculations of where everything will be drawn.
- (void)layout;

@end

ThemeInstallBubbleView::ThemeInstallBubbleView(NSWindow* window)
    : cocoa_view_([[ThemeInstallBubbleViewCocoa alloc] init]),
      num_loads_extant_(1) {
  DCHECK(window);

  NSView* parent_view = [window contentView];
  NSRect parent_bounds = [parent_view bounds];
  if (parent_bounds.size.height < [cocoa_view_ preferredSize].height)
    Close();

  // Close when theme has been installed.
  registrar_.Add(
      this,
      NotificationType::BROWSER_THEME_CHANGED,
      NotificationService::AllSources());

  // Close when we are installing an extension, not a theme.
  registrar_.Add(
      this,
      NotificationType::NO_THEME_DETECTED,
      NotificationService::AllSources());
  registrar_.Add(
      this,
      NotificationType::EXTENSION_INSTALLED,
      NotificationService::AllSources());
  registrar_.Add(
      this,
      NotificationType::EXTENSION_INSTALL_ERROR,
      NotificationService::AllSources());

  // Don't let the bubble overlap the confirm dialog.
  registrar_.Add(
      this,
      NotificationType::EXTENSION_WILL_SHOW_CONFIRM_DIALOG,
      NotificationService::AllSources());

  // Add the view.
  [cocoa_view_ setFrame:parent_bounds];
  [cocoa_view_ setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
  [parent_view addSubview:cocoa_view_
               positioned:NSWindowAbove
               relativeTo:nil];
  [cocoa_view_ layout];
}

ThemeInstallBubbleView::~ThemeInstallBubbleView() {
  // Need to delete self; the real work happens in Close().
}

void ThemeInstallBubbleView::Close() {
  --num_loads_extant_;
  if (num_loads_extant_ < 1) {
    registrar_.RemoveAll();
    if (cocoa_view_ && [cocoa_view_ superview]) {
      [cocoa_view_ removeFromSuperview];
      [cocoa_view_ release];
    }
    view_ = NULL;
    delete this;
    // this is deleted; nothing more!
  }
}

void ThemeInstallBubbleView::Observe(NotificationType type,
                                     const NotificationSource& source,
                                     const NotificationDetails& details) {
  Close();
}

// static
void ThemeInstallBubbleView::Show(NSWindow* window) {
  if (view_)
    ++view_->num_loads_extant_;
  else
    view_ = new ThemeInstallBubbleView(window);
}

@implementation ThemeInstallBubbleViewCocoa

- (id)init {
  self = [super initWithFrame:NSZeroRect];
  if (self) {
    NSString* loadingString =
        l10n_util::GetNSStringWithFixup(IDS_THEME_LOADING_TITLE);
    NSFont* loadingFont = [NSFont systemFontOfSize:kLoadingTextSize];
    NSColor* textColor = [NSColor whiteColor];
    NSDictionary* loadingAttrs = [NSDictionary dictionaryWithObjectsAndKeys:
                                  loadingFont, NSFontAttributeName,
                                  textColor, NSForegroundColorAttributeName,
                                  nil];
    message_.reset([[NSAttributedString alloc] initWithString:loadingString
                                                   attributes:loadingAttrs]);

    // TODO(avi): find a white-on-black spinner
  }
  return self;
}

- (NSSize)preferredSize {
  NSSize size = [message_.get() size];
  size.width += kTextHorizPadding;
  size.height += kTextVertPadding;
  return size;
}

// Update the layout to keep the view centered when the window is resized.
- (void)resizeWithOldSuperviewSize:(NSSize)oldBoundsSize {
  [super resizeWithOldSuperviewSize:oldBoundsSize];
  [self layout];
}

- (void)layout {
  NSRect bounds = [self bounds];

  grayRect_.size = [self preferredSize];
  grayRect_.origin.x = (bounds.size.width - grayRect_.size.width) / 2;
  grayRect_.origin.y = bounds.size.height / 2;

  textRect_.size = [message_.get() size];
  textRect_.origin.x = (bounds.size.width - [message_.get() size].width) / 2;
  textRect_.origin.y = (bounds.size.height + kTextVertPadding) / 2;
}

- (void)drawRect:(NSRect)dirtyRect {
  [[NSColor clearColor] set];
  NSRectFillUsingOperation([self bounds], NSCompositeSourceOver);

  [[[NSColor blackColor] colorWithAlphaComponent:kBubbleAlpha] set];
  [[NSBezierPath bezierPathWithRoundedRect:grayRect_
                                   xRadius:kBubbleCornerRadius
                                   yRadius:kBubbleCornerRadius] fill];

  [message_.get() drawInRect:textRect_];
}

@end