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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
|
// Copyright 2023 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/web_applications/sub_apps_install_dialog_controller.h"
#include <memory>
#include <string>
#include <vector>
#include "base/auto_reset.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/i18n/message_formatter.h"
#include "base/strings/utf_string_conversions.h"
#include "base/types/cxx23_to_underlying.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/web_applications/sub_apps_install_dialog_controller.h"
#include "chrome/browser/ui/web_applications/web_app_dialogs.h"
#include "chrome/browser/ui/web_applications/web_app_info_image_source.h"
#include "chrome/browser/ui/web_applications/web_app_ui_utils.h"
#include "chrome/browser/web_applications/web_app_install_info.h"
#include "chrome/grit/generated_resources.h"
#include "components/constrained_window/constrained_window_views.h"
#include "components/omnibox/browser/vector_icons.h"
#include "components/webapps/common/web_app_id.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/mojom/ui_base_types.mojom-shared.h"
#include "ui/gfx/geometry/insets.h"
#include "ui/gfx/image/image_skia.h"
#include "ui/views/bubble/bubble_dialog_model_host.h"
#include "ui/views/controls/image_view.h"
#include "ui/views/controls/label.h"
#include "ui/views/controls/scroll_view.h"
#include "ui/views/controls/styled_label.h"
#include "ui/views/layout/box_layout_view.h"
#include "ui/views/layout/layout_provider.h"
#include "ui/views/widget/widget.h"
namespace web_app {
namespace {
std::optional<SubAppsInstallDialogController::DialogActionForTesting>
g_dialog_override_for_testing;
constexpr int kSubAppIconSize = 32;
ui::ImageModel GetInstallAppIcon() {
return ui::ImageModel::FromVectorIcon(
omnibox::kInstallDesktopIcon, ui::kColorIcon,
views::LayoutProvider::Get()->GetDistanceMetric(
views::DISTANCE_BUBBLE_HEADER_VECTOR_ICON_SIZE));
}
std::u16string DialogTitle(int num_sub_apps) {
return base::i18n::MessageFormatter::FormatWithNamedArgs(
l10n_util::GetStringUTF16(IDS_SUB_APPS_INSTALL_DIALOG_TITLE),
"NUM_SUB_APP_INSTALLS", num_sub_apps);
}
ui::DialogModelLabel DialogDescription(int num_sub_apps,
std::u16string parent_app_name) {
std::u16string description =
base::i18n::MessageFormatter::FormatWithNamedArgs(
l10n_util::GetStringUTF16(IDS_SUB_APPS_INSTALL_DIALOG_DESCRIPTION),
/*name0=*/"NUM_SUB_APP_INSTALLS", num_sub_apps,
/*name1=*/"APP_NAME", parent_app_name);
ui::DialogModelLabel label = ui::DialogModelLabel(description);
label.set_is_secondary().set_allow_character_break();
return label;
}
std::unique_ptr<views::BubbleDialogModelHost::CustomView>
PermissionsExplanation(int num_sub_apps,
std::u16string parent_app_name,
base::RepeatingClosure settings_page_callback) {
std::u16string explanation_string = l10n_util::GetPluralStringFUTF16(
IDS_SUB_APPS_INSTALL_DIALOG_PERMISSIONS_DESCRIPTION, num_sub_apps);
const std::u16string manage_permissions_link_string =
l10n_util::GetStringUTF16(
IDS_SUB_APPS_INSTALL_DIALOG_MANAGE_PERMISSIONS_LINK);
std::vector<size_t> offsets;
const std::u16string formatted_string = base::ReplaceStringPlaceholders(
explanation_string, {parent_app_name, manage_permissions_link_string},
&offsets);
auto label = std::make_unique<views::StyledLabel>();
label->SetText(formatted_string);
label->SetDefaultTextStyle(views::style::STYLE_SECONDARY);
label->SetID(base::to_underlying(
SubAppsInstallDialogController::SubAppsInstallDialogViewID::
MANAGE_PERMISSIONS_LINK));
// Styles the "Manage" part of the string as a link and binds the callback
// (that opens the parent app's settings page) as the link's action
label->AddStyleRange(
gfx::Range(offsets.back(),
offsets.back() + manage_permissions_link_string.length()),
views::StyledLabel::RangeStyleInfo::CreateForLink(
std::move(settings_page_callback)));
return std::make_unique<views::BubbleDialogModelHost::CustomView>(
std::move(label), views::BubbleDialogModelHost::FieldType::kText);
}
std::u16string AcceptButtonLabel() {
return l10n_util::GetStringUTF16(
IDS_SUB_APPS_INSTALL_DIALOG_PERMISSIONS_BUTTON);
}
std::u16string CancelButtonLabel() {
return l10n_util::GetStringUTF16(IDS_SUB_APPS_INSTALL_DIALOG_CANCEL_BUTTON);
}
std::unique_ptr<views::ScrollView> CreateSubAppsListView(
const std::vector<std::unique_ptr<WebAppInstallInfo>>& sub_apps) {
// Create vertically scrollable area to contain the list of sub apps.
auto scroll_view = std::make_unique<views::ScrollView>();
scroll_view->SetHorizontalScrollBarMode(
views::ScrollView::ScrollBarMode::kDisabled);
const auto* const layout_provider = views::LayoutProvider::Get();
scroll_view->ClipHeightTo(
0, layout_provider->GetDistanceMetric(
views::DISTANCE_DIALOG_SCROLLABLE_AREA_MAX_HEIGHT));
// Set the content for the scrollable area to a sensibly layout view.
auto* sub_app_list =
scroll_view->SetContents(std::make_unique<views::BoxLayoutView>());
sub_app_list->SetOrientation(views::BoxLayout::Orientation::kVertical);
sub_app_list->SetBetweenChildSpacing(layout_provider->GetDistanceMetric(
views::DISTANCE_CONTROL_LIST_VERTICAL));
sub_app_list->SetInsideBorderInsets(
gfx::Insets().set_left(layout_provider->GetDistanceMetric(
views::DISTANCE_UNRELATED_CONTROL_HORIZONTAL)));
// Add a box view for each sub app containing the app's icon and title.
for (const std::unique_ptr<WebAppInstallInfo>& sub_app : sub_apps) {
auto* box =
sub_app_list->AddChildView(std::make_unique<views::BoxLayoutView>());
box->SetOrientation(views::BoxLayout::Orientation::kHorizontal);
box->SetBetweenChildSpacing(layout_provider->GetDistanceMetric(
views::DISTANCE_RELATED_LABEL_HORIZONTAL));
auto* sub_app_icon =
box->AddChildView(std::make_unique<views::ImageView>());
sub_app_icon->SetImage(ui::ImageModel::FromImageSkia(
gfx::ImageSkia(std::make_unique<WebAppInfoImageSource>(
kSubAppIconSize, sub_app->icon_bitmaps.any),
gfx::Size(kSubAppIconSize, kSubAppIconSize))));
sub_app_icon->SetGroup(
base::to_underlying(SubAppsInstallDialogController::
SubAppsInstallDialogViewID::SUB_APP_ICON));
auto* sub_app_label =
box->AddChildView(std::make_unique<views::Label>(sub_app->title));
sub_app_label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
sub_app_label->SetMultiLine(true);
sub_app_label->SetGroup(
base::to_underlying(SubAppsInstallDialogController::
SubAppsInstallDialogViewID::SUB_APP_LABEL));
}
return scroll_view;
}
} // namespace
// static
[[nodiscard]] base::AutoReset<
std::optional<SubAppsInstallDialogController::DialogActionForTesting>>
SubAppsInstallDialogController::SetAutomaticActionForTesting(
DialogActionForTesting auto_accept) {
return base::AutoReset<std::optional<DialogActionForTesting>>(
&g_dialog_override_for_testing, auto_accept);
}
SubAppsInstallDialogController::SubAppsInstallDialogController() = default;
SubAppsInstallDialogController::~SubAppsInstallDialogController() {
if (widget_) {
widget_observation_.Reset();
widget_->CloseWithReason(views::Widget::ClosedReason::kUnspecified);
}
}
void SubAppsInstallDialogController::Init(
base::OnceCallback<void(bool)> callback,
const std::vector<std::unique_ptr<WebAppInstallInfo>>& sub_apps,
const std::string& parent_app_name,
const webapps::AppId& parent_app_id,
Profile* profile,
gfx::NativeWindow window) {
if (g_dialog_override_for_testing) {
switch (g_dialog_override_for_testing.value()) {
case DialogActionForTesting::kAccept:
std::move(callback).Run(true);
break;
case DialogActionForTesting::kCancel:
std::move(callback).Run(false);
break;
}
return;
}
callback_ = std::move(callback);
widget_ = CreateWidget(
base::UTF8ToUTF16(parent_app_name), sub_apps,
base::BindRepeating(OpenAppSettingsForParentApp, parent_app_id, profile),
window);
widget_observation_.Observe(widget_);
widget_->Show();
}
views::Widget* SubAppsInstallDialogController::CreateWidget(
const std::u16string parent_app_name,
const std::vector<std::unique_ptr<web_app::WebAppInstallInfo>>& sub_apps,
base::RepeatingClosure settings_page_callback,
gfx::NativeWindow window) {
int num_sub_apps = sub_apps.size();
std::unique_ptr<ui::DialogModel> dialog_model =
ui::DialogModel::Builder()
.SetInternalName("SubAppsInstallDialogController")
.SetIcon(GetInstallAppIcon())
.SetTitle(DialogTitle(num_sub_apps))
.AddParagraph(DialogDescription(num_sub_apps, parent_app_name))
.AddCustomField(
std::make_unique<views::BubbleDialogModelHost::CustomView>(
CreateSubAppsListView(sub_apps),
views::BubbleDialogModelHost::FieldType::kMenuItem))
.AddCustomField(
PermissionsExplanation(sub_apps.size(), parent_app_name,
std::move(settings_page_callback)))
.AddOkButton(
base::DoNothing(),
ui::DialogModel::Button::Params().SetLabel(AcceptButtonLabel()))
.AddCancelButton(
base::DoNothing(),
ui::DialogModel::Button::Params().SetLabel(CancelButtonLabel()))
.OverrideShowCloseButton(false)
.Build();
auto dialog = views::BubbleDialogModelHost::CreateModal(
std::move(dialog_model), ui::mojom::ModalType::kWindow);
dialog->SetOwnedByWidget(views::WidgetDelegate::OwnedByWidgetPassKey());
views::Widget* widget = constrained_window::CreateBrowserModalDialogViews(
std::move(dialog), window);
return widget;
}
views::Widget* SubAppsInstallDialogController::GetWidgetForTesting() {
return widget_;
}
void SubAppsInstallDialogController::OnWidgetDestroying(views::Widget* widget) {
widget_observation_.Reset();
widget_ = nullptr;
switch (widget->closed_reason()) {
case views::Widget::ClosedReason::kAcceptButtonClicked:
std::move(callback_).Run(true);
break;
case views::Widget::ClosedReason::kUnspecified:
case views::Widget::ClosedReason::kEscKeyPressed:
case views::Widget::ClosedReason::kCloseButtonClicked:
case views::Widget::ClosedReason::kLostFocus:
case views::Widget::ClosedReason::kCancelButtonClicked:
std::move(callback_).Run(false);
break;
}
}
} // namespace web_app
|