- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / cocoa / first_run_bubble_controller.mm
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #import "chrome/browser/ui/cocoa/first_run_bubble_controller.h"
6
7 #include "base/logging.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "chrome/browser/first_run/first_run.h"
10 #include "chrome/browser/search_engines/util.h"
11 #include "chrome/browser/ui/chrome_pages.h"
12 #import "chrome/browser/ui/cocoa/info_bubble_view.h"
13 #import "chrome/browser/ui/cocoa/l10n_util.h"
14 #include "grit/generated_resources.h"
15 #include "ui/base/l10n/l10n_util.h"
16
17 @interface FirstRunBubbleController(Private)
18 - (id)initRelativeToView:(NSView*)view
19                   offset:(NSPoint)offset
20                  browser:(Browser*)browser
21                  profile:(Profile*)profile;
22 - (void)closeIfNotKey;
23 @end
24
25 @implementation FirstRunBubbleController
26
27 + (FirstRunBubbleController*) showForView:(NSView*)view
28                                    offset:(NSPoint)offset
29                                   browser:(Browser*)browser
30                                   profile:(Profile*)profile {
31   // Autoreleases itself on bubble close.
32   return [[FirstRunBubbleController alloc] initRelativeToView:view
33                                                        offset:offset
34                                                       browser:browser
35                                                       profile:profile];
36 }
37
38 - (id)initRelativeToView:(NSView*)view
39                   offset:(NSPoint)offset
40                  browser:(Browser*)browser
41                  profile:(Profile*)profile {
42   if ((self = [super initWithWindowNibPath:@"FirstRunBubble"
43                             relativeToView:view
44                                     offset:offset])) {
45     browser_ = browser;
46     profile_ = profile;
47     [self showWindow:nil];
48
49     // On 10.5, the first run bubble sometimes does not disappear when clicking
50     // the omnibox. This happens if the bubble never became key, due to it
51     // showing up so early in the startup sequence. As a workaround, close it
52     // automatically after a few seconds if it doesn't become key.
53     // http://crbug.com/52726
54     [self performSelector:@selector(closeIfNotKey) withObject:nil afterDelay:3];
55   }
56   return self;
57 }
58
59 - (void)awakeFromNib {
60   first_run::LogFirstRunMetric(first_run::FIRST_RUN_BUBBLE_SHOWN);
61
62   DCHECK(header_);
63   [header_ setStringValue:cocoa_l10n_util::ReplaceNSStringPlaceholders(
64       [header_ stringValue], GetDefaultSearchEngineName(profile_), NULL)];
65
66   // Adapt window size to contents. Do this before all other layouting.
67   CGFloat dy = cocoa_l10n_util::VerticallyReflowGroup([[self bubble] subviews]);
68   NSSize ds = NSMakeSize(0, dy);
69   ds = [[self bubble] convertSize:ds toView:nil];
70
71   NSRect frame = [[self window] frame];
72   frame.origin.y -= ds.height;
73   frame.size.height += ds.height;
74   [[self window] setFrame:frame display:YES];
75 }
76
77 - (void)close {
78   // If the window is closed before the timer is fired, cancel the timer, since
79   // it retains the controller.
80   [NSObject cancelPreviousPerformRequestsWithTarget:self
81                                            selector:@selector(closeIfNotKey)
82                                              object:nil];
83   [super close];
84 }
85
86 - (void)closeIfNotKey {
87   if (![[self window] isKeyWindow])
88     [self close];
89 }
90
91 - (IBAction)onChange:(id)sender {
92   first_run::LogFirstRunMetric(first_run::FIRST_RUN_BUBBLE_CHANGE_INVOKED);
93
94   Browser* browser = browser_;
95   [self close];
96   if (browser)
97     chrome::ShowSearchEngineSettings(browser);
98 }
99
100 @end  // FirstRunBubbleController