| // Copyright 2020 The Chromium Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| #include "ash/system/accessibility/switch_access_menu_button.h" |
| |
| #include "ash/style/ash_color_provider.h" |
| #include "base/bind.h" |
| #include "ui/accessibility/ax_node_data.h" |
| #include "ui/accessibility/mojom/ax_node_data.mojom-shared.h" |
| #include "ui/base/l10n/l10n_util.h" |
| #include "ui/gfx/canvas.h" |
| #include "ui/gfx/paint_vector_icon.h" |
| #include "ui/gfx/vector_icon_types.h" |
| #include "ui/views/accessibility/view_accessibility.h" |
| #include "ui/views/controls/image_view.h" |
| #include "ui/views/controls/label.h" |
| #include "ui/views/layout/box_layout.h" |
| #include "ui/views/metadata/metadata_impl_macros.h" |
| #include "ui/views/style/typography.h" |
| |
| namespace ash { |
| |
| namespace { |
| constexpr int kButtonBottomPaddingDefaultDip = 8; |
| constexpr int kButtonBottomPaddingSmallDip = 1; |
| constexpr int kButtonTopPaddingDip = 16; |
| constexpr int kIconSizeDip = 20; |
| constexpr int kLabelMinSidePaddingDip = 8; |
| constexpr int kLabelMaxWidthDip = |
| SwitchAccessMenuButton::kWidthDip - 2 * kLabelMinSidePaddingDip; |
| constexpr int kLabelTopPaddingDefaultDip = 16; |
| constexpr int kLabelTopPaddingSmallDip = 8; |
| constexpr int kTextLineHeightDip = 20; |
| } // namespace |
| |
| SwitchAccessMenuButton::SwitchAccessMenuButton(std::string action_name, |
| const gfx::VectorIcon& icon, |
| int label_text_id) |
| : views::Button( |
| base::BindRepeating(&SwitchAccessMenuButton::OnButtonPressed, |
| base::Unretained(this))), |
| action_name_(action_name) { |
| SkColor icon_color = AshColorProvider::Get()->GetContentLayerColor( |
| AshColorProvider::ContentLayerType::kIconColorPrimary); |
| SkColor label_color = AshColorProvider::Get()->GetContentLayerColor( |
| AshColorProvider::ContentLayerType::kTextColorPrimary); |
| |
| std::u16string label_text = l10n_util::GetStringUTF16(label_text_id); |
| views::Builder<SwitchAccessMenuButton>(this) |
| .SetFocusBehavior(views::View::FocusBehavior::ACCESSIBLE_ONLY) |
| .AddChildren( |
| {views::Builder<views::ImageView>() |
| .CopyAddressTo(&image_view_) |
| .SetImage(gfx::CreateVectorIcon(icon, kIconSizeDip, icon_color)), |
| views::Builder<views::Label>() |
| .CopyAddressTo(&label_) |
| .SetText(label_text) |
| .SetTextContext(views::style::CONTEXT_BUTTON) |
| .SetAutoColorReadabilityEnabled(false) |
| .SetEnabledColor(label_color) |
| .SetMultiLine(true) |
| .SetMaximumWidth(kLabelMaxWidthDip)}) |
| .BuildChildren(); |
| |
| std::unique_ptr<views::BoxLayout> layout = std::make_unique<views::BoxLayout>( |
| views::BoxLayout::Orientation::kVertical, |
| gfx::Insets(kButtonTopPaddingDip, kLabelMinSidePaddingDip, |
| kButtonBottomPaddingDefaultDip, kLabelMinSidePaddingDip), |
| kLabelTopPaddingDefaultDip); |
| |
| // The layout padding changes with the size of the text label. |
| gfx::Size label_size = label_->CalculatePreferredSize(); |
| int left_padding_dip = (kWidthDip - label_size.width()) / 2; |
| int right_padding_dip = kWidthDip - left_padding_dip - label_size.width(); |
| int bottom_padding_dip = kButtonBottomPaddingDefaultDip; |
| if (label_size.height() > kTextLineHeightDip) { |
| bottom_padding_dip = kButtonBottomPaddingSmallDip; |
| layout->set_between_child_spacing(kLabelTopPaddingSmallDip); |
| } |
| layout->set_inside_border_insets( |
| gfx::Insets(kButtonTopPaddingDip, left_padding_dip, bottom_padding_dip, |
| right_padding_dip)); |
| SetLayoutManager(std::move(layout)); |
| |
| GetViewAccessibility().OverrideName(label_text); |
| GetViewAccessibility().OverrideIsLeaf(true); |
| } |
| |
| void SwitchAccessMenuButton::GetAccessibleNodeData(ui::AXNodeData* node_data) { |
| views::Button::GetAccessibleNodeData(node_data); |
| node_data->AddStringAttribute(ax::mojom::StringAttribute::kValue, |
| action_name_); |
| } |
| |
| void SwitchAccessMenuButton::OnButtonPressed() { |
| NotifyAccessibilityEvent(ax::mojom::Event::kClicked, |
| /*send_native_event=*/false); |
| } |
| |
| BEGIN_METADATA(SwitchAccessMenuButton, views::Button) |
| END_METADATA |
| |
| } // namespace ash |