Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qohosplatformtheme.cpp
Go to the documentation of this file.
1// Copyright (C) 2025 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "private/qohosplatformtheme_p.h"
11#include <QtCore/private/qcore_ohos_p.h>
12#include <QtCore/private/qohoscommon_p.h>
13#include <QtCore/private/qohoslogger_p.h>
14#include <QtGui/private/qguiapplication_p.h>
15#include <QtGui/private/qhighdpiscaling_p.h>
16#include <QtGui/private/qabstractfileiconengine_p.h>
17#include <initializer_list>
18#include <qohosjsmain.h>
19#include <qohosjsutils.h>
20#include <qohosqpaenums.h>
21#include <qohosutils.h>
22#include <qpa/qplatformfontdatabase.h>
23#include <qpa/qplatformintegration.h>
24#include <string>
25#include <tuple>
26
28
29namespace {
30
32{
33 auto disabledColor = color;
34 disabledColor.setAlphaF(qBound(0.0, color.alphaF() * 0.4, 1.0));
35
36 return disabledColor;
37}
38
39QColor makeOverlayCompositedOnSurface(const QColor &overlay, const QColor &surface)
40{
41 const auto alpha = overlay.alphaF();
42
43 return QColor::fromRgbF(
44 overlay.redF() * alpha + surface.redF() * (1.0 - alpha),
45 overlay.greenF() * alpha + surface.greenF() * (1.0 - alpha),
46 overlay.blueF() * alpha + surface.blueF() * (1.0 - alpha));
47}
48
74
75PalettesColors makePalettesColorsLight(const QColor &accentColor)
76{
77 const auto activeWindow = QColor("#FFFFFFFF");
78 const auto activeButtonText = QColor("#0A59F7");
79 const auto textPrimary = QColor("#FF182431");
80
81 return {
82 .activeWindow = activeWindow,
83 .inactiveWindow = makeInactiveOrDisabledFromColor(activeWindow),
84 .activeButtonText = activeButtonText,
85 .inactiveButtonText = makeInactiveOrDisabledFromColor(activeButtonText),
86 .activeHighlight = accentColor,
87 .inactiveHighlight = makeInactiveOrDisabledFromColor(accentColor),
88 .clickEffect = QColor("#1A000000"),
89 .hover = QColor("#0D000000"),
90 .foregroundContrary = QColor("#FFFFFF"),
91 .highlighted = accentColor,
92 .inactiveHighlighted = makeInactiveOrDisabledFromColor(accentColor),
93 .textPrimary = textPrimary,
94 .inactiveTextPrimary = textPrimary,
95 .disabledTextPrimary = makeInactiveOrDisabledFromColor(textPrimary),
96 .inactiveText = makeInactiveOrDisabledFromColor(textPrimary),
97 .componentNormal = QColor("#19182431"),
98 .componentActivated = QColor("#007DFF"),
99 .indicatorArrow = QColor("#E5182431"),
100 .scrollBarSlider = QColor("#182431"),
101 .switchBgOff = QColor("#33FFFFFF"),
102 .switchOutlineOff = QColor("#66182431"),
103 .window = QColor("#FFF1F3F5"),
104 };
105};
106
107PalettesColors makePalettesColorsDark(const QColor &accentColor)
108{
109 const auto activeWindow = QColor("#18181A");
110 const auto activeButtonText = QColor("#3F97E9");
111 const auto textPrimary = QColor("#DBFFFFFF");
112
113 return {
114 .activeWindow = activeWindow,
115 .inactiveWindow = makeInactiveOrDisabledFromColor(activeWindow),
116 .activeButtonText = activeButtonText,
117 .inactiveButtonText = makeInactiveOrDisabledFromColor(activeButtonText),
118 .activeHighlight = accentColor,
119 .inactiveHighlight = makeInactiveOrDisabledFromColor(accentColor),
120 .clickEffect = QColor("#26FFFFFF"),
121 .hover = QColor("#19FFFFFF"),
122 .foregroundContrary = QColor("#E5E5E5"),
123 .highlighted = accentColor,
124 .inactiveHighlighted = makeInactiveOrDisabledFromColor(accentColor),
125 .textPrimary = textPrimary,
126 .inactiveTextPrimary = textPrimary,
127 .disabledTextPrimary = makeInactiveOrDisabledFromColor(textPrimary),
128 .inactiveText = makeInactiveOrDisabledFromColor(textPrimary),
129 .componentNormal = QColor("#19FFFFFF"),
130 .componentActivated = QColor("#3F97E9"),
131 .indicatorArrow = QColor("#DBFFFFFF"),
132 .scrollBarSlider = QColor("#FFFFFF"),
133 .switchBgOff = QColor("#33000000"),
134 .switchOutlineOff = QColor("#66FFFFFF"),
135 .window = QColor("#FF000000"),
136 };
137}
138
146
148{
149 const auto activeButton = QColor("#0C182431");
150
151 return {
152 .activeButton = activeButton,
153 .inactiveButton = makeInactiveOrDisabledFromColor(activeButton),
154 .focusedOutline = accentColor,
155 .enabledDefaultButtonText = QColor("#FFFFFFFF"),
156 };
157};
158
160{
161 const auto activeButton = QColor("#19FFFFFF");
162
163 return {
164 .activeButton = activeButton,
165 .inactiveButton = makeInactiveOrDisabledFromColor(activeButton),
166 .focusedOutline = accentColor,
167 .enabledDefaultButtonText = QColor("#E5E5E5"),
168 };
169}
170
175
177{
178 return {
179 .windowButtonBackground = QColor("#FFD0CFD5"),
180 };
181};
182
184{
185 return {
186 .windowButtonBackground = QColor("#FFD0CFD5"),
187 };
188};
189
196
198{
199 return {
200 .activeWindowFrame = QColor("#FFE4E4E4"),
201 .textHint = QColor("#99182431"),
202 .textTertiary = QColor("#66182431"),
203 };
204};
205
207{
208 return {
209 .activeWindowFrame = QColor("#FF000000"),
210 .textHint = QColor("#99FFFFFF"),
211 .textTertiary = QColor("#66FFFFFF"),
212 };
213}
214
220
222{
223 return {
224 .listSeparator = QColor("#1A000000"),
225 .inactiveText = makeInactiveOrDisabledFromColor(palettes.textPrimary),
226 };
227};
228
230{
231 return {
232 .listSeparator = QColor("#33FFFFFF"),
233 .inactiveText = makeInactiveOrDisabledFromColor(palettes.textPrimary),
234 };
235}
236
243
245{
246 const auto inactiveWindowText = QColor("#99182431");
247
248 return {
249 .activeWindowText = accentColor,
250 .inactiveWindowText = inactiveWindowText,
251 .disabledWindowText = makeInactiveOrDisabledFromColor(inactiveWindowText),
252 };
253};
254
256{
257 const auto inactiveWindowText = QColor("#99FFFFFF");
258
259 return {
260 .activeWindowText = accentColor,
261 .inactiveWindowText = inactiveWindowText,
262 .disabledWindowText = makeInactiveOrDisabledFromColor(inactiveWindowText),
263 };
264}
265
271
273{
274 const auto textEditBackground = QColor("#0C182431");
275 return {
276 .textEditBackground = textEditBackground,
277 .inactiveTextEditBackground = makeInactiveOrDisabledFromColor(textEditBackground),
278 };
279};
280
282{
283 const auto textEditBackground = QColor("#19FFFFFF");
284 return {
285 .textEditBackground = textEditBackground,
286 .inactiveTextEditBackground = makeInactiveOrDisabledFromColor(textEditBackground),
287 };
288}
289
294
296{
297 return {
298 .buttonBackground = QColor("#0C000000"),
299 };
300};
301
303{
304 return {
305 .buttonBackground = QColor("#33FFFFFF"),
306 };
307}
308
315
317{
318 return {
319 .background = QColor("#00000000"),
320 .groupBoxFrame = QColor("#66000000"),
321 .shadowXS = QColor("#19000000"),
322 };
323};
324
326{
327 return {
328 .background = QColor("#00000000"),
329 .groupBoxFrame = QColor("#66FFFFFF"),
330 .shadowXS = QColor("#19FFFFFF"),
331 };
332}
333
338
340{
341 return {
342 .activeButton = QColor("#FFFFFFFF"),
343 };
344};
345
347{
348 return {
349 .activeButton = QColor("#FF18181A"),
350 };
351}
352
358
360{
361 return {
362 .toolTipBorder = QColor("#1A000000"),
363 .toolTipBase = QColor("#FFFFFFFF"),
364 };
365};
366
368{
369 return {
370 .toolTipBorder = QColor("#1AFFFFFF"),
371 .toolTipBase = QColor("#404040"),
372 };
373}
374
389
391{
392 const auto palettes = makePalettesColorsLight(accentColor);
393
394 return {
395 .palettes = palettes,
396 .button = makeButtonPaletteColorsLight(accentColor),
397 .toolButton = makeToolButtonPaletteColorsLight(),
398 .system = makeSystemButtonPaletteColorsLight(),
399 .menu = makeMenuPaletteColorsLight(palettes),
400 .tabBar = makeTabBarPaletteColorsLight(accentColor),
401 .textLineEdit = makeTextLineEditPaletteColorsLight(),
402 .comboBox = makeComboBoxPaletteColorsLight(),
403 .groupBox = makeGroupboxPaletteColorsLight(),
404 .header = makeHeaderPaletteColorsLight(),
405 .toolTip = makeTooltipPaletteColorsLight(),
406 };
407}
408
410{
411 const auto palettes = makePalettesColorsDark(accentColor);
412
413 return {
414 .palettes = palettes,
415 .button = makeButtonPaletteColorsDark(accentColor),
416 .toolButton = makeToolButtonPaletteColorsDark(),
417 .system = makeSystemButtonPaletteColorsDark(),
418 .menu = makeMenuPaletteColorsDark(palettes),
419 .tabBar = makeTabBarPaletteColorsDark(accentColor),
420 .textLineEdit = makeTextLineEditPaletteColorsDark(),
421 .comboBox = makeComboBoxPaletteColorsDark(),
422 .groupBox = makeGroupboxPaletteColorsDark(),
423 .header = makeHeaderPaletteColorsDark(),
424 .toolTip = makeTooltipPaletteColorsDark(),
425 };
426}
427
428AllPalletesColors makeAllPalettesColors(Qt::ColorScheme scheme, const QColor &accentColor)
429{
430 switch (scheme) {
431 case Qt::ColorScheme::Light: return makeAllPalettesColorsLight(accentColor);
432 case Qt::ColorScheme::Dark: return makeAllPalettesColorsDark(accentColor);
433 default:
434 break;
435 }
436
437 qOhosReportFatalErrorAndAbort(
438 "%s: got unsupported ColorScheme: %d", Q_FUNC_INFO, static_cast<int>(scheme));
439}
440
441constexpr int mouseDoubleClickDistance = 60;
442constexpr int mouseDoubleClickInterval = 300;
443
445 const QPalette &basePalette, std::initializer_list<std::tuple<QPalette::ColorGroup, QPalette::ColorRole, const QColor &>> brushEntries)
446{
447 auto palette = basePalette;
448
449 for (const auto &brushEntry : brushEntries) {
450 palette.setBrush(
451 std::get<QPalette::ColorGroup>(brushEntry),
452 std::get<QPalette::ColorRole>(brushEntry),
453 std::get<const QColor &>(brushEntry));
454 }
455
456 return palette;
457}
458
460{
461 const auto &colors = palettesColors.palettes;
462 const auto button = makeOverlayCompositedOnSurface(
463 palettesColors.button.activeButton, colors.activeWindow);
464
465 QPalette palette(button, colors.activeWindow);
466
467 const auto textVersions = {
468 std::make_pair(QPalette::Active, colors.textPrimary),
469 std::make_pair(QPalette::Inactive, colors.inactiveTextPrimary),
470 std::make_pair(QPalette::Disabled, colors.disabledTextPrimary)};
471
472 for (const auto &groupAndText : textVersions) {
473 palette.setColor(groupAndText.first, QPalette::WindowText, groupAndText.second);
474 palette.setColor(groupAndText.first, QPalette::Text, groupAndText.second);
475 palette.setColor(groupAndText.first, QPalette::ButtonText, groupAndText.second);
476 }
477 palette.setColor(QPalette::BrightText, colors.foregroundContrary);
478
479 return palette;
480}
481
483{
484 return makePalette(
485 makeBasePalette(palettesColors),
486 {
487 {QPalette::Active, QPalette::AlternateBase, palettesColors.palettes.componentActivated},
488
489 {QPalette::Active, QPalette::Button, palettesColors.button.activeButton},
490 {QPalette::Disabled, QPalette::Button, palettesColors.button.inactiveButton},
491 {QPalette::Inactive, QPalette::Button, palettesColors.button.inactiveButton},
492
493 {QPalette::Active, QPalette::ButtonText, palettesColors.palettes.activeButtonText},
494 {QPalette::Disabled, QPalette::ButtonText, palettesColors.palettes.inactiveButtonText},
495 {QPalette::Inactive, QPalette::ButtonText, palettesColors.palettes.inactiveButtonText},
496
497 {QPalette::All, QPalette::BrightText, palettesColors.button.enabledDefaultButtonText},
498
499 {QPalette::Active, QPalette::Light, palettesColors.palettes.hover},
500 {QPalette::Active, QPalette::Dark, palettesColors.palettes.clickEffect},
501
502 {QPalette::Active, QPalette::Highlight, palettesColors.button.focusedOutline},
503 {QPalette::Active, QPalette::WindowText, palettesColors.palettes.indicatorArrow}
504 });
505}
506
508{
509 return makePalette(
510 makeButtonPalette(palettesColors),
511 {
512 {QPalette::Active, QPalette::AlternateBase, palettesColors.toolButton.windowButtonBackground},
513 });
514}
515
517{
518 return makePalette(
519 makeBasePalette(palettesColors),
520 {
521 {QPalette::Active, QPalette::Window, palettesColors.palettes.activeWindow},
522 {QPalette::Disabled, QPalette::Window, palettesColors.palettes.inactiveWindow},
523 {QPalette::Inactive, QPalette::Window, palettesColors.palettes.inactiveWindow},
524
525 {QPalette::Active, QPalette::WindowText, palettesColors.palettes.textPrimary},
526 {QPalette::Disabled, QPalette::WindowText, palettesColors.palettes.disabledTextPrimary},
527 {QPalette::Inactive, QPalette::WindowText, palettesColors.palettes.inactiveTextPrimary},
528
529 {QPalette::Active, QPalette::Text, palettesColors.palettes.textPrimary},
530 {QPalette::Disabled, QPalette::Text, palettesColors.palettes.disabledTextPrimary},
531 {QPalette::Inactive, QPalette::Text, palettesColors.palettes.inactiveTextPrimary},
532
533 {QPalette::Active, QPalette::Base, palettesColors.palettes.window},
534 {QPalette::Disabled, QPalette::Base, palettesColors.palettes.window},
535 {QPalette::Inactive, QPalette::Base, palettesColors.palettes.window},
536
537 {QPalette::Active, QPalette::AlternateBase, palettesColors.palettes.textPrimary},
538
539 {QPalette::Active, QPalette::Highlight, palettesColors.palettes.highlighted},
540
541 {QPalette::All, QPalette::PlaceholderText, palettesColors.system.textHint},
542 {QPalette::Disabled, QPalette::PlaceholderText, palettesColors.system.textTertiary},
543
544 {QPalette::Active, QPalette::Midlight, palettesColors.palettes.scrollBarSlider},
545
546 {QPalette::Active, QPalette::Mid, palettesColors.palettes.componentNormal}
547 });
548}
549
551{
552 return makePalette(
553 makeBasePalette(palettesColors),
554 {
555 {QPalette::Inactive, QPalette::Base, palettesColors.palettes.switchBgOff},
556 {QPalette::Inactive, QPalette::Button, palettesColors.palettes.switchOutlineOff},
557 {QPalette::Active, QPalette::Button, palettesColors.palettes.highlighted},
558 {QPalette::Active, QPalette::Highlight, palettesColors.palettes.highlighted},
559 {QPalette::Active, QPalette::WindowText, palettesColors.palettes.highlighted},
560 {QPalette::Inactive, QPalette::WindowText, palettesColors.palettes.inactiveText},
561 {QPalette::Disabled, QPalette::WindowText, palettesColors.palettes.inactiveHighlighted},
562 {QPalette::Active, QPalette::Light, palettesColors.palettes.hover},
563 {QPalette::Active, QPalette::Dark, palettesColors.palettes.clickEffect},
564 });
565}
566
568{
569 return makePalette(
570 makeBasePalette(palettesColors),
571 {
572 {QPalette::Active, QPalette::AlternateBase, palettesColors.palettes.componentActivated},
573 {QPalette::All, QPalette::Base, palettesColors.palettes.window},
574 {QPalette::All, QPalette::Window, palettesColors.palettes.window},
575 {QPalette::Active, QPalette::Text, palettesColors.palettes.highlighted},
576 {QPalette::Disabled, QPalette::Text, palettesColors.palettes.inactiveText},
577 {QPalette::Inactive, QPalette::Text, palettesColors.palettes.inactiveHighlighted},
578 {QPalette::Active, QPalette::Light, palettesColors.palettes.hover},
579 {QPalette::Active, QPalette::Dark, palettesColors.palettes.clickEffect},
580 {QPalette::Active, QPalette::WindowText, palettesColors.palettes.indicatorArrow},
581 {QPalette::All, QPalette::Button, palettesColors.comboBox.buttonBackground}
582 });
583}
584
586{
587 return makePalette(
588 makeBasePalette(palettesColors),
589 {
590 {QPalette::Active, QPalette::Window, palettesColors.palettes.activeWindow},
591 {QPalette::Inactive, QPalette::Base, palettesColors.palettes.switchBgOff},
592 {QPalette::Inactive, QPalette::Button, palettesColors.palettes.switchOutlineOff},
593 {QPalette::Active, QPalette::Button, palettesColors.palettes.highlighted},
594
595 {QPalette::Active, QPalette::Text, palettesColors.palettes.textPrimary},
596 {QPalette::Disabled, QPalette::Text, palettesColors.menu.inactiveText},
597 {QPalette::Inactive, QPalette::Text, palettesColors.menu.inactiveText},
598
599 {QPalette::Active, QPalette::Light, palettesColors.palettes.hover},
600 {QPalette::Active, QPalette::Dark, palettesColors.palettes.clickEffect},
601
602 {QPalette::Active, QPalette::Mid, palettesColors.menu.listSeparator}
603 });
604}
605
607{
608 return makePalette(
609 makeBasePalette(palettesColors),
610 {
611 {QPalette::Active, QPalette::ButtonText, palettesColors.palettes.textPrimary},
612 {QPalette::Disabled, QPalette::ButtonText, palettesColors.menu.inactiveText},
613 {QPalette::Inactive, QPalette::ButtonText, palettesColors.menu.inactiveText},
614 });
615}
616
618{
619 return makePalette(
620 makeBasePalette(palettesColors),
621 {
622 {QPalette::Active, QPalette::WindowText, palettesColors.tabBar.activeWindowText},
623 {QPalette::Inactive, QPalette::WindowText, palettesColors.tabBar.inactiveWindowText},
624 {QPalette::Disabled, QPalette::WindowText, palettesColors.tabBar.disabledWindowText},
625
626 {QPalette::Active, QPalette::Window, palettesColors.palettes.activeWindow},
627 {QPalette::Inactive, QPalette::Window, palettesColors.palettes.inactiveWindow},
628 {QPalette::Disabled, QPalette::Window, palettesColors.palettes.inactiveWindow},
629
630 {QPalette::Active, QPalette::Highlight, palettesColors.palettes.activeHighlight},
631 {QPalette::Inactive, QPalette::Highlight, palettesColors.palettes.inactiveHighlight},
632 {QPalette::Disabled, QPalette::Highlight, palettesColors.palettes.inactiveHighlight},
633 });
634}
635
637{
638 return makePalette(
639 makeSystemPalette(palettesColors),
640 {
641 {QPalette::Active, QPalette::AlternateBase, palettesColors.palettes.componentActivated},
642 {QPalette::All, QPalette::Base, palettesColors.textLineEdit.textEditBackground},
643 {QPalette::Disabled, QPalette::Base, palettesColors.textLineEdit.inactiveTextEditBackground},
644 {QPalette::All, QPalette::Text, palettesColors.palettes.textPrimary},
645 {QPalette::Disabled, QPalette::Text, palettesColors.palettes.disabledTextPrimary},
646 });
647}
648
650{
651 return makePalette(
652 makeTextLineEditPalette(palettesColors),
653 {
654 {QPalette::All, QPalette::Base, palettesColors.palettes.activeWindow},
655 });
656}
657
659{
660 return makePalette(
661 makeBasePalette(palettesColors),
662 {
663 {QPalette::Active, QPalette::Button, palettesColors.palettes.highlighted},
664 {QPalette::Inactive, QPalette::Button, palettesColors.palettes.switchOutlineOff},
665 {QPalette::Active, QPalette::Base, palettesColors.system.activeWindowFrame},
666 {QPalette::Inactive, QPalette::Base, palettesColors.palettes.switchBgOff},
667 {QPalette::Active, QPalette::AlternateBase, palettesColors.groupBox.groupBoxFrame},
668 {QPalette::Active, QPalette::Window, palettesColors.groupBox.background},
669 {QPalette::Inactive, QPalette::Window, palettesColors.groupBox.background},
670 {QPalette::Active, QPalette::Window, palettesColors.palettes.window},
671 {QPalette::Disabled, QPalette::Window, palettesColors.palettes.window},
672 {QPalette::Active, QPalette::Text, palettesColors.palettes.highlighted},
673 {QPalette::Inactive, QPalette::Text, palettesColors.palettes.highlighted},
674 {QPalette::Disabled, QPalette::Text, palettesColors.palettes.inactiveHighlighted},
675 {QPalette::Current, QPalette::Text, palettesColors.palettes.highlighted},
676 {QPalette::Disabled, QPalette::WindowText, palettesColors.palettes.inactiveHighlighted},
677 {QPalette::Active, QPalette::ButtonText, palettesColors.palettes.textPrimary},
678 {QPalette::Disabled, QPalette::ButtonText, palettesColors.palettes.inactiveHighlighted},
679 {QPalette::Active, QPalette::Dark, palettesColors.palettes.clickEffect},
680 {QPalette::Active, QPalette::Light, palettesColors.palettes.hover},
681 {QPalette::Active, QPalette::Shadow, palettesColors.groupBox.shadowXS},
682 {QPalette::Active, QPalette::Midlight, palettesColors.palettes.scrollBarSlider},
683 {QPalette::Active, QPalette::Mid, palettesColors.palettes.componentNormal}
684 });
685}
686
688{
689 return makePalette(
690 makeBasePalette(palettesColors),
691 {
692 {QPalette::Active, QPalette::Button, palettesColors.header.activeButton},
693 {QPalette::Inactive, QPalette::Button, palettesColors.header.activeButton},
694 {QPalette::Disabled, QPalette::Button, palettesColors.palettes.inactiveWindow},
695
696 {QPalette::Current, QPalette::ButtonText, palettesColors.palettes.activeButtonText},
697 {QPalette::Active, QPalette::ButtonText, palettesColors.palettes.activeButtonText},
698 {QPalette::Inactive, QPalette::ButtonText, palettesColors.palettes.activeButtonText},
699 {QPalette::Disabled, QPalette::ButtonText, palettesColors.palettes.inactiveButtonText},
700 });
701}
702
704{
705 return makePalette(
706 makeSystemPalette(palettesColors),
707 {
708 {QPalette::Active, QPalette::Base, palettesColors.palettes.window},
709 {QPalette::Inactive, QPalette::Base, palettesColors.palettes.window},
710 {QPalette::Disabled, QPalette::Base, palettesColors.palettes.window},
711
712 {QPalette::Active, QPalette::AlternateBase, palettesColors.palettes.activeWindow},
713 {QPalette::Inactive, QPalette::AlternateBase, palettesColors.palettes.activeWindow},
714 {QPalette::Disabled, QPalette::AlternateBase, palettesColors.palettes.inactiveWindow},
715 {QPalette::Current, QPalette::AlternateBase, palettesColors.palettes.activeWindow},
716
717 {QPalette::Active, QPalette::Highlight, palettesColors.palettes.activeHighlight},
718 {QPalette::Inactive, QPalette::Highlight, palettesColors.palettes.activeHighlight},
719 {QPalette::Disabled, QPalette::Highlight, palettesColors.palettes.inactiveHighlight},
720
721 {QPalette::Active, QPalette::Window, palettesColors.palettes.activeWindow},
722 {QPalette::Inactive, QPalette::Window, palettesColors.palettes.activeWindow},
723 {QPalette::Disabled, QPalette::Window, palettesColors.palettes.inactiveWindow},
724
725 {QPalette::Active, QPalette::Text, palettesColors.palettes.activeHighlight},
726 {QPalette::Inactive, QPalette::Text, palettesColors.palettes.activeHighlight},
727 {QPalette::Disabled, QPalette::Text, palettesColors.palettes.inactiveHighlight},
728
729 {QPalette::Active, QPalette::HighlightedText, palettesColors.palettes.activeWindow},
730 {QPalette::Inactive, QPalette::HighlightedText, palettesColors.palettes.activeWindow},
731 {QPalette::Disabled, QPalette::HighlightedText, palettesColors.palettes.inactiveWindow},
732
733 {QPalette::Active, QPalette::Dark, palettesColors.palettes.activeWindow},
734 {QPalette::Inactive, QPalette::Dark, palettesColors.palettes.activeWindow},
735 {QPalette::Disabled, QPalette::Dark, palettesColors.palettes.inactiveWindow},
736 {QPalette::Current, QPalette::Dark, palettesColors.palettes.activeWindow},
737 {QPalette::All, QPalette::Dark, palettesColors.palettes.activeWindow},
738
739 {QPalette::Active, QPalette::Light, palettesColors.palettes.hover},
740 {QPalette::Inactive, QPalette::Light, palettesColors.palettes.activeWindow},
741 {QPalette::Disabled, QPalette::Light, palettesColors.palettes.inactiveWindow},
742
743 {QPalette::Active, QPalette::Mid, palettesColors.palettes.activeWindow},
744 {QPalette::Inactive, QPalette::Mid, palettesColors.palettes.activeWindow},
745 {QPalette::Disabled, QPalette::Mid, palettesColors.palettes.inactiveWindow},
746 {QPalette::Current, QPalette::Mid, palettesColors.palettes.activeWindow},
747 {QPalette::All, QPalette::Mid, palettesColors.palettes.activeWindow},
748 });
749}
750
752{
753 return makePalette(
754 makeBasePalette(palettesColors),
755 {
756 {QPalette::Active, QPalette::WindowText, palettesColors.palettes.textPrimary},
757 {QPalette::Disabled, QPalette::WindowText, palettesColors.palettes.disabledTextPrimary},
758 {QPalette::Inactive, QPalette::WindowText, palettesColors.palettes.inactiveTextPrimary},
759
760 {QPalette::Active, QPalette::Text, palettesColors.palettes.textPrimary},
761 {QPalette::Disabled, QPalette::Text, palettesColors.palettes.disabledTextPrimary},
762 {QPalette::Inactive, QPalette::Text, palettesColors.palettes.inactiveTextPrimary},
763 });
764}
765
767{
768 return makePalette(
769 makeBasePalette(palettesColors),
770 {
771 {QPalette::All, QPalette::Base, palettesColors.toolTip.toolTipBorder},
772 {QPalette::All, QPalette::ToolTipBase, palettesColors.toolTip.toolTipBase},
773 {QPalette::All, QPalette::ToolTipText, palettesColors.palettes.highlighted},
774 });
775}
776
778{
779 auto font = QGuiApplicationPrivate::platformIntegration()->fontDatabase()->defaultFont();
780 font.setPointSize(12);
781 font.setBold(false);
782
783 return font;
784}
785
787{
788 auto font = QGuiApplicationPrivate::platformIntegration()->fontDatabase()->defaultFont();
789 font.setPointSize(20);
790 font.setBold(true);
791
792 return font;
793}
794
796{
797 auto font = QGuiApplicationPrivate::platformIntegration()->fontDatabase()->defaultFont();
798 font.setPointSize(16);
799
800 return font;
801}
802
804{
805 auto font = QGuiApplicationPrivate::platformIntegration()->fontDatabase()->defaultFont();
806 font.setPointSize(16);
807
808 return font;
809}
810
812{
813 auto font = QGuiApplicationPrivate::platformIntegration()->fontDatabase()->defaultFont();
814 font.setPointSize(16);
815
816 return font;
817}
818
820{
821 auto font = QGuiApplicationPrivate::platformIntegration()->fontDatabase()->defaultFont();
822 font.setPointSize(14);
823
824 return font;
825}
826
827QHash<QPlatformTheme::Palette, QPalette> makePalettesMap(Qt::ColorScheme scheme, const QColor &accentColor)
828{
829 const AllPalletesColors palettesColors = makeAllPalettesColors(scheme, accentColor);
830 return {
831 {QPlatformTheme::ButtonPalette, makeButtonPalette(palettesColors)},
832 {QPlatformTheme::CheckBoxPalette, makeCheckBoxOrRadioButtonPalette(palettesColors)},
833 {QPlatformTheme::ComboBoxPalette, makeComboBoxPalette(palettesColors)},
834 {QPlatformTheme::GroupBoxPalette, makeGroupBoxPalette(palettesColors)},
835 {QPlatformTheme::HeaderPalette, makeHeaderPalette(palettesColors)},
836 {QPlatformTheme::ItemViewPalette, makeItemViewPalette(palettesColors)},
837 {QPlatformTheme::LabelPalette, makeLabelPalette(palettesColors)},
838 {QPlatformTheme::MenuPalette, makeMenuPalette(palettesColors)},
839 {QPlatformTheme::MenuBarPalette, makeMenuBarPalette(palettesColors)},
840 {QPlatformTheme::RadioButtonPalette, makeCheckBoxOrRadioButtonPalette(palettesColors)},
841 {QPlatformTheme::SystemPalette, makeSystemPalette(palettesColors)},
842 {QPlatformTheme::TabBarPalette, makeTabBarPalette(palettesColors)},
843 {QPlatformTheme::TextEditPalette, makeTextEditPalette(palettesColors)},
844 {QPlatformTheme::TextLineEditPalette, makeTextLineEditPalette(palettesColors)},
845 {QPlatformTheme::ToolButtonPalette, makeToolButtonPalette(palettesColors)},
846 {QPlatformTheme::ToolTipPalette, makeToolTipPalette(palettesColors)},
847 };
848}
849
851 Qt::ColorScheme scheme)
852{
853 switch (scheme) {
854 case Qt::ColorScheme::Unknown:
855 return std::nullopt;
856 case Qt::ColorScheme::Light:
857 return false;
858 case Qt::ColorScheme::Dark:
859 return true;
860 };
861 return std::nullopt;
862}
863
865 std::optional<bool> darkModeFlag)
866{
867 if (darkModeFlag.has_value()) {
868 if (!darkModeFlag.value_or(false))
869 return Qt::ColorScheme::Light;
870 else
871 return Qt::ColorScheme::Dark;
872 } else {
873 return Qt::ColorScheme::Unknown;
874 }
875}
876
878
879OhosConfigurationColorMode mapOhosConfigurationColorModeFromJs(QOhosJsState &jsState, QNapi::Number colorModeJsEnum)
880{
881 constexpr auto fallbackColorMode = OhosConfigurationColorMode::COLOR_MODE_NOT_SET;
882 auto optColorMode = jsState.tryMapOhosEnumFromJs<OhosConfigurationColorMode>(colorModeJsEnum);
883 return optColorMode.value_or(fallbackColorMode);
884};
885
887{
889 qCWarning(QtForOhos, "%s: cannot set a color mode in 'no UI child mode'", Q_FUNC_INFO);
890 return;
891 }
892
893 QOhosJsThreadGateway::runAndWait(
894 [&](QOhosJsState &jsState) {
895 auto optQAbility = jsState.defaultQAbility();
896 if (!optQAbility) {
897 qCWarning(QtForOhos, "%s: cannot set a color mode without a UIAbility", Q_FUNC_INFO);
898 return;
899 }
900
901 const auto jsColorMode = jsState.mapOhosEnumToJs(colorMode);
902 optQAbility->eval("context.getApplicationContext().setColorMode(*)", {jsColorMode});
903 },
904 Q_FUNC_INFO);
905}
906
908 QOhosConsumer<OhosConfigurationColorMode> valueChangedHandler)
909{
910 return QtOhos::makeOhosConfigValueDataSource<OhosConfigurationColorMode>(
911 [](QOhosJsState &jsState) {
912 auto optQAbility = jsState.defaultQAbility();
913 return optQAbility
914 ? mapOhosConfigurationColorModeFromJs(
915 jsState, optQAbility->eval<QNapi::Number>("context.config.colorMode"))
916 : OhosConfigurationColorMode::COLOR_MODE_NOT_SET;
917 },
918 [](QOhosJsState &jsState, const QNapi::Object &config) {
919 return mapOhosConfigurationColorModeFromJs(jsState, config.get<QNapi::Number>("colorMode"));
920 },
921 std::move(valueChangedHandler));
922}
923
925{
926 switch (colorMode) {
928 return std::nullopt;
930 return false;
932 return true;
933 }
934
935 return std::nullopt;
936}
937
938QColor readAccentColor(QOhosJsState &jsState)
939{
940 auto optQAbility = jsState.defaultQAbility();
941 if (!optQAbility)
942 return {};
943
944 const auto argb = static_cast<uint32_t>(
945 optQAbility->eval<QNapi::Number>(
946 "context.resourceManager.getColorByNameSync(*)",
947 {"ohos_id_color_emphasize"}));
948 return QColor::fromRgba(argb);
949}
950
951QOhosSupplier<QColor> makeOhosAccentColorDataSource(QOhosConsumer<QColor> accentColorChangedHandler)
952{
953 return QtOhos::makeOhosConfigValueDataSource<QColor>(
954 readAccentColor,
955 [](QOhosJsState &jsState, const QNapi::Object &) {
956 return readAccentColor(jsState);
957 },
958 std::move(accentColorChangedHandler));
959}
960
961std::optional<QPixmap> tryGetFilePixmapByResourceObject(QOhosJsState &jsState, QNapi::Object resource)
962{
963 std::string undocumentedParamsPropertyName("params");
964 auto optParams = QNapi::getOptionalPropOrEmpty<QNapi::Value>(resource, undocumentedParamsPropertyName);
965 if (optParams.IsEmpty()) {
966 qOhosPrintfWarning(
967 "%s: expected '%s' property not found in resource object, ignore getting pixmap",
968 Q_FUNC_INFO, undocumentedParamsPropertyName.c_str());
969 return std::nullopt;
970 }
971 if (!optParams.IsArray()) {
972 qOhosPrintfWarning(
973 "%s: '%s' property is not an array, ignore getting pixmap",
974 Q_FUNC_INFO, undocumentedParamsPropertyName.c_str());
975 return std::nullopt;
976 }
977
978 auto params = QNapi::getArrayElements<std::vector<std::string>, QNapi::String>(
979 QNapi::checkedCast<QNapi::Array>(optParams));
980 if (params.empty()) {
981 qOhosPrintfWarning(
982 "%s: '%s' property is empty, ignore getting pixmap",
983 Q_FUNC_INFO, undocumentedParamsPropertyName.c_str());
984 return std::nullopt;
985 }
986
987 const auto resourceIdentifier = params.front();
988 const auto lastSeparatorPosition = resourceIdentifier.rfind('.');
989 if (lastSeparatorPosition == std::string::npos) {
990 qOhosPrintfWarning(
991 "%s: '%s' property has unknown resource name, ignore getting pixmap",
992 Q_FUNC_INFO, undocumentedParamsPropertyName.c_str());
993 return std::nullopt;
994 }
995
996 const auto simpleName = resourceIdentifier.substr(lastSeparatorPosition + 1);
997 auto resourceContentBuffer = jsState.eval<QNapi::TypedArrayOf<std::uint8_t>>(
998 "@ohos.resourceManager.getSysResourceManager().getMediaByNameSync(*)", {simpleName});
999
1000 QPixmap pixmap;
1001 bool pixmapLoadResult = pixmap.loadFromData(
1002 static_cast<const uchar *>(resourceContentBuffer.Data()), resourceContentBuffer.ByteLength());
1003
1004 if (pixmapLoadResult) {
1005 return pixmap;
1006 } else {
1007 qOhosPrintfWarning("%s: cannot load pixmap", Q_FUNC_INFO);
1008 return std::nullopt;
1009 }
1010}
1011
1013 QOhosJsState &jsState, const std::string &filenameExtension)
1014{
1015 std::string ohosDotFilenameExtension = "." + filenameExtension;
1016 auto uniformDataType = jsState.eval<QNapi::String>(
1017 "@ohos.data.uniformTypeDescriptor.getUniformDataTypeByFilenameExtension(*)",
1018 {ohosDotFilenameExtension});
1019
1020 auto resourceValue = jsState.eval(
1021 "@kit.FileManagerServiceKit.fileManagerService.getFileIconSync(*)", {uniformDataType});
1022 if (resourceValue.IsObject()) {
1023 return tryGetFilePixmapByResourceObject(jsState, QNapi::checkedCast<QNapi::Object>(resourceValue));
1024 } else {
1025 qOhosPrintfWarning("%s: got unhandled resource result type, ignore it", Q_FUNC_INFO);
1026 return std::nullopt;
1027 }
1028}
1029
1031{
1032public:
1033 explicit QOhosFileIconEngine(const QFileInfo &info, QPlatformTheme::IconOptions opts);
1034
1035protected:
1036 QPixmap filePixmap(const QSize &size, QIcon::Mode mode, QIcon::State) override;
1037};
1038
1039QOhosFileIconEngine::QOhosFileIconEngine(const QFileInfo &info, QPlatformTheme::IconOptions opts)
1041{
1042}
1043
1045{
1046 auto fileNameSuffix = fileInfo().suffix();
1047 auto optPixmap = QOhosJsThreadGateway::eval(
1048 [&](QOhosJsState &jsState) {
1049 return tryGetFilePixmapByFilenameExtension(jsState, fileNameSuffix.toStdString());
1050 },
1051 Q_FUNC_INFO);
1052
1053 return optPixmap.has_value()
1054 ? optPixmap.value().scaled(size, Qt::KeepAspectRatio, Qt::SmoothTransformation)
1055 : QPixmap();
1056}
1057
1058}
1059
1061 : m_fonts(
1062 {
1071 })
1072 , m_wheelScrollLines(defaultWheelScrollLines)
1073{
1074 auto colorModeDataSource = makeOhosConfigColorModeDataSource(
1075 [this](auto) {
1076 refreshPalettes();
1077 QWindowSystemInterface::handleThemeChange<QWindowSystemInterface::SynchronousDelivery>();
1078 });
1079 m_ohosConfigDarkModeFlagSupplier = [colorModeDataSource = std::move(colorModeDataSource)]() {
1080 return mapOhosConfigurationColorModeToDarkModeFlag(colorModeDataSource());
1081 };
1082
1083 m_ohosAccentColorSupplier = makeOhosAccentColorDataSource(
1084 [this](const auto &) {
1085 refreshPalettes();
1086 QWindowSystemInterface::handleThemeChange<QWindowSystemInterface::SynchronousDelivery>();
1087 });
1088
1089 refreshPalettes();
1090}
1091
1092void QOhosPlatformTheme::requestColorScheme(Qt::ColorScheme scheme)
1093{
1094 std::optional<bool> isDarkMode = mapOhosThemeFromColorScheme(scheme);
1095 setOhosConfigColorMode(
1096 isDarkMode.has_value()
1097 ? isDarkMode.value()
1101}
1102
1104{
1105 return mapOhosThemeToColorScheme(m_ohosConfigDarkModeFlagSupplier());
1106}
1107
1108void QOhosPlatformTheme::refreshPalettes()
1109{
1110 if (const auto currentColorScheme = colorScheme(); currentColorScheme != Qt::ColorScheme::Unknown)
1111 m_themePalette = makePalettesMap(currentColorScheme, m_ohosAccentColorSupplier());
1112}
1113
1115{
1116 auto _dbg = make_QCScopedDebug("QOhosPlatformTheme::createPlatformDialogHelper");
1117 return QOhosDialogs::createHelper(type);
1118}
1119
1120bool QOhosPlatformTheme::usePlatformNativeDialog(QPlatformTheme::DialogType type) const
1121{
1122 auto _dbg = make_QCScopedDebug("QOhosPlatformTheme::usePlatformNativeDialog");
1123 return (type == QOhosPlatformTheme::FileDialog);
1124}
1125
1127{
1128 switch (hint) {
1129 case StyleNames:
1130 return QStringList(QString::fromUtf8(ohosThemeName));
1131 case MouseDoubleClickDistance:
1132 case TouchDoubleTapDistance:
1133 {
1134 auto *primaryScreen = qGuiApp->primaryScreen();
1135 auto *platformScreen = primaryScreen != nullptr
1136 ? static_cast<QOhosPlatformScreen *>(primaryScreen->handle())
1137 : nullptr;
1138 return platformScreen != nullptr
1139 ? QHighDpi::fromNativePixels(
1140 QHighDpi::toNative(
1141 mouseDoubleClickDistance, platformScreen->pixelScalingCoefficient()),
1142 platformScreen)
1143 : mouseDoubleClickDistance;
1144 }
1145 case MouseDoubleClickInterval:
1147 case WheelScrollLines:
1148 return m_wheelScrollLines;
1149 case MenuBarFocusOnAltPressRelease:
1150 return true;
1151 case PreferFileIconFromTheme:
1152 return true;
1153 default:
1154 return QPlatformTheme::themeHint(hint);
1155 }
1156}
1157
1158const QPalette *QOhosPlatformTheme::palette(Palette type) const
1159{
1160 if (const auto it = m_themePalette.find(type); it != m_themePalette.end())
1161 return &it.value();
1162 return QPlatformTheme::palette(type);
1163}
1164
1165const QFont *QOhosPlatformTheme::font(Font type) const
1166{
1167 const auto fontIter = m_fonts.find(type);
1168 return fontIter != m_fonts.end()
1169 ? &fontIter.value()
1170 : QPlatformTheme::font(type);
1171}
1172
1173void QOhosPlatformTheme::setWheelScrollLines(int wheelScrollLines)
1174{
1175 m_wheelScrollLines = wheelScrollLines;
1176}
1177
1179{
1180 return makeQOhosSystemTrayIcon().release();
1181}
1182
1183QIcon QOhosPlatformTheme::fileIcon(const QFileInfo &fileInfo, QPlatformTheme::IconOptions iconOptions) const
1184{
1185 return !fileInfo.suffix().isEmpty()
1186 ? QIcon(new QOhosFileIconEngine(fileInfo, iconOptions))
1187 : QPlatformTheme::fileIcon(fileInfo, iconOptions);
1188}
1189
1190QIconEngine *QOhosPlatformTheme::createIconEngine(const QString &iconName) const
1191{
1192 return tryCreateQOhosIconEngine(iconName);
1193}
1194
1195QT_END_NAMESPACE
QIconEngine * createIconEngine(const QString &iconName) const override
Factory function for the QIconEngine used by QIcon::fromTheme().
void requestColorScheme(Qt::ColorScheme scheme) override
QIcon fileIcon(const QFileInfo &fileInfo, QPlatformTheme::IconOptions iconOptions) const override
Return an icon for fileInfo, observing iconOptions.
Qt::ColorScheme colorScheme() const override
void setWheelScrollLines(int wheelScrollLines)
QPlatformDialogHelper * createPlatformDialogHelper(DialogType type) const override
const QFont * font(Font type) const override
const QPalette * palette(Palette type) const override
Return a color palette for type type.
QPlatformSystemTrayIcon * createPlatformSystemTrayIcon() const override
Factory function for QSystemTrayIcon.
static constexpr int defaultWheelScrollLines
QVariant themeHint(ThemeHint hint) const override
QPixmap filePixmap(const QSize &size, QIcon::Mode mode, QIcon::State) override
QOhosFileIconEngine(const QFileInfo &info, QPlatformTheme::IconOptions opts)
Combined button and popup list for selecting options.
AllPalletesColors makeAllPalettesColorsLight(const QColor &accentColor)
SystemPaletteColors makeSystemButtonPaletteColorsDark()
QPalette makeLabelPalette(const AllPalletesColors &palettesColors)
QPalette makeCheckBoxOrRadioButtonPalette(const AllPalletesColors &palettesColors)
ComboBoxPaletteColors makeComboBoxPaletteColorsLight()
ComboBoxPaletteColors makeComboBoxPaletteColorsDark()
QPalette makeSystemPalette(const AllPalletesColors &palettesColors)
SystemPaletteColors makeSystemButtonPaletteColorsLight()
QColor makeInactiveOrDisabledFromColor(const QColor &color)
QHash< QPlatformTheme::Palette, QPalette > makePalettesMap(Qt::ColorScheme scheme, const QColor &accentColor)
QPalette makeTextEditPalette(const AllPalletesColors &palettesColors)
QColor readAccentColor(QOhosJsState &jsState)
TooltipPaletteColors makeTooltipPaletteColorsDark()
QPalette makePalette(const QPalette &basePalette, std::initializer_list< std::tuple< QPalette::ColorGroup, QPalette::ColorRole, const QColor & > > brushEntries)
std::optional< QPixmap > tryGetFilePixmapByResourceObject(QOhosJsState &jsState, QNapi::Object resource)
PalettesColors makePalettesColorsLight(const QColor &accentColor)
AllPalletesColors makeAllPalettesColorsDark(const QColor &accentColor)
QOhosSupplier< OhosConfigurationColorMode > makeOhosConfigColorModeDataSource(QOhosConsumer< OhosConfigurationColorMode > valueChangedHandler)
void setOhosConfigColorMode(OhosConfigurationColorMode colorMode)
ButtonPaletteColors makeButtonPaletteColorsDark(const QColor &accentColor)
std::optional< bool > mapOhosConfigurationColorModeToDarkModeFlag(OhosConfigurationColorMode colorMode)
QPalette makeBasePalette(const AllPalletesColors &palettesColors)
MenuPaletteColors makeMenuPaletteColorsDark(const PalettesColors &palettes)
QColor makeOverlayCompositedOnSurface(const QColor &overlay, const QColor &surface)
TooltipPaletteColors makeTooltipPaletteColorsLight()
QtOhosQpa::enums::ohos::app::ability::ConfigurationConstant::ColorMode OhosConfigurationColorMode
ToolButtonPaletteColors makeToolButtonPaletteColorsDark()
QOhosSupplier< QColor > makeOhosAccentColorDataSource(QOhosConsumer< QColor > accentColorChangedHandler)
GroupboxPaletteColors makeGroupboxPaletteColorsDark()
std::optional< QPixmap > tryGetFilePixmapByFilenameExtension(QOhosJsState &jsState, const std::string &filenameExtension)
TabBarPaletteColors makeTabBarPaletteColorsLight(const QColor &accentColor)
QPalette makeHeaderPalette(const AllPalletesColors &palettesColors)
AllPalletesColors makeAllPalettesColors(Qt::ColorScheme scheme, const QColor &accentColor)
MenuPaletteColors makeMenuPaletteColorsLight(const PalettesColors &palettes)
OhosConfigurationColorMode mapOhosConfigurationColorModeFromJs(QOhosJsState &jsState, QNapi::Number colorModeJsEnum)
QPalette makeItemViewPalette(const AllPalletesColors &palettesColors)
constexpr int mouseDoubleClickInterval
constexpr int mouseDoubleClickDistance
Qt::ColorScheme mapOhosThemeToColorScheme(std::optional< bool > darkModeFlag)
PalettesColors makePalettesColorsDark(const QColor &accentColor)
QPalette makeTabBarPalette(const AllPalletesColors &palettesColors)
ToolButtonPaletteColors makeToolButtonPaletteColorsLight()
TextLineEditPaletteColors makeTextLineEditPaletteColorsLight()
QPalette makeMenuBarPalette(const AllPalletesColors &palettesColors)
std::optional< bool > mapOhosThemeFromColorScheme(Qt::ColorScheme scheme)
HeaderPaletteColors makeHeaderPaletteColorsDark()
TabBarPaletteColors makeTabBarPaletteColorsDark(const QColor &accentColor)
QPalette makeComboBoxPalette(const AllPalletesColors &palettesColors)
TextLineEditPaletteColors makeTextLineEditPaletteColorsDark()
ButtonPaletteColors makeButtonPaletteColorsLight(const QColor &accentColor)
QPalette makeMenuPalette(const AllPalletesColors &palettesColors)
HeaderPaletteColors makeHeaderPaletteColorsLight()
QPalette makeGroupBoxPalette(const AllPalletesColors &palettesColors)
QPalette makeButtonPalette(const AllPalletesColors &palettesColors)
QPalette makeTextLineEditPalette(const AllPalletesColors &palettesColors)
QPalette makeToolTipPalette(const AllPalletesColors &palettesColors)
GroupboxPaletteColors makeGroupboxPaletteColorsLight()
QPalette makeToolButtonPalette(const AllPalletesColors &palettesColors)
bool isOhosNoUiChildMode()