| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ash/test/child_modal_window.h" | 5 #include "ash/test/child_modal_window.h" |
| 6 | 6 |
| 7 #include "base/memory/ptr_util.h" |
| 7 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
| 8 #include "ui/aura/window.h" | 9 #include "ui/aura/window.h" |
| 9 #include "ui/gfx/canvas.h" | 10 #include "ui/gfx/canvas.h" |
| 10 #include "ui/views/background.h" | 11 #include "ui/views/background.h" |
| 11 #include "ui/views/controls/button/label_button.h" | 12 #include "ui/views/controls/button/label_button.h" |
| 12 #include "ui/views/controls/native/native_view_host.h" | 13 #include "ui/views/controls/native/native_view_host.h" |
| 13 #include "ui/views/controls/textfield/textfield.h" | 14 #include "ui/views/controls/textfield/textfield.h" |
| 14 #include "ui/views/widget/widget.h" | 15 #include "ui/views/widget/widget.h" |
| 15 #include "ui/views/widget/widget_delegate.h" | 16 #include "ui/views/widget/widget_delegate.h" |
| 16 #include "ui/wm/core/window_modality_controller.h" | 17 #include "ui/wm/core/window_modality_controller.h" |
| (...skipping 23 matching lines...) Expand all Loading... |
| 40 const int kChildTextfieldLeft = 20; | 41 const int kChildTextfieldLeft = 20; |
| 41 const int kChildTextfieldTop = 50; | 42 const int kChildTextfieldTop = 50; |
| 42 const int kChildTextfieldWidth = 290; | 43 const int kChildTextfieldWidth = 290; |
| 43 const int kChildTextfieldHeight = 35; | 44 const int kChildTextfieldHeight = 35; |
| 44 | 45 |
| 45 const SkColor kModalParentColor = SK_ColorWHITE; | 46 const SkColor kModalParentColor = SK_ColorWHITE; |
| 46 const SkColor kChildColor = SK_ColorWHITE; | 47 const SkColor kChildColor = SK_ColorWHITE; |
| 47 | 48 |
| 48 } // namespace | 49 } // namespace |
| 49 | 50 |
| 50 void CreateChildModalParent(gfx::NativeView context) { | 51 void CreateChildModalParent(aura::Window* context) { |
| 51 Widget::CreateWindowWithContextAndBounds( | 52 Widget::CreateWindowWithContextAndBounds( |
| 52 new ChildModalParent(context), context, | 53 new ChildModalParent(context), context, |
| 53 gfx::Rect(kWindowLeft, kWindowTop, kWindowWidth, kWindowHeight)) | 54 gfx::Rect(kWindowLeft, kWindowTop, kWindowWidth, kWindowHeight)) |
| 54 ->Show(); | 55 ->Show(); |
| 55 } | 56 } |
| 56 | 57 |
| 57 class ChildModalWindow : public views::WidgetDelegateView { | 58 class ChildModalWindow : public views::WidgetDelegateView { |
| 58 public: | 59 public: |
| 59 ChildModalWindow(); | 60 ChildModalWindow(); |
| 60 ~ChildModalWindow() override; | 61 ~ChildModalWindow() override; |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 94 } | 95 } |
| 95 | 96 |
| 96 bool ChildModalWindow::CanResize() const { | 97 bool ChildModalWindow::CanResize() const { |
| 97 return false; | 98 return false; |
| 98 } | 99 } |
| 99 | 100 |
| 100 ui::ModalType ChildModalWindow::GetModalType() const { | 101 ui::ModalType ChildModalWindow::GetModalType() const { |
| 101 return ui::MODAL_TYPE_CHILD; | 102 return ui::MODAL_TYPE_CHILD; |
| 102 } | 103 } |
| 103 | 104 |
| 104 ChildModalParent::ChildModalParent(gfx::NativeView context) | 105 ChildModalParent::ChildModalParent(aura::Window* context) |
| 105 : button_(new views::LabelButton( | 106 : widget_(base::MakeUnique<Widget>()), |
| 107 button_(new views::LabelButton( |
| 106 this, | 108 this, |
| 107 base::ASCIIToUTF16("Show/Hide Child Modal Window"))), | 109 base::ASCIIToUTF16("Show/Hide Child Modal Window"))), |
| 108 textfield_(new views::Textfield), | 110 textfield_(new views::Textfield), |
| 109 host_(new views::NativeViewHost), | 111 host_(new views::NativeViewHost), |
| 110 modal_parent_(NULL), | 112 child_(nullptr) { |
| 111 child_(NULL) { | |
| 112 Widget* widget = new Widget; | |
| 113 Widget::InitParams params(Widget::InitParams::TYPE_CONTROL); | 113 Widget::InitParams params(Widget::InitParams::TYPE_CONTROL); |
| 114 params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; |
| 114 params.context = context; | 115 params.context = context; |
| 115 widget->Init(params); | 116 widget_->Init(params); |
| 116 widget->GetRootView()->set_background( | 117 widget_->GetRootView()->set_background( |
| 117 views::Background::CreateSolidBackground(kModalParentColor)); | 118 views::Background::CreateSolidBackground(kModalParentColor)); |
| 118 modal_parent_ = widget->GetNativeView(); | 119 widget_->GetNativeView()->SetName("ModalParent"); |
| 119 widget->GetNativeView()->SetName("ModalParent"); | |
| 120 AddChildView(button_); | 120 AddChildView(button_); |
| 121 AddChildView(textfield_); | 121 AddChildView(textfield_); |
| 122 AddChildView(host_); | 122 AddChildView(host_); |
| 123 } | 123 } |
| 124 | 124 |
| 125 ChildModalParent::~ChildModalParent() {} | 125 ChildModalParent::~ChildModalParent() {} |
| 126 | 126 |
| 127 void ChildModalParent::ShowChild() { | 127 void ChildModalParent::ShowChild() { |
| 128 if (!child_) | 128 if (!child_) |
| 129 child_ = CreateChild(); | 129 child_ = CreateChild(); |
| 130 child_->Show(); | 130 child_->Show(); |
| 131 } | 131 } |
| 132 | 132 |
| 133 gfx::NativeWindow ChildModalParent::GetModalParent() const { | 133 aura::Window* ChildModalParent::GetModalParent() const { |
| 134 return modal_parent_; | 134 return widget_->GetNativeView(); |
| 135 } | 135 } |
| 136 | 136 |
| 137 gfx::NativeWindow ChildModalParent::GetChild() const { | 137 aura::Window* ChildModalParent::GetChild() const { |
| 138 if (child_) | 138 if (child_) |
| 139 return child_->GetNativeView(); | 139 return child_->GetNativeView(); |
| 140 return NULL; | 140 return nullptr; |
| 141 } | 141 } |
| 142 | 142 |
| 143 Widget* ChildModalParent::CreateChild() { | 143 Widget* ChildModalParent::CreateChild() { |
| 144 Widget* child = Widget::CreateWindowWithParent(new ChildModalWindow, | 144 Widget* child = Widget::CreateWindowWithParent(new ChildModalWindow, |
| 145 GetWidget()->GetNativeView()); | 145 GetWidget()->GetNativeView()); |
| 146 wm::SetModalParent(child->GetNativeView(), GetModalParent()); | 146 wm::SetModalParent(child->GetNativeView(), GetModalParent()); |
| 147 child->AddObserver(this); | 147 child->AddObserver(this); |
| 148 child->GetNativeView()->SetName("ChildModalWindow"); | 148 child->GetNativeView()->SetName("ChildModalWindow"); |
| 149 return child; | 149 return child; |
| 150 } | 150 } |
| (...skipping 21 matching lines...) Expand all Loading... |
| 172 button_->SetBounds(x(), running_y, width(), kButtonHeight); | 172 button_->SetBounds(x(), running_y, width(), kButtonHeight); |
| 173 running_y += kButtonHeight; | 173 running_y += kButtonHeight; |
| 174 textfield_->SetBounds(x(), running_y, width(), kTextfieldHeight); | 174 textfield_->SetBounds(x(), running_y, width(), kTextfieldHeight); |
| 175 running_y += kTextfieldHeight; | 175 running_y += kTextfieldHeight; |
| 176 host_->SetBounds(x(), running_y, width(), height() - running_y); | 176 host_->SetBounds(x(), running_y, width(), height() - running_y); |
| 177 } | 177 } |
| 178 | 178 |
| 179 void ChildModalParent::ViewHierarchyChanged( | 179 void ChildModalParent::ViewHierarchyChanged( |
| 180 const ViewHierarchyChangedDetails& details) { | 180 const ViewHierarchyChangedDetails& details) { |
| 181 if (details.is_add && details.child == this) { | 181 if (details.is_add && details.child == this) { |
| 182 host_->Attach(modal_parent_); | 182 host_->Attach(widget_->GetNativeWindow()); |
| 183 GetWidget()->GetNativeView()->SetName("Parent"); | 183 GetWidget()->GetNativeView()->SetName("Parent"); |
| 184 } | 184 } |
| 185 } | 185 } |
| 186 | 186 |
| 187 void ChildModalParent::ButtonPressed(views::Button* sender, | 187 void ChildModalParent::ButtonPressed(views::Button* sender, |
| 188 const ui::Event& event) { | 188 const ui::Event& event) { |
| 189 if (sender == button_) { | 189 if (sender == button_) { |
| 190 if (!child_) | 190 if (!child_) |
| 191 child_ = CreateChild(); | 191 child_ = CreateChild(); |
| 192 if (child_->IsVisible()) | 192 if (child_->IsVisible()) |
| 193 child_->Hide(); | 193 child_->Hide(); |
| 194 else | 194 else |
| 195 child_->Show(); | 195 child_->Show(); |
| 196 } | 196 } |
| 197 } | 197 } |
| 198 | 198 |
| 199 void ChildModalParent::OnWidgetDestroying(Widget* widget) { | 199 void ChildModalParent::OnWidgetDestroying(Widget* widget) { |
| 200 if (child_) { | 200 if (child_) { |
| 201 DCHECK_EQ(child_, widget); | 201 DCHECK_EQ(child_, widget); |
| 202 child_ = NULL; | 202 child_ = NULL; |
| 203 } | 203 } |
| 204 } | 204 } |
| 205 | 205 |
| 206 } // namespace test | 206 } // namespace test |
| 207 } // namespace ash | 207 } // namespace ash |
| OLD | NEW |