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

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: Looks Good 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
« no previous file with comments | « mojo/examples/png_viewer/png_viewer.cc ('k') | mojo/views/native_widget_view_manager.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 virtual void OnNodeDestroyed(Node* parent) OVERRIDE {
166 DCHECK_EQ(parent, node_->parent());
167 parent->RemoveObserver(this);
168 node_ = NULL;
169 }
170
150 KeyboardServicePtr keyboard_service_; 171 KeyboardServicePtr keyboard_service_;
151 ViewManager* view_manager_; 172 ViewManager* view_manager_;
152 173
153 // Node the keyboard is attached to. 174 // Node the keyboard is attached to.
154 Node* node_; 175 Node* node_;
155 176
156 DISALLOW_COPY_AND_ASSIGN(KeyboardManager); 177 DISALLOW_COPY_AND_ASSIGN(KeyboardManager);
157 }; 178 };
158 179
159 class RootLayoutManager : public NodeObserver { 180 class RootLayoutManager : public NodeObserver {
160 public: 181 public:
161 explicit RootLayoutManager(ViewManager* view_manager, 182 RootLayoutManager(ViewManager* view_manager,
162 Node* root, 183 Node* root,
163 Id content_node_id) 184 Id content_node_id,
164 : root_(root), 185 Id launcher_ui_node_id,
165 view_manager_(view_manager), 186 Id control_panel_node_id)
166 content_node_id_(content_node_id) {} 187 : root_(root),
167 virtual ~RootLayoutManager() {} 188 view_manager_(view_manager),
189 content_node_id_(content_node_id),
190 launcher_ui_node_id_(launcher_ui_node_id),
191 control_panel_node_id_(control_panel_node_id) {}
192 virtual ~RootLayoutManager() {
193 if (root_)
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 virtual void OnNodeDestroyed(Node* node) OVERRIDE {
238 DCHECK_EQ(node, root_);
239 root_->RemoveObserver(this);
240 root_ = NULL;
180 } 241 }
181 242
182 Node* root_; 243 Node* root_;
183 ViewManager* view_manager_; 244 ViewManager* view_manager_;
184 Id content_node_id_; 245 const Id content_node_id_;
246 const Id launcher_ui_node_id_;
247 const Id control_panel_node_id_;
185 248
186 DISALLOW_COPY_AND_ASSIGN(RootLayoutManager); 249 DISALLOW_COPY_AND_ASSIGN(RootLayoutManager);
187 }; 250 };
188 251
189 class WindowManager : public ApplicationDelegate, 252 class WindowManager : public ApplicationDelegate,
190 public DebugPanel::Delegate, 253 public DebugPanel::Delegate,
191 public ViewManagerDelegate, 254 public ViewManagerDelegate,
192 public ViewEventDispatcher { 255 public ViewEventDispatcher {
193 public: 256 public:
194 WindowManager() 257 WindowManager()
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
276 virtual void OnRootAdded(ViewManager* view_manager, Node* root) OVERRIDE { 339 virtual void OnRootAdded(ViewManager* view_manager, Node* root) OVERRIDE {
277 DCHECK(!view_manager_); 340 DCHECK(!view_manager_);
278 view_manager_ = view_manager; 341 view_manager_ = view_manager;
279 view_manager_->SetEventDispatcher(this); 342 view_manager_->SetEventDispatcher(this);
280 343
281 Node* node = Node::Create(view_manager_); 344 Node* node = Node::Create(view_manager_);
282 root->AddChild(node); 345 root->AddChild(node);
283 node->SetBounds(gfx::Rect(root->bounds().size())); 346 node->SetBounds(gfx::Rect(root->bounds().size()));
284 content_node_id_ = node->id(); 347 content_node_id_ = node->id();
285 348
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_); 349 View* view = View::Create(view_manager_);
291 node->SetActiveView(view); 350 node->SetActiveView(view);
292 view->SetColor(SK_ColorBLUE); 351 view->SetColor(SK_ColorBLUE);
293 352
294 CreateLauncherUI(); 353 Id launcher_ui_id = CreateLauncherUI();
295 CreateControlPanel(node); 354 Id control_panel_id = CreateControlPanel(node);
355
356 root_layout_manager_.reset(
357 new RootLayoutManager(view_manager, root,
358 content_node_id_,
359 launcher_ui_id,
360 control_panel_id));
361 root->AddObserver(root_layout_manager_.get());
296 } 362 }
297 virtual void OnViewManagerDisconnected(ViewManager* view_manager) OVERRIDE { 363 virtual void OnViewManagerDisconnected(ViewManager* view_manager) OVERRIDE {
298 DCHECK_EQ(view_manager_, view_manager); 364 DCHECK_EQ(view_manager_, view_manager);
299 view_manager_ = NULL; 365 view_manager_ = NULL;
300 base::MessageLoop::current()->Quit(); 366 base::MessageLoop::current()->Quit();
301 } 367 }
302 368
303 // Overridden from ViewEventDispatcher: 369 // Overridden from ViewEventDispatcher:
304 virtual void DispatchEvent(View* target, EventPtr event) OVERRIDE { 370 virtual void DispatchEvent(View* target, EventPtr event) OVERRIDE {
305 // TODO(beng): More sophisticated focus handling than this is required! 371 // 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(); 408 dest_node = windows_.back();
343 } 409 }
344 410
345 if (dest_node) 411 if (dest_node)
346 Embed(dest_node, handler_url, nav_details.Pass(), response.Pass()); 412 Embed(dest_node, handler_url, nav_details.Pass(), response.Pass());
347 else 413 else
348 CreateWindow(handler_url, nav_details.Pass(), response.Pass()); 414 CreateWindow(handler_url, nav_details.Pass(), response.Pass());
349 } 415 }
350 416
351 // TODO(beng): proper layout manager!! 417 // TODO(beng): proper layout manager!!
352 void CreateLauncherUI() { 418 Id CreateLauncherUI() {
353 navigation::NavigationDetailsPtr nav_details; 419 navigation::NavigationDetailsPtr nav_details;
354 navigation::ResponseDetailsPtr response; 420 navigation::ResponseDetailsPtr response;
355 Node* node = view_manager_->GetNodeById(content_node_id_); 421 Node* node = view_manager_->GetNodeById(content_node_id_);
356 gfx::Rect bounds = node->bounds(); 422 gfx::Rect bounds = node->bounds();
357 bounds.Inset(kBorderInset, kBorderInset); 423 bounds.Inset(kBorderInset, kBorderInset);
358 bounds.set_height(kTextfieldHeight); 424 bounds.set_height(kTextfieldHeight);
359 launcher_ui_ = CreateChild(content_node_id_, "mojo:mojo_browser", bounds, 425 launcher_ui_ = CreateChild(content_node_id_, "mojo:mojo_browser", bounds,
360 nav_details.Pass(), response.Pass()); 426 nav_details.Pass(), response.Pass());
427 return launcher_ui_->id();
361 } 428 }
362 429
363 void CreateWindow(const std::string& handler_url, 430 void CreateWindow(const std::string& handler_url,
364 navigation::NavigationDetailsPtr nav_details, 431 navigation::NavigationDetailsPtr nav_details,
365 navigation::ResponseDetailsPtr response) { 432 navigation::ResponseDetailsPtr response) {
366 Node* node = view_manager_->GetNodeById(content_node_id_); 433 Node* node = view_manager_->GetNodeById(content_node_id_);
367 gfx::Rect bounds(kBorderInset, 434 gfx::Rect bounds(kBorderInset,
368 2 * kBorderInset + kTextfieldHeight, 435 2 * kBorderInset + kTextfieldHeight,
369 node->bounds().width() - 3 * kBorderInset - 436 node->bounds().width() - 3 * kBorderInset -
370 kControlPanelWidth, 437 kControlPanelWidth,
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
402 app_->ConnectToService(app_url, &navigator); 469 app_->ConnectToService(app_url, &navigator);
403 navigator->Navigate(node->id(), nav_details.Pass(), response.Pass()); 470 navigator->Navigate(node->id(), nav_details.Pass(), response.Pass());
404 } 471 }
405 } 472 }
406 473
407 bool IsDescendantOfKeyboard(View* target) { 474 bool IsDescendantOfKeyboard(View* target) {
408 return keyboard_manager_.get() && 475 return keyboard_manager_.get() &&
409 keyboard_manager_->node()->Contains(target->node()); 476 keyboard_manager_->node()->Contains(target->node());
410 } 477 }
411 478
412 void CreateControlPanel(view_manager::Node* root) { 479 Id CreateControlPanel(view_manager::Node* root) {
413 Node* node = Node::Create(view_manager_); 480 Node* node = Node::Create(view_manager_);
414 View* view = view_manager::View::Create(view_manager_); 481 View* view = view_manager::View::Create(view_manager_);
415 root->AddChild(node); 482 root->AddChild(node);
416 node->SetActiveView(view); 483 node->SetActiveView(view);
417 484
418 gfx::Rect bounds(root->bounds().width() - kControlPanelWidth - 485 gfx::Rect bounds(root->bounds().width() - kControlPanelWidth -
419 kBorderInset, 486 kBorderInset,
420 kBorderInset * 2 + kTextfieldHeight, 487 kBorderInset * 2 + kTextfieldHeight,
421 kControlPanelWidth, 488 kControlPanelWidth,
422 root->bounds().height() - kBorderInset * 3 - 489 root->bounds().height() - kBorderInset * 3 -
423 kTextfieldHeight); 490 kTextfieldHeight);
424 node->SetBounds(bounds); 491 node->SetBounds(bounds);
425 492
426 debug_panel_ = new DebugPanel(this, node); 493 debug_panel_ = new DebugPanel(this, node);
494 return node->id();
427 } 495 }
428 496
429 scoped_ptr<ViewsInit> views_init_; 497 scoped_ptr<ViewsInit> views_init_;
430 DebugPanel* debug_panel_; 498 DebugPanel* debug_panel_;
431 launcher::LauncherPtr launcher_; 499 launcher::LauncherPtr launcher_;
432 Node* launcher_ui_; 500 Node* launcher_ui_;
433 std::vector<Node*> windows_; 501 std::vector<Node*> windows_;
434 ViewManager* view_manager_; 502 ViewManager* view_manager_;
435 scoped_ptr<RootLayoutManager> root_layout_manager_; 503 scoped_ptr<RootLayoutManager> root_layout_manager_;
436 504
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
468 } 536 }
469 537
470 } // namespace examples 538 } // namespace examples
471 539
472 // static 540 // static
473 ApplicationDelegate* ApplicationDelegate::Create() { 541 ApplicationDelegate* ApplicationDelegate::Create() {
474 return new examples::WindowManager; 542 return new examples::WindowManager;
475 } 543 }
476 544
477 } // namespace mojo 545 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/examples/png_viewer/png_viewer.cc ('k') | mojo/views/native_widget_view_manager.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698