Android: Light/dark modes detection
After commit: 2248487c6ca9d5459c70a16868d5aeee07d96157 light/dark mode detection is supported by Windows and macOS. This commit add similar implementation on the Android side. Task-number: QTBUG-83185 Change-Id: Id1ece98e91a31759b58d651ef62b3715ea25d85f Reviewed-by: Assam Boudjelthia <assam.boudjelthia@qt.io> (cherry picked from commit 76abdaafb540be90e87c6689cc64040e95147777)
This commit is contained in:
parent
85e0298e9c
commit
7d372f6e7a
@ -849,6 +849,8 @@ public class QtActivityDelegate
|
|||||||
QtNative.handleOrientationChanged(rotation, m_nativeOrientation);
|
QtNative.handleOrientationChanged(rotation, m_nativeOrientation);
|
||||||
m_currentRotation = rotation;
|
m_currentRotation = rotation;
|
||||||
|
|
||||||
|
handleUiModeChange(m_activity.getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK);
|
||||||
|
|
||||||
float refreshRate = (Build.VERSION.SDK_INT < Build.VERSION_CODES.R)
|
float refreshRate = (Build.VERSION.SDK_INT < Build.VERSION_CODES.R)
|
||||||
? m_activity.getWindowManager().getDefaultDisplay().getRefreshRate()
|
? m_activity.getWindowManager().getDefaultDisplay().getRefreshRate()
|
||||||
: m_activity.getDisplay().getRefreshRate();
|
: m_activity.getDisplay().getRefreshRate();
|
||||||
@ -969,6 +971,18 @@ public class QtActivityDelegate
|
|||||||
updateFullScreen();
|
updateFullScreen();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void handleUiModeChange(int uiMode)
|
||||||
|
{
|
||||||
|
switch (uiMode) {
|
||||||
|
case Configuration.UI_MODE_NIGHT_NO:
|
||||||
|
QtNative.handleUiDarkModeChanged(0);
|
||||||
|
break;
|
||||||
|
case Configuration.UI_MODE_NIGHT_YES:
|
||||||
|
QtNative.handleUiDarkModeChanged(1);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void onConfigurationChanged(Configuration configuration)
|
public void onConfigurationChanged(Configuration configuration)
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
@ -976,6 +990,7 @@ public class QtActivityDelegate
|
|||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
handleUiModeChange(configuration.uiMode & Configuration.UI_MODE_NIGHT_MASK);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void onDestroy()
|
public void onDestroy()
|
||||||
|
@ -1404,6 +1404,7 @@ public class QtNative
|
|||||||
public static native void handleOrientationChanged(int newRotation, int nativeOrientation);
|
public static native void handleOrientationChanged(int newRotation, int nativeOrientation);
|
||||||
public static native void handleRefreshRateChanged(float refreshRate);
|
public static native void handleRefreshRateChanged(float refreshRate);
|
||||||
// screen methods
|
// screen methods
|
||||||
|
public static native void handleUiDarkModeChanged(int newUiMode);
|
||||||
|
|
||||||
// pointer methods
|
// pointer methods
|
||||||
public static native void mouseDown(int winId, int x, int y);
|
public static native void mouseDown(int winId, int x, int y);
|
||||||
|
@ -755,6 +755,12 @@ static void handleRefreshRateChanged(JNIEnv */*env*/, jclass /*cls*/, jfloat ref
|
|||||||
m_androidPlatformIntegration->setRefreshRate(refreshRate);
|
m_androidPlatformIntegration->setRefreshRate(refreshRate);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void handleUiDarkModeChanged(JNIEnv */*env*/, jobject /*thiz*/, jint newUiMode)
|
||||||
|
{
|
||||||
|
QAndroidPlatformIntegration::setAppearance(
|
||||||
|
(newUiMode == 1 ) ? QPlatformTheme::Appearance::Dark : QPlatformTheme::Appearance::Light);
|
||||||
|
}
|
||||||
|
|
||||||
static void onActivityResult(JNIEnv */*env*/, jclass /*cls*/,
|
static void onActivityResult(JNIEnv */*env*/, jclass /*cls*/,
|
||||||
jint requestCode,
|
jint requestCode,
|
||||||
jint resultCode,
|
jint resultCode,
|
||||||
@ -784,6 +790,7 @@ static JNINativeMethod methods[] = {
|
|||||||
{ "setSurface", "(ILjava/lang/Object;II)V", (void *)setSurface },
|
{ "setSurface", "(ILjava/lang/Object;II)V", (void *)setSurface },
|
||||||
{ "updateWindow", "()V", (void *)updateWindow },
|
{ "updateWindow", "()V", (void *)updateWindow },
|
||||||
{ "updateApplicationState", "(I)V", (void *)updateApplicationState },
|
{ "updateApplicationState", "(I)V", (void *)updateApplicationState },
|
||||||
|
{ "handleUiDarkModeChanged", "(I)V", (void *)handleUiDarkModeChanged },
|
||||||
{ "handleOrientationChanged", "(II)V", (void *)handleOrientationChanged },
|
{ "handleOrientationChanged", "(II)V", (void *)handleOrientationChanged },
|
||||||
{ "onActivityResult", "(IILandroid/content/Intent;)V", (void *)onActivityResult },
|
{ "onActivityResult", "(IILandroid/content/Intent;)V", (void *)onActivityResult },
|
||||||
{ "onNewIntent", "(Landroid/content/Intent;)V", (void *)onNewIntent },
|
{ "onNewIntent", "(Landroid/content/Intent;)V", (void *)onNewIntent },
|
||||||
|
@ -488,6 +488,15 @@ void QAndroidPlatformIntegration::setScreenSize(int width, int height)
|
|||||||
QMetaObject::invokeMethod(m_primaryScreen, "setSize", Qt::AutoConnection, Q_ARG(QSize, QSize(width, height)));
|
QMetaObject::invokeMethod(m_primaryScreen, "setSize", Qt::AutoConnection, Q_ARG(QSize, QSize(width, height)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QPlatformTheme::Appearance QAndroidPlatformIntegration::m_appearance = QPlatformTheme::Appearance::Light;
|
||||||
|
|
||||||
|
void QAndroidPlatformIntegration::setAppearance(QPlatformTheme::Appearance newAppearance)
|
||||||
|
{
|
||||||
|
if (m_appearance == newAppearance)
|
||||||
|
return;
|
||||||
|
m_appearance = newAppearance;
|
||||||
|
}
|
||||||
|
|
||||||
void QAndroidPlatformIntegration::setScreenSizeParameters(const QSize &physicalSize,
|
void QAndroidPlatformIntegration::setScreenSizeParameters(const QSize &physicalSize,
|
||||||
const QSize &screenSize,
|
const QSize &screenSize,
|
||||||
const QRect &availableGeometry)
|
const QRect &availableGeometry)
|
||||||
|
@ -13,6 +13,7 @@
|
|||||||
#include <qpa/qplatformnativeinterface.h>
|
#include <qpa/qplatformnativeinterface.h>
|
||||||
#include <qpa/qplatformopenglcontext.h>
|
#include <qpa/qplatformopenglcontext.h>
|
||||||
#include <qpa/qplatformoffscreensurface.h>
|
#include <qpa/qplatformoffscreensurface.h>
|
||||||
|
#include <qpa/qplatformtheme.h>
|
||||||
|
|
||||||
#include <EGL/egl.h>
|
#include <EGL/egl.h>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
@ -103,6 +104,8 @@ public:
|
|||||||
|
|
||||||
void flushPendingUpdates();
|
void flushPendingUpdates();
|
||||||
|
|
||||||
|
static void setAppearance(QPlatformTheme::Appearance newAppearance);
|
||||||
|
static QPlatformTheme::Appearance appearance() { return m_appearance; }
|
||||||
#if QT_CONFIG(vulkan)
|
#if QT_CONFIG(vulkan)
|
||||||
QPlatformVulkanInstance *createPlatformVulkanInstance(QVulkanInstance *instance) const override;
|
QPlatformVulkanInstance *createPlatformVulkanInstance(QVulkanInstance *instance) const override;
|
||||||
#endif
|
#endif
|
||||||
@ -115,6 +118,8 @@ private:
|
|||||||
|
|
||||||
QThread *m_mainThread;
|
QThread *m_mainThread;
|
||||||
|
|
||||||
|
static QPlatformTheme::Appearance m_appearance;
|
||||||
|
|
||||||
static QRect m_defaultAvailableGeometry;
|
static QRect m_defaultAvailableGeometry;
|
||||||
static QSize m_defaultPhysicalSize;
|
static QSize m_defaultPhysicalSize;
|
||||||
static QSize m_defaultScreenSize;
|
static QSize m_defaultScreenSize;
|
||||||
|
@ -358,6 +358,11 @@ void QAndroidPlatformTheme::showPlatformMenuBar()
|
|||||||
QtAndroidMenu::openOptionsMenu();
|
QtAndroidMenu::openOptionsMenu();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QPlatformTheme::Appearance QAndroidPlatformTheme::appearance() const
|
||||||
|
{
|
||||||
|
return QAndroidPlatformIntegration::appearance();
|
||||||
|
}
|
||||||
|
|
||||||
static inline int paletteType(QPlatformTheme::Palette type)
|
static inline int paletteType(QPlatformTheme::Palette type)
|
||||||
{
|
{
|
||||||
switch (type) {
|
switch (type) {
|
||||||
|
@ -35,6 +35,7 @@ public:
|
|||||||
QPlatformMenu *createPlatformMenu() const override;
|
QPlatformMenu *createPlatformMenu() const override;
|
||||||
QPlatformMenuItem *createPlatformMenuItem() const override;
|
QPlatformMenuItem *createPlatformMenuItem() const override;
|
||||||
void showPlatformMenuBar() override;
|
void showPlatformMenuBar() override;
|
||||||
|
Appearance appearance() const override;
|
||||||
const QPalette *palette(Palette type = SystemPalette) const override;
|
const QPalette *palette(Palette type = SystemPalette) const override;
|
||||||
const QFont *font(Font type = SystemFont) const override;
|
const QFont *font(Font type = SystemFont) const override;
|
||||||
QVariant themeHint(ThemeHint hint) const override;
|
QVariant themeHint(ThemeHint hint) const override;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user