Commit f0fe519c authored by Peter Boström's avatar Peter Boström Committed by Commit Bot

Move DialogModelField downcasts to base class

This introduces DialogModelField::AsButton, etc. instead of having
DialogModelHost and DialogModel both downcast.

Also adds DialogModel::AddField to make sure OnFieldAdded() is called
from all call sites that add a field.

Bug: 1106422
Change-Id: I512f5df9cf642314de7bde5b9ef4fe1d7c3efedc
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2363870
Commit-Queue: Peter Boström <pbos@chromium.org>
Commit-Queue: Scott Violet <sky@chromium.org>
Auto-Submit: Peter Boström <pbos@chromium.org>
Reviewed-by: default avatarScott Violet <sky@chromium.org>
Cr-Commit-Position: refs/heads/master@{#799439}
parent 9c916247
...@@ -127,28 +127,22 @@ DialogModel::~DialogModel() = default; ...@@ -127,28 +127,22 @@ DialogModel::~DialogModel() = default;
void DialogModel::AddBodyText(base::string16 text, void DialogModel::AddBodyText(base::string16 text,
const DialogModelBodyText::Params& params) { const DialogModelBodyText::Params& params) {
fields_.push_back(std::make_unique<DialogModelBodyText>( AddField(std::make_unique<DialogModelBodyText>(GetPassKey(), this,
GetPassKey(), this, std::move(text), params)); std::move(text), params));
if (host_)
host_->OnFieldAdded(fields_.back().get());
} }
void DialogModel::AddCombobox(base::string16 label, void DialogModel::AddCombobox(base::string16 label,
std::unique_ptr<ui::ComboboxModel> combobox_model, std::unique_ptr<ui::ComboboxModel> combobox_model,
const DialogModelCombobox::Params& params) { const DialogModelCombobox::Params& params) {
fields_.push_back(std::make_unique<DialogModelCombobox>( AddField(std::make_unique<DialogModelCombobox>(
GetPassKey(), this, std::move(label), std::move(combobox_model), params)); GetPassKey(), this, std::move(label), std::move(combobox_model), params));
if (host_)
host_->OnFieldAdded(fields_.back().get());
} }
void DialogModel::AddTextfield(base::string16 label, void DialogModel::AddTextfield(base::string16 label,
base::string16 text, base::string16 text,
const DialogModelTextfield::Params& params) { const DialogModelTextfield::Params& params) {
fields_.push_back(std::make_unique<DialogModelTextfield>( AddField(std::make_unique<DialogModelTextfield>(
GetPassKey(), this, std::move(label), std::move(text), params)); GetPassKey(), this, std::move(label), std::move(text), params));
if (host_)
host_->OnFieldAdded(fields_.back().get());
} }
DialogModelField* DialogModel::GetFieldByUniqueId(int unique_id) { DialogModelField* DialogModel::GetFieldByUniqueId(int unique_id) {
...@@ -161,16 +155,11 @@ DialogModelField* DialogModel::GetFieldByUniqueId(int unique_id) { ...@@ -161,16 +155,11 @@ DialogModelField* DialogModel::GetFieldByUniqueId(int unique_id) {
} }
DialogModelCombobox* DialogModel::GetComboboxByUniqueId(int unique_id) { DialogModelCombobox* DialogModel::GetComboboxByUniqueId(int unique_id) {
auto* field = GetFieldByUniqueId(unique_id); return GetFieldByUniqueId(unique_id)->AsCombobox();
DCHECK_EQ(field->type_, DialogModelField::kCombobox);
return static_cast<DialogModelCombobox*>(field);
} }
DialogModelTextfield* DialogModel::GetTextfieldByUniqueId(int unique_id) { DialogModelTextfield* DialogModel::GetTextfieldByUniqueId(int unique_id) {
auto* field = GetFieldByUniqueId(unique_id); return GetFieldByUniqueId(unique_id)->AsTextfield();
DCHECK_EQ(field->type_, DialogModelField::kTextfield);
return static_cast<DialogModelTextfield*>(field);
} }
void DialogModel::OnDialogAccepted(util::PassKey<DialogModelHost>) { void DialogModel::OnDialogAccepted(util::PassKey<DialogModelHost>) {
...@@ -193,4 +182,10 @@ void DialogModel::OnWindowClosing(util::PassKey<DialogModelHost>) { ...@@ -193,4 +182,10 @@ void DialogModel::OnWindowClosing(util::PassKey<DialogModelHost>) {
std::move(window_closing_callback_).Run(); std::move(window_closing_callback_).Run();
} }
void DialogModel::AddField(std::unique_ptr<DialogModelField> field) {
fields_.push_back(std::move(field));
if (host_)
host_->OnFieldAdded(fields_.back().get());
}
} // namespace ui } // namespace ui
\ No newline at end of file
...@@ -226,6 +226,8 @@ class COMPONENT_EXPORT(UI_BASE) DialogModel final { ...@@ -226,6 +226,8 @@ class COMPONENT_EXPORT(UI_BASE) DialogModel final {
return util::PassKey<DialogModel>(); return util::PassKey<DialogModel>();
} }
void AddField(std::unique_ptr<DialogModelField> field);
std::unique_ptr<DialogModelDelegate> delegate_; std::unique_ptr<DialogModelDelegate> delegate_;
DialogModelHost* host_ = nullptr; DialogModelHost* host_ = nullptr;
......
...@@ -21,6 +21,44 @@ DialogModelField::DialogModelField(util::PassKey<DialogModel>, ...@@ -21,6 +21,44 @@ DialogModelField::DialogModelField(util::PassKey<DialogModel>,
DialogModelField::~DialogModelField() = default; DialogModelField::~DialogModelField() = default;
DialogModelButton* DialogModelField::AsButton(util::PassKey<DialogModelHost>) {
return AsButton();
}
DialogModelBodyText* DialogModelField::AsBodyText(
util::PassKey<DialogModelHost>) {
return AsBodyText();
}
DialogModelCombobox* DialogModelField::AsCombobox(
util::PassKey<DialogModelHost>) {
return AsCombobox();
}
DialogModelTextfield* DialogModelField::AsTextfield(
util::PassKey<DialogModelHost>) {
return AsTextfield();
}
DialogModelButton* DialogModelField::AsButton() {
DCHECK_EQ(type_, kButton);
return static_cast<DialogModelButton*>(this);
}
DialogModelBodyText* DialogModelField::AsBodyText() {
DCHECK_EQ(type_, kBodyText);
return static_cast<DialogModelBodyText*>(this);
}
DialogModelCombobox* DialogModelField::AsCombobox() {
DCHECK_EQ(type_, kCombobox);
return static_cast<DialogModelCombobox*>(this);
}
DialogModelTextfield* DialogModelField::AsTextfield() {
DCHECK_EQ(type_, kTextfield);
return static_cast<DialogModelTextfield*>(this);
}
DialogModelButton::Params::Params() = default; DialogModelButton::Params::Params() = default;
DialogModelButton::Params::~Params() = default; DialogModelButton::Params::~Params() = default;
......
...@@ -15,7 +15,11 @@ ...@@ -15,7 +15,11 @@
namespace ui { namespace ui {
class DialogModel; class DialogModel;
class DialogModelButton;
class DialogModelBodyText;
class DialogModelCombobox;
class DialogModelHost; class DialogModelHost;
class DialogModelTextfield;
class Event; class Event;
// These "field" classes represent entries in a DialogModel. They are owned // These "field" classes represent entries in a DialogModel. They are owned
...@@ -41,6 +45,10 @@ class COMPONENT_EXPORT(UI_BASE) DialogModelField { ...@@ -41,6 +45,10 @@ class COMPONENT_EXPORT(UI_BASE) DialogModelField {
util::PassKey<DialogModelHost>) const { util::PassKey<DialogModelHost>) const {
return accelerators_; return accelerators_;
} }
DialogModelButton* AsButton(util::PassKey<DialogModelHost>);
DialogModelBodyText* AsBodyText(util::PassKey<DialogModelHost>);
DialogModelCombobox* AsCombobox(util::PassKey<DialogModelHost>);
DialogModelTextfield* AsTextfield(util::PassKey<DialogModelHost>);
protected: protected:
// Children of this class need to be constructed through DialogModel to help // Children of this class need to be constructed through DialogModel to help
...@@ -51,6 +59,11 @@ class COMPONENT_EXPORT(UI_BASE) DialogModelField { ...@@ -51,6 +59,11 @@ class COMPONENT_EXPORT(UI_BASE) DialogModelField {
int unique_id, int unique_id,
base::flat_set<Accelerator> accelerators); base::flat_set<Accelerator> accelerators);
DialogModelButton* AsButton();
DialogModelBodyText* AsBodyText();
DialogModelCombobox* AsCombobox();
DialogModelTextfield* AsTextfield();
private: private:
friend class DialogModel; friend class DialogModel;
......
...@@ -170,13 +170,13 @@ void BubbleDialogModelHost::AddInitialFields() { ...@@ -170,13 +170,13 @@ void BubbleDialogModelHost::AddInitialFields() {
// TODO(pbos): Add support for buttons that are part of content area. // TODO(pbos): Add support for buttons that are part of content area.
continue; continue;
case ui::DialogModelField::kBodyText: case ui::DialogModelField::kBodyText:
last_view = AddOrUpdateBodyText(FieldAsBodyText(field.get())); last_view = AddOrUpdateBodyText(field->AsBodyText(GetPassKey()));
break; break;
case ui::DialogModelField::kCombobox: case ui::DialogModelField::kCombobox:
last_view = AddOrUpdateCombobox(FieldAsCombobox(field.get())); last_view = AddOrUpdateCombobox(field->AsCombobox(GetPassKey()));
break; break;
case ui::DialogModelField::kTextfield: case ui::DialogModelField::kTextfield:
last_view = AddOrUpdateTextfield(FieldAsTextfield(field.get())); last_view = AddOrUpdateTextfield(field->AsTextfield(GetPassKey()));
break; break;
} }
...@@ -290,19 +290,23 @@ void BubbleDialogModelHost::AddLabelAndField(const base::string16& label_text, ...@@ -290,19 +290,23 @@ void BubbleDialogModelHost::AddLabelAndField(const base::string16& label_text,
} }
void BubbleDialogModelHost::NotifyTextfieldTextChanged(Textfield* textfield) { void BubbleDialogModelHost::NotifyTextfieldTextChanged(Textfield* textfield) {
FieldAsTextfield(view_to_field_[textfield]) view_to_field_[textfield]
->AsTextfield(GetPassKey())
->OnTextChanged(GetPassKey(), textfield->GetText()); ->OnTextChanged(GetPassKey(), textfield->GetText());
} }
void BubbleDialogModelHost::NotifyComboboxSelectedIndexChanged( void BubbleDialogModelHost::NotifyComboboxSelectedIndexChanged(
Combobox* combobox) { Combobox* combobox) {
FieldAsCombobox(view_to_field_[combobox]) view_to_field_[combobox]
->AsCombobox(GetPassKey())
->OnSelectedIndexChanged(GetPassKey(), combobox->GetSelectedIndex()); ->OnSelectedIndexChanged(GetPassKey(), combobox->GetSelectedIndex());
} }
void BubbleDialogModelHost::ButtonPressed(Button* sender, void BubbleDialogModelHost::ButtonPressed(Button* sender,
const ui::Event& event) { const ui::Event& event) {
FieldAsButton(view_to_field_[sender])->OnPressed(GetPassKey(), event); view_to_field_[sender]
->AsButton(GetPassKey())
->OnPressed(GetPassKey(), event);
} }
void BubbleDialogModelHost::OnPerformAction(Combobox* combobox) { void BubbleDialogModelHost::OnPerformAction(Combobox* combobox) {
...@@ -310,7 +314,9 @@ void BubbleDialogModelHost::OnPerformAction(Combobox* combobox) { ...@@ -310,7 +314,9 @@ void BubbleDialogModelHost::OnPerformAction(Combobox* combobox) {
// but Combobox right now doesn't support listening to selected-index changes. // but Combobox right now doesn't support listening to selected-index changes.
NotifyComboboxSelectedIndexChanged(combobox); NotifyComboboxSelectedIndexChanged(combobox);
FieldAsCombobox(view_to_field_[combobox])->OnPerformAction(GetPassKey()); view_to_field_[combobox]
->AsCombobox(GetPassKey())
->OnPerformAction(GetPassKey());
} }
void BubbleDialogModelHost::OnViewCreatedForField(View* view, void BubbleDialogModelHost::OnViewCreatedForField(View* view,
...@@ -338,32 +344,4 @@ View* BubbleDialogModelHost::FieldToView(ui::DialogModelField* field) { ...@@ -338,32 +344,4 @@ View* BubbleDialogModelHost::FieldToView(ui::DialogModelField* field) {
return nullptr; return nullptr;
} }
ui::DialogModelButton* BubbleDialogModelHost::FieldAsButton(
ui::DialogModelField* field) {
DCHECK(field);
DCHECK_EQ(field->type(GetPassKey()), ui::DialogModelField::kButton);
return static_cast<ui::DialogModelButton*>(field);
}
ui::DialogModelBodyText* BubbleDialogModelHost::FieldAsBodyText(
ui::DialogModelField* field) {
DCHECK(field);
DCHECK_EQ(field->type(GetPassKey()), ui::DialogModelField::kBodyText);
return static_cast<ui::DialogModelBodyText*>(field);
}
ui::DialogModelCombobox* BubbleDialogModelHost::FieldAsCombobox(
ui::DialogModelField* field) {
DCHECK(field);
DCHECK_EQ(field->type(GetPassKey()), ui::DialogModelField::kCombobox);
return static_cast<ui::DialogModelCombobox*>(field);
}
ui::DialogModelTextfield* BubbleDialogModelHost::FieldAsTextfield(
ui::DialogModelField* field) {
DCHECK(field);
DCHECK_EQ(field->type(GetPassKey()), ui::DialogModelField::kTextfield);
return static_cast<ui::DialogModelTextfield*>(field);
}
} // namespace views } // namespace views
...@@ -78,11 +78,6 @@ class VIEWS_EXPORT BubbleDialogModelHost : public BubbleDialogDelegateView, ...@@ -78,11 +78,6 @@ class VIEWS_EXPORT BubbleDialogModelHost : public BubbleDialogDelegateView,
View* FieldToView(ui::DialogModelField* field); View* FieldToView(ui::DialogModelField* field);
ui::DialogModelButton* FieldAsButton(ui::DialogModelField* field);
ui::DialogModelBodyText* FieldAsBodyText(ui::DialogModelField* field);
ui::DialogModelCombobox* FieldAsCombobox(ui::DialogModelField* field);
ui::DialogModelTextfield* FieldAsTextfield(ui::DialogModelField* field);
std::unique_ptr<ui::DialogModel> model_; std::unique_ptr<ui::DialogModel> model_;
base::flat_map<View*, ui::DialogModelField*> view_to_field_; base::flat_map<View*, ui::DialogModelField*> view_to_field_;
std::vector<PropertyChangedSubscription> property_changed_subscriptions_; std::vector<PropertyChangedSubscription> property_changed_subscriptions_;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment