| Index: ui/aura/mus/focus_synchronizer.cc
|
| diff --git a/ui/aura/mus/focus_synchronizer.cc b/ui/aura/mus/focus_synchronizer.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..8f69615ed114e661e44532fe83df35feb6538795
|
| --- /dev/null
|
| +++ b/ui/aura/mus/focus_synchronizer.cc
|
| @@ -0,0 +1,98 @@
|
| +// Copyright 2016 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "ui/aura/mus/focus_synchronizer.h"
|
| +
|
| +#include "base/auto_reset.h"
|
| +#include "services/ui/public/interfaces/window_tree.mojom.h"
|
| +#include "ui/aura/client/focus_client.h"
|
| +#include "ui/aura/env.h"
|
| +#include "ui/aura/mus/focus_synchronizer_delegate.h"
|
| +#include "ui/aura/mus/window_mus.h"
|
| +#include "ui/aura/window.h"
|
| +
|
| +namespace aura {
|
| +
|
| +FocusSynchronizer::FocusSynchronizer(FocusSynchronizerDelegate* delegate,
|
| + ui::mojom::WindowTree* window_tree)
|
| + : delegate_(delegate), window_tree_(window_tree) {
|
| + Env::GetInstance()->AddObserver(this);
|
| +}
|
| +
|
| +FocusSynchronizer::~FocusSynchronizer() {
|
| + SetActiveFocusClient(nullptr);
|
| + Env::GetInstance()->RemoveObserver(this);
|
| +}
|
| +
|
| +void FocusSynchronizer::SetFocusFromServer(WindowMus* window) {
|
| + if (focused_window_ == window)
|
| + return;
|
| +
|
| + DCHECK(!setting_focus_);
|
| + base::AutoReset<bool> focus_reset(&setting_focus_, true);
|
| + base::AutoReset<WindowMus*> window_setting_focus_to_reset(
|
| + &window_setting_focus_to_, window);
|
| + Env* env = aura::Env::GetInstance();
|
| + if (window) {
|
| + Window* root = window->GetWindow()->GetRootWindow();
|
| + if (env->active_focus_client_root() != root)
|
| + env->SetActiveFocusClient(aura::client::GetFocusClient(root), root);
|
| + window->GetWindow()->Focus();
|
| + } else if (env->active_focus_client()) {
|
| + env->active_focus_client()->FocusWindow(nullptr);
|
| + }
|
| +}
|
| +
|
| +void FocusSynchronizer::OnFocusedWindowDestroyed() {
|
| + focused_window_ = nullptr;
|
| +}
|
| +
|
| +void FocusSynchronizer::SetActiveFocusClient(
|
| + client::FocusClient* focus_client) {
|
| + if (focus_client == active_focus_client_)
|
| + return;
|
| +
|
| + if (active_focus_client_)
|
| + active_focus_client_->RemoveObserver(this);
|
| + active_focus_client_ = focus_client;
|
| + if (active_focus_client_)
|
| + active_focus_client_->AddObserver(this);
|
| +}
|
| +
|
| +void FocusSynchronizer::SetFocusedWindow(WindowMus* window) {
|
| + const uint32_t change_id = delegate_->CreateChangeIdForFocus(focused_window_);
|
| + focused_window_ = window;
|
| + window_tree_->SetFocus(change_id,
|
| + window ? window->server_id() : kInvalidServerId);
|
| +}
|
| +
|
| +void FocusSynchronizer::OnWindowFocused(Window* gained_focus,
|
| + Window* lost_focus) {
|
| + WindowMus* gained_focus_mus = WindowMus::Get(gained_focus);
|
| + if (setting_focus_ && gained_focus_mus == window_setting_focus_to_) {
|
| + focused_window_ = gained_focus_mus;
|
| + return;
|
| + }
|
| + SetFocusedWindow(gained_focus_mus);
|
| +}
|
| +
|
| +void FocusSynchronizer::OnWindowInitialized(Window* window) {}
|
| +
|
| +void FocusSynchronizer::OnActiveFocusClientChanged(
|
| + client::FocusClient* focus_client,
|
| + Window* window) {
|
| + SetActiveFocusClient(focus_client);
|
| + if (setting_focus_)
|
| + return;
|
| +
|
| + if (focus_client) {
|
| + Window* focused_window = focus_client->GetFocusedWindow();
|
| + SetFocusedWindow(focused_window ? WindowMus::Get(focused_window)
|
| + : WindowMus::Get(window));
|
| + } else {
|
| + SetFocusedWindow(nullptr);
|
| + }
|
| +}
|
| +
|
| +} // namespace aura
|
|
|