Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(109)

Side by Side Diff: chrome/browser/ui/cocoa/browser_window_cocoa.mm

Issue 2916753004: Use BookmarkAppConfirmationView on Mac. Delete the NSAlert Mac uses currently. (Closed)
Patch Set: using, OnceCallback Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/ui/cocoa/browser_window_cocoa.h" 5 #include "chrome/browser/ui/cocoa/browser_window_cocoa.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/command_line.h" 8 #include "base/command_line.h"
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #import "base/mac/sdk_forward_declarations.h" 10 #import "base/mac/sdk_forward_declarations.h"
11 #include "base/message_loop/message_loop.h" 11 #include "base/message_loop/message_loop.h"
12 #include "base/strings/string_util.h" 12 #include "base/strings/string_util.h"
13 #include "base/strings/sys_string_conversions.h" 13 #include "base/strings/sys_string_conversions.h"
14 #include "chrome/app/chrome_command_ids.h" 14 #include "chrome/app/chrome_command_ids.h"
15 #include "chrome/browser/chrome_notification_types.h" 15 #include "chrome/browser/chrome_notification_types.h"
16 #include "chrome/browser/download/download_shelf.h" 16 #include "chrome/browser/download/download_shelf.h"
17 #include "chrome/browser/extensions/extension_util.h"
18 #include "chrome/browser/extensions/tab_helper.h" 17 #include "chrome/browser/extensions/tab_helper.h"
19 #include "chrome/browser/metrics/browser_window_histogram_helper.h" 18 #include "chrome/browser/metrics/browser_window_histogram_helper.h"
20 #include "chrome/browser/profiles/profile.h" 19 #include "chrome/browser/profiles/profile.h"
21 #include "chrome/browser/profiles/profile_window.h" 20 #include "chrome/browser/profiles/profile_window.h"
22 #include "chrome/browser/shell_integration.h" 21 #include "chrome/browser/shell_integration.h"
23 #include "chrome/browser/signin/chrome_signin_helper.h" 22 #include "chrome/browser/signin/chrome_signin_helper.h"
24 #include "chrome/browser/translate/chrome_translate_client.h" 23 #include "chrome/browser/translate/chrome_translate_client.h"
25 #include "chrome/browser/ui/browser.h" 24 #include "chrome/browser/ui/browser.h"
26 #include "chrome/browser/ui/browser_command_controller.h" 25 #include "chrome/browser/ui/browser_command_controller.h"
27 #include "chrome/browser/ui/browser_commands.h" 26 #include "chrome/browser/ui/browser_commands.h"
(...skipping 15 matching lines...) Expand all
43 #import "chrome/browser/ui/cocoa/nsmenuitem_additions.h" 42 #import "chrome/browser/ui/cocoa/nsmenuitem_additions.h"
44 #import "chrome/browser/ui/cocoa/page_info/page_info_bubble_controller.h" 43 #import "chrome/browser/ui/cocoa/page_info/page_info_bubble_controller.h"
45 #import "chrome/browser/ui/cocoa/profiles/avatar_base_controller.h" 44 #import "chrome/browser/ui/cocoa/profiles/avatar_base_controller.h"
46 #include "chrome/browser/ui/cocoa/restart_browser.h" 45 #include "chrome/browser/ui/cocoa/restart_browser.h"
47 #include "chrome/browser/ui/cocoa/status_bubble_mac.h" 46 #include "chrome/browser/ui/cocoa/status_bubble_mac.h"
48 #include "chrome/browser/ui/cocoa/task_manager_mac.h" 47 #include "chrome/browser/ui/cocoa/task_manager_mac.h"
49 #import "chrome/browser/ui/cocoa/toolbar/toolbar_controller.h" 48 #import "chrome/browser/ui/cocoa/toolbar/toolbar_controller.h"
50 #include "chrome/browser/ui/exclusive_access/exclusive_access_context.h" 49 #include "chrome/browser/ui/exclusive_access/exclusive_access_context.h"
51 #include "chrome/browser/ui/profile_chooser_constants.h" 50 #include "chrome/browser/ui/profile_chooser_constants.h"
52 #include "chrome/browser/ui/tabs/tab_strip_model.h" 51 #include "chrome/browser/ui/tabs/tab_strip_model.h"
53 #include "chrome/browser/web_applications/web_app.h"
54 #include "chrome/common/chrome_switches.h" 52 #include "chrome/common/chrome_switches.h"
55 #include "chrome/common/features.h" 53 #include "chrome/common/features.h"
56 #include "chrome/common/pref_names.h" 54 #include "chrome/common/pref_names.h"
57 #include "chrome/grit/generated_resources.h" 55 #include "chrome/grit/generated_resources.h"
58 #include "components/bookmarks/common/bookmark_pref_names.h" 56 #include "components/bookmarks/common/bookmark_pref_names.h"
59 #include "components/prefs/pref_service.h" 57 #include "components/prefs/pref_service.h"
60 #include "components/strings/grit/components_strings.h" 58 #include "components/strings/grit/components_strings.h"
61 #include "components/translate/core/browser/language_state.h" 59 #include "components/translate/core/browser/language_state.h"
62 #include "content/public/browser/keyboard_event_processing_result.h" 60 #include "content/public/browser/keyboard_event_processing_result.h"
63 #include "content/public/browser/native_web_keyboard_event.h" 61 #include "content/public/browser/native_web_keyboard_event.h"
64 #include "content/public/browser/notification_details.h" 62 #include "content/public/browser/notification_details.h"
65 #include "content/public/browser/notification_service.h" 63 #include "content/public/browser/notification_service.h"
66 #include "content/public/browser/notification_source.h" 64 #include "content/public/browser/notification_source.h"
67 #include "content/public/browser/web_contents.h" 65 #include "content/public/browser/web_contents.h"
68 #include "extensions/browser/extension_registry.h" 66 #include "extensions/browser/extension_registry.h"
69 #include "extensions/browser/extension_system.h" 67 #include "extensions/browser/extension_system.h"
70 #include "extensions/common/constants.h" 68 #include "extensions/common/constants.h"
71 #include "ui/base/l10n/l10n_util_mac.h" 69 #include "ui/base/l10n/l10n_util_mac.h"
72 #include "ui/base/material_design/material_design_controller.h" 70 #include "ui/base/material_design/material_design_controller.h"
73 #include "ui/events/keycodes/keyboard_codes.h" 71 #include "ui/events/keycodes/keyboard_codes.h"
74 #include "ui/gfx/geometry/rect.h" 72 #include "ui/gfx/geometry/rect.h"
75 73
76 #if BUILDFLAG(ENABLE_ONE_CLICK_SIGNIN) 74 #if BUILDFLAG(ENABLE_ONE_CLICK_SIGNIN)
77 #import "chrome/browser/ui/cocoa/one_click_signin_dialog_controller.h" 75 #import "chrome/browser/ui/cocoa/one_click_signin_dialog_controller.h"
78 #endif 76 #endif
79 77
80 using content::NativeWebKeyboardEvent; 78 using content::NativeWebKeyboardEvent;
81 using content::WebContents; 79 using content::WebContents;
82 80
83 namespace {
84
85 // These UI constants are used in BrowserWindowCocoa::ShowBookmarkAppBubble.
86 // Used for defining the layout of the NSAlert and NSTextField within the
87 // accessory view.
88 const int kAppTextFieldVerticalSpacing = 2;
89 const int kAppTextFieldWidth = 200;
90 const int kAppTextFieldHeight = 22;
91 const int kBookmarkAppBubbleViewWidth = 200;
92 const int kBookmarkAppBubbleViewHeight = 46;
93
94 const int kIconPreviewTargetSize = 128;
95
96 base::string16 TrimText(NSString* controlText) {
97 base::string16 text = base::SysNSStringToUTF16(controlText);
98 base::TrimWhitespace(text, base::TRIM_ALL, &text);
99 return text;
100 }
101
102 } // namespace
103
104 @interface TextRequiringDelegate : NSObject<NSTextFieldDelegate> {
105 @private
106 // Disable |control_| when text changes to just whitespace or empty string.
107 NSControl* control_;
108 }
109 - (id)initWithControl:(NSControl*)control text:(NSString*)text;
110 - (void)controlTextDidChange:(NSNotification*)notification;
111 @end
112
113 @interface TextRequiringDelegate ()
114 - (void)validateText:(NSString*)text;
115 @end
116
117 @implementation TextRequiringDelegate
118 - (id)initWithControl:(NSControl*)control text:(NSString*)text {
119 if ((self = [super init])) {
120 control_ = control;
121 [self validateText:text];
122 }
123 return self;
124 }
125
126 - (void)controlTextDidChange:(NSNotification*)notification {
127 [self validateText:[[notification object] stringValue]];
128 }
129
130 - (void)validateText:(NSString*)text {
131 [control_ setEnabled:TrimText(text).empty() ? NO : YES];
132 }
133 @end
134
135 BrowserWindowCocoa::BrowserWindowCocoa(Browser* browser, 81 BrowserWindowCocoa::BrowserWindowCocoa(Browser* browser,
136 BrowserWindowController* controller) 82 BrowserWindowController* controller)
137 : browser_(browser), 83 : browser_(browser),
138 controller_(controller), 84 controller_(controller),
139 initial_show_state_(ui::SHOW_STATE_DEFAULT), 85 initial_show_state_(ui::SHOW_STATE_DEFAULT),
140 attention_request_id_(0) { 86 attention_request_id_(0) {
141 87
142 gfx::Rect bounds; 88 gfx::Rect bounds;
143 chrome::GetSavedWindowBoundsAndShowState(browser_, 89 chrome::GetSavedWindowBoundsAndShowState(browser_,
144 &bounds, 90 &bounds,
(...skipping 403 matching lines...) Expand 10 before | Expand all | Expand 10 after
548 restart_browser::RequestRestart(window()); 494 restart_browser::RequestRestart(window());
549 } 495 }
550 } 496 }
551 497
552 void BrowserWindowCocoa::ShowBookmarkBubble(const GURL& url, 498 void BrowserWindowCocoa::ShowBookmarkBubble(const GURL& url,
553 bool already_bookmarked) { 499 bool already_bookmarked) {
554 [controller_ showBookmarkBubbleForURL:url 500 [controller_ showBookmarkBubbleForURL:url
555 alreadyBookmarked:(already_bookmarked ? YES : NO)]; 501 alreadyBookmarked:(already_bookmarked ? YES : NO)];
556 } 502 }
557 503
558 void BrowserWindowCocoa::ShowBookmarkAppBubble(
559 const WebApplicationInfo& web_app_info,
560 const ShowBookmarkAppBubbleCallback& callback) {
561 base::scoped_nsobject<NSAlert> alert([[NSAlert alloc] init]);
562 [alert setMessageText:l10n_util::GetNSString(
563 IDS_ADD_TO_APPLICATIONS_BUBBLE_TITLE)];
564 [alert setAlertStyle:NSInformationalAlertStyle];
565
566 NSButton* continue_button =
567 [alert addButtonWithTitle:l10n_util::GetNSString(IDS_OK)];
568 [continue_button setKeyEquivalent:kKeyEquivalentReturn];
569 NSButton* cancel_button =
570 [alert addButtonWithTitle:l10n_util::GetNSString(IDS_CANCEL)];
571 [cancel_button setKeyEquivalent:kKeyEquivalentEscape];
572
573 base::scoped_nsobject<NSButton> open_as_window_checkbox(
574 [[NSButton alloc] initWithFrame:NSZeroRect]);
575 [open_as_window_checkbox setButtonType:NSSwitchButton];
576 [open_as_window_checkbox
577 setTitle:l10n_util::GetNSString(IDS_BOOKMARK_APP_BUBBLE_OPEN_AS_WINDOW)];
578 [open_as_window_checkbox setState:web_app_info.open_as_window];
579 [open_as_window_checkbox sizeToFit];
580
581 base::scoped_nsobject<NSTextField> app_title([[NSTextField alloc]
582 initWithFrame:NSMakeRect(0, kAppTextFieldHeight +
583 kAppTextFieldVerticalSpacing,
584 kAppTextFieldWidth, kAppTextFieldHeight)]);
585 NSString* original_title = SysUTF16ToNSString(web_app_info.title);
586 [[app_title cell] setWraps:NO];
587 [[app_title cell] setScrollable:YES];
588 [app_title setStringValue:original_title];
589 base::scoped_nsobject<TextRequiringDelegate> delegate(
590 [[TextRequiringDelegate alloc] initWithControl:continue_button
591 text:[app_title stringValue]]);
592 [app_title setDelegate:delegate];
593
594 base::scoped_nsobject<NSView> view([[NSView alloc]
595 initWithFrame:NSMakeRect(0, 0, kBookmarkAppBubbleViewWidth,
596 kBookmarkAppBubbleViewHeight)]);
597
598 // When CanHostedAppsOpenInWindows() returns false, do not show the open as
599 // window checkbox to avoid confusing users.
600 if (extensions::util::CanHostedAppsOpenInWindows())
601 [view addSubview:open_as_window_checkbox];
602 [view addSubview:app_title];
603 [alert setAccessoryView:view];
604
605 // Find the image with target size.
606 // Assumes that the icons are sorted in ascending order of size.
607 if (!web_app_info.icons.empty()) {
608 for (const WebApplicationInfo::IconInfo& info : web_app_info.icons) {
609 if (info.width == kIconPreviewTargetSize &&
610 info.height == kIconPreviewTargetSize) {
611 gfx::Image icon_image = gfx::Image::CreateFrom1xBitmap(info.data);
612 [alert setIcon:icon_image.ToNSImage()];
613 break;
614 }
615 }
616 }
617
618 NSInteger response = [alert runModal];
619
620 // Prevent |app_title| from accessing |delegate| after it's destroyed.
621 [app_title setDelegate:nil];
622
623 if (response == NSAlertFirstButtonReturn) {
624 WebApplicationInfo updated_info = web_app_info;
625 updated_info.open_as_window = [open_as_window_checkbox state] == NSOnState;
626 updated_info.title = TrimText([app_title stringValue]);
627
628 callback.Run(true, updated_info);
629 return;
630 }
631
632 callback.Run(false, web_app_info);
633 }
634
635 autofill::SaveCardBubbleView* BrowserWindowCocoa::ShowSaveCreditCardBubble( 504 autofill::SaveCardBubbleView* BrowserWindowCocoa::ShowSaveCreditCardBubble(
636 content::WebContents* web_contents, 505 content::WebContents* web_contents,
637 autofill::SaveCardBubbleController* controller, 506 autofill::SaveCardBubbleController* controller,
638 bool user_gesture) { 507 bool user_gesture) {
639 return new autofill::SaveCardBubbleViewBridge(controller, controller_); 508 return new autofill::SaveCardBubbleViewBridge(controller, controller_);
640 } 509 }
641 510
642 ShowTranslateBubbleResult BrowserWindowCocoa::ShowTranslateBubble( 511 ShowTranslateBubbleResult BrowserWindowCocoa::ShowTranslateBubble(
643 content::WebContents* contents, 512 content::WebContents* contents,
644 translate::TranslateStep step, 513 translate::TranslateStep step,
(...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after
854 ExclusiveAccessContext* BrowserWindowCocoa::GetExclusiveAccessContext() { 723 ExclusiveAccessContext* BrowserWindowCocoa::GetExclusiveAccessContext() {
855 return [controller_ exclusiveAccessController]; 724 return [controller_ exclusiveAccessController];
856 } 725 }
857 726
858 void BrowserWindowCocoa::ShowImeWarningBubble( 727 void BrowserWindowCocoa::ShowImeWarningBubble(
859 const extensions::Extension* extension, 728 const extensions::Extension* extension,
860 const base::Callback<void(ImeWarningBubblePermissionStatus status)>& 729 const base::Callback<void(ImeWarningBubblePermissionStatus status)>&
861 callback) { 730 callback) {
862 NOTREACHED() << "The IME warning bubble is unsupported on this platform."; 731 NOTREACHED() << "The IME warning bubble is unsupported on this platform.";
863 } 732 }
OLDNEW
« no previous file with comments | « chrome/browser/ui/cocoa/browser_window_cocoa.h ('k') | chrome/browser/ui/views/extensions/bookmark_app_confirmation_view.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698