Also color the panel on android, fix iOS landscape mode

This commit is contained in:
Michael Zanetti 2019-09-20 01:16:39 +02:00
parent 2255f99d70
commit 588131f9fe
5 changed files with 84 additions and 13 deletions

View File

@ -4,8 +4,24 @@
#include <QtAndroid>
#include <QDebug>
// WindowManager.LayoutParams
#define FLAG_TRANSLUCENT_STATUS 0x04000000
#define FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS 0x80000000
// View
#define SYSTEM_UI_FLAG_LIGHT_STATUS_BAR 0x00002000
static PlatformHelperAndroid *m_instance;
static QAndroidJniObject getAndroidWindow()
{
QAndroidJniObject window = QtAndroid::androidActivity().callObjectMethod("getWindow", "()Landroid/view/Window;");
window.callMethod<void>("addFlags", "(I)V", FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.callMethod<void>("clearFlags", "(I)V", FLAG_TRANSLUCENT_STATUS);
return window;
}
PlatformHelperAndroid::PlatformHelperAndroid(QObject *parent) : PlatformHelper(parent)
{
m_instance = this;
@ -77,6 +93,33 @@ void PlatformHelperAndroid::vibrate(PlatformHelper::HapticsFeedback feedbackType
QtAndroid::androidActivity().callMethod<void>("vibrate","(I)V", duration);
}
void PlatformHelperAndroid::setTopPanelColor(const QColor &color)
{
PlatformHelper::setTopPanelColor(color);
if (QtAndroid::androidSdkVersion() < 21)
return;
QtAndroid::runOnAndroidThread([=]() {
QAndroidJniObject window = getAndroidWindow();
window.callMethod<void>("setStatusBarColor", "(I)V", color.rgba());
});
if (((color.red() * 299 + color.green() * 587 + color.blue() * 114) / 1000) > 123) {
setTopPanelTheme(Light);
} else {
setTopPanelTheme(Dark);
}
}
void PlatformHelperAndroid::setBottomPanelColor(const QColor &color)
{
PlatformHelper::setBottomPanelColor(color);
if (QtAndroid::androidSdkVersion() < 21)
return;
}
void PlatformHelperAndroid::permissionRequestFinished(const QtAndroid::PermissionResultMap &result)
{
foreach (const QString &key, result.keys()) {
@ -84,3 +127,20 @@ void PlatformHelperAndroid::permissionRequestFinished(const QtAndroid::Permissio
}
emit m_instance->permissionsRequestFinished();
}
void PlatformHelperAndroid::setTopPanelTheme(PlatformHelperAndroid::Theme theme)
{
if (QtAndroid::androidSdkVersion() < 23)
return;
QtAndroid::runOnAndroidThread([=]() {
QAndroidJniObject window = getAndroidWindow();
QAndroidJniObject view = window.callObjectMethod("getDecorView", "()Landroid/view/View;");
int visibility = view.callMethod<int>("getSystemUiVisibility", "()I");
if (theme == Theme::Light)
visibility |= SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
else
visibility &= ~SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
view.callMethod<void>("setSystemUiVisibility", "(I)V", visibility);
});
}

View File

@ -9,6 +9,8 @@ class PlatformHelperAndroid : public PlatformHelper
{
Q_OBJECT
public:
enum Theme { Light, Dark };
explicit PlatformHelperAndroid(QObject *parent = nullptr);
Q_INVOKABLE void requestPermissions() override;
@ -24,6 +26,10 @@ public:
Q_INVOKABLE void vibrate(HapticsFeedback feedbackType) override;
void setTopPanelColor(const QColor &color) override;
void setTopPanelTheme(Theme theme);
void setBottomPanelColor(const QColor &color) override;
private:
static void permissionRequestFinished(const QtAndroid::PermissionResultMap &);

View File

@ -1,10 +1,16 @@
#include "platformhelperios.h"
#include <QDebug>
#include <QUuid>
#include <QScreen>
#include <QApplication>
PlatformHelperIOS::PlatformHelperIOS(QObject *parent) : PlatformHelper(parent)
{
QScreen *screen = qApp->primaryScreen();
screen->setOrientationUpdateMask(Qt::PortraitOrientation | Qt::LandscapeOrientation | Qt::InvertedPortraitOrientation | Qt::InvertedLandscapeOrientation);
QObject::connect(screen, &QScreen::orientationChanged, qApp, [this](Qt::ScreenOrientation) {
setBottomPanelColor(bottomPanelColor());
});
}
@ -83,6 +89,13 @@ void PlatformHelperIOS::setTopPanelColor(const QColor &color)
void PlatformHelperIOS::setBottomPanelColor(const QColor &color)
{
PlatformHelper::setBottomPanelColor(color);
setBottomPanelColorInternal(color);
// In landscape, ignore settings and keep it to black. On notched devices it'll look crap otherwise
if (qApp->primaryScreen()->orientation() == Qt::LandscapeOrientation || qApp->primaryScreen()->orientation() == Qt::InvertedLandscapeOrientation) {
setBottomPanelColorInternal(QColor("black"));
} else {
setBottomPanelColorInternal(color);
}
}

View File

@ -14,6 +14,7 @@ ApplicationWindow {
minimumWidth: 360
minimumHeight: 480
visibility: kioskMode ? ApplicationWindow.FullScreen : settings.viewMode
color: Material.background
// Those variables must be present in the Style
title: appName
@ -49,6 +50,8 @@ ApplicationWindow {
Component.onCompleted: {
styleController.setSystemFont(app.font)
PlatformHelper.topPanelColor = app.primaryColor
PlatformHelper.bottomPanelColor = Material.background
}
RootItem {

View File

@ -10,17 +10,6 @@ import "connection"
Item {
id: root
// Workaround flickering on pageStack animations when the white background shines through
Rectangle {
anchors.fill: parent
color: Material.background
Component.onCompleted: {
PlatformHelper.topPanelColor = app.primaryColor
PlatformHelper.bottomPanelColor = color
}
}
function handleAndroidBackButton() {
return swipeView.currentItem.handleAndroidBackButton()
}