Use dictionary for selection of theme

This commit is contained in:
Admar Schoonen 2023-04-16 22:14:10 +02:00
parent a7efef65c8
commit d418cbdffb
3 changed files with 108 additions and 73 deletions

View File

@ -228,56 +228,9 @@
BoxLayout:
orientation: "vertical"
pos_hint: {"center_x":0.5, "center_y":0.5}
id: settings_menu_theme_boxlayout
Label:
text: "Select theme"
font_size: root.height*0.05
color: root.theme.color_font
id: settings_menu_theme_label_top
GridLayout:
cols: 2
CheckBox:
group: "settings_menu_theme"
on_active: root.settings_menu_theme_cb(self, self.active, "Dark")
id: settings_menu_theme_Dark
Label:
text: "Dark"
halign: "left"
valign: "middle"
text_size: self.size
font_size: root.height*0.05
color: root.theme.color_font
active: True
id: settings_menu_theme_label_0
CheckBox:
group: "settings_menu_theme"
on_active: root.settings_menu_theme_cb(self, self.active, "Light")
id: settings_menu_theme_Light
Label:
text: "Light"
halign: "left"
valign: "middle"
text_size: self.size
font_size: root.height*0.05
color: root.theme.color_font
id: settings_menu_theme_label_1
CheckBox:
group: "settings_menu_theme"
on_active: root.settings_menu_theme_cb(self, self.active, "Automatic")
id: settings_menu_theme_Automatic
Label:
text: "Automatic"
halign: "left"
valign: "middle"
text_size: self.size
font_size: root.height*0.05
color: root.theme.color_font
id: settings_menu_theme_label_Automatic
id: settings_menu_theme_label_2
Button:
id: settings_menu_theme_Ok_button
on_press: root.settings_menu_theme_Ok_button_cb()
text: "Ok"
font_size: root.height*0.05
color: root.theme.color_font
background_normal: ''
background_color: root.theme.color_button

126
clock.py
View File

@ -10,6 +10,7 @@ from kivy.config import Config
from kivy.properties import ObjectProperty
import time
from sounds import WakeUpSounds
from themes import Themes
def is_arm():
if (os.uname()[4][:3] == 'arm') or (os.uname()[4][:7] == 'aarch64'):
@ -235,7 +236,68 @@ class MyClockWidget(FloatLayout):
is_arm = is_arm()
def add_wake_up_sound(self):
def add_themes(self):
x = self.ids["settings_menu_theme_boxlayout"]
gl = GridLayout(
cols=2,
)
self.themes = Themes
self.theme_selected = next(iter(self.themes))
self.ids["settings_menu_theme_select_button"].text = self.theme_selected
self.theme_checkboxes = []
self.theme_labels = []
i = 0
for w in self.themes:
c = CheckBox(
group = "settings_menu_theme",
size = [gl.size[0] * 0.1, gl.size[1]],
)
c.bind(active=self.settings_menu_theme_cb)
gl.add_widget(c)
self.theme_checkboxes.append(c)
if i == 0:
a = True
else:
a = False
l = Label(
text = w,
halign = "left",
valign = "middle",
size = [Window.size[0] * 0.3, Window.size[1]],
text_size = Window.size,
font_size = Window.height*0.05,
color = self.theme.color_font,
)
l.text_size = l.size
gl.add_widget(l)
self.theme_labels.append(l)
i = i + 1
x.add_widget(gl)
b = Button(
text = "Ok",
font_size = self.height*0.3,
color = self.theme.color_font,
background_normal = '',
background_color = self.theme.color_button
)
b.bind(on_press = self.settings_menu_theme_Ok_button_cb)
x.add_widget(b)
self.settings_menu_theme_Ok_button = b
if self.theme_selected == "Automatic":
self.theme = Theme("Light")
else:
self.theme = Theme(self.theme_selected)
def add_wake_up_sounds(self):
alarm_settings = App.get_running_app().alarm_settings
x = self.ids["settings_menu_wake_up_sound_boxlayout"]
@ -275,7 +337,6 @@ class MyClockWidget(FloatLayout):
text_size = Window.size,
font_size = Window.height*0.05,
color = self.theme.color_font,
active = a
)
l.text_size = l.size
gl.add_widget(l)
@ -687,11 +748,10 @@ class MyClockWidget(FloatLayout):
self.draw_list_curr_frame.append(["Color", background.canvas, rgb[0], rgb[1], rgb[2]])
self.draw_list_curr_frame.append(["Rectangle", background.canvas, background.size, background.pos, ""])
for s in ["label_top", "label_0", "label_1", "label_2", "Ok_button"]:
i = self.ids["settings_menu_theme_" + s]
for i in self.theme_labels:
i.color = self.theme.color_font
i = self.ids["settings_menu_theme_Ok_button"]
i.background_color = self.theme.color_button
self.settings_menu_theme_Ok_button.background_color = self.theme.color_button
def update_settings(self):
if self.view == "settings_menu":
@ -838,34 +898,48 @@ class MyClockWidget(FloatLayout):
alarm_settings.sound_source = self.wake_up_sounds[sound]
alarm_settings.sound_selected = sound
def settings_menu_theme_select_button_cb(self):
self.settings_menu_wake_up_theme_button_cb_hack = True
alarm_settings = App.get_running_app().alarm_settings
self.ids["settings_menu_theme_Dark"].active = False
self.ids["settings_menu_theme_Light"].active = False
print("theme selected: " + self.theme.name)
if self.theme.name == "Dark":
self.ids["settings_menu_theme_Dark"].active = True
elif self.theme.name == "Light":
self.ids["settings_menu_theme_Light"].active = True
def settings_menu_theme_select_button_cb(self):
self.settings_menu_theme_select_button_cb_hack = True
for c in self.theme_checkboxes:
c.active = False
print("theme selected: " + self.theme_selected)
n = 0
print("self.themes: " + str(self.themes))
for i in self.themes:
if i == self.theme_selected:
self.theme_checkboxes[n].active = True
n = n + 1
self.view = "settings_menu_theme"
self.view_active_since = time.time()
def settings_menu_theme_Ok_button_cb(self):
alarm_settings = App.get_running_app().alarm_settings
self.ids["settings_menu_theme_select_button"].text = self.theme.name
def settings_menu_theme_Ok_button_cb(self, event):
self.ids["settings_menu_theme_select_button"].text = self.theme_selected
self.view = "settings_menu"
self.view_active_since = time.time()
def settings_menu_theme_cb(self, instance, value, theme):
alarm_settings = App.get_running_app().alarm_settings
def settings_menu_theme_cb(self, instance, value):
n = 0
for c in self.theme_checkboxes:
if c == instance:
break
n = n + 1
k = 0
for theme in self.themes:
if k == n:
break
k = k + 1
if self.settings_menu_theme_select_button_cb_hack:
self.settings_menu_theme_select_button_cb_hack = False
if not (self.theme.name == "" and theme != ""):
if not (self.theme_selected == "" and theme != ""):
return
if value == True:
@ -874,6 +948,7 @@ class MyClockWidget(FloatLayout):
print("You deselected " + theme)
self.theme_selected = theme
if theme == "Automatic":
self.theme = Theme("Light")
else:
@ -1226,7 +1301,8 @@ class MyApp(App):
def build(self):
clock_widget = MyClockWidget()
clock_widget.add_wake_up_sound()
clock_widget.add_wake_up_sounds()
clock_widget.add_themes()
update_rate = App.get_running_app().update_rate
# update initially, just after construction of the widget is complete

6
themes.py Normal file
View File

@ -0,0 +1,6 @@
Themes = {
"Light": "Woodpecker Chirps - QuickSounds.com.mp3",
"Dark": "https://icecast.omroep.nl/radio1-bb-mp3",
"Automatic": "https://icecast-qmusicnl-cdp.triple-it.nl/Qmusic_nl_nonstop_high.aac?aw_0_1st.playerId=redirect",
}