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.
4
5#include "chrome/browser/ui/views/extensions/media_gallery_checkbox_view.h"
6
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/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"
22
23namespace {
24
25// Equal to the #9F9F9F color used in spec (note WebUI color is #999).
26const SkColor kDeemphasizedTextColor = SkColorSetRGB(159, 159, 159);
27
28}  // namespace
29
30MediaGalleryCheckboxView::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);
44
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);
52
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));
67
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));
78
79  AddChildView(checkbox_);
80  AddChildView(folder_viewer_button_);
81  AddChildView(secondary_text_);
82}
83
84MediaGalleryCheckboxView::~MediaGalleryCheckboxView() {}
85
86void MediaGalleryCheckboxView::Layout() {
87  views::View::Layout();
88  if (GetPreferredSize().width() <= GetLocalBounds().width())
89    return;
90
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;
101
102  gfx::Rect area(GetLocalBounds());
103  area.Inset(GetInsets());
104
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);
109  }
110  checkbox_width = area.width() - folder_viewer_width - secondary_text_width;
111
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());
116  }
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());
121  }
122}
123