| 
									
										
										
										
											2020-01-18 09:38:21 +01:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2021-04-22 01:24:48 -07:00
										 |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							| 
									
										
										
										
											2020-01-18 09:38:21 +01:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-30 21:40:27 +01:00
										 |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <AK/Function.h>
 | 
					
						
							| 
									
										
										
										
											2020-02-06 15:04:03 +01:00
										 |  |  | #include <LibCore/Object.h>
 | 
					
						
							| 
									
										
										
										
											2019-03-30 21:40:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 12:34:39 +01:00
										 |  |  | namespace Core { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class Timer final : public Object { | 
					
						
							| 
									
										
										
										
											2020-04-07 22:15:22 +02:00
										 |  |  |     C_OBJECT(Timer); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-30 21:40:27 +01:00
										 |  |  | public: | 
					
						
							| 
									
										
										
										
											2021-03-27 14:33:31 -06:00
										 |  |  |     static NonnullRefPtr<Timer> create_repeating(int interval, Function<void()>&& timeout_handler, Object* parent = nullptr) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-04-23 16:46:57 +02:00
										 |  |  |         auto timer = adopt_ref(*new Timer(interval, move(timeout_handler), parent)); | 
					
						
							| 
									
										
										
										
											2021-03-27 14:33:31 -06:00
										 |  |  |         timer->stop(); | 
					
						
							|  |  |  |         return timer; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-04-07 22:15:22 +02:00
										 |  |  |     static NonnullRefPtr<Timer> create_single_shot(int interval, Function<void()>&& timeout_handler, Object* parent = nullptr) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-04-23 16:46:57 +02:00
										 |  |  |         auto timer = adopt_ref(*new Timer(interval, move(timeout_handler), parent)); | 
					
						
							| 
									
										
										
										
											2020-04-07 22:15:22 +02:00
										 |  |  |         timer->set_single_shot(true); | 
					
						
							| 
									
										
										
										
											2020-12-28 20:47:46 +01:00
										 |  |  |         timer->stop(); | 
					
						
							| 
									
										
										
										
											2020-04-07 22:15:22 +02:00
										 |  |  |         return timer; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 12:34:39 +01:00
										 |  |  |     virtual ~Timer() override; | 
					
						
							| 
									
										
										
										
											2019-03-30 21:40:27 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     void start(); | 
					
						
							|  |  |  |     void start(int interval); | 
					
						
							| 
									
										
										
										
											2020-06-11 22:35:37 +02:00
										 |  |  |     void restart(); | 
					
						
							| 
									
										
										
										
											2019-04-18 04:38:04 +02:00
										 |  |  |     void restart(int interval); | 
					
						
							| 
									
										
										
										
											2019-03-30 21:40:27 +01:00
										 |  |  |     void stop(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     bool is_active() const { return m_active; } | 
					
						
							|  |  |  |     int interval() const { return m_interval; } | 
					
						
							| 
									
										
										
										
											2019-04-18 04:38:04 +02:00
										 |  |  |     void set_interval(int interval) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (m_interval == interval) | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         m_interval = interval; | 
					
						
							|  |  |  |         m_interval_dirty = true; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-03-30 21:40:27 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     bool is_single_shot() const { return m_single_shot; } | 
					
						
							|  |  |  |     void set_single_shot(bool single_shot) { m_single_shot = single_shot; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Function<void()> on_timeout; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							| 
									
										
										
										
											2020-02-02 12:34:39 +01:00
										 |  |  |     explicit Timer(Object* parent = nullptr); | 
					
						
							|  |  |  |     Timer(int interval, Function<void()>&& timeout_handler, Object* parent = nullptr); | 
					
						
							| 
									
										
										
										
											2019-09-20 15:19:46 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 12:34:39 +01:00
										 |  |  |     virtual void timer_event(TimerEvent&) override; | 
					
						
							| 
									
										
										
										
											2019-03-30 21:40:27 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     bool m_active { false }; | 
					
						
							|  |  |  |     bool m_single_shot { false }; | 
					
						
							| 
									
										
										
										
											2019-04-18 04:38:04 +02:00
										 |  |  |     bool m_interval_dirty { false }; | 
					
						
							| 
									
										
										
										
											2019-03-30 21:40:27 +01:00
										 |  |  |     int m_interval { 0 }; | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2020-02-02 12:34:39 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | } |