[go: nahoru, domu]

blob: d432c9f5027a4a5a1ab873a5283517bd20957f4b [file] [log] [blame]
Pu Shi5375b532022-11-17 19:34:391// Copyright 2022 The Chromium Authors
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 "ash/system/phonehub/app_stream_launcher_item.h"
6
Daniel Cheng667d1902023-12-01 23:16:327#include <utility>
8
Abbas Nayebib6ad3cc2022-12-12 22:04:259#include "ash/strings/grit/ash_strings.h"
Daniel Cheng667d1902023-12-01 23:16:3210#include "base/functional/callback.h"
Abbas Nayebib6ad3cc2022-12-12 22:04:2511#include "base/hash/hash.h"
Lei Zhangc5f765d52023-11-08 00:53:3312#include "base/strings/string_piece.h"
Abbas Nayebib6ad3cc2022-12-12 22:04:2513#include "base/strings/utf_string_conversions.h"
14#include "ui/base/l10n/l10n_util.h"
Allen Bauer912e8ff32023-11-08 21:13:1115#include "ui/base/metadata/metadata_header_macros.h"
16#include "ui/base/metadata/metadata_impl_macros.h"
Pu Shi5375b532022-11-17 19:34:3917#include "ui/gfx/geometry/insets.h"
Abbas Nayebib6ad3cc2022-12-12 22:04:2518#include "ui/gfx/image/image.h"
19#include "ui/gfx/image/image_skia_operations.h"
Pu Shi68350db2022-12-05 18:41:0320#include "ui/gfx/text_constants.h"
21#include "ui/views/border.h"
22#include "ui/views/controls/button/label_button.h"
Pu Shi5375b532022-11-17 19:34:3923#include "ui/views/controls/label.h"
24#include "ui/views/layout/box_layout.h"
25
26namespace ash {
27
28namespace {
29
Pu Shi97ad13a2022-11-30 20:08:2830constexpr int kEcheAppItemWidth = 50;
31constexpr int kEcheAppItemHeight = 60;
32constexpr gfx::Size kEcheAppItemSize(kEcheAppItemWidth, kEcheAppItemHeight);
33constexpr int kEcheAppItemSpacing = 4;
Pu Shi5375b532022-11-17 19:34:3934constexpr int kEcheAppNameLabelLineHeight = 14;
Pu Shi97ad13a2022-11-30 20:08:2835constexpr int kEcheAppNameLabelFontSize = 11;
Crisrael Lucero45d67792023-05-30 20:57:5636constexpr double kAlphaValueForInhibitedIconOpacity = 0.38;
Pu Shi5375b532022-11-17 19:34:3937
38void ConfigureLabel(views::Label* label, int line_height, int font_size) {
Pu Shi5375b532022-11-17 19:34:3939 label->SetLineHeight(line_height);
Pu Shi97ad13a2022-11-30 20:08:2840 label->SetTruncateLength(kEcheAppItemWidth);
Pu Shi5375b532022-11-17 19:34:3941
42 gfx::Font default_font;
43 gfx::Font label_font =
44 default_font.Derive(font_size - default_font.GetFontSize(),
45 gfx::Font::NORMAL, gfx::Font::Weight::NORMAL);
46 gfx::FontList font_list(label_font);
47 label->SetFontList(font_list);
48}
49
Pu Shi68350db2022-12-05 18:41:0350class AppNameLabel : public views::LabelButton {
Allen Bauer912e8ff32023-11-08 21:13:1151 METADATA_HEADER(AppNameLabel, views::LabelButton)
52
Pu Shi68350db2022-12-05 18:41:0353 public:
54 explicit AppNameLabel(PressedCallback callback = PressedCallback(),
55 const std::u16string& text = std::u16string())
56 : LabelButton(std::move(callback), text) {
57 ConfigureLabel(label(), kEcheAppNameLabelLineHeight,
58 kEcheAppNameLabelFontSize);
59 SetBorder(views::CreateEmptyBorder(gfx::Insets()));
60 SetHorizontalAlignment(gfx::ALIGN_CENTER);
61 }
62
63 ~AppNameLabel() override = default;
64 AppNameLabel(AppNameLabel&) = delete;
65 AppNameLabel operator=(AppNameLabel&) = delete;
66};
67
Allen Bauer912e8ff32023-11-08 21:13:1168BEGIN_METADATA(AppNameLabel)
69END_METADATA
70
Pu Shi5375b532022-11-17 19:34:3971} // namespace
72
73AppStreamLauncherItem::AppStreamLauncherItem(
Daniel Cheng667d1902023-12-01 23:16:3274 base::RepeatingClosure callback,
Pu Shi5375b532022-11-17 19:34:3975 const phonehub::Notification::AppMetadata& app_metadata) {
76 SetPreferredSize(kEcheAppItemSize);
77 auto* layout = SetLayoutManager(std::make_unique<views::BoxLayout>(
78 views::BoxLayout::Orientation::kVertical, gfx::Insets(),
79 kEcheAppItemSpacing));
80 layout->set_cross_axis_alignment(
81 views::BoxLayout::CrossAxisAlignment::kCenter);
82
Abbas Nayebib6ad3cc2022-12-12 22:04:2583 const bool enabled = app_metadata.app_streamability_status ==
84 phonehub::proto::AppStreamabilityStatus::STREAMABLE;
Crisrael Lucero16f9e272023-04-12 22:21:1885 gfx::Image image = app_metadata.color_icon;
Abbas Nayebib6ad3cc2022-12-12 22:04:2586 if (!enabled) {
87 // Fade the image in order to make it look like grayed out.
Abbas Nayebib6ad3cc2022-12-12 22:04:2588 image = gfx::Image(gfx::ImageSkiaOperations::CreateTransparentImage(
89 image.AsImageSkia(), kAlphaValueForInhibitedIconOpacity));
90 }
91
92 std::u16string accessible_name;
93 switch (app_metadata.app_streamability_status) {
94 case phonehub::proto::STREAMABLE:
95 accessible_name = app_metadata.visible_app_name;
96 break;
97 case phonehub::proto::BLOCKED_BY_APP:
98 accessible_name = l10n_util::GetStringUTF16(
99 IDS_ASH_PHONE_HUB_STREAM_NOT_SUPPORTED_BY_APP);
100 break;
101 case phonehub::proto::BLOCK_LISTED:
102 default:
103 accessible_name =
104 l10n_util::GetStringUTF16(IDS_ASH_PHONE_HUB_STREAM_NOT_SUPPORTED);
105 break;
106 }
Pu Shi5375b532022-11-17 19:34:39107 recent_app_button_ = AddChildView(std::make_unique<PhoneHubRecentAppButton>(
Abbas Nayebib6ad3cc2022-12-12 22:04:25108 image, app_metadata.visible_app_name, callback));
109 recent_app_button_->SetAccessibleName(accessible_name);
110 recent_app_button_->SetTooltipText(accessible_name);
111 recent_app_button_->SetEnabled(enabled);
Pu Shi5375b532022-11-17 19:34:39112
Daniel Cheng667d1902023-12-01 23:16:32113 label_ = AddChildView(std::make_unique<AppNameLabel>(
114 std::move(callback), app_metadata.visible_app_name));
Abbas Nayebib6ad3cc2022-12-12 22:04:25115 label_->SetEnabled(enabled);
116 label_->SetAccessibleName(accessible_name);
117 label_->SetTooltipText(accessible_name);
Pu Shi5375b532022-11-17 19:34:39118}
119
120AppStreamLauncherItem::~AppStreamLauncherItem() = default;
121
122bool AppStreamLauncherItem::HasFocus() const {
123 return recent_app_button_->HasFocus() || label_->HasFocus();
124}
125
126void AppStreamLauncherItem::RequestFocus() {
127 recent_app_button_->RequestFocus();
128}
129
Pu Shi68350db2022-12-05 18:41:03130views::LabelButton* AppStreamLauncherItem::GetLabelForTest() {
Abbas Nayebi223ebe82022-11-29 20:29:34131 return label_;
132}
133PhoneHubRecentAppButton* AppStreamLauncherItem::GetIconForTest() {
134 return recent_app_button_;
135}
136
Allen Bauer912e8ff32023-11-08 21:13:11137BEGIN_METADATA(AppStreamLauncherItem)
138END_METADATA
139
Pu Shi5375b532022-11-17 19:34:39140} // namespace ash