|
| 1 | +const Applet = imports.ui.applet; |
| 2 | +const Mainloop = imports.mainloop; |
| 3 | + |
| 4 | +function MyApplet(metadata, orientation, panelHeight, instance_id) { |
| 5 | + this._init(metadata, orientation, panelHeight, instance_id); |
| 6 | +} |
| 7 | + |
| 8 | +MyApplet.prototype = { |
| 9 | + __proto__: Applet.TextIconApplet.prototype, |
| 10 | + |
| 11 | + _init: function (metadata, orientation, panelHeight, instance_id) { |
| 12 | + Applet.TextIconApplet.prototype._init.call( |
| 13 | + this, |
| 14 | + orientation, |
| 15 | + panelHeight, |
| 16 | + instance_id |
| 17 | + ); |
| 18 | + |
| 19 | + this.set_applet_icon_name("clock"); |
| 20 | + this.set_applet_tooltip("Click to view the time"); |
| 21 | + |
| 22 | + // Debug: Log initialization |
| 23 | + // global.log("Click-Clock applet initialized successfully."); |
| 24 | + }, |
| 25 | + |
| 26 | + // Override the default on_applet_clicked method |
| 27 | + on_applet_clicked: function () { |
| 28 | + try { |
| 29 | + // global.log("Applet clicked!"); // Debug log |
| 30 | + |
| 31 | + let now = new Date(); |
| 32 | + let timeString = now.toLocaleTimeString(); |
| 33 | + |
| 34 | + // global.log("Current time: " + timeString); // Log the time |
| 35 | + this.set_applet_label(timeString); |
| 36 | + this.set_applet_tooltip(`Current time: ${timeString}`); |
| 37 | + |
| 38 | + // Reset to icon after 20 seconds |
| 39 | + if (this._timeoutId) { |
| 40 | + Mainloop.source_remove(this._timeoutId); |
| 41 | + } |
| 42 | + |
| 43 | + this._timeoutId = Mainloop.timeout_add_seconds(20, () => { |
| 44 | + this._resetToIcon(); |
| 45 | + return false; // Stop the timeout |
| 46 | + }); |
| 47 | + } catch (e) { |
| 48 | + global.logError("Error in on_applet_clicked: " + e); |
| 49 | + } |
| 50 | + }, |
| 51 | + |
| 52 | + _resetToIcon: function () { |
| 53 | + try { |
| 54 | + // global.log("Resetting to clock icon."); // Debug log |
| 55 | + this.set_applet_label(""); |
| 56 | + this.set_applet_icon_name("clock"); |
| 57 | + this.set_applet_tooltip("Click to view the time"); |
| 58 | + } catch (e) { |
| 59 | + global.logError("Error in _resetToIcon: " + e); |
| 60 | + } |
| 61 | + }, |
| 62 | + |
| 63 | + on_applet_removed_from_panel: function () { |
| 64 | + try { |
| 65 | + if (this._timeoutId) { |
| 66 | + Mainloop.source_remove(this._timeoutId); |
| 67 | + } |
| 68 | + } catch (e) { |
| 69 | + global.logError("Error in on_applet_removed_from_panel: " + e); |
| 70 | + } |
| 71 | + }, |
| 72 | +}; |
| 73 | + |
| 74 | +function main(metadata, orientation, panelHeight, instance_id) { |
| 75 | + return new MyApplet(metadata, orientation, panelHeight, instance_id); |
| 76 | +} |
0 commit comments