105 lines
3.1 KiB
C++
105 lines
3.1 KiB
C++
#pragma once
|
|
|
|
#include <iomanip>
|
|
#include <iostream>
|
|
#include <memory>
|
|
#include <sys/types.h>
|
|
|
|
#include "components/button/iconButton.hpp"
|
|
#include "services/timerService.hpp"
|
|
|
|
#include "gtkmm/box.h"
|
|
#include "gtkmm/label.h"
|
|
|
|
namespace {
|
|
std::string format_duration(const std::string &digits) {
|
|
std::string d = digits;
|
|
if (d.size() > 6) {
|
|
d = d.substr(d.size() - 6);
|
|
}
|
|
if (d.empty()) {
|
|
return "";
|
|
}
|
|
|
|
std::string padded = std::string(6 - d.size(), '0') + d;
|
|
|
|
int h = std::stoi(padded.substr(0, 2));
|
|
int m = std::stoi(padded.substr(2, 2));
|
|
int s = std::stoi(padded.substr(4, 2));
|
|
|
|
std::ostringstream out;
|
|
if (h > 0) {
|
|
out << h << "h " << std::setw(2) << std::setfill('0') << m << "m "
|
|
<< std::setw(2) << std::setfill('0') << s << "s";
|
|
} else if (m > 0) {
|
|
out << m << "m " << std::setw(2) << std::setfill('0') << s << "s";
|
|
} else {
|
|
out << s << "s";
|
|
}
|
|
|
|
return out.str();
|
|
}
|
|
} // namespace
|
|
class Timer : public Gtk::Box {
|
|
public:
|
|
Timer(const std::string &duration, uint64_t timerId) {
|
|
uint32_t totalTime = std::stoul(duration);
|
|
u_int64_t seconds = totalTime % 100;
|
|
u_int64_t minutes = (totalTime / 100) % 100;
|
|
u_int64_t hours = totalTime / 10000;
|
|
|
|
u_int64_t totalSeconds = hours * 3600 + minutes * 60 + seconds + 1; // +1 to account for the first tick happening after 1 second
|
|
|
|
this->timeLeft = totalSeconds;
|
|
this->timerLabel = std::make_shared<Gtk::Label>(format_duration(duration));
|
|
timerLabel->add_css_class("control-center-timer-label");
|
|
|
|
auto timerBox = Gtk::make_managed<Gtk::Box>(Gtk::Orientation::HORIZONTAL, 5);
|
|
timerBox->append(*timerLabel);
|
|
|
|
auto cancelButton = Gtk::make_managed<IconButton>(Icon::Type::TOKEN);
|
|
cancelButton->set_tooltip_text("Cancel Timer");
|
|
cancelButton->signal_clicked().connect([this, timerId]() {
|
|
this->timerService->removeTimer(timerId);
|
|
});
|
|
timerBox->append(*cancelButton);
|
|
this->append(*timerBox);
|
|
}
|
|
|
|
void activateTimer() {
|
|
std::cout << "Timer activated" << std::endl;
|
|
}
|
|
|
|
void updateTimeLeft(uint64_t timeValue) {
|
|
uint64_t s = timeValue % 100;
|
|
uint64_t m = (timeValue / 100) % 100;
|
|
uint64_t h = timeValue / 10000;
|
|
uint64_t totalSeconds = h * 3600 + m * 60 + s;
|
|
this->timeLeft = totalSeconds;
|
|
|
|
std::ostringstream out;
|
|
if (h > 0) {
|
|
out << h << "h " << std::setw(2) << std::setfill('0') << m << "m "
|
|
<< std::setw(2) << std::setfill('0') << s << "s";
|
|
} else if (m > 0) {
|
|
out << m << "m " << std::setw(2) << std::setfill('0') << s << "s";
|
|
} else {
|
|
out << s << "s";
|
|
}
|
|
|
|
this->timerLabel->set_text(out.str());
|
|
}
|
|
|
|
void tickDown() {
|
|
if (timeLeft > 0) {
|
|
this->updateTimeLeft(this->timeLeft - 1);
|
|
} else {
|
|
timeLeft = 0;
|
|
}
|
|
}
|
|
|
|
private:
|
|
std::shared_ptr<TimerService> timerService = TimerService::getInstance();
|
|
uint64_t timeLeft;
|
|
std::shared_ptr<Gtk::Label> timerLabel;
|
|
}; |