├── LICENSE ├── README.md ├── build_release_zip.sh ├── extension.js ├── install.sh ├── metadata.json ├── prefs.js ├── preview.png ├── preview_50000.gif ├── preview_5h30m.gif ├── schemas └── org.gnome.shell.extensions.simple_timer.gschema.xml ├── sfx └── Polite.wav ├── src ├── Hotkey.js ├── Settings.js ├── Timer.js └── misc.js └── stylesheet.css /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | 294 | Copyright (C) 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | , 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. 340 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Simple Timer - Gnome Extension 2 | 3 | ![Extension Preview](preview.png) 4 | 5 | # Description: 6 | **Simple Timer** is a GNOME Shell extension that adds a customizable countdown timer to your top panel. 7 | When the timer ends, it notifies you with a visual alert and a customizable sound effect. 8 | 9 | Perfect for reminders, productivity breaks, or - yes - not burning the pizza again. 10 | 11 | # Usage: 12 | 13 | There are two ways to enter the duration: 14 | 15 | ## Direct Numeric Input 16 | 17 | You can simply type the time in. The format `hh:mm:ss` will be automatically added. 18 | 19 | **Examples:** 20 | - `5` -> `0:05` 21 | - `112` -> `1:12` 22 | - `53000` -> `5:30:00` 23 | 24 | ![Extension Preview](preview_50000.gif) 25 | 26 | ## Detailed Time Specification 27 | 28 | You can specify the duration using hours, minutes, and seconds (h, m, s). The order of these units is flexible and does not need to follow a specific sequence. 29 | 30 | **Examples:** 31 | - `5s` -> `0:05` 32 | - `1m 12s` -> `1:12` 33 | - `5h 30m` -> `5:30:00` 34 | - `30m 5h` -> `5:30:00` 35 | - `1m` -> `1:00` 36 | 37 | ![Extension Preview](preview_5h30m.gif) 38 | 39 | # Installation 40 | 41 | ## Installation - Web 42 | - Visit the [official GNOME Extensions page for Simple Timer](https://extensions.gnome.org/extension/5115/simple-timer/) 43 | - Click on the "ON/OFF" switch to install the extension. If prompted, follow the instructions to enable the GNOME Shell integration. 44 | 45 | 46 | ## Installation - Manual 47 | - After downloading the extension just run the install script: 48 | 49 | ``` 50 | chmod +x install.sh 51 | ./install.sh 52 | ``` 53 | 54 | ## Enable the Extension: 55 | 56 | - To enable the extension install gnome-extensions-app and run gnome-extensions-app 57 | 58 | ``` 59 | sudo dnf install gnome-extensions-app 60 | gnome-extensions-app 61 | ``` 62 | 63 | - Or open the gnome extension webpage and enable the extension from there: 64 | 65 | https://extensions.gnome.org/local 66 | 67 | 68 | # Sources: 69 | - Polite.wav 70 | Source: https://github.com/akx/Notifications -------------------------------------------------------------------------------- /build_release_zip.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | shopt -s nullglob 4 | 5 | FILENAME_ZIP="simple-timer@majortomvr.github.com.zip" 6 | FILES=( 7 | LICENSE 8 | metadata.json 9 | schemas/*.gschema.xml 10 | stylesheet.css 11 | *.js 12 | src 13 | sfx 14 | ) 15 | 16 | if [[ "$1" == "--manual-install" ]]; then 17 | #echo "Manual install zip (including compiled schema)" 18 | FILES+=("schemas/gschemas.compiled") 19 | FILENAME_ZIP="install-${FILENAME_ZIP}" 20 | fi 21 | 22 | rm -f "$FILENAME_ZIP" 23 | zip -r "$FILENAME_ZIP" "${FILES[@]}" -------------------------------------------------------------------------------- /extension.js: -------------------------------------------------------------------------------- 1 | import St from 'gi://St'; 2 | import GLib from 'gi://GLib'; 3 | import Gio from 'gi://Gio'; 4 | import Clutter from 'gi://Clutter'; 5 | import {Extension} from 'resource:///org/gnome/shell/extensions/extension.js'; 6 | import * as Main from 'resource:///org/gnome/shell/ui/main.js'; // https://gitlab.gnome.org/GNOME/gnome-shell/-/tree/main/js/ui 7 | import * as MessageTray from 'resource:///org/gnome/shell/ui/messageTray.js'; 8 | import * as PanelMenu from 'resource:///org/gnome/shell/ui/panelMenu.js'; 9 | import * as PopupMenu from 'resource:///org/gnome/shell/ui/popupMenu.js'; 10 | 11 | import * as Misc from './src/misc.js'; 12 | import * as Timer from './src/Timer.js'; 13 | import * as Settings from './src/Settings.js'; 14 | import * as Hotkey from './src/Hotkey.js'; 15 | 16 | export default class TimerExtension extends Extension { 17 | enable() { 18 | this.timer = new Timer.Timer(); 19 | this.settings = new Settings.Settings(this.getSettings()); 20 | 21 | this.panelButton = new PanelMenu.Button(0, "MainButton", false); 22 | this.panelButton.add_style_class_name('simple-timer-panel-button'); 23 | 24 | // MAIN PANEL 25 | this.icon = new St.Icon({ icon_name: 'alarm-symbolic', style_class: 'system-status-icon' }); 26 | this.timerLabel = new St.Label({ text: '0:00', y_expand: true, y_align: Clutter.ActorAlign.CENTER }); 27 | this.timerLabel.hide(); 28 | 29 | this.panelButtonLayout = new St.BoxLayout(); 30 | this.panelButtonLayout.add_child(this.icon); 31 | this.panelButtonLayout.add_child(this.timerLabel); 32 | 33 | 34 | // Timer Input Field 35 | this.menuTimerInputEntry = new St.Entry({ 36 | name: 'time', 37 | primary_icon : new St.Icon({ icon_name : 'media-playback-start-symbolic', icon_size : 24 }), 38 | can_focus : true, 39 | hint_text: _("Enter countdown time..."), 40 | x_expand : true, 41 | y_expand : true, 42 | text: this.settings.getLastTimerInput(), 43 | }); 44 | 45 | // Focus Input field when panel is opened 46 | this.panelButton.menu.connect('open-state-changed', (menu, isOpen) => { 47 | if (isOpen) { 48 | this.menuOpeningDelayID = GLib.timeout_add(GLib.PRIORITY_DEFAULT, 100, () => { 49 | this.menuTimerInputEntry.grab_key_focus(); 50 | this.menuOpeningDelayID = null; 51 | return GLib.SOURCE_REMOVE; 52 | }); 53 | } 54 | }); 55 | 56 | this.menuTimerInputEntry.set_input_purpose(Clutter.TIME); 57 | this.menuTimerInputEntry.clutter_text.set_max_length(12); 58 | 59 | // Input Field Event Management 60 | this.menuTimerInputEntry.clutter_text.connect('activate', ()=> { 61 | this.timerStart(); 62 | }); 63 | this.menuTimerInputEntry.connect('primary-icon-clicked', () => { 64 | this.timerStart(); 65 | }); 66 | 67 | // Timer-Input Text Change Event Handling 68 | this.menuTimerInputEntry.clutter_text.connect('text-changed', ()=> { 69 | let text = this.menuTimerInputEntry.get_text(); 70 | let newText = ""; 71 | 72 | // This filters the time input based on its format, either as a colon-separated format like "3:00:00" or a letter format like "2h 47m 12s". 73 | if (Misc.getTimeInputFormat(text) === Misc.TimeInputFormat.LETTERS) { 74 | newText = Misc.timeInputLetterHandler(text); 75 | } else { 76 | newText = Misc.timeInputColonHandler(text); 77 | } 78 | 79 | // If the input filter has changed the input, we update the text input field with the corrected text. 80 | if (text != newText) { 81 | this.menuTimerInputEntry.set_text(newText); 82 | } 83 | }); 84 | 85 | this.itemInput = new PopupMenu.PopupBaseMenuItem({ 86 | reactive : false, 87 | can_focus : false 88 | }); 89 | this.itemInput.add_child(this.menuTimerInputEntry); 90 | this.panelButton.menu.addMenuItem(this.itemInput); 91 | 92 | // PANEL-MENU 93 | let boxMenuItem = new PopupMenu.PopupBaseMenuItem({ reactive: false, can_focus: false }); 94 | let boxLayout = new St.BoxLayout({ x_align: Clutter.ActorAlign.CENTER, x_expand: true }); 95 | boxMenuItem.add_child(boxLayout); 96 | 97 | // Resume Button 98 | this.menuButtonResume = new PopupMenu.PopupImageMenuItem("", "media-playback-start-symbolic", {style_class: 'control-button'}); 99 | this.menuButtonResume.connect('activate', () => { 100 | if (this.timer.isFinished() || this.timer.isStopped()) { 101 | this.timerStart(); 102 | } else { 103 | this.timer.resume(); 104 | } 105 | 106 | this.updateMenuButtonVisibilty(); 107 | }); 108 | boxLayout.add_child(this.menuButtonResume); 109 | this.panelButton.menu.addMenuItem(boxMenuItem); 110 | 111 | // Pause Button 112 | this.menuButtonPause = new PopupMenu.PopupImageMenuItem("", "media-playback-pause-symbolic", {style_class: 'control-button'}); 113 | this.menuButtonPause.connect('activate', () => { 114 | this.timer.pause(); 115 | this.updateMenuButtonVisibilty(); 116 | }); 117 | boxLayout.add_child(this.menuButtonPause); 118 | 119 | // STOP Button 120 | this.menuButtonStop = new PopupMenu.PopupImageMenuItem("", "media-playback-stop-symbolic", {style_class: 'control-button'}); 121 | this.menuButtonStop.connect('activate', () => { 122 | this.timer.reset(); 123 | this.updateTimerLabelStyle(); 124 | this.updateTimerLabel(); 125 | this.timerLabel.hide(); 126 | this.icon.show(); 127 | this.updateMenuButtonVisibilty(); 128 | }); 129 | boxLayout.add_child(this.menuButtonStop); 130 | 131 | this.panelButton.add_child(this.panelButtonLayout); 132 | 133 | // Create a separator 134 | this.panelButton.menu.addMenuItem(new PopupMenu.PopupSeparatorMenuItem()); 135 | 136 | // Settings entry 137 | const settingsMenuItem = new PopupMenu.PopupImageMenuItem('', 'preferences-system-symbolic', {style_class: 'control-button'}); 138 | settingsMenuItem.x_align = Clutter.ActorAlign.CENTER; 139 | settingsMenuItem.connect('activate', () => { 140 | this.openPreferences(); 141 | }); 142 | this.panelButton.menu.addMenuItem(settingsMenuItem); 143 | 144 | // Adds the panel button to the status area, positioned on the right side of the panel. 145 | Main.panel.addToStatusArea(this.uuid, this.panelButton, 0, "right"); 146 | 147 | // Start 148 | this.updateMenuButtonVisibilty(); 149 | this.initMainLoop(); 150 | 151 | // Check if timer is still running, and if it is -> reload the timer running view 152 | if (this.timer.isRunning()) { 153 | this.timer.update(); 154 | this.timerShow(); 155 | } 156 | 157 | this.hotkey = new Hotkey.Hotkey(this.settings, this.settings.getAlertStartHotkeyID(), this.timerStart.bind(this)); 158 | } 159 | 160 | disable() { 161 | this.hotkey.free(); 162 | this.hotkey = null; 163 | 164 | if (this.menuOpeningDelayID) { 165 | GLib.Source.remove(this.menuOpeningDelayID); 166 | this.menuOpeningDelayID = null; 167 | } 168 | // The Session-Mode "unlock-dialog" is needed because the timer should also be working on the lock screen. 169 | this.freeMainLoop(); 170 | this.panelButton.destroy(); 171 | this.panelButton = null; 172 | this.timer = null; 173 | this.settings = null; 174 | } 175 | 176 | // Shows Start/Input Timer or Stop Button in the Menu, depending on the current timer state [running/stopped]. 177 | updateMenuButtonVisibilty() { 178 | //showStartEntry ? this.menuTimerInputEntry.show() : this.menuTimerInputEntry.hide(); 179 | this.handleButtonStyle(this.menuButtonStop, !this.timer.isStopped()); 180 | this.handleButtonStyle(this.menuButtonPause, this.timer.isRunning()); 181 | this.handleButtonStyle(this.menuButtonResume, !this.timer.isRunning()); 182 | } 183 | 184 | initMainLoop() { 185 | // Update Timer 186 | this.timeout = GLib.timeout_add(GLib.PRIORITY_DEFAULT, 1000, () => { 187 | this.timer.update(); 188 | this.updateTimerLabel(); 189 | 190 | if (this.timer.isFinished() && !this.timer.isNotificationSent()) { 191 | this.timer.setNotificationSent(); 192 | this.createTimerFinishedAlert(); 193 | this.updateMenuButtonVisibilty(); 194 | } 195 | 196 | this.updateTimerLabelStyle(); 197 | 198 | return GLib.SOURCE_CONTINUE; 199 | }); 200 | } 201 | 202 | freeMainLoop() { 203 | GLib.Source.remove(this.timeout); 204 | this.timeout = null; 205 | } 206 | 207 | // Starts the timer and sets the countdown time. 208 | timerStart() { 209 | let timeSeconds = Misc.parseTimeInput( this.menuTimerInputEntry.get_text() ); 210 | 211 | if (timeSeconds > 0) { 212 | this.settings.setLastTimerInput(this.menuTimerInputEntry.get_text()); 213 | this.timer.start(timeSeconds); 214 | this.timerShow(); 215 | } 216 | } 217 | 218 | // Shows the timer if it is running 219 | timerShow() { 220 | if (this.timer.isRunning()) { 221 | this.updateTimerLabel(); 222 | this.updateTimerLabelStyle(false); 223 | this.timerLabel.show(); 224 | this.icon.hide(); 225 | this.menuButtonStop.show(); 226 | this.updateMenuButtonVisibilty(); 227 | } 228 | } 229 | 230 | // Updates the timer-label with the current time left. 231 | updateTimerLabel() { 232 | this.timerLabel.set_text( Misc.formatTime(this.timer.timeLeftSeconds) ); 233 | } 234 | 235 | // Shows the Timer in a different style depending on wether an alert was triggered, or not. 236 | updateTimerLabelStyle() { 237 | if (this.timerLabel) { 238 | let style = ''; 239 | 240 | if (this.timer.isFinished()) { 241 | style = 'countdown-alert'; 242 | } else if (this.timer.isPaused()) { 243 | style = 'countdown-paused'; 244 | } else { 245 | style = 'countdown'; 246 | } 247 | 248 | if (this.timerLabel.style_class != style) { 249 | this.timerLabel.style_class = style; 250 | } 251 | } 252 | } 253 | 254 | // Swichtes style classes depending on button active status. 255 | handleButtonStyle(button, active) { 256 | button.sensitive = active; 257 | 258 | if (active) { 259 | button.remove_style_class_name('img-button-inactive'); 260 | } else { 261 | button.add_style_class_name('img-button-inactive'); 262 | } 263 | } 264 | 265 | // Alert by sending a notification and a sound effect. 266 | createTimerFinishedAlert() { 267 | const defaultAudioFile = GLib.build_filenamev([this.path, '/sfx/Polite.wav']); 268 | const customAudioFile = this.settings.getCustomAlertSfxFile(); 269 | const audioFile = Misc.fileExists(customAudioFile) ? customAudioFile : defaultAudioFile; 270 | 271 | Misc.playAudio(global, audioFile); 272 | 273 | // Send Notification 274 | const systemSource = MessageTray.getSystemSource(); 275 | const notification = new MessageTray.Notification({ 276 | source: systemSource, 277 | title: 'Timer', 278 | body: 'The timer has finished!', 279 | gicon: new Gio.ThemedIcon({name: 'alarm-symbolic'}), 280 | // Same as `gicon`, but takes a themed icon name 281 | iconName: 'alarm-symbolic' 282 | }); 283 | systemSource.addNotification(notification); 284 | } 285 | } 286 | -------------------------------------------------------------------------------- /install.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | # Extract Extension Name 4 | EXTENSION_NAME=$(jq -r .uuid metadata.json) 5 | TARGET_DIRECTORY="${HOME}/.local/share/gnome-shell/extensions/${EXTENSION_NAME}" 6 | 7 | # Compile schema file 8 | glib-compile-schemas --strict schemas/ 9 | 10 | bash build_release_zip.sh --manual-install 11 | ZIP_FILENAME="install-${EXTENSION_NAME}.zip" 12 | 13 | # Remove old installation 14 | rm -rf "$TARGET_DIRECTORY" 15 | # Unzip into target directory 16 | unzip "$ZIP_FILENAME" -d "$TARGET_DIRECTORY" 17 | 18 | echo "Installed the extension to: ${TARGET_DIRECTORY}" 19 | -------------------------------------------------------------------------------- /metadata.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "Simple Timer", 3 | "description": "Simple Timer is a Gnome Shell Extension that adds a Timer to the Panel.", 4 | "uuid": "simple-timer@majortomvr.github.com", 5 | "shell-version": [ 6 | "45", "46", "47", "48" 7 | ], 8 | "url": "https://github.com/MajortomVR/simple-timer-extension", 9 | "settings-schema": "org.gnome.shell.extensions.simple_timer", 10 | "version": 10, 11 | "session-modes": [ "unlock-dialog", "user" ] 12 | } 13 | -------------------------------------------------------------------------------- /prefs.js: -------------------------------------------------------------------------------- 1 | import Gtk from 'gi://Gtk'; 2 | import Adw from 'gi://Adw'; 3 | import Gdk from 'gi://Gdk'; 4 | import Gio from 'gi://Gio'; 5 | import GObject from 'gi://GObject'; 6 | 7 | import {ExtensionPreferences, gettext as _} from 'resource:///org/gnome/Shell/Extensions/js/extensions/prefs.js'; 8 | 9 | import * as Settings from './src/Settings.js'; 10 | import * as Misc from './src/misc.js'; 11 | 12 | export default class SimpleTimerPreferences extends ExtensionPreferences { 13 | fillPreferencesWindow(window) { 14 | const settings = new Settings.Settings(this.getSettings()); 15 | 16 | // Main Page 17 | const mainPage = new Adw.PreferencesPage({ 18 | title: 'General', 19 | icon_name: 'dialog-information-symbolic', 20 | }); 21 | window.add(mainPage); 22 | 23 | // Custom alert sound file chooser 24 | const alertGroup = new Adw.PreferencesGroup({ title: 'Alert' }); 25 | alertGroup.add( createFileChooser(window, 'Select custom sound file', (file) => settings.setCustomAlertSfxFile(file), () => settings.getCustomAlertSfxFile()) ); 26 | 27 | // Custom Keyboard Shortcut 28 | const customKeyboardGroup = new Adw.PreferencesGroup({ title: 'Hotkey' }); 29 | customKeyboardGroup.add( createHotkeyInput(window, 'Hotkey', 'Hotkey to start the timer.', settings, settings.getAlertStartHotkeyID()) ); 30 | 31 | mainPage.add(alertGroup); 32 | mainPage.add(customKeyboardGroup); 33 | } 34 | } 35 | 36 | /** 37 | * Create a new Hotkey-Customizer ActionRow 38 | * @param {object} parent window 39 | * @param {string} title Action row title text 40 | * @param {string} description Action row subtitle description text 41 | * @param {Settings} settings Settings object 42 | * @param {string} hotkeyID The schema name of the hotkey 43 | * @returns {Adw.ActionRow} 44 | */ 45 | function createHotkeyInput(parent, title, description, settings, hotkeyID) { 46 | const hotkeyRow = new Adw.ActionRow({ 47 | title: title, 48 | subtitle: description 49 | }); 50 | 51 | // Displays the shortcut 52 | const shortcutLabel = new Gtk.ShortcutLabel({ 53 | accelerator: settings.getHotkey(hotkeyID), 54 | disabled_text: 'Set a hotkey', 55 | valign: Gtk.Align.CENTER, 56 | halign: Gtk.Align.CENTER, 57 | }); 58 | hotkeyRow.add_suffix(shortcutLabel); 59 | hotkeyRow.activatable_widget = shortcutLabel; 60 | 61 | // Update the displayed hotkey label whenever the associated setting changes 62 | settings.getSettings().connect(`changed::${hotkeyID}`, () => { 63 | shortcutLabel.set_accelerator(settings.getHotkey(hotkeyID)); 64 | }); 65 | 66 | let isCapturing = false; 67 | const controller = new Gtk.EventControllerKey(); 68 | // Waiting on key pressed 69 | controller.connect('key-pressed', (_, keyval, keycode, state) => { 70 | if (!isCapturing) return Gdk.EVENT_PROPAGATE; 71 | const mod = state & Gtk.accelerator_get_default_mod_mask(); 72 | 73 | // Escape cancels capturing mode 74 | if (!mod && keyval === Gdk.KEY_Escape) { 75 | isCapturing = false; 76 | shortcutLabel.set_accelerator(settings.getHotkey(hotkeyID)); 77 | shortcutLabel.disabled_text = 'Set a hotkey'; 78 | return Gdk.EVENT_STOP; 79 | } 80 | 81 | if (!mod || !Gtk.accelerator_valid(keyval, mod)) return Gdk.EVENT_STOP; 82 | 83 | // Save the new shortcut 84 | const shortcut = Gtk.accelerator_name_with_keycode( 85 | null, 86 | keyval, 87 | keycode, 88 | mod 89 | ); 90 | settings.setHotkey(hotkeyID, shortcut); 91 | isCapturing = false; 92 | return Gdk.EVENT_STOP; 93 | }); 94 | 95 | // Activated (clicked) hotkey configuration 96 | hotkeyRow.connect('activated', () => { 97 | isCapturing = true; 98 | shortcutLabel.set_accelerator(''); 99 | shortcutLabel.disabled_text = 'Set a hotkey ...'; 100 | }); 101 | hotkeyRow.add_controller(controller); 102 | return hotkeyRow; 103 | } 104 | 105 | 106 | /** 107 | * Create a new File-Chooser ActionRow 108 | * @param {object} parent window 109 | * @param {string} title string 110 | * @param {function} onSetAudioFile callback function executed to set(write) the audio filepath 111 | * @param {function} onGetAudioFile callback function executed to get(read) the audio filepath. 112 | * @returns {Adw.ActionRow} 113 | */ 114 | function createFileChooser(parent, title, onSetAudioFile, onGetAudioFile) { 115 | const soundFileButton = new Adw.ActionRow({ title: title }); 116 | 117 | // Warning Icon 118 | const warningIcon = new Gtk.Image({ icon_name: 'dialog-warning-symbolic' }); 119 | warningIcon.set_tooltip_text('File not found'); 120 | 121 | // Select File Button 122 | const fileIcon = new Gtk.Image({ icon_name: 'folder-open-symbolic' }); 123 | const fileButton = new Gtk.Button({ valign: Gtk.Align.CENTER }); 124 | fileButton.set_child(fileIcon); 125 | fileButton.set_tooltip_text('Select file'); 126 | 127 | // Delete Button 128 | const deleteIcon = new Gtk.Image({ icon_name: 'user-trash-symbolic' }); 129 | const deleteButton = new Gtk.Button({ valign: Gtk.Align.CENTER }); 130 | deleteButton.set_child(deleteIcon); 131 | deleteButton.set_tooltip_text('Remove custom sound file'); 132 | 133 | // Add Icons and Buttons 134 | soundFileButton.add_prefix(warningIcon); 135 | soundFileButton.add_suffix(fileButton); 136 | soundFileButton.add_suffix(deleteButton); 137 | 138 | // Reset the custom sfx file 139 | deleteButton.connect('clicked', () => { 140 | onSetAudioFile(''); 141 | onUpdate(); 142 | }); 143 | 144 | // Open the file chooser 145 | fileButton.connect('clicked', () => { 146 | // Create a row for selecting a custom sound effect file 147 | const filechooser = new Gtk.FileChooserDialog({ 148 | title: title, 149 | transient_for: parent, 150 | modal: true, 151 | action: Gtk.FileChooserAction.OPEN, 152 | }); 153 | filechooser.add_button('Cancel', Gtk.ResponseType.CANCEL); 154 | filechooser.add_button('OK', Gtk.ResponseType.ACCEPT); 155 | 156 | // Filter by mime types 157 | const mimeTypes = ['audio/mpeg', 'audio/wav', 'audio/ogg', 'audio/flac']; 158 | const filter = new Gtk.FileFilter(); 159 | for (const mimeType of mimeTypes) { 160 | filter.add_mime_type(mimeType); 161 | } 162 | filechooser.set_filter(filter); 163 | 164 | // Handle filechooser response 165 | filechooser.connect('response', (dialog, responseID) => { 166 | if (responseID === Gtk.ResponseType.ACCEPT) { 167 | const selectedFile = dialog.get_file().get_path(); 168 | 169 | if (Misc.fileExists(selectedFile)) { 170 | onSetAudioFile(selectedFile); 171 | onUpdate(); 172 | } 173 | } 174 | dialog.destroy(); 175 | }); 176 | 177 | filechooser.show(); 178 | }); 179 | 180 | function onUpdate() { 181 | const file = onGetAudioFile(); 182 | soundFileButton.subtitle = file || 'No file selected'; 183 | 184 | warningIcon.set_visible(file && !Misc.fileExists(file)); 185 | deleteButton.set_visible(file); 186 | } 187 | onUpdate(); 188 | 189 | return soundFileButton; 190 | } -------------------------------------------------------------------------------- /preview.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MajortomVR/simple-timer-extension/245854906018ceba33708738441891a5354c7c74/preview.png -------------------------------------------------------------------------------- /preview_50000.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MajortomVR/simple-timer-extension/245854906018ceba33708738441891a5354c7c74/preview_50000.gif -------------------------------------------------------------------------------- /preview_5h30m.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MajortomVR/simple-timer-extension/245854906018ceba33708738441891a5354c7c74/preview_5h30m.gif -------------------------------------------------------------------------------- /schemas/org.gnome.shell.extensions.simple_timer.gschema.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | '5m' 6 | Stores the user's most recent timer input. 7 | 8 | 9 | '' 10 | Custom sound for timer alerts 11 | Specifies the path to a custom audio file that will be played when the timer completes. If not set, the default alert sound will be used. 12 | 13 | 14 | a']]]> 15 | Hotkey to start the timer. 16 | 17 | 18 | -------------------------------------------------------------------------------- /sfx/Polite.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MajortomVR/simple-timer-extension/245854906018ceba33708738441891a5354c7c74/sfx/Polite.wav -------------------------------------------------------------------------------- /src/Hotkey.js: -------------------------------------------------------------------------------- 1 | import * as Main from 'resource:///org/gnome/shell/ui/main.js'; 2 | import Meta from 'gi://Meta'; 3 | import Shell from 'gi://Shell'; 4 | 5 | export class Hotkey { 6 | constructor(settings, hotkeyID, onTriggerCallbackFunction) { 7 | this.settings = settings; 8 | this.hotkeyID = hotkeyID; 9 | this.onTriggerCallbackFunction = onTriggerCallbackFunction; 10 | this.bind(); 11 | 12 | this.hotkeyChangedWatcherID = settings.getSettings().connect(`changed::${hotkeyID}`, () => { 13 | this.unbind(); 14 | this.bind(); 15 | }); 16 | } 17 | 18 | free() { 19 | this.unbind(); 20 | if (this.hotkeyChangedWatcherID) { 21 | this.settings.getSettings().disconnect(this.hotkeyChangedWatcherID); 22 | this.hotkeyChangedWatcherID = null; 23 | } 24 | } 25 | 26 | bind() { 27 | Main.wm.addKeybinding( 28 | this.hotkeyID, 29 | this.settings.getSettings(), 30 | Meta.KeyBindingFlags.NONE, 31 | Shell.ActionMode.NORMAL, 32 | this.onTriggerCallbackFunction 33 | ) 34 | } 35 | 36 | unbind() { 37 | Main.wm.removeKeybinding(this.hotkeyID); 38 | } 39 | } -------------------------------------------------------------------------------- /src/Settings.js: -------------------------------------------------------------------------------- 1 | export class Settings { 2 | constructor(gnomeSettings) { 3 | this._settings = gnomeSettings 4 | } 5 | 6 | getLastTimerInput() { 7 | return this._settings.get_string('last-timer-input') || ''; 8 | } 9 | setLastTimerInput(userTimerInputString) { 10 | this._settings.set_string('last-timer-input', userTimerInputString); 11 | } 12 | 13 | getCustomAlertSfxFile() { 14 | return this._settings.get_string('alert-sfx-file'); 15 | } 16 | setCustomAlertSfxFile(file) { 17 | this._settings.set_string('alert-sfx-file', file); 18 | } 19 | 20 | getAlertStartHotkeyID() { 21 | return 'keyboard-shortcut'; 22 | } 23 | 24 | getHotkey(hotkeyID) { 25 | return this._settings.get_strv(hotkeyID)[0]; 26 | } 27 | setHotkey(hotkeyID, str) { 28 | this._settings.set_strv(hotkeyID, [str]); 29 | } 30 | 31 | 32 | getSettings() { 33 | return this._settings; 34 | } 35 | } -------------------------------------------------------------------------------- /src/Timer.js: -------------------------------------------------------------------------------- 1 | export const TimerState = { 2 | STOPPED: 'Stopped', 3 | PAUSED: 'Paused', 4 | RUNNING: 'Running', 5 | FINISHED: 'Finished' 6 | }; 7 | 8 | export class Timer { 9 | constructor() { 10 | this.state = TimerState.STOPPED; 11 | this.lastUpdateTimestamp = 0; 12 | this.timeLeftSeconds = 0; 13 | this.notificationSent = false; 14 | } 15 | 16 | isRunning() { 17 | return this.state == TimerState.RUNNING; 18 | } 19 | 20 | isPaused() { 21 | return this.state == TimerState.PAUSED; 22 | } 23 | 24 | isFinished() { 25 | return this.state == TimerState.FINISHED; 26 | } 27 | 28 | isStopped() { 29 | return this.state == TimerState.STOPPED; 30 | } 31 | 32 | isNotificationSent() { 33 | return this.notificationSent; 34 | } 35 | 36 | setNotificationSent() { 37 | this.notificationSent = true; 38 | } 39 | 40 | start(timeSeconds) { 41 | this.state = TimerState.RUNNING; 42 | this.lastUpdateTimestamp = new Date().getTime(); 43 | this.timeLeftSeconds = timeSeconds; 44 | this.notificationSent = false; 45 | } 46 | 47 | pause() { 48 | if (this.state == TimerState.RUNNING) { 49 | this.state = TimerState.PAUSED; 50 | this._updateTimeLeft(); 51 | } 52 | } 53 | 54 | resume() { 55 | if (this.state == TimerState.PAUSED) { 56 | this.lastUpdateTimestamp = new Date().getTime(); 57 | this.state = TimerState.RUNNING; 58 | } 59 | } 60 | 61 | reset() { 62 | this.state = TimerState.STOPPED; 63 | this.lastUpdateTimestamp = 0; 64 | this.timeLeftSeconds = 0; 65 | this.notificationSent = false; 66 | } 67 | 68 | update() { 69 | if (this.state == TimerState.RUNNING) { 70 | this._updateTimeLeft(); 71 | 72 | if (this.timeLeftSeconds <= 0) { 73 | this.state = TimerState.FINISHED; 74 | this.timeLeftSeconds = 0; 75 | } 76 | } 77 | } 78 | 79 | _updateTimeLeft() { 80 | let timeElapsed = (new Date().getTime() - this.lastUpdateTimestamp) / 1000.0; 81 | this.timeLeftSeconds = Math.max(this.timeLeftSeconds - timeElapsed, 0); 82 | this.lastUpdateTimestamp = new Date().getTime(); 83 | } 84 | } -------------------------------------------------------------------------------- /src/misc.js: -------------------------------------------------------------------------------- 1 | import Gio from 'gi://Gio'; 2 | 3 | 4 | export const TimeInputFormat = { 5 | COLONS: 'COLONS', 6 | LETTERS: 'LETTERS' 7 | }; 8 | 9 | /** 10 | * Determinates the format of a given time input. 11 | * @param {string} text 12 | * @returns {string} - TimeInputFormat 13 | */ 14 | export function getTimeInputFormat(text) { 15 | // Detects the time input format. 16 | let lowerCasedText = text.toLowerCase(); 17 | 18 | // If the input contains the letters h, m or s, it's the letter format. 19 | if (lowerCasedText.includes("h") || lowerCasedText.includes("m") || lowerCasedText.includes("s")) { 20 | return TimeInputFormat.LETTERS; 21 | // In every other case, fallback to the colon format. 22 | } else { 23 | return TimeInputFormat.COLONS; 24 | } 25 | }; 26 | 27 | 28 | /** 29 | * Formats the Time to be nicely formatted for printing to the user. 30 | * @param {*} seconds 31 | * @returns the formatted time as a string 32 | */ 33 | export function formatTime(seconds) { 34 | let text = ""; 35 | 36 | let secondsLeft = seconds; 37 | 38 | let hours = Math.floor(secondsLeft / 3600); 39 | secondsLeft -= hours * 3600; 40 | 41 | let minutes = Math.floor(secondsLeft / 60); 42 | secondsLeft -= minutes * 60; 43 | secondsLeft = Math.floor(secondsLeft); 44 | 45 | if (hours > 0) { 46 | text += hours.toString(); 47 | text += ":"; 48 | } 49 | 50 | if (minutes < 10 && hours > 0) { 51 | text += "0"; 52 | } 53 | 54 | text += minutes.toString(); 55 | 56 | text += ":"; 57 | if (secondsLeft < 10) { 58 | text += "0"; 59 | } 60 | 61 | text += secondsLeft.toString(); 62 | 63 | return text; 64 | } 65 | 66 | 67 | /** 68 | * Takes a Time String (HH:MM:SS) like 2:47 (2min 47) and returns the time in seconds. 69 | * @param {string} text a string like 2:47 or 1:12:10 (1h, 12min, 10sec) 70 | * @return {number} the time duration in seconds 71 | */ 72 | export function parseTimeInput(text) { 73 | if (getTimeInputFormat(text) === TimeInputFormat.LETTERS) { 74 | return parseTimeInputHMS(text); 75 | } else { 76 | return parseTimeInputColons(text); 77 | } 78 | } 79 | 80 | /** 81 | * Takes a Time String (HH:MM:SS) like 2:47 (2min 47) and returns the time in seconds. 82 | * @param {*} text a string like 2:47 or 1:12:10 (1h, 12min, 10sec) 83 | */ 84 | export function parseTimeInputColons(text) { 85 | const timeArray = text.split(":"); 86 | let timerSeconds = 0; 87 | 88 | for (let i=0; i 0) ? text[i - 1] : ''; 122 | 123 | let currentCharType = getCharacterType(c); 124 | let previousCharType = getCharacterType(previousChar); 125 | 126 | // If character types are changing 127 | if (currentCharType != previousCharType) { 128 | if (buffer.length > 0) { 129 | if (previousCharType == CharacterType.DIGITS) { 130 | timeAmount = parseInt(buffer); 131 | } else if (previousCharType == CharacterType.LETTERS) { 132 | // Add the time in seconds to the timer 133 | if (buffer == 'h') { 134 | timerDurationSeconds += timeAmount * 60 * 60; 135 | } else if (buffer == 'm') { 136 | timerDurationSeconds += timeAmount * 60; 137 | } else { 138 | timerDurationSeconds += timeAmount; 139 | } 140 | 141 | timeAmount = 0; // Reset because if there is no letter at the end the time-amount will be added and assumed to be seconds. 142 | } 143 | } 144 | 145 | buffer = ""; 146 | } 147 | 148 | if (c != ' ') { 149 | buffer += c; 150 | } 151 | } 152 | 153 | // If there was no unit [h,m,s] added then assume seconds and add the time to the timer. 154 | timerDurationSeconds = timerDurationSeconds + timeAmount; 155 | 156 | return timerDurationSeconds; 157 | } 158 | 159 | 160 | // Returns true if any of the characters 'h', 'm' or 's' are found. 161 | export function isHMSCharacter(character) { 162 | character = character.toLowerCase(); 163 | return character.includes('h') || character.includes('m') || character.includes('s'); 164 | } 165 | 166 | /** 167 | * Returns true if the given char is a digit. 168 | * @param {string} character 169 | * @returns {boolean} if the given character is a digit 170 | */ 171 | export function isDigitCharacter(character) { 172 | return character >= '0' && character <= '9'; 173 | } 174 | 175 | /** 176 | * Returns true if the given char is a letter 177 | * @param {string} character 178 | * @returns {boolean} if the given character is a letter 179 | */ 180 | export function isLetterCharacter(character) { 181 | return (character >= 'a' && character <= 'z') || (character >= 'A' && character <= 'Z'); 182 | } 183 | 184 | /** 185 | * Returns true if the given char is a space ' ' 186 | * @param {string} character 187 | * @returns {boolean} if the given character is a space 188 | */ 189 | export function isSpaceCharacter(character) { 190 | return character === ' '; 191 | } 192 | 193 | export const CharacterType = { 194 | DIGITS: 'DIGITS', 195 | LETTERS: 'LETTERS', 196 | SPACE: 'SPACE', 197 | OTHER: 'OTHER' 198 | }; 199 | 200 | /** 201 | * Determines the type of the given character based on predefined character types. 202 | * @param {string} character - The character to check. 203 | * @returns {string} The string representation of the character type. Possible values: 'DIGITS', 'LETTERS', 'SPACE', or 'OTHER'. 204 | */ 205 | export function getCharacterType(character) { 206 | if (character == ' ') { 207 | return CharacterType.SPACE; 208 | } else if (isDigitCharacter(character)) { 209 | return CharacterType.DIGITS; 210 | } else if (isLetterCharacter(character)) { 211 | return CharacterType.LETTERS; 212 | } else { 213 | return CharacterType.OTHER; 214 | } 215 | } 216 | 217 | 218 | /** 219 | * Corrects the string to only show numbers, time characters and spaces. Example: 1h 12m 20s 220 | * @param {string} text 221 | * @returns {string} text corrected to the 0h 0m 0s format 222 | */ 223 | export function timeInputLetterHandler(text){ 224 | const allowedChars = "hms1234567890 "; 225 | let lowerCasedText = text.toLowerCase(); 226 | let filteredText = ""; 227 | 228 | // Filter all characters except for 'h','m','s', ' ' (spaces), and digits 229 | for (let i=0; i 0 ? lowerCasedText[i - 1] : ''; 234 | 235 | // Space is only allowed after a time letter ('h','m','s') 236 | if (c === ' ' && !isHMSCharacter(previousChar)) { 237 | continue; 238 | } 239 | 240 | // Allow only one h, m, s letter 241 | if (isHMSCharacter(c)) { 242 | // If the text already has this letter, or the previous character is not a digit, then skip adding the character. 243 | if (filteredText.includes(c) || !isDigitCharacter(previousChar)) { 244 | continue; 245 | } 246 | } 247 | 248 | filteredText += c; 249 | } 250 | } 251 | 252 | return filteredText; 253 | } 254 | 255 | /** 256 | * Corrects the string to only show numbers and colons in the HH:MM:SS format. 257 | * @param {string} text 258 | * @returns {string} text corrected to the HH:MM:SS format 259 | */ 260 | export function timeInputColonHandler(text) { 261 | // filter all characters except 0-9 262 | let numberString = ""; 263 | 264 | for (let i=0; i= '0' && c <= '9') { 267 | numberString += c; 268 | } 269 | } 270 | 271 | let numIndex = 0; 272 | let finalText = ""; 273 | // After every second number, add a colon 274 | for (let i=numberString.length - 1; i>=0; i--) { 275 | numIndex++; 276 | 277 | if (numIndex == 3) { 278 | numIndex = 1; 279 | finalText = ":" + finalText; 280 | } 281 | 282 | finalText = numberString[i] + finalText; 283 | } 284 | 285 | // Only return the first 9 characters 286 | return finalText.slice(0, 9); 287 | } 288 | 289 | /** 290 | * Checks if a file exists at the specified path. 291 | * @param {string} file 292 | * @returns {boolean} 293 | */ 294 | export function fileExists(file) { 295 | const gFile = Gio.File.new_for_path(file); 296 | return gFile.query_exists(null); 297 | } 298 | 299 | /** 300 | * Plays an audio file using the system sound player. 301 | * @param {string} file - The absolute file path to the audio file to play. 302 | */ 303 | export function playAudio(global, file) { 304 | const audioFileHandle = Gio.File.new_for_path(file); 305 | 306 | const player = global.display.get_sound_player(); 307 | player.play_from_file(audioFileHandle, 'Alert', null); 308 | } -------------------------------------------------------------------------------- /stylesheet.css: -------------------------------------------------------------------------------- 1 | .countdown { 2 | color: white; 3 | } 4 | 5 | .countdown-alert { 6 | color: red; 7 | } 8 | 9 | .countdown-paused { 10 | color: grey; 11 | } 12 | 13 | .img-button-inactive { 14 | color: rgba(128, 128, 128, 0.2); 15 | } 16 | 17 | .simple-timer-panel-button { 18 | padding: 0px; 19 | margin: 0px; 20 | -natural-hpadding: 2px; 21 | -minimum-hpadding: 2px; 22 | } 23 | 24 | .control-button { 25 | width: 22px; 26 | height: 28px; 27 | border-radius: 28px; 28 | margin: 5px; 29 | padding: 5px; 30 | padding-left: 11px; 31 | } 32 | --------------------------------------------------------------------------------