skyjake/lagrange Issue #591: Automatically switch to dark theme on Windows

Repo Home
Issues

2023-04-09 01:57:08 tello2004

The iOS build automatically switches themes according to the current system color scheme, and if IIRC the macOS build does it too. I wonder, could this feature land on Windows too? I think it's pretty useful for folks like me that use AutoDarkMode to switch color schemes on a schedule.

Comments (3)

2023-04-10 15:39:19 skyjake

This should be doable.

However, there is a small wrinkle in that this feature requires Windows 10 while the app should run on Windows 7, too. Something to take into consideration...

2023-04-17 18:41:41 tello2004

There's the `AppsUseLightTheme` registry key (located at `Computer\HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Themes\Personalize`). By being accesible through the registry _i think_ it should work on previous versions too.

Wish I could help with the implementation but I don't really know any C. 😕 Thank you for this great piece of software though! Hope my suggestion was helpful.

2024-07-06 23:27:26 tello2004

I've read through the code and it seems like the app already does [some form of dark mode detection](https://github.com/skyjake/lagrange/blob/593486a1bfb20e43953a896ba33e477532c47fd7/src/win32.c#L189), which is for the title bar only. Could it be possible to also enable the option in the UI and execute a `os.theme.changed` command like in MacOS & iOS?

---

Edit: In the mean time, if anyone is interested in this feature and is already a user of [Windows Auto Dark Mode](https://github.com/AutoDarkMode/Windows-Auto-Night-Mode), I wrote a PowerShell script for it:

# Lagrange.psm1
enum Theme {
  black = 0
  dark = 1
  light = 2
  white = 3
}

function Set-LagrangeTheme {
  param(
    [Theme]$Theme,
    [string]$LagrangePath = "C:\Program Files\Lagrange\lagrange.exe",
    [string]$LagrangeConfig = "$env:APPDATA\fi.skyjake.Lagrange"
  )

  if (Get-Process -Name "lagrange" -ErrorAction Ignore) {
    Write-Warning "Changing theme for running Lagrange instance..."
    Start-Process -FilePath $LagrangePath -ArgumentList "--theme $Theme"
  }
  else {
    $ConfigFile = Join-Path -Path $LagrangeConfig -ChildPath "prefs.cfg"
    $Config = Get-Content -Path $ConfigFile -Raw
    $Config = $Config -replace 'theme\.set arg:\d', "theme.set arg:$($Theme.value__)"
    Set-Content -Path $ConfigFile -Value $Config
  }
}

Export-ModuleMember -Function Set-LagrangeTheme

Then, ADM's `scripts.yaml` should look like this:

# $env:APPDATA\AutoDarkMode\scripts.yaml
...
  Scripts:
    - Name: Lagrange
      Command: pwsh
      WorkingDirectory: C:\Path\To\Scripts\Directory
      ArgsLight:
        [
          -NoLogo,
          -NoProfile,
          -Command,
          "Import-Module .\\Lagrange.psm1; Set-LagrangeTheme light",
        ]
      ArgsDark:
        [
          -NoLogo,
          -NoProfile,
          -Command,
          "Import-Module .\\Lagrange.psm1; Set-LagrangeTheme dark",
        ]
      AllowedSources: [Any]
      TimeoutMillis: