diff options
author | rsesek@chromium.org <rsesek@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-11-22 20:32:50 +0000 |
---|---|---|
committer | rsesek@chromium.org <rsesek@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-11-22 20:32:50 +0000 |
commit | ca6c990f200c470f2f026ccafac39e29a23c26f2 (patch) | |
tree | b102e3fc82137c824530017a255ecd35ede96111 /chrome/browser/cocoa | |
parent | 9935e9f58cc7fd518da760d563d9f360c0ace9da (diff) | |
download | chromium_src-ca6c990f200c470f2f026ccafac39e29a23c26f2.zip chromium_src-ca6c990f200c470f2f026ccafac39e29a23c26f2.tar.gz chromium_src-ca6c990f200c470f2f026ccafac39e29a23c26f2.tar.bz2 |
[Mac] Add a preference for Instant search.
XIB Changes (Preferences.xib):
* Add a NSButton checkbox for the Instant preference as an outlet (not with
bindings). Hook up the action of the button to First Responder.
* Add a "Learn more" link for Instant that sends the action to First Responder.
* Increase the size of the Basics view to accommodate those new subviews.
Note that this will not be properly sized-to-fit in some locales. See
http://crbug.com/64090 for details.
BUG=63651
TEST=Chromium-->Preferences. Toggle Instant and get the info sheet. Pressing OK enables it, Cancel does not.
TEST=After the first enabling, the sheet does not appear again.
Review URL: http://codereview.chromium.org/5090007
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@66994 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/cocoa')
5 files changed, 195 insertions, 0 deletions
diff --git a/chrome/browser/cocoa/instant_confirm_window_controller.h b/chrome/browser/cocoa/instant_confirm_window_controller.h new file mode 100644 index 0000000..f721af7 --- /dev/null +++ b/chrome/browser/cocoa/instant_confirm_window_controller.h @@ -0,0 +1,43 @@ +// Copyright (c) 2010 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. + +#ifndef CHROME_BROWSER_COCOA_INSTANT_CONFIRM_WINDOW_CONTROLLER_H_ +#define CHROME_BROWSER_COCOA_INSTANT_CONFIRM_WINDOW_CONTROLLER_H_ +#pragma once + +#import <Cocoa/Cocoa.h> + +#import "base/cocoa_protocols_mac.h" + +class Profile; + +// This controller manages a dialog that informs the user about Instant search. +// The recommended API is to not use this class directly, but instead to use +// the functions in //chrome/browser/instant/instant_confirm_dialog.h: +// void ShowInstantConfirmDialog[IfNecessary](gfx::NativeWindow* parent, ...) +// Which will attach the window to |parent| as a sheet. +@interface InstantConfirmWindowController : NSWindowController<NSWindowDelegate> +{ + @private + // The long description about Instant that needs to be sized-to-fit. + IBOutlet NSTextField* description_; + + Profile* profile_; // weak +} + +// Designated initializer. The controller will release itself on window close. +- (id)initWithProfile:(Profile*)profile; + +// Action for the "Learn more" link. +- (IBAction)learnMore:(id)sender; + +// The user has opted in to Instant. This enables the Instant preference. +- (IBAction)ok:(id)sender; + +// Closes the sheet without altering the preference value. +- (IBAction)cancel:(id)sender; + +@end + +#endif // CHROME_BROWSER_COCOA_INSTANT_CONFIRM_WINDOW_CONTROLLER_H_ diff --git a/chrome/browser/cocoa/instant_confirm_window_controller.mm b/chrome/browser/cocoa/instant_confirm_window_controller.mm new file mode 100644 index 0000000..55109cb --- /dev/null +++ b/chrome/browser/cocoa/instant_confirm_window_controller.mm @@ -0,0 +1,76 @@ +// Copyright (c) 2010 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 "chrome/browser/cocoa/instant_confirm_window_controller.h" + +#include "base/logging.h" +#include "base/mac_util.h" +#include "chrome/browser/instant/instant_confirm_dialog.h" +#include "chrome/browser/instant/instant_controller.h" +#include "chrome/browser/profile.h" +#include "chrome/browser/show_options_url.h" +#include "gfx/native_widget_types.h" +#include "googleurl/src/gurl.h" +#import "third_party/GTM/AppKit/GTMUILocalizerAndLayoutTweaker.h" + +namespace browser { + +void ShowInstantConfirmDialog(gfx::NativeWindow parent, Profile* profile) { + InstantConfirmWindowController* controller = + [[InstantConfirmWindowController alloc] initWithProfile:profile]; + [NSApp beginSheet:[controller window] + modalForWindow:parent + modalDelegate:nil + didEndSelector:NULL + contextInfo:NULL]; +} + +} // namespace browser + +@implementation InstantConfirmWindowController + +- (id)initWithProfile:(Profile*)profile { + NSString* nibPath = [mac_util::MainAppBundle() + pathForResource:@"InstantConfirm" + ofType:@"nib"]; + if ((self = [super initWithWindowNibPath:nibPath owner:self])) { + profile_ = profile; + } + return self; +} + +- (void)awakeFromNib { + DCHECK([self window]); + DCHECK_EQ(self, [[self window] delegate]); + + CGFloat delta = [GTMUILocalizerAndLayoutTweaker sizeToFitFixedWidthTextField: + description_]; + NSRect descriptionFrame = [description_ frame]; + descriptionFrame.origin.y -= delta; + [description_ setFrame:descriptionFrame]; + + NSRect frame = [[self window] frame]; + frame.size.height += delta; + [[self window] setFrame:frame display:YES]; +} + +- (void)windowWillClose:(NSNotification*)notif { + [self autorelease]; +} + +- (IBAction)learnMore:(id)sender { + browser::ShowOptionsURL(profile_, GURL(browser::kInstantLearnMoreURL)); +} + +- (IBAction)ok:(id)sender { + InstantController::Enable(profile_); + [self cancel:sender]; +} + +- (IBAction)cancel:(id)sender { + [NSApp endSheet:[self window]]; + [[self window] close]; +} + +@end diff --git a/chrome/browser/cocoa/instant_confirm_window_controller_unittest.mm b/chrome/browser/cocoa/instant_confirm_window_controller_unittest.mm new file mode 100644 index 0000000..ec545bc --- /dev/null +++ b/chrome/browser/cocoa/instant_confirm_window_controller_unittest.mm @@ -0,0 +1,36 @@ +// Copyright (c) 2010 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 "chrome/browser/cocoa/instant_confirm_window_controller.h" + +#import "chrome/browser/cocoa/browser_test_helper.h" +#import "chrome/browser/cocoa/cocoa_test_helper.h" +#include "chrome/browser/instant/instant_confirm_dialog.h" + +namespace { + +class InstantConfirmWindowControllerTest : public CocoaTest { + public: + InstantConfirmWindowControllerTest() : controller_(nil) {} + + BrowserTestHelper helper_; + InstantConfirmWindowController* controller_; // Weak. Owns self. +}; + +TEST_F(InstantConfirmWindowControllerTest, Init) { + controller_ = + [[InstantConfirmWindowController alloc] initWithProfile: + helper_.profile()]; + EXPECT_TRUE([controller_ window]); + [controller_ release]; +} + +TEST_F(InstantConfirmWindowControllerTest, Show) { + browser::ShowInstantConfirmDialog(test_window(), helper_.profile()); + controller_ = [[test_window() attachedSheet] windowController]; + EXPECT_TRUE(controller_); + [controller_ cancel:nil]; +} + +} // namespace diff --git a/chrome/browser/cocoa/preferences_window_controller.h b/chrome/browser/cocoa/preferences_window_controller.h index e6f1be4..1f5a295 100644 --- a/chrome/browser/cocoa/preferences_window_controller.h +++ b/chrome/browser/cocoa/preferences_window_controller.h @@ -89,6 +89,8 @@ class ProfileSyncService; BooleanPrefMember newTabPageIsHomePage_; StringPrefMember homepage_; BooleanPrefMember showHomeButton_; + BooleanPrefMember instantEnabled_; + IBOutlet NSButton* instantCheckbox_; scoped_nsobject<SearchEngineListModel> searchEngineModel_; // Used when creating a new home page url to make the new cell editable. BOOL pendingSelectForEdit_; @@ -167,6 +169,8 @@ class ProfileSyncService; - (IBAction)removeSelectedHomepages:(id)sender; - (IBAction)useCurrentPagesAsHomepage:(id)sender; - (IBAction)manageSearchEngines:(id)sender; +- (IBAction)toggleInstant:(id)sender; +- (IBAction)learnMoreAboutInstant:(id)sender; - (IBAction)makeDefaultBrowser:(id)sender; // User Data panel diff --git a/chrome/browser/cocoa/preferences_window_controller.mm b/chrome/browser/cocoa/preferences_window_controller.mm index c02d316..cf06527 100644 --- a/chrome/browser/cocoa/preferences_window_controller.mm +++ b/chrome/browser/cocoa/preferences_window_controller.mm @@ -33,6 +33,8 @@ #include "chrome/browser/download/download_prefs.h" #include "chrome/browser/extensions/extensions_service.h" #include "chrome/browser/google/google_util.h" +#include "chrome/browser/instant/instant_confirm_dialog.h" +#include "chrome/browser/instant/instant_controller.h" #include "chrome/browser/metrics/metrics_service.h" #include "chrome/browser/metrics/user_metrics.h" #include "chrome/browser/net/url_fixer_upper.h" @@ -334,6 +336,7 @@ CGFloat AutoSizeUnderTheHoodContent(NSView* view, // Record the user performed a certain action and save the preferences. - (void)recordUserAction:(const UserMetricsAction&) action; - (void)registerPrefObservers; +- (void)configureInstant; // KVC setter methods. - (void)setNewTabPageIsHomePageIndex:(NSInteger)val; @@ -585,6 +588,8 @@ class ManagedPrefsBannerState : public policy::ManagedPrefsBannerBase { DCHECK_EQ(defaultBrowserChange.height, 0.0) << "Button should have been right height in nib"; + [self configureInstant]; + // Size the sync row. CGFloat syncRowChange = SizeToFitButtonPair(syncButton_, syncCustomizeButton_); @@ -798,6 +803,7 @@ class ManagedPrefsBannerState : public policy::ManagedPrefsBannerBase { prefs_, observer_.get()); homepage_.Init(prefs::kHomePage, prefs_, observer_.get()); showHomeButton_.Init(prefs::kShowHomeButton, prefs_, observer_.get()); + instantEnabled_.Init(prefs::kInstantEnabled, prefs_, observer_.get()); // Personal Stuff panel askSavePasswords_.Init(prefs::kPasswordManagerEnabled, @@ -936,6 +942,8 @@ class ManagedPrefsBannerState : public policy::ManagedPrefsBannerBase { } else if (*prefName == prefs::kShowHomeButton) { [self setShowHomeButton:showHomeButton_.GetValue() ? YES : NO]; [self setShowHomeButtonEnabled:!showHomeButton_.IsManaged()]; + } else if (*prefName == prefs::kInstantEnabled) { + [self configureInstant]; } } @@ -1186,6 +1194,34 @@ enum { kHomepageNewTabPage, kHomepageURL }; [KeywordEditorCocoaController showKeywordEditor:profile_]; } +- (IBAction)toggleInstant:(id)sender { + if (instantEnabled_.GetValue()) { + InstantController::Disable(profile_); + } else { + [instantCheckbox_ setState:NSOffState]; + browser::ShowInstantConfirmDialogIfNecessary([self window], profile_); + } +} + +// Sets the state of the Instant checkbox and adds the type information to the +// label. +- (void)configureInstant { + bool enabled = instantEnabled_.GetValue(); + NSInteger state = enabled ? NSOnState : NSOffState; + [instantCheckbox_ setState:state]; + + NSString* title = l10n_util::GetNSStringWithFixup(IDS_INSTANT_PREF); + if (enabled) { + title = [NSString stringWithFormat:@"%@ [%d]", title, + prefs_->GetInteger(prefs::kInstantType)]; + } + [instantCheckbox_ setTitle:title]; +} + +- (IBAction)learnMoreAboutInstant:(id)sender { + browser::ShowOptionsURL(profile_, GURL(browser::kInstantLearnMoreURL)); +} + // Called when the user clicks the button to make Chromium the default // browser. Registers http and https. - (IBAction)makeDefaultBrowser:(id)sender { |