Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sync: from linuxdeepin/dtkgui #23

Merged
merged 1 commit into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading