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

Side by Side Diff: ash/wm/window_state.cc

Issue 1921353002: Moves handful of files to ash/wm/common (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@shell_ids
Patch Set: merge to trunk Created 4 years, 7 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
« no previous file with comments | « ash/wm/window_state.h ('k') | ash/wm/window_state_aura.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2013 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/wm/window_state.h"
6
7 #include <utility>
8
9 #include "ash/wm/common/window_positioning_utils.h"
10 #include "ash/wm/common/wm_event.h"
11 #include "ash/wm/common/wm_screen_util.h"
12 #include "ash/wm/common/wm_window.h"
13 #include "ash/wm/default_state.h"
14 #include "ash/wm/window_state_delegate.h"
15 #include "ash/wm/window_state_observer.h"
16 #include "base/auto_reset.h"
17 #include "ui/gfx/display.h"
18 #include "ui/gfx/screen.h"
19
20 namespace ash {
21 namespace wm {
22
23 namespace {
24
25 WMEventType WMEventTypeFromShowState(ui::WindowShowState requested_show_state) {
26 switch (requested_show_state) {
27 case ui::SHOW_STATE_DEFAULT:
28 case ui::SHOW_STATE_NORMAL:
29 return WM_EVENT_NORMAL;
30 case ui::SHOW_STATE_MINIMIZED:
31 return WM_EVENT_MINIMIZE;
32 case ui::SHOW_STATE_MAXIMIZED:
33 return WM_EVENT_MAXIMIZE;
34 case ui::SHOW_STATE_FULLSCREEN:
35 return WM_EVENT_FULLSCREEN;
36 case ui::SHOW_STATE_INACTIVE:
37 return WM_EVENT_SHOW_INACTIVE;
38 case ui::SHOW_STATE_DOCKED:
39 return WM_EVENT_DOCK;
40 case ui::SHOW_STATE_END:
41 NOTREACHED() << "No WMEvent defined for the show state:"
42 << requested_show_state;
43 }
44 return WM_EVENT_NORMAL;
45 }
46
47 } // namespace
48
49 WindowState::~WindowState() {
50 }
51
52 bool WindowState::HasDelegate() const {
53 return !!delegate_;
54 }
55
56 void WindowState::SetDelegate(std::unique_ptr<WindowStateDelegate> delegate) {
57 DCHECK(!delegate_.get());
58 delegate_ = std::move(delegate);
59 }
60
61 WindowStateType WindowState::GetStateType() const {
62 return current_state_->GetType();
63 }
64
65 bool WindowState::IsMinimized() const {
66 return GetStateType() == WINDOW_STATE_TYPE_MINIMIZED ||
67 GetStateType() == WINDOW_STATE_TYPE_DOCKED_MINIMIZED;
68 }
69
70 bool WindowState::IsMaximized() const {
71 return GetStateType() == WINDOW_STATE_TYPE_MAXIMIZED;
72 }
73
74 bool WindowState::IsFullscreen() const {
75 return GetStateType() == WINDOW_STATE_TYPE_FULLSCREEN;
76 }
77
78 bool WindowState::IsMaximizedOrFullscreen() const {
79 return GetStateType() == WINDOW_STATE_TYPE_FULLSCREEN ||
80 GetStateType() == WINDOW_STATE_TYPE_MAXIMIZED;
81 }
82
83 bool WindowState::IsSnapped() const {
84 return GetStateType() == WINDOW_STATE_TYPE_LEFT_SNAPPED ||
85 GetStateType() == WINDOW_STATE_TYPE_RIGHT_SNAPPED;
86 }
87
88 bool WindowState::IsNormalStateType() const {
89 return GetStateType() == WINDOW_STATE_TYPE_NORMAL ||
90 GetStateType() == WINDOW_STATE_TYPE_DEFAULT;
91 }
92
93 bool WindowState::IsNormalOrSnapped() const {
94 return IsNormalStateType() || IsSnapped();
95 }
96
97 bool WindowState::IsActive() const {
98 return window_->IsActive();
99 }
100
101 bool WindowState::IsDocked() const {
102 return GetStateType() == WINDOW_STATE_TYPE_DOCKED ||
103 GetStateType() == WINDOW_STATE_TYPE_DOCKED_MINIMIZED;
104 }
105
106 bool WindowState::IsUserPositionable() const {
107 return (window_->GetType() == ui::wm::WINDOW_TYPE_NORMAL ||
108 window_->GetType() == ui::wm::WINDOW_TYPE_PANEL);
109 }
110
111 bool WindowState::CanMaximize() const {
112 // Window must have the kCanMaximizeKey and have no maximum width or height.
113 if (!window_->CanMaximize())
114 return false;
115
116 if (!window_->HasNonClientArea())
117 return true;
118
119 gfx::Size max_size = window_->GetMaximumSize();
120 return !max_size.width() && !max_size.height();
121 }
122
123 bool WindowState::CanMinimize() const {
124 return window_->CanMinimize();
125 }
126
127 bool WindowState::CanResize() const {
128 return window_->CanResize();
129 }
130
131 bool WindowState::CanActivate() const {
132 return window_->CanActivate();
133 }
134
135 bool WindowState::CanSnap() const {
136 if (!CanResize() || window_->GetType() == ui::wm::WINDOW_TYPE_PANEL ||
137 window_->GetTransientParent()) {
138 return false;
139 }
140 // If a window cannot be maximized, assume it cannot snap either.
141 // TODO(oshima): We should probably snap if the maximum size is greater than
142 // the snapped size.
143 return CanMaximize();
144 }
145
146 bool WindowState::HasRestoreBounds() const {
147 return window_->HasRestoreBounds();
148 }
149
150 void WindowState::Maximize() {
151 window_->Maximize();
152 }
153
154 void WindowState::Minimize() {
155 window_->Minimize();
156 }
157
158 void WindowState::Unminimize() {
159 window_->Unminimize();
160 }
161
162 void WindowState::Activate() {
163 window_->Activate();
164 }
165
166 void WindowState::Deactivate() {
167 window_->Deactivate();
168 }
169
170 void WindowState::Restore() {
171 if (!IsNormalStateType()) {
172 const WMEvent event(WM_EVENT_NORMAL);
173 OnWMEvent(&event);
174 }
175 }
176
177 void WindowState::DisableAlwaysOnTop(WmWindow* window_on_top) {
178 DCHECK(window_on_top);
179 if (GetAlwaysOnTop()) {
180 // |window_| is hidden first to avoid canceling fullscreen mode when it is
181 // no longer always on top and gets added to default container. This avoids
182 // sending redundant OnFullscreenStateChanged to the layout manager. The
183 // |window_| visibility is restored after it no longer obscures the
184 // |window_on_top|.
185 bool visible = window_->IsVisible();
186 if (visible)
187 window_->Hide();
188 window_->SetAlwaysOnTop(false);
189 // Technically it is possible that a |window_| could make itself
190 // always_on_top really quickly. This is probably not a realistic case but
191 // check if the two windows are in the same container just in case.
192 if (window_on_top->GetParent() == window_->GetParent())
193 window_->GetParent()->StackChildAbove(window_on_top, window_);
194 if (visible)
195 window_->Show();
196 cached_always_on_top_ = true;
197 }
198 }
199
200 void WindowState::RestoreAlwaysOnTop() {
201 if (delegate() && delegate()->RestoreAlwaysOnTop(this))
202 return;
203 if (cached_always_on_top_) {
204 cached_always_on_top_ = false;
205 window_->SetAlwaysOnTop(true);
206 }
207 }
208
209 void WindowState::OnWMEvent(const WMEvent* event) {
210 current_state_->OnWMEvent(this, event);
211 }
212
213 void WindowState::SaveCurrentBoundsForRestore() {
214 gfx::Rect bounds_in_screen =
215 window_->GetParent()->ConvertRectToScreen(window_->GetBounds());
216 SetRestoreBoundsInScreen(bounds_in_screen);
217 }
218
219 gfx::Rect WindowState::GetRestoreBoundsInScreen() const {
220 return window_->GetRestoreBoundsInScreen();
221 }
222
223 gfx::Rect WindowState::GetRestoreBoundsInParent() const {
224 return window_->GetParent()->ConvertRectFromScreen(
225 GetRestoreBoundsInScreen());
226 }
227
228 void WindowState::SetRestoreBoundsInScreen(const gfx::Rect& bounds) {
229 window_->SetRestoreBoundsInScreen(bounds);
230 }
231
232 void WindowState::SetRestoreBoundsInParent(const gfx::Rect& bounds) {
233 SetRestoreBoundsInScreen(window_->GetParent()->ConvertRectToScreen(bounds));
234 }
235
236 void WindowState::ClearRestoreBounds() {
237 window_->ClearRestoreBounds();
238 }
239
240 std::unique_ptr<WindowState::State> WindowState::SetStateObject(
241 std::unique_ptr<WindowState::State> new_state) {
242 current_state_->DetachState(this);
243 std::unique_ptr<WindowState::State> old_object = std::move(current_state_);
244 current_state_ = std::move(new_state);
245 current_state_->AttachState(this, old_object.get());
246 return old_object;
247 }
248
249 void WindowState::SetPreAutoManageWindowBounds(
250 const gfx::Rect& bounds) {
251 pre_auto_manage_window_bounds_.reset(new gfx::Rect(bounds));
252 }
253
254 void WindowState::AddObserver(WindowStateObserver* observer) {
255 observer_list_.AddObserver(observer);
256 }
257
258 void WindowState::RemoveObserver(WindowStateObserver* observer) {
259 observer_list_.RemoveObserver(observer);
260 }
261
262 void WindowState::set_bounds_changed_by_user(bool bounds_changed_by_user) {
263 bounds_changed_by_user_ = bounds_changed_by_user;
264 if (bounds_changed_by_user)
265 pre_auto_manage_window_bounds_.reset();
266 }
267
268 void WindowState::CreateDragDetails(const gfx::Point& point_in_parent,
269 int window_component,
270 aura::client::WindowMoveSource source) {
271 drag_details_.reset(
272 new DragDetails(window_, point_in_parent, window_component, source));
273 }
274
275 void WindowState::DeleteDragDetails() {
276 drag_details_.reset();
277 }
278
279 void WindowState::SetAndClearRestoreBounds() {
280 DCHECK(HasRestoreBounds());
281 SetBoundsInScreen(GetRestoreBoundsInScreen());
282 ClearRestoreBounds();
283 }
284
285 void WindowState::OnWindowShowStateChanged() {
286 if (!ignore_property_change_) {
287 WMEvent event(WMEventTypeFromShowState(GetShowState()));
288 OnWMEvent(&event);
289 }
290 }
291
292 WindowState::WindowState(WmWindow* window)
293 : window_(window),
294 window_position_managed_(false),
295 bounds_changed_by_user_(false),
296 panel_attached_(true),
297 ignored_by_shelf_(false),
298 can_consume_system_keys_(false),
299 top_row_keys_are_function_keys_(false),
300 unminimize_to_restore_bounds_(false),
301 in_immersive_fullscreen_(false),
302 hide_shelf_when_fullscreen_(true),
303 minimum_visibility_(false),
304 can_be_dragged_(true),
305 cached_always_on_top_(false),
306 ignore_property_change_(false),
307 current_state_(new DefaultState(ToWindowStateType(GetShowState()))) {}
308
309 bool WindowState::GetAlwaysOnTop() const {
310 return window_->IsAlwaysOnTop();
311 }
312
313 ui::WindowShowState WindowState::GetShowState() const {
314 return window_->GetShowState();
315 }
316
317 void WindowState::SetBoundsInScreen(
318 const gfx::Rect& bounds_in_screen) {
319 gfx::Rect bounds_in_parent =
320 window_->GetParent()->ConvertRectFromScreen(bounds_in_screen);
321 window_->SetBounds(bounds_in_parent);
322 }
323
324 void WindowState::AdjustSnappedBounds(gfx::Rect* bounds) {
325 if (is_dragged() || !IsSnapped())
326 return;
327 gfx::Rect maximized_bounds = GetMaximizedWindowBoundsInParent(window_);
328 if (GetStateType() == WINDOW_STATE_TYPE_LEFT_SNAPPED)
329 bounds->set_x(maximized_bounds.x());
330 else if (GetStateType() == WINDOW_STATE_TYPE_RIGHT_SNAPPED)
331 bounds->set_x(maximized_bounds.right() - bounds->width());
332 bounds->set_y(maximized_bounds.y());
333 bounds->set_height(maximized_bounds.height());
334 }
335
336 void WindowState::UpdateWindowShowStateFromStateType() {
337 ui::WindowShowState new_window_state =
338 ToWindowShowState(current_state_->GetType());
339 if (new_window_state != GetShowState()) {
340 base::AutoReset<bool> resetter(&ignore_property_change_, true);
341 window_->SetShowState(new_window_state);
342 }
343 }
344
345 void WindowState::NotifyPreStateTypeChange(
346 WindowStateType old_window_state_type) {
347 FOR_EACH_OBSERVER(WindowStateObserver, observer_list_,
348 OnPreWindowStateTypeChange(this, old_window_state_type));
349 }
350
351 void WindowState::NotifyPostStateTypeChange(
352 WindowStateType old_window_state_type) {
353 FOR_EACH_OBSERVER(WindowStateObserver, observer_list_,
354 OnPostWindowStateTypeChange(this, old_window_state_type));
355 }
356
357 void WindowState::SetBoundsDirect(const gfx::Rect& bounds) {
358 gfx::Rect actual_new_bounds(bounds);
359 // Ensure we don't go smaller than our minimum bounds in "normal" window
360 // modes
361 if (window_->HasNonClientArea() && !IsMaximized() && !IsFullscreen()) {
362 // Get the minimum usable size of the minimum size and the screen size.
363 gfx::Size min_size = window_->GetMinimumSize();
364 min_size.SetToMin(window_->GetDisplayNearestWindow().work_area().size());
365
366 actual_new_bounds.set_width(
367 std::max(min_size.width(), actual_new_bounds.width()));
368 actual_new_bounds.set_height(
369 std::max(min_size.height(), actual_new_bounds.height()));
370 }
371 window_->SetBoundsDirect(actual_new_bounds);
372 }
373
374 void WindowState::SetBoundsConstrained(const gfx::Rect& bounds) {
375 gfx::Rect work_area_in_parent = GetDisplayWorkAreaBoundsInParent(window_);
376 gfx::Rect child_bounds(bounds);
377 AdjustBoundsSmallerThan(work_area_in_parent.size(), &child_bounds);
378 SetBoundsDirect(child_bounds);
379 }
380
381 void WindowState::SetBoundsDirectAnimated(const gfx::Rect& bounds) {
382 window_->SetBoundsDirectAnimated(bounds);
383 }
384
385 void WindowState::SetBoundsDirectCrossFade(const gfx::Rect& new_bounds) {
386 // Some test results in invoking CrossFadeToBounds when window is not visible.
387 // No animation is necessary in that case, thus just change the bounds and
388 // quit.
389 if (!window_->GetTargetVisibility()) {
390 SetBoundsConstrained(new_bounds);
391 return;
392 }
393
394 window_->SetBoundsDirectCrossFade(new_bounds);
395 }
396
397 } // namespace wm
398 } // namespace ash
OLDNEW
« no previous file with comments | « ash/wm/window_state.h ('k') | ash/wm/window_state_aura.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698