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

Side by Side Diff: headless/lib/browser/headless_screen.cc

Issue 2669693002: Minimize headless code that refers to aura. (Closed)
Patch Set: Add aura dependencies in BUIL.gn only when use_aura == true" Created 3 years, 10 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
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 "headless/lib/browser/headless_screen.h" 5 #include "headless/lib/browser/headless_screen.h"
6 6
7 #include <stdint.h> 7 #include <stdint.h>
8 8
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "ui/aura/env.h" 10 #include "ui/aura/env.h"
11 #include "ui/aura/window.h" 11 #include "ui/aura/window.h"
12 #include "ui/aura/window_event_dispatcher.h"
13 #include "ui/aura/window_tree_host.h"
14 #include "ui/base/ime/input_method.h" 12 #include "ui/base/ime/input_method.h"
15 #include "ui/gfx/geometry/rect_conversions.h" 13 #include "ui/gfx/geometry/rect_conversions.h"
16 #include "ui/gfx/geometry/size_conversions.h" 14 #include "ui/gfx/geometry/size_conversions.h"
17 #include "ui/gfx/native_widget_types.h" 15 #include "ui/gfx/native_widget_types.h"
18 16
19 namespace headless { 17 namespace headless {
20 18
21 namespace {
22
23 bool IsRotationPortrait(display::Display::Rotation rotation) {
24 return rotation == display::Display::ROTATE_90 ||
25 rotation == display::Display::ROTATE_270;
26 }
27
28 } // namespace
29
30 // static 19 // static
31 HeadlessScreen* HeadlessScreen::Create(const gfx::Size& size) { 20 HeadlessScreen* HeadlessScreen::Create(const gfx::Size& size) {
32 return new HeadlessScreen(gfx::Rect(size)); 21 return new HeadlessScreen(gfx::Rect(size));
33 } 22 }
34 23
35 HeadlessScreen::~HeadlessScreen() {} 24 HeadlessScreen::~HeadlessScreen() {}
36 25
37 aura::WindowTreeHost* HeadlessScreen::CreateHostForPrimaryDisplay() {
Sami 2017/02/09 18:08:22 Are we sure we don't need this code anymore? +Eric
Eric Seckler 2017/02/09 18:29:39 Hm, I think this code got here before my time... I
38 DCHECK(!host_);
39 host_ = aura::WindowTreeHost::Create(
40 gfx::Rect(GetPrimaryDisplay().GetSizeInPixel()));
41 // Some tests don't correctly manage window focus/activation states.
42 // Makes sure InputMethod is default focused so that IME basics can work.
43 host_->GetInputMethod()->OnFocus();
44 host_->window()->AddObserver(this);
45 host_->InitHost();
46 host_->window()->Show();
47 return host_;
48 }
49
50 void HeadlessScreen::SetDeviceScaleFactor(float device_scale_factor) {
51 display::Display display(GetPrimaryDisplay());
52 gfx::Rect bounds_in_pixel(display.GetSizeInPixel());
53 display.SetScaleAndBounds(device_scale_factor, bounds_in_pixel);
54 display_list().UpdateDisplay(display);
55 }
56
57 void HeadlessScreen::SetDisplayRotation(display::Display::Rotation rotation) {
58 display::Display display(GetPrimaryDisplay());
59 gfx::Rect bounds_in_pixel(display.GetSizeInPixel());
60 gfx::Rect new_bounds(bounds_in_pixel);
61 if (IsRotationPortrait(rotation) != IsRotationPortrait(display.rotation())) {
62 new_bounds.set_width(bounds_in_pixel.height());
63 new_bounds.set_height(bounds_in_pixel.width());
64 }
65 display.set_rotation(rotation);
66 display.SetScaleAndBounds(display.device_scale_factor(), new_bounds);
67 display_list().UpdateDisplay(display);
68 host_->SetRootTransform(GetRotationTransform() * GetUIScaleTransform());
69 }
70
71 void HeadlessScreen::SetUIScale(float ui_scale) {
72 ui_scale_ = ui_scale;
73 display::Display display(GetPrimaryDisplay());
74 gfx::Rect bounds_in_pixel(display.GetSizeInPixel());
75 gfx::Rect new_bounds = gfx::ToNearestRect(
76 gfx::ScaleRect(gfx::RectF(bounds_in_pixel), 1.0f / ui_scale));
77 display.SetScaleAndBounds(display.device_scale_factor(), new_bounds);
78 display_list().UpdateDisplay(display);
79 host_->SetRootTransform(GetRotationTransform() * GetUIScaleTransform());
80 }
81
82 void HeadlessScreen::SetWorkAreaInsets(const gfx::Insets& insets) {
83 display::Display display(GetPrimaryDisplay());
84 display.UpdateWorkAreaFromInsets(insets);
85 display_list().UpdateDisplay(display);
86 }
87
88 gfx::Transform HeadlessScreen::GetRotationTransform() const {
89 gfx::Transform rotate;
90 display::Display display(GetPrimaryDisplay());
91 switch (display.rotation()) {
92 case display::Display::ROTATE_0:
93 break;
94 case display::Display::ROTATE_90:
95 rotate.Translate(display.bounds().height(), 0);
96 rotate.Rotate(90);
97 break;
98 case display::Display::ROTATE_270:
99 rotate.Translate(0, display.bounds().width());
100 rotate.Rotate(270);
101 break;
102 case display::Display::ROTATE_180:
103 rotate.Translate(display.bounds().width(), display.bounds().height());
104 rotate.Rotate(180);
105 break;
106 }
107
108 return rotate;
109 }
110
111 gfx::Transform HeadlessScreen::GetUIScaleTransform() const {
112 gfx::Transform ui_scale;
113 ui_scale.Scale(1.0f / ui_scale_, 1.0f / ui_scale_);
114 return ui_scale;
115 }
116
117 void HeadlessScreen::OnWindowBoundsChanged(aura::Window* window,
118 const gfx::Rect& old_bounds,
119 const gfx::Rect& new_bounds) {
120 DCHECK_EQ(host_->window(), window);
121 display::Display display(GetPrimaryDisplay());
122 display.SetSize(gfx::ScaleToFlooredSize(new_bounds.size(),
123 display.device_scale_factor()));
124 display_list().UpdateDisplay(display);
125 }
126
127 void HeadlessScreen::OnWindowDestroying(aura::Window* window) {
128 if (host_->window() == window)
129 host_ = NULL;
130 }
131
132 gfx::Point HeadlessScreen::GetCursorScreenPoint() { 26 gfx::Point HeadlessScreen::GetCursorScreenPoint() {
133 return aura::Env::GetInstance()->last_mouse_location(); 27 return aura::Env::GetInstance()->last_mouse_location();
134 } 28 }
135 29
136 bool HeadlessScreen::IsWindowUnderCursor(gfx::NativeWindow window) { 30 bool HeadlessScreen::IsWindowUnderCursor(gfx::NativeWindow window) {
137 return GetWindowAtScreenPoint(GetCursorScreenPoint()) == window; 31 return GetWindowAtScreenPoint(GetCursorScreenPoint()) == window;
138 } 32 }
139 33
140 gfx::NativeWindow HeadlessScreen::GetWindowAtScreenPoint( 34 gfx::NativeWindow HeadlessScreen::GetWindowAtScreenPoint(
141 const gfx::Point& point) { 35 const gfx::Point& point) {
Sami 2017/02/09 18:08:22 Should we be calling into the platform here?
dvallet 2017/02/10 05:12:21 I'll have to check if this is needed for Mac and i
142 if (!host_ || !host_->window())
143 return nullptr; 36 return nullptr;
144 return host_->window()->GetTopWindowContainingPoint(point);
145 } 37 }
146 38
147 display::Display HeadlessScreen::GetDisplayNearestWindow( 39 display::Display HeadlessScreen::GetDisplayNearestWindow(
148 gfx::NativeWindow window) const { 40 gfx::NativeView window) const {
149 return GetPrimaryDisplay(); 41 return GetPrimaryDisplay();
150 } 42 }
151 43
152 HeadlessScreen::HeadlessScreen(const gfx::Rect& screen_bounds) 44 HeadlessScreen::HeadlessScreen(const gfx::Rect& screen_bounds) {
153 : host_(NULL), ui_scale_(1.0f) {
154 static int64_t synthesized_display_id = 2000; 45 static int64_t synthesized_display_id = 2000;
155 display::Display display(synthesized_display_id++); 46 display::Display display(synthesized_display_id++);
156 display.SetScaleAndBounds(1.0f, screen_bounds); 47 display.SetScaleAndBounds(1.0f, screen_bounds);
157 ProcessDisplayChanged(display, true /* is_primary */); 48 ProcessDisplayChanged(display, true /* is_primary */);
158 } 49 }
159 50
160 } // namespace headless 51 } // namespace headless
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698