Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(36)

Side by Side Diff: mojo/examples/window_manager/window_manager.cc

Issue 383123006: Preliminary interactive layout of window manager's demo_launcher (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: OK Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 "base/basictypes.h" 5 #include "base/basictypes.h"
6 #include "base/bind.h" 6 #include "base/bind.h"
7 #include "mojo/examples/keyboard/keyboard.mojom.h" 7 #include "mojo/examples/keyboard/keyboard.mojom.h"
8 #include "mojo/examples/window_manager/debug_panel.h" 8 #include "mojo/examples/window_manager/debug_panel.h"
9 #include "mojo/examples/window_manager/window_manager.mojom.h" 9 #include "mojo/examples/window_manager/window_manager.mojom.h"
10 #include "mojo/public/cpp/application/application_connection.h" 10 #include "mojo/public/cpp/application/application_connection.h"
11 #include "mojo/public/cpp/application/application_delegate.h" 11 #include "mojo/public/cpp/application/application_delegate.h"
12 #include "mojo/public/cpp/application/application_impl.h" 12 #include "mojo/public/cpp/application/application_impl.h"
13 #include "mojo/services/public/cpp/geometry/geometry_type_converters.h" 13 #include "mojo/services/public/cpp/geometry/geometry_type_converters.h"
14 #include "mojo/services/public/cpp/input_events/input_events_type_converters.h" 14 #include "mojo/services/public/cpp/input_events/input_events_type_converters.h"
15 #include "mojo/services/public/cpp/view_manager/node.h" 15 #include "mojo/services/public/cpp/view_manager/node.h"
16 #include "mojo/services/public/cpp/view_manager/node_observer.h" 16 #include "mojo/services/public/cpp/view_manager/node_observer.h"
17 #include "mojo/services/public/cpp/view_manager/view.h" 17 #include "mojo/services/public/cpp/view_manager/view.h"
18 #include "mojo/services/public/cpp/view_manager/view_event_dispatcher.h" 18 #include "mojo/services/public/cpp/view_manager/view_event_dispatcher.h"
19 #include "mojo/services/public/cpp/view_manager/view_manager.h" 19 #include "mojo/services/public/cpp/view_manager/view_manager.h"
20 #include "mojo/services/public/cpp/view_manager/view_manager_delegate.h" 20 #include "mojo/services/public/cpp/view_manager/view_manager_delegate.h"
21 #include "mojo/services/public/interfaces/input_events/input_events.mojom.h" 21 #include "mojo/services/public/interfaces/input_events/input_events.mojom.h"
22 #include "mojo/services/public/interfaces/launcher/launcher.mojom.h" 22 #include "mojo/services/public/interfaces/launcher/launcher.mojom.h"
23 #include "mojo/services/public/interfaces/navigation/navigation.mojom.h" 23 #include "mojo/services/public/interfaces/navigation/navigation.mojom.h"
24 #include "mojo/views/views_init.h" 24 #include "mojo/views/views_init.h"
25 #include "ui/events/event.h" 25 #include "ui/events/event.h"
26 #include "ui/events/event_constants.h" 26 #include "ui/events/event_constants.h"
27 #include "ui/gfx/geometry/size_conversions.h"
27 28
28 #if defined CreateWindow 29 #if defined CreateWindow
29 #undef CreateWindow 30 #undef CreateWindow
30 #endif 31 #endif
31 32
32 using mojo::view_manager::Id; 33 using mojo::view_manager::Id;
33 using mojo::view_manager::Node; 34 using mojo::view_manager::Node;
34 using mojo::view_manager::NodeObserver; 35 using mojo::view_manager::NodeObserver;
35 using mojo::view_manager::View; 36 using mojo::view_manager::View;
36 using mojo::view_manager::ViewEventDispatcher; 37 using mojo::view_manager::ViewEventDispatcher;
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
83 const mojo::String& url) OVERRIDE; 84 const mojo::String& url) OVERRIDE;
84 virtual void RequestNavigate( 85 virtual void RequestNavigate(
85 uint32 source_node_id, 86 uint32 source_node_id,
86 navigation::Target target, 87 navigation::Target target,
87 navigation::NavigationDetailsPtr nav_details) OVERRIDE; 88 navigation::NavigationDetailsPtr nav_details) OVERRIDE;
88 WindowManager* window_manager_; 89 WindowManager* window_manager_;
89 90
90 DISALLOW_COPY_AND_ASSIGN(NavigatorHost); 91 DISALLOW_COPY_AND_ASSIGN(NavigatorHost);
91 }; 92 };
92 93
93 class KeyboardManager : public KeyboardClient { 94 class KeyboardManager : public KeyboardClient,
95 public NodeObserver {
94 public: 96 public:
95 KeyboardManager() : view_manager_(NULL), node_(NULL) { 97 KeyboardManager() : view_manager_(NULL), node_(NULL) {
96 } 98 }
97 virtual ~KeyboardManager() { 99 virtual ~KeyboardManager() {
100 if (node_)
sky 2014/07/15 17:47:49 node_ is never set to NULL, so I'm not sure this c
hansmuller 2014/07/15 20:07:33 OK
101 node_->parent()->RemoveObserver(this);
98 } 102 }
99 103
100 Node* node() { return node_; } 104 Node* node() { return node_; }
101 105
102 void Init(ApplicationImpl* application, 106 void Init(ApplicationImpl* application,
103 ViewManager* view_manager, 107 ViewManager* view_manager,
104 Node* parent, 108 Node* parent,
105 const gfx::Rect& bounds) { 109 const gfx::Rect& bounds) {
106 view_manager_ = view_manager; 110 view_manager_ = view_manager;
107 node_ = Node::Create(view_manager); 111 node_ = Node::Create(view_manager);
108 node_->SetBounds(bounds); 112 node_->SetBounds(bounds);
109 parent->AddChild(node_); 113 parent->AddChild(node_);
110 node_->Embed("mojo:mojo_keyboard"); 114 node_->Embed("mojo:mojo_keyboard");
111 application->ConnectToService("mojo:mojo_keyboard", &keyboard_service_); 115 application->ConnectToService("mojo:mojo_keyboard", &keyboard_service_);
112 keyboard_service_.set_client(this); 116 keyboard_service_.set_client(this);
117 parent->AddObserver(this);
113 } 118 }
114 119
115 void Show(Id view_id, const gfx::Rect& bounds) { 120 void Show(Id view_id, const gfx::Rect& bounds) {
116 keyboard_service_->SetTarget(view_id); 121 keyboard_service_->SetTarget(view_id);
117 node_->SetVisible(true); 122 node_->SetVisible(true);
118 } 123 }
119 124
120 void Hide(Id view_id) { 125 void Hide(Id view_id) {
121 keyboard_service_->SetTarget(0); 126 keyboard_service_->SetTarget(0);
122 node_->SetVisible(false); 127 node_->SetVisible(false);
(...skipping 17 matching lines...) Expand all
140 Event::From(ui::KeyEvent(ui::ET_KEY_PRESSED, 145 Event::From(ui::KeyEvent(ui::ET_KEY_PRESSED,
141 static_cast<ui::KeyboardCode>(code), 146 static_cast<ui::KeyboardCode>(code),
142 flags, is_char))); 147 flags, is_char)));
143 view_manager_->DispatchEvent( 148 view_manager_->DispatchEvent(
144 view, 149 view,
145 Event::From(ui::KeyEvent(ui::ET_KEY_RELEASED, 150 Event::From(ui::KeyEvent(ui::ET_KEY_RELEASED,
146 static_cast<ui::KeyboardCode>(code), 151 static_cast<ui::KeyboardCode>(code),
147 flags, false))); 152 flags, false)));
148 } 153 }
149 154
155 // Overridden from NodeObserver:
156 virtual void OnNodeBoundsChanged(Node* parent,
157 const gfx::Rect& old_bounds,
158 const gfx::Rect& new_bounds) OVERRIDE {
159 gfx::Rect keyboard_bounds(node_->bounds());
160 keyboard_bounds.set_y(new_bounds.bottom() - keyboard_bounds.height());
161 keyboard_bounds.set_width(keyboard_bounds.width() +
162 new_bounds.width() - old_bounds.width());
163 node_->SetBounds(keyboard_bounds);
164 }
165 virtual void OnNodeDestroyed(Node* parent) OVERRIDE {
166 DCHECK_EQ(parent, node_->parent());
167 parent->RemoveObserver(this);
168 }
169
150 KeyboardServicePtr keyboard_service_; 170 KeyboardServicePtr keyboard_service_;
151 ViewManager* view_manager_; 171 ViewManager* view_manager_;
152 172
153 // Node the keyboard is attached to. 173 // Node the keyboard is attached to.
154 Node* node_; 174 Node* node_;
155 175
156 DISALLOW_COPY_AND_ASSIGN(KeyboardManager); 176 DISALLOW_COPY_AND_ASSIGN(KeyboardManager);
157 }; 177 };
158 178
159 class RootLayoutManager : public NodeObserver { 179 class RootLayoutManager : public NodeObserver {
160 public: 180 public:
161 explicit RootLayoutManager(ViewManager* view_manager, 181 RootLayoutManager(ViewManager* view_manager,
162 Node* root, 182 Node* root,
163 Id content_node_id) 183 Id content_node_id,
164 : root_(root), 184 Id launcher_ui_node_id,
165 view_manager_(view_manager), 185 Id control_panel_node_id)
166 content_node_id_(content_node_id) {} 186 : root_(root),
167 virtual ~RootLayoutManager() {} 187 view_manager_(view_manager),
188 content_node_id_(content_node_id),
189 launcher_ui_node_id_(launcher_ui_node_id),
190 control_panel_node_id_(control_panel_node_id) {}
191 virtual ~RootLayoutManager() {
192 if (root_)
193 root_->RemoveObserver(this);
194 }
168 195
169 private: 196 private:
170 // Overridden from NodeObserver: 197 // Overridden from NodeObserver:
171 virtual void OnNodeBoundsChanged(Node* node, 198 virtual void OnNodeBoundsChanged(Node* node,
172 const gfx::Rect& /*old_bounds*/, 199 const gfx::Rect& old_bounds,
173 const gfx::Rect& new_bounds) OVERRIDE { 200 const gfx::Rect& new_bounds) OVERRIDE {
174 DCHECK_EQ(node, root_); 201 DCHECK_EQ(node, root_);
202
175 Node* content_node = view_manager_->GetNodeById(content_node_id_); 203 Node* content_node = view_manager_->GetNodeById(content_node_id_);
176 content_node->SetBounds(new_bounds); 204 content_node->SetBounds(new_bounds);
177 // Force the view's bitmap to be recreated 205 // Force the view's bitmap to be recreated
178 content_node->active_view()->SetColor(SK_ColorBLUE); 206 content_node->active_view()->SetColor(SK_ColorBLUE);
179 // TODO(hansmuller): Do Layout 207
208 int delta_width = new_bounds.width() - old_bounds.width();
209 int delta_height = new_bounds.height() - old_bounds.height();
210
211 Node* launcher_ui_node =
212 view_manager_->GetNodeById(launcher_ui_node_id_);
213 gfx::Rect launcher_ui_bounds(launcher_ui_node->bounds());
214 launcher_ui_bounds.set_width(launcher_ui_bounds.width() + delta_width);
215 launcher_ui_node->SetBounds(launcher_ui_bounds);
216
217 Node* control_panel_node =
218 view_manager_->GetNodeById(control_panel_node_id_);
219 gfx::Rect control_panel_bounds(control_panel_node->bounds());
220 control_panel_bounds.set_x(control_panel_bounds.x() + delta_width);
221 control_panel_node->SetBounds(control_panel_bounds);
222
223 const Node::Children& content_nodes = content_node->children();
224 Node::Children::const_iterator iter = content_nodes.begin();
225 for(; iter != content_nodes.end(); ++iter) {
226 Node* node = *iter;
227 if (node->id() == control_panel_node->id() ||
228 node->id() == launcher_ui_node->id())
229 continue;
230 gfx::Rect node_bounds(node->bounds());
231 node_bounds.set_width(node_bounds.width() + delta_width);
232 node_bounds.set_height(node_bounds.height() + delta_height);
233 node->SetBounds(node_bounds);
234 }
235 }
236 virtual void OnNodeDestroyed(Node* node) OVERRIDE {
237 DCHECK_EQ(node, root_);
238 root_->RemoveObserver(this);
239 root_ = NULL;
180 } 240 }
181 241
182 Node* root_; 242 Node* root_;
183 ViewManager* view_manager_; 243 ViewManager* view_manager_;
184 Id content_node_id_; 244 const Id content_node_id_;
245 const Id launcher_ui_node_id_;
246 const Id control_panel_node_id_;
185 247
186 DISALLOW_COPY_AND_ASSIGN(RootLayoutManager); 248 DISALLOW_COPY_AND_ASSIGN(RootLayoutManager);
187 }; 249 };
188 250
189 class WindowManager : public ApplicationDelegate, 251 class WindowManager : public ApplicationDelegate,
190 public DebugPanel::Delegate, 252 public DebugPanel::Delegate,
191 public ViewManagerDelegate, 253 public ViewManagerDelegate,
192 public ViewEventDispatcher { 254 public ViewEventDispatcher {
193 public: 255 public:
194 WindowManager() 256 WindowManager()
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
276 virtual void OnRootAdded(ViewManager* view_manager, Node* root) OVERRIDE { 338 virtual void OnRootAdded(ViewManager* view_manager, Node* root) OVERRIDE {
277 DCHECK(!view_manager_); 339 DCHECK(!view_manager_);
278 view_manager_ = view_manager; 340 view_manager_ = view_manager;
279 view_manager_->SetEventDispatcher(this); 341 view_manager_->SetEventDispatcher(this);
280 342
281 Node* node = Node::Create(view_manager_); 343 Node* node = Node::Create(view_manager_);
282 root->AddChild(node); 344 root->AddChild(node);
283 node->SetBounds(gfx::Rect(root->bounds().size())); 345 node->SetBounds(gfx::Rect(root->bounds().size()));
284 content_node_id_ = node->id(); 346 content_node_id_ = node->id();
285 347
286 root_layout_manager_.reset(
287 new RootLayoutManager(view_manager_, root, content_node_id_));
288 root->AddObserver(root_layout_manager_.get());
289
290 View* view = View::Create(view_manager_); 348 View* view = View::Create(view_manager_);
291 node->SetActiveView(view); 349 node->SetActiveView(view);
292 view->SetColor(SK_ColorBLUE); 350 view->SetColor(SK_ColorBLUE);
293 351
294 CreateLauncherUI(); 352 Id launcher_ui_id = CreateLauncherUI();
295 CreateControlPanel(node); 353 Id control_panel_id = CreateControlPanel(node);
354
355 root_layout_manager_.reset(
356 new RootLayoutManager(view_manager, root,
357 content_node_id_,
358 launcher_ui_id,
359 control_panel_id));
360 root->AddObserver(root_layout_manager_.get());
296 } 361 }
297 virtual void OnViewManagerDisconnected(ViewManager* view_manager) OVERRIDE { 362 virtual void OnViewManagerDisconnected(ViewManager* view_manager) OVERRIDE {
298 DCHECK_EQ(view_manager_, view_manager); 363 DCHECK_EQ(view_manager_, view_manager);
299 view_manager_ = NULL; 364 view_manager_ = NULL;
300 base::MessageLoop::current()->Quit(); 365 base::MessageLoop::current()->Quit();
301 } 366 }
302 367
303 // Overridden from ViewEventDispatcher: 368 // Overridden from ViewEventDispatcher:
304 virtual void DispatchEvent(View* target, EventPtr event) OVERRIDE { 369 virtual void DispatchEvent(View* target, EventPtr event) OVERRIDE {
305 // TODO(beng): More sophisticated focus handling than this is required! 370 // TODO(beng): More sophisticated focus handling than this is required!
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
342 dest_node = windows_.back(); 407 dest_node = windows_.back();
343 } 408 }
344 409
345 if (dest_node) 410 if (dest_node)
346 Embed(dest_node, handler_url, nav_details.Pass(), response.Pass()); 411 Embed(dest_node, handler_url, nav_details.Pass(), response.Pass());
347 else 412 else
348 CreateWindow(handler_url, nav_details.Pass(), response.Pass()); 413 CreateWindow(handler_url, nav_details.Pass(), response.Pass());
349 } 414 }
350 415
351 // TODO(beng): proper layout manager!! 416 // TODO(beng): proper layout manager!!
352 void CreateLauncherUI() { 417 Id CreateLauncherUI() {
353 navigation::NavigationDetailsPtr nav_details; 418 navigation::NavigationDetailsPtr nav_details;
354 navigation::ResponseDetailsPtr response; 419 navigation::ResponseDetailsPtr response;
355 Node* node = view_manager_->GetNodeById(content_node_id_); 420 Node* node = view_manager_->GetNodeById(content_node_id_);
356 gfx::Rect bounds = node->bounds(); 421 gfx::Rect bounds = node->bounds();
357 bounds.Inset(kBorderInset, kBorderInset); 422 bounds.Inset(kBorderInset, kBorderInset);
358 bounds.set_height(kTextfieldHeight); 423 bounds.set_height(kTextfieldHeight);
359 launcher_ui_ = CreateChild(content_node_id_, "mojo:mojo_browser", bounds, 424 launcher_ui_ = CreateChild(content_node_id_, "mojo:mojo_browser", bounds,
360 nav_details.Pass(), response.Pass()); 425 nav_details.Pass(), response.Pass());
426 return launcher_ui_->id();
361 } 427 }
362 428
363 void CreateWindow(const std::string& handler_url, 429 void CreateWindow(const std::string& handler_url,
364 navigation::NavigationDetailsPtr nav_details, 430 navigation::NavigationDetailsPtr nav_details,
365 navigation::ResponseDetailsPtr response) { 431 navigation::ResponseDetailsPtr response) {
366 Node* node = view_manager_->GetNodeById(content_node_id_); 432 Node* node = view_manager_->GetNodeById(content_node_id_);
367 gfx::Rect bounds(kBorderInset, 433 gfx::Rect bounds(kBorderInset,
368 2 * kBorderInset + kTextfieldHeight, 434 2 * kBorderInset + kTextfieldHeight,
369 node->bounds().width() - 3 * kBorderInset - 435 node->bounds().width() - 3 * kBorderInset -
370 kControlPanelWidth, 436 kControlPanelWidth,
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
402 app_->ConnectToService(app_url, &navigator); 468 app_->ConnectToService(app_url, &navigator);
403 navigator->Navigate(node->id(), nav_details.Pass(), response.Pass()); 469 navigator->Navigate(node->id(), nav_details.Pass(), response.Pass());
404 } 470 }
405 } 471 }
406 472
407 bool IsDescendantOfKeyboard(View* target) { 473 bool IsDescendantOfKeyboard(View* target) {
408 return keyboard_manager_.get() && 474 return keyboard_manager_.get() &&
409 keyboard_manager_->node()->Contains(target->node()); 475 keyboard_manager_->node()->Contains(target->node());
410 } 476 }
411 477
412 void CreateControlPanel(view_manager::Node* root) { 478 Id CreateControlPanel(view_manager::Node* root) {
413 Node* node = Node::Create(view_manager_); 479 Node* node = Node::Create(view_manager_);
414 View* view = view_manager::View::Create(view_manager_); 480 View* view = view_manager::View::Create(view_manager_);
415 root->AddChild(node); 481 root->AddChild(node);
416 node->SetActiveView(view); 482 node->SetActiveView(view);
417 483
418 gfx::Rect bounds(root->bounds().width() - kControlPanelWidth - 484 gfx::Rect bounds(root->bounds().width() - kControlPanelWidth -
419 kBorderInset, 485 kBorderInset,
420 kBorderInset * 2 + kTextfieldHeight, 486 kBorderInset * 2 + kTextfieldHeight,
421 kControlPanelWidth, 487 kControlPanelWidth,
422 root->bounds().height() - kBorderInset * 3 - 488 root->bounds().height() - kBorderInset * 3 -
423 kTextfieldHeight); 489 kTextfieldHeight);
424 node->SetBounds(bounds); 490 node->SetBounds(bounds);
425 491
426 debug_panel_ = new DebugPanel(this, node); 492 debug_panel_ = new DebugPanel(this, node);
493 return node->id();
427 } 494 }
428 495
429 scoped_ptr<ViewsInit> views_init_; 496 scoped_ptr<ViewsInit> views_init_;
430 DebugPanel* debug_panel_; 497 DebugPanel* debug_panel_;
431 launcher::LauncherPtr launcher_; 498 launcher::LauncherPtr launcher_;
432 Node* launcher_ui_; 499 Node* launcher_ui_;
433 std::vector<Node*> windows_; 500 std::vector<Node*> windows_;
434 ViewManager* view_manager_; 501 ViewManager* view_manager_;
435 scoped_ptr<RootLayoutManager> root_layout_manager_; 502 scoped_ptr<RootLayoutManager> root_layout_manager_;
436 503
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
468 } 535 }
469 536
470 } // namespace examples 537 } // namespace examples
471 538
472 // static 539 // static
473 ApplicationDelegate* ApplicationDelegate::Create() { 540 ApplicationDelegate* ApplicationDelegate::Create() {
474 return new examples::WindowManager; 541 return new examples::WindowManager;
475 } 542 }
476 543
477 } // namespace mojo 544 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698