| 1 |
/* |
| 2 |
* SPDX-FileCopyrightText: 2021~2021 CSSlayer <[email protected]> |
| 3 |
* |
| 4 |
* SPDX-License-Identifier: BSD-3-Clause |
| 5 |
* |
| 6 |
*/ |
| 7 |
#include "fcitxcandidatewindow.h" |
| 8 |
#include "fcitxflags.h" |
| 9 |
#include "fcitxtheme.h" |
| 10 |
#include "qfcitxplatforminputcontext.h" |
| 11 |
#include <QDebug> |
| 12 |
#include <QExposeEvent> |
| 13 |
#include <QFont> |
| 14 |
#include <QGuiApplication> |
| 15 |
#include <QMouseEvent> |
| 16 |
#include <QPalette> |
| 17 |
#include <QResizeEvent> |
| 18 |
#include <QScreen> |
| 19 |
#include <QTextLayout> |
| 20 |
#include <QVariant> |
| 21 |
#include <QtMath> |
| 22 |
#include <utility> |
| 23 |
|
| 24 |
#if defined(FCITX_ENABLE_QT6_WAYLAND_WORKAROUND) && \ |
| 25 |
QT_VERSION >= QT_VERSION_CHECK(6, 6, 0) |
| 26 |
|
| 27 |
#include <QtGui/private/qhighdpiscaling_p.h> |
| 28 |
#include <QtWaylandClient/private/qwaylandwindow_p.h> |
| 29 |
|
| 30 |
#if QT_VERSION < QT_VERSION_CHECK(6, 10, 0) |
| 31 |
#include <QtWaylandClient/private/qwayland-xdg-shell.h> |
| 32 |
#include <QtWaylandClient/private/qwaylanddisplay_p.h> |
| 33 |
#include <QtWaylandClient/private/qwaylandintegration_p.h> |
| 34 |
#include <QtWaylandClient/private/wayland-xdg-shell-client-protocol.h> |
| 35 |
#include <qpa/qplatformnativeinterface.h> |
| 36 |
#endif |
| 37 |
|
| 38 |
#endif |
| 39 |
|
| 40 |
namespace fcitx { |
| 41 |
|
| 42 |
namespace { |
| 43 |
|
| 44 |
#if defined(FCITX_ENABLE_QT6_WAYLAND_WORKAROUND) && \ |
| 45 |
QT_VERSION >= QT_VERSION_CHECK(6, 6, 0) && \ |
| 46 |
QT_VERSION < QT_VERSION_CHECK(6, 10, 0) |
| 47 |
class XdgWmBase : public QtWayland::xdg_wm_base { |
| 48 |
public: |
| 49 |
using xdg_wm_base::xdg_wm_base; |
| 50 |
|
| 51 |
protected: |
| 52 |
// This is required for all xdg_wm_base bind. |
| 53 |
void xdg_wm_base_ping(uint32_t serial) override { pong(serial); } |
| 54 |
}; |
| 55 |
|
| 56 |
#endif |
| 57 |
|
| 58 |
void doLayout(QTextLayout &layout) { |
| 59 |
QFontMetrics fontMetrics(layout.font()); |
| 60 |
auto minH = fontMetrics.ascent() + fontMetrics.descent(); |
| 61 |
layout.setCacheEnabled(true); |
| 62 |
layout.beginLayout(); |
| 63 |
int height = 0; |
| 64 |
while (1) { |
| 65 |
QTextLine line = layout.createLine(); |
| 66 |
if (!line.isValid()) |
| 67 |
break; |
| 68 |
|
| 69 |
line.setLineWidth(INT_MAX); |
| 70 |
line.setLeadingIncluded(true); |
| 71 |
|
| 72 |
line.setPosition( |
| 73 |
QPoint(0, height - line.ascent() + fontMetrics.ascent())); |
| 74 |
height += minH; |
| 75 |
} |
| 76 |
layout.endLayout(); |
| 77 |
} |
| 78 |
|
| 79 |
} // namespace |
| 80 |
|
| 81 |
class MultilineText { |
| 82 |
public: |
| 83 |
MultilineText(const QFont &font, const QString &text) { |
| 84 |
QStringList lines = text.split("\n"); |
| 85 |
int width = 0; |
| 86 |
QFontMetrics fontMetrics(font); |
| 87 |
fontHeight_ = fontMetrics.ascent() + fontMetrics.descent(); |
| 88 |
for (const auto &line : lines) { |
| 89 |
layouts_.emplace_back(std::make_unique<QTextLayout>(line)); |
| 90 |
layouts_.back()->setFont(font); |
| 91 |
doLayout(*layouts_.back()); |
| 92 |
width = std::max(width, |
| 93 |
layouts_.back()->boundingRect().toRect().width()); |
| 94 |
} |
| 95 |
boundingRect_.setTopLeft(QPoint(0, 0)); |
| 96 |
boundingRect_.setSize(QSize(width, lines.size() * fontHeight_)); |
| 97 |
} |
| 98 |
|
| 99 |
bool isEmpty() const { return layouts_.empty(); } |
| 100 |
|
| 101 |
void draw(QPainter *painter, QColor color, QPoint position) { |
| 102 |
painter->save(); |
| 103 |
painter->setPen(color); |
| 104 |
int currentY = 0; |
| 105 |
for (const auto &layout : layouts_) { |
| 106 |
layout->draw(painter, position + QPoint(0, currentY)); |
| 107 |
currentY += fontHeight_; |
| 108 |
} |
| 109 |
painter->restore(); |
| 110 |
} |
| 111 |
|
| 112 |
QRect boundingRect() { return boundingRect_; } |
| 113 |
|
| 114 |
private: |
| 115 |
std::vector<std::unique_ptr<QTextLayout>> layouts_; |
| 116 |
int fontHeight_; |
| 117 |
QRect boundingRect_; |
| 118 |
}; |
| 119 |
|
| 120 |
FcitxCandidateWindow::FcitxCandidateWindow(QWindow *window, |
| 121 |
QFcitxPlatformInputContext *context) |
| 122 |
: QRasterWindow(), context_(context), theme_(context->theme()), |
| 123 |
parent_(window) { |
| 124 |
constexpr Qt::WindowFlags commonFlags = Qt::FramelessWindowHint | |
| 125 |
Qt::WindowDoesNotAcceptFocus | |
| 126 |
Qt::NoDropShadowWindowHint; |
| 127 |
if (isWayland_) { |
| 128 |
// Qt::ToolTip ensures wayland doesn't grab focus. |
| 129 |
// Not using Qt::BypassWindowManagerHint ensures wayland handle |
| 130 |
// fractional scale. |
| 131 |
setFlags(Qt::ToolTip | commonFlags); |
| 132 |
#if defined(FCITX_ENABLE_QT6_WAYLAND_WORKAROUND) && \ |
| 133 |
QT_VERSION >= QT_VERSION_CHECK(6, 6, 0) |
| 134 |
#if QT_VERSION < QT_VERSION_CHECK(6, 10, 0) |
| 135 |
if (auto instance = QtWaylandClient::QWaylandIntegration::instance()) { |
| 136 |
for (QtWaylandClient::QWaylandDisplay::RegistryGlobal global : |
| 137 |
instance->display()->globals()) { |
| 138 |
if (global.interface == QLatin1String("xdg_wm_base")) { |
| 139 |
xdgWmBase_.reset( |
| 140 |
new XdgWmBase(instance->display()->wl_registry(), |
| 141 |
global.id, global.version)); |
| 142 |
break; |
| 143 |
} |
| 144 |
} |
| 145 |
} |
| 146 |
#endif |
| 147 |
setProperty("_q_waylandPopupAnchor", |
| 148 |
QVariant::fromValue(Qt::BottomEdge | Qt::LeftEdge)); |
| 149 |
setProperty("_q_waylandPopupGravity", |
| 150 |
QVariant::fromValue(Qt::BottomEdge | Qt::RightEdge)); |
| 151 |
setProperty( |
| 152 |
"_q_waylandPopupConstraintAdjustment", |
| 153 |
static_cast<unsigned int>( |
| 154 |
QtWayland::xdg_positioner::constraint_adjustment_slide_x | |
| 155 |
QtWayland::xdg_positioner::constraint_adjustment_flip_y)); |
| 156 |
#endif |
| 157 |
} else { |
| 158 |
// Qt::Popup ensures X11 doesn't apply tooltip animation under kwin. |
| 159 |
setFlags(Qt::Popup | Qt::BypassWindowManagerHint | commonFlags); |
| 160 |
} |
| 161 |
if (isWayland_) { |
| 162 |
setTransientParent(parent_); |
| 163 |
} |
| 164 |
QSurfaceFormat surfaceFormat = format(); |
| 165 |
surfaceFormat.setAlphaBufferSize(8); |
| 166 |
setFormat(surfaceFormat); |
| 167 |
connect(this, &QWindow::visibleChanged, this, [this] { hoverIndex_ = -1; }); |
| 168 |
} |
| 169 |
|
| 170 |
FcitxCandidateWindow::~FcitxCandidateWindow() {} |
| 171 |
|
| 172 |
bool FcitxCandidateWindow::event(QEvent *event) { |
| 173 |
if (event->type() == QEvent::Leave) { |
| 174 |
auto oldHighlight = highlight(); |
| 175 |
hoverIndex_ = -1; |
| 176 |
if (highlight() != oldHighlight) { |
| 177 |
update(); |
| 178 |
} |
| 179 |
} |
| 180 |
return QRasterWindow::event(event); |
| 181 |
} |
| 182 |
|
| 183 |
void FcitxCandidateWindow::render(QPainter *painter) { |
| 184 |
theme_->paint(painter, theme_->background(), |
| 185 |
QRect(QPoint(0, 0), actualSize_)); |
| 186 |
auto contentMargin = theme_->contentMargin(); |
| 187 |
|
| 188 |
const QPoint topLeft(contentMargin.left(), contentMargin.top()); |
| 189 |
painter->setPen(theme_->normalColor()); |
| 190 |
auto minH = |
| 191 |
theme_->fontMetrics().ascent() + theme_->fontMetrics().descent(); |
| 192 |
auto textMargin = theme_->textMargin(); |
| 193 |
auto extraW = textMargin.left() + textMargin.right(); |
| 194 |
auto extraH = textMargin.top() + textMargin.bottom(); |
| 195 |
size_t currentHeight = 0; |
| 196 |
if (!upperLayout_.text().isEmpty()) { |
| 197 |
upperLayout_.draw( |
| 198 |
painter, topLeft + QPoint(textMargin.left(), textMargin.top())); |
| 199 |
// Draw cursor |
| 200 |
currentHeight += minH + extraH; |
| 201 |
if (cursor_ >= 0) { |
| 202 |
auto line = upperLayout_.lineForTextPosition(cursor_); |
| 203 |
if (line.isValid()) { |
| 204 |
int cursorX = line.cursorToX(cursor_); |
| 205 |
line.lineNumber(); |
| 206 |
painter->save(); |
| 207 |
QPen pen = painter->pen(); |
| 208 |
pen.setWidth(2); |
| 209 |
painter->setPen(pen); |
| 210 |
QPoint start = topLeft + QPoint(textMargin.left() + cursorX + 1, |
| 211 |
textMargin.top() + |
| 212 |
line.lineNumber() * minH); |
| 213 |
painter->drawLine(start, start + QPoint(0, minH)); |
| 214 |
painter->restore(); |
| 215 |
} |
| 216 |
} |
| 217 |
} |
| 218 |
if (!lowerLayout_.text().isEmpty()) { |
| 219 |
lowerLayout_.draw(painter, |
| 220 |
topLeft + QPoint(textMargin.left(), |
| 221 |
textMargin.top() + currentHeight)); |
| 222 |
currentHeight += minH + extraH; |
| 223 |
} |
| 224 |
|
| 225 |
bool vertical = theme_->vertical(); |
| 226 |
if (layoutHint_ == FcitxCandidateLayoutHint::Vertical) { |
| 227 |
vertical = true; |
| 228 |
} else if (layoutHint_ == FcitxCandidateLayoutHint::Horizontal) { |
| 229 |
vertical = false; |
| 230 |
} |
| 231 |
|
| 232 |
candidateRegions_.clear(); |
| 233 |
candidateRegions_.reserve(labelLayouts_.size()); |
| 234 |
size_t wholeW = 0, wholeH = 0; |
| 235 |
|
| 236 |
// size of text = textMargin + actual text size. |
| 237 |
// HighLight = HighLight margin + TEXT. |
| 238 |
// Click region = HighLight - click |
| 239 |
|
| 240 |
for (size_t i = 0; i < labelLayouts_.size(); i++) { |
| 241 |
int x, y; |
| 242 |
if (vertical) { |
| 243 |
x = 0; |
| 244 |
y = currentHeight + wholeH; |
| 245 |
} else { |
| 246 |
x = wholeW; |
| 247 |
y = currentHeight; |
| 248 |
} |
| 249 |
x += textMargin.left(); |
| 250 |
y += textMargin.top(); |
| 251 |
int labelW = 0, labelH = 0, candidateW = 0, candidateH = 0; |
| 252 |
if (!labelLayouts_[i]->isEmpty()) { |
| 253 |
auto size = labelLayouts_[i]->boundingRect(); |
| 254 |
labelW = size.width(); |
| 255 |
labelH = size.height(); |
| 256 |
} |
| 257 |
if (!candidateLayouts_[i]->isEmpty()) { |
| 258 |
auto size = candidateLayouts_[i]->boundingRect(); |
| 259 |
candidateW = size.width(); |
| 260 |
candidateH = size.height(); |
| 261 |
} |
| 262 |
int vheight; |
| 263 |
if (vertical) { |
| 264 |
vheight = std::max({minH, labelH, candidateH}); |
| 265 |
wholeH += vheight + extraH; |
| 266 |
} else { |
| 267 |
vheight = candidatesHeight_ - extraH; |
| 268 |
wholeW += candidateW + labelW + extraW; |
| 269 |
} |
| 270 |
QMargins highlightMargin = theme_->highlightMargin(); |
| 271 |
QMargins clickMargin = theme_->highlightClickMargin(); |
| 272 |
auto highlightWidth = labelW + candidateW; |
| 273 |
bool fullWidthHighlight = true; |
| 274 |
if (fullWidthHighlight && vertical) { |
| 275 |
// Last candidate, fill. |
| 276 |
highlightWidth = actualSize_.width() - contentMargin.left() - |
| 277 |
contentMargin.right() - textMargin.left() - |
| 278 |
textMargin.right(); |
| 279 |
} |
| 280 |
const int highlightIndex = highlight(); |
| 281 |
QColor color = theme_->normalColor(); |
| 282 |
if (highlightIndex >= 0 && i == static_cast<size_t>(highlightIndex)) { |
| 283 |
// Paint highlight |
| 284 |
theme_->paint( |
| 285 |
painter, theme_->highlight(), |
| 286 |
QRect(topLeft + QPoint(x, y) - |
| 287 |
QPoint(highlightMargin.left(), highlightMargin.top()), |
| 288 |
QSize(highlightWidth + highlightMargin.left() + |
| 289 |
highlightMargin.right(), |
| 290 |
vheight + highlightMargin.top() + |
| 291 |
highlightMargin.bottom()))); |
| 292 |
color = theme_->highlightCandidateColor(); |
| 293 |
} |
| 294 |
QRect candidateRegion( |
| 295 |
topLeft + QPoint(x, y) - |
| 296 |
QPoint(highlightMargin.left(), highlightMargin.right()) + |
| 297 |
QPoint(clickMargin.left(), clickMargin.right()), |
| 298 |
QSize(highlightWidth + highlightMargin.left() + |
| 299 |
highlightMargin.right() - clickMargin.left() - |
| 300 |
clickMargin.right(), |
| 301 |
vheight + highlightMargin.top() + highlightMargin.bottom() - |
| 302 |
clickMargin.top() - clickMargin.bottom())); |
| 303 |
candidateRegions_.push_back(candidateRegion); |
| 304 |
if (!labelLayouts_[i]->isEmpty()) { |
| 305 |
labelLayouts_[i]->draw(painter, color, topLeft + QPoint(x, y)); |
| 306 |
} |
| 307 |
if (!candidateLayouts_[i]->isEmpty()) { |
| 308 |
candidateLayouts_[i]->draw(painter, color, |
| 309 |
topLeft + QPoint(x + labelW, y)); |
| 310 |
} |
| 311 |
} |
| 312 |
prevRegion_ = QRect(); |
| 313 |
nextRegion_ = QRect(); |
| 314 |
if (labelLayouts_.size() && (hasPrev_ || hasNext_)) { |
| 315 |
if (theme_->prev().valid() && theme_->next().valid()) { |
| 316 |
int prevY = 0, nextY = 0; |
| 317 |
if (theme_->buttonAlignment() == "Top") { |
| 318 |
prevY = contentMargin.top(); |
| 319 |
nextY = contentMargin.top(); |
| 320 |
} else if (theme_->buttonAlignment() == "First Candidate") { |
| 321 |
prevY = candidateRegions_.front().top() + |
| 322 |
(candidateRegions_.front().height() - |
| 323 |
theme_->prev().height()) / |
| 324 |
2.0; |
| 325 |
nextY = candidateRegions_.front().top() + |
| 326 |
(candidateRegions_.front().height() - |
| 327 |
theme_->next().height()) / |
| 328 |
2.0; |
| 329 |
} else if (theme_->buttonAlignment() == "Center") { |
| 330 |
prevY = contentMargin.top() + |
| 331 |
(actualSize_.height() - contentMargin.top() - |
| 332 |
contentMargin.bottom() - theme_->prev().height()) / |
| 333 |
2.0; |
| 334 |
nextY = contentMargin.top() + |
| 335 |
(actualSize_.height() - contentMargin.top() - |
| 336 |
contentMargin.bottom() - theme_->next().height()) / |
| 337 |
2.0; |
| 338 |
} else if (theme_->buttonAlignment() == "Last Candidate") { |
| 339 |
prevY = candidateRegions_.back().top() + |
| 340 |
(candidateRegions_.back().height() - |
| 341 |
theme_->prev().height()) / |
| 342 |
2.0; |
| 343 |
nextY = candidateRegions_.back().top() + |
| 344 |
(candidateRegions_.back().height() - |
| 345 |
theme_->next().height()) / |
| 346 |
2.0; |
| 347 |
} else { |
| 348 |
prevY = actualSize_.height() - contentMargin.bottom() - |
| 349 |
theme_->prev().height(); |
| 350 |
nextY = actualSize_.height() - contentMargin.bottom() - |
| 351 |
theme_->next().height(); |
| 352 |
} |
| 353 |
nextRegion_ = |
| 354 |
QRect(QPoint(actualSize_.width() - contentMargin.right() - |
| 355 |
theme_->prev().width(), |
| 356 |
nextY), |
| 357 |
theme_->next().size()); |
| 358 |
double alpha = 1.0; |
| 359 |
if (!hasNext_) { |
| 360 |
alpha = 0.3; |
| 361 |
} else if (nextHovered_) { |
| 362 |
alpha = 0.7; |
| 363 |
} |
| 364 |
theme_->paint(painter, theme_->next(), nextRegion_.topLeft(), |
| 365 |
alpha); |
| 366 |
nextRegion_ = nextRegion_.marginsRemoved(theme_->next().margin()); |
| 367 |
prevRegion_ = QRect( |
| 368 |
QPoint(actualSize_.width() - contentMargin.right() - |
| 369 |
theme_->next().width() - theme_->prev().width(), |
| 370 |
prevY), |
| 371 |
theme_->prev().size()); |
| 372 |
alpha = 1.0; |
| 373 |
if (!hasPrev_) { |
| 374 |
alpha = 0.3; |
| 375 |
} else if (prevHovered_) { |
| 376 |
alpha = 0.7; |
| 377 |
} |
| 378 |
theme_->paint(painter, theme_->prev(), prevRegion_.topLeft(), |
| 379 |
alpha); |
| 380 |
prevRegion_ = prevRegion_.marginsRemoved(theme_->prev().margin()); |
| 381 |
} |
| 382 |
} |
| 383 |
} |
| 384 |
|
| 385 |
void UpdateLayout(QTextLayout &layout, const FcitxTheme &theme, |
| 386 |
std::initializer_list< |
| 387 |
std::reference_wrapper<const FcitxQtFormattedPreeditList>> |
| 388 |
texts) { |
| 389 |
#if QT_VERSION >= QT_VERSION_CHECK(5, 6, 0) |
| 390 |
layout.clearFormats(); |
| 391 |
#endif |
| 392 |
layout.setFont(theme.font()); |
| 393 |
QVector<QTextLayout::FormatRange> formats; |
| 394 |
QString str; |
| 395 |
int pos = 0; |
| 396 |
for (const auto &text : texts) { |
| 397 |
for (const auto &preedit : text.get()) { |
| 398 |
str += preedit.string(); |
| 399 |
QTextCharFormat format; |
| 400 |
if (preedit.format() & FcitxTextFormatFlag_Underline) { |
| 401 |
format.setUnderlineStyle(QTextCharFormat::DashUnderline); |
| 402 |
} |
| 403 |
if (preedit.format() & FcitxTextFormatFlag_Strike) { |
| 404 |
format.setFontStrikeOut(true); |
| 405 |
} |
| 406 |
if (preedit.format() & FcitxTextFormatFlag_Bold) { |
| 407 |
format.setFontWeight(QFont::Bold); |
| 408 |
} |
| 409 |
if (preedit.format() & FcitxTextFormatFlag_Italic) { |
| 410 |
format.setFontItalic(true); |
| 411 |
} |
| 412 |
if (preedit.format() & FcitxTextFormatFlag_HighLight) { |
| 413 |
format.setBackground(theme.highlightBackgroundColor()); |
| 414 |
format.setForeground(theme.highlightColor()); |
| 415 |
} |
| 416 |
formats.append(QTextLayout::FormatRange{ |
| 417 |
pos, static_cast<int>(preedit.string().length()), format}); |
| 418 |
pos += preedit.string().length(); |
| 419 |
} |
| 420 |
} |
| 421 |
layout.setText(str); |
| 422 |
#if QT_VERSION >= QT_VERSION_CHECK(5, 6, 0) |
| 423 |
layout.setFormats(formats); |
| 424 |
#endif |
| 425 |
} |
| 426 |
|
| 427 |
void FcitxCandidateWindow::updateClientSideUI( |
| 428 |
const FcitxQtFormattedPreeditList &preedit, int cursorpos, |
| 429 |
const FcitxQtFormattedPreeditList &auxUp, |
| 430 |
const FcitxQtFormattedPreeditList &auxDown, |
| 431 |
const FcitxQtStringKeyValueList &candidates, int candidateIndex, |
| 432 |
int layoutHint, bool hasPrev, bool hasNext) { |
| 433 |
bool preeditVisible = !preedit.isEmpty(); |
| 434 |
bool auxUpVisbile = !auxUp.isEmpty(); |
| 435 |
bool auxDownVisible = !auxDown.isEmpty(); |
| 436 |
bool candidatesVisible = !candidates.isEmpty(); |
| 437 |
bool visible = |
| 438 |
preeditVisible || auxUpVisbile || auxDownVisible || candidatesVisible; |
| 439 |
auto window = context_->focusWindowWrapper(); |
| 440 |
if (!theme_ || !visible || !window || window != parent_) { |
| 441 |
hide(); |
| 442 |
return; |
| 443 |
} |
| 444 |
|
| 445 |
UpdateLayout(upperLayout_, *theme_, {auxUp, preedit}); |
| 446 |
if (cursorpos >= 0) { |
| 447 |
int auxUpLength = 0; |
| 448 |
for (const auto &auxUpText : auxUp) { |
| 449 |
auxUpLength += auxUpText.string().length(); |
| 450 |
} |
| 451 |
// Get the preedit part |
| 452 |
auto preeditString = upperLayout_.text().mid(auxUpLength).toUtf8(); |
| 453 |
preeditString = preeditString.mid(0, cursorpos); |
| 454 |
cursor_ = auxUpLength + QString::fromUtf8(preeditString).length(); |
| 455 |
} else { |
| 456 |
cursor_ = -1; |
| 457 |
} |
| 458 |
doLayout(upperLayout_); |
| 459 |
UpdateLayout(lowerLayout_, *theme_, {auxDown}); |
| 460 |
doLayout(lowerLayout_); |
| 461 |
labelLayouts_.clear(); |
| 462 |
candidateLayouts_.clear(); |
| 463 |
for (const auto &candidate : candidates) { |
| 464 |
labelLayouts_.emplace_back( |
| 465 |
std::make_unique<MultilineText>(theme_->font(), candidate.key())); |
| 466 |
candidateLayouts_.emplace_back( |
| 467 |
std::make_unique<MultilineText>(theme_->font(), candidate.value())); |
| 468 |
} |
| 469 |
highlight_ = candidateIndex; |
| 470 |
hasPrev_ = hasPrev; |
| 471 |
hasNext_ = hasNext; |
| 472 |
layoutHint_ = static_cast<FcitxCandidateLayoutHint>(layoutHint); |
| 473 |
|
| 474 |
actualSize_ = sizeHint(); |
| 475 |
|
| 476 |
if (actualSize_.width() <= 0 || actualSize_.height() <= 0) { |
| 477 |
hide(); |
| 478 |
return; |
| 479 |
} |
| 480 |
|
| 481 |
if (size() != actualSize_) { |
| 482 |
resize(actualSize_); |
| 483 |
} |
| 484 |
update(); |
| 485 |
|
| 486 |
updatePosition(); |
| 487 |
} |
| 488 |
|
| 489 |
void FcitxCandidateWindow::updatePosition() { |
| 490 |
auto *window = context_->focusWindowWrapper(); |
| 491 |
if (!window) { |
| 492 |
hide(); |
| 493 |
return; |
| 494 |
} |
| 495 |
QRect cursorRect = context_->cursorRectangleWrapper(); |
| 496 |
QRect screenGeometry; |
| 497 |
|
| 498 |
#if defined(FCITX_ENABLE_QT6_WAYLAND_WORKAROUND) && \ |
| 499 |
QT_VERSION >= QT_VERSION_CHECK(6, 6, 0) |
| 500 |
if (isWayland_) { |
| 501 |
auto *waylandWindow = |
| 502 |
static_cast<QtWaylandClient::QWaylandWindow *>(window->handle()); |
| 503 |
auto windowGeometry = waylandWindow->windowContentGeometry(); |
| 504 |
if (!cursorRect.isValid()) { |
| 505 |
if (cursorRect.width() <= 0) { |
| 506 |
cursorRect.setWidth(1); |
| 507 |
} |
| 508 |
if (cursorRect.height() <= 0) { |
| 509 |
cursorRect.setHeight(1); |
| 510 |
} |
| 511 |
} |
| 512 |
QRect nativeCursorRect = QHighDpi::toNativePixels(cursorRect, this); |
| 513 |
// valid the anchor rect. |
| 514 |
if (!nativeCursorRect.intersects(windowGeometry)) { |
| 515 |
if (nativeCursorRect.right() < windowGeometry.left()) { |
| 516 |
nativeCursorRect.setLeft(windowGeometry.left()); |
| 517 |
nativeCursorRect.setWidth(1); |
| 518 |
} |
| 519 |
if (nativeCursorRect.left() > windowGeometry.right()) { |
| 520 |
nativeCursorRect.setLeft(windowGeometry.right()); |
| 521 |
nativeCursorRect.setWidth(1); |
| 522 |
} |
| 523 |
if (nativeCursorRect.bottom() < windowGeometry.top()) { |
| 524 |
nativeCursorRect.setTop(windowGeometry.top()); |
| 525 |
nativeCursorRect.setHeight(1); |
| 526 |
} |
| 527 |
if (nativeCursorRect.top() > windowGeometry.bottom()) { |
| 528 |
nativeCursorRect.setTop(windowGeometry.bottom()); |
| 529 |
nativeCursorRect.setHeight(1); |
| 530 |
} |
| 531 |
} |
| 532 |
|
| 533 |
bool cursorRectChanged = false; |
| 534 |
Q_UNUSED(cursorRectChanged); |
| 535 |
if (property("_q_waylandPopupAnchorRect") != nativeCursorRect) { |
| 536 |
cursorRectChanged = true; |
| 537 |
setProperty("_q_waylandPopupAnchorRect", nativeCursorRect); |
| 538 |
setPosition(cursorRect.topLeft()); |
| 539 |
} |
| 540 |
#if QT_VERSION < QT_VERSION_CHECK(6, 10, 0) |
| 541 |
const auto windowMargins = waylandWindow->windowContentMargins() - |
| 542 |
waylandWindow->clientSideMargins(); |
| 543 |
bool wasVisible = isVisible(); |
| 544 |
#endif |
| 545 |
|
| 546 |
show(); |
| 547 |
#if QT_VERSION < QT_VERSION_CHECK(6, 10, 0) |
| 548 |
xdg_popup *xdgPopup = static_cast<xdg_popup *>( |
| 549 |
QGuiApplication::platformNativeInterface()->nativeResourceForWindow( |
| 550 |
"xdg_popup", this)); |
| 551 |
if (xdgWmBase_ && xdgPopup && |
| 552 |
xdg_popup_get_version(xdgPopup) >= |
| 553 |
XDG_POPUP_REPOSITION_SINCE_VERSION) { |
| 554 |
nativeCursorRect.translate(-windowMargins.left(), |
| 555 |
-windowMargins.top()); |
| 556 |
auto *positioner = |
| 557 |
new QtWayland::xdg_positioner(xdgWmBase_->create_positioner()); |
| 558 |
positioner->set_anchor_rect( |
| 559 |
nativeCursorRect.x(), nativeCursorRect.y(), |
| 560 |
nativeCursorRect.width(), nativeCursorRect.height()); |
| 561 |
positioner->set_anchor( |
| 562 |
QtWayland::xdg_positioner::anchor_bottom_left); |
| 563 |
positioner->set_gravity( |
| 564 |
QtWayland::xdg_positioner::gravity_bottom_right); |
| 565 |
|
| 566 |
auto *waylandCandidateWindow = |
| 567 |
static_cast<QtWaylandClient::QWaylandWindow *>(handle()); |
| 568 |
QRect nativeGeometry = |
| 569 |
waylandCandidateWindow->windowContentGeometry(); |
| 570 |
positioner->set_size(nativeGeometry.width(), |
| 571 |
nativeGeometry.height()); |
| 572 |
positioner->set_reactive(); |
| 573 |
positioner->set_constraint_adjustment( |
| 574 |
QtWayland::xdg_positioner::constraint_adjustment_slide_x | |
| 575 |
QtWayland::xdg_positioner::constraint_adjustment_flip_y); |
| 576 |
xdg_popup_reposition(xdgPopup, positioner->object(), |
| 577 |
repositionToken_++); |
| 578 |
positioner->destroy(); |
| 579 |
return; |
| 580 |
} |
| 581 |
// Check if we need remap. |
| 582 |
// If it was invisible, nothing need to be done. |
| 583 |
// If cursor rect changed, the window must be remapped. |
| 584 |
// If adjustment is already happening (flip/slide), then remap. |
| 585 |
// If we predict adjustment may be happening, then remap. |
| 586 |
const auto predictGeometry = |
| 587 |
QRect(QPoint(cursorRect.x(), cursorRect.y() + cursorRect.height()), |
| 588 |
actualSize_); |
| 589 |
|
| 590 |
if (wasVisible && |
| 591 |
(cursorRectChanged || position() != predictGeometry.topLeft() || |
| 592 |
!windowGeometry.contains(predictGeometry))) { |
| 593 |
hide(); |
| 594 |
show(); |
| 595 |
} |
| 596 |
#endif |
| 597 |
return; |
| 598 |
} |
| 599 |
#endif |
| 600 |
// Try to apply the screen edge detection over the window, because if we |
| 601 |
// intent to use this with wayland. It we have no information above |
| 602 |
// screen edge. |
| 603 |
if (isWayland_) { |
| 604 |
screenGeometry = window->frameGeometry(); |
| 605 |
cursorRect.translate(window->framePosition()); |
| 606 |
auto margins = window->frameMargins(); |
| 607 |
cursorRect.translate(margins.left(), margins.top()); |
| 608 |
} else { |
| 609 |
screenGeometry = window->screen()->geometry(); |
| 610 |
auto pos = window->mapToGlobal(cursorRect.topLeft()); |
| 611 |
cursorRect.moveTo(pos); |
| 612 |
} |
| 613 |
|
| 614 |
#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0) |
| 615 |
QSize sizeWithoutShadow = actualSize_.shrunkBy(theme_->shadowMargin()); |
| 616 |
#else |
| 617 |
QSize sizeWithoutShadow = |
| 618 |
actualSize_ - |
| 619 |
QSize(theme_->shadowMargin().left() + theme_->shadowMargin().right(), |
| 620 |
theme_->shadowMargin().top() + theme_->shadowMargin().bottom()); |
| 621 |
#endif |
| 622 |
if (sizeWithoutShadow.width() < 0) { |
| 623 |
sizeWithoutShadow.setWidth(0); |
| 624 |
} |
| 625 |
if (sizeWithoutShadow.height() < 0) { |
| 626 |
sizeWithoutShadow.setHeight(0); |
| 627 |
} |
| 628 |
int x = cursorRect.left(); |
| 629 |
int y = cursorRect.bottom(); |
| 630 |
if (cursorRect.left() + sizeWithoutShadow.width() > |
| 631 |
screenGeometry.right()) { |
| 632 |
x = screenGeometry.right() - sizeWithoutShadow.width() + 1; |
| 633 |
} |
| 634 |
|
| 635 |
if (x < screenGeometry.left()) { |
| 636 |
x = screenGeometry.left(); |
| 637 |
} |
| 638 |
|
| 639 |
if (y + sizeWithoutShadow.height() > screenGeometry.bottom()) { |
| 640 |
if (y > screenGeometry.bottom()) { |
| 641 |
y = screenGeometry.bottom() - sizeWithoutShadow.height() - 40; |
| 642 |
} else { /* better position the window */ |
| 643 |
y = y - sizeWithoutShadow.height() - |
| 644 |
((cursorRect.height() == 0) ? 40 : cursorRect.height()); |
| 645 |
} |
| 646 |
} |
| 647 |
|
| 648 |
if (y < screenGeometry.top()) { |
| 649 |
y = screenGeometry.top(); |
| 650 |
} |
| 651 |
|
| 652 |
QPoint newPosition(x, y); |
| 653 |
newPosition -= |
| 654 |
QPoint(theme_->shadowMargin().left(), theme_->shadowMargin().top()); |
| 655 |
if (newPosition != position()) { |
| 656 |
if (isWayland_ && isVisible()) { |
| 657 |
hide(); |
| 658 |
} |
| 659 |
setPosition(newPosition); |
| 660 |
} |
| 661 |
show(); |
| 662 |
} |
| 663 |
|
| 664 |
void FcitxCandidateWindow::mouseMoveEvent(QMouseEvent *event) { |
| 665 |
bool needRepaint = false; |
| 666 |
|
| 667 |
bool prevHovered = false; |
| 668 |
bool nextHovered = false; |
| 669 |
auto oldHighlight = highlight(); |
| 670 |
hoverIndex_ = -1; |
| 671 |
|
| 672 |
prevHovered = prevRegion_.contains(event->pos()); |
| 673 |
if (!prevHovered) { |
| 674 |
nextHovered = nextRegion_.contains(event->pos()); |
| 675 |
if (!nextHovered) { |
| 676 |
for (int idx = 0, e = candidateRegions_.size(); idx < e; idx++) { |
| 677 |
if (candidateRegions_[idx].contains(event->pos())) { |
| 678 |
hoverIndex_ = idx; |
| 679 |
break; |
| 680 |
} |
| 681 |
} |
| 682 |
} |
| 683 |
} |
| 684 |
|
| 685 |
needRepaint = needRepaint || prevHovered_ != prevHovered; |
| 686 |
prevHovered_ = prevHovered; |
| 687 |
|
| 688 |
needRepaint = needRepaint || nextHovered_ != nextHovered; |
| 689 |
nextHovered_ = nextHovered; |
| 690 |
|
| 691 |
needRepaint = needRepaint || oldHighlight != highlight(); |
| 692 |
if (needRepaint) { |
| 693 |
update(); |
| 694 |
} |
| 695 |
} |
| 696 |
|
| 697 |
void FcitxCandidateWindow::mouseReleaseEvent(QMouseEvent *event) { |
| 698 |
if (event->button() != Qt::LeftButton) { |
| 699 |
return; |
| 700 |
} |
| 701 |
|
| 702 |
if (prevRegion_.contains(event->pos())) { |
| 703 |
Q_EMIT prevClicked(); |
| 704 |
return; |
| 705 |
} |
| 706 |
|
| 707 |
if (nextRegion_.contains(event->pos())) { |
| 708 |
Q_EMIT nextClicked(); |
| 709 |
return; |
| 710 |
} |
| 711 |
|
| 712 |
for (int idx = 0, e = candidateRegions_.size(); idx < e; idx++) { |
| 713 |
if (candidateRegions_[idx].contains(event->pos())) { |
| 714 |
Q_EMIT candidateSelected(idx); |
| 715 |
return; |
| 716 |
} |
| 717 |
} |
| 718 |
} |
| 719 |
|
| 720 |
QSize FcitxCandidateWindow::sizeHint() { |
| 721 |
auto minH = |
| 722 |
theme_->fontMetrics().ascent() + theme_->fontMetrics().descent(); |
| 723 |
|
| 724 |
size_t width = 0; |
| 725 |
size_t height = 0; |
| 726 |
auto updateIfLarger = [](size_t &m, size_t n) { |
| 727 |
if (n > m) { |
| 728 |
m = n; |
| 729 |
} |
| 730 |
}; |
| 731 |
auto textMargin = theme_->textMargin(); |
| 732 |
auto extraW = textMargin.left() + textMargin.right(); |
| 733 |
auto extraH = textMargin.top() + textMargin.bottom(); |
| 734 |
if (!upperLayout_.text().isEmpty()) { |
| 735 |
auto size = upperLayout_.boundingRect(); |
| 736 |
height += minH + extraH; |
| 737 |
updateIfLarger(width, size.width() + extraW); |
| 738 |
} |
| 739 |
if (!lowerLayout_.text().isEmpty()) { |
| 740 |
auto size = lowerLayout_.boundingRect(); |
| 741 |
height += minH + extraH; |
| 742 |
updateIfLarger(width, size.width() + extraW); |
| 743 |
} |
| 744 |
|
| 745 |
bool vertical = theme_->vertical(); |
| 746 |
if (layoutHint_ == FcitxCandidateLayoutHint::Vertical) { |
| 747 |
vertical = true; |
| 748 |
} else if (layoutHint_ == FcitxCandidateLayoutHint::Horizontal) { |
| 749 |
vertical = false; |
| 750 |
} |
| 751 |
|
| 752 |
size_t wholeH = 0, wholeW = 0; |
| 753 |
for (size_t i = 0; i < labelLayouts_.size(); i++) { |
| 754 |
size_t candidateW = 0, candidateH = 0; |
| 755 |
if (!labelLayouts_[i]->isEmpty()) { |
| 756 |
auto size = labelLayouts_[i]->boundingRect(); |
| 757 |
candidateW += size.width(); |
| 758 |
updateIfLarger(candidateH, |
| 759 |
std::max(minH, qCeil(size.height())) + extraH); |
| 760 |
} |
| 761 |
if (!candidateLayouts_[i]->isEmpty()) { |
| 762 |
auto size = candidateLayouts_[i]->boundingRect(); |
| 763 |
candidateW += size.width(); |
| 764 |
updateIfLarger(candidateH, |
| 765 |
std::max(minH, qCeil(size.height())) + extraH); |
| 766 |
} |
| 767 |
candidateW += extraW; |
| 768 |
|
| 769 |
if (vertical) { |
| 770 |
wholeH += candidateH; |
| 771 |
updateIfLarger(wholeW, candidateW); |
| 772 |
} else { |
| 773 |
wholeW += candidateW; |
| 774 |
updateIfLarger(wholeH, candidateH); |
| 775 |
} |
| 776 |
} |
| 777 |
updateIfLarger(width, wholeW); |
| 778 |
candidatesHeight_ = wholeH; |
| 779 |
height += wholeH; |
| 780 |
|
| 781 |
auto contentMargin = theme_->contentMargin(); |
| 782 |
width += contentMargin.left() + contentMargin.right(); |
| 783 |
height += contentMargin.top() + contentMargin.bottom(); |
| 784 |
|
| 785 |
if (!labelLayouts_.empty() && (hasPrev_ || hasNext_)) { |
| 786 |
if (theme_->prev().valid() && theme_->next().valid()) { |
| 787 |
width += theme_->prev().width() + theme_->next().width(); |
| 788 |
} |
| 789 |
} |
| 790 |
|
| 791 |
return {static_cast<int>(width), static_cast<int>(height)}; |
| 792 |
} |
| 793 |
|
| 794 |
void FcitxCandidateWindow::wheelEvent(QWheelEvent *event) { |
| 795 |
if (!theme_ || !theme_->wheelForPaging()) { |
| 796 |
return; |
| 797 |
} |
| 798 |
accAngle_ += event->angleDelta().y(); |
| 799 |
auto angleForClick = 120; |
| 800 |
while (accAngle_ >= angleForClick) { |
| 801 |
accAngle_ -= angleForClick; |
| 802 |
Q_EMIT prevClicked(); |
| 803 |
} |
| 804 |
while (accAngle_ <= -angleForClick) { |
| 805 |
accAngle_ += angleForClick; |
| 806 |
Q_EMIT nextClicked(); |
| 807 |
} |
| 808 |
} |
| 809 |
|
| 810 |
void FcitxCandidateWindow::paintEvent(QPaintEvent *) { |
| 811 |
QPainter p(this); |
| 812 |
render(&p); |
| 813 |
} |
| 814 |
} // namespace fcitx |