OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "components/mus/ws/focus_controller.h" | 5 #include "components/mus/ws/focus_controller.h" |
6 | 6 |
7 #include "components/mus/ws/focus_controller_delegate.h" | 7 #include "components/mus/ws/focus_controller_observer.h" |
8 #include "components/mus/ws/server_window.h" | 8 #include "components/mus/ws/server_window.h" |
9 #include "components/mus/ws/server_window_drawn_tracker.h" | 9 #include "components/mus/ws/server_window_drawn_tracker.h" |
10 | 10 |
11 namespace mus { | 11 namespace mus { |
12 | |
13 namespace ws { | 12 namespace ws { |
14 | 13 |
15 FocusController::FocusController(FocusControllerDelegate* delegate) | 14 FocusController::FocusController() {} |
16 : delegate_(delegate) {} | |
17 | 15 |
18 FocusController::~FocusController() {} | 16 FocusController::~FocusController() {} |
19 | 17 |
20 void FocusController::SetFocusedWindow(ServerWindow* window) { | 18 void FocusController::SetFocusedWindow(ServerWindow* window) { |
21 if (GetFocusedWindow() == window) | 19 if (GetFocusedWindow() == window) |
22 return; | 20 return; |
23 | 21 |
24 SetFocusedWindowImpl(window, CHANGE_SOURCE_EXPLICIT); | 22 SetFocusedWindowImpl(FocusControllerChangeSource::EXPLICIT, window); |
25 } | 23 } |
26 | 24 |
27 ServerWindow* FocusController::GetFocusedWindow() { | 25 ServerWindow* FocusController::GetFocusedWindow() { |
28 return drawn_tracker_ ? drawn_tracker_->window() : nullptr; | 26 return drawn_tracker_ ? drawn_tracker_->window() : nullptr; |
29 } | 27 } |
30 | 28 |
| 29 void FocusController::AddObserver(FocusControllerObserver* observer) { |
| 30 observers_.AddObserver(observer); |
| 31 } |
| 32 |
| 33 void FocusController::RemoveObserver(FocusControllerObserver* observer) { |
| 34 observers_.RemoveObserver(observer); |
| 35 } |
| 36 |
31 bool FocusController::CanBeFocused(ServerWindow* window) const { | 37 bool FocusController::CanBeFocused(ServerWindow* window) const { |
32 // All ancestors of |window| must be drawn, and be focusable. | 38 // All ancestors of |window| must be drawn, and be focusable. |
33 for (ServerWindow* w = window; w; w = w->parent()) { | 39 for (ServerWindow* w = window; w; w = w->parent()) { |
34 if (!w->IsDrawn()) | 40 if (!w->IsDrawn()) |
35 return false; | 41 return false; |
36 if (!w->can_focus()) | 42 if (!w->can_focus()) |
37 return false; | 43 return false; |
38 } | 44 } |
39 | 45 |
40 // |window| must be a descendent of an activatable window. | 46 // |window| must be a descendent of an activatable window. |
41 for (ServerWindow* w = window; w; w = w->parent()) { | 47 for (ServerWindow* w = window; w; w = w->parent()) { |
42 if (CanBeActivated(w)) | 48 if (CanBeActivated(w)) |
43 return true; | 49 return true; |
44 } | 50 } |
45 | 51 |
46 return false; | 52 return false; |
47 } | 53 } |
48 | 54 |
49 bool FocusController::CanBeActivated(ServerWindow* window) const { | 55 bool FocusController::CanBeActivated(ServerWindow* window) const { |
50 // TODO(sad): Implement this. | 56 // TODO(sad): Implement this. |
51 return true; | 57 return true; |
52 } | 58 } |
53 | 59 |
54 void FocusController::SetFocusedWindowImpl(ServerWindow* window, | 60 void FocusController::SetFocusedWindowImpl( |
55 ChangeSource change_source) { | 61 FocusControllerChangeSource change_source, |
| 62 ServerWindow* window) { |
56 if (window && !CanBeFocused(window)) | 63 if (window && !CanBeFocused(window)) |
57 return; | 64 return; |
58 ServerWindow* old = GetFocusedWindow(); | 65 ServerWindow* old = GetFocusedWindow(); |
59 | 66 |
60 DCHECK(!window || window->IsDrawn()); | 67 DCHECK(!window || window->IsDrawn()); |
61 | 68 |
62 // TODO(sad): Activate the closest activatable ancestor window. | 69 // TODO(sad): Activate the closest activatable ancestor window. |
63 if (window) | 70 if (window) |
64 drawn_tracker_.reset(new ServerWindowDrawnTracker(window, this)); | 71 drawn_tracker_.reset(new ServerWindowDrawnTracker(window, this)); |
65 else | 72 else |
66 drawn_tracker_.reset(); | 73 drawn_tracker_.reset(); |
67 | 74 |
68 if (change_source == CHANGE_SOURCE_DRAWN_STATE_CHANGED) | 75 FOR_EACH_OBSERVER(FocusControllerObserver, observers_, |
69 delegate_->OnFocusChanged(old, window); | 76 OnFocusChanged(change_source, old, window)); |
70 } | 77 } |
71 | 78 |
72 void FocusController::OnDrawnStateChanged(ServerWindow* ancestor, | 79 void FocusController::OnDrawnStateChanged(ServerWindow* ancestor, |
73 ServerWindow* window, | 80 ServerWindow* window, |
74 bool is_drawn) { | 81 bool is_drawn) { |
75 DCHECK(!is_drawn); // We only observe when drawn. | 82 DCHECK(!is_drawn); // We only observe when drawn. |
76 SetFocusedWindowImpl(ancestor, CHANGE_SOURCE_DRAWN_STATE_CHANGED); | 83 SetFocusedWindowImpl(FocusControllerChangeSource::DRAWN_STATE_CHANGED, |
| 84 ancestor); |
77 } | 85 } |
78 | 86 |
79 } // namespace ws | 87 } // namespace ws |
80 | |
81 } // namespace mus | 88 } // namespace mus |
OLD | NEW |