fcitx / fcitx5

maybe a new fcitx.
1.62k stars 118 forks source link

automatic switch light/dark theme on sway #900

Closed name-snrl closed 10 months ago

name-snrl commented 11 months ago

Hello. I'm using sway with xdg-desktop-portal v1.16.0 plus wlr and gtk backends.

If I change the theme with gsettings set org.gnome.desktop.interface color-scheme prefer-dark and then check it with busctl --user call org.freedesktop.portal.Desktop /org/freedesktop/portal/desktop org.freedesktop.portal.Settings Read ss "org.freedesktop.appearance" "color-scheme", it works and the value changes from 1 to 0.

What I'm missing to make it work on the sway?

wengxt commented 11 months ago
  1. did you enable dark support in classic ui?
  2. did you use text-input for input method protocol?
name-snrl commented 11 months ago
  1. did you enable dark support in classic ui?

do you mean UseDarkTheme=True?

  1. did you use text-input for input method protocol?

sry, don't understand what does it mean. I test it with mozc

wengxt commented 11 months ago

can you post the output of fcitx5-diagnose while some application opened?

name-snrl commented 11 months ago

output of fcitx5-diagnose

diagnose.log

wengxt commented 11 months ago

from your fcitx5-diagnose output, you're still set GTK_IM_MODULE, which will make gtk im module itself to draw your popup window.

Unfortunately, the two new advanced color feature (Accent color and dark mode theme) are not supported for gtk im module rendered popup, since I don't want to create more burden (e.g. listen to additional dbus service) in the im module.

for sway https://fcitx-im.org/wiki/Using_Fcitx_5_on_Wayland#Sway you'd required to use a patched version to enable zwp_input_method_v2's popup. then unset GTK_IM_MODULE and you will be able to use the fcitx server rendered UI.

name-snrl commented 11 months ago

use a patched version

Thanks again. Where is the best place for me to express financial support to you, is github a good enough solution? it would be convenient for me to do it in usdt trc20.

wengxt commented 10 months ago

https://fcitx-im.org/wiki/Donate

name-snrl commented 9 months ago

Unfortunately, the two new advanced color feature (Accent color and dark mode theme) are not supported for gtk im module rendered popup

hmm... what about Qt on sway? not supported either? the only application that uses gtk im render is firefox for me.

use a patched version to enable

can we leave this issue open until this PR is merged and tested? it looks like it's gonna happen soon.

name-snrl commented 9 months ago

https://fcitx-im.org/wiki/Donate

I'm currently in Russia, so I can't sponsor you on github, but I'm ready to send you 100 usdt trc20 at any time, just gimme wallet in pm

name-snrl commented 9 months ago

can we leave this issue open until this PR is merged and tested?

@wengxt what do you think?

wengxt commented 9 months ago

I don't plan to do anything. So I don't see why should I leave it open.

Actually, if you want such feature, I can give you a simple command to be execute manually.

Change the "default-dark" in the command below to your desired theme name.

gdbus call \
   --session --dest org.fcitx.Fcitx5 \
   --object-path /controller \
  --method org.fcitx.Fcitx.Controller1.SetConfig \
 fcitx://config/addon/classicui "<{'Theme': <'default-dark'>}>"
IceAsteroid commented 8 months ago

I have a script that I wrote to switch between dark & light themes for various programs, which have their own command to switch themes in terminal.

Though it might be somehow incomplete, but it does the job. To share it here, you can make changes to fit your needs. Hope it helps :)

It can then be bound as keybindings in Sway based on its options for dark & light themes.

For exmaple:

    bindsym $mod+bracketleft exec "$HOME/Scripts/swaySwitchDarkLightThemes.sh -D"
    bindsym $mod+bracketright exec "$HOME/Scripts/swaySwitchDarkLightThemes.sh -L"
    bindsym $mod+Shift+bracketleft exec "$HOME/Scripts/swaySwitchDarkLightThemes.sh -d"
    bindsym $mod+Shift+bracketright exec "$HOME/Scripts/swaySwitchDarkLightThemes.sh -l
$: swaySwitchDarkLightThemes.sh -h
## Help Page ##
  -L switch to the specified light themes for GTK in the script
  -l switch to the specified light themes for trmnl in the script
  -D switch to the specified dark themes for GTK the script
  -d switch to the specified dark themes for trml the script
#!/bin/bash

scrptName="$(basename $0)"
darkLightThemeQueryDir="/tmp/swayScriptsTmpDir"
darkLightThemeQueryFile="${darkLightThemeQueryDir}/swayThemeSwitched.txt"

[ -d ${darkLightThemeQueryDir} ] || mkdir ${darkLightThemeQueryDir}; chmod 770 ${darkLightThemeQueryDir}
[ -s ${darkLightThemeQueryFile} ] || cat > ${darkLightThemeQueryFile} <<EOF
GTK dark: 0
GTK light: 0
Terminal dark: 0
Terminal light: 0
EOF
chmod 660 ${darkLightThemeQueryFile}

gtk_() {
gtkDarkCurrentIndex="$(grep --extended-regexp "^GTK dark: [0-9]+" ${darkLightThemeQueryFile} | awk -F' ' '{print $NF}')" 
gtkLightCurrentIndex="$(grep --extended-regexp "^GTK light: [0-9]+" ${darkLightThemeQueryFile} | awk -F' ' '{print $NF}')" 

gtkDarkThemes=('Adapta-Nokto-Eta')
gtkLightThemes=('Adapta-Eta' 'Bluebird')

gtk3confDir="${HOME}/.config/gtk-3.0"
gtk3confFile="${gtk3confDir}/settings.ini"
gtkSchema="org.gnome.desktop.interface"
}
gtkSwitchTo_() {
#GTK theme to set
  if [[ "${1}" == dark ]]; then
    [[ "${gtkDarkCurrentIndex}" -ge $((${#gtkDarkThemes[@]} - 1)) ]] \
      && gtkDarkCurrentIndexToBe=0 \
      || gtkDarkCurrentIndexToBe=$((${#gtkDarkCurrentIndex[@]} + 1))

    echo "# Dark theme '${gtkDarkThemes[${gtkDarkCurrentIndexToBe}]}' for GTK.."
    notify-send "${scrptName}" "# Dark theme '${gtkDarkThemes[${gtkDarkCurrentIndexToBe}]}' for GTK.."

    # Global dbus broadcasting
    gsettings set org.gnome.desktop.interface color-scheme 'prefer-dark'
    gsettings set org.gnome.desktop.interface gtk-theme "${gtkDarkThemes[${gtkDarkCurrentIndexToBe}]}"

    # Write which dark theme is switched to, to conf files
    sed -iE "s/^gtk-theme-name=.\+/gtk-theme-name=${gtkDarkThemes[${gtkDarkCurrentIndexToBe}]}/" ${gtk3confFile}
    sed -i "s/^GTK dark:.*/GTK dark: ${gtkDarkCurrentIndexToBe}/" ${darkLightThemeQueryFile}
  elif [[ "${1}" == light ]]; then
    [[ "${gtkLightCurrentIndex}" -ge $((${#gtkLightThemes[@]} - 1)) ]] \
      && gtkLightCurrentIndexToBe=0 \
      || gtkLightCurrentIndexToBe=$((${gtkLightCurrentIndex} + 1))
    echo "# Light theme '${gtkLightThemes[${gtkLightCurrentIndexToBe}]}' for GTK.."
    notify-send "${scrptName}" "# Light theme '${gtkLightThemes[${gtkLightCurrentIndexToBe}]}' for GTK.."

    # Global dbus broadcasting
    gsettings set org.gnome.desktop.interface color-scheme 'prefer-light'
    gsettings set org.gnome.desktop.interface gtk-theme "${gtkLightThemes[${gtkLightCurrentIndexToBe}]}"

    # Write which light theme is switched to, to conf files
    sed -iE "s/^gtk-theme-name=.\+/gtk-theme-name=${gtkLightThemes[${gtkLightCurrentIndexToBe}]}/" ${gtk3confFile}
    sed -i "s/^GTK light:.*/GTK light: ${gtkLightCurrentIndexToBe}/" ${darkLightThemeQueryFile}
  fi
}

qt_() {
  # Dark & light themes need to be the same number as GTK's
  # As to keep the querying aligned between GTK and QT themes
  # So each time, say GTK dark A theme will also match QT dark A theme when switched to.
  qtDarkThemes=('KvAdaptaDark')
  qtLightthemes=('KvAdapta' 'Bluebird')
}
qtSwitchTo_() {
  if [[ "${1}" == dark ]]; then
    kvantummanager --set "${qtDarkThemes[0]}"
  elif [[ "${1}" == light ]]; then
    kvantummanager --set "${qtLightthemes[0]}"
  fi
}

fcitx5_() {
  fcitx5DarkThemes=('default-dark')
  fcitx5LightThemes=('default')
}
fcitx5SwitchTo_() {
  if [[ "${1}" == dark ]]; then
    gdbus call \
      --session --dest org.fcitx.Fcitx5 \
      --object-path /controller \
      --method org.fcitx.Fcitx.Controller1.SetConfig \
        fcitx://config/addon/classicui "<{'Theme': <'${fcitx5DarkThemes[0]}'>}>"
  elif [[ "${1}" == light ]]; then
    gdbus call \
      --session --dest org.fcitx.Fcitx5 \
      --object-path /controller \
      --method org.fcitx.Fcitx.Controller1.SetConfig \
        fcitx://config/addon/classicui "<{'Theme': <'${fcitx5LightThemes[0]}'>}>"
  fi
}

#waybar_() {
#
#}
#waybarSwitchTo_() {
#
#}

kitty_() {
terminalDarkCurrentIndex="$(grep --extended-regexp "^Terminal dark: [0-9]+" ${darkLightThemeQueryFile} | awk -F' ' '{print $NF}')" 
terminalLightCurrentIndex="$(grep --extended-regexp "^Terminal light: [0-9]+" ${darkLightThemeQueryFile} | awk -F' ' '{print $NF}')" 

# the '--cache-age' for kitten themes stands for cache days before checking updates
# default 1 day, 0 for always checking, negative value for no checking.
terminalSetTheme='kitty +kitten themes --cache-age=-1'
# Abandoned dark themes: Encom#BackgrountTooDarkBlutTooBright
#  Box#GreenAsTextTooBright
#  Ciapre#OtherColorsTooDim
terminalDarkThemes=('Nightfly' 'Sakura Night' 'Cyberpunk' 'atelier sulphurpool' 'Breath2' 'desert night')
# Abandoned light themes: Neobones_light#SameBackgrountBrightness&MaterialButLessContrastness
#  Leaf light#prompt color is hard to see
terminalLightThemes=('gruvbox light' 'novel' 'h-pux' 'seoulbones_light' 'material' 'bluloco dark')
}
kittySwitchTo_() {
  if [[ "${1}" == dark ]]; then
    [[ "${terminalDarkCurrentIndex}" -ge $((${#terminalDarkThemes[@]} - 1)) ]] \
      && terminalDarkCurrentIndexToBe=0 \
      || terminalDarkCurrentIndexToBe=$((${terminalDarkCurrentIndex} + 1))

    echo "# Dark theme '${terminalLightThemes[${terminalDarkCurrentIndexToBe}]}' for trmnl.."
    notify-send "${scrptName}" "# Dark theme '${terminalDarkThemes[${terminalDarkCurrentIndexToBe}]}' for trmnl.."

    ${terminalSetTheme} "${terminalDarkThemes[${terminalDarkCurrentIndexToBe}]}"
    echo terminalDarkThemes[terminalDarkCurrentIndexToBe ${terminalDarkThemes[${terminalDarkCurrentIndexToBe}]}
    kill -SIGUSR1 $(pgrep kitty) #tell kitty to reload conf
    sed -i "s/^Terminal dark:.*/Terminal dark: ${terminalDarkCurrentIndexToBe}/" ${darkLightThemeQueryFile}

    #Cmus
    cmus-remote -C 'colorscheme dracula'
  elif [[ "${1}" == light ]]; then
    [[ "${terminalLightCurrentIndex}" -ge $((${#terminalLightThemes[@]} - 1)) ]] \
      && terminalLightCurrentIndexToBe=0 \
      || terminalLightCurrentIndexToBe=$((${terminalLightCurrentIndex} + 1))

    echo "# Light theme '${terminalLightThemes[${terminalLightCurrentIndexToBe}]}' for trmnl.."
    notify-send "${scrptName}" "# Light theme '${terminalLightThemes[${terminalLightCurrentIndexToBe}]}' for trmnl.."

    ${terminalSetTheme} "${terminalLightThemes[${terminalLightCurrentIndexToBe}]}"
    kill -SIGUSR1 $(pgrep kitty) #tell kitty to reload conf
    sed -i "s/^Terminal light:.*/Terminal light: ${terminalLightCurrentIndexToBe}/" ${darkLightThemeQueryFile}

    #Cmus
    cmus-remote -C 'colorscheme green'
  fi
}

while getopts :LlDd opt; do
  case ${opt} in
    D) gtk_; gtkSwitchTo_ dark;
       qt_; qtSwitchTo_ dark;
       fcitx5_; fcitx5SwitchTo_ dark;;
    L) gtk_; gtkSwitchTo_ light;
       qt_; qtSwitchTo_ light;
       fcitx5_; fcitx5SwitchTo_ light;;
    d) kitty_; kittySwitchTo_ dark;;
    l) kitty_; kittySwitchTo_ light;;
    h|*)
       cat <<EOF
## Help Page ##
  -L switch to the specified light themes for GTK in the script
  -l switch to the specified light themes for trmnl in the script
  -D switch to the specified dark themes for GTK the script
  -d switch to the specified dark themes for trml the script
EOF
      ;;
  esac
done