Rename AppWindow to app_logic

This commit is contained in:
Vyn 2025-07-03 09:57:20 +02:00
parent 049353e06a
commit 00d9cbe7ef
Signed by: vyn
GPG key ID: E1B2BE34E7A971E7
4 changed files with 69 additions and 71 deletions

View file

@ -8,7 +8,7 @@ set(CMAKE_BUILD_RPATH_USE_ORIGIN ON)
add_executable(mirai add_executable(mirai
src/main.cpp src/main.cpp
src/windows/AppWindow/AppWindow.cpp src/app_logic.cpp
src/shared/Utils.cpp src/shared/Utils.cpp
) )

View file

@ -4,8 +4,7 @@
* The license can be found in the LICENSE file or at https://www.gnu.org/licenses/gpl-3.0.txt * The license can be found in the LICENSE file or at https://www.gnu.org/licenses/gpl-3.0.txt
*/ */
#include "AppWindow.h" #include "app_logic.h"
#include "../../shared/Utils.h"
#include "mirai-core/core.h" #include "mirai-core/core.h"
#include "mirai-core/date.h" #include "mirai-core/date.h"
#include "mirai-core/event.h" #include "mirai-core/event.h"
@ -13,6 +12,7 @@
#include "mirai-core/source.h" #include "mirai-core/source.h"
#include "selenite/palette.h" #include "selenite/palette.h"
#include "selenite/selenite.h" #include "selenite/selenite.h"
#include "shared/Utils.h"
#include "slint_color.h" #include "slint_color.h"
#include "slint_models.h" #include "slint_models.h"
#include "slint_string.h" #include "slint_string.h"
@ -32,7 +32,7 @@
#include <string_view> #include <string_view>
#include <vector> #include <vector>
AppWindow::AppWindow(mirai::core *miraiInstance) : miraiInstance_(miraiInstance) app_logic::app_logic(mirai::core *miraiInstance) : _mirai_core(miraiInstance)
{ {
_sidebar_view = ui::SidebarView(); _sidebar_view = ui::SidebarView();
_calendar_view = ui::CalendarView(); _calendar_view = ui::CalendarView();
@ -48,11 +48,11 @@ AppWindow::AppWindow(mirai::core *miraiInstance) : miraiInstance_(miraiInstance)
if (palette.has_value()) { if (palette.has_value()) {
std::println(std::cerr, "Warning, no {} found", palettePath); std::println(std::cerr, "Warning, no {} found", palettePath);
setSelenitePalette<slint::Color, selenite::Color>( setSelenitePalette<slint::Color, selenite::Color>(
mainWindow_->global<ui::Palette>(), palette.value() _main_window->global<ui::Palette>(), palette.value()
); );
} }
bindSlintUtils(mainWindow_->global<ui::Utils>()); bindSlintUtils(_main_window->global<ui::Utils>());
models().set_sidebar_view(_sidebar_view); models().set_sidebar_view(_sidebar_view);
models().set_calendar_view(_calendar_view); models().set_calendar_view(_calendar_view);
@ -90,59 +90,59 @@ std::optional<ui::Date> stringToDate(const std::string &dateStr)
return ui::Date{.year = year, .month = month, .day = day}; return ui::Date{.year = year, .month = month, .day = day};
} }
void AppWindow::setupCallbacks() void app_logic::setupCallbacks()
{ {
models().on_get_source_id_from_name([&](slint::SharedString name) { models().on_get_source_id_from_name([&](slint::SharedString name) {
auto source = miraiInstance_->getSourceByName(std::string(name)); auto source = _mirai_core->getSourceByName(std::string(name));
assert(source); assert(source);
return source->id; return source->id;
}); });
models().on_get_source_name_from_id([&](int sourceId) { models().on_get_source_name_from_id([&](int sourceId) {
auto source = miraiInstance_->getSourceById(sourceId); auto source = _mirai_core->getSourceById(sourceId);
assert(source); assert(source);
return slint::SharedString(source->name()); return slint::SharedString(source->name());
}); });
models().on_get_source_color_from_id_as_hex([&](int sourceId) { models().on_get_source_color_from_id_as_hex([&](int sourceId) {
auto source = miraiInstance_->getSourceById(sourceId); auto source = _mirai_core->getSourceById(sourceId);
assert(source); assert(source);
return slint::SharedString(source->color()); return slint::SharedString(source->color());
}); });
models().on_get_source_color_from_id_as_color([&](int sourceId) { models().on_get_source_color_from_id_as_color([&](int sourceId) {
auto source = miraiInstance_->getSourceById(sourceId); auto source = _mirai_core->getSourceById(sourceId);
assert(source); assert(source);
auto color = selenite::hexStringToColor(source->color()); auto color = selenite::hexStringToColor(source->color());
return slint::Color::from_rgb_uint8(color.r, color.g, color.b); return slint::Color::from_rgb_uint8(color.r, color.g, color.b);
}); });
models().on_get_source_path_from_id([&](int sourceId) { models().on_get_source_path_from_id([&](int sourceId) {
auto source = miraiInstance_->getSourceById(sourceId); auto source = _mirai_core->getSourceById(sourceId);
assert(source); assert(source);
mirai::markdown_data_provider *sourceProvider = mirai::markdown_data_provider *sourceProvider =
dynamic_cast<mirai::markdown_data_provider *>(source->data_provider()); dynamic_cast<mirai::markdown_data_provider *>(source->data_provider());
return slint::SharedString(sourceProvider->path()); return slint::SharedString(sourceProvider->path());
}); });
miraiInstance_->onSourceAdded([&](mirai::source *source) { refreshModels(); }); _mirai_core->onSourceAdded([&](mirai::source *source) { refreshModels(); });
miraiInstance_->onSourceEdited([&](mirai::source *source) { refreshModels(); }); _mirai_core->onSourceEdited([&](mirai::source *source) { refreshModels(); });
miraiInstance_->onSourceDeleted([&](int id) { refreshModels(); }); _mirai_core->onSourceDeleted([&](int id) { refreshModels(); });
actions().on_task_clicked([&](int sourceId, int taskId) { actions().on_task_clicked([&](int sourceId, int taskId) {
auto source = miraiInstance_->getSourceById(sourceId); auto source = _mirai_core->getSourceById(sourceId);
assert(source); assert(source);
auto task = source->get_task_by_id(taskId); auto task = source->get_task_by_id(taskId);
assert(task); assert(task);
task->set_checked(!task->checked()); task->set_checked(!task->checked());
miraiInstance_->save(); _mirai_core->save();
update_views(); update_views();
// view_.update(); // view_.update();
reloadTasks(); reloadTasks();
}); });
actions().on_source_clicked([&](int index) { actions().on_source_clicked([&](int index) {
const mirai::source *const source = miraiInstance_->getSourceById(index); const mirai::source *const source = _mirai_core->getSourceById(index);
assert(source != nullptr); assert(source != nullptr);
if (should_show_source(*source)) { if (should_show_source(*source)) {
hide_source(*source); hide_source(*source);
@ -156,23 +156,21 @@ void AppWindow::setupCallbacks()
actions().on_add_source([&](slint::SharedString name, slint::SharedString path) { actions().on_add_source([&](slint::SharedString name, slint::SharedString path) {
std::unique_ptr<mirai::data_provider> file = std::unique_ptr<mirai::data_provider> file =
std::make_unique<mirai::markdown_data_provider>(std::string(path)); std::make_unique<mirai::markdown_data_provider>(std::string(path));
miraiInstance_->addSource(std::string(name), "FileSystemMarkdown", std::move(file)); _mirai_core->addSource(std::string(name), "FileSystemMarkdown", std::move(file));
}); });
actions().on_edit_source([&](int sourceId, slint::SharedString name, slint::SharedString color, actions().on_edit_source([&](int sourceId, slint::SharedString name, slint::SharedString color,
slint::SharedString path) { slint::SharedString path) {
miraiInstance_->editSource( _mirai_core->editSource(sourceId, std::string(name), std::string(color), std::string(path));
sourceId, std::string(name), std::string(color), std::string(path)
);
}); });
actions().on_delete_task_clicked([&](int sourceId, int taskId) { actions().on_delete_task_clicked([&](int sourceId, int taskId) {
auto source = miraiInstance_->getSourceById(sourceId); auto source = _mirai_core->getSourceById(sourceId);
assert(source); assert(source);
auto task = source->get_task_by_id(taskId); auto task = source->get_task_by_id(taskId);
assert(task); assert(task);
source->remove_task(*task); source->remove_task(*task);
miraiInstance_->save(); _mirai_core->save();
// view_.update(); // view_.update();
update_views(); update_views();
reloadTasks(); reloadTasks();
@ -191,7 +189,7 @@ void AppWindow::setupCallbacks()
}); });
actions().on_save_task([&](ui::SaveTaskData newTaskData) { actions().on_save_task([&](ui::SaveTaskData newTaskData) {
auto source = miraiInstance_->getSourceById(newTaskData.sourceId); auto source = _mirai_core->getSourceById(newTaskData.sourceId);
assert(source); assert(source);
auto task = source->get_task_by_id(newTaskData.id); auto task = source->get_task_by_id(newTaskData.id);
assert(task.has_value()); assert(task.has_value());
@ -199,7 +197,7 @@ void AppWindow::setupCallbacks()
// const auto dayOpt = source->get_day_by_date(date); // const auto dayOpt = source->get_day_by_date(date);
task->set_title(std::string(newTaskData.title)); task->set_title(std::string(newTaskData.title));
miraiInstance_->save(); _mirai_core->save();
// view_.update(); // view_.update();
update_views(); update_views();
reloadTasks(); reloadTasks();
@ -210,7 +208,7 @@ void AppWindow::setupCallbacks()
if (newTaskData.date.year != 0) { if (newTaskData.date.year != 0) {
date = SlintDateToMiraiDate(newTaskData.date); date = SlintDateToMiraiDate(newTaskData.date);
} }
auto source = miraiInstance_->getSourceById(newTaskData.sourceId); auto source = _mirai_core->getSourceById(newTaskData.sourceId);
std::optional<mirai::event> event = std::nullopt; std::optional<mirai::event> event = std::nullopt;
if (newTaskData.eventId >= 0) { if (newTaskData.eventId >= 0) {
@ -222,19 +220,19 @@ void AppWindow::setupCallbacks()
.due_date = date, .due_date = date,
}); });
miraiInstance_->save(); _mirai_core->save();
// view_.update(); // view_.update();
update_views(); update_views();
reloadTasks(); reloadTasks();
}); });
actions().on_delete_event([&](int sourceId, int eventId) { actions().on_delete_event([&](int sourceId, int eventId) {
auto source = miraiInstance_->getSourceById(sourceId); auto source = _mirai_core->getSourceById(sourceId);
assert(source); assert(source);
auto event = source->get_event_by_id(eventId); auto event = source->get_event_by_id(eventId);
assert(event.has_value()); assert(event.has_value());
source->remove_event(event.value()); source->remove_event(event.value());
miraiInstance_->save(); _mirai_core->save();
// view_.update(); // view_.update();
update_views(); update_views();
reloadTasks(); reloadTasks();
@ -243,7 +241,7 @@ void AppWindow::setupCallbacks()
actions().on_create_event([&](ui::CreateEventParams newEventParams) { actions().on_create_event([&](ui::CreateEventParams newEventParams) {
const ui::Date &date = newEventParams.date; const ui::Date &date = newEventParams.date;
const std::string dateStr = SlintDateToStdString(date); const std::string dateStr = SlintDateToStdString(date);
auto source = miraiInstance_->getSourceById(newEventParams.source_id); auto source = _mirai_core->getSourceById(newEventParams.source_id);
source->create_event({ source->create_event({
.title = std::string(newEventParams.title), .title = std::string(newEventParams.title),
@ -251,7 +249,7 @@ void AppWindow::setupCallbacks()
.starts_at = SlintTimeToMiraiTime(newEventParams.starts_at), .starts_at = SlintTimeToMiraiTime(newEventParams.starts_at),
.ends_at = SlintTimeToMiraiTime(newEventParams.ends_at), .ends_at = SlintTimeToMiraiTime(newEventParams.ends_at),
}); });
miraiInstance_->save(); _mirai_core->save();
update_views(); update_views();
}); });
} }
@ -266,7 +264,7 @@ stdToSlintStringVector(const std::vector<std::string> &stdVector)
return slintVector; return slintVector;
} }
void AppWindow::reloadTasks() void app_logic::reloadTasks()
{ {
/* days_->clear();*/ /* days_->clear();*/
/*if (miraiInstance_->getSources().size() == 0) {*/ /*if (miraiInstance_->getSources().size() == 0) {*/
@ -395,7 +393,7 @@ void AppWindow::reloadTasks()
/*}*/ /*}*/
} }
void AppWindow::reloadSources() void app_logic::reloadSources()
{ {
/*sources_->clear();*/ /*sources_->clear();*/
/*bool noSourceSelected = miraiInstance_->getSources().size() == view_.activeSourceCount();*/ /*bool noSourceSelected = miraiInstance_->getSources().size() == view_.activeSourceCount();*/
@ -413,13 +411,13 @@ void AppWindow::reloadSources()
/*models().set_no_source_selected(noSourceSelected);*/ /*models().set_no_source_selected(noSourceSelected);*/
} }
void AppWindow::refreshModels() void app_logic::refreshModels()
{ {
show_all_sources(); show_all_sources();
update_views(); update_views();
} }
void AppWindow::update_views() void app_logic::update_views()
{ {
update_sidebar_view(); update_sidebar_view();
update_calendar_view(); update_calendar_view();
@ -427,16 +425,16 @@ void AppWindow::update_views()
update_available_sources(); update_available_sources();
} }
void AppWindow::update_available_sources() void app_logic::update_available_sources()
{ {
const auto &sources = miraiInstance_->getSources(); const auto &sources = _mirai_core->getSources();
auto available_sources = std::make_shared<slint::VectorModel<ui::AvailableSource>>(); auto available_sources = std::make_shared<slint::VectorModel<ui::AvailableSource>>();
auto available_sources_strings = std::make_shared<slint::VectorModel<slint::SharedString>>(); auto available_sources_strings = std::make_shared<slint::VectorModel<slint::SharedString>>();
for (auto &source : sources) { for (auto &source : sources) {
mirai::markdown_data_provider *sourceProvider = mirai::markdown_data_provider *sourceProvider =
dynamic_cast<mirai::markdown_data_provider *>(source->data_provider()); dynamic_cast<mirai::markdown_data_provider *>(source->data_provider());
const auto color = const auto color =
selenite::hexStringToColor(miraiInstance_->getSourceById(source->id)->color()); selenite::hexStringToColor(_mirai_core->getSourceById(source->id)->color());
auto source_color = slint::Color::from_rgb_uint8(color.r, color.g, color.b); auto source_color = slint::Color::from_rgb_uint8(color.r, color.g, color.b);
available_sources->push_back( available_sources->push_back(
@ -449,16 +447,16 @@ void AppWindow::update_available_sources()
models().set_available_sources_strings(available_sources_strings); models().set_available_sources_strings(available_sources_strings);
}; };
void AppWindow::update_sidebar_view() void app_logic::update_sidebar_view()
{ {
const auto &sources = miraiInstance_->getSources(); const auto &sources = _mirai_core->getSources();
auto new_sources = std::make_shared<slint::VectorModel<ui::SidebarViewSources>>(); auto new_sources = std::make_shared<slint::VectorModel<ui::SidebarViewSources>>();
for (auto &source : sources) { for (auto &source : sources) {
mirai::markdown_data_provider *sourceProvider = mirai::markdown_data_provider *sourceProvider =
dynamic_cast<mirai::markdown_data_provider *>(source->data_provider()); dynamic_cast<mirai::markdown_data_provider *>(source->data_provider());
const auto color = const auto color =
selenite::hexStringToColor(miraiInstance_->getSourceById(source->id)->color()); selenite::hexStringToColor(_mirai_core->getSourceById(source->id)->color());
auto source_color = slint::Color::from_rgb_uint8(color.r, color.g, color.b); auto source_color = slint::Color::from_rgb_uint8(color.r, color.g, color.b);
new_sources->push_back({ new_sources->push_back({
.id = source->id, .id = source->id,
@ -494,12 +492,12 @@ get_events_for_date(const std::vector<mirai::event> &events, const mirai::date &
return std::ranges::to<std::vector>(filtered_events); return std::ranges::to<std::vector>(filtered_events);
} }
void AppWindow::update_calendar_view() void app_logic::update_calendar_view()
{ {
auto today = mirai::date(std::chrono::system_clock::now()); auto today = mirai::date(std::chrono::system_clock::now());
auto new_slint_dates = std::make_shared<slint::VectorModel<ui::CalendarViewDate>>(); auto new_slint_dates = std::make_shared<slint::VectorModel<ui::CalendarViewDate>>();
auto &sources = miraiInstance_->getSources(); auto &sources = _mirai_core->getSources();
auto all_events = merge_all_events_from_sources(sources); auto all_events = merge_all_events_from_sources(sources);
for (int day_index = 0; day_index < 7; ++day_index) { for (int day_index = 0; day_index < 7; ++day_index) {
@ -520,7 +518,7 @@ void AppWindow::update_calendar_view()
for (int event_index = 0; event_index < events_for_date.size(); ++event_index) { for (int event_index = 0; event_index < events_for_date.size(); ++event_index) {
auto &current_event = events_for_date.at(event_index); auto &current_event = events_for_date.at(event_index);
// TODO directly remove the source instead of this workaround after data layer refacto // TODO directly remove the source instead of this workaround after data layer refacto
auto source = miraiInstance_->getSourceById(current_event.source_id()); auto source = _mirai_core->getSourceById(current_event.source_id());
if (!should_show_source(*source)) { if (!should_show_source(*source)) {
continue; continue;
} }
@ -615,12 +613,12 @@ std::vector<tasks_date_group> group_tasks_by_date(const std::vector<mirai::task>
return dates_group; return dates_group;
}; };
void AppWindow::update_tasks_view() void app_logic::update_tasks_view()
{ {
auto today = mirai::date(std::chrono::system_clock::now()); auto today = mirai::date(std::chrono::system_clock::now());
auto new_slint_dates = std::make_shared<slint::VectorModel<ui::TasksViewDate>>(); auto new_slint_dates = std::make_shared<slint::VectorModel<ui::TasksViewDate>>();
auto &sources = miraiInstance_->getSources(); auto &sources = _mirai_core->getSources();
auto all_tasks_dates = group_tasks_by_date(get_all_tasks_from_sources(sources)); auto all_tasks_dates = group_tasks_by_date(get_all_tasks_from_sources(sources));
for (int date_index = 0; date_index < all_tasks_dates.size(); ++date_index) { for (int date_index = 0; date_index < all_tasks_dates.size(); ++date_index) {
@ -639,7 +637,7 @@ void AppWindow::update_tasks_view()
for (auto &source_group : current_date_group.sources) { for (auto &source_group : current_date_group.sources) {
auto new_slint_source = ui::TasksViewSource(); auto new_slint_source = ui::TasksViewSource();
auto new_slint_tasks = std::make_shared<slint::VectorModel<ui::TasksViewTask>>(); auto new_slint_tasks = std::make_shared<slint::VectorModel<ui::TasksViewTask>>();
auto source = miraiInstance_->getSourceById(source_group.id); auto source = _mirai_core->getSourceById(source_group.id);
if (!should_show_source(*source)) { if (!should_show_source(*source)) {
continue; continue;
} }
@ -657,7 +655,7 @@ void AppWindow::update_tasks_view()
new_slint_source.tasks = new_slint_tasks; new_slint_source.tasks = new_slint_tasks;
new_slint_source.id = source_group.id; new_slint_source.id = source_group.id;
new_slint_source.name = new_slint_source.name =
slint::SharedString(miraiInstance_->getSourceById(source_group.id)->name()); slint::SharedString(_mirai_core->getSourceById(source_group.id)->name());
const auto color = selenite::hexStringToColor(source->color()); const auto color = selenite::hexStringToColor(source->color());
new_slint_source.color = slint::Color::from_rgb_uint8(color.r, color.g, color.b); new_slint_source.color = slint::Color::from_rgb_uint8(color.r, color.g, color.b);
if (new_slint_source.tasks->row_count() > 0) { if (new_slint_source.tasks->row_count() > 0) {
@ -683,58 +681,58 @@ void AppWindow::update_tasks_view()
} }
} }
void AppWindow::show_source(const mirai::source &source) void app_logic::show_source(const mirai::source &source)
{ {
_source_filters.insert_or_assign(source.id, true); _source_filters.insert_or_assign(source.id, true);
} }
void AppWindow::hide_source(const mirai::source &source) void app_logic::hide_source(const mirai::source &source)
{ {
_source_filters.insert_or_assign(source.id, false); _source_filters.insert_or_assign(source.id, false);
} }
bool AppWindow::should_show_source(const mirai::source &source) const bool app_logic::should_show_source(const mirai::source &source) const
{ {
const int source_id = source.id; const int source_id = source.id;
return _source_filters.contains(source_id) && _source_filters.at(source_id); return _source_filters.contains(source_id) && _source_filters.at(source_id);
} }
void AppWindow::show_all_sources() void app_logic::show_all_sources()
{ {
auto &sources = miraiInstance_->getSources(); auto &sources = _mirai_core->getSources();
for (auto &source : sources) { for (auto &source : sources) {
show_source(*source); show_source(*source);
} }
} }
void AppWindow::hide_all_sources() void app_logic::hide_all_sources()
{ {
} }
void AppWindow::show_completed_tasks() void app_logic::show_completed_tasks()
{ {
} }
void AppWindow::hide_completed_tasks() void app_logic::hide_completed_tasks()
{ {
} }
bool AppWindow::should_hide_completed_tasks() const bool app_logic::should_hide_completed_tasks() const
{ {
return true; return true;
} }
void AppWindow::run() void app_logic::run()
{ {
mainWindow_->run(); _main_window->run();
} }
const ui::AppModels &AppWindow::models() const ui::AppModels &app_logic::models()
{ {
return mainWindow_->global<ui::AppModels>(); return _main_window->global<ui::AppModels>();
} }
const ui::AppActions &AppWindow::actions() const ui::AppActions &app_logic::actions()
{ {
return mainWindow_->global<ui::AppActions>(); return _main_window->global<ui::AppActions>();
} }

View file

@ -13,11 +13,11 @@
#include <unordered_map> #include <unordered_map>
#include <vector> #include <vector>
class AppWindow class app_logic
{ {
public: public:
AppWindow(mirai::core *mirai); app_logic(mirai::core *mirai);
void run(); void run();
@ -50,9 +50,9 @@ class AppWindow
ui::TasksView _tasks_view; ui::TasksView _tasks_view;
ui::CalendarView _calendar_view; ui::CalendarView _calendar_view;
slint::ComponentHandle<ui::AppWindow> mainWindow_ = ui::AppWindow::create(); slint::ComponentHandle<ui::AppWindow> _main_window = ui::AppWindow::create();
mirai::core *miraiInstance_; mirai::core *_mirai_core;
std::unordered_map<int, bool> _source_filters; std::unordered_map<int, bool> _source_filters;
bool _should_hide_completed_tasks; bool _should_hide_completed_tasks;

View file

@ -4,10 +4,10 @@
* The license can be found in the LICENSE file or at https://www.gnu.org/licenses/gpl-3.0.txt * The license can be found in the LICENSE file or at https://www.gnu.org/licenses/gpl-3.0.txt
*/ */
#include "app_logic.h"
#include "evalyte-cpp-common/evalyte.h" #include "evalyte-cpp-common/evalyte.h"
#include "mirai-core/core.h" #include "mirai-core/core.h"
#include "ui.h" #include "ui.h"
#include "windows/AppWindow/AppWindow.h"
#include <cstdlib> #include <cstdlib>
#include <string> #include <string>
@ -16,7 +16,7 @@ int main(int argc, char **argv)
evalyte::create_required_directories("mirai"); evalyte::create_required_directories("mirai");
const auto config_file_path = evalyte::config_directory_path("mirai") + "/config.json"; const auto config_file_path = evalyte::config_directory_path("mirai") + "/config.json";
mirai::core mirai{config_file_path}; mirai::core mirai{config_file_path};
AppWindow appWindow{&mirai}; app_logic app{&mirai};
appWindow.run(); app.run();
return 0; return 0;
} }