Skip to content

Commit

Permalink
Add initial separator support to DialogModel
Browse files Browse the repository at this point in the history
This is unpolished (separator should probably have some margin), but the
DialogModel use of it isn't shipped so it's effectively a no-op.

Bug: 1324298
Change-Id: I173d4a73315d1cedc534d232e5897b955acaffa0
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3639051
Reviewed-by: Elly Fong-Jones <ellyjones@chromium.org>
Auto-Submit: Peter Boström <pbos@chromium.org>
Commit-Queue: Peter Boström <pbos@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1002216}
  • Loading branch information
pbos authored and Chromium LUCI CQ committed May 11, 2022
1 parent 4b8e7b4 commit 82cc966
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ views::Widget* TabGroupEditorBubbleView::Show(
// picker.

dialog_builder.OverrideShowCloseButton(false)
.AddSeparator()
.AddCustomField(
std::make_unique<MenuItemFactory>(
l10n_util::GetStringUTF16(
Expand Down
6 changes: 6 additions & 0 deletions ui/base/models/dialog_model.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
// found in the LICENSE file.

#include "ui/base/models/dialog_model.h"
#include <memory>

#include "base/callback_helpers.h"
#include "base/ranges/algorithm.h"
#include "ui/base/models/dialog_model_field.h"

namespace ui {

Expand Down Expand Up @@ -102,6 +104,10 @@ void DialogModel::AddCombobox(std::u16string label,
GetPassKey(), this, std::move(label), std::move(combobox_model), params));
}

void DialogModel::AddSeparator() {
AddField(std::make_unique<DialogModelSeparator>(GetPassKey(), this));
}

void DialogModel::AddTextfield(std::u16string label,
std::u16string text,
const DialogModelTextfield::Params& params) {
Expand Down
9 changes: 9 additions & 0 deletions ui/base/models/dialog_model.h
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,12 @@ class COMPONENT_EXPORT(UI_BASE) DialogModel final {
return *this;
}

// Adds a separator. See DialogModel::AddSeparator().
Builder& AddSeparator() {
model_->AddSeparator();
return *this;
}

// Adds a textfield. See DialogModel::AddTextfield().
Builder& AddTextfield(std::u16string label,
std::u16string text,
Expand Down Expand Up @@ -272,6 +278,9 @@ class COMPONENT_EXPORT(UI_BASE) DialogModel final {
const DialogModelCombobox::Params& params =
DialogModelCombobox::Params());

// Adds a separator at the end of the dialog model.
void AddSeparator();

// Adds a labeled textfield (label: [text]) at the end of the dialog model.
void AddTextfield(std::u16string label,
std::u16string text,
Expand Down
25 changes: 9 additions & 16 deletions ui/base/models/dialog_model_field.cc
Original file line number Diff line number Diff line change
Expand Up @@ -166,12 +166,7 @@ void DialogModelButton::OnPressed(base::PassKey<DialogModelHost>,
DialogModelBodyText::DialogModelBodyText(base::PassKey<DialogModel> pass_key,
DialogModel* model,
const DialogModelLabel& label)
: DialogModelField(pass_key,
model,
kBodyText,
-1,
base::flat_set<Accelerator>()),
label_(label) {}
: DialogModelField(pass_key, model, kBodyText, -1, {}), label_(label) {}

DialogModelBodyText::~DialogModelBodyText() = default;

Expand All @@ -181,11 +176,7 @@ DialogModelCheckbox::DialogModelCheckbox(
int unique_id,
const DialogModelLabel& label,
const DialogModelCheckbox::Params& params)
: DialogModelField(pass_key,
model,
kCheckbox,
unique_id,
base::flat_set<Accelerator>()),
: DialogModelField(pass_key, model, kCheckbox, unique_id, {}),
label_(label),
is_checked_(params.is_checked_) {}

Expand Down Expand Up @@ -247,6 +238,12 @@ void DialogModelCombobox::OnPerformAction(base::PassKey<DialogModelHost>) {
callback_.Run();
}

DialogModelSeparator::DialogModelSeparator(base::PassKey<DialogModel> pass_key,
DialogModel* model)
: DialogModelField(pass_key, model, kSeparator, -1, {}) {}

DialogModelSeparator::~DialogModelSeparator() = default;

DialogModelTextfield::Params::Params() = default;
DialogModelTextfield::Params::~Params() = default;

Expand Down Expand Up @@ -292,11 +289,7 @@ DialogModelCustomField::DialogModelCustomField(
DialogModel* model,
int unique_id,
std::unique_ptr<DialogModelCustomField::Factory> factory)
: DialogModelField(pass_key,
model,
kCustom,
unique_id,
base::flat_set<Accelerator>()),
: DialogModelField(pass_key, model, kCustom, unique_id, {}),
factory_(std::move(factory)) {}

DialogModelCustomField::~DialogModelCustomField() = default;
Expand Down
25 changes: 22 additions & 3 deletions ui/base/models/dialog_model_field.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,15 @@ class COMPONENT_EXPORT(UI_BASE) DialogModelLabel {
// stays in sync with the visible dialog (through DialogModelHosts).
class COMPONENT_EXPORT(UI_BASE) DialogModelField {
public:
enum Type { kButton, kBodyText, kCheckbox, kCombobox, kTextfield, kCustom };
enum Type {
kButton,
kBodyText,
kCheckbox,
kCombobox,
kCustom,
kSeparator,
kTextfield
};

DialogModelField(const DialogModelField&) = delete;
DialogModelField& operator=(const DialogModelField&) = delete;
Expand Down Expand Up @@ -343,6 +351,17 @@ class COMPONENT_EXPORT(UI_BASE) DialogModelCombobox : public DialogModelField {
base::RepeatingClosure callback_;
};

// Field class representing a separator.
class COMPONENT_EXPORT(UI_BASE) DialogModelSeparator : public DialogModelField {
public:
// Note that this is constructed through a DialogModel which adds it to model
// fields.
DialogModelSeparator(base::PassKey<DialogModel> pass_key, DialogModel* model);
DialogModelSeparator(const DialogModelSeparator&) = delete;
DialogModelSeparator& operator=(const DialogModelSeparator&) = delete;
~DialogModelSeparator() override;
};

// Field class representing a textfield and corresponding label to describe the
// textfield:
//
Expand Down Expand Up @@ -422,8 +441,8 @@ class COMPONENT_EXPORT(UI_BASE) DialogModelCustomField
DialogModel* model,
int unique_id,
std::unique_ptr<DialogModelCustomField::Factory> factory);
DialogModelCustomField(const DialogModelTextfield&) = delete;
DialogModelCustomField& operator=(const DialogModelTextfield&) = delete;
DialogModelCustomField(const DialogModelCustomField&) = delete;
DialogModelCustomField& operator=(const DialogModelCustomField&) = delete;
~DialogModelCustomField() override;

// Methods with base::PassKey<DialogModelHost> are only intended to be called
Expand Down
18 changes: 18 additions & 0 deletions ui/views/bubble/bubble_dialog_model_host.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/base/models/combobox_model.h"
#include "ui/base/models/dialog_model.h"
#include "ui/base/models/dialog_model_field.h"
#include "ui/views/accessibility/accessibility_paint_checks.h"
#include "ui/views/border.h"
#include "ui/views/bubble/bubble_dialog_delegate_view.h"
Expand All @@ -22,6 +23,7 @@
#include "ui/views/controls/button/md_text_button.h"
#include "ui/views/controls/combobox/combobox.h"
#include "ui/views/controls/label.h"
#include "ui/views/controls/separator.h"
#include "ui/views/controls/styled_label.h"
#include "ui/views/controls/textfield/textfield.h"
#include "ui/views/layout/box_layout.h"
Expand All @@ -48,6 +50,8 @@ BubbleDialogModelHost::FieldType GetFieldTypeForField(
return BubbleDialogModelHost::FieldType::kControl;
case ui::DialogModelField::kCombobox:
return BubbleDialogModelHost::FieldType::kControl;
case ui::DialogModelField::kSeparator:
return BubbleDialogModelHost::FieldType::kMenuItem;
case ui::DialogModelField::kCustom:
return static_cast<BubbleDialogModelHost::CustomViewFactory*>(
field->AsCustomField(pass_key)->factory(pass_key))
Expand Down Expand Up @@ -407,6 +411,9 @@ void BubbleDialogModelHost::OnFieldAdded(ui::DialogModelField* field) {
case ui::DialogModelField::kCombobox:
AddOrUpdateCombobox(field->AsCombobox(GetPassKey()));
break;
case ui::DialogModelField::kSeparator:
AddOrUpdateSeparator(field);
break;
case ui::DialogModelField::kTextfield:
AddOrUpdateTextfield(field->AsTextfield(GetPassKey()));
break;
Expand Down Expand Up @@ -567,6 +574,17 @@ void BubbleDialogModelHost::AddOrUpdateCombobox(
std::move(combobox), font_list);
}

void BubbleDialogModelHost::AddOrUpdateSeparator(
ui::DialogModelField* model_field) {
DCHECK_EQ(ui::DialogModelField::Type::kSeparator,
model_field->type(GetPassKey()));
// TODO(pbos): Support updates to the existing model.

auto separator = std::make_unique<Separator>();
DialogModelHostField info{model_field, separator.get(), nullptr};
AddDialogModelHostField(std::move(separator), info);
}

void BubbleDialogModelHost::AddOrUpdateTextfield(
ui::DialogModelTextfield* model_field) {
// TODO(pbos): Support updates to the existing model.
Expand Down
1 change: 1 addition & 0 deletions ui/views/bubble/bubble_dialog_model_host.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ class VIEWS_EXPORT BubbleDialogModelHost : public BubbleDialogDelegate,
void AddOrUpdateBodyText(ui::DialogModelBodyText* model_field);
void AddOrUpdateCheckbox(ui::DialogModelCheckbox* model_field);
void AddOrUpdateCombobox(ui::DialogModelCombobox* model_field);
void AddOrUpdateSeparator(ui::DialogModelField* model_field);
void AddOrUpdateTextfield(ui::DialogModelTextfield* model_field);

void UpdateSpacingAndMargins();
Expand Down

0 comments on commit 82cc966

Please sign in to comment.