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

Support auto-setting dark mode on Linux with org.freedesktop.appearance.color-scheme #2525

Closed
louwers opened this issue May 2, 2022 · 15 comments
Labels
Milestone

Comments

@louwers
Copy link

louwers commented May 2, 2022

There should be an option to use the system wide dark mode setting on startup on Linux.

org.freedesktop.appearance.color-scheme can be used, see:

Watching the dark mode preference and updating it real-time would be even cooler, but this is a good fist step.

Example apps that use it:

@pbek
Copy link
Owner

pbek commented May 2, 2022

This only works for Gnome, right?
And it looks like you need to use GTK to check the setting, which QOwnNotes doesn't want to depend on...

@louwers
Copy link
Author

louwers commented May 2, 2022

It should also work for KDE. See the Telegram issue. It's also an official freedesktop spec.

@pbek
Copy link
Owner

pbek commented May 3, 2022

But is there a way to include this without getting GTK (or KDE) dependencies?

@louwers
Copy link
Author

louwers commented May 7, 2022

It seems that a D-Bus library is intergrated into Qt? https://doc.qt.io/qt-5/qtdbus-index.html

I can take look at some point. For myself I found the following solution, that might be helpful to other people that want to toggle dark mode automatically. My solution is GNOME specific but can probably be adapted for KDE.

There is a very nice GNOME extension called Night Theme Switcher. You can run custom commands on toggle:

image

I run this script to modify the config and to restart QOwnNotes:

#!/usr/bin/bash

if [[ $1 = "on" ]]; then
  dark=true
elif [[ $1 = "off" ]]; then
  light=true
else
  echo "Usage: dark-mode on|off"
  exit 1
fi

replace() {
  sed -ie --follow-symlinks 's/'$1'=.*/'$1'='$2'/' "$3"
}

qon=~/.config/PBE/QOwnNotes.conf

if [[ $dark ]]; then
  replace darkMode true $qon
  replace darkModeColors true $qon
  replace darkModeIconTheme true $qon
  replace CurrentSchemaKey EditorColorSchema-cdbf28fc-1ddc-4d13-bb21-6a4043316a2f  $qon
elif [[ $light ]]; then
  replace darkMode false $qon
  replace darkModeColors false $qon
  replace darkModeIconTheme false $qon
  replace CurrentSchemaKey EditorColorSchema-6033d61b-cb96-46d5-a3a8-20d5172017eb $qon
fi
killall QOwnNotes
if [[ $? == 0 ]]; then
  QOwnNotes &
fi

@pbek
Copy link
Owner

pbek commented May 8, 2022

It seems that a D-Bus library is intergrated into Qt? https://doc.qt.io/qt-5/qtdbus-index.html

hm, which also adds another Qt library dependency just for this small feature... QT += dbus 😬
https://code.qt.io/cgit/qt/qtbase.git/tree/examples/dbus/chat/chat.pro?h=5.15#n1

@louwers
Copy link
Author

louwers commented Jun 6, 2022

@pbek The following command line argument would avoid that dependency:

dbus-send --session --print-reply=literal --reply-timeout=1000 --dest=org.freedesktop.portal.Desktop /org/freedesktop/portal/desktop org.freedesktop.portal.Settings.Read string:'org.freedesktop.appearance' string:'color-scheme'

dbus-send should be available on all systems where D-Bus is installed. It returns

variant       variant          uint32 1

to indicate a dark-mode preference (see here) for both KDE and GNOME (and other DEs that support this spec).

@pbek
Copy link
Owner

pbek commented Jun 7, 2022

Sounds like a nice alternative, thank you. I wonder if you are able to execute that from a flatpak or snap... 🤔

@pbek
Copy link
Owner

pbek commented Jun 7, 2022

All I got so far when running with QProcess under Linux natively with...

    auto parameters = QStringList() <<
                    QStringLiteral("--session") << QStringLiteral("--print-reply=literal") << QStringLiteral("--reply-timeout=1000") <<
                    QStringLiteral("--dest=org.freedesktop.portal.Desktop") << QStringLiteral("/org/freedesktop/portal/desktop") <<
                    QStringLiteral("org.freedesktop.portal.Settings.Read") << QStringLiteral("string:'org.freedesktop.appearance' string:'color-scheme'");

    QProcess process;
    process.start(QStringLiteral("dbus-send"), parameters);

is

Error org.freedesktop.DBus.Error.InvalidArgs: Type of message, “(s)”, does not match expected type “.

Other permutations of input arguments (e.g. every part is a different argument item) got me: Error org.freedesktop.portal.Error.NotFound: Requested setting not found\n

@pbek
Copy link
Owner

pbek commented Jun 7, 2022

Running it with sh seems to work.

@pbek pbek added Type: Feature adds functionality Importance: Low and removed Type: Support labels Jun 7, 2022
@pbek pbek added this to the 22.6.1 milestone Jun 7, 2022
@louwers
Copy link
Author

louwers commented Jun 7, 2022

@pbek I think you have to separate those two strings, otherwise it is interpreting it as one string.

QStringLiteral("string:'org.freedesktop.appearance' string:'color-scheme'");

⬇️

QStringLiteral("string:'org.freedesktop.appearance') <<  QStringLiteral("string:'color-scheme'");

I am not a D-Bus expert, but this method requires two strings as input. I used an app called D-Feet to figure it out:

image

@pbek
Copy link
Owner

pbek commented Jun 7, 2022

22.6.1

  • under Linux (with dbus) the application will now ask you if you want to turn on
    dark mode if your desktop environment is in dark mode and the application is not
    (for #2525)
    • the application will now also ask you if you want to turn off dark mode if your
      desktop environment is in light mode and the application has dark mode turned on
    • you can remember those decisions so that the switch will be made
      automatically next time

@pbek
Copy link
Owner

pbek commented Jun 7, 2022

@pbek I think you have to separate those two strings, otherwise it is interpreting it as one string.

I had them all separate in the beginning... sh -c did the trick

@pbek
Copy link
Owner

pbek commented Jun 7, 2022

There now is a new release, could you please test it and report if it works for you?

@louwers
Copy link
Author

louwers commented Jun 7, 2022

Turns out there is a GNOME bug that sets the preference to 'default' when setting light as a preference!

However, the QOwnNotes feature works as advertised. 👍

@louwers louwers closed this as completed Jun 7, 2022
@pbek
Copy link
Owner

pbek commented Jun 8, 2022

Great, thank you for testing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants