Home | History | Annotate | Download | only in cocoa
      1 // Copyright (c) 2010 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/utf_string_conversions.h"
      9 #include "chrome/browser/search_engines/util.h"
     10 #import "chrome/browser/ui/cocoa/l10n_util.h"
     11 #import "chrome/browser/ui/cocoa/info_bubble_view.h"
     12 #include "grit/generated_resources.h"
     13 #include "ui/base/l10n/l10n_util.h"
     14 
     15 @interface FirstRunBubbleController(Private)
     16 - (id)initRelativeToView:(NSView*)view
     17                   offset:(NSPoint)offset
     18                  profile:(Profile*)profile;
     19 - (void)closeIfNotKey;
     20 @end
     21 
     22 @implementation FirstRunBubbleController
     23 
     24 + (FirstRunBubbleController*) showForView:(NSView*)view
     25                                    offset:(NSPoint)offset
     26                                   profile:(Profile*)profile {
     27   // Autoreleases itself on bubble close.
     28   return [[FirstRunBubbleController alloc] initRelativeToView:view
     29                                                        offset:offset
     30                                                       profile:profile];
     31 }
     32 
     33 - (id)initRelativeToView:(NSView*)view
     34                   offset:(NSPoint)offset
     35                  profile:(Profile*)profile {
     36   if ((self = [super initWithWindowNibPath:@"FirstRunBubble"
     37                             relativeToView:view
     38                                     offset:offset])) {
     39     profile_ = profile;
     40     [self showWindow:nil];
     41 
     42     // On 10.5, the first run bubble sometimes does not disappear when clicking
     43     // the omnibox. This happens if the bubble never became key, due to it
     44     // showing up so early in the startup sequence. As a workaround, close it
     45     // automatically after a few seconds if it doesn't become key.
     46     // http://crbug.com/52726
     47     [self performSelector:@selector(closeIfNotKey) withObject:nil afterDelay:3];
     48   }
     49   return self;
     50 }
     51 
     52 - (void)awakeFromNib {
     53   DCHECK(header_);
     54   [header_ setStringValue:cocoa_l10n_util::ReplaceNSStringPlaceholders(
     55       [header_ stringValue], GetDefaultSearchEngineName(profile_), NULL)];
     56 
     57   // Adapt window size to bottom buttons. Do this before all other layouting.
     58   CGFloat dy = cocoa_l10n_util::VerticallyReflowGroup([[self bubble] subviews]);
     59   NSSize ds = NSMakeSize(0, dy);
     60   ds = [[self bubble] convertSize:ds toView:nil];
     61 
     62   NSRect frame = [[self window] frame];
     63   frame.origin.y -= ds.height;
     64   frame.size.height += ds.height;
     65   [[self window] setFrame:frame display:YES];
     66 }
     67 
     68 - (void)close {
     69   // If the window is closed before the timer is fired, cancel the timer, since
     70   // it retains the controller.
     71   [NSObject cancelPreviousPerformRequestsWithTarget:self
     72                                            selector:@selector(closeIfNotKey)
     73                                              object:nil];
     74   [super close];
     75 }
     76 
     77 - (void)closeIfNotKey {
     78   if (![[self window] isKeyWindow])
     79     [self close];
     80 }
     81 
     82 @end  // FirstRunBubbleController
     83