1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ash/shell/window_type_launcher.h"
7 #include "ash/root_window_controller.h"
8 #include "ash/screensaver/screensaver_view.h"
9 #include "ash/session/session_state_delegate.h"
10 #include "ash/shelf/shelf_widget.h"
11 #include "ash/shell.h"
12 #include "ash/shell/example_factory.h"
13 #include "ash/shell/panel_window.h"
14 #include "ash/shell/toplevel_window.h"
15 #include "ash/shell_delegate.h"
16 #include "ash/shell_window_ids.h"
17 #include "ash/system/status_area_widget.h"
18 #include "ash/system/web_notification/web_notification_tray.h"
19 #include "ash/test/child_modal_window.h"
20 #include "base/bind.h"
21 #include "base/strings/utf_string_conversions.h"
22 #include "base/time/time.h"
23 #include "content/public/browser/browser_thread.h"
24 #include "ui/aura/window.h"
25 #include "ui/aura/window_event_dispatcher.h"
26 #include "ui/compositor/layer.h"
27 #include "ui/gfx/canvas.h"
28 #include "ui/message_center/message_center.h"
29 #include "ui/message_center/notification_types.h"
30 #include "ui/views/controls/button/label_button.h"
31 #include "ui/views/controls/menu/menu_item_view.h"
32 #include "ui/views/controls/menu/menu_runner.h"
33 #include "ui/views/examples/examples_window_with_content.h"
34 #include "ui/views/layout/grid_layout.h"
35 #include "ui/views/widget/widget.h"
36 #include "ui/wm/core/shadow_types.h"
38 using views::MenuItemView;
39 using views::MenuRunner;
46 SkColor g_colors[] = { SK_ColorRED,
50 int g_color_index = 0;
52 class ModalWindow : public views::WidgetDelegateView,
53 public views::ButtonListener {
55 explicit ModalWindow(ui::ModalType modal_type)
56 : modal_type_(modal_type),
57 color_(g_colors[g_color_index]),
58 open_button_(new views::LabelButton(this,
59 base::ASCIIToUTF16("Moar!"))) {
60 ++g_color_index %= arraysize(g_colors);
61 open_button_->SetStyle(views::Button::STYLE_BUTTON);
62 AddChildView(open_button_);
64 ~ModalWindow() override {}
66 static void OpenModalWindow(aura::Window* parent, ui::ModalType modal_type) {
67 views::Widget* widget =
68 views::Widget::CreateWindowWithParent(new ModalWindow(modal_type),
70 widget->GetNativeView()->SetName("ModalWindow");
74 // Overridden from views::View:
75 void OnPaint(gfx::Canvas* canvas) override {
76 canvas->FillRect(GetLocalBounds(), color_);
78 gfx::Size GetPreferredSize() const override { return gfx::Size(200, 200); }
79 void Layout() override {
80 gfx::Size open_ps = open_button_->GetPreferredSize();
81 gfx::Rect local_bounds = GetLocalBounds();
82 open_button_->SetBounds(
83 5, local_bounds.bottom() - open_ps.height() - 5,
84 open_ps.width(), open_ps.height());
87 // Overridden from views::WidgetDelegate:
88 views::View* GetContentsView() override { return this; }
89 bool CanResize() const override { return true; }
90 base::string16 GetWindowTitle() const override {
91 return base::ASCIIToUTF16("Modal Window");
93 ui::ModalType GetModalType() const override { return modal_type_; }
95 // Overridden from views::ButtonListener:
96 void ButtonPressed(views::Button* sender, const ui::Event& event) override {
97 DCHECK(sender == open_button_);
98 OpenModalWindow(GetWidget()->GetNativeView(), modal_type_);
102 ui::ModalType modal_type_;
104 views::LabelButton* open_button_;
106 DISALLOW_COPY_AND_ASSIGN(ModalWindow);
109 class NonModalTransient : public views::WidgetDelegateView {
112 : color_(g_colors[g_color_index]) {
113 ++g_color_index %= arraysize(g_colors);
115 ~NonModalTransient() override {}
117 static void OpenNonModalTransient(aura::Window* parent) {
118 views::Widget* widget =
119 views::Widget::CreateWindowWithParent(new NonModalTransient, parent);
120 widget->GetNativeView()->SetName("NonModalTransient");
124 static void ToggleNonModalTransient(aura::Window* parent) {
125 if (!non_modal_transient_) {
126 non_modal_transient_ =
127 views::Widget::CreateWindowWithParent(new NonModalTransient, parent);
128 non_modal_transient_->GetNativeView()->SetName("NonModalTransient");
130 if (non_modal_transient_->IsVisible())
131 non_modal_transient_->Hide();
133 non_modal_transient_->Show();
136 // Overridden from views::View:
137 void OnPaint(gfx::Canvas* canvas) override {
138 canvas->FillRect(GetLocalBounds(), color_);
140 gfx::Size GetPreferredSize() const override { return gfx::Size(250, 250); }
142 // Overridden from views::WidgetDelegate:
143 views::View* GetContentsView() override { return this; }
144 bool CanResize() const override { return true; }
145 base::string16 GetWindowTitle() const override {
146 return base::ASCIIToUTF16("Non-Modal Transient");
148 void DeleteDelegate() override {
149 if (GetWidget() == non_modal_transient_)
150 non_modal_transient_ = NULL;
158 static views::Widget* non_modal_transient_;
160 DISALLOW_COPY_AND_ASSIGN(NonModalTransient);
164 views::Widget* NonModalTransient::non_modal_transient_ = NULL;
166 void AddViewToLayout(views::GridLayout* layout, views::View* view) {
167 layout->StartRow(0, 0);
168 layout->AddView(view);
169 layout->AddPaddingRow(0, 5);
174 void InitWindowTypeLauncher() {
175 views::Widget* widget =
176 views::Widget::CreateWindowWithContextAndBounds(
177 new WindowTypeLauncher,
178 Shell::GetPrimaryRootWindow(),
179 gfx::Rect(120, 150, 300, 410));
180 widget->GetNativeView()->SetName("WindowTypeLauncher");
181 wm::SetShadowType(widget->GetNativeView(),
182 wm::SHADOW_TYPE_RECTANGULAR);
186 WindowTypeLauncher::WindowTypeLauncher()
187 : create_button_(new views::LabelButton(
188 this, base::ASCIIToUTF16("Create Window"))),
189 panel_button_(new views::LabelButton(
190 this, base::ASCIIToUTF16("Create Panel"))),
191 create_nonresizable_button_(new views::LabelButton(
192 this, base::ASCIIToUTF16("Create Non-Resizable Window"))),
193 bubble_button_(new views::LabelButton(
194 this, base::ASCIIToUTF16("Create Pointy Bubble"))),
195 lock_button_(new views::LabelButton(
196 this, base::ASCIIToUTF16("Lock Screen"))),
197 widgets_button_(new views::LabelButton(
198 this, base::ASCIIToUTF16("Show Example Widgets"))),
199 system_modal_button_(new views::LabelButton(
200 this, base::ASCIIToUTF16("Open System Modal Window"))),
201 window_modal_button_(new views::LabelButton(
202 this, base::ASCIIToUTF16("Open Window Modal Window"))),
203 child_modal_button_(new views::LabelButton(
204 this, base::ASCIIToUTF16("Open Child Modal Window"))),
205 transient_button_(new views::LabelButton(
206 this, base::ASCIIToUTF16("Open Non-Modal Transient Window"))),
207 examples_button_(new views::LabelButton(
208 this, base::ASCIIToUTF16("Open Views Examples Window"))),
209 show_hide_window_button_(new views::LabelButton(
210 this, base::ASCIIToUTF16("Show/Hide a Window"))),
211 show_screensaver_(new views::LabelButton(
212 this, base::ASCIIToUTF16("Show the Screensaver [for 5 seconds]"))),
213 show_web_notification_(new views::LabelButton(
214 this, base::ASCIIToUTF16("Show a web/app notification"))) {
215 create_button_->SetStyle(views::Button::STYLE_BUTTON);
216 panel_button_->SetStyle(views::Button::STYLE_BUTTON);
217 create_nonresizable_button_->SetStyle(views::Button::STYLE_BUTTON);
218 bubble_button_->SetStyle(views::Button::STYLE_BUTTON);
219 lock_button_->SetStyle(views::Button::STYLE_BUTTON);
220 widgets_button_->SetStyle(views::Button::STYLE_BUTTON);
221 system_modal_button_->SetStyle(views::Button::STYLE_BUTTON);
222 window_modal_button_->SetStyle(views::Button::STYLE_BUTTON);
223 child_modal_button_->SetStyle(views::Button::STYLE_BUTTON);
224 transient_button_->SetStyle(views::Button::STYLE_BUTTON);
225 examples_button_->SetStyle(views::Button::STYLE_BUTTON);
226 show_hide_window_button_->SetStyle(views::Button::STYLE_BUTTON);
227 show_screensaver_->SetStyle(views::Button::STYLE_BUTTON);
228 show_web_notification_->SetStyle(views::Button::STYLE_BUTTON);
230 views::GridLayout* layout = new views::GridLayout(this);
231 layout->SetInsets(5, 5, 5, 5);
232 SetLayoutManager(layout);
233 views::ColumnSet* column_set = layout->AddColumnSet(0);
234 column_set->AddColumn(views::GridLayout::LEADING,
235 views::GridLayout::CENTER,
237 views::GridLayout::USE_PREF,
240 AddViewToLayout(layout, create_button_);
241 AddViewToLayout(layout, panel_button_);
242 AddViewToLayout(layout, create_nonresizable_button_);
243 AddViewToLayout(layout, bubble_button_);
244 AddViewToLayout(layout, lock_button_);
245 AddViewToLayout(layout, widgets_button_);
246 AddViewToLayout(layout, system_modal_button_);
247 AddViewToLayout(layout, window_modal_button_);
248 AddViewToLayout(layout, child_modal_button_);
249 AddViewToLayout(layout, transient_button_);
250 AddViewToLayout(layout, examples_button_);
251 AddViewToLayout(layout, show_hide_window_button_);
252 AddViewToLayout(layout, show_screensaver_);
253 AddViewToLayout(layout, show_web_notification_);
254 set_context_menu_controller(this);
257 WindowTypeLauncher::~WindowTypeLauncher() {
260 void WindowTypeLauncher::OnPaint(gfx::Canvas* canvas) {
261 canvas->FillRect(GetLocalBounds(), SK_ColorWHITE);
264 bool WindowTypeLauncher::OnMousePressed(const ui::MouseEvent& event) {
265 // Overridden so we get OnMouseReleased and can show the context menu.
269 views::View* WindowTypeLauncher::GetContentsView() {
273 bool WindowTypeLauncher::CanResize() const {
277 base::string16 WindowTypeLauncher::GetWindowTitle() const {
278 return base::ASCIIToUTF16("Examples: Window Builder");
281 bool WindowTypeLauncher::CanMaximize() const {
285 bool WindowTypeLauncher::CanMinimize() const {
289 void WindowTypeLauncher::ButtonPressed(views::Button* sender,
290 const ui::Event& event) {
291 if (sender == create_button_) {
292 ToplevelWindow::CreateParams params;
293 params.can_resize = true;
294 params.can_maximize = true;
295 ToplevelWindow::CreateToplevelWindow(params);
296 } else if (sender == panel_button_) {
297 PanelWindow::CreatePanelWindow(gfx::Rect());
298 } else if (sender == create_nonresizable_button_) {
299 ToplevelWindow::CreateToplevelWindow(ToplevelWindow::CreateParams());
300 } else if (sender == bubble_button_) {
301 CreatePointyBubble(sender);
302 } else if (sender == lock_button_) {
303 Shell::GetInstance()->session_state_delegate()->LockScreen();
304 } else if (sender == widgets_button_) {
305 CreateWidgetsWindow();
306 } else if (sender == system_modal_button_) {
307 ModalWindow::OpenModalWindow(GetWidget()->GetNativeView(),
308 ui::MODAL_TYPE_SYSTEM);
309 } else if (sender == window_modal_button_) {
310 ModalWindow::OpenModalWindow(GetWidget()->GetNativeView(),
311 ui::MODAL_TYPE_WINDOW);
312 } else if (sender == child_modal_button_) {
313 ash::test::CreateChildModalParent(
314 GetWidget()->GetNativeView()->GetRootWindow());
315 } else if (sender == transient_button_) {
316 NonModalTransient::OpenNonModalTransient(GetWidget()->GetNativeView());
317 } else if (sender == show_hide_window_button_) {
318 NonModalTransient::ToggleNonModalTransient(GetWidget()->GetNativeView());
319 } else if (sender == show_screensaver_) {
320 ash::ShowScreensaver(GURL("http://www.google.com"));
321 content::BrowserThread::PostDelayedTask(content::BrowserThread::UI,
323 base::Bind(&ash::CloseScreensaver),
324 base::TimeDelta::FromSeconds(5));
326 } else if (sender == show_web_notification_) {
327 scoped_ptr<message_center::Notification> notification;
328 notification.reset(new message_center::Notification(
329 message_center::NOTIFICATION_TYPE_SIMPLE,
331 base::ASCIIToUTF16("Test Shell Web Notification"),
332 base::ASCIIToUTF16("Notification message body."),
334 base::ASCIIToUTF16("www.testshell.org"),
335 message_center::NotifierId(
336 message_center::NotifierId::APPLICATION, "test-id"),
337 message_center::RichNotificationData(),
338 NULL /* delegate */));
340 ash::Shell::GetPrimaryRootWindowController()->shelf()->status_area_widget()
341 ->web_notification_tray()->message_center()
342 ->AddNotification(notification.Pass());
343 } else if (sender == examples_button_) {
344 views::examples::ShowExamplesWindowWithContent(
345 views::examples::DO_NOTHING_ON_CLOSE,
346 Shell::GetInstance()->delegate()->GetActiveBrowserContext(),
351 void WindowTypeLauncher::ExecuteCommand(int id, int event_flags) {
353 case COMMAND_NEW_WINDOW:
354 InitWindowTypeLauncher();
356 case COMMAND_TOGGLE_FULLSCREEN:
357 GetWidget()->SetFullscreen(!GetWidget()->IsFullscreen());
364 void WindowTypeLauncher::ShowContextMenuForView(
366 const gfx::Point& point,
367 ui::MenuSourceType source_type) {
368 MenuItemView* root = new MenuItemView(this);
369 root->AppendMenuItem(COMMAND_NEW_WINDOW,
370 base::ASCIIToUTF16("New Window"),
371 MenuItemView::NORMAL);
372 root->AppendMenuItem(COMMAND_TOGGLE_FULLSCREEN,
373 base::ASCIIToUTF16("Toggle FullScreen"),
374 MenuItemView::NORMAL);
375 // MenuRunner takes ownership of root.
376 menu_runner_.reset(new MenuRunner(
377 root, MenuRunner::HAS_MNEMONICS | views::MenuRunner::CONTEXT_MENU));
378 if (menu_runner_->RunMenuAt(GetWidget(),
380 gfx::Rect(point, gfx::Size()),
381 views::MENU_ANCHOR_TOPLEFT,
382 source_type) == MenuRunner::MENU_DELETED) {