| 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 "base/bind.h" | 5 #include "base/bind.h" |
| 6 #include "base/macros.h" | 6 #include "base/macros.h" |
| 7 #include "mojo/application/application_runner_chromium.h" | 7 #include "mojo/application/application_runner_chromium.h" |
| 8 #include "mojo/converters/geometry/geometry_type_converters.h" | 8 #include "mojo/converters/geometry/geometry_type_converters.h" |
| 9 #include "mojo/converters/input_events/input_events_type_converters.h" | 9 #include "mojo/converters/input_events/input_events_type_converters.h" |
| 10 #include "mojo/examples/keyboard/keyboard.mojom.h" | 10 #include "mojo/examples/keyboard/keyboard.mojom.h" |
| (...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 148 virtual ~KeyboardManager() { | 148 virtual ~KeyboardManager() { |
| 149 if (view_) | 149 if (view_) |
| 150 view_->parent()->RemoveObserver(this); | 150 view_->parent()->RemoveObserver(this); |
| 151 } | 151 } |
| 152 | 152 |
| 153 View* view() { return view_; } | 153 View* view() { return view_; } |
| 154 | 154 |
| 155 void Init(ApplicationImpl* application, | 155 void Init(ApplicationImpl* application, |
| 156 ViewManager* view_manager, | 156 ViewManager* view_manager, |
| 157 View* parent, | 157 View* parent, |
| 158 const gfx::Rect& bounds) { | 158 const Rect& bounds) { |
| 159 view_manager_ = view_manager; | 159 view_manager_ = view_manager; |
| 160 view_ = View::Create(view_manager); | 160 view_ = View::Create(view_manager); |
| 161 view_->SetBounds(bounds); | 161 view_->SetBounds(bounds); |
| 162 parent->AddChild(view_); | 162 parent->AddChild(view_); |
| 163 view_->Embed("mojo:keyboard"); | 163 view_->Embed("mojo:keyboard"); |
| 164 application->ConnectToService("mojo:keyboard", &keyboard_service_); | 164 application->ConnectToService("mojo:keyboard", &keyboard_service_); |
| 165 keyboard_service_.set_client(this); | 165 keyboard_service_.set_client(this); |
| 166 parent->AddObserver(this); | 166 parent->AddObserver(this); |
| 167 } | 167 } |
| 168 | 168 |
| 169 void Show(Id view_id, const gfx::Rect& bounds) { | 169 void Show(Id view_id, const Rect& bounds) { |
| 170 keyboard_service_->SetTarget(view_id); | 170 keyboard_service_->SetTarget(view_id); |
| 171 view_->SetVisible(true); | 171 view_->SetVisible(true); |
| 172 } | 172 } |
| 173 | 173 |
| 174 void Hide(Id view_id) { | 174 void Hide(Id view_id) { |
| 175 keyboard_service_->SetTarget(0); | 175 keyboard_service_->SetTarget(0); |
| 176 view_->SetVisible(false); | 176 view_->SetVisible(false); |
| 177 } | 177 } |
| 178 | 178 |
| 179 private: | 179 private: |
| 180 // KeyboardClient: | 180 // KeyboardClient: |
| 181 virtual void OnKeyboardEvent(Id view_id, | 181 virtual void OnKeyboardEvent(Id view_id, |
| 182 int32_t code, | 182 int32_t code, |
| 183 int32_t flags) override { | 183 int32_t flags) override { |
| 184 // TODO(sky): figure this out. Code use to dispatch events, but that's a | 184 // TODO(sky): figure this out. Code use to dispatch events, but that's a |
| 185 // hack. Instead strings should be passed through, or maybe a richer text | 185 // hack. Instead strings should be passed through, or maybe a richer text |
| 186 // input interface. | 186 // input interface. |
| 187 } | 187 } |
| 188 | 188 |
| 189 // Overridden from ViewObserver: | 189 // Overridden from ViewObserver: |
| 190 virtual void OnViewBoundsChanged(View* parent, | 190 virtual void OnViewBoundsChanged(View* parent, |
| 191 const gfx::Rect& old_bounds, | 191 const Rect& old_bounds, |
| 192 const gfx::Rect& new_bounds) override { | 192 const Rect& new_bounds) override { |
| 193 gfx::Rect keyboard_bounds(view_->bounds()); | 193 Rect keyboard_bounds(view_->bounds()); |
| 194 keyboard_bounds.set_y(new_bounds.bottom() - keyboard_bounds.height()); | 194 keyboard_bounds.y = |
| 195 keyboard_bounds.set_width(keyboard_bounds.width() + | 195 new_bounds.y + new_bounds.height - keyboard_bounds.height; |
| 196 new_bounds.width() - old_bounds.width()); | 196 keyboard_bounds.width = |
| 197 keyboard_bounds.width + new_bounds.width - old_bounds.width; |
| 197 view_->SetBounds(keyboard_bounds); | 198 view_->SetBounds(keyboard_bounds); |
| 198 } | 199 } |
| 199 virtual void OnViewDestroyed(View* parent) override { | 200 virtual void OnViewDestroyed(View* parent) override { |
| 200 DCHECK_EQ(parent, view_->parent()); | 201 DCHECK_EQ(parent, view_->parent()); |
| 201 parent->RemoveObserver(this); | 202 parent->RemoveObserver(this); |
| 202 view_ = NULL; | 203 view_ = NULL; |
| 203 } | 204 } |
| 204 | 205 |
| 205 KeyboardServicePtr keyboard_service_; | 206 KeyboardServicePtr keyboard_service_; |
| 206 ViewManager* view_manager_; | 207 ViewManager* view_manager_; |
| (...skipping 17 matching lines...) Expand all Loading... |
| 224 launcher_ui_view_id_(launcher_ui_view_id), | 225 launcher_ui_view_id_(launcher_ui_view_id), |
| 225 control_panel_view_id_(control_panel_view_id) {} | 226 control_panel_view_id_(control_panel_view_id) {} |
| 226 virtual ~RootLayoutManager() { | 227 virtual ~RootLayoutManager() { |
| 227 if (root_) | 228 if (root_) |
| 228 root_->RemoveObserver(this); | 229 root_->RemoveObserver(this); |
| 229 } | 230 } |
| 230 | 231 |
| 231 private: | 232 private: |
| 232 // Overridden from ViewObserver: | 233 // Overridden from ViewObserver: |
| 233 virtual void OnViewBoundsChanged(View* view, | 234 virtual void OnViewBoundsChanged(View* view, |
| 234 const gfx::Rect& old_bounds, | 235 const Rect& old_bounds, |
| 235 const gfx::Rect& new_bounds) override { | 236 const Rect& new_bounds) override { |
| 236 DCHECK_EQ(view, root_); | 237 DCHECK_EQ(view, root_); |
| 237 | 238 |
| 238 View* content_view = view_manager_->GetViewById(content_view_id_); | 239 View* content_view = view_manager_->GetViewById(content_view_id_); |
| 239 content_view->SetBounds(new_bounds); | 240 content_view->SetBounds(new_bounds); |
| 240 | 241 |
| 241 int delta_width = new_bounds.width() - old_bounds.width(); | 242 int delta_width = new_bounds.width - old_bounds.width; |
| 242 int delta_height = new_bounds.height() - old_bounds.height(); | 243 int delta_height = new_bounds.height - old_bounds.height; |
| 243 | 244 |
| 244 View* launcher_ui_view = | 245 View* launcher_ui_view = |
| 245 view_manager_->GetViewById(launcher_ui_view_id_); | 246 view_manager_->GetViewById(launcher_ui_view_id_); |
| 246 gfx::Rect launcher_ui_bounds(launcher_ui_view->bounds()); | 247 Rect launcher_ui_bounds(launcher_ui_view->bounds()); |
| 247 launcher_ui_bounds.set_width(launcher_ui_bounds.width() + delta_width); | 248 launcher_ui_bounds.width += delta_width; |
| 248 launcher_ui_view->SetBounds(launcher_ui_bounds); | 249 launcher_ui_view->SetBounds(launcher_ui_bounds); |
| 249 | 250 |
| 250 View* control_panel_view = | 251 View* control_panel_view = |
| 251 view_manager_->GetViewById(control_panel_view_id_); | 252 view_manager_->GetViewById(control_panel_view_id_); |
| 252 gfx::Rect control_panel_bounds(control_panel_view->bounds()); | 253 Rect control_panel_bounds(control_panel_view->bounds()); |
| 253 control_panel_bounds.set_x(control_panel_bounds.x() + delta_width); | 254 control_panel_bounds.x += delta_width; |
| 254 control_panel_view->SetBounds(control_panel_bounds); | 255 control_panel_view->SetBounds(control_panel_bounds); |
| 255 | 256 |
| 256 const View::Children& content_views = content_view->children(); | 257 const View::Children& content_views = content_view->children(); |
| 257 View::Children::const_iterator iter = content_views.begin(); | 258 View::Children::const_iterator iter = content_views.begin(); |
| 258 for(; iter != content_views.end(); ++iter) { | 259 for(; iter != content_views.end(); ++iter) { |
| 259 View* view = *iter; | 260 View* view = *iter; |
| 260 if (view->id() == control_panel_view->id() || | 261 if (view->id() == control_panel_view->id() || |
| 261 view->id() == launcher_ui_view->id()) | 262 view->id() == launcher_ui_view->id()) |
| 262 continue; | 263 continue; |
| 263 gfx::Rect view_bounds(view->bounds()); | 264 Rect view_bounds(view->bounds()); |
| 264 view_bounds.set_width(view_bounds.width() + delta_width); | 265 view_bounds.width += delta_width; |
| 265 view_bounds.set_height(view_bounds.height() + delta_height); | 266 view_bounds.height += delta_height; |
| 266 view->SetBounds(view_bounds); | 267 view->SetBounds(view_bounds); |
| 267 } | 268 } |
| 268 } | 269 } |
| 269 virtual void OnViewDestroyed(View* view) override { | 270 virtual void OnViewDestroyed(View* view) override { |
| 270 DCHECK_EQ(view, root_); | 271 DCHECK_EQ(view, root_); |
| 271 root_->RemoveObserver(this); | 272 root_->RemoveObserver(this); |
| 272 root_ = NULL; | 273 root_ = NULL; |
| 273 } | 274 } |
| 274 | 275 |
| 275 View* root_; | 276 View* root_; |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 332 } | 333 } |
| 333 | 334 |
| 334 void CloseWindow(Id view_id) { | 335 void CloseWindow(Id view_id) { |
| 335 WindowVector::iterator iter = GetWindowByViewId(view_id); | 336 WindowVector::iterator iter = GetWindowByViewId(view_id); |
| 336 DCHECK(iter != windows_.end()); | 337 DCHECK(iter != windows_.end()); |
| 337 Window* window = *iter; | 338 Window* window = *iter; |
| 338 windows_.erase(iter); | 339 windows_.erase(iter); |
| 339 window->view()->Destroy(); | 340 window->view()->Destroy(); |
| 340 } | 341 } |
| 341 | 342 |
| 342 void ShowKeyboard(Id view_id, const gfx::Rect& bounds) { | 343 void ShowKeyboard(Id view_id, const Rect& bounds) { |
| 343 // TODO: this needs to validate |view_id|. That is, it shouldn't assume | 344 // TODO: this needs to validate |view_id|. That is, it shouldn't assume |
| 344 // |view_id| is valid and it also needs to make sure the client that sent | 345 // |view_id| is valid and it also needs to make sure the client that sent |
| 345 // this really owns |view_id|. | 346 // this really owns |view_id|. |
| 346 // TODO: honor |bounds|. | 347 // TODO: honor |bounds|. |
| 347 if (!keyboard_manager_) { | 348 if (!keyboard_manager_) { |
| 348 keyboard_manager_.reset(new KeyboardManager); | 349 keyboard_manager_.reset(new KeyboardManager); |
| 349 View* parent = view_manager_->GetRoots().back(); | 350 View* parent = view_manager_->GetRoots().back(); |
| 350 int ideal_height = 200; | 351 int ideal_height = 200; |
| 351 // TODO(sky): 10 is a bit of a hack here. There is a bug that causes | 352 // TODO(sky): 10 is a bit of a hack here. There is a bug that causes |
| 352 // white strips to appear when 0 is used. Figure this out! | 353 // white strips to appear when 0 is used. Figure this out! |
| 353 const gfx::Rect keyboard_bounds( | 354 Rect keyboard_bounds; |
| 354 10, parent->bounds().height() - ideal_height, | 355 keyboard_bounds.x = 10; |
| 355 parent->bounds().width() - 20, ideal_height); | 356 keyboard_bounds.y = parent->bounds().height - ideal_height; |
| 357 keyboard_bounds.width = parent->bounds().width - 20; |
| 358 keyboard_bounds.height = ideal_height; |
| 356 keyboard_manager_->Init(app_, view_manager_, parent, keyboard_bounds); | 359 keyboard_manager_->Init(app_, view_manager_, parent, keyboard_bounds); |
| 357 } | 360 } |
| 358 keyboard_manager_->Show(view_id, bounds); | 361 keyboard_manager_->Show(view_id, bounds); |
| 359 } | 362 } |
| 360 | 363 |
| 361 void HideKeyboard(Id view_id) { | 364 void HideKeyboard(Id view_id) { |
| 362 // See comment in ShowKeyboard() about validating args. | 365 // See comment in ShowKeyboard() about validating args. |
| 363 if (keyboard_manager_) | 366 if (keyboard_manager_) |
| 364 keyboard_manager_->Hide(view_id); | 367 keyboard_manager_->Hide(view_id); |
| 365 } | 368 } |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 402 // Overridden from ViewManagerDelegate: | 405 // Overridden from ViewManagerDelegate: |
| 403 virtual void OnEmbed(ViewManager* view_manager, | 406 virtual void OnEmbed(ViewManager* view_manager, |
| 404 View* root, | 407 View* root, |
| 405 ServiceProviderImpl* exported_services, | 408 ServiceProviderImpl* exported_services, |
| 406 scoped_ptr<ServiceProvider> imported_services) override { | 409 scoped_ptr<ServiceProvider> imported_services) override { |
| 407 DCHECK(!view_manager_); | 410 DCHECK(!view_manager_); |
| 408 view_manager_ = view_manager; | 411 view_manager_ = view_manager; |
| 409 | 412 |
| 410 View* view = View::Create(view_manager_); | 413 View* view = View::Create(view_manager_); |
| 411 root->AddChild(view); | 414 root->AddChild(view); |
| 412 view->SetBounds(gfx::Rect(root->bounds().size())); | 415 Rect rect; |
| 416 rect.width = root->bounds().width; |
| 417 rect.height = root->bounds().height; |
| 418 view->SetBounds(rect); |
| 413 content_view_id_ = view->id(); | 419 content_view_id_ = view->id(); |
| 414 | 420 |
| 415 Id launcher_ui_id = CreateLauncherUI(); | 421 Id launcher_ui_id = CreateLauncherUI(); |
| 416 Id control_panel_id = CreateControlPanel(view); | 422 Id control_panel_id = CreateControlPanel(view); |
| 417 | 423 |
| 418 root_layout_manager_.reset( | 424 root_layout_manager_.reset( |
| 419 new RootLayoutManager(view_manager, root, | 425 new RootLayoutManager(view_manager, root, |
| 420 content_view_id_, | 426 content_view_id_, |
| 421 launcher_ui_id, | 427 launcher_ui_id, |
| 422 control_panel_id)); | 428 control_panel_id)); |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 479 dest_view = CreateWindow(); | 485 dest_view = CreateWindow(); |
| 480 windows_.push_back(dest_view); | 486 windows_.push_back(dest_view); |
| 481 } | 487 } |
| 482 | 488 |
| 483 dest_view->Embed(url); | 489 dest_view->Embed(url); |
| 484 } | 490 } |
| 485 | 491 |
| 486 // TODO(beng): proper layout manager!! | 492 // TODO(beng): proper layout manager!! |
| 487 Id CreateLauncherUI() { | 493 Id CreateLauncherUI() { |
| 488 View* view = view_manager_->GetViewById(content_view_id_); | 494 View* view = view_manager_->GetViewById(content_view_id_); |
| 489 gfx::Rect bounds = view->bounds(); | 495 Rect bounds = view->bounds(); |
| 490 bounds.Inset(kBorderInset, kBorderInset); | 496 bounds.x += kBorderInset; |
| 491 bounds.set_height(kTextfieldHeight); | 497 bounds.y += kBorderInset; |
| 498 bounds.width -= 2 * kBorderInset; |
| 499 bounds.height = kTextfieldHeight; |
| 492 launcher_ui_ = CreateWindow(bounds); | 500 launcher_ui_ = CreateWindow(bounds); |
| 493 launcher_ui_->Embed("mojo:browser"); | 501 launcher_ui_->Embed("mojo:browser"); |
| 494 return launcher_ui_->view()->id(); | 502 return launcher_ui_->view()->id(); |
| 495 } | 503 } |
| 496 | 504 |
| 497 Window* CreateWindow() { | 505 Window* CreateWindow() { |
| 498 View* view = view_manager_->GetViewById(content_view_id_); | 506 View* view = view_manager_->GetViewById(content_view_id_); |
| 499 gfx::Rect bounds(kBorderInset, | 507 Rect bounds; |
| 500 2 * kBorderInset + kTextfieldHeight, | 508 bounds.x = kBorderInset; |
| 501 view->bounds().width() - 3 * kBorderInset - | 509 bounds.y = 2 * kBorderInset + kTextfieldHeight; |
| 502 kControlPanelWidth, | 510 bounds.width = view->bounds().width - 3 * kBorderInset - kControlPanelWidth; |
| 503 view->bounds().height() - | 511 bounds.height = |
| 504 (3 * kBorderInset + kTextfieldHeight)); | 512 view->bounds().height - (3 * kBorderInset + kTextfieldHeight); |
| 505 if (!windows_.empty()) { | 513 if (!windows_.empty()) { |
| 506 gfx::Point position = windows_.back()->view()->bounds().origin(); | 514 bounds.x = windows_.back()->view()->bounds().x + 35; |
| 507 position.Offset(35, 35); | 515 bounds.y = windows_.back()->view()->bounds().y + 35; |
| 508 bounds.set_origin(position); | |
| 509 } | 516 } |
| 510 return CreateWindow(bounds); | 517 return CreateWindow(bounds); |
| 511 } | 518 } |
| 512 | 519 |
| 513 Window* CreateWindow(const gfx::Rect& bounds) { | 520 Window* CreateWindow(const Rect& bounds) { |
| 514 View* content = view_manager_->GetViewById(content_view_id_); | 521 View* content = view_manager_->GetViewById(content_view_id_); |
| 515 View* view = View::Create(view_manager_); | 522 View* view = View::Create(view_manager_); |
| 516 content->AddChild(view); | 523 content->AddChild(view); |
| 517 view->SetBounds(bounds); | 524 view->SetBounds(bounds); |
| 518 view->SetFocus(); | 525 view->SetFocus(); |
| 519 return new Window(this, view); | 526 return new Window(this, view); |
| 520 } | 527 } |
| 521 | 528 |
| 522 bool IsDescendantOfKeyboard(View* target) { | 529 bool IsDescendantOfKeyboard(View* target) { |
| 523 return keyboard_manager_.get() && | 530 return keyboard_manager_.get() && |
| 524 keyboard_manager_->view()->Contains(target); | 531 keyboard_manager_->view()->Contains(target); |
| 525 } | 532 } |
| 526 | 533 |
| 527 Id CreateControlPanel(View* root) { | 534 Id CreateControlPanel(View* root) { |
| 528 View* view = View::Create(view_manager_); | 535 View* view = View::Create(view_manager_); |
| 529 root->AddChild(view); | 536 root->AddChild(view); |
| 530 | 537 |
| 531 gfx::Rect bounds(root->bounds().width() - kControlPanelWidth - | 538 Rect bounds; |
| 532 kBorderInset, | 539 bounds.x = root->bounds().width - kControlPanelWidth - kBorderInset; |
| 533 kBorderInset * 2 + kTextfieldHeight, | 540 bounds.y = kBorderInset * 2 + kTextfieldHeight; |
| 534 kControlPanelWidth, | 541 bounds.width = kControlPanelWidth; |
| 535 root->bounds().height() - kBorderInset * 3 - | 542 bounds.height = |
| 536 kTextfieldHeight); | 543 root->bounds().height - kBorderInset * 3 - kTextfieldHeight; |
| 537 view->SetBounds(bounds); | 544 view->SetBounds(bounds); |
| 538 | 545 |
| 539 debug_panel_ = new DebugPanel(this, shell_, view); | 546 debug_panel_ = new DebugPanel(this, shell_, view); |
| 540 return view->id(); | 547 return view->id(); |
| 541 } | 548 } |
| 542 | 549 |
| 543 WindowVector::iterator GetWindowByViewId(Id view_id) { | 550 WindowVector::iterator GetWindowByViewId(Id view_id) { |
| 544 for (std::vector<Window*>::iterator iter = windows_.begin(); | 551 for (std::vector<Window*>::iterator iter = windows_.begin(); |
| 545 iter != windows_.end(); | 552 iter != windows_.end(); |
| 546 ++iter) { | 553 ++iter) { |
| (...skipping 25 matching lines...) Expand all Loading... |
| 572 ApplicationImpl* app_; | 579 ApplicationImpl* app_; |
| 573 | 580 |
| 574 DISALLOW_COPY_AND_ASSIGN(WindowManager); | 581 DISALLOW_COPY_AND_ASSIGN(WindowManager); |
| 575 }; | 582 }; |
| 576 | 583 |
| 577 void WindowManagerConnection::CloseWindow(Id view_id) { | 584 void WindowManagerConnection::CloseWindow(Id view_id) { |
| 578 window_manager_->CloseWindow(view_id); | 585 window_manager_->CloseWindow(view_id); |
| 579 } | 586 } |
| 580 | 587 |
| 581 void WindowManagerConnection::ShowKeyboard(Id view_id, RectPtr bounds) { | 588 void WindowManagerConnection::ShowKeyboard(Id view_id, RectPtr bounds) { |
| 582 window_manager_->ShowKeyboard(view_id, bounds.To<gfx::Rect>()); | 589 window_manager_->ShowKeyboard(view_id, *bounds); |
| 583 } | 590 } |
| 584 | 591 |
| 585 void WindowManagerConnection::HideKeyboard(Id view_id) { | 592 void WindowManagerConnection::HideKeyboard(Id view_id) { |
| 586 window_manager_->HideKeyboard(view_id); | 593 window_manager_->HideKeyboard(view_id); |
| 587 } | 594 } |
| 588 | 595 |
| 589 void NavigatorHostImpl::DidNavigateLocally(const mojo::String& url) { | 596 void NavigatorHostImpl::DidNavigateLocally(const mojo::String& url) { |
| 590 window_manager_->DidNavigateLocally(view_id_, url); | 597 window_manager_->DidNavigateLocally(view_id_, url); |
| 591 } | 598 } |
| 592 | 599 |
| 593 void NavigatorHostImpl::RequestNavigate(Target target, URLRequestPtr request) { | 600 void NavigatorHostImpl::RequestNavigate(Target target, URLRequestPtr request) { |
| 594 window_manager_->RequestNavigate(view_id_, target, request.Pass()); | 601 window_manager_->RequestNavigate(view_id_, target, request.Pass()); |
| 595 } | 602 } |
| 596 | 603 |
| 597 } // namespace examples | 604 } // namespace examples |
| 598 } // namespace mojo | 605 } // namespace mojo |
| 599 | 606 |
| 600 MojoResult MojoMain(MojoHandle shell_handle) { | 607 MojoResult MojoMain(MojoHandle shell_handle) { |
| 601 mojo::ApplicationRunnerChromium runner(new mojo::examples::WindowManager); | 608 mojo::ApplicationRunnerChromium runner(new mojo::examples::WindowManager); |
| 602 return runner.Run(shell_handle); | 609 return runner.Run(shell_handle); |
| 603 } | 610 } |
| OLD | NEW |