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 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
|
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/351564777): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif
// IMPORTANT NOTE: All QtShim members that use `delegate_` must be decorated
// with DISABLE_CFI_VCALL.
#include "ui/qt/qt_shim.h"
#include <cmath>
#include <QApplication>
#include <QFont>
#include <QIcon>
#include <QMimeDatabase>
#include <QMimeType>
#include <QPainter>
#include <QPalette>
#include <QScreen>
#include <QStyle>
#include <QStyleOptionTitleBar>
namespace qt {
namespace {
bool IsStyleItalic(QFont::Style style) {
switch (style) {
case QFont::Style::StyleNormal:
return false;
case QFont::Style::StyleItalic:
// gfx::Font::FontStyle doesn't support oblique, so treat it as italic.
case QFont::Style::StyleOblique:
return true;
}
}
FontHinting QtHintingToFontHinting(QFont::HintingPreference hinting) {
switch (hinting) {
case QFont::PreferDefaultHinting:
return FontHinting::kDefault;
case QFont::PreferNoHinting:
return FontHinting::kNone;
case QFont::PreferVerticalHinting:
// QT treats vertical hinting as "light" for Freetype:
// https://doc.qt.io/qt-5/qfont.html#HintingPreference-enum
return FontHinting::kLight;
case QFont::PreferFullHinting:
return FontHinting::kFull;
}
}
// Obtain the average color of a gradient.
SkColor GradientColor(const QGradient& gradient) {
QGradientStops stops = gradient.stops();
if (stops.empty()) {
return qRgba(0, 0, 0, 0);
}
float a = 0;
float r = 0;
float g = 0;
float b = 0;
for (int i = 0; i < stops.size(); i++) {
// Determine the extents of this stop. The whole gradient interval is [0,
// 1], so extend to the endpoint if this is the first or last stop.
float left_interval =
i == 0 ? stops[i].first : (stops[i].first - stops[i - 1].first) / 2;
float right_interval = i == stops.size() - 1
? 1 - stops[i].first
: (stops[i + 1].first - stops[i].first) / 2;
float length = left_interval + right_interval;
// alpha() returns a value in [0, 255] and alphaF() returns a value in
// [0, 1]. The color values are not premultiplied so the RGB channels need
// to be multiplied by the alpha (in range [0, 1]) before summing. The
// alpha doesn't need to be multiplied, so we just sum color.alpha() in
// range [0, 255] directly.
const QColor& color = stops[i].second;
a += color.alpha() * length;
r += color.alphaF() * color.red() * length;
g += color.alphaF() * color.green() * length;
b += color.alphaF() * color.blue() * length;
}
return qRgba(r, g, b, a);
}
// Obtain the average color of a texture.
SkColor TextureColor(QImage image) {
size_t size = image.width() * image.height();
if (!size) {
return qRgba(0, 0, 0, 0);
}
if (image.format() != QImage::Format_ARGB32_Premultiplied) {
image = image.convertToFormat(QImage::Format_ARGB32_Premultiplied);
}
size_t a = 0;
size_t r = 0;
size_t g = 0;
size_t b = 0;
const auto* pixels = reinterpret_cast<QRgb*>(image.bits());
for (size_t i = 0; i < size; i++) {
auto color = QColor::fromRgba(pixels[i]);
a += color.alpha();
r += color.red();
g += color.green();
b += color.blue();
}
return qRgba(r / size, g / size, b / size, a / size);
}
SkColor BrushColor(const QBrush& brush) {
QColor color = brush.color();
auto alpha_blend = [&](uint8_t alpha) {
QColor blended = color;
blended.setAlpha(blended.alpha() * alpha / 255);
return blended.rgba();
};
switch (brush.style()) {
case Qt::SolidPattern:
return alpha_blend(0xFF);
case Qt::Dense1Pattern:
return alpha_blend(0xE0);
case Qt::Dense2Pattern:
return alpha_blend(0xC0);
case Qt::Dense3Pattern:
return alpha_blend(0xA0);
case Qt::Dense4Pattern:
return alpha_blend(0x80);
case Qt::Dense5Pattern:
return alpha_blend(0x60);
case Qt::Dense6Pattern:
return alpha_blend(0x40);
case Qt::Dense7Pattern:
return alpha_blend(0x20);
case Qt::NoBrush:
return alpha_blend(0x00);
case Qt::HorPattern:
case Qt::VerPattern:
return alpha_blend(0x20);
case Qt::CrossPattern:
return alpha_blend(0x40);
case Qt::BDiagPattern:
case Qt::FDiagPattern:
return alpha_blend(0x20);
case Qt::DiagCrossPattern:
return alpha_blend(0x40);
case Qt::LinearGradientPattern:
case Qt::RadialGradientPattern:
case Qt::ConicalGradientPattern:
return GradientColor(*brush.gradient());
case Qt::TexturePattern:
return TextureColor(brush.textureImage());
}
}
QPalette::ColorRole ColorTypeToColorRole(ColorType type) {
switch (type) {
case ColorType::kWindowBg:
return QPalette::Window;
case ColorType::kWindowFg:
return QPalette::WindowText;
case ColorType::kHighlightBg:
return QPalette::Highlight;
case ColorType::kHighlightFg:
return QPalette::HighlightedText;
case ColorType::kEntryBg:
return QPalette::Base;
case ColorType::kEntryFg:
return QPalette::Text;
case ColorType::kButtonBg:
return QPalette::Button;
case ColorType::kButtonFg:
return QPalette::ButtonText;
case ColorType::kLight:
return QPalette::Light;
case ColorType::kMidlight:
return QPalette::Midlight;
case ColorType::kMidground:
return QPalette::Mid;
case ColorType::kDark:
return QPalette::Dark;
case ColorType::kShadow:
return QPalette::Shadow;
}
}
QPalette::ColorGroup ColorStateToColorGroup(ColorState state) {
switch (state) {
case ColorState::kNormal:
return QPalette::Normal;
case ColorState::kDisabled:
return QPalette::Disabled;
case ColorState::kInactive:
return QPalette::Inactive;
}
}
float GetScreenScale(const QScreen* screen) {
constexpr double kDefaultPixelDpi = 96.0;
double scale = screen->devicePixelRatio() * screen->logicalDotsPerInch() /
kDefaultPixelDpi;
// Round to the nearest 1/16th so that UI can losslessly multiply and divide
// by the scale factor using floating point arithmetic. GtkUi also rounds
// in this way, but to 1/64th. 1/16th is chosen here since that's what
// KDE settings uses.
scale = std::round(scale * 16) / 16;
return scale > 0 ? scale : 1.0;
}
} // namespace
QtShim::QtShim(QtInterface::Delegate* delegate, int* argc, char** argv)
: delegate_(delegate), app_(*argc, argv) {
connect(&app_, SIGNAL(fontChanged(const QFont&)), this,
SLOT(FontChanged(const QFont&)));
connect(&app_, SIGNAL(paletteChanged(const QPalette&)), this,
SLOT(PaletteChanged(const QPalette&)));
connect(&app_, SIGNAL(screenAdded(QScreen*)), this,
SLOT(ScreenAdded(QScreen*)));
connect(&app_, SIGNAL(screenRemoved(QScreen*)), this,
SLOT(ScreenRemoved(QScreen*)));
for (QScreen* screen : app_.screens()) {
ScreenAdded(screen);
}
}
QtShim::~QtShim() = default;
size_t QtShim::GetMonitorConfig(MonitorScale** monitors, float* primary_scale) {
size_t n_monitors = app_.screens().size();
monitor_scales_.resize(n_monitors);
for (size_t i = 0; i < n_monitors; i++) {
const QScreen* screen = app_.screens()[i];
MonitorScale monitor = monitor_scales_[i];
monitor.x_px = screen->geometry().x();
monitor.y_px = screen->geometry().y();
monitor.width_px = screen->geometry().width();
monitor.height_px = screen->geometry().height();
monitor.scale = GetScreenScale(screen);
}
*monitors = monitor_scales_.data();
*primary_scale = GetScreenScale(app_.primaryScreen());
return n_monitors;
}
FontRenderParams QtShim::GetFontRenderParams() const {
QFont font = app_.font();
auto style = font.styleStrategy();
return {
.antialiasing = !(style & QFont::StyleStrategy::NoAntialias),
.use_bitmaps = !!(style & QFont::StyleStrategy::PreferBitmap),
.hinting = QtHintingToFontHinting(font.hintingPreference()),
};
}
FontDescription QtShim::GetFontDescription() const {
QFont font = app_.font();
return {
.family = String(font.family().toStdString().c_str()),
.size_pixels = font.pixelSize(),
.size_points = font.pointSize(),
.is_italic = IsStyleItalic(font.style()),
.weight = font.weight(),
};
}
Image QtShim::GetIconForContentType(const String& content_type,
int size) const {
QMimeDatabase db;
for (const char* mime : {content_type.c_str(), "application/octet-stream"}) {
auto mt = db.mimeTypeForName(mime);
for (const auto& name : {mt.iconName(), mt.genericIconName()}) {
auto icon = QIcon::fromTheme(name);
auto pixmap = icon.pixmap(size);
auto image = pixmap.toImage();
if (image.format() != QImage::Format_ARGB32_Premultiplied) {
image = image.convertToFormat(QImage::Format_ARGB32_Premultiplied);
}
if (auto bytes = image.sizeInBytes()) {
return {image.width(), image.height(),
static_cast<float>(image.devicePixelRatio()),
Buffer(image.bits(), bytes)};
}
}
}
return {};
}
SkColor QtShim::GetColor(ColorType role, ColorState state) const {
return BrushColor(app_.palette().brush(ColorStateToColorGroup(state),
ColorTypeToColorRole(role)));
}
SkColor QtShim::GetFrameColor(ColorState state, bool use_custom_frame) const {
constexpr int kSampleSize = 32;
return TextureColor(DrawHeaderImpl(kSampleSize, kSampleSize,
GetColor(ColorType::kWindowBg, state),
state, use_custom_frame));
}
int QtShim::GetCursorBlinkIntervalMs() const {
return app_.cursorFlashTime();
}
int QtShim::GetAnimationDurationMs() const {
return app_.style()->styleHint(QStyle::SH_Widget_Animation_Duration);
}
DISABLE_CFI_VCALL
void QtShim::FontChanged(const QFont& font) {
delegate_->FontChanged();
}
DISABLE_CFI_VCALL
void QtShim::PaletteChanged(const QPalette& palette) {
delegate_->ThemeChanged();
}
DISABLE_CFI_VCALL
void QtShim::ScreenAdded(QScreen* screen) {
connect(screen, SIGNAL(logicalDotsPerInchChanged(qreal)), this,
SLOT(LogicalDotsPerInchChanged(qreal)));
connect(screen, SIGNAL(physicalDotsPerInchChanged(qreal)), this,
SLOT(PhysicalDotsPerInchChanged(qreal)));
delegate_->ScaleFactorMaybeChanged();
}
DISABLE_CFI_VCALL
void QtShim::ScreenRemoved(QScreen* screen) {
delegate_->ScaleFactorMaybeChanged();
}
DISABLE_CFI_VCALL
void QtShim::LogicalDotsPerInchChanged(qreal dpi) {
delegate_->ScaleFactorMaybeChanged();
}
DISABLE_CFI_VCALL
void QtShim::PhysicalDotsPerInchChanged(qreal dpi) {
delegate_->ScaleFactorMaybeChanged();
}
Image QtShim::DrawHeader(int width,
int height,
SkColor default_color,
ColorState state,
bool use_custom_frame) const {
QImage image =
DrawHeaderImpl(width, height, default_color, state, use_custom_frame);
return {width, height, 1.0f, Buffer(image.bits(), image.sizeInBytes())};
}
QImage QtShim::DrawHeaderImpl(int width,
int height,
SkColor default_color,
ColorState state,
bool use_custom_frame) const {
QImage image(width, height, QImage::Format_ARGB32_Premultiplied);
image.fill(default_color);
QPainter painter(&image);
if (use_custom_frame) {
// Chrome renders it's own window border, so clip the border out by
// rendering the titlebar larger than the image.
constexpr int kBorderWidth = 5;
QStyleOptionTitleBar opt;
opt.rect = QRect(-kBorderWidth, -kBorderWidth, width + 2 * kBorderWidth,
height + 2 * kBorderWidth);
if (state == ColorState::kNormal) {
opt.titleBarState = QStyle::State_Active;
}
app_.style()->drawComplexControl(QStyle::CC_TitleBar, &opt, &painter,
nullptr);
} else {
painter.fillRect(
0, 0, width, height,
app_.palette().brush(ColorStateToColorGroup(state), QPalette::Window));
}
return image;
}
} // namespace qt
qt::QtInterface* CreateQtInterface(qt::QtInterface::Delegate* delegate,
int* argc,
char** argv) {
return new qt::QtShim(delegate, argc, argv);
}
|