timers work, fix crash when vscode window closes after timeout
This commit is contained in:
@@ -67,7 +67,7 @@ class Timer : public Gtk::Box {
|
||||
}
|
||||
|
||||
void activateTimer() {
|
||||
std::cout << "Timer activated" << std::endl;
|
||||
// std::cout << "Timer activated" << std::endl;
|
||||
}
|
||||
|
||||
void updateTimeLeft(uint64_t timeValue) {
|
||||
|
||||
@@ -41,6 +41,12 @@ class StringHelper {
|
||||
if (input.length() <= maxSize) {
|
||||
return input;
|
||||
}
|
||||
return input.substr(0, maxSize) + "...";
|
||||
|
||||
size_t len = maxSize;
|
||||
while (len > 0 && (static_cast<unsigned char>(input[len]) & 0xC0) == 0x80) {
|
||||
len--;
|
||||
}
|
||||
|
||||
return input.substr(0, len) + "...";
|
||||
}
|
||||
};
|
||||
@@ -2,9 +2,12 @@
|
||||
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <spdlog/spdlog.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#include "glibmm/main.h"
|
||||
#include "gtkmm/mediafile.h"
|
||||
#include "gtkmm/mediastream.h"
|
||||
#include "sigc++/signal.h"
|
||||
class TimerService {
|
||||
struct TimerData {
|
||||
@@ -15,7 +18,7 @@ class TimerService {
|
||||
public:
|
||||
static std::shared_ptr<TimerService> getInstance() {
|
||||
if (!instance) {
|
||||
instance = std::make_shared<TimerService>();
|
||||
instance = std::shared_ptr<TimerService>(new TimerService());
|
||||
|
||||
// add a timer to tick every second
|
||||
Glib::signal_timeout().connect([]() {
|
||||
@@ -37,11 +40,28 @@ class TimerService {
|
||||
|
||||
void expireTimer(uint64_t timerId) {
|
||||
this->timerExpiredSignal.emit(timerId);
|
||||
|
||||
if (ringingTimerCounter == 0) {
|
||||
mediaFile->set_volume(1.0);
|
||||
mediaFile->set_loop(true);
|
||||
mediaFile->play();
|
||||
spdlog::info("Playing timer sound");
|
||||
}
|
||||
|
||||
ringingTimerCounter++;
|
||||
|
||||
}
|
||||
|
||||
void removeTimer(uint64_t timerId) {
|
||||
ringingTimerCounter--;
|
||||
this->activeTimers.erase(timerId);
|
||||
this->timerCancelledSignal.emit(timerId);
|
||||
|
||||
if (ringingTimerCounter <= 0) {
|
||||
ringingTimerCounter = 0;
|
||||
mediaFile->pause();
|
||||
spdlog::info("Paused timer sound");
|
||||
}
|
||||
}
|
||||
|
||||
sigc::signal<void(const std::string &, u_int64_t)> &getSignalTimerSet() {
|
||||
@@ -59,7 +79,32 @@ class TimerService {
|
||||
sigc::signal<void()> tickSignal;
|
||||
|
||||
private:
|
||||
static inline uint64_t timerIdCounter = 0;
|
||||
TimerService() {
|
||||
this->mediaFile = Gtk::MediaFile::create();
|
||||
if (this->mediaFile) {
|
||||
this->mediaFile->set_filename("/usr/share/sounds/freedesktop/stereo/alarm-clock-elapsed.oga");
|
||||
|
||||
// Connect to signals to debug playback issues
|
||||
this->mediaFile->property_error().signal_changed().connect([]() {
|
||||
if (auto err = mediaFile->get_error()) {
|
||||
spdlog::error("Timer sound error: {}", err.what());
|
||||
}
|
||||
});
|
||||
|
||||
this->mediaFile->property_prepared().signal_changed().connect([]() {
|
||||
if (mediaFile->is_prepared()) {
|
||||
spdlog::info("Timer sound ready");
|
||||
}
|
||||
});
|
||||
} else {
|
||||
spdlog::error("Failed to create media file!");
|
||||
}
|
||||
}
|
||||
|
||||
inline static Glib::RefPtr<Gtk::MediaFile> mediaFile;
|
||||
|
||||
static inline uint64_t timerIdCounter = 0;
|
||||
static inline u_int64_t ringingTimerCounter = 0;
|
||||
sigc::signal<void(const std::string &, u_int64_t)> timerAddedSignal;
|
||||
sigc::signal<void(u_int64_t)> timerCancelledSignal;
|
||||
sigc::signal<void(u_int64_t)> timerExpiredSignal;
|
||||
@@ -73,6 +118,7 @@ class TimerService {
|
||||
if (timer.duration > 0) {
|
||||
timer.duration--;
|
||||
} else if (timer.duration == 0) {
|
||||
spdlog::info("Timer {} expired", timer.timerId);
|
||||
expireTimer(timer.timerId);
|
||||
timer.duration = -1; // Mark as expired to prevent multiple expirations
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
#include <chrono>
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
#include <sigc++/connection.h>
|
||||
|
||||
#include "gdkmm/monitor.h"
|
||||
@@ -9,7 +10,7 @@
|
||||
|
||||
#define DEFAULT_NOTIFICATION_TIMEOUT 6700
|
||||
|
||||
class BaseNotification : public Gtk::Window {
|
||||
class BaseNotification : public Gtk::Window, public std::enable_shared_from_this<BaseNotification> {
|
||||
public:
|
||||
BaseNotification(uint64_t notificationId, std::shared_ptr<Gdk::Monitor> monitor);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user