File: window_preview.cc

package info (click to toggle)
chromium 120.0.6099.224-1~deb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,112,112 kB
  • sloc: cpp: 32,907,025; ansic: 8,148,123; javascript: 3,679,536; python: 2,031,248; asm: 959,718; java: 804,675; xml: 617,256; sh: 111,417; objc: 100,835; perl: 88,443; cs: 53,032; makefile: 29,579; fortran: 24,137; php: 21,162; tcl: 21,147; sql: 20,809; ruby: 17,735; pascal: 12,864; yacc: 8,045; lisp: 3,388; lex: 1,323; ada: 727; awk: 329; jsp: 267; csh: 117; exp: 43; sed: 37
file content (179 lines) | stat: -rw-r--r-- 6,931 bytes parent folder | download
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
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ash/shelf/window_preview.h"

#include "ash/public/cpp/shelf_config.h"
#include "ash/resources/vector_icons/vector_icons.h"
#include "ash/wm/window_preview_view.h"
#include "ash/wm/window_util.h"
#include "base/functional/bind.h"
#include "ui/aura/window.h"
#include "ui/color/color_id.h"
#include "ui/color/color_provider.h"
#include "ui/gfx/color_palette.h"
#include "ui/gfx/paint_vector_icon.h"
#include "ui/views/background.h"
#include "ui/views/controls/button/image_button.h"
#include "ui/views/controls/label.h"

namespace ash {

// The margins around window titles.
constexpr int kTitleLineHeight = 20;
constexpr int kTitleMarginTop = 10;
constexpr int kTitleMarginBottom = 10;
constexpr int kTitleMarginRight = 16;

// The width and height of close buttons.
constexpr int kCloseButtonSize = 36;
constexpr int kCloseButtonImageSize = 24;
constexpr int kCloseButtonSideBleed = 8;
constexpr SkColor kCloseButtonColor = SK_ColorWHITE;

constexpr SkColor kPreviewContainerBgColor =
    SkColorSetA(gfx::kGoogleGrey100, 0x24);
constexpr int kPreviewBorderRadius = 4;

WindowPreview::WindowPreview(aura::Window* window, Delegate* delegate)
    : delegate_(delegate) {
  preview_view_ = new WindowPreviewView(window);
  preview_container_view_ = new views::View();
  preview_container_view_->SetBackground(views::CreateRoundedRectBackground(
      kPreviewContainerBgColor, kPreviewBorderRadius));
  title_ = new views::Label(window->GetTitle());
  close_button_ = new views::ImageButton(base::BindRepeating(
      &WindowPreview::CloseButtonPressed, base::Unretained(this)));
  close_button_->SetFocusBehavior(FocusBehavior::NEVER);

  AddChildView(preview_container_view_.get());
  AddChildView(preview_view_.get());
  AddChildView(title_.get());
  AddChildView(close_button_.get());
}

WindowPreview::~WindowPreview() = default;

gfx::Size WindowPreview::CalculatePreferredSize() const {
  // The preview itself will always be strictly contained within its container,
  // so only the container's size matters to calculate the preferred size.
  const gfx::Size container_size = GetPreviewContainerSize();
  const int title_height_with_padding =
      kTitleLineHeight + kTitleMarginTop + kTitleMarginBottom;
  return gfx::Size(container_size.width(),
                   container_size.height() + title_height_with_padding);
}

void WindowPreview::Layout() {
  gfx::Rect content_rect = GetContentsBounds();

  gfx::Size title_size = title_->CalculatePreferredSize();
  int title_height_with_padding =
      kTitleLineHeight + kTitleMarginTop + kTitleMarginBottom;
  int title_width =
      std::min(title_size.width(),
               content_rect.width() - kCloseButtonSize - kTitleMarginRight);
  title_->SetBoundsRect(gfx::Rect(content_rect.x(),
                                  content_rect.y() + kTitleMarginTop,
                                  title_width, kTitleLineHeight));

  close_button_->SetBoundsRect(
      gfx::Rect(content_rect.right() - kCloseButtonSize + kCloseButtonSideBleed,
                content_rect.y(), kCloseButtonSize, kCloseButtonSize));

  const gfx::Size container_size = GetPreviewContainerSize();
  gfx::Size mirror_size = preview_view_->CalculatePreferredSize();
  float preview_ratio = static_cast<float>(mirror_size.width()) /
                        static_cast<float>(mirror_size.height());

  int preview_height = ShelfConfig::Get()->shelf_tooltip_preview_height();
  int preview_width = preview_height * preview_ratio;
  if (preview_ratio > ShelfConfig::Get()->shelf_tooltip_preview_max_ratio()) {
    // Very wide window.
    preview_width = ShelfConfig::Get()->shelf_tooltip_preview_max_width();
    preview_height =
        ShelfConfig::Get()->shelf_tooltip_preview_max_width() / preview_ratio;
  }

  // Center the actual preview over the container, horizontally and vertically.
  gfx::Point preview_offset_from_container(
      (container_size.width() - preview_width) / 2,
      (container_size.height() - preview_height) / 2);

  const int preview_container_top =
      content_rect.y() + title_height_with_padding;
  preview_container_view_->SetBoundsRect(
      gfx::Rect(content_rect.x(), preview_container_top, container_size.width(),
                container_size.height()));
  preview_view_->SetBoundsRect(
      gfx::Rect(content_rect.x() + preview_offset_from_container.x(),
                preview_container_top + preview_offset_from_container.y(),
                preview_width, preview_height));
}

bool WindowPreview::OnMousePressed(const ui::MouseEvent& event) {
  if (!preview_view_->bounds().Contains(event.location()))
    return false;

  aura::Window* target = preview_view_->window();
  if (target) {
    // The window might have been closed in the mean time.
    // TODO: Use WindowObserver to listen to when previewed windows are
    // being closed and remove this condition.
    wm::ActivateWindow(target);

    // This will have the effect of deleting this view.
    delegate_->OnPreviewActivated(this);
  }
  return true;
}

const char* WindowPreview::GetClassName() const {
  return "WindowPreview";
}

void WindowPreview::OnThemeChanged() {
  views::View::OnThemeChanged();
  const auto* color_provider = GetColorProvider();
  SkColor background_color =
      color_provider->GetColor(ui::kColorTooltipBackground);
  title_->SetEnabledColor(
      color_provider->GetColor(ui::kColorTooltipForeground));
  title_->SetBackgroundColor(background_color);

  // The background is not opaque, so we can't do subpixel rendering.
  title_->SetSubpixelRenderingEnabled(false);

  close_button_->SetImage(
      views::Button::STATE_NORMAL,
      gfx::CreateVectorIcon(kOverviewWindowCloseIcon, kCloseButtonColor));
  close_button_->SetImageHorizontalAlignment(views::ImageButton::ALIGN_CENTER);
  close_button_->SetImageVerticalAlignment(views::ImageButton::ALIGN_MIDDLE);
  close_button_->SetMinimumImageSize(
      gfx::Size(kCloseButtonImageSize, kCloseButtonImageSize));
}

gfx::Size WindowPreview::GetPreviewContainerSize() const {
  return gfx::Size(
      std::min(delegate_->GetMaxPreviewRatio() *
                   ShelfConfig::Get()->shelf_tooltip_preview_height(),
               static_cast<float>(
                   ShelfConfig::Get()->shelf_tooltip_preview_max_width())),
      ShelfConfig::Get()->shelf_tooltip_preview_height());
}

void WindowPreview::CloseButtonPressed() {
  // The window might have been closed in the mean time.
  // TODO: Use WindowObserver to listen to when previewed windows are
  // being closed and remove this condition.
  aura::Window* target = preview_view_->window();
  if (!target)
    return;
  window_util::CloseWidgetForWindow(target);

  // This will have the effect of deleting this view.
  delegate_->OnPreviewDismissed(this);
}

}  // namespace ash