1a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved.
2a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// found in the LICENSE file.
4a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
5a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "chrome/browser/ui/views/extensions/media_gallery_checkbox_view.h"
6a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
7f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#include "chrome/browser/media_galleries/media_galleries_preferences.h"
81320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "chrome/grit/generated_resources.h"
9a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "grit/theme_resources.h"
10a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "third_party/skia/include/core/SkColor.h"
11a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "ui/base/l10n/l10n_util.h"
12a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "ui/base/resource/resource_bundle.h"
13a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "ui/gfx/rect.h"
14a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "ui/views/border.h"
15a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "ui/views/context_menu_controller.h"
16a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "ui/views/controls/button/button.h"
17a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "ui/views/controls/button/checkbox.h"
18a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "ui/views/controls/button/image_button.h"
19a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "ui/views/controls/label.h"
20a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "ui/views/layout/box_layout.h"
21a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "ui/views/layout/layout_constants.h"
22a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
23a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)namespace {
24a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
25a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// Equal to the #9F9F9F color used in spec (note WebUI color is #999).
26a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)const SkColor kDeemphasizedTextColor = SkColorSetRGB(159, 159, 159);
27a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
28a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}  // namespace
29a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
30a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)MediaGalleryCheckboxView::MediaGalleryCheckboxView(
31f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    const MediaGalleryPrefInfo& pref_info,
32a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    bool show_folder_button,
33a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    int trailing_vertical_space,
34a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    views::ButtonListener* button_listener,
35a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    views::ContextMenuController* menu_controller) {
36a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  DCHECK(button_listener != NULL);
37a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  SetLayoutManager(
38a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      new views::BoxLayout(views::BoxLayout::kHorizontal, 0, 0, 0));
39a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  SetBorder(views::Border::CreateEmptyBorder(
40a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      0, views::kPanelHorizMargin, trailing_vertical_space,
41a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      views::kPanelHorizMargin));
42a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (menu_controller)
43a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    set_context_menu_controller(menu_controller);
44a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
45f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  checkbox_ = new views::Checkbox(pref_info.GetGalleryDisplayName());
46a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  checkbox_->set_listener(button_listener);
47a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (menu_controller)
48a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    checkbox_->set_context_menu_controller(menu_controller);
4946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  checkbox_->SetElideBehavior(gfx::ELIDE_MIDDLE);
50f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  base::string16 tooltip_text = pref_info.GetGalleryTooltip();
51a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  checkbox_->SetTooltipText(tooltip_text);
52a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
53a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
54a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  folder_viewer_button_ = new views::ImageButton(button_listener);
55a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (menu_controller)
56a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    folder_viewer_button_->set_context_menu_controller(menu_controller);
57a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  folder_viewer_button_->SetImage(views::ImageButton::STATE_NORMAL,
58a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                  rb.GetImageSkiaNamed(IDR_FILE_FOLDER));
59a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  folder_viewer_button_->SetImageAlignment(views::ImageButton::ALIGN_CENTER,
60a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                           views::ImageButton::ALIGN_MIDDLE);
61a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  folder_viewer_button_->SetAccessibleName(l10n_util::GetStringUTF16(
62a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      IDS_MEDIA_GALLERIES_SCAN_RESULT_OPEN_FOLDER_VIEW_ACCESSIBILITY_NAME));
63a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  folder_viewer_button_->SetFocusable(true);
64a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  folder_viewer_button_->SetVisible(show_folder_button);
65a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  folder_viewer_button_->SetBorder(views::Border::CreateEmptyBorder(
66a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      0, views::kRelatedControlSmallHorizontalSpacing, 0, 0));
67a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
68f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  base::string16 details = pref_info.GetGalleryAdditionalDetails();
69a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  secondary_text_ = new views::Label(details);
70a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (menu_controller)
71a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    secondary_text_->set_context_menu_controller(menu_controller);
72a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  secondary_text_->SetVisible(details.length() > 0);
73a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  secondary_text_->SetEnabledColor(kDeemphasizedTextColor);
7446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  secondary_text_->SetElideBehavior(gfx::ELIDE_HEAD);
75a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  secondary_text_->SetTooltipText(tooltip_text);
76a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  secondary_text_->SetBorder(views::Border::CreateEmptyBorder(
77a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      0, views::kRelatedControlSmallHorizontalSpacing, 0, 0));
78a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
79a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  AddChildView(checkbox_);
80a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  AddChildView(folder_viewer_button_);
81a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  AddChildView(secondary_text_);
82a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
83a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
84a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)MediaGalleryCheckboxView::~MediaGalleryCheckboxView() {}
85a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
86a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void MediaGalleryCheckboxView::Layout() {
87a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  views::View::Layout();
88a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (GetPreferredSize().width() <= GetLocalBounds().width())
89a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    return;
90a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
91a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // If box layout doesn't fit, do custom layout. The folder_viewer_button and
92a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // the secondary text should take up at most half of the space and the
93a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // checkbox can take up what ever is left.
94a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  int checkbox_width = checkbox_->GetPreferredSize().width();
95a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  int folder_viewer_width = folder_viewer_button_->GetPreferredSize().width();
96a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  int secondary_text_width = secondary_text_->GetPreferredSize().width();
97a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (!folder_viewer_button_->visible())
98a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    folder_viewer_width = 0;
99a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (!secondary_text_->visible())
100a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    secondary_text_width = 0;
101a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
102a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  gfx::Rect area(GetLocalBounds());
103a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  area.Inset(GetInsets());
104a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
105a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (folder_viewer_width + secondary_text_width > area.width() / 2) {
106a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    secondary_text_width =
107a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        std::max(area.width() / 2 - folder_viewer_width,
108a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                 area.width() - folder_viewer_width - checkbox_width);
109a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
110a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  checkbox_width = area.width() - folder_viewer_width - secondary_text_width;
111a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
112a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  checkbox_->SetBounds(area.x(), area.y(), checkbox_width, area.height());
113a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (folder_viewer_button_->visible()) {
114a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    folder_viewer_button_->SetBounds(checkbox_->x() + checkbox_width, area.y(),
115a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                     folder_viewer_width, area.height());
116a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
117a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (secondary_text_->visible()) {
118a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    secondary_text_->SetBounds(
119a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        checkbox_->x() + checkbox_width + folder_viewer_width,
120a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        area.y(), secondary_text_width, area.height());
121a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
122a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
123