Time Chooser: Follow system clock format
This commit is contained in:
+106
-27
@@ -4,37 +4,116 @@ SPDX-FileCopyrightText: Night Theme Switcher Contributors
|
|||||||
SPDX-License-Identifier: GPL-3.0-or-later
|
SPDX-License-Identifier: GPL-3.0-or-later
|
||||||
-->
|
-->
|
||||||
<interface domain="nightthemeswitcher@romainvigier.fr">
|
<interface domain="nightthemeswitcher@romainvigier.fr">
|
||||||
<template class="TimeChooser" parent="GtkBox">
|
<template class="TimeChooser" parent="GtkWidget">
|
||||||
<property name="orientation">horizontal</property>
|
<property name="layout-manager">
|
||||||
<property name="spacing">8</property>
|
<object class="GtkBinLayout"/>
|
||||||
<signal name="notify::time" handler="onTimeChanged" swapped="no"/>
|
</property>
|
||||||
|
<signal name="notify::time" handler="onTimeChanged"/>
|
||||||
<child>
|
<child>
|
||||||
<object class="GtkSpinButton" id="hours">
|
<object class="GtkStack" id="clock_format_stack">
|
||||||
<property name="orientation">vertical</property>
|
<child>
|
||||||
<property name="numeric">True</property>
|
<object class="GtkStackPage">
|
||||||
<property name="wrap">True</property>
|
<property name="name">12h</property>
|
||||||
<property name="adjustment">hours_adjustment</property>
|
<property name="child">
|
||||||
<signal name="value-changed" handler="onValueChanged" swapped="no"/>
|
<object class="GtkBox">
|
||||||
<signal name="output" handler="onOutputChanged" swapped="no"/>
|
<property name="spacing">8</property>
|
||||||
</object>
|
<property name="halign">end</property>
|
||||||
</child>
|
<child>
|
||||||
<child>
|
<object class="GtkSpinButton" id="hours_12">
|
||||||
<object class="GtkLabel">
|
<property name="orientation">vertical</property>
|
||||||
<property name="label" translatable="yes" comments="Time separator (eg. 08:27)">:</property>
|
<property name="numeric">True</property>
|
||||||
</object>
|
<property name="wrap">True</property>
|
||||||
</child>
|
<property name="adjustment">hours_12_adjustment</property>
|
||||||
<child>
|
<signal name="value-changed" handler="onValueChanged"/>
|
||||||
<object class="GtkSpinButton" id="minutes">
|
<signal name="output" handler="onOutputChanged"/>
|
||||||
<property name="orientation">vertical</property>
|
</object>
|
||||||
<property name="numeric">True</property>
|
</child>
|
||||||
<property name="wrap">True</property>
|
<child>
|
||||||
<property name="adjustment">minutes_adjustment</property>
|
<object class="GtkLabel">
|
||||||
<signal name="value-changed" handler="onValueChanged" swapped="no"/>
|
<property name="label" translatable="yes" comments="Time separator (eg. 08:27)">:</property>
|
||||||
<signal name="output" handler="onOutputChanged" swapped="no"/>
|
</object>
|
||||||
|
</child>
|
||||||
|
<child>
|
||||||
|
<object class="GtkSpinButton" id="minutes_12">
|
||||||
|
<property name="orientation">vertical</property>
|
||||||
|
<property name="numeric">True</property>
|
||||||
|
<property name="wrap">True</property>
|
||||||
|
<property name="adjustment">minutes_adjustment</property>
|
||||||
|
<signal name="value-changed" handler="onValueChanged"/>
|
||||||
|
<signal name="output" handler="onOutputChanged"/>
|
||||||
|
</object>
|
||||||
|
</child>
|
||||||
|
<child>
|
||||||
|
<object class="GtkBox">
|
||||||
|
<property name="orientation">vertical</property>
|
||||||
|
<property name="valign">center</property>
|
||||||
|
<child>
|
||||||
|
<object class="GtkToggleButton" id="am_toggle_button">
|
||||||
|
<property name="label" translatable="yes">AM</property>
|
||||||
|
<signal name="toggled" handler="onValueChanged"/>
|
||||||
|
</object>
|
||||||
|
</child>
|
||||||
|
<child>
|
||||||
|
<object class="GtkToggleButton" id="pm_toggle_button">
|
||||||
|
<property name="label" translatable="yes">PM</property>
|
||||||
|
<property name="group">am_toggle_button</property>
|
||||||
|
<signal name="toggled" handler="onValueChanged"/>
|
||||||
|
</object>
|
||||||
|
</child>
|
||||||
|
<style>
|
||||||
|
<class name="linked"/>
|
||||||
|
</style>
|
||||||
|
</object>
|
||||||
|
</child>
|
||||||
|
</object>
|
||||||
|
</property>
|
||||||
|
</object>
|
||||||
|
</child>
|
||||||
|
<child>
|
||||||
|
<object class="GtkStackPage">
|
||||||
|
<property name="name">24h</property>
|
||||||
|
<property name="child">
|
||||||
|
<object class="GtkBox">
|
||||||
|
<property name="spacing">8</property>
|
||||||
|
<property name="halign">end</property>
|
||||||
|
<child>
|
||||||
|
<object class="GtkSpinButton" id="hours_24">
|
||||||
|
<property name="orientation">vertical</property>
|
||||||
|
<property name="numeric">True</property>
|
||||||
|
<property name="wrap">True</property>
|
||||||
|
<property name="adjustment">hours_24_adjustment</property>
|
||||||
|
<signal name="value-changed" handler="onValueChanged"/>
|
||||||
|
<signal name="output" handler="onOutputChanged"/>
|
||||||
|
</object>
|
||||||
|
</child>
|
||||||
|
<child>
|
||||||
|
<object class="GtkLabel">
|
||||||
|
<property name="label" translatable="yes" comments="Time separator (eg. 08:27)">:</property>
|
||||||
|
</object>
|
||||||
|
</child>
|
||||||
|
<child>
|
||||||
|
<object class="GtkSpinButton" id="minutes_24">
|
||||||
|
<property name="orientation">vertical</property>
|
||||||
|
<property name="numeric">True</property>
|
||||||
|
<property name="wrap">True</property>
|
||||||
|
<property name="adjustment">minutes_adjustment</property>
|
||||||
|
<signal name="value-changed" handler="onValueChanged"/>
|
||||||
|
<signal name="output" handler="onOutputChanged"/>
|
||||||
|
</object>
|
||||||
|
</child>
|
||||||
|
</object>
|
||||||
|
</property>
|
||||||
|
</object>
|
||||||
|
</child>
|
||||||
</object>
|
</object>
|
||||||
</child>
|
</child>
|
||||||
</template>
|
</template>
|
||||||
<object class="GtkAdjustment" id="hours_adjustment">
|
<object class="GtkAdjustment" id="hours_12_adjustment">
|
||||||
|
<property name="lower">0</property>
|
||||||
|
<property name="upper">11</property>
|
||||||
|
<property name="step-increment">1</property>
|
||||||
|
</object>
|
||||||
|
<object class="GtkAdjustment" id="hours_24_adjustment">
|
||||||
<property name="lower">0</property>
|
<property name="lower">0</property>
|
||||||
<property name="upper">23</property>
|
<property name="upper">23</property>
|
||||||
<property name="step-increment">1</property>
|
<property name="step-increment">1</property>
|
||||||
|
|||||||
@@ -1,16 +1,21 @@
|
|||||||
// SPDX-FileCopyrightText: Night Theme Switcher Contributors
|
// SPDX-FileCopyrightText: Night Theme Switcher Contributors
|
||||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||||
|
|
||||||
|
import GDesktopEnums from 'gi://GDesktopEnums';
|
||||||
|
import Gio from 'gi://Gio';
|
||||||
import GObject from 'gi://GObject';
|
import GObject from 'gi://GObject';
|
||||||
import Gtk from 'gi://Gtk';
|
import Gtk from 'gi://Gtk';
|
||||||
|
|
||||||
|
|
||||||
export class TimeChooser extends Gtk.Box {
|
export class TimeChooser extends Gtk.Widget {
|
||||||
|
#clockFormat;
|
||||||
|
#interfaceSettings;
|
||||||
|
|
||||||
static {
|
static {
|
||||||
GObject.registerClass({
|
GObject.registerClass({
|
||||||
GTypeName: 'TimeChooser',
|
GTypeName: 'TimeChooser',
|
||||||
Template: 'resource:///org/gnome/Shell/Extensions/nightthemeswitcher/preferences/ui/TimeChooser.ui',
|
Template: 'resource:///org/gnome/Shell/Extensions/nightthemeswitcher/preferences/ui/TimeChooser.ui',
|
||||||
InternalChildren: ['hours', 'minutes'],
|
InternalChildren: ['clock_format_stack', 'hours_12', 'minutes_12', 'hours_24', 'minutes_24', 'am_toggle_button', 'pm_toggle_button'],
|
||||||
Properties: {
|
Properties: {
|
||||||
time: GObject.ParamSpec.double(
|
time: GObject.ParamSpec.double(
|
||||||
'time',
|
'time',
|
||||||
@@ -25,17 +30,64 @@ export class TimeChooser extends Gtk.Box {
|
|||||||
}, this);
|
}, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
onTimeChanged(chooser) {
|
constructor({ ...params } = {}) {
|
||||||
const hours = Math.trunc(chooser.time);
|
super(params);
|
||||||
const minutes = Math.round((chooser.time - hours) * 60);
|
this.#interfaceSettings = new Gio.Settings({ schema: 'org.gnome.desktop.interface' });
|
||||||
chooser._hours.value = hours;
|
this.#interfaceSettings.connect('changed::clock-format', this.#onClockFormatChanged.bind(this));
|
||||||
chooser._minutes.value = minutes;
|
this.#onClockFormatChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
onValueChanged(_spin) {
|
#onClockFormatChanged(_settings) {
|
||||||
const hours = this._hours.value;
|
this.#clockFormat = this.#interfaceSettings.get_enum('clock-format');
|
||||||
const minutes = this._minutes.value / 60;
|
this.#syncClockFormatStack();
|
||||||
this.time = hours + minutes;
|
}
|
||||||
|
|
||||||
|
#syncClockFormatStack() {
|
||||||
|
this._clock_format_stack.set_visible_child_name(this.#clockFormat === GDesktopEnums.ClockFormat['12H'] ? '12h' : '24h');
|
||||||
|
}
|
||||||
|
|
||||||
|
#convertTimeTo24hFormat(time) {
|
||||||
|
const hours = Math.trunc(time);
|
||||||
|
const minutes = Math.round((time - hours) * 60);
|
||||||
|
return { hours, minutes };
|
||||||
|
}
|
||||||
|
|
||||||
|
#convertTimeTo12hFormat(time) {
|
||||||
|
const { hours: hours24, minutes } = this.#convertTimeTo24hFormat(time);
|
||||||
|
const hours = hours24 % 12;
|
||||||
|
const isPm = hours24 > 12;
|
||||||
|
return { hours, minutes, isPm };
|
||||||
|
}
|
||||||
|
|
||||||
|
#convert24hFormatToTime({ hours, minutes }) {
|
||||||
|
return hours + minutes / 60;
|
||||||
|
}
|
||||||
|
|
||||||
|
#convert12hFormatToTime({ hours, minutes, isPm }) {
|
||||||
|
return this.#convert24hFormatToTime({
|
||||||
|
hours: hours + Number(isPm) * 12,
|
||||||
|
minutes,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
onTimeChanged(_chooser) {
|
||||||
|
const time = this.time;
|
||||||
|
|
||||||
|
const clock12 = this.#convertTimeTo12hFormat(time);
|
||||||
|
this._hours_12.value = clock12.hours;
|
||||||
|
this._minutes_12.value = clock12.minutes;
|
||||||
|
this._am_toggle_button.active = !clock12.isPm;
|
||||||
|
this._pm_toggle_button.active = clock12.isPm;
|
||||||
|
|
||||||
|
const clock24 = this.#convertTimeTo24hFormat(time);
|
||||||
|
this._hours_24.value = clock24.hours;
|
||||||
|
this._minutes_24.value = clock24.minutes;
|
||||||
|
}
|
||||||
|
|
||||||
|
onValueChanged(_widget) {
|
||||||
|
this.time = this.#clockFormat === GDesktopEnums.ClockFormat['12H']
|
||||||
|
? this.#convert12hFormatToTime({ hours: this._hours_12.value, minutes: this._minutes_12.value, isPm: this._pm_toggle_button.active })
|
||||||
|
: this.#convert24hFormatToTime({ hours: this._hours_24.value, minutes: this._minutes_24.value });
|
||||||
}
|
}
|
||||||
|
|
||||||
onOutputChanged(spin) {
|
onOutputChanged(spin) {
|
||||||
|
|||||||
Reference in New Issue
Block a user