Avi Drissman | 3a215d1e | 2022-09-07 19:43:09 | [diff] [blame] | 1 | // Copyright 2018 The Chromium Authors |
Manu Cornet | a6cf9b4f | 2018-06-26 22:00:31 | [diff] [blame] | 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 "ash/shelf/window_preview.h" |
| 6 | |
Matthew Mourgos | 7532756 | 2019-09-09 21:23:57 | [diff] [blame] | 7 | #include "ash/public/cpp/shelf_config.h" |
Manu Cornet | a6cf9b4f | 2018-06-26 22:00:31 | [diff] [blame] | 8 | #include "ash/resources/vector_icons/vector_icons.h" |
Sammie Quon | b2223fc | 2018-12-06 01:34:52 | [diff] [blame] | 9 | #include "ash/wm/window_preview_view.h" |
Manu Cornet | a6cf9b4f | 2018-06-26 22:00:31 | [diff] [blame] | 10 | #include "ash/wm/window_util.h" |
Avi Drissman | 4de6dab | 2023-01-06 23:17:35 | [diff] [blame] | 11 | #include "base/functional/bind.h" |
Manu Cornet | a6cf9b4f | 2018-06-26 22:00:31 | [diff] [blame] | 12 | #include "ui/aura/window.h" |
Peter Kasting | c63c993 | 2021-09-27 15:36:00 | [diff] [blame] | 13 | #include "ui/color/color_id.h" |
| 14 | #include "ui/color/color_provider.h" |
Manu Cornet | 89952093 | 2019-04-03 04:59:27 | [diff] [blame] | 15 | #include "ui/gfx/color_palette.h" |
Manu Cornet | a6cf9b4f | 2018-06-26 22:00:31 | [diff] [blame] | 16 | #include "ui/gfx/paint_vector_icon.h" |
Manu Cornet | 89952093 | 2019-04-03 04:59:27 | [diff] [blame] | 17 | #include "ui/views/background.h" |
Manu Cornet | a6cf9b4f | 2018-06-26 22:00:31 | [diff] [blame] | 18 | #include "ui/views/controls/button/image_button.h" |
| 19 | #include "ui/views/controls/label.h" |
| 20 | |
| 21 | namespace ash { |
| 22 | |
Manu Cornet | a6cf9b4f | 2018-06-26 22:00:31 | [diff] [blame] | 23 | // The margins around window titles. |
| 24 | constexpr int kTitleLineHeight = 20; |
| 25 | constexpr int kTitleMarginTop = 10; |
| 26 | constexpr int kTitleMarginBottom = 10; |
| 27 | constexpr int kTitleMarginRight = 16; |
| 28 | |
| 29 | // The width and height of close buttons. |
| 30 | constexpr int kCloseButtonSize = 36; |
| 31 | constexpr int kCloseButtonImageSize = 24; |
| 32 | constexpr int kCloseButtonSideBleed = 8; |
| 33 | constexpr SkColor kCloseButtonColor = SK_ColorWHITE; |
| 34 | |
Manu Cornet | 89952093 | 2019-04-03 04:59:27 | [diff] [blame] | 35 | constexpr SkColor kPreviewContainerBgColor = |
| 36 | SkColorSetA(gfx::kGoogleGrey100, 0x24); |
| 37 | constexpr int kPreviewBorderRadius = 4; |
| 38 | |
Peter Kasting | c63c993 | 2021-09-27 15:36:00 | [diff] [blame] | 39 | WindowPreview::WindowPreview(aura::Window* window, Delegate* delegate) |
Manu Cornet | a6cf9b4f | 2018-06-26 22:00:31 | [diff] [blame] | 40 | : delegate_(delegate) { |
Sammie Quon | b2223fc | 2018-12-06 01:34:52 | [diff] [blame] | 41 | preview_view_ = |
James Cook | 00e65e9 | 2019-07-25 03:19:08 | [diff] [blame] | 42 | new WindowPreviewView(window, /*trilinear_filtering_on_init=*/false); |
Manu Cornet | 89952093 | 2019-04-03 04:59:27 | [diff] [blame] | 43 | preview_container_view_ = new views::View(); |
| 44 | preview_container_view_->SetBackground(views::CreateRoundedRectBackground( |
| 45 | kPreviewContainerBgColor, kPreviewBorderRadius)); |
Manu Cornet | a6cf9b4f | 2018-06-26 22:00:31 | [diff] [blame] | 46 | title_ = new views::Label(window->GetTitle()); |
Peter Kasting | 6771812 | 2020-11-03 07:54:04 | [diff] [blame] | 47 | close_button_ = new views::ImageButton(base::BindRepeating( |
| 48 | &WindowPreview::CloseButtonPressed, base::Unretained(this))); |
Sophie Wen | a4c4b8f | 2022-06-21 18:19:38 | [diff] [blame] | 49 | close_button_->SetFocusBehavior(FocusBehavior::NEVER); |
Manu Cornet | a6cf9b4f | 2018-06-26 22:00:31 | [diff] [blame] | 50 | |
Arthur Sonzogni | 834e018f | 2023-04-22 10:20:02 | [diff] [blame^] | 51 | AddChildView(preview_container_view_.get()); |
| 52 | AddChildView(preview_view_.get()); |
| 53 | AddChildView(title_.get()); |
| 54 | AddChildView(close_button_.get()); |
Manu Cornet | a6cf9b4f | 2018-06-26 22:00:31 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | WindowPreview::~WindowPreview() = default; |
| 58 | |
Manu Cornet | a6cf9b4f | 2018-06-26 22:00:31 | [diff] [blame] | 59 | gfx::Size WindowPreview::CalculatePreferredSize() const { |
Manu Cornet | 89952093 | 2019-04-03 04:59:27 | [diff] [blame] | 60 | // The preview itself will always be strictly contained within its container, |
| 61 | // so only the container's size matters to calculate the preferred size. |
| 62 | const gfx::Size container_size = GetPreviewContainerSize(); |
| 63 | const int title_height_with_padding = |
Manu Cornet | a6cf9b4f | 2018-06-26 22:00:31 | [diff] [blame] | 64 | kTitleLineHeight + kTitleMarginTop + kTitleMarginBottom; |
Manu Cornet | 89952093 | 2019-04-03 04:59:27 | [diff] [blame] | 65 | return gfx::Size(container_size.width(), |
| 66 | container_size.height() + title_height_with_padding); |
Manu Cornet | a6cf9b4f | 2018-06-26 22:00:31 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | void WindowPreview::Layout() { |
| 70 | gfx::Rect content_rect = GetContentsBounds(); |
Manu Cornet | a6cf9b4f | 2018-06-26 22:00:31 | [diff] [blame] | 71 | |
| 72 | gfx::Size title_size = title_->CalculatePreferredSize(); |
| 73 | int title_height_with_padding = |
| 74 | kTitleLineHeight + kTitleMarginTop + kTitleMarginBottom; |
Manu Cornet | 89952093 | 2019-04-03 04:59:27 | [diff] [blame] | 75 | int title_width = |
| 76 | std::min(title_size.width(), |
| 77 | content_rect.width() - kCloseButtonSize - kTitleMarginRight); |
Manu Cornet | a6cf9b4f | 2018-06-26 22:00:31 | [diff] [blame] | 78 | title_->SetBoundsRect(gfx::Rect(content_rect.x(), |
| 79 | content_rect.y() + kTitleMarginTop, |
| 80 | title_width, kTitleLineHeight)); |
Manu Cornet | 89952093 | 2019-04-03 04:59:27 | [diff] [blame] | 81 | |
Manu Cornet | a6cf9b4f | 2018-06-26 22:00:31 | [diff] [blame] | 82 | close_button_->SetBoundsRect( |
| 83 | gfx::Rect(content_rect.right() - kCloseButtonSize + kCloseButtonSideBleed, |
| 84 | content_rect.y(), kCloseButtonSize, kCloseButtonSize)); |
Manu Cornet | 89952093 | 2019-04-03 04:59:27 | [diff] [blame] | 85 | |
| 86 | const gfx::Size container_size = GetPreviewContainerSize(); |
| 87 | gfx::Size mirror_size = preview_view_->CalculatePreferredSize(); |
| 88 | float preview_ratio = static_cast<float>(mirror_size.width()) / |
| 89 | static_cast<float>(mirror_size.height()); |
| 90 | |
Matthew Mourgos | 7532756 | 2019-09-09 21:23:57 | [diff] [blame] | 91 | int preview_height = ShelfConfig::Get()->shelf_tooltip_preview_height(); |
Manu Cornet | 89952093 | 2019-04-03 04:59:27 | [diff] [blame] | 92 | int preview_width = preview_height * preview_ratio; |
Matthew Mourgos | 7532756 | 2019-09-09 21:23:57 | [diff] [blame] | 93 | if (preview_ratio > ShelfConfig::Get()->shelf_tooltip_preview_max_ratio()) { |
Manu Cornet | 89952093 | 2019-04-03 04:59:27 | [diff] [blame] | 94 | // Very wide window. |
Matthew Mourgos | 7532756 | 2019-09-09 21:23:57 | [diff] [blame] | 95 | preview_width = ShelfConfig::Get()->shelf_tooltip_preview_max_width(); |
| 96 | preview_height = |
| 97 | ShelfConfig::Get()->shelf_tooltip_preview_max_width() / preview_ratio; |
Manu Cornet | 89952093 | 2019-04-03 04:59:27 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | // Center the actual preview over the container, horizontally and vertically. |
| 101 | gfx::Point preview_offset_from_container( |
| 102 | (container_size.width() - preview_width) / 2, |
| 103 | (container_size.height() - preview_height) / 2); |
| 104 | |
| 105 | const int preview_container_top = |
| 106 | content_rect.y() + title_height_with_padding; |
| 107 | preview_container_view_->SetBoundsRect( |
| 108 | gfx::Rect(content_rect.x(), preview_container_top, container_size.width(), |
| 109 | container_size.height())); |
Sammie Quon | b2223fc | 2018-12-06 01:34:52 | [diff] [blame] | 110 | preview_view_->SetBoundsRect( |
Manu Cornet | 89952093 | 2019-04-03 04:59:27 | [diff] [blame] | 111 | gfx::Rect(content_rect.x() + preview_offset_from_container.x(), |
| 112 | preview_container_top + preview_offset_from_container.y(), |
| 113 | preview_width, preview_height)); |
Manu Cornet | a6cf9b4f | 2018-06-26 22:00:31 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | bool WindowPreview::OnMousePressed(const ui::MouseEvent& event) { |
Sammie Quon | b2223fc | 2018-12-06 01:34:52 | [diff] [blame] | 117 | if (!preview_view_->bounds().Contains(event.location())) |
Manu Cornet | a6cf9b4f | 2018-06-26 22:00:31 | [diff] [blame] | 118 | return false; |
| 119 | |
Sammie Quon | b2223fc | 2018-12-06 01:34:52 | [diff] [blame] | 120 | aura::Window* target = preview_view_->window(); |
| 121 | if (target) { |
Manu Cornet | a6cf9b4f | 2018-06-26 22:00:31 | [diff] [blame] | 122 | // The window might have been closed in the mean time. |
| 123 | // TODO: Use WindowObserver to listen to when previewed windows are |
| 124 | // being closed and remove this condition. |
Sammie Quon | b2223fc | 2018-12-06 01:34:52 | [diff] [blame] | 125 | wm::ActivateWindow(target); |
Manu Cornet | a6cf9b4f | 2018-06-26 22:00:31 | [diff] [blame] | 126 | |
| 127 | // This will have the effect of deleting this view. |
| 128 | delegate_->OnPreviewActivated(this); |
| 129 | } |
| 130 | return true; |
| 131 | } |
| 132 | |
Andrew Lee | 4c6dc194 | 2019-05-30 00:18:35 | [diff] [blame] | 133 | const char* WindowPreview::GetClassName() const { |
| 134 | return "WindowPreview"; |
| 135 | } |
| 136 | |
Peter Kasting | c63c993 | 2021-09-27 15:36:00 | [diff] [blame] | 137 | void WindowPreview::OnThemeChanged() { |
Sophie Wen | a4c4b8f | 2022-06-21 18:19:38 | [diff] [blame] | 138 | views::View::OnThemeChanged(); |
Peter Kasting | c63c993 | 2021-09-27 15:36:00 | [diff] [blame] | 139 | const auto* color_provider = GetColorProvider(); |
Manu Cornet | 89952093 | 2019-04-03 04:59:27 | [diff] [blame] | 140 | SkColor background_color = |
Peter Kasting | c63c993 | 2021-09-27 15:36:00 | [diff] [blame] | 141 | color_provider->GetColor(ui::kColorTooltipBackground); |
Manu Cornet | 89952093 | 2019-04-03 04:59:27 | [diff] [blame] | 142 | title_->SetEnabledColor( |
Peter Kasting | c63c993 | 2021-09-27 15:36:00 | [diff] [blame] | 143 | color_provider->GetColor(ui::kColorTooltipForeground)); |
Manu Cornet | 89952093 | 2019-04-03 04:59:27 | [diff] [blame] | 144 | title_->SetBackgroundColor(background_color); |
| 145 | |
| 146 | // The background is not opaque, so we can't do subpixel rendering. |
| 147 | title_->SetSubpixelRenderingEnabled(false); |
| 148 | |
| 149 | close_button_->SetImage( |
| 150 | views::Button::STATE_NORMAL, |
| 151 | gfx::CreateVectorIcon(kOverviewWindowCloseIcon, kCloseButtonColor)); |
Peter Kasting | 4978eacb | 2019-05-15 18:36:10 | [diff] [blame] | 152 | close_button_->SetImageHorizontalAlignment(views::ImageButton::ALIGN_CENTER); |
| 153 | close_button_->SetImageVerticalAlignment(views::ImageButton::ALIGN_MIDDLE); |
Manu Cornet | 89952093 | 2019-04-03 04:59:27 | [diff] [blame] | 154 | close_button_->SetMinimumImageSize( |
| 155 | gfx::Size(kCloseButtonImageSize, kCloseButtonImageSize)); |
| 156 | } |
| 157 | |
| 158 | gfx::Size WindowPreview::GetPreviewContainerSize() const { |
| 159 | return gfx::Size( |
Matthew Mourgos | 7532756 | 2019-09-09 21:23:57 | [diff] [blame] | 160 | std::min(delegate_->GetMaxPreviewRatio() * |
| 161 | ShelfConfig::Get()->shelf_tooltip_preview_height(), |
| 162 | static_cast<float>( |
| 163 | ShelfConfig::Get()->shelf_tooltip_preview_max_width())), |
| 164 | ShelfConfig::Get()->shelf_tooltip_preview_height()); |
Manu Cornet | 89952093 | 2019-04-03 04:59:27 | [diff] [blame] | 165 | } |
| 166 | |
Peter Kasting | 6771812 | 2020-11-03 07:54:04 | [diff] [blame] | 167 | void WindowPreview::CloseButtonPressed() { |
| 168 | // The window might have been closed in the mean time. |
| 169 | // TODO: Use WindowObserver to listen to when previewed windows are |
| 170 | // being closed and remove this condition. |
| 171 | aura::Window* target = preview_view_->window(); |
| 172 | if (!target) |
| 173 | return; |
| 174 | window_util::CloseWidgetForWindow(target); |
| 175 | |
| 176 | // This will have the effect of deleting this view. |
| 177 | delegate_->OnPreviewDismissed(this); |
| 178 | } |
| 179 | |
Manu Cornet | a6cf9b4f | 2018-06-26 22:00:31 | [diff] [blame] | 180 | } // namespace ash |