Skip to content

Commit 5438bd7

Browse files
authored
Handle typed user input into the night light module (manual schedule) (#12826)
1 parent a165975 commit 5438bd7

File tree

1 file changed

+37
-2
lines changed

1 file changed

+37
-2
lines changed

files/usr/share/cinnamon/cinnamon-settings/modules/cs_nightlight.py

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,46 @@ def __init__(self, *args, **kwargs):
8585
self.props.width_chars = 10
8686
self.props.update_policy=Gtk.SpinButtonUpdatePolicy.IF_VALID
8787

88-
interface_settings = Gio.Settings(schema_id="org.cinnamon.desktop.interface")
89-
self.locale_format = "%R" if interface_settings.get_boolean("clock-use-24h") else "%I:%M %p"
88+
self.interface_settings = Gio.Settings(schema_id="org.cinnamon.desktop.interface")
89+
self.locale_format = "%R" if self.interface_settings.get_boolean("clock-use-24h") else "%I:%M %p"
9090

91+
self.connect("focus-out-event", self.label_to_value)
9192
self.formatter_handle = self.connect("output", self.value_to_label)
9293

94+
def label_to_value(self, spinbutton, data=None):
95+
text = self.get_text()
96+
if ":" in text:
97+
adjustment = self.get_adjustment()
98+
input_hours, input_minutes = self.input_to_frac(text)
99+
if input_hours is not None and input_minutes is not None:
100+
adjustment.set_value(float(input_hours + input_minutes/60))
101+
t = GLib.DateTime.new_local(2000, 1, 1, input_hours, input_minutes, 0)
102+
self.set_text(t.format(self.locale_format))
103+
return 0 # Prevents setting night light to minimum value
104+
105+
def input_to_frac(self, value):
106+
pm = False
107+
if self.interface_settings.get_boolean("clock-use-24h"):
108+
hours, minutes = value.split(":")
109+
else:
110+
if " AM" in value or " PM" in value:
111+
h_m, meridian = value.split(" ")
112+
pm = True if "PM" in meridian else False
113+
hours, minutes = h_m.split(":")
114+
else:
115+
hours, minutes = value.split(":")
116+
if hours.isnumeric() and minutes.isnumeric():
117+
hours, minutes = abs(int(hours)), abs(int(minutes))
118+
if pm:
119+
hours += 12
120+
if hours > 23:
121+
remainder = hours % 24
122+
hours = remainder
123+
remainder = minutes % 60
124+
minutes = remainder if minutes % 15 == 0 else 0
125+
return hours, minutes
126+
return None, None
127+
93128
def value_to_label(self, spinbutton, data=None):
94129
adjustment = self.get_adjustment()
95130
value = adjustment.get_value()

0 commit comments

Comments
 (0)