| OLD | NEW | 
 | (Empty) | 
|    1 // Copyright (c) 2011 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 "ui/views/desktop/desktop_window_view.h" |  | 
|    6  |  | 
|    7 #include "base/utf_string_conversions.h" |  | 
|    8 #include "ui/gfx/canvas.h" |  | 
|    9 #include "ui/gfx/compositor/layer.h" |  | 
|   10 #include "ui/gfx/compositor/layer_animator.h" |  | 
|   11 #include "ui/gfx/transform.h" |  | 
|   12 #include "ui/views/desktop/desktop_background.h" |  | 
|   13 #include "ui/views/desktop/desktop_window_manager.h" |  | 
|   14 #include "ui/views/window/native_frame_view.h" |  | 
|   15 #include "views/widget/native_widget_view.h" |  | 
|   16 #include "views/widget/native_widget_views.h" |  | 
|   17 #include "views/widget/widget.h" |  | 
|   18  |  | 
|   19 #if defined(USE_AURA) |  | 
|   20 #include "views/widget/native_widget_aura.h" |  | 
|   21 #elif defined(OS_WIN) |  | 
|   22 #include "views/widget/native_widget_win.h" |  | 
|   23 #elif defined(USE_WAYLAND) |  | 
|   24 #include "views/widget/native_widget_wayland.h" |  | 
|   25 #elif defined(TOOLKIT_USES_GTK) |  | 
|   26 #include "views/widget/native_widget_gtk.h" |  | 
|   27 #endif |  | 
|   28  |  | 
|   29 namespace views { |  | 
|   30 namespace desktop { |  | 
|   31  |  | 
|   32 // The Widget that hosts the DesktopWindowView. Subclasses Widget to override |  | 
|   33 // CreateRootView() so that the DesktopWindowRootView can be supplied instead |  | 
|   34 // for custom event filtering. |  | 
|   35 class DesktopWindow : public Widget { |  | 
|   36  public: |  | 
|   37   explicit DesktopWindow(DesktopWindowView* desktop_window_view) |  | 
|   38       : desktop_window_view_(desktop_window_view) {} |  | 
|   39   virtual ~DesktopWindow() {} |  | 
|   40  |  | 
|   41  private: |  | 
|   42   // Overridden from Widget: |  | 
|   43   virtual bool OnKeyEvent(const KeyEvent& event) OVERRIDE { |  | 
|   44     return WindowManager::Get()->HandleKeyEvent(this, event); |  | 
|   45   } |  | 
|   46  |  | 
|   47   virtual bool OnMouseEvent(const MouseEvent& event) OVERRIDE { |  | 
|   48     return WindowManager::Get()->HandleMouseEvent(this, event) || |  | 
|   49         Widget::OnMouseEvent(event); |  | 
|   50   } |  | 
|   51  |  | 
|   52   virtual ui::TouchStatus OnTouchEvent(const TouchEvent& event) OVERRIDE { |  | 
|   53     ui::TouchStatus status = WindowManager::Get()-> |  | 
|   54         HandleTouchEvent(this, event); |  | 
|   55     if (status == ui::TOUCH_STATUS_UNKNOWN) |  | 
|   56       status = Widget::OnTouchEvent(event); |  | 
|   57     return status; |  | 
|   58   } |  | 
|   59  |  | 
|   60   DesktopWindowView* desktop_window_view_; |  | 
|   61  |  | 
|   62   DISALLOW_COPY_AND_ASSIGN(DesktopWindow); |  | 
|   63 }; |  | 
|   64  |  | 
|   65 class TestWindowContentView : public WidgetDelegateView { |  | 
|   66  public: |  | 
|   67   TestWindowContentView(const string16& title, SkColor color) |  | 
|   68       : title_(title), |  | 
|   69         color_(color) { |  | 
|   70   } |  | 
|   71   virtual ~TestWindowContentView() {} |  | 
|   72  |  | 
|   73  private: |  | 
|   74   // Overridden from View: |  | 
|   75   virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE { |  | 
|   76     canvas->FillRect(color_, GetLocalBounds()); |  | 
|   77   } |  | 
|   78  |  | 
|   79   // Overridden from WindowDelegate: |  | 
|   80   virtual string16 GetWindowTitle() const OVERRIDE { |  | 
|   81     return title_; |  | 
|   82   } |  | 
|   83   virtual View* GetContentsView() { |  | 
|   84     return this; |  | 
|   85   } |  | 
|   86   virtual bool CanMaximize() const OVERRIDE { |  | 
|   87     return true; |  | 
|   88   } |  | 
|   89   virtual bool OnMousePressed(const MouseEvent& event) OVERRIDE { |  | 
|   90     Widget* widget = View::GetWidget(); |  | 
|   91     if (widget->IsMinimized()) |  | 
|   92       widget->Restore(); |  | 
|   93     else |  | 
|   94       widget->Minimize(); |  | 
|   95     return true; |  | 
|   96   } |  | 
|   97  |  | 
|   98   string16 title_; |  | 
|   99   SkColor color_; |  | 
|  100  |  | 
|  101   DISALLOW_COPY_AND_ASSIGN(TestWindowContentView); |  | 
|  102 }; |  | 
|  103  |  | 
|  104 //////////////////////////////////////////////////////////////////////////////// |  | 
|  105 // DesktopWindowView, public: |  | 
|  106  |  | 
|  107 // static |  | 
|  108 DesktopWindowView* DesktopWindowView::desktop_window_view = NULL; |  | 
|  109  |  | 
|  110 DesktopWindowView::DesktopWindowView(DesktopType type) |  | 
|  111     : type_(type) { |  | 
|  112   switch (type_) { |  | 
|  113     case DESKTOP_DEFAULT: |  | 
|  114     case DESKTOP_NETBOOK: |  | 
|  115       set_background(new DesktopBackground); |  | 
|  116       break; |  | 
|  117     case DESKTOP_OTHER: |  | 
|  118       set_background(Background::CreateStandardPanelBackground()); |  | 
|  119       break; |  | 
|  120   } |  | 
|  121 } |  | 
|  122  |  | 
|  123 DesktopWindowView::~DesktopWindowView() { |  | 
|  124 } |  | 
|  125  |  | 
|  126 // static |  | 
|  127 void DesktopWindowView::CreateDesktopWindow(DesktopType type) { |  | 
|  128   DCHECK(!desktop_window_view); |  | 
|  129   desktop_window_view = new DesktopWindowView(type); |  | 
|  130   views::Widget* window = new DesktopWindow(desktop_window_view); |  | 
|  131   desktop_window_view->widget_ = window; |  | 
|  132  |  | 
|  133   WindowManager::Install(new DesktopWindowManager(window)); |  | 
|  134  |  | 
|  135   views::Widget::InitParams params; |  | 
|  136   params.delegate = desktop_window_view; |  | 
|  137   // In this environment, CreateChromeWindow will default to creating a views- |  | 
|  138   // window, so we need to construct a NativeWidgetWin by hand. |  | 
|  139   // TODO(beng): Replace this with NativeWindow::CreateNativeRootWindow(). |  | 
|  140 #if defined(USE_AURA) |  | 
|  141   params.native_widget = new views::NativeWidgetAura(window); |  | 
|  142 #elif defined(OS_WIN) |  | 
|  143   params.native_widget = new views::NativeWidgetWin(window); |  | 
|  144 #elif defined(USE_WAYLAND) |  | 
|  145   params.native_widget = new views::NativeWidgetWayland(window); |  | 
|  146 #elif defined(TOOLKIT_USES_GTK) |  | 
|  147   params.native_widget = new views::NativeWidgetGtk(window); |  | 
|  148   params.show_state = ui::SHOW_STATE_MAXIMIZED; |  | 
|  149 #endif |  | 
|  150   params.bounds = gfx::Rect(20, 20, 1920, 1200); |  | 
|  151   window->Init(params); |  | 
|  152   window->Show(); |  | 
|  153 } |  | 
|  154  |  | 
|  155 void DesktopWindowView::CreateTestWindow(const string16& title, |  | 
|  156                                          SkColor color, |  | 
|  157                                          gfx::Rect initial_bounds, |  | 
|  158                                          bool rotate) { |  | 
|  159   views::Widget* window = views::Widget::CreateWindowWithBounds( |  | 
|  160       new TestWindowContentView(title, color), |  | 
|  161       initial_bounds); |  | 
|  162   window->Show(); |  | 
|  163  |  | 
|  164   NativeWidgetViews* native_widget_views = |  | 
|  165       static_cast<NativeWidgetViews*>(window->native_widget()); |  | 
|  166  |  | 
|  167   if (rotate) { |  | 
|  168     ui::Transform transform; |  | 
|  169     transform.SetRotate(90.0f); |  | 
|  170     transform.SetTranslateX(window->GetWindowScreenBounds().width()); |  | 
|  171     native_widget_views->GetView()->SetTransform(transform); |  | 
|  172   } |  | 
|  173  |  | 
|  174   native_widget_views->GetView()->SetPaintToLayer(true); |  | 
|  175   if (native_widget_views->GetView()->layer()) { |  | 
|  176     native_widget_views->GetView()->layer()->SetAnimator( |  | 
|  177         ui::LayerAnimator::CreateImplicitAnimator()); |  | 
|  178   } |  | 
|  179 } |  | 
|  180  |  | 
|  181 void DesktopWindowView::AddObserver(DesktopWindowView::Observer* observer) { |  | 
|  182   observers_.AddObserver(observer); |  | 
|  183 } |  | 
|  184  |  | 
|  185 void DesktopWindowView::RemoveObserver(DesktopWindowView::Observer* observer) { |  | 
|  186   observers_.RemoveObserver(observer); |  | 
|  187 } |  | 
|  188  |  | 
|  189 bool DesktopWindowView::HasObserver(DesktopWindowView::Observer* observer) { |  | 
|  190   return observers_.HasObserver(observer); |  | 
|  191 } |  | 
|  192  |  | 
|  193 //////////////////////////////////////////////////////////////////////////////// |  | 
|  194 // DesktopWindowView, View overrides: |  | 
|  195  |  | 
|  196 void DesktopWindowView::Layout() { |  | 
|  197 } |  | 
|  198  |  | 
|  199 void DesktopWindowView::OnBoundsChanged(const gfx::Rect& previous_bounds) { |  | 
|  200   static_cast<DesktopWindowManager*>(WindowManager::Get())-> |  | 
|  201       UpdateWindowsAfterScreenSizeChanged(bounds()); |  | 
|  202  |  | 
|  203   FOR_EACH_OBSERVER(Observer, observers_, |  | 
|  204                     OnDesktopBoundsChanged(previous_bounds)); |  | 
|  205 } |  | 
|  206  |  | 
|  207 void DesktopWindowView::ViewHierarchyChanged( |  | 
|  208     bool is_add, View* parent, View* child) { |  | 
|  209   if (child->GetClassName() == internal::NativeWidgetView::kViewClassName) { |  | 
|  210     Widget* widget = |  | 
|  211         static_cast<internal::NativeWidgetView*>(child)->GetAssociatedWidget(); |  | 
|  212     if (is_add) |  | 
|  213       WindowManager::Get()->Register(widget); |  | 
|  214   } |  | 
|  215 } |  | 
|  216  |  | 
|  217 //////////////////////////////////////////////////////////////////////////////// |  | 
|  218 // DesktopWindowView, WidgetDelegate implementation: |  | 
|  219  |  | 
|  220 Widget* DesktopWindowView::GetWidget() { |  | 
|  221   return widget_; |  | 
|  222 } |  | 
|  223  |  | 
|  224 const Widget* DesktopWindowView::GetWidget() const { |  | 
|  225   return widget_; |  | 
|  226 } |  | 
|  227  |  | 
|  228 bool DesktopWindowView::CanResize() const { |  | 
|  229   return true; |  | 
|  230 } |  | 
|  231  |  | 
|  232 bool DesktopWindowView::CanMaximize() const { |  | 
|  233   return CanResize(); |  | 
|  234 } |  | 
|  235  |  | 
|  236 string16 DesktopWindowView::GetWindowTitle() const { |  | 
|  237   return ASCIIToUTF16("Aura Desktop"); |  | 
|  238 } |  | 
|  239  |  | 
|  240 SkBitmap DesktopWindowView::GetWindowAppIcon() { |  | 
|  241   return SkBitmap(); |  | 
|  242 } |  | 
|  243  |  | 
|  244 SkBitmap DesktopWindowView::GetWindowIcon() { |  | 
|  245   return SkBitmap(); |  | 
|  246 } |  | 
|  247  |  | 
|  248 bool DesktopWindowView::ShouldShowWindowIcon() const { |  | 
|  249   return false; |  | 
|  250 } |  | 
|  251  |  | 
|  252 void DesktopWindowView::WindowClosing() { |  | 
|  253   MessageLoopForUI::current()->Quit(); |  | 
|  254 } |  | 
|  255  |  | 
|  256 View* DesktopWindowView::GetContentsView() { |  | 
|  257   return this; |  | 
|  258 } |  | 
|  259  |  | 
|  260 NonClientFrameView* DesktopWindowView::CreateNonClientFrameView() { |  | 
|  261   switch (type_) { |  | 
|  262     case DESKTOP_DEFAULT: |  | 
|  263     case DESKTOP_NETBOOK: |  | 
|  264       return NULL; |  | 
|  265  |  | 
|  266     case DESKTOP_OTHER: |  | 
|  267       return new NativeFrameView(widget_); |  | 
|  268   } |  | 
|  269   return NULL; |  | 
|  270 } |  | 
|  271  |  | 
|  272 //////////////////////////////////////////////////////////////////////////////// |  | 
|  273 // ui::LayerAnimationObserver Implementation: |  | 
|  274  |  | 
|  275 void DesktopWindowView::OnLayerAnimationEnded( |  | 
|  276     const ui::LayerAnimationSequence* animation) { |  | 
|  277   // The layer, and all the observers should be notified of the |  | 
|  278   // transformed size of the desktop. |  | 
|  279   if (widget_) { |  | 
|  280     gfx::Rect current_bounds(widget_->GetClientAreaScreenBounds().size()); |  | 
|  281     layer()->transform().TransformRect(¤t_bounds); |  | 
|  282     SetBoundsRect(gfx::Rect(current_bounds.size())); |  | 
|  283   } |  | 
|  284 } |  | 
|  285  |  | 
|  286 void DesktopWindowView::OnLayerAnimationScheduled( |  | 
|  287     const ui::LayerAnimationSequence* animation) { |  | 
|  288 } |  | 
|  289  |  | 
|  290 void DesktopWindowView::OnLayerAnimationAborted( |  | 
|  291     const ui::LayerAnimationSequence* animation) { |  | 
|  292 } |  | 
|  293  |  | 
|  294 }  // namespace desktop |  | 
|  295 }  // namespace views |  | 
| OLD | NEW |