toplevel_window.cc revision 1e9bf3e0803691d0a228da41fc608347b6db4340
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. 4 5#include "ash/shell/toplevel_window.h" 6 7#include "ash/display/display_controller.h" 8#include "ash/screen_ash.h" 9#include "ash/shell.h" 10#include "ash/wm/window_positioner.h" 11#include "ash/wm/window_state.h" 12#include "base/strings/utf_string_conversions.h" 13#include "ui/aura/root_window.h" 14#include "ui/aura/window.h" 15#include "ui/gfx/canvas.h" 16#include "ui/views/widget/widget.h" 17 18namespace ash { 19namespace shell { 20namespace { 21 22struct SavedState { 23 gfx::Rect bounds; 24 ui::WindowShowState show_state; 25}; 26 27// The last window state in ash_shell. We don't bother deleting 28// this on shutdown. 29SavedState* saved_state = NULL; 30 31} // namespace 32 33ToplevelWindow::CreateParams::CreateParams() 34 : can_resize(false), 35 can_maximize(false) { 36} 37 38// static 39views::Widget* ToplevelWindow::CreateToplevelWindow( 40 const CreateParams& params) { 41 views::Widget* widget = views::Widget::CreateWindowWithContext( 42 new ToplevelWindow(params), Shell::GetPrimaryRootWindow()); 43 widget->GetNativeView()->SetName("Examples:ToplevelWindow"); 44 wm::WindowState* window_state = wm::GetWindowState(widget->GetNativeView()); 45 window_state->set_window_position_managed(true); 46 widget->Show(); 47 return widget; 48} 49 50// static 51void ToplevelWindow::ClearSavedStateForTest() { 52 delete saved_state; 53 saved_state = NULL; 54} 55 56ToplevelWindow::ToplevelWindow(const CreateParams& params) : params_(params) { 57} 58 59ToplevelWindow::~ToplevelWindow() { 60} 61 62void ToplevelWindow::OnPaint(gfx::Canvas* canvas) { 63 canvas->FillRect(GetLocalBounds(), SK_ColorDKGRAY); 64} 65 66base::string16 ToplevelWindow::GetWindowTitle() const { 67 return ASCIIToUTF16("Examples: Toplevel Window"); 68} 69 70void ToplevelWindow::SaveWindowPlacement(const gfx::Rect& bounds, 71 ui::WindowShowState show_state) { 72 if (!saved_state) 73 saved_state = new SavedState; 74 saved_state->bounds = bounds; 75 saved_state->show_state = show_state; 76} 77 78bool ToplevelWindow::GetSavedWindowPlacement( 79 gfx::Rect* bounds, 80 ui::WindowShowState* show_state) const { 81 bool is_saved_bounds = !!saved_state; 82 if (saved_state) { 83 *bounds = saved_state->bounds; 84 *show_state = saved_state->show_state; 85 } else { 86 // Initial default bounds. 87 bounds->SetRect(10, 150, 300, 300); 88 } 89 ash::WindowPositioner::GetBoundsAndShowStateForNewWindow( 90 ash::Shell::GetScreen(), 91 NULL, 92 is_saved_bounds, 93 *show_state, 94 bounds, 95 show_state); 96 return true; 97} 98 99views::View* ToplevelWindow::GetContentsView() { 100 return this; 101} 102 103bool ToplevelWindow::CanResize() const { 104 return params_.can_resize; 105} 106 107bool ToplevelWindow::CanMaximize() const { 108 return params_.can_maximize; 109} 110 111} // namespace shell 112} // namespace ash 113