OLD | NEW |
| (Empty) |
1 // Copyright 2014 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 "mojo/examples/wm_flow/wm/frame_controller.h" | |
6 | |
7 #include "base/macros.h" | |
8 #include "base/strings/utf_string_conversions.h" | |
9 #include "mojo/converters/geometry/geometry_type_converters.h" | |
10 #include "mojo/services/public/cpp/view_manager/view.h" | |
11 #include "mojo/services/window_manager/window_manager_app.h" | |
12 #include "mojo/views/native_widget_view_manager.h" | |
13 #include "ui/views/background.h" | |
14 #include "ui/views/controls/button/label_button.h" | |
15 #include "ui/views/layout/layout_manager.h" | |
16 #include "ui/views/widget/widget.h" | |
17 #include "ui/wm/public/activation_client.h" | |
18 | |
19 class FrameController::LayoutManager : public views::LayoutManager, | |
20 public views::ButtonListener { | |
21 public: | |
22 explicit LayoutManager(FrameController* controller) | |
23 : controller_(controller), | |
24 close_button_( | |
25 new views::LabelButton(this, base::ASCIIToUTF16("Begone"))), | |
26 maximize_button_( | |
27 new views::LabelButton(this, base::ASCIIToUTF16("Embiggen"))) {} | |
28 virtual ~LayoutManager() {} | |
29 | |
30 private: | |
31 static const int kButtonFrameMargin = 5; | |
32 static const int kButtonFrameSpacing = 2; | |
33 static const int kFrameSize = 10; | |
34 | |
35 // Overridden from views::LayoutManager: | |
36 virtual void Installed(views::View* host) override { | |
37 host->AddChildView(close_button_); | |
38 host->AddChildView(maximize_button_); | |
39 } | |
40 virtual void Layout(views::View* host) override { | |
41 gfx::Size ps = close_button_->GetPreferredSize(); | |
42 gfx::Rect bounds = host->GetLocalBounds(); | |
43 close_button_->SetBounds(bounds.right() - kButtonFrameMargin - ps.width(), | |
44 kButtonFrameMargin, ps.width(), ps.height()); | |
45 | |
46 ps = maximize_button_->GetPreferredSize(); | |
47 maximize_button_->SetBounds( | |
48 close_button_->x() - kButtonFrameSpacing - ps.width(), | |
49 kButtonFrameMargin, ps.width(), ps.height()); | |
50 | |
51 bounds.Inset(kFrameSize, | |
52 close_button_->bounds().bottom() + kButtonFrameMargin, | |
53 kFrameSize, kFrameSize); | |
54 controller_->app_view_->SetBounds(*mojo::Rect::From(bounds)); | |
55 } | |
56 virtual gfx::Size GetPreferredSize(const views::View* host) const override { | |
57 return gfx::Size(); | |
58 } | |
59 | |
60 // Overridden from views::ButtonListener: | |
61 virtual void ButtonPressed(views::Button* sender, | |
62 const ui::Event& event) override { | |
63 if (sender == close_button_) | |
64 controller_->CloseWindow(); | |
65 else if (sender == maximize_button_) | |
66 controller_->ToggleMaximize(); | |
67 } | |
68 | |
69 FrameController* controller_; | |
70 views::Button* close_button_; | |
71 views::Button* maximize_button_; | |
72 | |
73 DISALLOW_COPY_AND_ASSIGN(LayoutManager); | |
74 }; | |
75 | |
76 class FrameController::FrameEventHandler : public ui::EventHandler { | |
77 public: | |
78 explicit FrameEventHandler(FrameController* frame_controller) | |
79 : frame_controller_(frame_controller) {} | |
80 virtual ~FrameEventHandler() {} | |
81 | |
82 private: | |
83 | |
84 // Overriden from ui::EventHandler: | |
85 virtual void OnMouseEvent(ui::MouseEvent* event) override { | |
86 if (event->type() == ui::ET_MOUSE_PRESSED) | |
87 frame_controller_->ActivateWindow(); | |
88 } | |
89 | |
90 FrameController* frame_controller_; | |
91 | |
92 DISALLOW_COPY_AND_ASSIGN(FrameEventHandler); | |
93 }; | |
94 | |
95 //////////////////////////////////////////////////////////////////////////////// | |
96 // FrameController, public: | |
97 | |
98 FrameController::FrameController( | |
99 mojo::Shell* shell, | |
100 mojo::View* view, | |
101 mojo::View** app_view, | |
102 aura::client::ActivationClient* activation_client, | |
103 mojo::WindowManagerApp* window_manager_app) | |
104 : view_(view), | |
105 app_view_(mojo::View::Create(view->view_manager())), | |
106 frame_view_(new views::View), | |
107 frame_view_layout_manager_(new LayoutManager(this)), | |
108 widget_(new views::Widget), | |
109 maximized_(false), | |
110 activation_client_(activation_client), | |
111 window_manager_app_(window_manager_app) { | |
112 view_->AddChild(app_view_); | |
113 view_->AddObserver(this); | |
114 *app_view = app_view_; | |
115 frame_view_->set_background( | |
116 views::Background::CreateSolidBackground(SK_ColorBLUE)); | |
117 frame_view_->SetLayoutManager(frame_view_layout_manager_); | |
118 frame_event_handler_.reset(new FrameEventHandler(this)); | |
119 frame_view_->AddPreTargetHandler(frame_event_handler_.get()); | |
120 views::Widget::InitParams params( | |
121 views::Widget::InitParams::TYPE_WINDOW_FRAMELESS); | |
122 params.native_widget = | |
123 new mojo::NativeWidgetViewManager(widget_, shell, view_); | |
124 params.bounds = gfx::Rect( | |
125 0, 0, view_->bounds().width, view_->bounds().height); | |
126 widget_->Init(params); | |
127 widget_->SetContentsView(frame_view_); | |
128 widget_->Show(); | |
129 } | |
130 | |
131 FrameController::~FrameController() {} | |
132 | |
133 void FrameController::CloseWindow() { | |
134 app_view_->Destroy(); | |
135 view_->Destroy(); | |
136 } | |
137 | |
138 void FrameController::ToggleMaximize() { | |
139 if (!maximized_) | |
140 restored_bounds_ = view_->bounds().To<gfx::Rect>(); | |
141 maximized_ = !maximized_; | |
142 if (maximized_) | |
143 view_->SetBounds(view_->parent()->bounds()); | |
144 else | |
145 view_->SetBounds(*mojo::Rect::From(restored_bounds_)); | |
146 } | |
147 | |
148 void FrameController::ActivateWindow() { | |
149 aura::Window* window = window_manager_app_->GetWindowForViewId(view_->id()); | |
150 activation_client_->ActivateWindow(window); | |
151 } | |
152 | |
153 //////////////////////////////////////////////////////////////////////////////// | |
154 // FrameController, mojo::ViewObserver implementation: | |
155 | |
156 void FrameController::OnViewDestroyed(mojo::View* view) { | |
157 view_->RemoveObserver(this); | |
158 delete this; | |
159 } | |
OLD | NEW |