| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "ui/aura/desktop.h" | 5 #include "ui/aura/desktop.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/command_line.h" | 11 #include "base/command_line.h" |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/message_loop.h" | 13 #include "base/message_loop.h" |
| 14 #include "base/string_number_conversions.h" | 14 #include "base/string_number_conversions.h" |
| 15 #include "base/string_split.h" | 15 #include "base/string_split.h" |
| 16 #include "ui/aura/aura_switches.h" | 16 #include "ui/aura/aura_switches.h" |
| 17 #include "ui/aura/desktop_delegate.h" | 17 #include "ui/aura/desktop_delegate.h" |
| 18 #include "ui/aura/desktop_host.h" | 18 #include "ui/aura/desktop_host.h" |
| 19 #include "ui/aura/desktop_observer.h" | 19 #include "ui/aura/desktop_observer.h" |
| 20 #include "ui/aura/event.h" | 20 #include "ui/aura/event.h" |
| 21 #include "ui/aura/focus_manager.h" | 21 #include "ui/aura/focus_manager.h" |
| 22 #include "ui/aura/screen_aura.h" | 22 #include "ui/aura/screen_aura.h" |
| 23 #include "ui/aura/screen_rotation.h" |
| 23 #include "ui/aura/toplevel_window_container.h" | 24 #include "ui/aura/toplevel_window_container.h" |
| 24 #include "ui/aura/window.h" | 25 #include "ui/aura/window.h" |
| 25 #include "ui/aura/window_delegate.h" | 26 #include "ui/aura/window_delegate.h" |
| 26 #include "ui/gfx/compositor/compositor.h" | 27 #include "ui/gfx/compositor/compositor.h" |
| 27 #include "ui/gfx/compositor/layer.h" | 28 #include "ui/gfx/compositor/layer.h" |
| 29 #include "ui/gfx/compositor/layer_animation_sequence.h" |
| 30 #include "ui/gfx/compositor/layer_animator.h" |
| 31 #include "ui/gfx/interpolated_transform.h" |
| 28 | 32 |
| 29 using std::string; | 33 using std::string; |
| 30 using std::vector; | 34 using std::vector; |
| 31 | 35 |
| 32 namespace aura { | 36 namespace aura { |
| 33 | 37 |
| 34 namespace { | 38 namespace { |
| 35 | 39 |
| 36 // Default bounds for the host window. | 40 // Default bounds for the host window. |
| 37 static const int kDefaultHostWindowX = 200; | 41 static const int kDefaultHostWindowX = 200; |
| 38 static const int kDefaultHostWindowY = 200; | 42 static const int kDefaultHostWindowY = 200; |
| 39 static const int kDefaultHostWindowWidth = 1280; | 43 static const int kDefaultHostWindowWidth = 1280; |
| 40 static const int kDefaultHostWindowHeight = 1024; | 44 static const int kDefaultHostWindowHeight = 1024; |
| 41 | 45 |
| 46 #if !defined(NDEBUG) |
| 47 // Converts degrees to an angle in the range [-180, 180). |
| 48 int NormalizeAngle(int degrees) { |
| 49 while (degrees <= -180) degrees += 360; |
| 50 while (degrees > 180) degrees -= 360; |
| 51 return degrees; |
| 52 } |
| 53 |
| 54 static int SymmetricRound(float x) { |
| 55 return static_cast<int>( |
| 56 x > 0 |
| 57 ? std::floor(x + 0.5f) |
| 58 : std::ceil(x - 0.5f)); |
| 59 } |
| 60 #endif |
| 61 |
| 42 class DefaultDesktopDelegate : public DesktopDelegate { | 62 class DefaultDesktopDelegate : public DesktopDelegate { |
| 43 public: | 63 public: |
| 44 explicit DefaultDesktopDelegate(Desktop* desktop) : desktop_(desktop) {} | 64 explicit DefaultDesktopDelegate(Desktop* desktop) : desktop_(desktop) {} |
| 45 virtual ~DefaultDesktopDelegate() {} | 65 virtual ~DefaultDesktopDelegate() {} |
| 46 | 66 |
| 47 private: | 67 private: |
| 48 virtual void AddChildToDefaultParent(Window* window) OVERRIDE { | 68 virtual void AddChildToDefaultParent(Window* window) OVERRIDE { |
| 49 desktop_->AddChild(window); | 69 desktop_->AddChild(window); |
| 50 } | 70 } |
| 51 | 71 |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 178 } | 198 } |
| 179 if (target && target->delegate()) { | 199 if (target && target->delegate()) { |
| 180 MouseEvent translated_event(*event, this, target); | 200 MouseEvent translated_event(*event, this, target); |
| 181 return target->OnMouseEvent(&translated_event); | 201 return target->OnMouseEvent(&translated_event); |
| 182 } | 202 } |
| 183 return false; | 203 return false; |
| 184 } | 204 } |
| 185 | 205 |
| 186 bool Desktop::DispatchKeyEvent(KeyEvent* event) { | 206 bool Desktop::DispatchKeyEvent(KeyEvent* event) { |
| 187 #if !defined(NDEBUG) | 207 #if !defined(NDEBUG) |
| 188 // Press Home key to rotate the screen. Primarily used for testing. | |
| 189 if (event->type() == ui::ET_KEY_PRESSED && | 208 if (event->type() == ui::ET_KEY_PRESSED && |
| 190 (event->flags() & ui::EF_CONTROL_DOWN) && | 209 (event->flags() & ui::EF_SHIFT_DOWN) && |
| 191 event->key_code() == ui::VKEY_HOME) { | 210 (event->flags() & ui::EF_ALT_DOWN) && |
| 192 ui::Transform transform; | 211 event->is_char()) { |
| 193 static int count = 0; | 212 |
| 194 gfx::Size size = host_->GetSize(); | 213 bool should_rotate = true; |
| 195 switch (count) { | 214 int new_degrees = 0; |
| 196 case 0: | 215 switch (event->key_code()) { |
| 197 transform.ConcatRotate(-90.0f); | 216 case ui::VKEY_UP: new_degrees = 0; break; |
| 198 transform.ConcatTranslate(0, size.height()); | 217 case ui::VKEY_DOWN: new_degrees = 180; break; |
| 199 break; | 218 case ui::VKEY_RIGHT: new_degrees = 90; break; |
| 200 case 1: | 219 case ui::VKEY_LEFT: new_degrees = -90; break; |
| 201 transform.ConcatRotate(180.0f); | 220 default: should_rotate = false; break; |
| 202 transform.ConcatTranslate(size.width(), size.height()); | |
| 203 break; | |
| 204 case 2: | |
| 205 transform.ConcatRotate(90.0f); | |
| 206 transform.ConcatTranslate(size.width(), 0); | |
| 207 break; | |
| 208 } | 221 } |
| 209 layer()->SetAnimation(CreateDefaultAnimation()); | 222 |
| 210 SetTransform(transform); | 223 if (should_rotate) { |
| 211 count = (count + 1) % 4; | 224 float rotation = 0.0f; |
| 212 return true; | 225 int degrees = 0; |
| 226 const ui::Transform& transform = layer()->GetTargetTransform(); |
| 227 if (ui::InterpolatedTransform::FactorTRS(transform, |
| 228 NULL, &rotation, NULL)) |
| 229 degrees = NormalizeAngle(new_degrees - SymmetricRound(rotation)); |
| 230 |
| 231 if (degrees != 0) { |
| 232 layer()->GetAnimator()->set_preemption_strategy( |
| 233 ui::LayerAnimator::REPLACE_QUEUED_ANIMATIONS); |
| 234 layer()->GetAnimator()->ScheduleAnimationElement( |
| 235 new ScreenRotation(degrees)); |
| 236 return true; |
| 237 } |
| 238 } |
| 213 } | 239 } |
| 214 #endif | 240 #endif |
| 215 | 241 |
| 216 if (focused_window_) { | 242 if (focused_window_) { |
| 217 KeyEvent translated_event(*event); | 243 KeyEvent translated_event(*event); |
| 218 return focused_window_->OnKeyEvent(&translated_event); | 244 return focused_window_->OnKeyEvent(&translated_event); |
| 219 } | 245 } |
| 220 return false; | 246 return false; |
| 221 } | 247 } |
| 222 | 248 |
| (...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 361 if (capture_window_ && capture_window_->delegate()) | 387 if (capture_window_ && capture_window_->delegate()) |
| 362 capture_window_->delegate()->OnCaptureLost(); | 388 capture_window_->delegate()->OnCaptureLost(); |
| 363 capture_window_ = NULL; | 389 capture_window_ = NULL; |
| 364 } | 390 } |
| 365 | 391 |
| 366 void Desktop::SetTransform(const ui::Transform& transform) { | 392 void Desktop::SetTransform(const ui::Transform& transform) { |
| 367 Window::SetTransform(transform); | 393 Window::SetTransform(transform); |
| 368 | 394 |
| 369 // If the layer is not animating, then we need to update the host size | 395 // If the layer is not animating, then we need to update the host size |
| 370 // immediately. | 396 // immediately. |
| 371 if (!layer()->has_animation()) | 397 if (!layer()->GetAnimator()->is_animating()) |
| 372 OnHostResized(host_->GetSize()); | 398 OnHostResized(host_->GetSize()); |
| 373 } | 399 } |
| 374 | 400 |
| 375 void Desktop::HandleMouseMoved(const MouseEvent& event, Window* target) { | 401 void Desktop::HandleMouseMoved(const MouseEvent& event, Window* target) { |
| 376 if (target == mouse_moved_handler_) | 402 if (target == mouse_moved_handler_) |
| 377 return; | 403 return; |
| 378 | 404 |
| 379 // Send an exited event. | 405 // Send an exited event. |
| 380 if (mouse_moved_handler_ && mouse_moved_handler_->delegate()) { | 406 if (mouse_moved_handler_ && mouse_moved_handler_->delegate()) { |
| 381 MouseEvent translated_event(event, this, mouse_moved_handler_, | 407 MouseEvent translated_event(event, this, mouse_moved_handler_, |
| (...skipping 22 matching lines...) Expand all Loading... |
| 404 } | 430 } |
| 405 | 431 |
| 406 internal::FocusManager* Desktop::GetFocusManager() { | 432 internal::FocusManager* Desktop::GetFocusManager() { |
| 407 return this; | 433 return this; |
| 408 } | 434 } |
| 409 | 435 |
| 410 Desktop* Desktop::GetDesktop() { | 436 Desktop* Desktop::GetDesktop() { |
| 411 return this; | 437 return this; |
| 412 } | 438 } |
| 413 | 439 |
| 414 void Desktop::OnLayerAnimationEnded(const ui::Animation* animation) { | 440 void Desktop::OnLayerAnimationEnded( |
| 441 const ui::LayerAnimationSequence* animation) { |
| 415 OnHostResized(host_->GetSize()); | 442 OnHostResized(host_->GetSize()); |
| 416 } | 443 } |
| 417 | 444 |
| 418 void Desktop::SetFocusedWindow(Window* focused_window) { | 445 void Desktop::SetFocusedWindow(Window* focused_window) { |
| 419 if (focused_window == focused_window_ || | 446 if (focused_window == focused_window_ || |
| 420 (focused_window && !focused_window->CanFocus())) { | 447 (focused_window && !focused_window->CanFocus())) { |
| 421 return; | 448 return; |
| 422 } | 449 } |
| 423 if (focused_window_ && focused_window_->delegate()) | 450 if (focused_window_ && focused_window_->delegate()) |
| 424 focused_window_->delegate()->OnBlur(); | 451 focused_window_->delegate()->OnBlur(); |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 456 base::StringToInt(parts[1], &parsed_height) && parsed_height > 0) { | 483 base::StringToInt(parts[1], &parsed_height) && parsed_height > 0) { |
| 457 bounds.set_size(gfx::Size(parsed_width, parsed_height)); | 484 bounds.set_size(gfx::Size(parsed_width, parsed_height)); |
| 458 } else if (use_fullscreen_host_window_) { | 485 } else if (use_fullscreen_host_window_) { |
| 459 bounds = gfx::Rect(DesktopHost::GetNativeDisplaySize()); | 486 bounds = gfx::Rect(DesktopHost::GetNativeDisplaySize()); |
| 460 } | 487 } |
| 461 | 488 |
| 462 return bounds; | 489 return bounds; |
| 463 } | 490 } |
| 464 | 491 |
| 465 } // namespace aura | 492 } // namespace aura |
| OLD | NEW |