1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
|
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/ozone/platform/headless/headless_window.h"
#include <string>
#include "base/notreached.h"
#include "build/build_config.h"
#include "ui/base/cursor/platform_cursor.h"
#include "ui/display/screen.h"
#include "ui/display/types/display_constants.h"
#include "ui/events/platform/platform_event_source.h"
#include "ui/ozone/platform/headless/headless_window_manager.h"
namespace ui {
HeadlessWindow::HeadlessWindow(PlatformWindowDelegate* delegate,
HeadlessWindowManager* manager,
const gfx::Rect& bounds)
: delegate_(delegate), manager_(manager), bounds_(bounds) {
widget_ = manager_->AddWindow(this);
delegate->OnAcceleratedWidgetAvailable(widget_);
}
HeadlessWindow::~HeadlessWindow() {
manager_->RemoveWindow(widget_, this);
}
void HeadlessWindow::Show(bool inactive) {
visible_ = true;
}
void HeadlessWindow::Hide() {
visible_ = false;
}
void HeadlessWindow::Close() {
delegate_->OnClosed();
}
bool HeadlessWindow::IsVisible() const {
return visible_;
}
void HeadlessWindow::PrepareForShutdown() {}
void HeadlessWindow::SetBoundsInPixels(const gfx::Rect& bounds) {
UpdateBounds(bounds);
}
gfx::Rect HeadlessWindow::GetBoundsInPixels() const {
return bounds_;
}
void HeadlessWindow::SetBoundsInDIP(const gfx::Rect& bounds) {
SetBoundsInPixels(delegate_->ConvertRectToPixels(bounds));
}
gfx::Rect HeadlessWindow::GetBoundsInDIP() const {
return delegate_->ConvertRectToDIP(bounds_);
}
void HeadlessWindow::SetTitle(const std::u16string& title) {}
void HeadlessWindow::SetCapture() {}
void HeadlessWindow::ReleaseCapture() {}
bool HeadlessWindow::HasCapture() const {
return false;
}
void HeadlessWindow::SetFullscreen(bool fullscreen, int64_t target_display_id) {
DCHECK_EQ(target_display_id, display::kInvalidDisplayId);
if (!delegate_->CanFullscreen()) {
return;
}
if (fullscreen) {
if (window_state_ != PlatformWindowState::kMaximized &&
window_state_ != PlatformWindowState::kFullScreen) {
restored_bounds_ = bounds_;
}
ZoomWindowBounds();
UpdateWindowState(PlatformWindowState::kFullScreen);
} else {
if (window_state_ != PlatformWindowState::kFullScreen) {
return;
}
RestoreWindowBounds();
UpdateWindowState(PlatformWindowState::kNormal);
}
}
void HeadlessWindow::Maximize() {
if (!delegate_->CanMaximize()) {
return;
}
if (window_state_ != PlatformWindowState::kMaximized &&
window_state_ != PlatformWindowState::kFullScreen) {
restored_bounds_ = bounds_;
ZoomWindowBounds();
UpdateWindowState(PlatformWindowState::kMaximized);
}
}
void HeadlessWindow::Minimize() {
if (window_state_ != PlatformWindowState::kMinimized) {
// Minimized window retains its size and position, however, it's made
// hidden by Aura.
if (window_state_ == PlatformWindowState::kMaximized ||
window_state_ == PlatformWindowState::kFullScreen) {
RestoreWindowBounds();
}
UpdateWindowState(PlatformWindowState::kMinimized);
// Minimized windows are inactive. Aura activates minimized windows
// when restoring. If we don't deactivate the window here, the subsequent
// activation will be optimized away, causing https://crbug.com/358998544.
Deactivate();
}
}
void HeadlessWindow::Restore() {
if (window_state_ != PlatformWindowState::kNormal) {
RestoreWindowBounds();
UpdateWindowState(PlatformWindowState::kNormal);
}
}
PlatformWindowState HeadlessWindow::GetPlatformWindowState() const {
return window_state_;
}
void HeadlessWindow::Activate() {
if (activation_state_ != ActivationState::kActive) {
activation_state_ = ActivationState::kActive;
delegate_->OnActivationChanged(/*active=*/true);
}
}
void HeadlessWindow::Deactivate() {
if (activation_state_ != ActivationState::kInactive) {
activation_state_ = ActivationState::kInactive;
delegate_->OnActivationChanged(/*active=*/false);
}
}
void HeadlessWindow::SetUseNativeFrame(bool use_native_frame) {}
bool HeadlessWindow::ShouldUseNativeFrame() const {
NOTIMPLEMENTED_LOG_ONCE();
return false;
}
void HeadlessWindow::SetCursor(scoped_refptr<PlatformCursor> cursor) {}
void HeadlessWindow::MoveCursorTo(const gfx::Point& location) {}
void HeadlessWindow::ConfineCursorToBounds(const gfx::Rect& bounds) {}
void HeadlessWindow::SetRestoredBoundsInDIP(const gfx::Rect& bounds) {
restored_bounds_ = delegate_->ConvertRectToPixels(bounds);
}
gfx::Rect HeadlessWindow::GetRestoredBoundsInDIP() const {
return delegate_->ConvertRectToDIP(restored_bounds_.value_or(bounds_));
}
void HeadlessWindow::SetWindowIcons(const gfx::ImageSkia& window_icon,
const gfx::ImageSkia& app_icon) {}
void HeadlessWindow::SizeConstraintsChanged() {}
void HeadlessWindow::ZoomWindowBounds() {
gfx::Rect zoomed_bounds =
display::Screen::GetScreen()->GetDisplayMatching(bounds_).work_area();
UpdateBounds(zoomed_bounds);
}
void HeadlessWindow::RestoreWindowBounds() {
if (restored_bounds_) {
gfx::Rect restored_bounds = *restored_bounds_;
restored_bounds_.reset();
UpdateBounds(restored_bounds);
}
}
void HeadlessWindow::UpdateBounds(const gfx::Rect& bounds) {
// Even if the pixel bounds didn't change this call to the delegate should
// still happen. The device scale factor may have changed which effectively
// changes the bounds.
bool origin_changed = bounds_.origin() != bounds.origin();
bounds_ = bounds;
delegate_->OnBoundsChanged({origin_changed});
}
void HeadlessWindow::UpdateWindowState(PlatformWindowState new_window_state) {
DCHECK_NE(window_state_, new_window_state);
auto old_window_state = window_state_;
window_state_ = new_window_state;
delegate_->OnWindowStateChanged(old_window_state, new_window_state);
}
} // namespace ui
|