| OLD | NEW |
| 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 "ui/aura/mus/focus_synchronizer.h" | 5 #include "ui/aura/mus/focus_synchronizer.h" |
| 6 | 6 |
| 7 #include "base/auto_reset.h" | 7 #include "base/auto_reset.h" |
| 8 #include "services/ui/public/interfaces/window_tree.mojom.h" | 8 #include "services/ui/public/interfaces/window_tree.mojom.h" |
| 9 #include "ui/aura/client/aura_constants.h" |
| 9 #include "ui/aura/client/focus_client.h" | 10 #include "ui/aura/client/focus_client.h" |
| 10 #include "ui/aura/env.h" | |
| 11 #include "ui/aura/mus/focus_synchronizer_delegate.h" | 11 #include "ui/aura/mus/focus_synchronizer_delegate.h" |
| 12 #include "ui/aura/mus/window_mus.h" | 12 #include "ui/aura/mus/window_mus.h" |
| 13 #include "ui/aura/window.h" | 13 #include "ui/aura/window.h" |
| 14 | 14 |
| 15 namespace aura { | 15 namespace aura { |
| 16 | 16 |
| 17 FocusSynchronizer::FocusSynchronizer(FocusSynchronizerDelegate* delegate, | 17 FocusSynchronizer::FocusSynchronizer(FocusSynchronizerDelegate* delegate, |
| 18 ui::mojom::WindowTree* window_tree) | 18 ui::mojom::WindowTree* window_tree) |
| 19 : delegate_(delegate), window_tree_(window_tree) { | 19 : delegate_(delegate), window_tree_(window_tree) {} |
| 20 Env::GetInstance()->AddObserver(this); | 20 |
| 21 FocusSynchronizer::~FocusSynchronizer() { |
| 22 SetActiveFocusClientInternal(nullptr); |
| 21 } | 23 } |
| 22 | 24 |
| 23 FocusSynchronizer::~FocusSynchronizer() { | 25 void FocusSynchronizer::AddObserver(FocusSynchronizerObserver* observer) { |
| 24 SetActiveFocusClient(nullptr); | 26 observers_.AddObserver(observer); |
| 25 Env::GetInstance()->RemoveObserver(this); | 27 } |
| 28 |
| 29 void FocusSynchronizer::RemoveObserver(FocusSynchronizerObserver* observer) { |
| 30 observers_.RemoveObserver(observer); |
| 26 } | 31 } |
| 27 | 32 |
| 28 void FocusSynchronizer::SetFocusFromServer(WindowMus* window) { | 33 void FocusSynchronizer::SetFocusFromServer(WindowMus* window) { |
| 29 if (focused_window_ == window) | 34 if (focused_window_ == window) |
| 30 return; | 35 return; |
| 31 | 36 |
| 32 DCHECK(!setting_focus_); | 37 DCHECK(!setting_focus_); |
| 33 base::AutoReset<bool> focus_reset(&setting_focus_, true); | 38 base::AutoReset<bool> focus_reset(&setting_focus_, true); |
| 34 base::AutoReset<WindowMus*> window_setting_focus_to_reset( | 39 base::AutoReset<WindowMus*> window_setting_focus_to_reset( |
| 35 &window_setting_focus_to_, window); | 40 &window_setting_focus_to_, window); |
| 36 Env* env = Env::GetInstance(); | |
| 37 if (window) { | 41 if (window) { |
| 38 Window* root = window->GetWindow()->GetRootWindow(); | 42 Window* root = window->GetWindow()->GetRootWindow(); |
| 39 // The client should provide a focus client for all roots. | 43 // The client should provide a focus client for all roots. |
| 40 DCHECK(client::GetFocusClient(root)); | 44 DCHECK(client::GetFocusClient(root)); |
| 41 if (env->active_focus_client_root() != root) | 45 if (active_focus_client_root_ != root) |
| 42 env->SetActiveFocusClient(client::GetFocusClient(root), root); | 46 SetActiveFocusClient(client::GetFocusClient(root), root); |
| 43 window->GetWindow()->Focus(); | 47 window->GetWindow()->Focus(); |
| 44 } else if (env->active_focus_client()) { | 48 } else if (active_focus_client_) { |
| 45 env->active_focus_client()->FocusWindow(nullptr); | 49 active_focus_client_->FocusWindow(nullptr); |
| 46 } | 50 } |
| 47 } | 51 } |
| 48 | 52 |
| 49 void FocusSynchronizer::OnFocusedWindowDestroyed() { | 53 void FocusSynchronizer::OnFocusedWindowDestroyed() { |
| 50 focused_window_ = nullptr; | 54 focused_window_ = nullptr; |
| 51 } | 55 } |
| 52 | 56 |
| 53 void FocusSynchronizer::SetActiveFocusClient( | 57 void FocusSynchronizer::SetActiveFocusClient(client::FocusClient* focus_client, |
| 58 Window* focus_client_root) { |
| 59 if (focus_client_root == active_focus_client_root_) { |
| 60 DCHECK_EQ(focus_client, active_focus_client_); |
| 61 return; |
| 62 } |
| 63 |
| 64 if (active_focus_client_root_) |
| 65 active_focus_client_root_->RemoveObserver(this); |
| 66 active_focus_client_root_ = focus_client_root; |
| 67 if (active_focus_client_root_) |
| 68 active_focus_client_root_->AddObserver(this); |
| 69 |
| 70 OnActiveFocusClientChanged(focus_client, focus_client_root); |
| 71 for (FocusSynchronizerObserver& observer : observers_) |
| 72 observer.OnActiveFocusClientChanged(focus_client, focus_client_root); |
| 73 } |
| 74 |
| 75 void FocusSynchronizer::SetActiveFocusClientInternal( |
| 54 client::FocusClient* focus_client) { | 76 client::FocusClient* focus_client) { |
| 55 if (focus_client == active_focus_client_) | 77 if (focus_client == active_focus_client_) |
| 56 return; | 78 return; |
| 57 | 79 |
| 58 if (active_focus_client_) | 80 if (active_focus_client_) |
| 59 active_focus_client_->RemoveObserver(this); | 81 active_focus_client_->RemoveObserver(this); |
| 60 active_focus_client_ = focus_client; | 82 active_focus_client_ = focus_client; |
| 61 if (active_focus_client_) | 83 if (active_focus_client_) |
| 62 active_focus_client_->AddObserver(this); | 84 active_focus_client_->AddObserver(this); |
| 63 } | 85 } |
| 64 | 86 |
| 65 void FocusSynchronizer::SetFocusedWindow(WindowMus* window) { | 87 void FocusSynchronizer::SetFocusedWindow(WindowMus* window) { |
| 66 const uint32_t change_id = delegate_->CreateChangeIdForFocus(focused_window_); | 88 const uint32_t change_id = delegate_->CreateChangeIdForFocus(focused_window_); |
| 67 focused_window_ = window; | 89 focused_window_ = window; |
| 68 window_tree_->SetFocus(change_id, | 90 window_tree_->SetFocus(change_id, |
| 69 window ? window->server_id() : kInvalidServerId); | 91 window ? window->server_id() : kInvalidServerId); |
| 70 } | 92 } |
| 71 | 93 |
| 94 void FocusSynchronizer::OnActiveFocusClientChanged( |
| 95 client::FocusClient* focus_client, |
| 96 Window* focus_client_root) { |
| 97 SetActiveFocusClientInternal(focus_client); |
| 98 if (setting_focus_) |
| 99 return; |
| 100 |
| 101 if (focus_client) { |
| 102 Window* focused_window = focus_client->GetFocusedWindow(); |
| 103 SetFocusedWindow(focused_window ? WindowMus::Get(focused_window) |
| 104 : WindowMus::Get(focus_client_root)); |
| 105 } else { |
| 106 SetFocusedWindow(nullptr); |
| 107 } |
| 108 } |
| 109 |
| 72 void FocusSynchronizer::OnWindowFocused(Window* gained_focus, | 110 void FocusSynchronizer::OnWindowFocused(Window* gained_focus, |
| 73 Window* lost_focus) { | 111 Window* lost_focus) { |
| 74 WindowMus* gained_focus_mus = WindowMus::Get(gained_focus); | 112 WindowMus* gained_focus_mus = WindowMus::Get(gained_focus); |
| 75 if (setting_focus_ && gained_focus_mus == window_setting_focus_to_) { | 113 if (setting_focus_ && gained_focus_mus == window_setting_focus_to_) { |
| 76 focused_window_ = gained_focus_mus; | 114 focused_window_ = gained_focus_mus; |
| 77 return; | 115 return; |
| 78 } | 116 } |
| 79 SetFocusedWindow(gained_focus_mus); | 117 SetFocusedWindow(gained_focus_mus); |
| 80 } | 118 } |
| 81 | 119 |
| 82 void FocusSynchronizer::OnWindowInitialized(Window* window) {} | 120 void FocusSynchronizer::OnWindowDestroying(Window* window) { |
| 121 SetActiveFocusClient(nullptr, nullptr); |
| 122 } |
| 83 | 123 |
| 84 void FocusSynchronizer::OnActiveFocusClientChanged( | 124 void FocusSynchronizer::OnWindowPropertyChanged(Window* window, |
| 85 client::FocusClient* focus_client, | 125 const void* key, |
| 86 Window* window) { | 126 intptr_t old) { |
| 87 SetActiveFocusClient(focus_client); | 127 if (key != client::kFocusClientKey) |
| 88 if (setting_focus_) | |
| 89 return; | 128 return; |
| 90 | 129 |
| 91 if (focus_client) { | 130 // Assume if the focus client changes the window is being destroyed. |
| 92 Window* focused_window = focus_client->GetFocusedWindow(); | 131 SetActiveFocusClient(nullptr, nullptr); |
| 93 SetFocusedWindow(focused_window ? WindowMus::Get(focused_window) | |
| 94 : WindowMus::Get(window)); | |
| 95 } else { | |
| 96 SetFocusedWindow(nullptr); | |
| 97 } | |
| 98 } | 132 } |
| 99 | 133 |
| 100 } // namespace aura | 134 } // namespace aura |
| OLD | NEW |