Skip to content

Commit

Permalink
Apply font scaling and font weight settings under Android
Browse files Browse the repository at this point in the history
  • Loading branch information
Martchus committed Jan 20, 2025
1 parent a128c69 commit 8875a2d
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 1 deletion.
20 changes: 20 additions & 0 deletions tray/android/src/io/github/martchus/syncthingtray/Activity.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.content.res.Configuration;
import android.media.MediaScannerConnection;
import android.os.Build;
import android.os.Bundle;
Expand All @@ -23,6 +24,8 @@

public class Activity extends QtActivity {
private static final String TAG = "SyncthingActivity";
private float m_fontScale = 1.0f;
private int m_fontWeightAdjustment = 0;

public boolean performHapticFeedback() {
View rootView = getWindow().getDecorView().getRootView();
Expand Down Expand Up @@ -116,10 +119,27 @@ void handleSendText(Intent intent) {
}
}

public float fontScale() {
return m_fontScale;
}

public int fontWeightAdjustment() {
return m_fontWeightAdjustment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
Log.i(TAG, "Creating");
super.onCreate(savedInstanceState);

// read font scale as Qt does not handle this automatically on Android
Configuration config = getResources().getConfiguration();
m_fontScale = config.fontScale;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
m_fontWeightAdjustment = config.fontWeightAdjustment;
}

// read text another app might have shared with us
Intent intent = getIntent();
String action = intent.getAction();
String type = intent.getType();
Expand Down
7 changes: 6 additions & 1 deletion tray/gui/qml/Main.qml
Original file line number Diff line number Diff line change
Expand Up @@ -412,10 +412,15 @@ ApplicationWindow {
onAccepted: pageStack.pop(true)
}

// handle global keyboard and mouse events
Component.onCompleted: {
// propagate palette of Qt Quick Controls 2 style to regular QPalette of QGuiApplication for icon rendering
App.setPalette(Material.foreground, Material.background);

// apply font scaling (on platforms where additional scaling is necassary because Qt doesn't do it such as Android)
window.font.pixelSize *= App.fontScale;
window.font.weight += App.fontWeightAdjustment;

// handle global keyboard and mouse events
window.contentItem.forceActiveFocus(Qt.ActiveWindowFocusReason);
window.contentItem.Keys.released.connect((event) => {
const key = event.key;
Expand Down
18 changes: 18 additions & 0 deletions tray/gui/quick/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,24 @@ DiffHighlighter *App::createDiffHighlighter(QTextDocument *parent)
return new DiffHighlighter(parent);
}

float App::fontScale() const
{
#ifdef Q_OS_ANDROID
return QJniObject(QNativeInterface::QAndroidApplication::context()).callMethod<jfloat>("fontScale", "()F");
#else
return 1.0f;
#endif
}

int App::fontWeightAdjustment() const
{
#ifdef Q_OS_ANDROID
return QJniObject(QNativeInterface::QAndroidApplication::context()).callMethod<jint>("fontWeightAdjustment", "()I");
#else
return 0;
#endif
}

bool App::eventFilter(QObject *object, QEvent *event)
{
if (object != m_app) {
Expand Down
4 changes: 4 additions & 0 deletions tray/gui/quick/app.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ class App : public QObject {
Q_PROPERTY(QIcon statusIcon READ statusIcon NOTIFY statusInfoChanged)
Q_PROPERTY(QString additionalStatusText READ additionalStatusText NOTIFY statusInfoChanged)
Q_PROPERTY(bool scanSupported READ isScanSupported CONSTANT)
Q_PROPERTY(float fontScale READ fontScale CONSTANT)
Q_PROPERTY(int fontWeightAdjustment READ fontWeightAdjustment CONSTANT)
QML_ELEMENT
QML_SINGLETON

Expand Down Expand Up @@ -201,6 +203,8 @@ class App : public QObject {
return false;
#endif
}
float fontScale() const;
int fontWeightAdjustment() const;

// helper functions invoked from QML
Q_INVOKABLE bool loadMain();
Expand Down

0 comments on commit 8875a2d

Please sign in to comment.