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

Side by Side Diff: chrome/browser/ui/views/create_application_shortcut_view.cc

Issue 2929733003: MacViews: Wire up CreateChromeApplicationShortcutView
Patch Set: rename ivar 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
« no previous file with comments | « chrome/browser/ui/views/create_application_shortcut_view.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/views/create_application_shortcut_view.h" 5 #include "chrome/browser/ui/views/create_application_shortcut_view.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/bind_helpers.h" 8 #include "base/bind_helpers.h"
9 #include "build/build_config.h" 9 #include "build/build_config.h"
10 #include "chrome/browser/profiles/profile.h" 10 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/browser/ui/browser_dialogs.h" 11 #include "chrome/browser/ui/browser_dialogs.h"
12 #include "chrome/browser/ui/views/harmony/chrome_layout_provider.h" 12 #include "chrome/browser/ui/views/harmony/chrome_layout_provider.h"
13 #include "chrome/common/pref_names.h" 13 #include "chrome/common/pref_names.h"
14 #include "chrome/grit/generated_resources.h" 14 #include "chrome/grit/generated_resources.h"
15 #include "components/constrained_window/constrained_window_views.h" 15 #include "components/constrained_window/constrained_window_views.h"
16 #include "components/prefs/pref_service.h" 16 #include "components/prefs/pref_service.h"
17 #include "extensions/common/extension.h" 17 #include "extensions/common/extension.h"
18 #include "ui/base/l10n/l10n_util.h" 18 #include "ui/base/l10n/l10n_util.h"
19 #include "ui/base/ui_features.h"
19 #include "ui/views/controls/button/checkbox.h" 20 #include "ui/views/controls/button/checkbox.h"
20 #include "ui/views/controls/label.h" 21 #include "ui/views/controls/label.h"
21 #include "ui/views/layout/grid_layout.h" 22 #include "ui/views/layout/grid_layout.h"
22 #include "ui/views/window/dialog_client_view.h" 23 #include "ui/views/window/dialog_client_view.h"
23 24
24 #if defined(OS_WIN) 25 #if defined(OS_WIN)
25 #include "base/win/shortcut.h" 26 #include "base/win/shortcut.h"
26 #include "base/win/windows_version.h" 27 #include "base/win/windows_version.h"
27 #endif // defined(OS_WIN) 28 #endif // defined(OS_WIN)
28 29
30 namespace {
31
32 bool IsChecked(views::Checkbox* checkbox) {
33 return checkbox && checkbox->checked();
34 }
35
36 } // namespace
37
29 namespace chrome { 38 namespace chrome {
30 39
40 #if !defined(OS_MACOSX) || BUILDFLAG(MAC_VIEWS_BROWSER)
31 void ShowCreateChromeAppShortcutsDialog( 41 void ShowCreateChromeAppShortcutsDialog(
32 gfx::NativeWindow parent_window, 42 gfx::NativeWindow parent_window,
33 Profile* profile, 43 Profile* profile,
34 const extensions::Extension* app, 44 const extensions::Extension* app,
35 const base::Callback<void(bool)>& close_callback) { 45 const base::Callback<void(bool)>& close_callback) {
36 constrained_window::CreateBrowserModalDialogViews( 46 constrained_window::CreateBrowserModalDialogViews(
37 new CreateChromeApplicationShortcutView(profile, app, close_callback), 47 new CreateChromeApplicationShortcutView(profile, app, close_callback),
38 parent_window)->Show(); 48 parent_window)->Show();
39 } 49 }
50 #endif
40 51
41 } // namespace chrome 52 } // namespace chrome
42 53
43 CreateChromeApplicationShortcutView::CreateChromeApplicationShortcutView( 54 CreateChromeApplicationShortcutView::CreateChromeApplicationShortcutView(
44 Profile* profile, 55 Profile* profile,
45 const extensions::Extension* app, 56 const extensions::Extension* app,
46 const base::Callback<void(bool)>& close_callback) 57 const base::Callback<void(bool)>& close_callback)
47 : profile_(profile), 58 : profile_(profile),
48 close_callback_(close_callback), 59 close_callback_(close_callback),
49 create_shortcuts_label_(nullptr), 60 create_shortcuts_label_(nullptr),
50 desktop_check_box_(nullptr), 61 desktop_check_box_(nullptr),
51 menu_check_box_(nullptr), 62 applications_check_box_(nullptr),
52 quick_launch_check_box_(nullptr), 63 quick_launch_check_box_(nullptr),
53 weak_ptr_factory_(this) { 64 weak_ptr_factory_(this) {
54 InitControls(); 65 InitControls();
55 66
56 // Get shortcut and icon information; needed for creating the shortcut. 67 // Get shortcut and icon information; needed for creating the shortcut.
57 web_app::GetShortcutInfoForApp( 68 web_app::GetShortcutInfoForApp(
58 app, profile, 69 app, profile,
59 base::Bind(&CreateChromeApplicationShortcutView::OnAppInfoLoaded, 70 base::Bind(&CreateChromeApplicationShortcutView::OnAppInfoLoaded,
60 weak_ptr_factory_.GetWeakPtr())); 71 weak_ptr_factory_.GetWeakPtr()));
61 chrome::RecordDialogCreation( 72 chrome::RecordDialogCreation(
62 chrome::DialogIdentifier::CREATE_CHROME_APPLICATION_SHORTCUT); 73 chrome::DialogIdentifier::CREATE_CHROME_APPLICATION_SHORTCUT);
63 } 74 }
64 75
65 CreateChromeApplicationShortcutView::~CreateChromeApplicationShortcutView() {} 76 CreateChromeApplicationShortcutView::~CreateChromeApplicationShortcutView() {}
66 77
67 void CreateChromeApplicationShortcutView::InitControls() { 78 void CreateChromeApplicationShortcutView::InitControls() {
68 create_shortcuts_label_ = new views::Label( 79 create_shortcuts_label_ = new views::Label(
69 l10n_util::GetStringUTF16(IDS_CREATE_SHORTCUTS_LABEL)); 80 l10n_util::GetStringUTF16(IDS_CREATE_SHORTCUTS_LABEL));
70 create_shortcuts_label_->SetHorizontalAlignment(gfx::ALIGN_LEFT); 81 create_shortcuts_label_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
71 create_shortcuts_label_->SetMultiLine(true); 82 create_shortcuts_label_->SetMultiLine(true);
72 83
84 desktop_check_box_ = nullptr;
85 applications_check_box_ = nullptr;
86 quick_launch_check_box_ = nullptr;
87
88 #if !defined(OS_MACOSX)
73 desktop_check_box_ = AddCheckbox( 89 desktop_check_box_ = AddCheckbox(
74 l10n_util::GetStringUTF16(IDS_CREATE_SHORTCUTS_DESKTOP_CHKBOX), 90 l10n_util::GetStringUTF16(IDS_CREATE_SHORTCUTS_DESKTOP_CHKBOX),
75 profile_->GetPrefs()->GetBoolean(prefs::kWebAppCreateOnDesktop)); 91 profile_->GetPrefs()->GetBoolean(prefs::kWebAppCreateOnDesktop));
76 92 #endif
77 menu_check_box_ = nullptr;
78 quick_launch_check_box_ = nullptr;
79 93
80 #if defined(OS_WIN) 94 #if defined(OS_WIN)
81 base::win::Version version = base::win::GetVersion(); 95 base::win::Version version = base::win::GetVersion();
82 // Do not allow creating shortcuts on the Start Screen for Windows 8. 96 // Do not allow creating shortcuts on the Start Screen for Windows 8.
83 if (version != base::win::VERSION_WIN8 && 97 if (version != base::win::VERSION_WIN8 &&
84 version != base::win::VERSION_WIN8_1) { 98 version != base::win::VERSION_WIN8_1) {
85 menu_check_box_ = AddCheckbox( 99 applications_check_box_ = AddCheckbox(
86 l10n_util::GetStringUTF16(IDS_CREATE_SHORTCUTS_START_MENU_CHKBOX), 100 l10n_util::GetStringUTF16(IDS_CREATE_SHORTCUTS_START_MENU_CHKBOX),
87 profile_->GetPrefs()->GetBoolean(prefs::kWebAppCreateInAppsMenu)); 101 profile_->GetPrefs()->GetBoolean(prefs::kWebAppCreateInAppsMenu));
88 } 102 }
89 103
90 // Win10 actively prevents creating shortcuts on the taskbar so we eliminate 104 // Win10 actively prevents creating shortcuts on the taskbar so we eliminate
91 // that option from the dialog. 105 // that option from the dialog.
92 if (base::win::CanPinShortcutToTaskbar()) { 106 if (base::win::CanPinShortcutToTaskbar()) {
93 quick_launch_check_box_ = AddCheckbox( 107 quick_launch_check_box_ = AddCheckbox(
94 (version >= base::win::VERSION_WIN7) 108 (version >= base::win::VERSION_WIN7)
95 ? l10n_util::GetStringUTF16(IDS_PIN_TO_TASKBAR_CHKBOX) 109 ? l10n_util::GetStringUTF16(IDS_PIN_TO_TASKBAR_CHKBOX)
96 : l10n_util::GetStringUTF16( 110 : l10n_util::GetStringUTF16(
97 IDS_CREATE_SHORTCUTS_QUICK_LAUNCH_BAR_CHKBOX), 111 IDS_CREATE_SHORTCUTS_QUICK_LAUNCH_BAR_CHKBOX),
98 profile_->GetPrefs()->GetBoolean(prefs::kWebAppCreateInQuickLaunchBar)); 112 profile_->GetPrefs()->GetBoolean(prefs::kWebAppCreateInQuickLaunchBar));
99 } 113 }
100 #elif defined(OS_POSIX) 114 #else
101 menu_check_box_ = AddCheckbox( 115 applications_check_box_ = AddCheckbox(
102 l10n_util::GetStringUTF16(IDS_CREATE_SHORTCUTS_MENU_CHKBOX), 116 l10n_util::GetStringUTF16(IDS_CREATE_SHORTCUTS_APPLICATIONS_CHKBOX),
103 profile_->GetPrefs()->GetBoolean(prefs::kWebAppCreateInAppsMenu)); 117 profile_->GetPrefs()->GetBoolean(prefs::kWebAppCreateInAppsMenu));
104 #endif 118 #endif
105 119
106 ChromeLayoutProvider* provider = ChromeLayoutProvider::Get(); 120 ChromeLayoutProvider* provider = ChromeLayoutProvider::Get();
107 121
108 // Layout controls 122 // Layout controls
109 views::GridLayout* layout = views::GridLayout::CreatePanel(this); 123 views::GridLayout* layout = views::GridLayout::CreatePanel(this);
110 124
111 static const int kHeaderColumnSetId = 0; 125 static const int kHeaderColumnSetId = 0;
112 views::ColumnSet* column_set = layout->AddColumnSet(kHeaderColumnSetId); 126 views::ColumnSet* column_set = layout->AddColumnSet(kHeaderColumnSetId);
113 column_set->AddColumn(views::GridLayout::FILL, views::GridLayout::CENTER, 127 column_set->AddColumn(views::GridLayout::FILL, views::GridLayout::CENTER,
114 100.0f, views::GridLayout::FIXED, 0, 0); 128 100.0f, views::GridLayout::FIXED, 0, 0);
115 129
116 static const int kTableColumnSetId = 1; 130 static const int kTableColumnSetId = 1;
117 column_set = layout->AddColumnSet(kTableColumnSetId); 131 column_set = layout->AddColumnSet(kTableColumnSetId);
118 column_set->AddPaddingColumn( 132 column_set->AddPaddingColumn(
119 0, provider->GetDistanceMetric(DISTANCE_SUBSECTION_HORIZONTAL_INDENT)); 133 0, provider->GetDistanceMetric(DISTANCE_SUBSECTION_HORIZONTAL_INDENT));
120 column_set->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL, 134 column_set->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL,
121 100.0f, views::GridLayout::USE_PREF, 0, 0); 135 100.0f, views::GridLayout::USE_PREF, 0, 0);
122 136
123 layout->StartRow(0, kHeaderColumnSetId); 137 layout->StartRow(0, kHeaderColumnSetId);
124 layout->AddView(create_shortcuts_label_); 138 layout->AddView(create_shortcuts_label_);
125 139
126 layout->AddPaddingRow(0, provider->GetDistanceMetric( 140 int vertical_spacing =
127 views::DISTANCE_RELATED_CONTROL_VERTICAL)); 141 provider->GetDistanceMetric(views::DISTANCE_RELATED_CONTROL_VERTICAL);
128 layout->StartRow(0, kTableColumnSetId); 142 const int between_spacing =
129 layout->AddView(desktop_check_box_);
130
131 const int vertical_spacing =
132 provider->GetDistanceMetric(DISTANCE_RELATED_CONTROL_VERTICAL_SMALL); 143 provider->GetDistanceMetric(DISTANCE_RELATED_CONTROL_VERTICAL_SMALL);
133 if (menu_check_box_ != nullptr) { 144 if (desktop_check_box_ != nullptr) {
134 layout->AddPaddingRow(0, vertical_spacing); 145 layout->AddPaddingRow(0, vertical_spacing);
135 layout->StartRow(0, kTableColumnSetId); 146 layout->StartRow(0, kTableColumnSetId);
136 layout->AddView(menu_check_box_); 147 layout->AddView(desktop_check_box_);
148 vertical_spacing = between_spacing;
149 }
150
151 if (applications_check_box_ != nullptr) {
152 layout->AddPaddingRow(0, vertical_spacing);
153 layout->StartRow(0, kTableColumnSetId);
154 layout->AddView(applications_check_box_);
155 vertical_spacing = between_spacing;
137 } 156 }
138 157
139 if (quick_launch_check_box_ != nullptr) { 158 if (quick_launch_check_box_ != nullptr) {
140 layout->AddPaddingRow(0, vertical_spacing); 159 layout->AddPaddingRow(0, vertical_spacing);
141 layout->StartRow(0, kTableColumnSetId); 160 layout->StartRow(0, kTableColumnSetId);
142 layout->AddView(quick_launch_check_box_); 161 layout->AddView(quick_launch_check_box_);
143 } 162 }
144 } 163 }
145 164
146 gfx::Size CreateChromeApplicationShortcutView::CalculatePreferredSize() const { 165 gfx::Size CreateChromeApplicationShortcutView::CalculatePreferredSize() const {
147 // TODO(evanm): should this use IDS_CREATE_SHORTCUTS_DIALOG_WIDTH_CHARS? 166 // TODO(evanm): should this use IDS_CREATE_SHORTCUTS_DIALOG_WIDTH_CHARS?
148 static const int kDialogWidth = 360; 167 static const int kDialogWidth = 360;
149 int height = GetLayoutManager()->GetPreferredHeightForWidth(this, 168 int height = GetLayoutManager()->GetPreferredHeightForWidth(this,
150 kDialogWidth); 169 kDialogWidth);
151 return gfx::Size(kDialogWidth, height); 170 return gfx::Size(kDialogWidth, height);
152 } 171 }
153 172
154 base::string16 CreateChromeApplicationShortcutView::GetDialogButtonLabel( 173 base::string16 CreateChromeApplicationShortcutView::GetDialogButtonLabel(
155 ui::DialogButton button) const { 174 ui::DialogButton button) const {
156 if (button == ui::DIALOG_BUTTON_OK) 175 if (button == ui::DIALOG_BUTTON_OK)
157 return l10n_util::GetStringUTF16(IDS_CREATE_SHORTCUTS_COMMIT); 176 return l10n_util::GetStringUTF16(IDS_CREATE_SHORTCUTS_COMMIT);
158 return views::DialogDelegateView::GetDialogButtonLabel(button); 177 return views::DialogDelegateView::GetDialogButtonLabel(button);
159 } 178 }
160 179
161 bool CreateChromeApplicationShortcutView::IsDialogButtonEnabled( 180 bool CreateChromeApplicationShortcutView::IsDialogButtonEnabled(
162 ui::DialogButton button) const { 181 ui::DialogButton button) const {
163 if (button == ui::DIALOG_BUTTON_OK) 182 return button != ui::DIALOG_BUTTON_OK || IsChecked(desktop_check_box_) ||
164 return desktop_check_box_->checked() || 183 IsChecked(applications_check_box_) ||
165 ((menu_check_box_ != nullptr) && 184 IsChecked(quick_launch_check_box_);
166 menu_check_box_->checked()) ||
167 ((quick_launch_check_box_ != nullptr) &&
168 quick_launch_check_box_->checked());
169
170 return true;
171 } 185 }
172 186
173 ui::ModalType CreateChromeApplicationShortcutView::GetModalType() const { 187 ui::ModalType CreateChromeApplicationShortcutView::GetModalType() const {
174 return ui::MODAL_TYPE_WINDOW; 188 return ui::MODAL_TYPE_WINDOW;
175 } 189 }
176 190
177 base::string16 CreateChromeApplicationShortcutView::GetWindowTitle() const { 191 base::string16 CreateChromeApplicationShortcutView::GetWindowTitle() const {
178 return l10n_util::GetStringUTF16(IDS_CREATE_SHORTCUTS_TITLE); 192 return l10n_util::GetStringUTF16(IDS_CREATE_SHORTCUTS_TITLE);
179 } 193 }
180 194
181 bool CreateChromeApplicationShortcutView::Accept() { 195 bool CreateChromeApplicationShortcutView::Accept() {
182 // NOTE: This procedure will reset |shortcut_info_| to null. 196 // NOTE: This procedure will reset |shortcut_info_| to null.
183 if (!close_callback_.is_null()) 197 if (!close_callback_.is_null())
184 close_callback_.Run(true); 198 close_callback_.Run(true);
185 199
186 // Can happen if the shortcut data is not yet loaded. 200 // Can happen if the shortcut data is not yet loaded.
187 if (!shortcut_info_) 201 if (!shortcut_info_)
188 return false; 202 return false;
189 203
190 if (!IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK)) 204 if (!IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK))
191 return false; 205 return false;
192 206
193 web_app::ShortcutLocations creation_locations; 207 web_app::ShortcutLocations creation_locations;
194 creation_locations.on_desktop = desktop_check_box_->checked(); 208 creation_locations.on_desktop = IsChecked(desktop_check_box_);
195 if (menu_check_box_ != nullptr && menu_check_box_->checked()) { 209 if (IsChecked(applications_check_box_)) {
196 creation_locations.applications_menu_location = 210 creation_locations.applications_menu_location =
197 web_app::APP_MENU_LOCATION_SUBDIR_CHROMEAPPS; 211 web_app::APP_MENU_LOCATION_SUBDIR_CHROMEAPPS;
198 } 212 }
199 213
200 #if defined(OS_WIN) 214 #if defined(OS_WIN)
201 creation_locations.in_quick_launch_bar = quick_launch_check_box_ == nullptr ? 215 creation_locations.in_quick_launch_bar = IsChecked(quick_launch_check_box_);
202 false : quick_launch_check_box_->checked(); 216 #else
203 #elif defined(OS_POSIX)
204 // Create shortcut in Mac dock or as Linux (gnome/kde) application launcher 217 // Create shortcut in Mac dock or as Linux (gnome/kde) application launcher
205 // are not implemented yet. 218 // are not implemented yet.
206 creation_locations.in_quick_launch_bar = false; 219 creation_locations.in_quick_launch_bar = false;
207 #endif 220 #endif
208 221
209 web_app::CreateShortcutsWithInfo(web_app::SHORTCUT_CREATION_BY_USER, 222 web_app::CreateShortcutsWithInfo(web_app::SHORTCUT_CREATION_BY_USER,
210 creation_locations, 223 creation_locations,
211 std::move(shortcut_info_)); 224 std::move(shortcut_info_));
212 return true; 225 return true;
213 } 226 }
214 227
215 bool CreateChromeApplicationShortcutView::Cancel() { 228 bool CreateChromeApplicationShortcutView::Cancel() {
216 if (!close_callback_.is_null()) 229 if (!close_callback_.is_null())
217 close_callback_.Run(false); 230 close_callback_.Run(false);
218 return true; 231 return true;
219 } 232 }
220 233
221 void CreateChromeApplicationShortcutView::ButtonPressed( 234 void CreateChromeApplicationShortcutView::ButtonPressed(
222 views::Button* sender, 235 views::Button* sender,
223 const ui::Event& event) { 236 const ui::Event& event) {
224 if (sender == desktop_check_box_) { 237 if (sender == desktop_check_box_) {
225 profile_->GetPrefs()->SetBoolean(prefs::kWebAppCreateOnDesktop, 238 profile_->GetPrefs()->SetBoolean(prefs::kWebAppCreateOnDesktop,
226 desktop_check_box_->checked()); 239 desktop_check_box_->checked());
227 } else if (sender == menu_check_box_) { 240 } else if (sender == applications_check_box_) {
228 profile_->GetPrefs()->SetBoolean(prefs::kWebAppCreateInAppsMenu, 241 profile_->GetPrefs()->SetBoolean(prefs::kWebAppCreateInAppsMenu,
229 menu_check_box_->checked()); 242 applications_check_box_->checked());
230 } else if (sender == quick_launch_check_box_) { 243 } else if (sender == quick_launch_check_box_) {
231 profile_->GetPrefs()->SetBoolean(prefs::kWebAppCreateInQuickLaunchBar, 244 profile_->GetPrefs()->SetBoolean(prefs::kWebAppCreateInQuickLaunchBar,
232 quick_launch_check_box_->checked()); 245 quick_launch_check_box_->checked());
233 } 246 }
234 247
235 // When no checkbox is checked we should not have the action button enabled. 248 // When no checkbox is checked we should not have the action button enabled.
236 GetDialogClientView()->UpdateDialogButtons(); 249 GetDialogClientView()->UpdateDialogButtons();
237 } 250 }
238 251
239 views::Checkbox* CreateChromeApplicationShortcutView::AddCheckbox( 252 views::Checkbox* CreateChromeApplicationShortcutView::AddCheckbox(
240 const base::string16& text, 253 const base::string16& text,
241 bool checked) { 254 bool checked) {
242 views::Checkbox* checkbox = new views::Checkbox(text); 255 views::Checkbox* checkbox = new views::Checkbox(text);
243 checkbox->SetChecked(checked); 256 checkbox->SetChecked(checked);
244 checkbox->set_listener(this); 257 checkbox->set_listener(this);
245 return checkbox; 258 return checkbox;
246 } 259 }
247 260
248 void CreateChromeApplicationShortcutView::OnAppInfoLoaded( 261 void CreateChromeApplicationShortcutView::OnAppInfoLoaded(
249 std::unique_ptr<web_app::ShortcutInfo> shortcut_info) { 262 std::unique_ptr<web_app::ShortcutInfo> shortcut_info) {
250 shortcut_info_ = std::move(shortcut_info); 263 shortcut_info_ = std::move(shortcut_info);
251 } 264 }
OLDNEW
« no previous file with comments | « chrome/browser/ui/views/create_application_shortcut_view.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698