File: test_child_modal_parent.cc

package info (click to toggle)
chromium 120.0.6099.224-1~deb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,112,112 kB
  • sloc: cpp: 32,907,025; ansic: 8,148,123; javascript: 3,679,536; python: 2,031,248; asm: 959,718; java: 804,675; xml: 617,256; sh: 111,417; objc: 100,835; perl: 88,443; cs: 53,032; makefile: 29,579; fortran: 24,137; php: 21,162; tcl: 21,147; sql: 20,809; ruby: 17,735; pascal: 12,864; yacc: 8,045; lisp: 3,388; lex: 1,323; ada: 727; awk: 329; jsp: 267; csh: 117; exp: 43; sed: 37
file content (148 lines) | stat: -rw-r--r-- 5,135 bytes parent folder | download
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
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ash/wm/test_child_modal_parent.h"

#include "base/functional/bind.h"
#include "base/strings/utf_string_conversions.h"
#include "ui/aura/window.h"
#include "ui/views/background.h"
#include "ui/views/controls/button/md_text_button.h"
#include "ui/views/controls/native/native_view_host.h"
#include "ui/views/controls/textfield/textfield.h"
#include "ui/views/widget/widget.h"
#include "ui/wm/core/window_modality_controller.h"
#include "ui/wm/core/window_util.h"

using views::Widget;

namespace ash {

namespace {

// Parent window layout.
const int kWindowLeft = 170;
const int kWindowTop = 200;
const int kWindowWidth = 400;
const int kWindowHeight = 400;
const int kButtonHeight = 35;

// Child window size.
const int kChildWindowWidth = 330;
const int kChildWindowHeight = 200;

// Child window layout.
const int kTextfieldLeft = 10;
const int kTextfieldTop = 20;
const int kTextfieldWidth = 300;
const int kTextfieldHeight = 35;

const SkColor kModalParentColor = SK_ColorBLUE;
const SkColor kChildColor = SK_ColorWHITE;

views::WidgetDelegateView* CreateChildModalWindow() {
  auto child = std::make_unique<views::WidgetDelegateView>();
  child->SetModalType(ui::MODAL_TYPE_CHILD);
  child->SetTitle(u"Examples: Child Modal Window");
  child->SetBackground(views::CreateSolidBackground(kChildColor));
  child->SetPreferredSize(gfx::Size(kChildWindowWidth, kChildWindowHeight));

  auto textfield = std::make_unique<views::Textfield>();
  textfield->SetBounds(kTextfieldLeft, kTextfieldTop, kTextfieldWidth,
                       kTextfieldHeight);
  textfield->SetPlaceholderText(u"modal child window");
  child->AddChildView(std::move(textfield));
  return child.release();
}

}  // namespace

// static
TestChildModalParent* TestChildModalParent::Show(aura::Window* context) {
  auto* test_child_modal_parent = new TestChildModalParent(context);
  views::Widget* widget = new views::Widget;
  views::Widget::InitParams params;
  params.delegate = test_child_modal_parent;
  params.context = context;
  params.bounds =
      gfx::Rect(kWindowLeft, kWindowTop, kWindowWidth, kWindowHeight);
  widget->Init(std::move(params));
  widget->Show();
  return test_child_modal_parent;
}

TestChildModalParent::TestChildModalParent(aura::Window* context)
    : modal_parent_(std::make_unique<Widget>()),
      textfield_(new views::Textfield),
      host_(new views::NativeViewHost) {
  SetTitle(u"Examples: Child Modal Parent");
  textfield_->SetPlaceholderText(u"top level window");
  Widget::InitParams params(Widget::InitParams::TYPE_CONTROL);
  params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
  params.context = context;
  modal_parent_->Init(std::move(params));
  modal_parent_->GetRootView()->SetBackground(
      views::CreateSolidBackground(kModalParentColor));
  auto* modal_parent_textfield = new views::Textfield;
  modal_parent_->GetRootView()->AddChildView(modal_parent_textfield);
  modal_parent_textfield->SetBounds(kTextfieldLeft, kTextfieldTop,
                                    kTextfieldWidth, kTextfieldHeight);
  modal_parent_textfield->SetPlaceholderText(u"modal parent window");
  modal_parent_->GetNativeView()->SetName("ModalParent");
  auto button = std::make_unique<views::MdTextButton>(
      base::BindRepeating(&TestChildModalParent::ButtonPressed,
                          base::Unretained(this)),
      u"Show/Hide Child Modal Window");
  button_ = AddChildView(std::move(button));
  AddChildView(textfield_.get());
  AddChildView(host_.get());
}

TestChildModalParent::~TestChildModalParent() = default;

aura::Window* TestChildModalParent::GetModalParent() const {
  return modal_parent_->GetNativeView();
}

aura::Window* TestChildModalParent::ShowModalChild() {
  DCHECK(!modal_child_);
  modal_child_ = Widget::CreateWindowWithParent(CreateChildModalWindow(),
                                                GetWidget()->GetNativeView());
  wm::SetModalParent(modal_child_->GetNativeView(),
                     modal_parent_->GetNativeView());
  modal_child_->AddObserver(this);
  modal_child_->GetNativeView()->SetName("ChildModalWindow");
  modal_child_->Show();
  return modal_child_->GetNativeView();
}

void TestChildModalParent::Layout() {
  int running_y = y();
  button_->SetBounds(x(), running_y, width(), kButtonHeight);
  running_y += kButtonHeight;
  textfield_->SetBounds(x(), running_y, width(), kTextfieldHeight);
  running_y += kTextfieldHeight;
  host_->SetBounds(x(), running_y, width(), height() - running_y);
}

void TestChildModalParent::AddedToWidget() {
  // The function requires a Widget be present.
  DCHECK(GetWidget());
  host_->Attach(modal_parent_->GetNativeView());
  GetWidget()->GetNativeView()->SetName("Parent");
}

void TestChildModalParent::OnWidgetDestroying(Widget* widget) {
  DCHECK_EQ(modal_child_, widget);
  modal_child_ = nullptr;
}

void TestChildModalParent::ButtonPressed() {
  if (modal_child_)
    modal_child_->Close();
  else
    ShowModalChild();
}

}  // namespace ash