Calculating dynamic background gradient, option to stretch image

This commit is contained in:
2024-01-14 15:32:45 +01:00
parent 66e4023b7a
commit 20223b21de
6 changed files with 102 additions and 47 deletions

View File

@@ -1,8 +1,8 @@
from enum import Enum
class PrefenceEnums(enumerate):
EXPAND_OVER_ALL_DISPLAY = "expand_over_all_displays"
SHOW_ON_LOCK_SCREEN = "show_on_lock_screen"
PICTURE_ASPECT = "picture_aspect"
DYNAMIC_BACKGROUND_COLOR = "dynamic_background_color"
# Which type of image source will be used
# image_set, heic_file, source_folder

View File

@@ -59,6 +59,8 @@ CinnamonDynamicWallpaperExtension.prototype = {
_init: function(uuid) {
this.settings = new Settings.ExtensionSettings(this, uuid);
this.bindSettings("expand_over_all_displays", this.settingsUpdated)
// Check for the first startup
if (this.settings.getValue("first_start")) {

View File

@@ -6,10 +6,10 @@ from datetime import datetime, time
from enums.PreferenceEnums import *
from enums.PeriodSourceEnum import *
from scripts.location import *
import gi
from gi.repository import Gio
from PIL import Image
prefs = Cinnamon_Pref_Handler()
suntimes = Suntimes()
location_thread = Location()
@@ -17,8 +17,10 @@ background_settings = Gio.Settings.new("org.cinnamon.desktop.background")
class Loop():
def __init__(self) -> None:
self.prefs = Cinnamon_Pref_Handler()
# Position should estimate by network
if prefs.prefs[PrefenceEnums.PERIOD_SOURCE] == PeriodSourceEnum.NETWORKLOCATION:
if self.prefs.prefs[PrefenceEnums.PERIOD_SOURCE] == PeriodSourceEnum.NETWORKLOCATION:
location_thread.start()
location_thread.join()
@@ -28,8 +30,8 @@ class Loop():
self.start_times = suntimes.day_periods
# Position is given by user
elif prefs.prefs[PrefenceEnums.PERIOD_SOURCE] == PeriodSourceEnum.CUSTOMLOCATION:
suntimes.calc_suntimes(float(prefs.prefs[PrefenceEnums.LATITUDE_CUSTOM]), float(prefs.prefs[PrefenceEnums.LONGITUDE_CUSTOM]))
elif self.prefs.prefs[PrefenceEnums.PERIOD_SOURCE] == PeriodSourceEnum.CUSTOMLOCATION:
suntimes.calc_suntimes(float(self.prefs.prefs[PrefenceEnums.LATITUDE_CUSTOM]), float(self.prefs.prefs[PrefenceEnums.LONGITUDE_CUSTOM]))
self.start_times = suntimes.day_periods
# No position, concrete times
@@ -41,32 +43,65 @@ class Loop():
return time(hour=int(hour), minute=int(minute))
self.start_times = [
string_to_time_converter(prefs.prefs[PrefenceEnums.PERIOD_0_STARTTIME]),
string_to_time_converter(prefs.prefs[PrefenceEnums.PERIOD_1_STARTTIME]),
string_to_time_converter(prefs.prefs[PrefenceEnums.PERIOD_2_STARTTIME]),
string_to_time_converter(prefs.prefs[PrefenceEnums.PERIOD_3_STARTTIME]),
string_to_time_converter(prefs.prefs[PrefenceEnums.PERIOD_4_STARTTIME]),
string_to_time_converter(prefs.prefs[PrefenceEnums.PERIOD_5_STARTTIME]),
string_to_time_converter(prefs.prefs[PrefenceEnums.PERIOD_6_STARTTIME]),
string_to_time_converter(prefs.prefs[PrefenceEnums.PERIOD_7_STARTTIME]),
string_to_time_converter(prefs.prefs[PrefenceEnums.PERIOD_8_STARTTIME]),
string_to_time_converter(prefs.prefs[PrefenceEnums.PERIOD_9_STARTTIME])
string_to_time_converter(self.prefs.prefs[PrefenceEnums.PERIOD_0_STARTTIME]),
string_to_time_converter(self.prefs.prefs[PrefenceEnums.PERIOD_1_STARTTIME]),
string_to_time_converter(self.prefs.prefs[PrefenceEnums.PERIOD_2_STARTTIME]),
string_to_time_converter(self.prefs.prefs[PrefenceEnums.PERIOD_3_STARTTIME]),
string_to_time_converter(self.prefs.prefs[PrefenceEnums.PERIOD_4_STARTTIME]),
string_to_time_converter(self.prefs.prefs[PrefenceEnums.PERIOD_5_STARTTIME]),
string_to_time_converter(self.prefs.prefs[PrefenceEnums.PERIOD_6_STARTTIME]),
string_to_time_converter(self.prefs.prefs[PrefenceEnums.PERIOD_7_STARTTIME]),
string_to_time_converter(self.prefs.prefs[PrefenceEnums.PERIOD_8_STARTTIME]),
string_to_time_converter(self.prefs.prefs[PrefenceEnums.PERIOD_9_STARTTIME])
]
def exchange_image(self):
""" Replace the desktop image
"""
# Get the time of day
time_now = time(datetime.now().hour, datetime.now().minute)
# Assign the last image as fallback
self.current_image_uri = self.prefs.prefs[PrefenceEnums.SOURCE_FOLDER] + self.prefs.prefs[PrefenceEnums.PERIOD_9_IMAGE]
for i in range(0, 9):
# Replace the image URI, if it's not the last time period of the day
if self.start_times[i] <= time_now and time_now < self.start_times[i + 1]:
background_settings['picture-uri'] = "file://" + prefs.prefs[PrefenceEnums.SOURCE_FOLDER] + prefs.prefs["period_%d_image" % (i)]
return
self.current_image_uri = self.prefs.prefs[PrefenceEnums.SOURCE_FOLDER] + self.prefs.prefs["period_%d_image" % (i)]
break
# Set the background
background_settings['picture-uri'] = "file://" + self.current_image_uri
background_settings['picture-uri'] = "file://" + prefs.prefs[PrefenceEnums.SOURCE_FOLDER] + prefs.prefs[PrefenceEnums.PERIOD_9_IMAGE]
# Set background stretching
background_settings['picture-options'] = self.prefs.prefs[PrefenceEnums.PICTURE_ASPECT]
if self.prefs.prefs[PrefenceEnums.DYNAMIC_BACKGROUND_COLOR]:
self.set_background_gradient()
def set_background_gradient(self):
""" Setting a gradient background to hide images, which are not high enough
"""
# Load the image
im = Image.open(self.current_image_uri)
pix = im.load()
# Width and height of the current setted image
width, height = im.size
# Color of the top and bottom pixel in the middle of the image
top_color = pix[width / 2,0]
bottom_color = pix[width / 2, height - 1]
# Create the gradient
background_settings['color-shading-type'] = "vertical"
background_settings['primary-color'] = f"#{top_color[0]:x}{top_color[1]:x}{top_color[2]:x}"
background_settings['secondary-color'] = f"#{bottom_color[0]:x}{bottom_color[1]:x}{bottom_color[2]:x}"
# Needed for JavaScript
if __name__ == "__main__":
l = Loop()
l.exchange_image()

View File

@@ -2449,7 +2449,8 @@
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="halign">start</property>
<property name="label" translatable="yes">Expand image over all displays</property>
<property name="hexpand">True</property>
<property name="label" translatable="yes">Picture aspect</property>
</object>
<packing>
<property name="expand">False</property>
@@ -2458,13 +2459,13 @@
</packing>
</child>
<child>
<object class="GtkSwitch" id="sw_expand_over_all_displays">
<object class="GtkComboBox" id="cb_picture_aspect">
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="halign">end</property>
<property name="can-focus">False</property>
<signal name="changed" handler="on_cb_picture_aspect_changed" swapped="no"/>
</object>
<packing>
<property name="expand">True</property>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
@@ -2492,7 +2493,8 @@
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="halign">start</property>
<property name="label" translatable="yes">Show image on Lock Screen</property>
<property name="hexpand">True</property>
<property name="label" translatable="yes">Dynamic Background color</property>
</object>
<packing>
<property name="expand">False</property>
@@ -2501,13 +2503,13 @@
</packing>
</child>
<child>
<object class="GtkSwitch" id="sw_show_on_lock_screen">
<object class="GtkSwitch" id="sw_dynamic_background_color">
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="halign">end</property>
<signal name="state-set" handler="on_sw_dynamic_background_color_state_set" swapped="no"/>
</object>
<packing>
<property name="expand">True</property>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>

View File

@@ -11,6 +11,7 @@ from scripts.images import *
from enums.PreferenceEnums import PrefenceEnums
from enums.ImageSourceEnum import ImageSourceEnum
from enums.PeriodSourceEnum import PeriodSourceEnum
from loop import *
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk, GdkPixbuf
@@ -26,7 +27,7 @@ class Preferences:
"""
############################################################
# Lifecycle #
# Lifecycle #
############################################################
def __init__(self) -> None:
@@ -60,8 +61,6 @@ class Preferences:
# Time bar chart
self.img_bar_images = self.builder.get_object("img_bar_images")
self.sw_expand_over_all_displays = self.builder.get_object("sw_expand_over_all_displays")
self.sw_show_on_lock_screen = self.builder.get_object("sw_show_on_lock_screen")
self.etr_periods = [
self.builder.get_object("etr_period_1"), self.builder.get_object("etr_period_2"),
self.builder.get_object("etr_period_3"), self.builder.get_object("etr_period_4"),
@@ -133,6 +132,11 @@ class Preferences:
]
# Page 3: Behaviour
self.cb_picture_aspect: Gtk.ComboBox = self.builder.get_object("cb_picture_aspect")
self.sw_dynamic_background_color: Gtk.Switch = self.builder.get_object("sw_dynamic_background_color")
def show(self):
""" Display the window to the screen
"""
@@ -148,18 +152,12 @@ class Preferences:
elif self.c_prefs.prefs[PrefenceEnums.IMAGE_SOURCE] == ImageSourceEnum.SOURCEFOLDER:
self.tb_source_folder.set_active(True)
# image_set_choices = ["aurora", "beach", "bitday", "cliffs", "gradient", "lakeside", "mountains", "sahara"]
# self.add_items_to_combo_box(self.cb_image_set, image_set_choices)
# self.set_active_combobox_item(self.cb_image_set, self.c_prefs.prefs[PrefenceEnums.SELECTED_IMAGE_SET])
picture_aspects = ["mosaic", "centered", "scaled", "stretched", "zoom", "spanned"]
self.add_items_to_combo_box(self.cb_picture_aspect, picture_aspects)
self.set_active_combobox_item(self.cb_picture_aspect, self.c_prefs.prefs[PrefenceEnums.PICTURE_ASPECT])
# for i, combobox in enumerate(self.cb_periods):
# selected_image_name = self.c_prefs.prefs["period_%s_image" % (i)]
# self.set_active_combobox_item(combobox, selected_image_name)
self.sw_expand_over_all_displays.set_active(self.c_prefs.prefs[PrefenceEnums.EXPAND_OVER_ALL_DISPLAY])
self.sw_show_on_lock_screen.set_active(self.c_prefs.prefs[PrefenceEnums.SHOW_ON_LOCK_SCREEN])
self.sw_dynamic_background_color.set_active(self.c_prefs.prefs[PrefenceEnums.DYNAMIC_BACKGROUND_COLOR])
if self.c_prefs.prefs[PrefenceEnums.PERIOD_SOURCE] == PeriodSourceEnum.NETWORKLOCATION:
@@ -583,6 +581,20 @@ class Preferences:
pass
# Behaviour
def on_cb_picture_aspect_changed(self, combobox: Gtk.ComboBox):
tree_iter = combobox.get_active_iter()
if tree_iter is not None:
model = combobox.get_model()
self.c_prefs.prefs[PrefenceEnums.PICTURE_ASPECT] = model[tree_iter][0]
print(self.c_prefs.prefs[PrefenceEnums.PICTURE_ASPECT])
def on_sw_dynamic_background_color_state_set(self, switch: Gtk.Switch, state):
self.c_prefs.prefs[PrefenceEnums.DYNAMIC_BACKGROUND_COLOR] = state
# About
def on_cinnamon_spices_website_button_clicked(self, button: Gtk.Button):
@@ -618,6 +630,10 @@ class Preferences:
# Store all values to the JSON file
self.c_prefs.store_preferences()
# Use the new settings
loop = Loop()
loop.exchange_image()
# Close the window
self.on_destroy()

View File

@@ -3,13 +3,13 @@
"type": "generic",
"default": true
},
"expand_over_all_displays": {
"picture_aspect": {
"type": "generic",
"default": "false"
"default": "Scaled"
},
"show_on_lock_screen": {
"dynamic_background_color": {
"type": "generic",
"default": "false"
"default": true
},
"image_source": {
"type": "generic",