File: media_gallery_checkbox_view.cc

package info (click to toggle)
chromium 139.0.7258.138-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,120,676 kB
  • sloc: cpp: 35,100,869; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (94 lines) | stat: -rw-r--r-- 3,809 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/views/extensions/media_gallery_checkbox_view.h"

#include "chrome/browser/media_galleries/media_galleries_preferences.h"
#include "chrome/browser/ui/views/chrome_layout_provider.h"
#include "third_party/skia/include/core/SkColor.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/gfx/geometry/insets.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/views/border.h"
#include "ui/views/context_menu_controller.h"
#include "ui/views/controls/button/button.h"
#include "ui/views/controls/button/checkbox.h"
#include "ui/views/controls/label.h"
#include "ui/views/layout/box_layout.h"
#include "ui/views/style/typography.h"

MediaGalleryCheckboxView::MediaGalleryCheckboxView(
    const MediaGalleryPrefInfo& pref_info,
    int trailing_vertical_space,
    views::ContextMenuController* menu_controller) {
  SetOrientation(views::BoxLayout::Orientation::kHorizontal);
  ChromeLayoutProvider* provider = ChromeLayoutProvider::Get();
  const gfx::Insets dialog_insets =
      provider->GetInsetsMetric(views::INSETS_DIALOG);
  SetBorder(views::CreateEmptyBorder(gfx::Insets::TLBR(0, dialog_insets.left(),
                                                       trailing_vertical_space,
                                                       dialog_insets.right())));
  if (menu_controller) {
    set_context_menu_controller(menu_controller);
  }

  checkbox_ = AddChildView(std::make_unique<views::Checkbox>(
      pref_info.GetGalleryDisplayName(), views::Button::PressedCallback()));
  if (menu_controller) {
    checkbox_->set_context_menu_controller(menu_controller);
  }
  checkbox_->SetElideBehavior(gfx::ELIDE_MIDDLE);
  std::u16string tooltip_text = pref_info.GetGalleryTooltip();
  checkbox_->SetTooltipText(tooltip_text);

  std::u16string details = pref_info.GetGalleryAdditionalDetails();
  secondary_text_ = AddChildView(std::make_unique<views::Label>(
      details, views::style::CONTEXT_LABEL, views::style::STYLE_SECONDARY));
  if (menu_controller) {
    secondary_text_->set_context_menu_controller(menu_controller);
  }
  secondary_text_->SetVisible(details.length() > 0);
  secondary_text_->SetElideBehavior(gfx::ELIDE_HEAD);
  secondary_text_->SetCustomTooltipText(tooltip_text);
  secondary_text_->SetBorder(views::CreateEmptyBorder(gfx::Insets::TLBR(
      0, provider->GetDistanceMetric(DISTANCE_RELATED_CONTROL_HORIZONTAL_SMALL),
      0, 0)));
}

MediaGalleryCheckboxView::~MediaGalleryCheckboxView() = default;

void MediaGalleryCheckboxView::Layout(PassKey) {
  LayoutSuperclass<views::BoxLayoutView>(this);
  if (GetPreferredSize().width() <= GetLocalBounds().width()) {
    return;
  }

  // If box layout doesn't fit, do custom layout. The secondary text should take
  // up at most half of the space and the checkbox can take up whatever is left.
  int checkbox_width = checkbox_->GetPreferredSize().width();
  int secondary_text_width =
      secondary_text_
          ->GetPreferredSize(views::SizeBounds(secondary_text_->width(), {}))
          .width();
  if (!secondary_text_->GetVisible()) {
    secondary_text_width = 0;
  }

  gfx::Rect area = GetContentsBounds();

  if (secondary_text_width > area.width() / 2) {
    secondary_text_width =
        std::max(area.width() / 2, area.width() - checkbox_width);
  }
  checkbox_width = area.width() - secondary_text_width;

  checkbox_->SetBounds(area.x(), area.y(), checkbox_width, area.height());
  if (secondary_text_->GetVisible()) {
    secondary_text_->SetBounds(checkbox_->x() + checkbox_width, area.y(),
                               secondary_text_width, area.height());
  }
}

BEGIN_METADATA(MediaGalleryCheckboxView)
END_METADATA