0

Desk Template: Adding dialog for template name duplication.

Add dialog to user name duplication and replace old template.

Test: Unit Test
Bug: 1281384
Change-Id: Ibbc8650f75e9535fcf768d7b551fc31348768995
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3393966
Reviewed-by: Sammie Quon <sammiequon@chromium.org>
Commit-Queue: Judy Wang <shidi@chromium.org>
Cr-Commit-Position: refs/heads/main@{#962159}
This commit is contained in:
Judy Wang
2022-01-21 23:59:17 +00:00
committed by Chromium LUCI CQ
parent c676b9355a
commit 1a0d116691
5 changed files with 115 additions and 30 deletions

@ -190,20 +190,22 @@ void DesksTemplatesDialogController::ShowUnsupportedAppsDialog(
void DesksTemplatesDialogController::ShowReplaceDialog(
aura::Window* root_window,
const std::u16string& template_name) {
auto dialog =
views::Builder<DesksTemplatesDialog>()
.SetTitleText(IDS_ASH_DESKS_TEMPLATES_REPLACE_DIALOG_TITLE)
.SetConfirmButtonText(
IDS_ASH_DESKS_TEMPLATES_REPLACE_DIALOG_CONFIRM_BUTTON)
.SetDescriptionText(l10n_util::GetStringFUTF16(
IDS_ASH_DESKS_TEMPLATES_REPLACE_DIALOG_DESCRIPTION,
GetStringWithQuotes(template_name)))
.SetDescriptionAccessibleName(l10n_util::GetStringFUTF16(
IDS_ASH_DESKS_TEMPLATES_REPLACE_DIALOG_DESCRIPTION,
template_name))
.SetAcceptCallback(base::BindOnce(&RecordReplaceTemplateHistogram))
.Build();
const std::u16string& template_name,
base::OnceClosure on_accept_callback,
base::OnceClosure on_cancel_callback) {
auto dialog = views::Builder<DesksTemplatesDialog>()
.SetTitleText(IDS_ASH_DESKS_TEMPLATES_REPLACE_DIALOG_TITLE)
.SetConfirmButtonText(
IDS_ASH_DESKS_TEMPLATES_REPLACE_DIALOG_CONFIRM_BUTTON)
.SetDescriptionText(l10n_util::GetStringFUTF16(
IDS_ASH_DESKS_TEMPLATES_REPLACE_DIALOG_DESCRIPTION,
GetStringWithQuotes(template_name)))
.SetDescriptionAccessibleName(l10n_util::GetStringFUTF16(
IDS_ASH_DESKS_TEMPLATES_REPLACE_DIALOG_DESCRIPTION,
template_name))
.SetAcceptCallback(std::move(on_accept_callback))
.SetCancelCallback(std::move(on_cancel_callback))
.Build();
CreateDialogWidget(std::move(dialog), root_window);
}

@ -51,7 +51,9 @@ class ASH_EXPORT DesksTemplatesDialogController : public views::WidgetObserver {
DesksController::GetDeskTemplateCallback callback,
std::unique_ptr<DeskTemplate> desk_template);
void ShowReplaceDialog(aura::Window* root_window,
const std::u16string& template_name);
const std::u16string& template_name,
base::OnceClosure on_accept_callback,
base::OnceClosure on_cancel_callback);
// Show the delete template dialog when user press the delete button.
// The `template_name` shows the name of the template which will be deleted in
// the dialog description.

@ -16,11 +16,14 @@
#include "ash/style/style_util.h"
#include "ash/wm/desks/desks_textfield.h"
#include "ash/wm/desks/templates/desks_templates_dialog_controller.h"
#include "ash/wm/desks/templates/desks_templates_grid_view.h"
#include "ash/wm/desks/templates/desks_templates_icon_container.h"
#include "ash/wm/desks/templates/desks_templates_metrics_util.h"
#include "ash/wm/desks/templates/desks_templates_name_view.h"
#include "ash/wm/desks/templates/desks_templates_presenter.h"
#include "ash/wm/overview/overview_constants.h"
#include "ash/wm/overview/overview_controller.h"
#include "ash/wm/overview/overview_grid.h"
#include "ash/wm/overview/overview_highlight_controller.h"
#include "ash/wm/overview/overview_session.h"
#include "base/strings/utf_string_conversions.h"
@ -218,6 +221,22 @@ bool DesksTemplatesItemView::IsTemplateNameBeingModified() const {
return name_view_->HasFocus();
}
void DesksTemplatesItemView::ReplaceTemplate(const std::string& uuid,
const std::u16string& new_name) {
UpdateTemplateName();
DesksTemplatesPresenter::Get()->DeleteEntry(uuid);
RecordReplaceTemplateHistogram();
}
void DesksTemplatesItemView::RevertTemplateName() {
views::FocusManager* focus_manager = GetFocusManager();
focus_manager->SetFocusedView(name_view_);
name_view_->SetText(desk_template_->template_name());
name_view_->SelectAll(true);
name_view_->OnContentsChanged();
}
void DesksTemplatesItemView::Layout() {
const int previous_name_view_width = name_view_->width();
@ -335,6 +354,47 @@ void DesksTemplatesItemView::OnViewBlurred(views::View* observed_view) {
return;
}
// Check if template name exist, replace existing template if confirmed by
// user.
aura::Window* root_window = GetWidget()->GetNativeWindow()->GetRootWindow();
OverviewGrid* overview_grid = Shell::Get()
->overview_controller()
->overview_session()
->GetGridWithRootWindow(root_window);
auto* templates_grid_view = static_cast<DesksTemplatesGridView*>(
overview_grid->desks_templates_grid_widget()->GetContentsView());
for (DesksTemplatesItemView* template_item :
templates_grid_view->grid_items()) {
auto new_name = name_view_->GetText();
if (template_item != this &&
template_item->desk_template_->template_name() == new_name) {
// Show replace template dialog.
// If accepted, replace old template and commit name change.
DesksTemplatesDialogController::Get()->ShowReplaceDialog(
root_window, new_name,
base::BindOnce(
&DesksTemplatesItemView::ReplaceTemplate, base::Unretained(this),
template_item->desk_template_->uuid().AsLowercaseString(),
new_name),
base::BindOnce(&DesksTemplatesItemView::RevertTemplateName,
base::Unretained(this)));
return;
}
}
UpdateTemplateName();
}
views::Button::KeyClickAction DesksTemplatesItemView::GetKeyClickActionForEvent(
const ui::KeyEvent& event) {
// Prevents any key events from activating a button click while the template
// name is being modified.
if (is_template_name_being_modified_)
return KeyClickAction::kNone;
return Button::GetKeyClickActionForEvent(event);
}
void DesksTemplatesItemView::UpdateTemplateName() {
auto updated_template = desk_template_->Clone();
updated_template->set_template_name(name_view_->GetText());
OnTemplateNameChanged(updated_template->template_name());
@ -353,16 +413,6 @@ void DesksTemplatesItemView::OnViewBlurred(views::View* observed_view) {
std::move(updated_template)));
}
views::Button::KeyClickAction DesksTemplatesItemView::GetKeyClickActionForEvent(
const ui::KeyEvent& event) {
// Prevents any key events from activating a button click while the template
// name is being modified.
if (is_template_name_being_modified_)
return KeyClickAction::kNone;
return Button::GetKeyClickActionForEvent(event);
}
void DesksTemplatesItemView::ContentsChanged(
views::Textfield* sender,
const std::u16string& new_contents) {

@ -52,6 +52,11 @@ class ASH_EXPORT DesksTemplatesItemView : public views::Button,
// `DesksTemplatesNameView` has the focus).
bool IsTemplateNameBeingModified() const;
// Rename current template with new name, delete old template with same name
// by uuid. Used for callback functions for Replace Dialog.
void ReplaceTemplate(const std::string& uuid, const std::u16string& new_name);
void RevertTemplateName();
// views::Button:
void Layout() override;
void OnThemeChanged() override;
@ -87,6 +92,9 @@ class ASH_EXPORT DesksTemplatesItemView : public views::Button,
// changes.
void OnTemplateNameChanged(const std::u16string& new_name);
// Update template name based on `name_view_` string.
void UpdateTemplateName();
// OverviewHighlightableView:
views::View* GetView() override;
void MaybeActivateHighlightedView() override;

@ -554,7 +554,8 @@ TEST_F(DesksTemplatesTest, DialogSystemModal) {
// Show one of the dialogs. Activating the dialog keeps us in overview mode.
auto* dialog_controller = DesksTemplatesDialogController::Get();
dialog_controller->ShowReplaceDialog(Shell::GetPrimaryRootWindow(), u"Bento");
dialog_controller->ShowReplaceDialog(Shell::GetPrimaryRootWindow(), u"Bento",
base::DoNothing(), base::DoNothing());
EXPECT_TRUE(Shell::IsSystemModalWindowOpen());
ASSERT_TRUE(GetOverviewSession());
@ -2113,17 +2114,31 @@ TEST_F(DesksTemplatesTest, LayoutItemsInPortrait) {
}
// Tests record metrics when current template being replaced.
TEST_F(DesksTemplatesTest, ReplaceTemplateRecordMetrics) {
TEST_F(DesksTemplatesTest, ReplaceTemplate) {
base::HistogramTester histogram_tester;
UpdateDisplay("800x600,800x600");
ToggleOverview();
ASSERT_TRUE(GetOverviewSession());
const base::GUID uuid_1 = base::GUID::GenerateRandomV4();
const std::u16string name_1 = u"template_1";
AddEntry(uuid_1, "template_1", base::Time::Now());
const base::GUID uuid_2 = base::GUID::GenerateRandomV4();
const std::u16string name_2 = u"template_2";
AddEntry(uuid_2, "template_2", base::Time::Now());
OpenOverviewAndShowTemplatesGrid();
DesksTemplatesItemView* item_view = GetItemViewFromTemplatesGrid(
/*grid_item_index=*/0);
// Show replace dialogs.
auto* dialog_controller = DesksTemplatesDialogController::Get();
dialog_controller->ShowReplaceDialog(Shell::GetPrimaryRootWindow(), u"Bento");
auto callback = base::BindLambdaForTesting([&]() {
item_view->ReplaceTemplate(uuid_1.AsLowercaseString(), name_1);
});
dialog_controller->ShowReplaceDialog(Shell::GetPrimaryRootWindow(), u"Bento",
callback, base::DoNothing());
EXPECT_TRUE(Shell::IsSystemModalWindowOpen());
ASSERT_TRUE(GetOverviewSession());
@ -2133,6 +2148,14 @@ TEST_F(DesksTemplatesTest, ReplaceTemplateRecordMetrics) {
->AsDialogDelegate()
->AcceptDialog();
// Only one template left.
EXPECT_EQ(1ul, desk_model()->GetEntryCount());
// The Template has been replaced.
DesksTemplatesNameView* name_view =
GetItemViewFromTemplatesGrid(0)->name_view();
EXPECT_EQ(name_1, name_view->GetText());
std::vector<DeskTemplate*> entries = GetAllEntries();
EXPECT_EQ(uuid_2, entries[0]->uuid());
// Assert metrics being recorded.
histogram_tester.ExpectTotalCount(kReplaceTemplateHistogramName, 1);