Skip to content

Commit

Permalink
Removing errors when swaybg is not installed
Browse files Browse the repository at this point in the history
  • Loading branch information
anufrievroman committed Dec 31, 2023
1 parent 0733122 commit 7d912f4
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 24 deletions.
5 changes: 3 additions & 2 deletions waypaper/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from waypaper.config import Config
from waypaper.app import App
from waypaper.changer import change_wallpaper
from waypaper.common import get_random_file
from waypaper.common import get_random_file, check_missing_backends
from waypaper.aboutdata import AboutData
from waypaper.options import FILL_OPTIONS, BACKEND_OPTIONS
from waypaper.translations import English, German, French, Russian, Polish, Chinese
Expand Down Expand Up @@ -44,6 +44,7 @@ def run():
"""Read user arguments and either run GUI app or just reset the wallpaper"""

cf.read_parameters_from_user_arguments(args)
missing_backends = check_missing_backends()

# Set the wallpaper and quit:
if args.restore:
Expand All @@ -54,7 +55,7 @@ def run():

if wallpaper is None:
continue
change_wallpaper(wallpaper, cf.fill_option, cf.color, cf.backend, monitor, cf.swww_transition, txt)
change_wallpaper(wallpaper, cf, monitor, txt, missing_backends)
time.sleep(0.1)
exit(0)

Expand Down
19 changes: 5 additions & 14 deletions waypaper/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import subprocess
import threading
import os
import shutil
import gi
from pathlib import Path
from platformdirs import user_cache_path
Expand All @@ -12,7 +11,7 @@
from waypaper.aboutdata import AboutData
from waypaper.changer import change_wallpaper
from waypaper.config import Config
from waypaper.common import get_image_paths, get_random_file
from waypaper.common import get_image_paths, get_random_file, check_missing_backends
from waypaper.options import FILL_OPTIONS, BACKEND_OPTIONS, SORT_OPTIONS, SORT_DISPLAYS

gi.require_version("Gtk", "3.0")
Expand Down Expand Up @@ -221,12 +220,7 @@ def monitor_option_display(self):

def check_backends(self):
"""Before running the app, check which backends are installed"""
self.missing_backends = []
for backend in BACKEND_OPTIONS:
if backend == "wallutils":
backend = "setwallpaper"
is_backend_missing = not bool(shutil.which(backend))
self.missing_backends.append(is_backend_missing)
self.missing_backends = check_missing_backends()

# Show error message if no backends are installed:
if all(self.missing_backends):
Expand Down Expand Up @@ -411,8 +405,7 @@ def on_image_clicked(self, widget, path):
self.load_image_grid()
print(self.txt.msg_path, self.cf.selected_wallpaper)
self.cf.fill_option = self.fill_option_combo.get_active_text() or self.cf.fill_option
change_wallpaper(self.cf.selected_wallpaper, self.cf.fill_option, self.cf.color,
self.cf.backend, self.cf.selected_monitor, self.cf.swww_transition, self.txt)
change_wallpaper(self.cf.selected_wallpaper, self.cf, self.cf.selected_monitor, self.txt, self.missing_backends)
self.cf.save()


Expand All @@ -438,8 +431,7 @@ def set_random_wallpaper(self):
return
print(self.txt.msg_path, self.cf.selected_wallpaper)
self.cf.fill_option = self.fill_option_combo.get_active_text() or self.cf.fill_option
change_wallpaper(self.cf.selected_wallpaper, self.cf.fill_option, self.cf.color,
self.cf.backend, self.cf.selected_monitor, self.cf.swww_transition, self.txt)
change_wallpaper(self.cf.selected_wallpaper, self.cf, self.cf.selected_monitor, self.txt, self.missing_backends)
self.cf.save()


Expand Down Expand Up @@ -506,8 +498,7 @@ def on_key_pressed(self, widget, event):
self.cf.selected_wallpaper = wallpaper_path
print(self.txt.msg_path, self.cf.selected_wallpaper)
self.cf.fill_option = self.fill_option_combo.get_active_text() or self.cf.fill_option
change_wallpaper(self.cf.selected_wallpaper, self.cf.fill_option, self.cf.color,
self.cf.backend, self.cf.selected_monitor, self.cf.swww_transition, self.txt)
change_wallpaper(self.cf.selected_wallpaper, self.cf, self.cf.selected_monitor, self.txt, self.missing_backends)
self.cf.save()

# Prevent other default key handling:
Expand Down
24 changes: 17 additions & 7 deletions waypaper/changer.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,17 @@
import subprocess
import time

from waypaper.options import BACKEND_OPTIONS

def change_wallpaper(image_path, fill_option, color, backend, monitor, transition, txt):

def change_wallpaper(image_path, cf, monitor, txt, missing_backends):
"""Run a system command to change the wallpaper depending on the backend"""

fill_option = cf.fill_option
color = cf.color
backend = cf.backend
swww_transition = cf.swww_transition

try:
# swaybg backend:
if backend == "swaybg":
Expand Down Expand Up @@ -34,16 +41,19 @@ def change_wallpaper(image_path, fill_option, color, backend, monitor, transitio
"tile": "no",
}
fill = fill_types[fill_option.lower()]
try:
subprocess.Popen(["killall", "swaybg"])
time.sleep(0.005)
except Exception as e:
print(f"{ERR_KILL} {e}")
is_swaybg_installed = not missing_backends[BACKEND_OPTIONS.index("swaybg")]
if is_swaybg_installed:
try:
subprocess.Popen(["killall", "swaybg"])
time.sleep(0.005)
except Exception as e:
print(f"{ERR_KILL} {e}")
print(missing_backends)
subprocess.Popen(["swww", "init"])
command = ["swww", "img", image_path]
command.extend(["--resize", fill])
command.extend(["--fill-color", color])
command.extend(["--transition-type", transition])
command.extend(["--transition-type", swww_transition])
# command.extend(["--transition-step", str(30)])
if monitor != "All":
command.extend(["--outputs", monitor])
Expand Down
14 changes: 13 additions & 1 deletion waypaper/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

import os
import random
import shutil

from waypaper.options import IMAGE_EXTENSIONS
from waypaper.options import IMAGE_EXTENSIONS, BACKEND_OPTIONS

def has_image_extension(file_path, backend):
"""Check if the file has image extension"""
Expand Down Expand Up @@ -35,3 +36,14 @@ def get_random_file(folder, include_subfolders):
return random.choice(image_paths)
except:
return None


def check_missing_backends():
"""Check which backends are installed in the system"""
missing_backends = []
for backend in BACKEND_OPTIONS:
if backend == "wallutils":
backend = "setwallpaper"
is_backend_missing = not bool(shutil.which(backend))
missing_backends.append(is_backend_missing)
return missing_backends

0 comments on commit 7d912f4

Please sign in to comment.