blob: 5bc44ad004753b7f4de317c817bef415991ad8eb [file] [log] [blame]
[email protected]55ad8c12014-01-17 18:24:331// 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
5#include "ash/screen_util.h"
6
Ahmed Fakhry1ac713b2018-11-06 18:57:467#include "ash/display/display_configuration_controller.h"
8#include "ash/display/mirror_window_controller.h"
9#include "ash/display/window_tree_host_manager.h"
Sammie Quon17d0def2018-12-14 22:19:1510#include "ash/public/cpp/shell_window_ids.h"
James Cook840177e2017-05-25 02:20:0111#include "ash/shelf/shelf.h"
[email protected]55ad8c12014-01-17 18:24:3312#include "ash/shell.h"
Aga Wronskaf053e522019-03-27 00:08:4213#include "ash/wm/work_area_insets.h"
[email protected]55ad8c12014-01-17 18:24:3314#include "base/logging.h"
15#include "ui/aura/client/screen_position_client.h"
[email protected]fcc51c952014-02-21 21:31:2616#include "ui/aura/window_event_dispatcher.h"
Malay Keshavfd6fd0c2018-07-31 22:07:4717#include "ui/aura/window_tree_host.h"
oshimaf84b0da722016-04-27 19:47:1918#include "ui/display/display.h"
rjkroege72f8154f2016-10-29 00:49:0219#include "ui/display/manager/display_manager.h"
oshimaf84b0da722016-04-27 19:47:1920#include "ui/display/screen.h"
oshima5337ca92015-07-18 02:23:5721#include "ui/gfx/geometry/size_conversions.h"
yhanada698af192017-02-23 10:57:0722#include "ui/wm/core/coordinate_conversion.h"
[email protected]55ad8c12014-01-17 18:24:3323
24namespace ash {
25
Qiang Xu07d7c9e32018-01-18 20:25:4426namespace screen_util {
27
28gfx::Rect GetMaximizedWindowBoundsInParent(aura::Window* window) {
msw257a6a232017-06-01 01:46:3029 if (Shelf::ForWindow(window)->shelf_widget())
[email protected]55ad8c12014-01-17 18:24:3330 return GetDisplayWorkAreaBoundsInParent(window);
sky00f0b892017-05-05 17:06:2431 return GetDisplayBoundsInParent(window);
[email protected]55ad8c12014-01-17 18:24:3332}
33
Qiang Xu07d7c9e32018-01-18 20:25:4434gfx::Rect GetDisplayBoundsInParent(aura::Window* window) {
yhanada698af192017-02-23 10:57:0735 gfx::Rect result =
36 display::Screen::GetScreen()->GetDisplayNearestWindow(window).bounds();
37 ::wm::ConvertRectFromScreen(window->parent(), &result);
38 return result;
[email protected]55ad8c12014-01-17 18:24:3339}
40
Ahmed Fakhry5d73bdf72019-02-27 18:21:0741gfx::Rect GetFullscreenWindowBoundsInParent(aura::Window* window) {
42 gfx::Rect result = GetDisplayBoundsInParent(window);
Aga Wronskaf053e522019-03-27 00:08:4243 const WorkAreaInsets* const work_area_insets =
44 WorkAreaInsets::ForWindow(window->GetRootWindow());
Aga Wronska5f0a7e072019-03-26 17:31:2045 result.Inset(0,
Aga Wronskaf053e522019-03-27 00:08:4246 work_area_insets->accessibility_panel_height() +
47 work_area_insets->docked_magnifier_height(),
Aga Wronska5f0a7e072019-03-26 17:31:2048 0, 0);
Ahmed Fakhry5d73bdf72019-02-27 18:21:0749 return result;
50}
51
Qiang Xu07d7c9e32018-01-18 20:25:4452gfx::Rect GetDisplayWorkAreaBoundsInParent(aura::Window* window) {
yhanada698af192017-02-23 10:57:0753 gfx::Rect result =
54 display::Screen::GetScreen()->GetDisplayNearestWindow(window).work_area();
55 ::wm::ConvertRectFromScreen(window->parent(), &result);
56 return result;
[email protected]55ad8c12014-01-17 18:24:3357}
58
Qiang Xu07d7c9e32018-01-18 20:25:4459gfx::Rect GetDisplayWorkAreaBoundsInParentForLockScreen(aura::Window* window) {
tbarzic98adf002017-05-26 17:28:1760 gfx::Rect bounds = Shelf::ForWindow(window)->GetUserWorkAreaBounds();
61 ::wm::ConvertRectFromScreen(window->parent(), &bounds);
62 return bounds;
63}
64
Sammie Quon17d0def2018-12-14 22:19:1565gfx::Rect GetDisplayWorkAreaBoundsInParentForDefaultContainer(
66 aura::Window* window) {
67 aura::Window* root_window = window->GetRootWindow();
68 return GetDisplayWorkAreaBoundsInParent(
69 root_window->GetChildById(kShellWindowId_DefaultContainer));
70}
71
72gfx::Rect GetDisplayWorkAreaBoundsInScreenForDefaultContainer(
73 aura::Window* window) {
74 gfx::Rect bounds =
75 GetDisplayWorkAreaBoundsInParentForDefaultContainer(window);
76 ::wm::ConvertRectToScreen(window->GetRootWindow(), &bounds);
77 return bounds;
78}
79
Qiang Xu07d7c9e32018-01-18 20:25:4480gfx::Rect GetDisplayBoundsWithShelf(aura::Window* window) {
Ahmed Fakhry4f8e3722017-10-31 21:01:5881 if (!Shell::Get()->display_manager()->IsInUnifiedMode())
82 return window->GetRootWindow()->bounds();
sky00f0b892017-05-05 17:06:2483
Ahmed Fakhry1ac713b2018-11-06 18:57:4684 // In Unified Mode, the display that should contain the shelf depends on the
85 // current shelf alignment.
86 const display::Display shelf_display =
87 Shell::Get()
88 ->display_configuration_controller()
89 ->GetPrimaryMirroringDisplayForUnifiedDesktop();
90 DCHECK_NE(shelf_display.id(), display::kInvalidDisplayId);
91 gfx::RectF shelf_display_screen_bounds(shelf_display.bounds());
Ahmed Fakhry4f8e3722017-10-31 21:01:5892
Ahmed Fakhry1ac713b2018-11-06 18:57:4693 // Transform the bounds back to the unified host's coordinates.
94 auto inverse_unified_transform =
95 window->GetRootWindow()->GetHost()->GetInverseRootTransform();
96 inverse_unified_transform.TransformRect(&shelf_display_screen_bounds);
Ahmed Fakhry4f8e3722017-10-31 21:01:5897
Ahmed Fakhry1ac713b2018-11-06 18:57:4698 return gfx::ToEnclosingRect(shelf_display_screen_bounds);
sky00f0b892017-05-05 17:06:2499}
100
Malay Keshavfd6fd0c2018-07-31 22:07:47101gfx::Rect SnapBoundsToDisplayEdge(const gfx::Rect& bounds,
102 const aura::Window* window) {
103 const aura::WindowTreeHost* host = window->GetHost();
104 if (!host)
105 return bounds;
106
107 const float dsf = host->device_scale_factor();
108 const gfx::Rect display_bounds_in_pixel = host->GetBoundsInPixels();
109 const gfx::Rect display_bounds_in_dip = window->GetRootWindow()->bounds();
110 const gfx::Rect bounds_in_pixel = gfx::ScaleToEnclosedRect(bounds, dsf);
111
112 // Adjusts |bounds| such that the scaled enclosed bounds are atleast as big as
113 // the scaled enclosing unadjusted bounds.
114 gfx::Rect snapped_bounds = bounds;
115 if ((display_bounds_in_dip.width() == bounds.width() &&
116 bounds_in_pixel.width() != display_bounds_in_pixel.width()) ||
117 (bounds.right() == display_bounds_in_dip.width() &&
118 bounds_in_pixel.right() != display_bounds_in_pixel.width())) {
119 snapped_bounds.Inset(0, 0, -1, 0);
120 DCHECK_GE(gfx::ScaleToEnclosedRect(snapped_bounds, dsf).right(),
121 gfx::ScaleToEnclosingRect(bounds, dsf).right());
122 }
123 if ((display_bounds_in_dip.height() == bounds.height() &&
124 bounds_in_pixel.height() != display_bounds_in_pixel.height()) ||
125 (bounds.bottom() == display_bounds_in_dip.height() &&
126 bounds_in_pixel.bottom() != display_bounds_in_pixel.height())) {
127 snapped_bounds.Inset(0, 0, 0, -1);
128 DCHECK_GE(gfx::ScaleToEnclosedRect(snapped_bounds, dsf).bottom(),
129 gfx::ScaleToEnclosingRect(bounds, dsf).bottom());
130 }
131
132 return snapped_bounds;
133}
134
Qiang Xu07d7c9e32018-01-18 20:25:44135} // namespace screen_util
136
[email protected]55ad8c12014-01-17 18:24:33137} // namespace ash