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 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
|
// Copyright 2024 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/system/mahi/mahi_panel_widget.h"
#include <cstdint>
#include <memory>
#include <utility>
#include "ash/frame/non_client_frame_view_ash.h"
#include "ash/public/cpp/shell_window_ids.h"
#include "ash/shell.h"
#include "ash/strings/grit/ash_strings.h"
#include "ash/system/mahi/mahi_constants.h"
#include "ash/system/mahi/mahi_panel_view.h"
#include "ash/system/mahi/mahi_ui_controller.h"
#include "ash/system/mahi/refresh_banner_view.h"
#include "ash/wm/window_properties.h"
#include "ash/wm/work_area_insets.h"
#include "base/feature_list.h"
#include "chromeos/constants/chromeos_features.h"
#include "ui/aura/window.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/compositor/layer.h"
#include "ui/compositor/layer_type.h"
#include "ui/gfx/geometry/insets.h"
#include "ui/gfx/geometry/outsets.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/rounded_corners_f.h"
#include "ui/gfx/geometry/vector2d.h"
#include "ui/views/layout/box_layout_view.h"
#include "ui/views/view.h"
#include "ui/views/view_class_properties.h"
#include "ui/views/view_utils.h"
#include "ui/views/widget/unique_widget_ptr.h"
#include "ui/views/widget/widget.h"
#include "ui/views/widget/widget_delegate.h"
#include "ui/views/window/non_client_view.h"
namespace ash {
namespace {
constexpr int kRefreshBannerHeight = 32;
constexpr int kPanelBoundsPadding = 8;
constexpr char kWidgetName[] = "MahiPanel";
// TODO(b/319731776): Use panel bounds in size calculations instead of
// `kPanelDefaultWidth` and `kPanelDefaultHeight` when the panel is resizable.
bool IsSpaceAvailableOnRight(const gfx::Rect& screen_work_area,
const gfx::Rect& mahi_menu_bounds) {
return mahi_menu_bounds.x() + mahi_constants::kPanelDefaultWidth <
screen_work_area.right();
}
int CalculateAvailableSpaceOnBottom(const gfx::Rect& screen_work_area,
const gfx::Rect& mahi_menu_bounds) {
return screen_work_area.bottom() - mahi_menu_bounds.y() -
mahi_constants::kPanelDefaultHeight - kPanelBoundsPadding;
}
gfx::Rect CalculateAnimationStartBounds(const gfx::Rect& mahi_menu_bounds) {
const gfx::Rect screen_work_area = display::Screen::GetScreen()
->GetDisplayMatching(mahi_menu_bounds)
.work_area();
return gfx::Rect(
IsSpaceAvailableOnRight(screen_work_area, mahi_menu_bounds)
? mahi_menu_bounds.x()
: mahi_menu_bounds.right() - mahi_constants::kPanelDefaultWidth,
CalculateAvailableSpaceOnBottom(screen_work_area, mahi_menu_bounds)
? mahi_menu_bounds.y()
: mahi_menu_bounds.bottom() - mahi_menu_bounds.height(),
mahi_constants::kPanelDefaultWidth, mahi_menu_bounds.height());
}
gfx::Rect CalculateInitialWidgetBounds(const gfx::Rect& mahi_menu_bounds) {
const gfx::Rect screen_work_area = display::Screen::GetScreen()
->GetDisplayMatching(mahi_menu_bounds)
.work_area();
int available_space_on_bottom =
CalculateAvailableSpaceOnBottom(screen_work_area, mahi_menu_bounds);
// The `MahiPanelWidget` will be aligned with the top of the
// mahi context menu when possible. Otherwise we will try to keep the top of
// the `MahiPanelWidget` as close to the top of the mahi context menu as
// possible. This is to provide a seamless transition between the context menu
// and the panel.
return gfx::Rect(
IsSpaceAvailableOnRight(screen_work_area, mahi_menu_bounds)
? mahi_menu_bounds.x()
: mahi_menu_bounds.right() - mahi_constants::kPanelDefaultWidth,
available_space_on_bottom > 0
? mahi_menu_bounds.y()
: mahi_menu_bounds.y() + available_space_on_bottom,
mahi_constants::kPanelDefaultWidth, mahi_constants::kPanelDefaultHeight);
}
std::unique_ptr<views::BoxLayoutView> CreateMahiPanelContentsView() {
return views::Builder<views::BoxLayoutView>()
// We need to set a negative value for between child spacing here
// because we need the `RefreshBannerView` to overlap with the
// `MahiPanelView`.
.SetBetweenChildSpacing(-mahi_constants::kRefreshBannerStackDepth)
.SetOrientation(views::BoxLayout::Orientation::kVertical)
.Build();
}
// TODO(zoraiznaem): Investigate if MahiFrameView needs NonClientFrameViewAsh.
class MahiFrameView : public NonClientFrameViewAsh {
public:
explicit MahiFrameView(views::Widget* frame) : NonClientFrameViewAsh(frame) {
SetFrameEnabled(false);
SetShouldPaintHeader(false);
}
MahiFrameView(const MahiFrameView&) = delete;
MahiFrameView& operator=(const MahiFrameView&) = delete;
~MahiFrameView() override = default;
// views::NonClientFrameView:
gfx::Size GetMinimumSize() const override {
return gfx::Size(mahi_constants::kPanelDefaultWidth,
mahi_constants::kPanelDefaultHeight);
}
gfx::Size GetMaximumSize() const override {
return gfx::Size(mahi_constants::kPanelMaximumWidth,
mahi_constants::kPanelMaximumHeight);
}
};
} // namespace
MahiPanelWidget::MahiPanelWidget(InitParams params,
RefreshBannerView* refresh_view,
MahiUiController* ui_controller)
: views::Widget(std::move(params)), refresh_view_(refresh_view) {
CHECK(refresh_view_);
refresh_view_observation_.Observe(refresh_view_);
shelf_observation_.Observe(Shelf::ForWindow(Shell::GetPrimaryRootWindow()));
}
MahiPanelWidget::~MahiPanelWidget() = default;
// static
views::UniqueWidgetPtr MahiPanelWidget::CreateAndShowPanelWidget(
int64_t display_id,
const gfx::Rect& mahi_menu_bounds,
MahiUiController* ui_controller) {
auto* root_window = Shell::GetRootWindowForDisplayId(display_id);
views::Widget::InitParams params(
views::Widget::InitParams::NATIVE_WIDGET_OWNS_WIDGET,
base::FeatureList::IsEnabled(chromeos::features::kMahiPanelResizable)
? views::Widget::InitParams::TYPE_WINDOW
: views::Widget::InitParams::TYPE_WINDOW_FRAMELESS);
params.name = GetName();
std::unique_ptr<views::BoxLayoutView> contents_view =
CreateMahiPanelContentsView();
auto* refresh_view = contents_view->AddChildView(
std::make_unique<RefreshBannerView>(ui_controller));
auto* panel_view = contents_view->AddChildView(
std::make_unique<MahiPanelView>(ui_controller));
// Make sure the `MahiPanelView` is sized to fill up the available space.
contents_view->SetFlexForView(panel_view, 1.0);
// If resizing is enabled, create a custom delegate and set the contents view
// so that it can be set on the client view. Else the contents view will be
// set on the widget directly as there will be no client view.
if (base::FeatureList::IsEnabled(chromeos::features::kMahiPanelResizable)) {
auto delegate = std::make_unique<views::WidgetDelegate>();
// Set to true so that the delegate deletes itself.
delegate->SetOwnedByWidget(views::WidgetDelegate::OwnedByWidgetPassKey());
delegate->SetCanResize(true);
delegate->SetContentsView(std::move(contents_view));
delegate->SetNonClientFrameViewFactory(
base::BindRepeating([](views::Widget* widget)
-> std::unique_ptr<views::NonClientFrameView> {
return std::make_unique<MahiFrameView>(widget);
}));
params.delegate = delegate.release();
// If resizable, disable the resize shadow on the window border.
params.init_properties_container.SetProperty(kDisableResizeShadow, true);
params.init_properties_container.SetProperty(
kWindowResizeHistogramName,
new std::string(mahi_constants::kMahiPanelResizingHistogram));
params.init_properties_container.SetProperty(
kWindowResizeMaxLatencyHistogramName,
new std::string(mahi_constants::kMahiPanelResizingMaxLatencyHistogram));
}
// `SystemModalContainer` can travel across displays, is not automatically
// resizable on limited screen size and stays on top on full-screen.
params.parent =
Shell::GetContainer(root_window, kShellWindowId_SystemModalContainer);
// The widget's view handles round corners and blur via layers.
params.opacity = views::Widget::InitParams::WindowOpacity::kTranslucent;
params.layer_type = ui::LAYER_NOT_DRAWN;
views::UniqueWidgetPtr widget = std::make_unique<MahiPanelWidget>(
std::move(params), refresh_view, ui_controller);
// Set the contents view of the widget directly if non-resizable.
if (!base::FeatureList::IsEnabled(chromeos::features::kMahiPanelResizable)) {
widget->SetContentsView(std::move(contents_view));
}
widget->SetBounds(CalculateInitialWidgetBounds(mahi_menu_bounds));
widget->widget_delegate()->SetAccessibleTitle(
l10n_util::GetStringUTF16(IDS_ASH_MAHI_PANEL_TITLE));
widget->Show();
views::AsViewClass<MahiPanelView>(widget->GetContentsView()->GetViewByID(
mahi_constants::ViewId::kMahiPanelView))
->AnimatePopIn(CalculateAnimationStartBounds(mahi_menu_bounds));
return widget;
}
// static
const char* MahiPanelWidget::GetName() {
return kWidgetName;
}
void MahiPanelWidget::OnShelfWorkAreaInsetsChanged() {
gfx::Rect work_area_bounds =
WorkAreaInsets::ForWindow(GetNativeWindow())->user_work_area_bounds();
gfx::Rect panel_bounds = GetWindowBoundsInScreen();
// If the panel widget does not fit inside of the work area bounds (e.g. due
// to opening the virtual keyboard), its bounds will be updated so it's
// partially visible, prioritizing the bottom part of the panel.
if (panel_bounds.bottom() + kPanelBoundsPadding >=
work_area_bounds.bottom()) {
SetY(work_area_bounds.bottom() - mahi_constants::kPanelDefaultHeight -
kPanelBoundsPadding);
} else if (panel_bounds.y() - kPanelBoundsPadding <= work_area_bounds.y()) {
SetY(work_area_bounds.y() + kPanelBoundsPadding);
}
}
void MahiPanelWidget::OnViewVisibilityChanged(views::View* observed_view,
views::View* starting_view) {
CHECK_EQ(observed_view, refresh_view_);
if (is_refresh_view_visible_ == observed_view->GetVisible()) {
return;
}
is_refresh_view_visible_ = observed_view->GetVisible();
// Update widget bounds to provide space for the refresh banner if needed.
gfx::Rect widget_bounds = GetWindowBoundsInScreen();
if (is_refresh_view_visible_) {
widget_bounds.Outset(gfx::Outsets::TLBR(kRefreshBannerHeight, 0, 0, 0));
} else {
widget_bounds.Inset(gfx::Insets::TLBR(kRefreshBannerHeight, 0, 0, 0));
}
SetBounds(widget_bounds);
}
void MahiPanelWidget::OnViewIsDeleting(views::View* observed_view) {
CHECK_EQ(observed_view, refresh_view_);
refresh_view_observation_.Reset();
is_refresh_view_visible_ = false;
refresh_view_ = nullptr;
}
} // namespace ash
|