PolyMeilex / sctk-adwaita

Adwaita-like SCTK Frame
MIT License
26 stars 19 forks source link

Use dbus to hint dark theme preference #10

Closed alexheretic closed 2 years ago

alexheretic commented 2 years ago

Automatically select light/dark theme by querying org.gnome.desktop.interface.color-scheme which indicates the light or dark selection in modern gnome "Appearance" settings.

Perhaps there is a better way to query gsettings than running the bin as a command? For me this takes ~2.5ms to run which will happen once on init. Using command is fairly simple and means no additional dependencies at least.

PolyMeilex commented 2 years ago

Using xdg desktop portal standard would probably be a better choice, but there is no way we can include dbus crate in this crate, so I guess gsettings will have to suffice.

Technically, we could parse output of dbus-send:

dbus-send --print-reply --dest=org.freedesktop.portal.Desktop /org/freedesktop/portal/desktop org.freedesktop.portal.Settings.Read string:"org.freedesktop.appearance" string:"color-scheme"

it outputs:

0 For no preference
1 For prefer dark appearance
2 For prefer light appearance

but the output of dbus-send is not really machine friendly, and I don't know how common the dbus-send binary is.

In general, the PR looks good to me, I just have to think about XDG alternative for a bit, and If I don't come up with any solution we can merge gnome specific version as is.

Drakulix commented 2 years ago

Using xdg desktop portal standard would probably be a better choice, but there is no way we can include dbus crate in this crate, so I guess gsettings will have to suffice.

Would zbus be an option (at least as an optional feature), given that it is pure-rust? Going dbus would be nice, because it would also allow to monitor this setting.

alexheretic commented 2 years ago

Technically, we could parse output of dbus-send:

Sure sounds good.

Would zbus be an option

Ah cool, though it has a ton of dependencies so maybe feels a bit much for this simple feature?

PolyMeilex commented 2 years ago

Would zbus be an option (at least as an optional feature), given that it is pure-rust? Going dbus would be nice, because it would also allow to monitor this setting.

If we want to go dbus crate route, zbus would definitely be the crate of choice. Then we could spawn a thread and put zbus event processing in there and update the theme on the fly.

Still a cargo project with a NOP main function and zbus as its dependency takes 23.83s (debug) / 30s (release) to build on my system, probably due to the fact that it requires an async executor.

alexheretic commented 2 years ago

dbus-send command version works fine for me, takes ~2ms so a touch faster too :+1:

alexheretic commented 2 years ago

I don't know how common the dbus-send binary is

It's common on Arch at least. I imagine it's probably around whenever gsettings is.

murlakatamenka commented 2 years ago

Just to chime in: to use dbus you can use busctl which is part of systemd.

How to use it, for example: https://github.com/MaxVerevkin/wl-gammarelay-rs#example-usage-in-scripts

murlakatamenka commented 2 years ago

I don't know how common the dbus-send binary is.

dbus is a dependency of systemd on Arch, so you can expect it on distros with systemd.

PolyMeilex commented 2 years ago

Just to chime in: to use dbus you can use busctl which is part of systemd.

How to use it, for example: https://github.com/MaxVerevkin/wl-gammarelay-rs#example-usage-in-scripts

Indeed, it has more machine friendly output:

busctl --user call org.freedesktop.portal.Desktop /org/freedesktop/portal/desktop org.freedesktop.portal.Settings Read ss "org.freedesktop.appearance" "color-scheme" --json="short"

Gives us:

{
  "type": "v",
  "data": [
    {
      "type": "v",
      "data": {
        "type": "u",
        "data": 1
      }
    }
  ]
}

Not sure if that gives us any benefits over what this PR does. So I would just leave it as is for now.