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

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

Issue 1972843002: MacViews GN: Get chrome compiling with mac_views_browser = true (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase Created 4 years, 7 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
(Empty)
1 // Copyright 2016 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 <Cocoa/Cocoa.h>
6
7 #import "base/mac/scoped_nsobject.h"
8 #import "chrome/browser/web_applications/web_app_mac.h"
9 #import "ui/base/l10n/l10n_util_mac.h"
10 #include "base/bind.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/browser/ui/cocoa/key_equivalent_constants.h"
13 #include "chrome/grit/generated_resources.h"
14 #include "grit/components_strings.h"
15 #include "ui/gfx/image/image.h"
16
17 using web_app::ShortcutInfo;
msw 2016/05/16 19:17:03 optional nit: inline namespace in one use.
tapted 2016/05/17 03:38:37 Done.
18 using web_app::ShortcutLocations;
msw 2016/05/16 19:17:03 nit: remove if not used
tapted 2016/05/17 03:38:37 Done - there was just one use. I think I left thes
19
20 @interface CrCreateAppShortcutCheckboxObserver : NSObject {
21 @private
22 NSButton* checkbox_;
23 NSButton* continueButton_;
24 }
25
26 - (id)initWithCheckbox:(NSButton*)checkbox
27 continueButton:(NSButton*)continueButton;
28 - (void)startObserving;
29 - (void)stopObserving;
30 @end
31
32 @implementation CrCreateAppShortcutCheckboxObserver
33
34 - (id)initWithCheckbox:(NSButton*)checkbox
35 continueButton:(NSButton*)continueButton {
36 if ((self = [super init])) {
37 checkbox_ = checkbox;
38 continueButton_ = continueButton;
39 }
40 return self;
41 }
42
43 - (void)startObserving {
44 [checkbox_ addObserver:self
45 forKeyPath:@"cell.state"
46 options:0
47 context:nil];
48 }
49
50 - (void)stopObserving {
51 [checkbox_ removeObserver:self
52 forKeyPath:@"cell.state"];
53 }
54
55 - (void)observeValueForKeyPath:(NSString*)keyPath
56 ofObject:(id)object
57 change:(NSDictionary*)change
58 context:(void*)context {
59 [continueButton_ setEnabled:([checkbox_ state] == NSOnState)];
60 }
61
62 @end
63
64 namespace {
65
66 // Called when the app's ShortcutInfo (with icon) is loaded when creating app
67 // shortcuts.
68 void CreateAppShortcutInfoLoaded(
69 Profile* profile,
70 const extensions::Extension* app,
71 const base::Callback<void(bool)>& close_callback,
72 std::unique_ptr<ShortcutInfo> shortcut_info) {
73 base::scoped_nsobject<NSAlert> alert([[NSAlert alloc] init]);
74
75 NSButton* continue_button = [alert
76 addButtonWithTitle:l10n_util::GetNSString(IDS_CREATE_SHORTCUTS_COMMIT)];
77 [continue_button setKeyEquivalent:kKeyEquivalentReturn];
78
79 NSButton* cancel_button =
80 [alert addButtonWithTitle:l10n_util::GetNSString(IDS_CANCEL)];
81 [cancel_button setKeyEquivalent:kKeyEquivalentEscape];
82
83 [alert setMessageText:l10n_util::GetNSString(IDS_CREATE_SHORTCUTS_LABEL)];
84 [alert setAlertStyle:NSInformationalAlertStyle];
85
86 base::scoped_nsobject<NSButton> application_folder_checkbox(
87 [[NSButton alloc] initWithFrame:NSZeroRect]);
88 [application_folder_checkbox setButtonType:NSSwitchButton];
89 [application_folder_checkbox
90 setTitle:l10n_util::GetNSString(IDS_CREATE_SHORTCUTS_APP_FOLDER_CHKBOX)];
91 [application_folder_checkbox setState:NSOnState];
92 [application_folder_checkbox sizeToFit];
93
94 base::scoped_nsobject<CrCreateAppShortcutCheckboxObserver> checkbox_observer(
95 [[CrCreateAppShortcutCheckboxObserver alloc]
96 initWithCheckbox:application_folder_checkbox
97 continueButton:continue_button]);
98 [checkbox_observer startObserving];
99
100 [alert setAccessoryView:application_folder_checkbox];
101
102 const int kIconPreviewSizePixels = 128;
103 const int kIconPreviewTargetSize = 64;
104 const gfx::Image* icon = shortcut_info->favicon.GetBest(
105 kIconPreviewSizePixels, kIconPreviewSizePixels);
106
107 if (icon && !icon->IsEmpty()) {
108 NSImage* icon_image = icon->ToNSImage();
109 [icon_image
110 setSize:NSMakeSize(kIconPreviewTargetSize, kIconPreviewTargetSize)];
111 [alert setIcon:icon_image];
112 }
113
114 bool dialog_accepted = false;
115 if ([alert runModal] == NSAlertFirstButtonReturn &&
116 [application_folder_checkbox state] == NSOnState) {
117 dialog_accepted = true;
118 CreateShortcuts(
119 web_app::SHORTCUT_CREATION_BY_USER, ShortcutLocations(), profile, app);
120 }
121
122 [checkbox_observer stopObserving];
123
124 if (!close_callback.is_null())
125 close_callback.Run(dialog_accepted);
126 }
127
128 } // namespace
129
130 namespace chrome {
131
132 void ShowCreateChromeAppShortcutsDialog(
133 gfx::NativeWindow /*parent_window*/,
134 Profile* profile,
135 const extensions::Extension* app,
136 const base::Callback<void(bool)>& close_callback) {
137 web_app::GetShortcutInfoForApp(
138 app,
139 profile,
140 base::Bind(&CreateAppShortcutInfoLoaded,
141 profile,
142 app,
143 close_callback));
144 }
145
146 } // namespace chrome
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698