Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2015 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 #include "chrome/browser/ui/views/website_settings/chooser_bubble_ui_view.h" | |
| 6 | |
| 7 #include <string> | |
| 8 | |
| 9 #include "base/prefs/pref_service.h" | |
| 10 #include "base/strings/string16.h" | |
| 11 #include "chrome/browser/profiles/profile.h" | |
| 12 #include "chrome/browser/ui/browser.h" | |
| 13 #include "chrome/browser/ui/views/exclusive_access_bubble_views.h" | |
| 14 #include "chrome/browser/ui/views/frame/browser_view.h" | |
| 15 #include "chrome/browser/ui/views/frame/top_container_view.h" | |
| 16 #include "chrome/browser/ui/views/location_bar/location_bar_view.h" | |
| 17 #include "chrome/browser/ui/views/location_bar/location_icon_view.h" | |
| 18 #include "chrome/browser/ui/website_settings/chooser_bubble_delegate.h" | |
| 19 #include "chrome/browser/ui/website_settings/chooser_options.h" | |
| 20 #include "chrome/common/pref_names.h" | |
| 21 #include "chrome/grit/generated_resources.h" | |
| 22 #include "ui/accessibility/ax_view_state.h" | |
| 23 #include "ui/base/l10n/l10n_util.h" | |
| 24 #include "ui/base/resource/resource_bundle.h" | |
| 25 #include "ui/gfx/paint_vector_icon.h" | |
| 26 #include "ui/gfx/text_constants.h" | |
| 27 #include "ui/gfx/vector_icons_public.h" | |
| 28 #include "ui/views/bubble/bubble_delegate.h" | |
| 29 #include "ui/views/bubble/bubble_frame_view.h" | |
| 30 #include "ui/views/controls/button/label_button.h" | |
| 31 #include "ui/views/controls/button/label_button_border.h" | |
| 32 #include "ui/views/controls/table/table_view.h" | |
| 33 #include "ui/views/controls/table/table_view_observer.h" | |
| 34 #include "ui/views/layout/box_layout.h" | |
| 35 #include "ui/views/layout/grid_layout.h" | |
| 36 | |
| 37 namespace { | |
| 38 | |
| 39 // chooser permission bubble width | |
| 40 const int kChooserPermissionBubbleWidth = 300; | |
| 41 | |
| 42 // chooser permission bubble height | |
| 43 const int kChooserPermissionBubbleHeight = 200; | |
| 44 | |
| 45 // Spacing constant for outer margin. This is added to the | |
| 46 // bubble margin itself to equalize the margins at 13px. | |
| 47 const int kBubbleOuterMargin = 5; | |
| 48 | |
| 49 // Spacing between major items should be 9px. | |
| 50 const int kItemMajorSpacing = 9; | |
| 51 | |
| 52 // Button border size, draws inside the spacing distance. | |
| 53 const int kButtonBorderSize = 2; | |
| 54 | |
| 55 } // namespace | |
| 56 | |
| 57 class ChooserTableModel; | |
| 58 | |
| 59 /////////////////////////////////////////////////////////////////////////////// | |
| 60 // View implementation for the chooser bubble. | |
| 61 class ChooserBubbleUiViewDelegate : public views::BubbleDelegateView, | |
| 62 public views::ButtonListener, | |
| 63 public views::TableViewObserver { | |
| 64 public: | |
| 65 ChooserBubbleUiViewDelegate(views::View* anchor_view, | |
| 66 views::BubbleBorder::Arrow anchor_arrow, | |
| 67 ChooserBubbleUiView* owner, | |
| 68 ChooserOptions* chooser_options, | |
| 69 ChooserBubbleDelegate* chooser_bubble_delegate); | |
| 70 ~ChooserBubbleUiViewDelegate() override; | |
| 71 | |
| 72 void Close(); | |
| 73 | |
| 74 // BubbleDelegateView: | |
| 75 bool ShouldShowCloseButton() const override; | |
| 76 bool ShouldShowWindowTitle() const override; | |
| 77 base::string16 GetWindowTitle() const override; | |
| 78 void OnWidgetDestroying(views::Widget* widget) override; | |
| 79 | |
| 80 // ButtonListener: | |
| 81 void ButtonPressed(views::Button* button, const ui::Event& event) override; | |
| 82 | |
| 83 // views::TableViewObserver: | |
| 84 void OnSelectionChanged() override; | |
| 85 | |
| 86 // Updates the anchor's arrow and view. Also repositions the bubble so it's | |
| 87 // displayed in the correct location. | |
| 88 void UpdateAnchor(views::View* anchor_view, | |
| 89 views::BubbleBorder::Arrow anchor_arrow); | |
| 90 | |
| 91 private: | |
| 92 friend ChooserBubbleUiView; | |
| 93 | |
| 94 ChooserBubbleUiView* owner_; | |
| 95 ChooserOptions* chooser_options_; | |
| 96 ChooserBubbleDelegate* chooser_bubble_delegate_; | |
| 97 | |
| 98 views::LabelButton* connect_button_; | |
| 99 views::LabelButton* cancel_button_; | |
| 100 views::TableView* table_view_; | |
| 101 ChooserTableModel* chooser_table_model_; | |
| 102 | |
| 103 DISALLOW_COPY_AND_ASSIGN(ChooserBubbleUiViewDelegate); | |
| 104 }; | |
| 105 | |
| 106 ui::TableColumn ChooserTableColumn(int id, const std::string& title) { | |
| 107 ui::TableColumn column; | |
| 108 column.id = id; | |
| 109 column.title = base::ASCIIToUTF16(title.c_str()); | |
| 110 return column; | |
| 111 } | |
| 112 | |
| 113 class ChooserTableModel : public ui::TableModel, | |
| 114 public ChooserOptions::Observer { | |
| 115 public: | |
| 116 explicit ChooserTableModel(ChooserOptions* chooser_options) | |
| 117 : observer_(nullptr), chooser_options_(chooser_options) { | |
| 118 chooser_options_->set_observer(this); | |
| 119 } | |
| 120 | |
| 121 // ui::TableModel: | |
| 122 int RowCount() override { | |
| 123 const std::vector<base::string16>& device_names = | |
| 124 chooser_options_->GetOptions(); | |
| 125 if (device_names.empty()) { | |
| 126 return 1; | |
| 127 } else { | |
| 128 return static_cast<int>(chooser_options_->GetOptions().size()); | |
| 129 } | |
| 130 } | |
| 131 | |
| 132 // ui::TableModel: | |
| 133 base::string16 GetText(int row, int column_id) override { | |
| 134 const std::vector<base::string16>& device_names = | |
| 135 chooser_options_->GetOptions(); | |
| 136 if (device_names.empty()) { | |
| 137 DCHECK(row == 0); | |
| 138 return l10n_util::GetStringUTF16( | |
| 139 IDS_CHOOSER_BUBBLE_NO_DEVICES_FOUND_PROMPT); | |
| 140 } else { | |
| 141 if (row >= 0 && row < static_cast<int>(device_names.size())) { | |
| 142 return device_names[row]; | |
| 143 } else { | |
| 144 return base::string16(); | |
| 145 } | |
| 146 } | |
| 147 } | |
| 148 | |
| 149 // ui::TableModel: | |
| 150 void SetObserver(ui::TableModelObserver* observer) override { | |
| 151 observer_ = observer; | |
| 152 } | |
| 153 | |
| 154 // ChooserOptions::Observer: | |
| 155 void OnOptionsInitialized() override { | |
| 156 if (observer_) { | |
| 157 observer_->OnModelChanged(); | |
| 158 Update(); | |
| 159 } | |
| 160 } | |
| 161 | |
| 162 // ChooserOptions::Observer: | |
| 163 void OnOptionAdded(int index) override { | |
| 164 if (observer_) { | |
| 165 observer_->OnItemsAdded(index, 1); | |
| 166 Update(); | |
| 167 } | |
| 168 } | |
| 169 | |
| 170 // ChooserOptions::Observer: | |
| 171 void OnOptionRemoved(int index) override { | |
| 172 if (observer_) { | |
| 173 observer_->OnItemsRemoved(index, 1); | |
| 174 Update(); | |
| 175 } | |
| 176 } | |
| 177 | |
| 178 void Update() { | |
| 179 views::TableView* table_view = static_cast<views::TableView*>(observer_); | |
| 180 | |
| 181 if (chooser_options_->GetOptions().empty()) { | |
| 182 observer_->OnModelChanged(); | |
| 183 table_view->SetEnabled(false); | |
| 184 } else { | |
| 185 table_view->SetEnabled(true); | |
| 186 } | |
| 187 | |
| 188 connect_button_->SetEnabled(!table_view->selection_model().empty()); | |
| 189 } | |
| 190 | |
| 191 void SetConnectButton(views::LabelButton* connect_button) { | |
| 192 connect_button_ = connect_button; | |
| 193 } | |
| 194 | |
| 195 private: | |
| 196 ui::TableModelObserver* observer_ = nullptr; | |
| 197 ChooserOptions* chooser_options_; | |
| 198 views::LabelButton* connect_button_; | |
| 199 }; | |
| 200 | |
| 201 ChooserBubbleUiViewDelegate::ChooserBubbleUiViewDelegate( | |
| 202 views::View* anchor_view, | |
| 203 views::BubbleBorder::Arrow anchor_arrow, | |
| 204 ChooserBubbleUiView* owner, | |
| 205 ChooserOptions* chooser_options, | |
| 206 ChooserBubbleDelegate* chooser_bubble_delegate) | |
| 207 : views::BubbleDelegateView(anchor_view, anchor_arrow), | |
| 208 owner_(owner), | |
| 209 chooser_options_(chooser_options), | |
| 210 chooser_bubble_delegate_(chooser_bubble_delegate) { | |
| 211 views::GridLayout* layout = new views::GridLayout(this); | |
| 212 SetLayoutManager(layout); | |
| 213 | |
| 214 views::ColumnSet* column_set = layout->AddColumnSet(0); | |
| 215 column_set->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL, 1, | |
| 216 views::GridLayout::USE_PREF, 0, 0); | |
| 217 | |
| 218 layout->StartRow(1, 0); | |
| 219 | |
| 220 // create a table view | |
| 221 std::vector<ui::TableColumn> table_columns; | |
| 222 table_columns.push_back(ChooserTableColumn( | |
| 223 0, "" /* empty string makes the column title invisible */)); | |
| 224 chooser_table_model_ = new ChooserTableModel(chooser_options_); | |
| 225 table_view_ = new views::TableView(chooser_table_model_, table_columns, | |
| 226 views::TEXT_ONLY, true); | |
| 227 table_view_->set_select_on_remove(false); | |
| 228 chooser_table_model_->SetObserver(table_view_); | |
| 229 table_view_->SetObserver(this); | |
| 230 layout->AddView(table_view_->CreateParentIfNecessary(), 1, 1, | |
| 231 views::GridLayout::FILL, views::GridLayout::FILL, | |
| 232 kChooserPermissionBubbleWidth, | |
| 233 kChooserPermissionBubbleHeight); | |
| 234 if (chooser_options_->GetOptions().empty()) { | |
| 235 table_view_->SetEnabled(false); | |
| 236 } | |
| 237 | |
| 238 layout->AddPaddingRow(0, kItemMajorSpacing); | |
| 239 | |
| 240 views::View* button_row = new views::View(); | |
| 241 views::GridLayout* button_layout = new views::GridLayout(button_row); | |
| 242 views::ColumnSet* button_columns = button_layout->AddColumnSet(0); | |
| 243 button_row->SetLayoutManager(button_layout); | |
| 244 layout->StartRow(1, 0); | |
| 245 layout->AddView(button_row); | |
| 246 | |
| 247 // lay out the Connect/Cancel buttons. | |
| 248 button_columns->AddColumn(views::GridLayout::TRAILING, | |
| 249 views::GridLayout::FILL, 100, | |
| 250 views::GridLayout::USE_PREF, 0, 0); | |
| 251 button_columns->AddPaddingColumn(0, | |
| 252 kItemMajorSpacing - (2 * kButtonBorderSize)); | |
| 253 button_columns->AddColumn(views::GridLayout::TRAILING, | |
| 254 views::GridLayout::FILL, 0, | |
| 255 views::GridLayout::USE_PREF, 0, 0); | |
| 256 button_layout->StartRow(0, 0); | |
| 257 | |
| 258 base::string16 connect_text = | |
| 259 l10n_util::GetStringUTF16(IDS_CHOOSER_BUBBLE_CONNECT_BUTTON_TEXT); | |
| 260 connect_button_ = new views::LabelButton(this, connect_text); | |
| 261 connect_button_->SetStyle(views::Button::STYLE_BUTTON); | |
| 262 // disable the connect button at the beginning since no device selected yet. | |
| 263 connect_button_->SetEnabled(false); | |
| 264 button_layout->AddView(connect_button_); | |
| 265 chooser_table_model_->SetConnectButton(connect_button_); | |
| 266 | |
| 267 base::string16 cancel_text = | |
| 268 l10n_util::GetStringUTF16(IDS_CHOOSER_BUBBLE_CANCEL_BUTTON_TEXT); | |
| 269 cancel_button_ = new views::LabelButton(this, cancel_text); | |
| 270 cancel_button_->SetStyle(views::Button::STYLE_BUTTON); | |
| 271 button_layout->AddView(cancel_button_); | |
| 272 | |
| 273 button_layout->AddPaddingRow(0, kBubbleOuterMargin); | |
| 274 } | |
| 275 | |
| 276 ChooserBubbleUiViewDelegate::~ChooserBubbleUiViewDelegate() { | |
| 277 RemoveAllChildViews(true); | |
| 278 if (owner_) | |
| 279 owner_->Close(); | |
| 280 chooser_table_model_->SetObserver(nullptr); | |
| 281 } | |
| 282 | |
| 283 void ChooserBubbleUiViewDelegate::Close() { | |
| 284 owner_ = nullptr; | |
| 285 GetWidget()->Close(); | |
| 286 } | |
| 287 | |
| 288 bool ChooserBubbleUiViewDelegate::ShouldShowCloseButton() const { | |
| 289 return true; | |
| 290 } | |
| 291 | |
| 292 bool ChooserBubbleUiViewDelegate::ShouldShowWindowTitle() const { | |
| 293 return true; | |
| 294 } | |
| 295 | |
| 296 base::string16 ChooserBubbleUiViewDelegate::GetWindowTitle() const { | |
| 297 return l10n_util::GetStringUTF16(IDS_CHOOSER_BUBBLE_PROMPT); | |
| 298 } | |
| 299 | |
| 300 void ChooserBubbleUiViewDelegate::OnWidgetDestroying(views::Widget* widget) { | |
| 301 views::BubbleDelegateView::OnWidgetDestroying(widget); | |
| 302 if (owner_) { | |
| 303 owner_->Close(); | |
| 304 owner_ = nullptr; | |
| 305 } | |
| 306 } | |
| 307 | |
| 308 void ChooserBubbleUiViewDelegate::ButtonPressed(views::Button* button, | |
| 309 const ui::Event& event) { | |
| 310 if (button == connect_button_) | |
| 311 chooser_bubble_delegate_->Select(table_view_->selection_model().active()); | |
| 312 owner_->Close(); | |
| 313 } | |
| 314 | |
| 315 void ChooserBubbleUiViewDelegate::OnSelectionChanged() { | |
| 316 chooser_table_model_->Update(); | |
|
Reilly Grant (use Gerrit)
2015/11/16 22:45:45
Just move line 188 here.
juncai
2015/11/17 03:42:15
Done.
| |
| 317 } | |
| 318 | |
| 319 void ChooserBubbleUiViewDelegate::UpdateAnchor( | |
| 320 views::View* anchor_view, | |
| 321 views::BubbleBorder::Arrow anchor_arrow) { | |
| 322 if (GetAnchorView() == anchor_view && arrow() == anchor_arrow) | |
| 323 return; | |
| 324 | |
| 325 set_arrow(anchor_arrow); | |
| 326 | |
| 327 // Update the border in the bubble: will either add or remove the arrow. | |
| 328 views::BubbleFrameView* frame = | |
| 329 views::BubbleDelegateView::GetBubbleFrameView(); | |
| 330 views::BubbleBorder::Arrow adjusted_arrow = anchor_arrow; | |
| 331 if (base::i18n::IsRTL()) | |
| 332 adjusted_arrow = views::BubbleBorder::horizontal_mirror(adjusted_arrow); | |
| 333 frame->SetBubbleBorder(scoped_ptr<views::BubbleBorder>( | |
| 334 new views::BubbleBorder(adjusted_arrow, shadow(), color()))); | |
| 335 | |
| 336 // Reposition the bubble based on the updated arrow and view. | |
| 337 SetAnchorView(anchor_view); | |
| 338 } | |
| 339 | |
| 340 ////////////////////////////////////////////////////////////////////////////// | |
| 341 // ChooserBubbleUiView | |
| 342 | |
| 343 ChooserBubbleUiView::ChooserBubbleUiView( | |
| 344 Browser* browser, | |
| 345 ChooserOptions* chooser_options, | |
| 346 ChooserBubbleDelegate* chooser_bubble_delegate) | |
| 347 : browser_(browser), | |
| 348 chooser_options_(chooser_options), | |
| 349 chooser_bubble_delegate_(chooser_bubble_delegate), | |
| 350 chooser_bubble_ui_view_delegate_(nullptr) { | |
| 351 DCHECK(browser_); | |
| 352 DCHECK(chooser_options_); | |
| 353 DCHECK(chooser_bubble_delegate_); | |
| 354 } | |
| 355 | |
| 356 ChooserBubbleUiView::~ChooserBubbleUiView() {} | |
| 357 | |
| 358 void ChooserBubbleUiView::Show(BubbleReference bubble_reference) { | |
| 359 chooser_bubble_ui_view_delegate_ = new ChooserBubbleUiViewDelegate( | |
| 360 GetAnchorView(), GetAnchorArrow(), this, chooser_options_, | |
| 361 chooser_bubble_delegate_); | |
| 362 | |
| 363 // Set |parent_window| because some valid anchors can become hidden. | |
| 364 views::Widget* widget = views::Widget::GetWidgetForNativeWindow( | |
| 365 browser_->window()->GetNativeWindow()); | |
| 366 chooser_bubble_ui_view_delegate_->set_parent_window(widget->GetNativeView()); | |
| 367 | |
| 368 views::BubbleDelegateView::CreateBubble(chooser_bubble_ui_view_delegate_) | |
| 369 ->Show(); | |
| 370 } | |
| 371 | |
| 372 void ChooserBubbleUiView::Close() { | |
| 373 if (chooser_bubble_ui_view_delegate_) { | |
| 374 chooser_bubble_ui_view_delegate_->Close(); | |
| 375 chooser_bubble_ui_view_delegate_ = nullptr; | |
| 376 } | |
| 377 } | |
| 378 | |
| 379 void ChooserBubbleUiView::UpdateAnchorPosition() { | |
| 380 if (chooser_bubble_ui_view_delegate_) { | |
| 381 chooser_bubble_ui_view_delegate_->UpdateAnchor(GetAnchorView(), | |
| 382 GetAnchorArrow()); | |
| 383 } | |
| 384 } | |
| 385 | |
| 386 views::View* ChooserBubbleUiView::GetAnchorView() { | |
| 387 BrowserView* browser_view = BrowserView::GetBrowserViewForBrowser(browser_); | |
| 388 | |
| 389 if (browser_->SupportsWindowFeature(Browser::FEATURE_LOCATIONBAR)) | |
| 390 return browser_view->GetLocationBarView()->location_icon_view(); | |
| 391 | |
| 392 if (browser_view->IsFullscreenBubbleVisible()) | |
| 393 return browser_view->exclusive_access_bubble()->GetView(); | |
| 394 | |
| 395 return browser_view->top_container(); | |
| 396 } | |
| 397 | |
| 398 views::BubbleBorder::Arrow ChooserBubbleUiView::GetAnchorArrow() { | |
| 399 if (browser_->SupportsWindowFeature(Browser::FEATURE_LOCATIONBAR)) | |
| 400 return views::BubbleBorder::TOP_LEFT; | |
| 401 return views::BubbleBorder::NONE; | |
| 402 } | |
| OLD | NEW |