ash_window_tree_host.cc 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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
  3. // found in the LICENSE file.
  4. #include "ash/host/ash_window_tree_host.h"
  5. #include <memory>
  6. #include "ash/constants/ash_switches.h"
  7. #include "ash/host/ash_window_tree_host_init_params.h"
  8. #include "ash/host/ash_window_tree_host_mirroring_unified.h"
  9. #include "ash/host/ash_window_tree_host_platform.h"
  10. #include "ash/host/ash_window_tree_host_unified.h"
  11. #include "base/command_line.h"
  12. #include "base/system/sys_info.h"
  13. #include "ui/aura/client/screen_position_client.h"
  14. #include "ui/aura/env.h"
  15. #include "ui/aura/window_tree_host.h"
  16. #include "ui/events/event.h"
  17. #include "ui/gfx/geometry/insets.h"
  18. #include "ui/gfx/geometry/rect.h"
  19. #include "ui/platform_window/platform_window_init_properties.h"
  20. namespace ash {
  21. namespace {
  22. bool GetAllowConfineCursor() {
  23. return base::SysInfo::IsRunningOnChromeOS() ||
  24. base::CommandLine::ForCurrentProcess()->HasSwitch(
  25. switches::kAshConstrainPointerToRoot);
  26. }
  27. } // namespace
  28. AshWindowTreeHost::AshWindowTreeHost()
  29. : allow_confine_cursor_(GetAllowConfineCursor()) {}
  30. AshWindowTreeHost::~AshWindowTreeHost() = default;
  31. void AshWindowTreeHost::TranslateLocatedEvent(ui::LocatedEvent* event) {
  32. if (event->IsTouchEvent())
  33. return;
  34. aura::WindowTreeHost* wth = AsWindowTreeHost();
  35. aura::Window* root_window = wth->window();
  36. aura::client::ScreenPositionClient* screen_position_client =
  37. aura::client::GetScreenPositionClient(root_window);
  38. gfx::Rect local(wth->GetBoundsInPixels().size());
  39. local.Inset(GetHostInsets());
  40. if (screen_position_client && !local.Contains(event->location())) {
  41. gfx::Point location(event->location());
  42. // In order to get the correct point in screen coordinates
  43. // during passive grab, we first need to find on which host window
  44. // the mouse is on, and find out the screen coordinates on that
  45. // host window, then convert it back to this host window's coordinate.
  46. screen_position_client->ConvertHostPointToScreen(root_window, &location);
  47. screen_position_client->ConvertPointFromScreen(root_window, &location);
  48. wth->ConvertDIPToPixels(&location);
  49. event->set_location(location);
  50. event->set_root_location(location);
  51. }
  52. }
  53. // static
  54. std::unique_ptr<AshWindowTreeHost> AshWindowTreeHost::Create(
  55. const AshWindowTreeHostInitParams& init_params) {
  56. if (init_params.mirroring_unified) {
  57. return std::make_unique<AshWindowTreeHostMirroringUnified>(
  58. init_params.initial_bounds, init_params.display_id,
  59. init_params.delegate);
  60. }
  61. if (init_params.offscreen) {
  62. return std::make_unique<AshWindowTreeHostUnified>(
  63. init_params.initial_bounds, init_params.delegate,
  64. init_params.compositor_memory_limit_mb);
  65. }
  66. ui::PlatformWindowInitProperties properties{init_params.initial_bounds};
  67. properties.enable_compositing_based_throttling = true;
  68. properties.compositor_memory_limit_mb =
  69. init_params.compositor_memory_limit_mb;
  70. return std::make_unique<AshWindowTreeHostPlatform>(std::move(properties),
  71. init_params.delegate);
  72. }
  73. } // namespace ash