Skip to content

Commit

Permalink
sync: from linuxdeepin/dtkgui
Browse files Browse the repository at this point in the history
Synchronize source files from linuxdeepin/dtkgui.

Source-pull-request: linuxdeepin/dtkgui#223
  • Loading branch information
deepin-ci-robot authored and 18202781743 committed Feb 1, 2024
1 parent 4aebc16 commit 0e8a23b
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 10 deletions.
59 changes: 49 additions & 10 deletions src/kernel/dguiapplicationhelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,36 @@ void LoadManualServiceWorker::checkManualServiceWakeUp()
start();
}

#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
class Q_DECL_HIDDEN GuiApplicationEventFilter : public QObject
{
public:
explicit GuiApplicationEventFilter(DGuiApplicationHelperPrivate *transmitter,
QObject *parent = nullptr)
: QObject(parent)
, m_transmitter(transmitter)
{
}
virtual bool eventFilter(QObject *watched, QEvent *event) override
{
switch(event->type()) {
case QEvent::ApplicationFontChange: {
const QFont font(qGuiApp->font());
m_transmitter->q_func()->fontChanged(font);
} break;
case QEvent::ApplicationPaletteChange: {
m_transmitter->onApplicationPaletteChanged();
} break;
default:
break;
}
return QObject::eventFilter(watched, event);
}
private:
DGuiApplicationHelperPrivate *m_transmitter = nullptr;
};
#endif

HelperCreator _DGuiApplicationHelper::creator = _DGuiApplicationHelper::defaultCreator;
Q_GLOBAL_STATIC(_DGuiApplicationHelper, _globalHelper)

Expand Down Expand Up @@ -234,18 +264,15 @@ void DGuiApplicationHelperPrivate::initApplication(QGuiApplication *app)
// 跟随application销毁
qAddPostRoutine(staticCleanApplication);

q->connect(app, &QGuiApplication::paletteChanged, q, [q, this, app] {
// 如果用户没有自定义颜色类型, 则应该通知程序的颜色类型发送变化
if (Q_LIKELY(!isCustomPalette())) {
Q_EMIT q->themeTypeChanged(q->toColorType(app->palette()));
Q_EMIT q->applicationPaletteChanged();
} else {
qWarning() << "DGuiApplicationHelper: Don't use QGuiApplication::setPalette on DTK application.";
}
});

// 转发程序自己变化的信号
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
app->installEventFilter(new GuiApplicationEventFilter(this, app));
#else
q->connect(app, &QGuiApplication::fontChanged, q, &DGuiApplicationHelper::fontChanged);
q->connect(app, &QGuiApplication::paletteChanged, q, [this] {
onApplicationPaletteChanged();
});
#endif

if (Q_UNLIKELY(!appTheme)) { // 此时说明appTheme可能已经被初始化为了systemtheme
if (QGuiApplicationPrivate::is_app_running) {
Expand Down Expand Up @@ -349,6 +376,18 @@ void DGuiApplicationHelperPrivate::notifyAppThemeChangedByEvent()
QGuiApplicationPrivate::processThemeChanged(&event);
}

void DGuiApplicationHelperPrivate::onApplicationPaletteChanged()
{
D_Q(DGuiApplicationHelper);
// 如果用户没有自定义颜色类型, 则应该通知程序的颜色类型发送变化
if (Q_LIKELY(!isCustomPalette())) {
Q_EMIT q->themeTypeChanged(q->toColorType(qGuiApp->palette()));
Q_EMIT q->applicationPaletteChanged();
} else {
qWarning() << "DGuiApplicationHelper: Don't use QGuiApplication::setPalette on DTK application.";
}
}

bool DGuiApplicationHelperPrivate::isCustomPalette() const
{
return appPalette || paletteType != DGuiApplicationHelper::UnknownType;
Expand Down
1 change: 1 addition & 0 deletions src/private/dguiapplicationhelper_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class DGuiApplicationHelperPrivate : public DCORE_NAMESPACE::DObjectPrivate
DGuiApplicationHelper::SizeMode fetchSizeMode(bool *isSystemSizeMode = nullptr) const;
void notifyAppThemeChanged();
void notifyAppThemeChangedByEvent();
void onApplicationPaletteChanged();
// 返回程序是否自定义了调色板
inline bool isCustomPalette() const;
void setPaletteType(DGuiApplicationHelper::ColorType ct, bool emitSignal);
Expand Down

0 comments on commit 0e8a23b

Please sign in to comment.