toplevel_window.cc 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. #include "ash/test/toplevel_window.h"
  5. #include "ash/shell.h"
  6. #include "ash/wm/window_positioner.h"
  7. #include "ash/wm/window_state.h"
  8. #include "base/strings/utf_string_conversions.h"
  9. #include "ui/aura/window.h"
  10. #include "ui/aura/window_event_dispatcher.h"
  11. #include "ui/gfx/canvas.h"
  12. #include "ui/views/widget/widget.h"
  13. namespace ash {
  14. namespace shell {
  15. namespace {
  16. struct SavedState {
  17. gfx::Rect bounds;
  18. ui::WindowShowState show_state;
  19. };
  20. // The last window state in ash_shell. We don't bother deleting
  21. // this on shutdown.
  22. SavedState* saved_state = nullptr;
  23. } // namespace
  24. ToplevelWindow::CreateParams::CreateParams()
  25. : can_resize(false), can_maximize(false), use_saved_placement(true) {}
  26. // static
  27. views::Widget* ToplevelWindow::CreateToplevelWindow(
  28. const CreateParams& params) {
  29. views::Widget* widget = views::Widget::CreateWindowWithContext(
  30. new ToplevelWindow(params), Shell::GetPrimaryRootWindow());
  31. widget->GetNativeView()->SetName("Examples:ToplevelWindow");
  32. WindowState* window_state = WindowState::Get(widget->GetNativeView());
  33. window_state->SetWindowPositionManaged(true);
  34. widget->Show();
  35. return widget;
  36. }
  37. // static
  38. void ToplevelWindow::ClearSavedStateForTest() {
  39. delete saved_state;
  40. saved_state = nullptr;
  41. }
  42. ToplevelWindow::ToplevelWindow(const CreateParams& params)
  43. : use_saved_placement_(params.use_saved_placement) {
  44. SetCanMaximize(params.can_maximize);
  45. SetCanMinimize(params.can_maximize);
  46. SetCanResize(params.can_resize);
  47. SetTitle(u"Examples: Toplevel Window");
  48. }
  49. ToplevelWindow::~ToplevelWindow() = default;
  50. void ToplevelWindow::OnPaint(gfx::Canvas* canvas) {
  51. canvas->FillRect(GetLocalBounds(), SK_ColorDKGRAY);
  52. }
  53. bool ToplevelWindow::ShouldSaveWindowPlacement() const {
  54. return true;
  55. }
  56. void ToplevelWindow::SaveWindowPlacement(const gfx::Rect& bounds,
  57. ui::WindowShowState show_state) {
  58. if (!saved_state)
  59. saved_state = new SavedState;
  60. saved_state->bounds = bounds;
  61. saved_state->show_state = show_state;
  62. }
  63. bool ToplevelWindow::GetSavedWindowPlacement(
  64. const views::Widget* widget,
  65. gfx::Rect* bounds,
  66. ui::WindowShowState* show_state) const {
  67. bool is_saved_bounds = !!saved_state;
  68. if (saved_state && use_saved_placement_) {
  69. *bounds = saved_state->bounds;
  70. *show_state = saved_state->show_state;
  71. } else {
  72. // Initial default bounds.
  73. bounds->SetRect(10, 150, 300, 300);
  74. }
  75. WindowPositioner::GetBoundsAndShowStateForNewWindow(
  76. is_saved_bounds, *show_state, bounds, show_state);
  77. return true;
  78. }
  79. } // namespace shell
  80. } // namespace ash