OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "ui/aura/mus/focus_synchronizer.h" |
| 6 |
| 7 #include "base/auto_reset.h" |
| 8 #include "services/ui/public/interfaces/window_tree.mojom.h" |
| 9 #include "ui/aura/client/focus_client.h" |
| 10 #include "ui/aura/env.h" |
| 11 #include "ui/aura/mus/focus_synchronizer_delegate.h" |
| 12 #include "ui/aura/mus/window_mus.h" |
| 13 #include "ui/aura/window.h" |
| 14 |
| 15 namespace aura { |
| 16 |
| 17 FocusSynchronizer::FocusSynchronizer(FocusSynchronizerDelegate* delegate, |
| 18 ui::mojom::WindowTree* window_tree) |
| 19 : delegate_(delegate), window_tree_(window_tree) { |
| 20 Env::GetInstance()->AddObserver(this); |
| 21 } |
| 22 |
| 23 FocusSynchronizer::~FocusSynchronizer() { |
| 24 SetActiveFocusClient(nullptr); |
| 25 Env::GetInstance()->RemoveObserver(this); |
| 26 } |
| 27 |
| 28 void FocusSynchronizer::SetFocusFromServer(WindowMus* window) { |
| 29 if (focused_window_ == window) |
| 30 return; |
| 31 |
| 32 DCHECK(!setting_focus_); |
| 33 base::AutoReset<bool> focus_reset(&setting_focus_, true); |
| 34 base::AutoReset<WindowMus*> window_setting_focus_to_reset( |
| 35 &window_setting_focus_to_, window); |
| 36 Env* env = aura::Env::GetInstance(); |
| 37 if (window) { |
| 38 Window* root = window->GetWindow()->GetRootWindow(); |
| 39 if (env->active_focus_client_root() != root) |
| 40 env->SetActiveFocusClient(aura::client::GetFocusClient(root), root); |
| 41 window->GetWindow()->Focus(); |
| 42 } else if (env->active_focus_client()) { |
| 43 env->active_focus_client()->FocusWindow(nullptr); |
| 44 } |
| 45 } |
| 46 |
| 47 void FocusSynchronizer::OnFocusedWindowDestroyed() { |
| 48 focused_window_ = nullptr; |
| 49 } |
| 50 |
| 51 void FocusSynchronizer::SetActiveFocusClient( |
| 52 client::FocusClient* focus_client) { |
| 53 if (focus_client == active_focus_client_) |
| 54 return; |
| 55 |
| 56 if (active_focus_client_) |
| 57 active_focus_client_->RemoveObserver(this); |
| 58 active_focus_client_ = focus_client; |
| 59 if (active_focus_client_) |
| 60 active_focus_client_->AddObserver(this); |
| 61 } |
| 62 |
| 63 void FocusSynchronizer::SetFocusedWindow(WindowMus* window) { |
| 64 const uint32_t change_id = delegate_->CreateChangeIdForFocus(focused_window_); |
| 65 focused_window_ = window; |
| 66 window_tree_->SetFocus(change_id, |
| 67 window ? window->server_id() : kInvalidServerId); |
| 68 } |
| 69 |
| 70 void FocusSynchronizer::OnWindowFocused(Window* gained_focus, |
| 71 Window* lost_focus) { |
| 72 WindowMus* gained_focus_mus = WindowMus::Get(gained_focus); |
| 73 if (setting_focus_ && gained_focus_mus == window_setting_focus_to_) { |
| 74 focused_window_ = gained_focus_mus; |
| 75 return; |
| 76 } |
| 77 SetFocusedWindow(gained_focus_mus); |
| 78 } |
| 79 |
| 80 void FocusSynchronizer::OnWindowInitialized(Window* window) {} |
| 81 |
| 82 void FocusSynchronizer::OnActiveFocusClientChanged( |
| 83 client::FocusClient* focus_client, |
| 84 Window* window) { |
| 85 SetActiveFocusClient(focus_client); |
| 86 if (setting_focus_) |
| 87 return; |
| 88 |
| 89 if (focus_client) { |
| 90 Window* focused_window = focus_client->GetFocusedWindow(); |
| 91 SetFocusedWindow(focused_window ? WindowMus::Get(focused_window) |
| 92 : WindowMus::Get(window)); |
| 93 } else { |
| 94 SetFocusedWindow(nullptr); |
| 95 } |
| 96 } |
| 97 |
| 98 } // namespace aura |
OLD | NEW |