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
|
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/glic/widget/glic_widget.h"
#include "base/memory/ptr_util.h"
#include "chrome/browser/glic/resources/grit/glic_browser_resources.h"
#include "chrome/browser/glic/widget/glic_view.h"
#include "chrome/browser/themes/theme_service.h"
#include "chrome/browser/themes/theme_service_factory.h"
#include "chrome/browser/ui/views/chrome_widget_sublevel.h"
#include "chrome/common/chrome_features.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/color/color_provider_key.h"
#include "ui/display/screen.h"
#include "ui/gfx/geometry/insets.h"
#include "ui/gfx/geometry/outsets.h"
#include "ui/gfx/geometry/rounded_corners_f.h"
#include "ui/views/widget/native_widget.h"
#include "ui/views/widget/widget_delegate.h"
#if BUILDFLAG(IS_OZONE)
#include "ui/ozone/public/ozone_platform.h"
#endif
#if BUILDFLAG(IS_WIN)
#include "chrome/installer/util/install_util.h"
#include "chrome/installer/util/shell_util.h"
#include "ui/aura/window.h"
#include "ui/aura/window_tree_host.h"
#include "ui/base/win/hwnd_metrics.h"
#include "ui/base/win/shell.h"
#include "ui/views/win/hwnd_util.h"
#endif
namespace glic {
namespace {
constexpr float kGlicWidgetCornerRadius = 12;
// For resizeable windows, there may be an invisible border which affects the
// widget size. Given a target rect, this method provides the outsets which
// should be applied in order to calculate the correct widget bounds.
gfx::Outsets GetTargetOutsets(const gfx::Rect& bounds) {
gfx::Outsets outsets;
#if BUILDFLAG(IS_WIN)
RECT bounds_rect = bounds.ToRECT();
int frame_thickness = ui::GetResizableFrameThicknessFromMonitorInDIP(
MonitorFromRect(&bounds_rect, MONITOR_DEFAULTTONEAREST),
/*has_caption=*/false);
// On Windows, the presence of a frame means that we need to adjust the left,
// right and bottom by frame thickness.
outsets.set_left_right(frame_thickness, frame_thickness);
outsets.set_bottom(frame_thickness);
#endif
return outsets;
}
} // namespace
class GlicWidgetDelegate : public views::WidgetDelegate {
public:
GlicWidgetDelegate() {
SetFocusTraversesOut(true);
RegisterDeleteDelegateCallback(
RegisterDeleteCallbackPassKey(),
base::BindOnce(&GlicWidgetDelegate::Destroy, base::Unretained(this)));
}
GlicWidgetDelegate(const GlicWidgetDelegate&) = delete;
GlicWidgetDelegate& operator=(const GlicWidgetDelegate&) = delete;
~GlicWidgetDelegate() override = default;
private:
void Destroy() { delete this; }
};
void* kGlicWidgetIdentifier = &kGlicWidgetIdentifier;
GlicWidget::GlicWidget(ThemeService* theme_service, InitParams params)
: views::Widget(std::move(params)) {
minimum_widget_size_ = GetInitialSize();
OnSizeConstraintsChanged();
theme_service_observation_.Observe(theme_service);
}
GlicWidget::~GlicWidget() = default;
// static
gfx::Size GlicWidget::GetInitialSize() {
return {features::kGlicInitialWidth.Get(),
features::kGlicInitialHeight.Get()};
}
std::unique_ptr<GlicWidget> GlicWidget::Create(
Profile* profile,
const gfx::Rect& initial_bounds,
base::WeakPtr<ui::AcceleratorTarget> accelerator_delegate,
bool user_resizable) {
views::Widget::InitParams params(
views::Widget::InitParams::CLIENT_OWNS_WIDGET,
views::Widget::InitParams::TYPE_WINDOW_FRAMELESS);
params.bounds = initial_bounds;
#if BUILDFLAG(IS_OZONE)
// Some platforms don't allow accelerated widgets to be positioned from
// client-side. Don't set an origin in that case.
if (!ui::OzonePlatform::GetInstance()
->GetPlatformProperties()
.supports_global_screen_coordinates) {
params.bounds.set_origin({});
}
#endif
if (user_resizable) {
params.bounds.Outset(GetTargetOutsets(initial_bounds));
}
#if BUILDFLAG(IS_WIN)
// If floaty won't be always on top, it should appear in the taskbar and
// alt tab list.
if (!base::FeatureList::IsEnabled(features::kGlicZOrderChanges)) {
params.dont_show_in_taskbar = true;
}
params.force_system_menu_for_frameless = true;
#endif
params.sublevel = ChromeWidgetSublevel::kSublevelGlic;
// Don't change this name. This is used by other code to identify the glic
// window. See b/404947780.
params.name = "GlicWidget";
// Support of rounded corners varies across platforms. See
// Widget::InitParams::rounded_corners. DO NOT apply this radius using
// views::Background or in the web client because it will mismatch with
// the window's actual corner radius. e.g. on win10 resizable windows
// do have rounded corners.
params.rounded_corners = gfx::RoundedCornersF(kGlicWidgetCornerRadius);
#if BUILDFLAG(IS_MAC)
params.animation_enabled = true;
#endif
auto delegate = std::make_unique<GlicWidgetDelegate>();
delegate->SetCanResize(user_resizable);
params.delegate = delegate.release();
auto widget = base::WrapUnique(new GlicWidget(
ThemeServiceFactory::GetForProfile(profile), std::move(params)));
widget->SetMinimumSize(GetInitialSize());
widget->SetContentsView(std::make_unique<GlicView>(
profile, initial_bounds.size(), accelerator_delegate));
// Mac fullscreen uses this identifier to find this widget and reparent it to
// the overlay widget.
widget->SetNativeWindowProperty(views::kWidgetIdentifierKey,
kGlicWidgetIdentifier);
#if BUILDFLAG(IS_WIN)
HWND hwnd = widget->GetNativeWindow()->GetHost()->GetAcceleratedWidget();
if (hwnd != nullptr) {
ui::win::PreventWindowFromPinning(hwnd);
if (base::FeatureList::IsEnabled(features::kGlicZOrderChanges)) {
ui::win::SetAppIdForWindow(
ShellUtil::GetBrowserModelId(InstallUtil::IsPerUserInstall()), hwnd);
}
}
#endif // BUILDFLAG(IS_WIN)
return widget;
}
display::Display GlicWidget::GetDisplay() {
std::optional<display::Display> display = GetNearestDisplay();
if (display) [[likely]] {
return *display;
}
// This should not happen after Widget::Init().
return display::Screen::GetScreen()->GetPrimaryDisplay();
}
void GlicWidget::SetMinimumSize(const gfx::Size& size) {
minimum_widget_size_ = size;
minimum_widget_size_.SetToMax(GetInitialSize());
OnSizeConstraintsChanged();
}
gfx::Size GlicWidget::GetMinimumSize() const {
return minimum_widget_size_;
}
gfx::Rect GlicWidget::VisibleToWidgetBounds(gfx::Rect visible_bounds) {
if (widget_delegate()->CanResize()) {
visible_bounds.Outset(GetTargetOutsets(visible_bounds));
}
return visible_bounds;
}
gfx::Rect GlicWidget::WidgetToVisibleBounds(gfx::Rect widget_bounds) {
if (widget_delegate()->CanResize()) {
widget_bounds.Inset(-GetTargetOutsets(widget_bounds).ToInsets());
}
return widget_bounds;
}
ui::ColorProviderKey GlicWidget::GetColorProviderKey() const {
ui::ColorProviderKey key = Widget::GetColorProviderKey();
ThemeService::BrowserColorScheme theme_color_scheme =
theme_service_observation_.GetSource()->GetBrowserColorScheme();
if (theme_color_scheme != ThemeService::BrowserColorScheme::kSystem) {
key.color_mode =
theme_color_scheme == ThemeService::BrowserColorScheme::kLight
? ui::ColorProviderKey::ColorMode::kLight
: ui::ColorProviderKey::ColorMode::kDark;
}
return key;
}
void GlicWidget::OnThemeChanged() {
NotifyColorProviderChanged();
ThemeChanged();
}
} // namespace glic
|