Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / ui / views / extensions / media_gallery_checkbox_view.cc
blob6d8c8c65c85d580fe206030d4bbb53c0648e88db
1 // Copyright 2014 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.
5 #include "chrome/browser/ui/views/extensions/media_gallery_checkbox_view.h"
7 #include "chrome/browser/media_galleries/media_galleries_preferences.h"
8 #include "chrome/grit/generated_resources.h"
9 #include "grit/theme_resources.h"
10 #include "third_party/skia/include/core/SkColor.h"
11 #include "ui/base/l10n/l10n_util.h"
12 #include "ui/base/resource/resource_bundle.h"
13 #include "ui/gfx/geometry/rect.h"
14 #include "ui/views/border.h"
15 #include "ui/views/context_menu_controller.h"
16 #include "ui/views/controls/button/button.h"
17 #include "ui/views/controls/button/checkbox.h"
18 #include "ui/views/controls/button/image_button.h"
19 #include "ui/views/controls/label.h"
20 #include "ui/views/layout/box_layout.h"
21 #include "ui/views/layout/layout_constants.h"
23 namespace {
25 // Equal to the #9F9F9F color used in spec (note WebUI color is #999).
26 const SkColor kDeemphasizedTextColor = SkColorSetRGB(159, 159, 159);
28 } // namespace
30 MediaGalleryCheckboxView::MediaGalleryCheckboxView(
31 const MediaGalleryPrefInfo& pref_info,
32 bool show_folder_button,
33 int trailing_vertical_space,
34 views::ButtonListener* button_listener,
35 views::ContextMenuController* menu_controller) {
36 DCHECK(button_listener != NULL);
37 SetLayoutManager(
38 new views::BoxLayout(views::BoxLayout::kHorizontal, 0, 0, 0));
39 SetBorder(views::Border::CreateEmptyBorder(
40 0, views::kPanelHorizMargin, trailing_vertical_space,
41 views::kPanelHorizMargin));
42 if (menu_controller)
43 set_context_menu_controller(menu_controller);
45 checkbox_ = new views::Checkbox(pref_info.GetGalleryDisplayName());
46 checkbox_->set_listener(button_listener);
47 if (menu_controller)
48 checkbox_->set_context_menu_controller(menu_controller);
49 checkbox_->SetElideBehavior(gfx::ELIDE_MIDDLE);
50 base::string16 tooltip_text = pref_info.GetGalleryTooltip();
51 checkbox_->SetTooltipText(tooltip_text);
53 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
54 folder_viewer_button_ = new views::ImageButton(button_listener);
55 if (menu_controller)
56 folder_viewer_button_->set_context_menu_controller(menu_controller);
57 folder_viewer_button_->SetImage(views::ImageButton::STATE_NORMAL,
58 rb.GetImageSkiaNamed(IDR_FILE_FOLDER));
59 folder_viewer_button_->SetImageAlignment(views::ImageButton::ALIGN_CENTER,
60 views::ImageButton::ALIGN_MIDDLE);
61 folder_viewer_button_->SetAccessibleName(l10n_util::GetStringUTF16(
62 IDS_MEDIA_GALLERIES_SCAN_RESULT_OPEN_FOLDER_VIEW_ACCESSIBILITY_NAME));
63 folder_viewer_button_->SetFocusable(true);
64 folder_viewer_button_->SetVisible(show_folder_button);
65 folder_viewer_button_->SetBorder(views::Border::CreateEmptyBorder(
66 0, views::kRelatedControlSmallHorizontalSpacing, 0, 0));
68 base::string16 details = pref_info.GetGalleryAdditionalDetails();
69 secondary_text_ = new views::Label(details);
70 if (menu_controller)
71 secondary_text_->set_context_menu_controller(menu_controller);
72 secondary_text_->SetVisible(details.length() > 0);
73 secondary_text_->SetEnabledColor(kDeemphasizedTextColor);
74 secondary_text_->SetElideBehavior(gfx::ELIDE_HEAD);
75 secondary_text_->SetTooltipText(tooltip_text);
76 secondary_text_->SetBorder(views::Border::CreateEmptyBorder(
77 0, views::kRelatedControlSmallHorizontalSpacing, 0, 0));
79 AddChildView(checkbox_);
80 AddChildView(folder_viewer_button_);
81 AddChildView(secondary_text_);
84 MediaGalleryCheckboxView::~MediaGalleryCheckboxView() {}
86 void MediaGalleryCheckboxView::Layout() {
87 views::View::Layout();
88 if (GetPreferredSize().width() <= GetLocalBounds().width())
89 return;
91 // If box layout doesn't fit, do custom layout. The folder_viewer_button and
92 // the secondary text should take up at most half of the space and the
93 // checkbox can take up what ever is left.
94 int checkbox_width = checkbox_->GetPreferredSize().width();
95 int folder_viewer_width = folder_viewer_button_->GetPreferredSize().width();
96 int secondary_text_width = secondary_text_->GetPreferredSize().width();
97 if (!folder_viewer_button_->visible())
98 folder_viewer_width = 0;
99 if (!secondary_text_->visible())
100 secondary_text_width = 0;
102 gfx::Rect area(GetLocalBounds());
103 area.Inset(GetInsets());
105 if (folder_viewer_width + secondary_text_width > area.width() / 2) {
106 secondary_text_width =
107 std::max(area.width() / 2 - folder_viewer_width,
108 area.width() - folder_viewer_width - checkbox_width);
110 checkbox_width = area.width() - folder_viewer_width - secondary_text_width;
112 checkbox_->SetBounds(area.x(), area.y(), checkbox_width, area.height());
113 if (folder_viewer_button_->visible()) {
114 folder_viewer_button_->SetBounds(checkbox_->x() + checkbox_width, area.y(),
115 folder_viewer_width, area.height());
117 if (secondary_text_->visible()) {
118 secondary_text_->SetBounds(
119 checkbox_->x() + checkbox_width + folder_viewer_width,
120 area.y(), secondary_text_width, area.height());