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

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: make NativeWidgetViewManager observer cleanup consistent with other classes 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_)
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
166 // Overridden from NodeObserver:
167 virtual void OnNodeDestroying(Node* parent) OVERRIDE {
168 DCHECK_EQ(parent, node_->parent());
169 parent->RemoveObserver(this);
170 }
171
150 KeyboardServicePtr keyboard_service_; 172 KeyboardServicePtr keyboard_service_;
151 ViewManager* view_manager_; 173 ViewManager* view_manager_;
152 174
153 // Node the keyboard is attached to. 175 // Node the keyboard is attached to.
154 Node* node_; 176 Node* node_;
155 177
156 DISALLOW_COPY_AND_ASSIGN(KeyboardManager); 178 DISALLOW_COPY_AND_ASSIGN(KeyboardManager);
157 }; 179 };
158 180
159 class RootLayoutManager : public NodeObserver { 181 class RootLayoutManager : public NodeObserver {
160 public: 182 public:
161 explicit RootLayoutManager(ViewManager* view_manager, 183 RootLayoutManager(ViewManager* view_manager,
162 Node* root, 184 Node* root,
163 Id content_node_id) 185 Id content_node_id,
164 : root_(root), 186 Id launcher_ui_node_id,
165 view_manager_(view_manager), 187 Id control_panel_node_id)
166 content_node_id_(content_node_id) {} 188 : root_(root),
167 virtual ~RootLayoutManager() {} 189 view_manager_(view_manager),
190 content_node_id_(content_node_id),
191 launcher_ui_node_id_(launcher_ui_node_id),
192 control_panel_node_id_(control_panel_node_id) {}
193 virtual ~RootLayoutManager() {
194 root_->RemoveObserver(this);
195 }
168 196
169 private: 197 private:
170 // Overridden from NodeObserver: 198 // Overridden from NodeObserver:
171 virtual void OnNodeBoundsChanged(Node* node, 199 virtual void OnNodeBoundsChanged(Node* node,
172 const gfx::Rect& /*old_bounds*/, 200 const gfx::Rect& old_bounds,
173 const gfx::Rect& new_bounds) OVERRIDE { 201 const gfx::Rect& new_bounds) OVERRIDE {
174 DCHECK_EQ(node, root_); 202 DCHECK_EQ(node, root_);
203
175 Node* content_node = view_manager_->GetNodeById(content_node_id_); 204 Node* content_node = view_manager_->GetNodeById(content_node_id_);
176 content_node->SetBounds(new_bounds); 205 content_node->SetBounds(new_bounds);
177 // Force the view's bitmap to be recreated 206 // Force the view's bitmap to be recreated
178 content_node->active_view()->SetColor(SK_ColorBLUE); 207 content_node->active_view()->SetColor(SK_ColorBLUE);
179 // TODO(hansmuller): Do Layout 208
209 int delta_width = new_bounds.width() - old_bounds.width();
210 int delta_height = new_bounds.height() - old_bounds.height();
211
212 Node* launcher_ui_node =
213 view_manager_->GetNodeById(launcher_ui_node_id_);
214 gfx::Rect launcher_ui_bounds(launcher_ui_node->bounds());
215 launcher_ui_bounds.set_width(launcher_ui_bounds.width() + delta_width);
216 launcher_ui_node->SetBounds(launcher_ui_bounds);
217
218 Node* control_panel_node =
219 view_manager_->GetNodeById(control_panel_node_id_);
220 gfx::Rect control_panel_bounds(control_panel_node->bounds());
221 control_panel_bounds.set_x(control_panel_bounds.x() + delta_width);
222 control_panel_node->SetBounds(control_panel_bounds);
223
224 const Node::Children& content_nodes = content_node->children();
225 Node::Children::const_iterator iter = content_nodes.begin();
226 for(; iter != content_nodes.end(); ++iter) {
227 Node* node = *iter;
228 if (node->id() == control_panel_node->id() ||
229 node->id() == launcher_ui_node->id())
230 continue;
231 gfx::Rect node_bounds(node->bounds());
232 node_bounds.set_width(node_bounds.width() + delta_width);
233 node_bounds.set_height(node_bounds.height() + delta_height);
234 node->SetBounds(node_bounds);
235 }
236 }
237
238 // Overridden from NodeObserver:
239 virtual void OnNodeDestroying(Node* node) OVERRIDE {
240 DCHECK_EQ(node, root_);
241 root_->RemoveObserver(this);
180 } 242 }
181 243
182 Node* root_; 244 Node* root_;
183 ViewManager* view_manager_; 245 ViewManager* view_manager_;
184 Id content_node_id_; 246 const Id content_node_id_;
247 const Id launcher_ui_node_id_;
248 const Id control_panel_node_id_;
185 249
186 DISALLOW_COPY_AND_ASSIGN(RootLayoutManager); 250 DISALLOW_COPY_AND_ASSIGN(RootLayoutManager);
187 }; 251 };
188 252
189 class WindowManager : public ApplicationDelegate, 253 class WindowManager : public ApplicationDelegate,
190 public DebugPanel::Delegate, 254 public DebugPanel::Delegate,
191 public ViewManagerDelegate, 255 public ViewManagerDelegate,
192 public ViewEventDispatcher { 256 public ViewEventDispatcher {
193 public: 257 public:
194 WindowManager() 258 WindowManager()
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
276 virtual void OnRootAdded(ViewManager* view_manager, Node* root) OVERRIDE { 340 virtual void OnRootAdded(ViewManager* view_manager, Node* root) OVERRIDE {
277 DCHECK(!view_manager_); 341 DCHECK(!view_manager_);
278 view_manager_ = view_manager; 342 view_manager_ = view_manager;
279 view_manager_->SetEventDispatcher(this); 343 view_manager_->SetEventDispatcher(this);
280 344
281 Node* node = Node::Create(view_manager_); 345 Node* node = Node::Create(view_manager_);
282 root->AddChild(node); 346 root->AddChild(node);
283 node->SetBounds(gfx::Rect(root->bounds().size())); 347 node->SetBounds(gfx::Rect(root->bounds().size()));
284 content_node_id_ = node->id(); 348 content_node_id_ = node->id();
285 349
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_); 350 View* view = View::Create(view_manager_);
291 node->SetActiveView(view); 351 node->SetActiveView(view);
292 view->SetColor(SK_ColorBLUE); 352 view->SetColor(SK_ColorBLUE);
293 353
294 CreateLauncherUI(); 354 Id launcher_ui_id = CreateLauncherUI();
295 CreateControlPanel(node); 355 Id control_panel_id = CreateControlPanel(node);
356
357 root_layout_manager_.reset(
358 new RootLayoutManager(view_manager, root,
359 content_node_id_,
360 launcher_ui_id,
361 control_panel_id));
362 root->AddObserver(root_layout_manager_.get());
296 } 363 }
297 virtual void OnViewManagerDisconnected(ViewManager* view_manager) OVERRIDE { 364 virtual void OnViewManagerDisconnected(ViewManager* view_manager) OVERRIDE {
298 DCHECK_EQ(view_manager_, view_manager); 365 DCHECK_EQ(view_manager_, view_manager);
299 view_manager_ = NULL; 366 view_manager_ = NULL;
300 base::MessageLoop::current()->Quit(); 367 base::MessageLoop::current()->Quit();
301 } 368 }
302 369
303 // Overridden from ViewEventDispatcher: 370 // Overridden from ViewEventDispatcher:
304 virtual void DispatchEvent(View* target, EventPtr event) OVERRIDE { 371 virtual void DispatchEvent(View* target, EventPtr event) OVERRIDE {
305 // TODO(beng): More sophisticated focus handling than this is required! 372 // 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(); 409 dest_node = windows_.back();
343 } 410 }
344 411
345 if (dest_node) 412 if (dest_node)
346 Embed(dest_node, handler_url, nav_details.Pass(), response.Pass()); 413 Embed(dest_node, handler_url, nav_details.Pass(), response.Pass());
347 else 414 else
348 CreateWindow(handler_url, nav_details.Pass(), response.Pass()); 415 CreateWindow(handler_url, nav_details.Pass(), response.Pass());
349 } 416 }
350 417
351 // TODO(beng): proper layout manager!! 418 // TODO(beng): proper layout manager!!
352 void CreateLauncherUI() { 419 Id CreateLauncherUI() {
353 navigation::NavigationDetailsPtr nav_details; 420 navigation::NavigationDetailsPtr nav_details;
354 navigation::ResponseDetailsPtr response; 421 navigation::ResponseDetailsPtr response;
355 Node* node = view_manager_->GetNodeById(content_node_id_); 422 Node* node = view_manager_->GetNodeById(content_node_id_);
356 gfx::Rect bounds = node->bounds(); 423 gfx::Rect bounds = node->bounds();
357 bounds.Inset(kBorderInset, kBorderInset); 424 bounds.Inset(kBorderInset, kBorderInset);
358 bounds.set_height(kTextfieldHeight); 425 bounds.set_height(kTextfieldHeight);
359 launcher_ui_ = CreateChild(content_node_id_, "mojo:mojo_browser", bounds, 426 launcher_ui_ = CreateChild(content_node_id_, "mojo:mojo_browser", bounds,
360 nav_details.Pass(), response.Pass()); 427 nav_details.Pass(), response.Pass());
428 return launcher_ui_->id();
361 } 429 }
362 430
363 void CreateWindow(const std::string& handler_url, 431 void CreateWindow(const std::string& handler_url,
364 navigation::NavigationDetailsPtr nav_details, 432 navigation::NavigationDetailsPtr nav_details,
365 navigation::ResponseDetailsPtr response) { 433 navigation::ResponseDetailsPtr response) {
366 Node* node = view_manager_->GetNodeById(content_node_id_); 434 Node* node = view_manager_->GetNodeById(content_node_id_);
367 gfx::Rect bounds(kBorderInset, 435 gfx::Rect bounds(kBorderInset,
368 2 * kBorderInset + kTextfieldHeight, 436 2 * kBorderInset + kTextfieldHeight,
369 node->bounds().width() - 3 * kBorderInset - 437 node->bounds().width() - 3 * kBorderInset -
370 kControlPanelWidth, 438 kControlPanelWidth,
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
402 app_->ConnectToService(app_url, &navigator); 470 app_->ConnectToService(app_url, &navigator);
403 navigator->Navigate(node->id(), nav_details.Pass(), response.Pass()); 471 navigator->Navigate(node->id(), nav_details.Pass(), response.Pass());
404 } 472 }
405 } 473 }
406 474
407 bool IsDescendantOfKeyboard(View* target) { 475 bool IsDescendantOfKeyboard(View* target) {
408 return keyboard_manager_.get() && 476 return keyboard_manager_.get() &&
409 keyboard_manager_->node()->Contains(target->node()); 477 keyboard_manager_->node()->Contains(target->node());
410 } 478 }
411 479
412 void CreateControlPanel(view_manager::Node* root) { 480 Id CreateControlPanel(view_manager::Node* root) {
413 Node* node = Node::Create(view_manager_); 481 Node* node = Node::Create(view_manager_);
414 View* view = view_manager::View::Create(view_manager_); 482 View* view = view_manager::View::Create(view_manager_);
415 root->AddChild(node); 483 root->AddChild(node);
416 node->SetActiveView(view); 484 node->SetActiveView(view);
417 485
418 gfx::Rect bounds(root->bounds().width() - kControlPanelWidth - 486 gfx::Rect bounds(root->bounds().width() - kControlPanelWidth -
419 kBorderInset, 487 kBorderInset,
420 kBorderInset * 2 + kTextfieldHeight, 488 kBorderInset * 2 + kTextfieldHeight,
421 kControlPanelWidth, 489 kControlPanelWidth,
422 root->bounds().height() - kBorderInset * 3 - 490 root->bounds().height() - kBorderInset * 3 -
423 kTextfieldHeight); 491 kTextfieldHeight);
424 node->SetBounds(bounds); 492 node->SetBounds(bounds);
425 493
426 debug_panel_ = new DebugPanel(this, node); 494 debug_panel_ = new DebugPanel(this, node);
495 return node->id();
427 } 496 }
428 497
429 scoped_ptr<ViewsInit> views_init_; 498 scoped_ptr<ViewsInit> views_init_;
430 DebugPanel* debug_panel_; 499 DebugPanel* debug_panel_;
431 launcher::LauncherPtr launcher_; 500 launcher::LauncherPtr launcher_;
432 Node* launcher_ui_; 501 Node* launcher_ui_;
433 std::vector<Node*> windows_; 502 std::vector<Node*> windows_;
434 ViewManager* view_manager_; 503 ViewManager* view_manager_;
435 scoped_ptr<RootLayoutManager> root_layout_manager_; 504 scoped_ptr<RootLayoutManager> root_layout_manager_;
436 505
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
468 } 537 }
469 538
470 } // namespace examples 539 } // namespace examples
471 540
472 // static 541 // static
473 ApplicationDelegate* ApplicationDelegate::Create() { 542 ApplicationDelegate* ApplicationDelegate::Create() {
474 return new examples::WindowManager; 543 return new examples::WindowManager;
475 } 544 }
476 545
477 } // namespace mojo 546 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698