blob: 9200fab48103dfc1f76844a3c4ab9a5b7435cce6 [file] [log] [blame]
// Copyright 2018 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 "ash/shell_state.h"
#include <memory>
#include <utility>
#include "ash/shell.h"
#include "ash/ws/window_service_owner.h"
#include "services/ws/window_service.h"
#include "ui/display/display.h"
#include "ui/display/screen.h"
namespace ash {
ShellState::ShellState() = default;
ShellState::~ShellState() = default;
aura::Window* ShellState::GetRootWindowForNewWindows() const {
if (scoped_root_window_for_new_windows_)
return scoped_root_window_for_new_windows_;
return root_window_for_new_windows_;
}
void ShellState::SetRootWindowForNewWindows(aura::Window* root) {
if (root == root_window_for_new_windows_)
return;
root_window_for_new_windows_ = root;
NotifyAllClients();
}
void ShellState::NotifyAllClients() {
const int64_t display_id = GetDisplayIdForNewWindows();
display::Screen::GetScreen()->SetDisplayForNewWindows(display_id);
// WindowService broadcasts the display id over mojo to all remote apps.
// TODO(jamescook): Move this into Shell when ShellState is removed.
WindowServiceOwner* ws_owner = Shell::Get()->window_service_owner();
// |ws_owner| is null during shutdown and tests. |window_service()| is null
// during early startup.
if (ws_owner && ws_owner->window_service())
ws_owner->window_service()->SetDisplayForNewWindows(display_id);
}
int64_t ShellState::GetDisplayIdForNewWindows() const {
// GetDisplayNearestWindow() handles null.
return display::Screen::GetScreen()
->GetDisplayNearestWindow(GetRootWindowForNewWindows())
.id();
}
void ShellState::SetScopedRootWindowForNewWindows(aura::Window* root) {
if (root == scoped_root_window_for_new_windows_)
return;
// Only allow set and clear, not switch.
DCHECK(!scoped_root_window_for_new_windows_ || !root);
scoped_root_window_for_new_windows_ = root;
NotifyAllClients();
}
} // namespace ash