File: test_app_window.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (274 lines) | stat: -rw-r--r-- 10,715 bytes parent folder | download | duplicates (3)
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
// Copyright 2025 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/examples/test_app_window.h"

#include <memory>
#include <optional>

#include "ash/examples/client_controlled_state_util.h"
#include "ash/shell.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/memory/weak_ptr.h"
#include "base/strings/string_number_conversions.h"
#include "base/task/sequenced_task_runner.h"
#include "base/time/time.h"
#include "base/timer/timer.h"
#include "chromeos/ui/base/app_types.h"
#include "chromeos/ui/base/window_properties.h"
#include "ui/aura/client/aura_constants.h"
#include "ui/base/models/combobox_model.h"
#include "ui/color/color_variant.h"
#include "ui/gfx/geometry/size.h"
#include "ui/views/accessibility/view_accessibility.h"
#include "ui/views/background.h"
#include "ui/views/controls/button/checkbox.h"
#include "ui/views/controls/combobox/combobox.h"
#include "ui/views/controls/label.h"
#include "ui/views/controls/textfield/textfield.h"
#include "ui/views/layout/box_layout.h"
#include "ui/views/view.h"
#include "ui/views/widget/widget.h"
#include "ui/views/widget/widget_delegate.h"

namespace ash {
namespace {

class AppTypeComboboxModel : public ui::ComboboxModel {
 public:
  AppTypeComboboxModel() = default;
  AppTypeComboboxModel(const AppTypeComboboxModel&) = delete;
  AppTypeComboboxModel& operator=(const AppTypeComboboxModel&) = delete;
  ~AppTypeComboboxModel() override = default;

 private:
  // ui::ComboboxModel:
  size_t GetItemCount() const override {
    return static_cast<size_t>(chromeos::AppType::kMaxValue) + 1;
  }
  std::u16string GetItemAt(size_t index) const override {
    switch (static_cast<chromeos::AppType>(index)) {
      case chromeos::AppType::NON_APP:
        return u"AppType: None";
      case chromeos::AppType::BROWSER:
        return u"AppType: Browser";
      case chromeos::AppType::CHROME_APP:
        return u"AppType: ChromeApp";
      case chromeos::AppType::ARC_APP:
        return u"AppType: Arc";
      case chromeos::AppType::CROSTINI_APP:
        return u"AppType: Crostini";
      case chromeos::AppType::SYSTEM_APP:
        return u"AppType: SystemApp";
    }
    return u"AppType: Unknown";
  }
};

class TestView : public views::View {
 public:
  enum ViewElements : int {
    kAppTypesCombobox = 1000,
    kOpenClientControlledButton,
  };

  explicit TestView(bool add_open_client_controlled) {
    SetBackground(
        views::CreateSolidBackground(ui::ColorVariant(SK_ColorWHITE)));
    auto* layout = SetLayoutManager(std::make_unique<views::BoxLayout>(
        views::BoxLayout::Orientation::kVertical));
    layout->set_cross_axis_alignment(
        views::BoxLayout::CrossAxisAlignment::kStart);

    auto add_checkbox =
        [](views::View* container, const std::u16string& text,
           const std::u16string& tooltip, bool initial_state,
           base::RepeatingCallback<void(const ui::Event&)> callback) {
          views::Checkbox* checkbox = container->AddChildView(
              std::make_unique<views::Checkbox>(text, std::move(callback)));
          checkbox->SetChecked(initial_state);
          checkbox->SetTooltipText(tooltip);
          return checkbox;
        };
    auto add_textfield = [](views::View* container, const std::u16string& text,
                            const std::u16string& tooltip,
                            const std::u16string& initial_value) {
      views::View* hc =
          container->AddChildView(std::make_unique<views::View>());
      auto* layout = hc->SetLayoutManager(std::make_unique<views::BoxLayout>(
          views::BoxLayout::Orientation::kHorizontal));
      layout->set_cross_axis_alignment(
          views::BoxLayout::CrossAxisAlignment::kCenter);

      views::Label* label =
          hc->AddChildView(std::make_unique<views::Label>(text));
      label->SetTooltipText(tooltip);

      views::Textfield* textfield =
          hc->AddChildView(std::make_unique<views::Textfield>());
      textfield->SetText(initial_value);
      textfield->GetViewAccessibility().SetName(
          std::u16string(), ax::mojom::NameFrom::kAttributeExplicitlyEmpty);
      layout->SetFlexForView(textfield, 1);
      return textfield;
    };

    add_checkbox(
        this, u"Activatable", u"activatable",
        /*initial_state=*/true, base::BindRepeating([](const ui::Event& event) {
          views::Checkbox* cb = static_cast<views::Checkbox*>(event.target());
          cb->GetWidget()->widget_delegate()->SetCanActivate(cb->GetChecked());
        }));
    add_checkbox(
        this, u"Resizable", u"resizable",
        /*initial_state=*/true, base::BindRepeating([](const ui::Event& event) {
          views::Checkbox* cb = static_cast<views::Checkbox*>(event.target());
          cb->GetWidget()->widget_delegate()->SetCanResize(cb->GetChecked());
        }));
    add_checkbox(
        this, u"Maximizable", u"maximizable",
        /*initial_state=*/true, base::BindRepeating([](const ui::Event& event) {
          views::Checkbox* cb = static_cast<views::Checkbox*>(event.target());
          cb->GetWidget()->widget_delegate()->SetCanMaximize(cb->GetChecked());
        }));
    add_checkbox(this, u"Always On Top", u"always on top",
                 /*initial_state=*/false,
                 base::BindRepeating([](const ui::Event& event) {
                   views::Checkbox* cb =
                       static_cast<views::Checkbox*>(event.target());
                   auto* window = cb->GetWidget()->GetNativeWindow();
                   window->SetProperty(aura::client::kZOrderingKey,
                                       cb->GetChecked()
                                           ? ui::ZOrderLevel::kFloatingWindow
                                           : ui::ZOrderLevel::kNormal);
                 }));

    AddChildView(std::make_unique<views::LabelButton>(
        base::BindRepeating(
            &TestView::RunAfterFiveSeconds, base::Unretained(this),
            base::BindRepeating(
                [](views::View* view) -> void { view->GetWidget()->Close(); },
                base::Unretained(this))),
        u"Close After 5 seconds"));

    AddChildView(std::make_unique<views::LabelButton>(
        base::BindRepeating(
            &TestView::RunAfterFiveSeconds, base::Unretained(this),
            base::BindRepeating(
                [](views::View* view) { view->GetWidget()->Activate(); },
                base::Unretained(this))),
        u"Activate After 5 seconds"));

    AddChildView(std::make_unique<views::LabelButton>(
        base::BindRepeating(
            &TestView::RunAfterFiveSeconds, base::Unretained(this),
            base::BindRepeating(
                [](views::View* view) { view->GetWidget()->Restore(); },
                base::Unretained(this))),
        u"Restore After 5 seconds"));

    if (add_open_client_controlled) {
      AddChildView(
          std::make_unique<views::LabelButton>(
              base::BindRepeating(&OpenTestAppWindow,
                                  /*use_client_controlled_state=*/true),
              u"Open ClientControlled App Window"))
          ->SetID(kOpenClientControlledButton);
    }

    AddChildView(std::make_unique<views::LabelButton>(
        base::BindRepeating(&TestView::UpdateMinimumSize,
                            base::Unretained(this)),
        u"Apply Minimum Size"));

    width_ = add_textfield(this, u"Min Width", u"minimum width",
                           base::NumberToString16(GetMinimumSize().width()));
    height_ = add_textfield(this, u"Min height", u"minimum height",
                            base::NumberToString16(GetMinimumSize().height()));
    auto* app_types = AddChildView(std::make_unique<views::Combobox>(
        std::make_unique<AppTypeComboboxModel>()));
    app_types->GetViewAccessibility().SetName(
        std::u16string(), ax::mojom::NameFrom::kAttributeExplicitlyEmpty);
    app_types->SetCallback(base::BindRepeating(
        [](views::Combobox* app_types) -> void {
          size_t selected = app_types->GetSelectedIndex().value_or(0);
          auto* window = app_types->GetWidget()->GetNativeWindow();
          window->SetProperty(chromeos::kAppTypeKey,
                              static_cast<chromeos::AppType>(selected));
        },
        base::Unretained(app_types)));
    app_types->SetID(kAppTypesCombobox);
  }

  // views::View:
  gfx::Size GetMinimumSize() const override { return minimum_size_; }

  void AddedToWidget() override {
    // Initialize the app type values when the widget/window is ready.
    auto* app_types =
        static_cast<views::Combobox*>(GetViewByID(kAppTypesCombobox));
    if (GetViewByID(kOpenClientControlledButton)) {
      app_types->MenuSelectionAt(1);  // Browser
    } else {
      app_types->MenuSelectionAt(3);  // ARC
    }
  }

 private:
  void UpdateMinimumSize() {
    int width;
    int height;
    if (base::StringToInt(width_->GetText(), &width) &&
        base::StringToInt(height_->GetText(), &height)) {
      minimum_size_.SetSize(width, height);
      PreferredSizeChanged();
    } else {
      LOG(ERROR) << "Failed to set minimum size to:" << width_->GetText()
                 << " x " << height_->GetText();
    }
  }

  void RunAfterFiveSeconds(base::RepeatingCallback<void()> callback) {
    timer_.Start(FROM_HERE, base::Seconds(5), callback);
  }

  raw_ptr<views::Textfield> width_, height_;
  gfx::Size minimum_size_{250, 100};
  base::OneShotTimer timer_;
};

}  // namespace

void OpenTestAppWindow(bool use_client_controlled_state) {
  views::Widget* widget = new views::Widget;
  views::Widget::InitParams params(
      views::Widget::InitParams::NATIVE_WIDGET_OWNS_WIDGET,
      views::Widget::InitParams::TYPE_WINDOW);
  auto widget_delegate = std::make_unique<views::WidgetDelegate>();
  widget_delegate->SetCanActivate(true);
  widget_delegate->SetCanFullscreen(true);
  widget_delegate->SetCanMaximize(true);
  widget_delegate->SetCanMinimize(true);
  widget_delegate->SetCanResize(true);
  widget_delegate->SetTitle(use_client_controlled_state
                                ? u"TestAppWindow(ClientControlled)"
                                : u"TestAppWindow(Normal)");

  params.delegate = widget_delegate.release();
  params.delegate->SetContentsView(
      std::make_unique<TestView>(!use_client_controlled_state));

  params.context = Shell::GetPrimaryRootWindow();
  params.name = "AshTestAppWindow";
  widget->Init(std::move(params));

  if (use_client_controlled_state) {
    ClientControlledStateUtil::BuildAndSet(widget->GetNativeWindow());
  }

  widget->Show();
}

}  // namespace ash