mirror of
https://codeberg.org/vyn/mirai.git
synced 2025-07-04 02:33:19 +00:00
Move all UI code into new 'ui' directory
This commit is contained in:
parent
00d9cbe7ef
commit
0046cb9bbb
21 changed files with 94 additions and 93 deletions
93
src/ui/utils.cpp
Normal file
93
src/ui/utils.cpp
Normal file
|
@ -0,0 +1,93 @@
|
|||
/*
|
||||
* Mirai. Copyright (C) 2024 Vyn
|
||||
* This file is licensed under version 3 of the GNU General Public License (GPL-3.0-only)
|
||||
* The license can be found in the LICENSE file or at https://www.gnu.org/licenses/gpl-3.0.txt
|
||||
*/
|
||||
|
||||
#include "utils.h"
|
||||
#include <cctype>
|
||||
#include <chrono>
|
||||
#include <format>
|
||||
#include <string>
|
||||
|
||||
void bind_slint_utils(const ui::Utils &utils)
|
||||
{
|
||||
utils.on_format_date([&](const ui::Date &date) {
|
||||
std::chrono::year_month_day chronoDate{
|
||||
std::chrono::year(date.year),
|
||||
std::chrono::month(date.month),
|
||||
std::chrono::day(date.day),
|
||||
};
|
||||
return std::format("{:%B %d}", chronoDate);
|
||||
});
|
||||
|
||||
auto currentDate = mirai_date_to_slint_date(mirai::date(std::chrono::system_clock::now()));
|
||||
utils.set_current_date(currentDate);
|
||||
|
||||
auto currentTime = mirai_time_to_slint_time(mirai::time(std::chrono::system_clock::now()));
|
||||
utils.set_current_time(currentTime);
|
||||
}
|
||||
|
||||
std::string format_zero_padding(const int number)
|
||||
{
|
||||
if (number < 10) {
|
||||
return "0" + std::to_string(number);
|
||||
}
|
||||
return std::to_string(number);
|
||||
}
|
||||
|
||||
std::string format_date_relative(const ui::Date &date)
|
||||
{
|
||||
auto todayDate = mirai::date(std::chrono::system_clock::now());
|
||||
auto relativeDaysDiff =
|
||||
std::chrono::duration_cast<std::chrono::days>(
|
||||
std::chrono::sys_days(slint_date_to_mirai_date(date).to_std_chrono())
|
||||
- std::chrono::sys_days(todayDate.to_std_chrono())
|
||||
)
|
||||
.count();
|
||||
|
||||
if (relativeDaysDiff == 0) {
|
||||
return std::string("today");
|
||||
} else if (relativeDaysDiff == 1) {
|
||||
return std::string("tomorrow");
|
||||
}
|
||||
return std::format("in {} days", relativeDaysDiff);
|
||||
};
|
||||
|
||||
std::string capitalize(std::string str)
|
||||
{
|
||||
str[0] = static_cast<char>(toupper(str[0]));
|
||||
return str;
|
||||
};
|
||||
|
||||
std::string slint_date_to_std_string(const ui::Date &date)
|
||||
{
|
||||
return std::to_string(date.year) + "-" + format_zero_padding(date.month) + "-"
|
||||
+ format_zero_padding(date.day);
|
||||
}
|
||||
|
||||
mirai::date slint_date_to_mirai_date(const ui::Date &date)
|
||||
{
|
||||
return mirai::date(
|
||||
date.year, static_cast<unsigned>(date.month), static_cast<unsigned>(date.day)
|
||||
);
|
||||
}
|
||||
|
||||
ui::Date mirai_date_to_slint_date(const mirai::date &date)
|
||||
{
|
||||
return {
|
||||
.year = date.year,
|
||||
.month = static_cast<int>(date.month),
|
||||
.day = static_cast<int>(date.day),
|
||||
};
|
||||
}
|
||||
|
||||
ui::Time mirai_time_to_slint_time(const mirai::time &time)
|
||||
{
|
||||
return {.hour = time.hour, .minute = time.minute, .second = 0};
|
||||
}
|
||||
|
||||
mirai::time slint_time_to_mirai_time(const ui::Time &time)
|
||||
{
|
||||
return mirai::time{time.hour, time.minute};
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue