toplevel_window.cc revision 4e180b6a0b4720a9b8e9e959a882386f690f08ff
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 39void ToplevelWindow::CreateToplevelWindow(const CreateParams& params) { 40 views::Widget* widget = views::Widget::CreateWindowWithContext( 41 new ToplevelWindow(params), Shell::GetPrimaryRootWindow()); 42 widget->GetNativeView()->SetName("Examples:ToplevelWindow"); 43 wm::WindowState* window_state = wm::GetWindowState(widget->GetNativeView()); 44 window_state->set_window_position_managed(true); 45 widget->Show(); 46} 47 48ToplevelWindow::ToplevelWindow(const CreateParams& params) : params_(params) { 49} 50 51ToplevelWindow::~ToplevelWindow() { 52} 53 54void ToplevelWindow::OnPaint(gfx::Canvas* canvas) { 55 canvas->FillRect(GetLocalBounds(), SK_ColorDKGRAY); 56} 57 58base::string16 ToplevelWindow::GetWindowTitle() const { 59 return ASCIIToUTF16("Examples: Toplevel Window"); 60} 61 62void ToplevelWindow::SaveWindowPlacement(const gfx::Rect& bounds, 63 ui::WindowShowState show_state) { 64 if (!saved_state) 65 saved_state = new SavedState; 66 saved_state->bounds = bounds; 67 saved_state->show_state = show_state; 68} 69 70bool ToplevelWindow::GetSavedWindowPlacement( 71 gfx::Rect* bounds, 72 ui::WindowShowState* show_state) const { 73 bool is_saved_bounds = !!saved_state; 74 if (saved_state) { 75 *bounds = saved_state->bounds; 76 *show_state = saved_state->show_state; 77 } else { 78 // Initial default bounds. 79 bounds->SetRect(10, 150, 300, 300); 80 } 81 ash::WindowPositioner::GetBoundsAndShowStateForNewWindow( 82 ash::Shell::GetScreen(), 83 NULL, 84 is_saved_bounds, 85 *show_state, 86 bounds, 87 show_state); 88 return true; 89} 90 91views::View* ToplevelWindow::GetContentsView() { 92 return this; 93} 94 95bool ToplevelWindow::CanResize() const { 96 return params_.can_resize; 97} 98 99bool ToplevelWindow::CanMaximize() const { 100 return params_.can_maximize; 101} 102 103} // namespace shell 104} // namespace ash 105