Skip to content

Commit

Permalink
Adding multiple rotation modes #46 #80
Browse files Browse the repository at this point in the history
This fundamentally changes the way the interval timer works. It is now used as
a base for all operations. The rotation modes themselves have been moved into
a UI file for easier updates.

This update adds the following list of rotation modes:
 * One Minute Interval
 * Five Minutes Interval
 * Thirty Minutes Interval
 * One Hour Interval
 * Six Hours Interval
 * Twelve Hours Interval
 * Twenty Four Hours Interval
 * Hourly - Changes at the beginning of each hour
 * Daily - Changes at the beginning of each day
 * Custom - Allows you to specify your own interval
 * Disabled - Completely disables automatic rotation
  • Loading branch information
BigE committed Aug 17, 2021
1 parent d403145 commit d59ad0f
Show file tree
Hide file tree
Showing 13 changed files with 268 additions and 98 deletions.
13 changes: 9 additions & 4 deletions [email protected]/_deskchanger.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
'use strict';

const Gio = imports.gi.Gio;
const GIRepository = imports.gi.GIRepository;
const GLib = imports.gi.GLib;
const GObject = imports.gi.GObject;
const {Gio, GIRepository, GLib, GObject, Gtk} = imports.gi;

String.prototype.format = imports.format.format;

Expand Down Expand Up @@ -304,3 +301,11 @@ deskchanger.dbusxml = deskchanger.get_resource(`${deskchanger.app_id}.xml`);
deskchanger.dbusinfo = Gio.DBusNodeInfo.new_for_xml(deskchanger.dbusxml);

deskchanger.dbusinfo.nodes.forEach(info => info.cache_build());


let builder = new Gtk.Builder();

builder.set_translation_domain('desk-changer');
builder.add_from_resource(`${deskchanger.app_path}/ui/rotation.ui`);

deskchanger.rotation = builder.get_object('rotation');
30 changes: 24 additions & 6 deletions [email protected]/daemon/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,14 +263,32 @@ class Server extends Gio.Application {
}

_create_timer() {
if (deskchanger.settings.rotation === 'interval') {
this._timer = new Timer.Interval(deskchanger.settings.interval, this.next.bind(this));
this._interval_changed_id = deskchanger.settings.connect('changed::interval', () => {
this._timer.destroy();
this._timer = new Timer.Interval(deskchanger.settings.interval, this.next.bind(this));
});
let interval,
rotation = deskchanger.settings.rotation,
[success, iterator] = deskchanger.rotation.get_iter_first();

while (success) {
if (deskchanger.rotation.get_value(iterator, 0) === rotation) {
interval = (rotation === 'interval')? deskchanger.settings.interval : deskchanger.rotation.get_value(iterator, 3);
rotation = deskchanger.rotation.get_value(iterator, 1);
break;
}

success = deskchanger.rotation.iter_next(iterator);
}

if (rotation === 'interval') {
this._timer = new Timer.Interval(interval, this.next.bind(this));
if (deskchanger.settings.rotation === 'interval') {
this._interval_changed_id = deskchanger.settings.connect('changed::interval', () => {
this._timer.destroy();
this._timer = new Timer.Interval(deskchanger.settings.interval, this.next.bind(this));
});
}
} else if (deskchanger.settings.rotation === 'hourly') {
this._timer = new Timer.Hourly(this.next.bind(this));
} else if (deskchanger.settings.rotation === 'daily') {
this._timer = new Timer.Daily(this.next.bind(this));
}
}

Expand Down
31 changes: 24 additions & 7 deletions [email protected]/daemon/timer.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,20 @@ var Interval = GObject.registerClass({
},
},
class DeskChangerTimerInterval extends GObject.Object {
_init(interval = 300, callback = null, params = {}) {
_init(interval, callback = null, params = {}) {
if (callback && typeof callback !== 'function') {
throw 'callback must be function';
}

if (interval < 1) {
if (!interval || interval < 1) {
throw 'invalid interval, must be 1 or higher';
}

this._callback = callback;
this._interval = parseInt(interval);
super._init(params);
this._timer = GLib.timeout_add_seconds(GLib.PRIORITY_DEFAULT, this._interval, this.__callback__.bind(this));
deskchanger.debug(`added timer ${this._timer}`);
deskchanger.debug(`added interval(${this._interval}) timer ${this._timer}`);
}

get callback() {
Expand All @@ -46,7 +46,7 @@ class DeskChangerTimerInterval extends GObject.Object {
}

destroy() {
deskchanger.debug(`removing timer ${this._timer}`);
deskchanger.debug(`removing interval timer ${this._timer}`);
GLib.source_remove(this._timer);
}
});
Expand All @@ -58,10 +58,16 @@ class DeskChangerTimerHourly extends Interval {
super._init(5, callback, params);
}

__callback__() {
let date = new Date();

_timer_check(date) {
if (date.getMinutes() === 0 && date.getSeconds() < 10) {
return true;
}

return false
}

__callback__() {
if (this._timer_check(new Date())) {
if (!this._done) {
this._done = true;
deskchanger.debug('calling hourly callback');
Expand All @@ -75,3 +81,14 @@ class DeskChangerTimerHourly extends Interval {
return true;
}
});

var Daily = GObject.registerClass(
class DeskChangerTimerDaily extends Hourly {
_timer_check(date) {
if (super._timer_check(date) && date.getHours() === 0) {
return true;
}

return false;
}
});
26 changes: 23 additions & 3 deletions [email protected]/extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,35 @@ function enable() {
});

rotation_id = deskchanger.settings.connect('changed::rotation', function () {
let message;
let message, interval,
rotation = deskchanger.settings.rotation,
[success, iterator] = deskchanger.rotation.get_iter_first();

while (success) {
if (deskchanger.rotation.get_value(iterator, 0) === rotation) {
if (rotation === 'interval') {
interval = `${deskchanger.rotation.get_value(iterator, 2)} of ${deskchanger.settings.interval} seconds`;
} else {
interval = deskchanger.rotation.get_value(iterator, 2);
}

rotation = deskchanger.rotation.get_value(iterator, 1);
break;
}

success = deskchanger.rotation.iter_next(iterator);
}

switch (deskchanger.settings.rotation) {
switch (rotation) {
case 'interval':
message = _('Rotation will occur every %d seconds'.format(deskchanger.settings.interval));
message = _(`Rotation will occur at a ${interval}`);
break;
case 'hourly':
message = _('Rotation will occur at the beginning of every hour');
break;
case 'daily':
message = _('Rotation will occur at the beginning of every day');
break;
default:
message = _('Rotation has been disabled');
break;
Expand Down
4 changes: 2 additions & 2 deletions [email protected]/prefs.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ const PrefsWidget = GObject.registerClass({
'keyboard',
'locations',
'profiles',
'rotation',
'spinner_interval',
'switch_auto_start',
'switch_daemon_state',
Expand Down Expand Up @@ -83,6 +82,7 @@ class PrefsWidget extends Gtk.Box {

// load everything else
this._allowed_mime_types.set_text(mime_types, mime_types.length);
this._combo_rotation_mode.set_model(deskchanger.rotation);
this._combo_rotation_mode.set_active_id(deskchanger.settings.rotation);
this._load_profiles();
this._switch_daemon_state.set_active(this._daemon.Running);
Expand Down Expand Up @@ -308,7 +308,7 @@ class PrefsWidget extends Gtk.Box {
let [ok, iterator] = this._combo_rotation_mode.get_active_iter();

if (this._is_init || !ok) return;
deskchanger.settings.rotation = this._rotation.get_value(iterator, 0);
deskchanger.settings.rotation = deskchanger.rotation.get_value(iterator, 0);
}

_on_response_add_items(_dialog, response)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
<file compressed="true">org.gnome.Shell.Extensions.DeskChanger.Daemon.service.in</file>
<file compressed="true">ui/prefs.3.ui</file>
<file compressed="true">ui/prefs.ui</file>
<file compressed="true">ui/rotation.ui</file>
</gresource>
</gresources>
25 changes: 2 additions & 23 deletions [email protected]/resources/ui/prefs.3.ui
Original file line number Diff line number Diff line change
Expand Up @@ -45,26 +45,6 @@
<column type="gchararray"/>
</columns>
</object>
<object class="GtkListStore" id="rotation">
<columns>
<column type="gchararray"/>
<column type="gchararray"/>
</columns>
<data>
<row>
<col id="0">interval</col>
<col id="1" translatable="yes">Interval Timer</col>
</row>
<row>
<col id="0" translatable="yes">hourly</col>
<col id="1" translatable="yes">Hourly Timer</col>
</row>
<row>
<col id="0" translatable="yes">disabled</col>
<col id="1" translatable="yes">Disabled</col>
</row>
</data>
</object>
<template class="PrefsWidget" parent="GtkBox">
<property name="orientation">vertical</property>
<child>
Expand Down Expand Up @@ -502,13 +482,12 @@
<object class="GtkComboBox" id="combo_rotation_mode">
<property name="can-focus">0</property>
<property name="halign">end</property>
<property name="model">rotation</property>
<property name="id-column">0</property>
<signal name="changed" handler="_on_combo_rotation_mode_changed" swapped="no"/>
<child>
<object class="GtkCellRendererText"/>
<attributes>
<attribute name="text">1</attribute>
<attribute name="text">2</attribute>
</attributes>
</child>
</object>
Expand All @@ -532,7 +511,7 @@
<object class="GtkLabel">
<property name="can-focus">0</property>
<property name="tooltip-text" translatable="yes">This only applies to the Interval Timer option and sets the interval at which the timer will change the wallpaper.</property>
<property name="label" translatable="yes">Interval Timer (in seconds)</property>
<property name="label" translatable="yes">Custom Timer (in seconds)</property>
</object>
<packing>
<property name="expand">False</property>
Expand Down
27 changes: 2 additions & 25 deletions [email protected]/resources/ui/prefs.glade.ui
Original file line number Diff line number Diff line change
Expand Up @@ -54,28 +54,6 @@
<column type="gchararray"/>
</columns>
</object>
<object class="GtkListStore" id="rotation">
<columns>
<!-- column-name key -->
<column type="gchararray"/>
<!-- column-name label -->
<column type="gchararray"/>
</columns>
<data>
<row>
<col id="0">interval</col>
<col id="1" translatable="yes">Interval Timer</col>
</row>
<row>
<col id="0" translatable="yes">hourly</col>
<col id="1" translatable="yes">Hourly Timer</col>
</row>
<row>
<col id="0" translatable="yes">disabled</col>
<col id="1" translatable="yes">Disabled</col>
</row>
</data>
</object>
<template class="PrefsWidget" parent="GtkBox">
<property name="visible">True</property>
<property name="can-focus">True</property>
Expand Down Expand Up @@ -568,13 +546,12 @@
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="halign">end</property>
<property name="model">rotation</property>
<property name="id-column">0</property>
<signal name="changed" handler="_on_combo_rotation_mode_changed" swapped="no"/>
<child>
<object class="GtkCellRendererText"/>
<attributes>
<attribute name="text">1</attribute>
<attribute name="text">2</attribute>
</attributes>
</child>
</object>
Expand All @@ -600,7 +577,7 @@
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="tooltip-text" translatable="yes">This only applies to the Interval Timer option and sets the interval at which the timer will change the wallpaper.</property>
<property name="label" translatable="yes">Interval Timer (in seconds)</property>
<property name="label" translatable="yes">Custom Timer (in seconds)</property>
</object>
<packing>
<property name="expand">False</property>
Expand Down
25 changes: 2 additions & 23 deletions [email protected]/resources/ui/prefs.ui
Original file line number Diff line number Diff line change
Expand Up @@ -45,26 +45,6 @@
<column type="gchararray"/>
</columns>
</object>
<object class="GtkListStore" id="rotation">
<columns>
<column type="gchararray"/>
<column type="gchararray"/>
</columns>
<data>
<row>
<col id="0">interval</col>
<col id="1" translatable="yes">Interval Timer</col>
</row>
<row>
<col id="0" translatable="yes">hourly</col>
<col id="1" translatable="yes">Hourly Timer</col>
</row>
<row>
<col id="0" translatable="yes">disabled</col>
<col id="1" translatable="yes">Disabled</col>
</row>
</data>
</object>
<template class="PrefsWidget" parent="GtkBox">
<property name="orientation">vertical</property>
<child>
Expand Down Expand Up @@ -396,13 +376,12 @@
<property name="hexpand">1</property>
<property name="can-focus">0</property>
<property name="halign">end</property>
<property name="model">rotation</property>
<property name="id-column">0</property>
<signal name="changed" handler="_on_combo_rotation_mode_changed" swapped="no"/>
<child>
<object class="GtkCellRendererText"/>
<attributes>
<attribute name="text">1</attribute>
<attribute name="text">2</attribute>
</attributes>
</child>
</object>
Expand All @@ -416,7 +395,7 @@
<object class="GtkLabel">
<property name="can-focus">0</property>
<property name="tooltip-text" translatable="yes">This only applies to the Interval Timer option and sets the interval at which the timer will change the wallpaper.</property>
<property name="label" translatable="yes">Interval Timer (in seconds)</property>
<property name="label" translatable="yes">Custom Timer (in seconds)</property>
</object>
</child>
<child>
Expand Down
Loading

0 comments on commit d59ad0f

Please sign in to comment.