OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "examples/wm_flow/wm/frame_controller.h" | 5 #include "examples/wm_flow/wm/frame_controller.h" |
6 | 6 |
7 #include "base/macros.h" | 7 #include "base/macros.h" |
8 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
9 #include "mojo/converters/geometry/geometry_type_converters.h" | 9 #include "mojo/converters/geometry/geometry_type_converters.h" |
| 10 #include "mojo/public/cpp/application/service_provider_impl.h" |
10 #include "mojo/services/view_manager/public/cpp/view.h" | 11 #include "mojo/services/view_manager/public/cpp/view.h" |
11 #include "mojo/services/view_manager/public/cpp/view_manager.h" | 12 #include "mojo/services/view_manager/public/cpp/view_manager.h" |
12 #include "mojo/views/native_widget_mojo.h" | |
13 #include "services/window_manager/capture_controller.h" | 13 #include "services/window_manager/capture_controller.h" |
14 #include "services/window_manager/window_manager_app.h" | 14 #include "services/window_manager/window_manager_app.h" |
15 #include "ui/views/background.h" | 15 #include "url/gurl.h" |
16 #include "ui/views/controls/button/checkbox.h" | |
17 #include "ui/views/controls/button/label_button.h" | |
18 #include "ui/views/layout/layout_manager.h" | |
19 #include "ui/views/widget/widget.h" | |
20 #include "ui/wm/public/activation_client.h" | |
21 | |
22 class FrameController::LayoutManager : public views::LayoutManager, | |
23 public views::ButtonListener { | |
24 public: | |
25 explicit LayoutManager(FrameController* controller) | |
26 : controller_(controller), | |
27 capture_checkbox_( | |
28 new views::Checkbox(base::ASCIIToUTF16("Capture"))), | |
29 close_button_( | |
30 new views::LabelButton(this, base::ASCIIToUTF16("Begone"))), | |
31 maximize_button_( | |
32 new views::LabelButton(this, base::ASCIIToUTF16("Embiggen"))) { | |
33 capture_checkbox_->set_listener(this); | |
34 } | |
35 virtual ~LayoutManager() {} | |
36 | |
37 private: | |
38 static const int kButtonFrameMargin = 5; | |
39 static const int kButtonFrameSpacing = 2; | |
40 static const int kFrameSize = 10; | |
41 | |
42 // Overridden from views::LayoutManager: | |
43 virtual void Installed(views::View* host) override { | |
44 host->AddChildView(capture_checkbox_); | |
45 host->AddChildView(close_button_); | |
46 host->AddChildView(maximize_button_); | |
47 } | |
48 virtual void Layout(views::View* host) override { | |
49 gfx::Size ps = capture_checkbox_->GetPreferredSize(); | |
50 capture_checkbox_->SetBounds(kButtonFrameMargin, kButtonFrameMargin, | |
51 ps.width(), ps.height()); | |
52 | |
53 ps = close_button_->GetPreferredSize(); | |
54 gfx::Rect bounds = host->GetLocalBounds(); | |
55 close_button_->SetBounds(bounds.right() - kButtonFrameMargin - ps.width(), | |
56 kButtonFrameMargin, ps.width(), ps.height()); | |
57 | |
58 ps = maximize_button_->GetPreferredSize(); | |
59 maximize_button_->SetBounds( | |
60 close_button_->x() - kButtonFrameSpacing - ps.width(), | |
61 kButtonFrameMargin, ps.width(), ps.height()); | |
62 | |
63 bounds.Inset(kFrameSize, | |
64 close_button_->bounds().bottom() + kButtonFrameMargin, | |
65 kFrameSize, kFrameSize); | |
66 controller_->app_view_->SetBounds(*mojo::Rect::From(bounds)); | |
67 } | |
68 virtual gfx::Size GetPreferredSize(const views::View* host) const override { | |
69 return gfx::Size(); | |
70 } | |
71 | |
72 // Overridden from views::ButtonListener: | |
73 virtual void ButtonPressed(views::Button* sender, | |
74 const ui::Event& event) override { | |
75 if (sender == close_button_) | |
76 controller_->CloseWindow(); | |
77 else if (sender == maximize_button_) | |
78 controller_->ToggleMaximize(); | |
79 else if (sender == capture_checkbox_) | |
80 controller_->SetCapture(capture_checkbox_->checked()); | |
81 } | |
82 | |
83 FrameController* controller_; | |
84 views::Checkbox* capture_checkbox_; | |
85 views::Button* close_button_; | |
86 views::Button* maximize_button_; | |
87 | |
88 DISALLOW_COPY_AND_ASSIGN(LayoutManager); | |
89 }; | |
90 | |
91 class FrameController::FrameEventHandler : public ui::EventHandler { | |
92 public: | |
93 explicit FrameEventHandler(FrameController* frame_controller) | |
94 : frame_controller_(frame_controller) {} | |
95 virtual ~FrameEventHandler() {} | |
96 | |
97 private: | |
98 | |
99 // Overriden from ui::EventHandler: | |
100 virtual void OnMouseEvent(ui::MouseEvent* event) override { | |
101 if (event->type() == ui::ET_MOUSE_PRESSED) | |
102 frame_controller_->ActivateWindow(); | |
103 } | |
104 | |
105 FrameController* frame_controller_; | |
106 | |
107 DISALLOW_COPY_AND_ASSIGN(FrameEventHandler); | |
108 }; | |
109 | 16 |
110 //////////////////////////////////////////////////////////////////////////////// | 17 //////////////////////////////////////////////////////////////////////////////// |
111 // FrameController, public: | 18 // FrameController, public: |
112 | 19 |
113 FrameController::FrameController( | 20 FrameController::FrameController( |
114 mojo::Shell* shell, | 21 const GURL& frame_app_url, |
115 mojo::View* view, | 22 mojo::View* view, |
116 mojo::View** app_view, | 23 mojo::View** app_view, |
117 window_manager::WindowManagerApp* window_manager_app) | 24 window_manager::WindowManagerApp* window_manager_app) |
118 : view_(view), | 25 : view_(view), |
119 app_view_(view->view_manager()->CreateView()), | 26 app_view_(view->view_manager()->CreateView()), |
120 frame_view_(new views::View), | |
121 frame_view_layout_manager_(new LayoutManager(this)), | |
122 widget_(new views::Widget), | |
123 maximized_(false), | 27 maximized_(false), |
124 window_manager_app_(window_manager_app) { | 28 window_manager_app_(window_manager_app), |
| 29 binding_(this) { |
| 30 view_->AddObserver(this); |
| 31 view_->SetVisible(true); // FIXME: This should not be our responsibility? |
| 32 *app_view = app_view_; |
| 33 |
| 34 scoped_ptr<mojo::ServiceProviderImpl> exported_services( |
| 35 new mojo::ServiceProviderImpl()); |
| 36 exported_services->AddService(this); |
| 37 |
| 38 viewer_services_ = |
| 39 view_->Embed(frame_app_url.spec(), exported_services.Pass()); |
| 40 |
| 41 // We weren't observing when our initial bounds was set: |
| 42 OnViewBoundsChanged(view, view->bounds(), view->bounds()); |
| 43 |
| 44 // Add the child view after embedding sky, since embed clears children. |
125 view_->AddChild(app_view_); | 45 view_->AddChild(app_view_); |
126 app_view_->SetVisible(true); | 46 app_view_->SetVisible(true); |
127 view_->AddObserver(this); | |
128 *app_view = app_view_; | |
129 frame_view_->set_background( | |
130 views::Background::CreateSolidBackground(SK_ColorBLUE)); | |
131 frame_view_->SetLayoutManager(frame_view_layout_manager_); | |
132 frame_event_handler_.reset(new FrameEventHandler(this)); | |
133 frame_view_->AddPreTargetHandler(frame_event_handler_.get()); | |
134 views::Widget::InitParams params( | |
135 views::Widget::InitParams::TYPE_WINDOW_FRAMELESS); | |
136 params.native_widget = new mojo::NativeWidgetMojo(widget_, shell, view_); | |
137 params.bounds = view_->bounds().To<gfx::Rect>(); | |
138 widget_->Init(params); | |
139 widget_->SetContentsView(frame_view_); | |
140 widget_->Show(); | |
141 } | 47 } |
142 | 48 |
143 FrameController::~FrameController() {} | 49 FrameController::~FrameController() {} |
144 | 50 |
| 51 void FrameController::Create( |
| 52 mojo::ApplicationConnection* connection, |
| 53 mojo::InterfaceRequest<examples::WindowFrameHost> request) { |
| 54 binding_.Bind(request.Pass()); |
| 55 } |
| 56 |
145 void FrameController::CloseWindow() { | 57 void FrameController::CloseWindow() { |
146 // This destroys |app_view_| as it is a child of |view_|. | 58 // This destroys |app_view_| as it is a child of |view_|. |
147 view_->Destroy(); | 59 view_->Destroy(); |
148 } | 60 } |
149 | 61 |
150 void FrameController::ToggleMaximize() { | 62 void FrameController::ToggleMaximize() { |
151 if (!maximized_) | 63 if (!maximized_) |
152 restored_bounds_ = view_->bounds().To<gfx::Rect>(); | 64 restored_bounds_ = view_->bounds().To<gfx::Rect>(); |
153 maximized_ = !maximized_; | 65 maximized_ = !maximized_; |
154 if (maximized_) | 66 if (maximized_) |
(...skipping 13 matching lines...) Expand all Loading... |
168 window_manager_app_->capture_controller()->ReleaseCapture(view_); | 80 window_manager_app_->capture_controller()->ReleaseCapture(view_); |
169 } | 81 } |
170 | 82 |
171 //////////////////////////////////////////////////////////////////////////////// | 83 //////////////////////////////////////////////////////////////////////////////// |
172 // FrameController, mojo::ViewObserver implementation: | 84 // FrameController, mojo::ViewObserver implementation: |
173 | 85 |
174 void FrameController::OnViewDestroyed(mojo::View* view) { | 86 void FrameController::OnViewDestroyed(mojo::View* view) { |
175 view_->RemoveObserver(this); | 87 view_->RemoveObserver(this); |
176 delete this; | 88 delete this; |
177 } | 89 } |
| 90 |
| 91 void FrameController::OnViewBoundsChanged(mojo::View* view, |
| 92 const mojo::Rect& old_bounds, |
| 93 const mojo::Rect& new_bounds) { |
| 94 CHECK(view == view_); |
| 95 // Statically size the embedded view. Unclear if we should use a |
| 96 // sky iframe to participate in sky layout or not. |
| 97 const int kTopControlsAdditionalInset = 15; |
| 98 const int kDefaultInset = 25; |
| 99 mojo::Rect bounds; |
| 100 bounds.x = bounds.y = kDefaultInset; |
| 101 bounds.y += kTopControlsAdditionalInset; |
| 102 bounds.width = view_->bounds().width - kDefaultInset * 2; |
| 103 bounds.height = |
| 104 view_->bounds().height - kDefaultInset * 2 - kTopControlsAdditionalInset; |
| 105 app_view_->SetBounds(bounds); |
| 106 } |
OLD | NEW |