Refactor the whole structure, no more separation for C++ and Slint files

This commit is contained in:
Vyn 2024-11-04 14:45:27 +01:00
parent d6c781faa2
commit 893fcc11e3
35 changed files with 920 additions and 518 deletions

View file

@ -0,0 +1,59 @@
/*
* 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 "AddSourceWindow.h"
#include "../../SeleniteSetup.h"
#include "evalyte-cpp-common/evalyte.h"
#include "mirai-core/MarkdownDataProvider.h"
#include "mirai-core/Mirai.h"
#include "slint.h"
#include "slint_string.h"
#include "ui.h"
#include <bits/chrono.h>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <optional>
#include <print>
#include <string>
AddSourceWindow::AddSourceWindow(mirai::Mirai *miraiInstance) : miraiInstance_(miraiInstance)
{
window_->set_default_source_path(slint::SharedString(evalyte::dataDirectoryPath("mirai") + "/")
);
const auto palettePath = std::string(getenv("HOME")) + "/.config/evalyte/theme.json";
const auto palette = selenite::parseJson(palettePath);
if (palette.has_value()) {
setSelenitePalette(window_->global<ui::Palette>(), palette.value());
}
setupCallbacks();
}
void AddSourceWindow::setupCallbacks()
{
window_->on_add_source([&](ui::AddSourceParam params) {
std::unique_ptr<mirai::DataProvider> file =
std::make_unique<mirai::MarkdownDataProvider>(std::string(params.path));
miraiInstance_->addSource(
std::string(params.name), std::string(params.type), std::move(file)
);
window_->hide();
});
}
void AddSourceWindow::open()
{
window_->show();
}
void AddSourceWindow::close()
{
window_->hide();
}

View file

@ -0,0 +1,28 @@
/*
* 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
*/
#pragma once
#include "mirai-core/Mirai.h"
#include "slint.h"
#include "ui.h"
class AddSourceWindow
{
public:
AddSourceWindow(mirai::Mirai *mirai);
void open();
void close();
private:
void setupCallbacks();
std::shared_ptr<slint::VectorModel<ui::Source>> sources_;
slint::ComponentHandle<ui::AddSourceWindow> window_ = ui::AddSourceWindow::create();
mirai::Mirai *miraiInstance_;
};

View file

@ -0,0 +1,46 @@
import { VerticalBox, CheckBox } from "std-widgets.slint";
import { VButton, VText, VTextInput, Palette } from "@selenite";
export struct AddSourceParam {
name: string,
type: string,
path: string
}
export component AddSourceWindow inherits Window {
title: "Mirai - Add source";
min-height: 100px;
max-height: 4000px; // needed, otherwise the window wants to fit the content (on Swaywm)
min-width: 400px;
default-font-size: 16px;
background: Palette.background;
in-out property <string> default-source-path;
callback add-source(AddSourceParam);
VerticalLayout {
padding: 16px;
spacing: 8px;
nameInput := VTextInput {
label: "Name";
text: "todo";
}
pathInput := VTextInput {
label: "Path";
text: root.default-source-path + nameInput.text + ".md";
}
VButton {
text: "Create";
clicked => {
root.add-source({
name: nameInput.text,
type: "FileSystemMarkdown",
path: pathInput.text
})
}
}
}
}
export { Palette } // Export to make it visible to the C++ backend

View file

@ -0,0 +1,61 @@
import { Date, Time } from "std-widgets.slint";
import { CalendarDay } from "../../components/Calendar.slint";
export struct NewTaskData {
sourceId: int,
eventId: int,
title: string,
scheduled: bool,
date: Date
}
export struct SaveTaskData {
sourceId: int,
id: int,
title: string,
scheduled: bool,
date: Date,
}
export struct NewEventParams {
sourceId: int,
title: string,
date: Date,
startsAt: Time,
endsAt: Time
}
export struct SaveEventParams {
sourceId: int,
id: int,
title: string,
date: Date,
startsAt: Time,
endsAt: Time
}
struct OpenNewTaskFormParams {
eventSourceId: int,
eventId: int,
}
export global AppWindowActions {
callback task-clicked(int, int);
callback source-clicked(int);
callback open-settings-window();
callback open-add-source-window();
callback open-edit-source-window(int);
callback open-new-task-form(OpenNewTaskFormParams);
callback open-edit-task-form(int, int);
callback open-new-event-form();
callback open-edit-event-form(int, int);
callback toggle-show-completed-tasks();
callback delete-task-clicked(int, int);
callback delete-event-clicked(int, int);
callback create-task(NewTaskData);
callback save-task(SaveTaskData);
callback create-event(NewEventParams);
callback save-event(SaveEventParams);
}

View file

@ -0,0 +1,422 @@
/*
* 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 "AppWindow.h"
#include "../../SeleniteSetup.h"
#include "../../shared/Utils.h"
#include "mirai-core/DateTime.h"
#include "mirai-core/Day.h"
#include "mirai-core/MarkdownDataProvider.h"
#include "mirai-core/Mirai.h"
#include "selenite/palette.h"
#include "slint.h"
#include "slint_string.h"
#include "ui.h"
#include <bits/chrono.h>
#include <cassert>
#include <charconv>
#include <chrono>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <memory>
#include <optional>
#include <ostream>
#include <print>
#include <ranges>
#include <string>
#include <string_view>
#include <vector>
AppWindow::AppWindow(mirai::Mirai *miraiInstance)
: miraiInstance_(miraiInstance), addSourceWindow_(miraiInstance),
editSourceWindow_(miraiInstance), settingsWindow_(miraiInstance), view_(miraiInstance)
{
sources_ = std::make_shared<slint::VectorModel<ui::Source>>();
days_ = std::make_shared<slint::VectorModel<ui::Day>>();
calendar_ = std::make_shared<slint::VectorModel<ui::CalendarDay>>();
unscheduledTasks_ = std::make_shared<slint::VectorModel<ui::TaskData>>();
auto sourcesNames = std::make_shared<slint::MapModel<ui::Source, slint::SharedString>>(
sources_,
[&](const ui::Source &a) {
return a.name;
}
);
const auto palettePath = std::string(getenv("HOME")) + "/.config/evalyte/theme.json";
const auto palette = selenite::parseJson(palettePath);
if (palette.has_value()) {
std::println(std::cerr, "Warning, no {} found", palettePath);
setSelenitePalette(mainWindow_->global<ui::Palette>(), palette.value());
}
bindSlintUtils(mainWindow_->global<ui::Utils>());
models().set_sources(sourcesNames);
models().set_sources_selected(sources_);
models().set_days(days_);
models().set_calendar(calendar_);
view_.setAllSources();
view_.update();
reloadSources();
reloadTasks();
setupCallbacks();
}
std::optional<ui::Date> stringToDate(const std::string &dateStr)
{
using std::operator""sv;
auto dateSplitView =
dateStr | std::views::split("-"sv) | std::views::transform([](auto v) -> int {
int i = 0;
std::from_chars(v.data(), v.data() + v.size(), i);
return i;
});
std::vector<int> dateSplit{dateSplitView.begin(), dateSplitView.end()};
if (dateSplit.size() != 3) {
return std::nullopt;
}
auto year = dateSplit[0];
auto month = dateSplit[1];
auto day = dateSplit[2];
return ui::Date{.year = year, .month = month, .day = day};
}
void AppWindow::setupCallbacks()
{
miraiInstance_->onSourceAdded([&](mirai::Source *source) {
refreshModels();
});
miraiInstance_->onSourceEdited([&](mirai::Source *source) {
refreshModels();
});
miraiInstance_->onSourceDeleted([&](int id) {
refreshModels();
});
actions().on_open_settings_window([&]() {
settingsWindow_.open();
});
actions().on_open_add_source_window([&]() {
addSourceWindow_.open();
});
actions().on_open_edit_source_window([&](int sourceId) {
auto source = miraiInstance_->getSourceById(sourceId);
assert(source);
editSourceWindow_.open(source);
});
actions().on_task_clicked([&](int sourceId, int taskId) {
auto source = miraiInstance_->getSourceById(sourceId);
assert(source);
auto task = source->getTaskById(taskId);
assert(task);
if (!task->checked() && !task->hasDate() && !task->hasEvent()) {
task->setDate(mirai::Date(mirai::Date(std::chrono::system_clock::now())));
}
task->setChecked(!task->checked());
miraiInstance_->save();
view_.update();
reloadTasks();
});
actions().on_source_clicked([&](int index) {
// index with value -1 is equal to no selection, aka "All"
if (index == -1) {
view_.setAllSources();
} else {
view_.removeSources();
const mirai::Source *source = miraiInstance_->getSourceById(index);
view_.addSource(*source);
}
models().set_default_source_index(index == -1 ? 0 : index);
view_.update();
reloadSources();
reloadTasks();
});
actions().on_delete_task_clicked([&](int sourceId, int taskId) {
auto source = miraiInstance_->getSourceById(sourceId);
assert(source);
auto task = source->getTaskById(taskId);
assert(task);
source->removeTask(*task);
miraiInstance_->save();
view_.update();
reloadTasks();
});
actions().on_toggle_show_completed_tasks([&] {
view_.hideCompletedTasks(!view_.shouldHideCompletedTasks());
view_.update();
reloadTasks();
});
actions().on_save_task([&](ui::SaveTaskData newTaskData) {
auto source = miraiInstance_->getSourceById(newTaskData.sourceId);
assert(source);
auto task = source->getTaskById(newTaskData.id);
assert(task.has_value());
const mirai::Date &date = SlintDateToMiraiDate(newTaskData.date);
const auto dayOpt = source->getDayByDate(date);
task->setTitle(std::string(newTaskData.title));
if (!task.value().hasEvent() && date.year != 0) {
task->setDate(date);
}
if (!task.value().hasEvent() && date.year == 0) {
task->unschedule();
}
miraiInstance_->save();
view_.update();
reloadTasks();
});
actions().on_create_task([&](ui::NewTaskData newTaskData) {
std::optional<mirai::Date> date = std::nullopt;
if (newTaskData.date.year != 0) {
date = SlintDateToMiraiDate(newTaskData.date);
}
auto source = miraiInstance_->getSourceById(newTaskData.sourceId);
std::optional<mirai::Event> event = std::nullopt;
if (newTaskData.eventId >= 0) {
event = source->getEventById(newTaskData.eventId);
}
source->createTask({
.title = std::string(newTaskData.title),
.event = event,
.date = date,
});
miraiInstance_->save();
view_.update();
reloadTasks();
});
actions().on_delete_event_clicked([&](int sourceId, int eventId) {
auto source = miraiInstance_->getSourceById(sourceId);
assert(source);
auto event = source->getEventById(eventId);
assert(event.has_value());
source->removeEvent(event.value());
miraiInstance_->save();
view_.update();
reloadTasks();
});
actions().on_create_event([&](ui::NewEventParams newEventParams) {
const ui::Date &date = newEventParams.date;
const std::string dateStr = SlintDateToStdString(date);
auto source = miraiInstance_->getSourceById(newEventParams.sourceId);
source->createEvent({
.title = std::string(newEventParams.title),
.date = SlintDateToMiraiDate(newEventParams.date),
.startsAt = SlintTimeToMiraiTime(newEventParams.startsAt),
.endsAt = SlintTimeToMiraiTime(newEventParams.endsAt),
});
miraiInstance_->save();
view_.update();
reloadTasks();
});
actions().on_save_event([&](ui::SaveEventParams newEventParams) {
const ui::Date &date = newEventParams.date;
const std::string dateStr = SlintDateToStdString(date);
auto source = miraiInstance_->getSourceById(newEventParams.sourceId);
assert(source);
auto event = source->getEventById(newEventParams.id);
assert(event);
event->setTitle(std::string(newEventParams.title));
event->setStartTime(SlintTimeToMiraiTime(newEventParams.startsAt));
event->setEndTime(SlintTimeToMiraiTime(newEventParams.endsAt));
// TODO we can't change the date of the event for now.
miraiInstance_->save();
view_.update();
reloadTasks();
});
}
std::shared_ptr<slint::VectorModel<slint::SharedString>>
stdToSlintStringVector(const std::vector<std::string> &stdVector)
{
auto slintVector = std::make_shared<slint::VectorModel<slint::SharedString>>();
for (const auto &item : stdVector) {
slintVector->push_back(slint::SharedString(item));
}
return slintVector;
}
void AppWindow::reloadTasks()
{
days_->clear();
if (miraiInstance_->getSources().size() == 0) {
return;
}
auto todayDate = mirai::Date(std::chrono::system_clock::now());
auto dates = view_.getDates();
auto slintDays = std::make_shared<slint::VectorModel<ui::Day>>();
for (int dayIndex = 0; dayIndex < dates.size(); ++dayIndex) {
auto &currentDate = dates.at(dayIndex);
auto slintEvents = std::make_shared<slint::VectorModel<ui::Event>>();
auto slintDayTasks = std::make_shared<slint::VectorModel<ui::TaskData>>();
auto relativeDaysDiff = std::chrono::duration_cast<std::chrono::days>(
std::chrono::sys_days(currentDate.toStdChrono()) -
std::chrono::sys_days(todayDate.toStdChrono())
)
.count();
slintDays->push_back(ui::Day{
.date = MiraiDateToSlintDate(currentDate),
.events = slintEvents,
.tasks = slintDayTasks,
.isLate = currentDate < todayDate,
.isToday = currentDate == todayDate,
.relativeDaysDiff = static_cast<int>(relativeDaysDiff)
});
// Day's tasks
const std::vector<mirai::Task> tasksForDate = view_.getTasksForDate(currentDate);
for (int taskIndex = 0; taskIndex < tasksForDate.size(); ++taskIndex) {
auto &task = tasksForDate.at(taskIndex);
slintDayTasks->push_back({
.sourceId = task.sourceId(),
.eventId = -1,
.id = task.id(),
.title = slint::SharedString(task.title()),
.checked = task.checked(),
});
}
// Day's events
const std::vector<mirai::Event> eventsForDate = view_.getEventsForDate(currentDate);
for (int eventIndex = 0; eventIndex < eventsForDate.size(); ++eventIndex) {
auto &currentEvent = eventsForDate.at(eventIndex);
auto slintTasks = std::make_shared<slint::VectorModel<ui::TaskData>>();
slintEvents->push_back(ui::Event{
.sourceId = currentEvent.sourceId(),
.id = currentEvent.id(),
.title = slint::SharedString(currentEvent.title()),
.startsAt = MiraiTimeToSlintTime(currentEvent.startsAt()),
.endsAt = MiraiTimeToSlintTime(currentEvent.endsAt()),
.tasks = slintTasks,
});
auto eventTasks = currentEvent.queryTasks();
for (int taskIndex = 0; taskIndex < eventTasks.size(); ++taskIndex) {
auto &task = eventTasks.at(taskIndex);
slintTasks->push_back({
.sourceId = task.sourceId(),
.eventId = currentEvent.id(),
.id = task.id(),
.title = slint::SharedString(task.title()),
.checked = task.checked(),
});
}
}
}
days_ = slintDays;
models().set_days(days_);
auto unscheduledTasksView = view_.getUnscheduledTasks();
unscheduledTasks_->clear();
for (int taskIndex = 0; taskIndex < unscheduledTasksView.size(); ++taskIndex) {
auto &task = unscheduledTasksView.at(taskIndex);
unscheduledTasks_->push_back({
.sourceId = task.sourceId(),
.eventId = -1,
.id = task.id(),
.title = slint::SharedString(task.title()),
.checked = task.checked(),
});
}
models().set_unscheduled_tasks(unscheduledTasks_);
calendar_->clear();
for (int dayIndex = 0; dayIndex < 7; ++dayIndex) {
std::chrono::year_month_day nextDate =
std::chrono::floor<std::chrono::days>(std::chrono::system_clock::now()) +
std::chrono::days{dayIndex};
auto currentDate = mirai::Date{nextDate};
auto events = view_.getEventsForDate(currentDate);
auto slintEvents = std::make_shared<slint::VectorModel<ui::CalendarDayEvent>>();
auto relativeDaysDiff = std::chrono::duration_cast<std::chrono::days>(
std::chrono::sys_days(currentDate.toStdChrono()) -
std::chrono::sys_days(todayDate.toStdChrono())
)
.count();
if (relativeDaysDiff < 0 || relativeDaysDiff >= 3) {
continue;
}
const std::vector<mirai::Event> eventsForDate = view_.getEventsForDate(currentDate);
for (int eventIndex = 0; eventIndex < eventsForDate.size(); ++eventIndex) {
auto &currentEvent = eventsForDate.at(eventIndex);
slintEvents->push_back(ui::CalendarDayEvent{
.title = slint::SharedString(currentEvent.title()),
.startsAt = MiraiTimeToSlintTime(currentEvent.startsAt()),
.endsAt = MiraiTimeToSlintTime(currentEvent.endsAt()),
});
}
auto calendarDay = ui::CalendarDay{
.events = slintEvents,
.date = MiraiDateToSlintDate(currentDate),
.header =
slint::SharedString(capitalize(formatDateRelative(MiraiDateToSlintDate(currentDate))
))
};
calendar_->push_back(calendarDay);
}
}
void AppWindow::reloadSources()
{
sources_->clear();
bool noSourceSelected = miraiInstance_->getSources().size() == view_.activeSourceCount();
for (const auto &source : miraiInstance_->getSources()) {
bool isSourceSelected = view_.isSourceSelected(*source);
mirai::MarkdownDataProvider *sourceProvider =
dynamic_cast<mirai::MarkdownDataProvider *>(source->dataProvider());
sources_->push_back(
{.id = source->id,
.name = slint::SharedString(source->name()),
.selected = isSourceSelected && !noSourceSelected,
.path = slint::SharedString(sourceProvider->path())}
);
}
models().set_no_source_selected(noSourceSelected);
}
void AppWindow::refreshModels()
{
view_.setAllSources();
view_.update();
reloadSources();
reloadTasks();
}
void AppWindow::run()
{
mainWindow_->run();
}
const ui::AppWindowModels &AppWindow::models()
{
return mainWindow_->global<ui::AppWindowModels>();
}
const ui::AppWindowActions &AppWindow::actions()
{
return mainWindow_->global<ui::AppWindowActions>();
}

View file

@ -0,0 +1,46 @@
/*
* 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
*/
#pragma once
#include "../AddSourceWindow/AddSourceWindow.h"
#include "../EditSourceWindow/EditSourceWindow.h"
#include "../SettingsWindow/SettingsWindow.h"
#include "mirai-core/Mirai.h"
#include "mirai-core/View.h"
#include "slint.h"
#include "ui.h"
class AppWindow
{
public:
AppWindow(mirai::Mirai *mirai);
void run();
void reloadSources();
void reloadTasks();
void refreshModels();
private:
void setupCallbacks();
const ui::AppWindowModels &models();
const ui::AppWindowActions &actions();
std::shared_ptr<slint::VectorModel<ui::Source>> sources_;
std::shared_ptr<slint::VectorModel<ui::Day>> days_;
std::shared_ptr<slint::VectorModel<ui::CalendarDay>> calendar_;
std::shared_ptr<slint::VectorModel<ui::TaskData>> unscheduledTasks_;
slint::ComponentHandle<ui::AppWindow> mainWindow_ = ui::AppWindow::create();
SettingsWindow settingsWindow_;
AddSourceWindow addSourceWindow_;
EditSourceWindow editSourceWindow_;
mirai::Mirai *miraiInstance_;
mirai::View view_;
};

View file

@ -0,0 +1,26 @@
import { AppWindowModels } from "Models.slint";
import { Button, VerticalBox, CheckBox } from "std-widgets.slint";
import { SideBar } from "views/SideBar.slint";
import { MainView } from "views/TasksView.slint";
import { SettingsWindow } from "../SettingsWindow/SettingsWindow.slint";
import { AddSourceWindow } from "../AddSourceWindow//AddSourceWindow.slint";
import { EditSourceWindow } from "../EditSourceWindow/EditSourceWindow.slint";
import { Palette } from "@selenite";
export component AppWindow inherits Window {
title: "Mirai";
min-height: 100px;
max-height: 4000px; // needed, otherwise the window wants to fit the content (on Swaywm)
default-font-size: 16px;
HorizontalLayout {
SideBar {}
MainView {
horizontal-stretch: 1;
}
}
}
export { AppWindowModels, Palette, SettingsWindow, AddSourceWindow, EditSourceWindow } // Export to make it visible to the C++ backend

View file

@ -0,0 +1,50 @@
import { Date, Time } from "std-widgets.slint";
import { CalendarDay } from "../../components/Calendar.slint";
export struct Source {
id: int,
name: string,
selected: bool,
path: string
}
export struct TaskData {
sourceId: int,
eventId: int,
id: int,
title: string,
date: Date,
checked: bool,
}
export struct Event {
sourceId: int,
id: int,
title: string,
startsAt: Time,
endsAt: Time,
tasks: [TaskData],
}
export struct Day {
sourceId: int,
id: int,
date: Date,
events: [Event],
tasks: [TaskData],
isLate: bool,
isToday: bool,
relativeDaysDiff: int
}
export global AppWindowModels {
in-out property<[Source]> sources-selected;
in-out property<int> default-source-index;
in-out property<[string]> sources;
in-out property<bool> no-source-selected;
in-out property<[Day]> days;
in-out property<[CalendarDay]> calendar;
in-out property<[TaskData]> unscheduled-tasks;
callback get-source-id-from-name(string) -> int;
}

View file

@ -0,0 +1,65 @@
import { AppWindowModels, TaskData } from "../Models.slint";
import { AppWindowActions } from "../Actions.slint";
import { VButton, ToggleButton, VActionButton, VText, Svg, Palette } from "@selenite";
export component SideBar inherits Rectangle {
background: Palette.pane;
VerticalLayout {
height: parent.height;
padding: 16px;
spacing: 16px;
HorizontalLayout {
alignment: space-between;
spacing: 64px;
VText {
text: "Sources";
font-size: 1.5rem;
}
VActionButton {
icon-svg: Svg.plus;
icon-colorize: Palette.green;
background: transparent;
clicked => { AppWindowActions.open-add-source-window() }
}
}
VerticalLayout {
alignment: space-between;
vertical-stretch: 1;
VerticalLayout {
vertical-stretch: 1;
spacing: 4px;
ToggleButton {
text: "All";
text-alignment: left;
active: AppWindowModels.no-source-selected;
clicked => { AppWindowActions.source-clicked(-1) }
}
for item[index] in AppWindowModels.sources-selected: ToggleButton {
text: item.name;
text-alignment: left;
active: item.selected;
clicked => { AppWindowActions.source-clicked(item.id) }
VActionButton {
visible: parent.active;
icon-svg: Svg.cog;
background: transparent;
clicked => { AppWindowActions.open-edit-source-window(item.id) }
}
}
}
VerticalLayout {
spacing: 4px;
/*VButton {
icon-svg: Svg.cog;
text: "Settings";
background: transparent;
clicked => { AppWindowModels.open-settings-window() }
}*/
}
}
}
}

View file

@ -0,0 +1,221 @@
import { AppWindowModels, TaskData } from "../Models.slint";
import { AppWindowActions, NewTaskData, SaveTaskData } from "../Actions.slint";
import { Button, VerticalBox, CheckBox, ScrollView, ComboBox } from "std-widgets.slint";
import { TaskLine } from "../../../components/TaskLine.slint";
import { EventGroup } from "../../../components/EventGroup.slint";
import { Calendar } from "../../../components/Calendar.slint";
import { VPopupIconMenu, VDatePicker, VTimePicker, VCheckBox, VButton, VTag, VText, VTextInput, Svg, Palette } from "@selenite";
import { CreateTaskOrEvent } from "../../../components/CreateTaskOrEvent.slint";
import { Utils } from "../../../shared/Utils.slint";
export component MainView inherits Rectangle {
background: Palette.background;
private property<string> icon-visible: Svg.visible;
private property<string> icon-not-visible: Svg.not-visible;
private property<bool> completed-tasks-visible: false;
HorizontalLayout {
VerticalLayout {
horizontal-stretch: 1;
padding: 16px;
spacing: 16px;
HorizontalLayout {
horizontal-stretch: 1;
alignment: start;
spacing: 8px;
VButton {
text: "Show/Hide completed tasks";
clicked => {
AppWindowActions.toggle-show-completed-tasks();
completed-tasks-visible = !completed-tasks-visible;
}
icon-svg: completed-tasks-visible ? icon-visible : icon-not-visible;
icon-colorize: Palette.control-foreground;
}
}
Rectangle {
horizontal-stretch: 1;
background: Palette.background.brighter(0.2);
height: 1px;
}
CreateTaskOrEvent {
sources: AppWindowModels.sources;
create-task(data) => {
AppWindowActions.create-task({
sourceId: data.sourceId,
eventId: -1,
title: data.title,
scheduled: data.date.year != 0,
date: data.date
})
}
create-event(data) => {
AppWindowActions.create-event({
sourceId: data.sourceId,
title: data.title,
date: data.date,
startsAt: data.startsAt,
endsAt: data.endsAt,
});
}
}
Flickable {
horizontal-stretch: 1;
VerticalLayout {
alignment: start;
spacing: 16px;
if AppWindowModels.days.length == 0 && AppWindowModels.unscheduled-tasks.length == 0 : VText {
text: "There is no task to show";
horizontal-alignment: center;
vertical-alignment: center;
}
for day[dayIndex] in AppWindowModels.days: VerticalLayout {
Rectangle {
background: Palette.card-background;
border-radius: 8px;
VerticalLayout {
padding: 16px;
HorizontalLayout {
alignment: start;
VText {
text: Utils.format-date(day.date);
color: day.isLate ? Palette.orange : Palette.foreground;
font-size: 1.2rem;
}
VerticalLayout {
alignment: center;
VText {
text: day.relativeDaysDiff == 0 ? " - today" :
day.relativeDaysDiff == 1 ? " - tomorrow" :
day.relativeDaysDiff == -1 ? " - yesterday" :
day.relativeDaysDiff > 0 ? " - in \{day.relativeDaysDiff} days" :
" - \{-day.relativeDaysDiff} days ago";
color: Palette.foreground-hint;
font-size: 1rem;
}
}
}
for event[eventIndex] in day.events: VerticalLayout {
padding-top: 16px;
EventGroup {
event: event;
add-task(data) => {
AppWindowActions.create-task({
sourceId: event.sourceId,
eventId: event.id,
title: data.title,
});
}
edit-task(taskId, data) => {
AppWindowActions.save-task({
id: taskId,
sourceId: event.sourceId,
title: data.title,
});
}
delete-task(taskId) => {
AppWindowActions.delete-task-clicked(event.sourceId, taskId)
}
toggle-check-task(taskId) => {
AppWindowActions.task-clicked(event.sourceId, taskId);
}
delete => {
AppWindowActions.delete-event-clicked(event.sourceId, event.id)
}
edit(data) => {
AppWindowActions.save-event({
sourceId: event.sourceId,
id: event.id,
title: data.title,
//date: event.date,
startsAt: event.startsAt,
endsAt: event.endsAt,
});
}
}
}
for task[taskIndex] in day.tasks: VerticalLayout {
padding-top: taskIndex == 0 ? 16px : 0px;
padding-bottom: 8px;
TaskLine {
title: task.title;
scheduled: task.date.year != 0;
date: day.date;
checked: task.checked;
allow-edit-date: true;
delete => {
AppWindowActions.delete-task-clicked(task.sourceId, task.id)
}
toggle-check => {
AppWindowActions.task-clicked(task.sourceId, task.id);
}
edited(data) => {
AppWindowActions.save-task({
id: task.id,
sourceId: task.sourceId,
title: data.title,
scheduled: data.scheduled,
date: data.date
})
}
}
}
}
}
}
if AppWindowModels.unscheduled-tasks.length > 0 : VerticalLayout {
Rectangle {
background: Palette.card-background;
border-radius: 8px;
VerticalLayout {
padding: 16px;
HorizontalLayout {
alignment: start;
VText {
text: "Unscheduled";
color: Palette.foreground;
font-size: 1.2rem;
}
}
for task[taskIndex] in AppWindowModels.unscheduled-tasks: VerticalLayout {
padding-top: taskIndex == 0 ? 16px : 0px;
padding-bottom: 8px;
TaskLine {
title: task.title;
checked: task.checked;
allow-edit-date: true;
delete => {
AppWindowActions.delete-task-clicked(task.sourceId, task.id)
}
toggle-check => {
AppWindowActions.task-clicked(task.sourceId, task.id);
}
edited(data) => {
AppWindowActions.save-task({
id: task.id,
sourceId: task.sourceId,
title: data.title,
scheduled: data.scheduled,
date: data.date
})
}
}
}
}
}
}
}
}
}
Calendar {
init => { debug("cal len", AppWindowModels.calendar.length) }
days: AppWindowModels.calendar;
}
}
}

View file

@ -0,0 +1,61 @@
/*
* 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 "EditSourceWindow.h"
#include "../../SeleniteSetup.h"
#include "mirai-core/MarkdownDataProvider.h"
#include "mirai-core/Mirai.h"
#include "slint.h"
#include "slint_string.h"
#include "ui.h"
#include <bits/chrono.h>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <optional>
#include <print>
#include <string>
EditSourceWindow::EditSourceWindow(mirai::Mirai *miraiInstance) : miraiInstance_(miraiInstance)
{
const auto palettePath = std::string(getenv("HOME")) + "/.config/evalyte/theme.json";
const auto palette = selenite::parseJson(palettePath);
if (palette.has_value()) {
setSelenitePalette(window_->global<ui::Palette>(), palette.value());
}
setupCallbacks();
}
void EditSourceWindow::setupCallbacks()
{
window_->on_modify_source([&](ui::ModifySourceParam params) {
miraiInstance_->editSource(params.id, std::string(params.name), std::string(params.path));
close();
});
window_->on_delete_source([&](int sourceId) {
miraiInstance_->deleteSource(sourceId);
close();
});
}
void EditSourceWindow::open(mirai::Source *source)
{
assert(source != nullptr);
auto markdownSource = dynamic_cast<mirai::MarkdownDataProvider *>(source->dataProvider());
window_->set_id(source->id);
window_->set_name(slint::SharedString(source->name()));
window_->set_path(slint::SharedString(markdownSource->path()));
window_->show();
}
void EditSourceWindow::close()
{
window_->hide();
}

View file

@ -0,0 +1,29 @@
/*
* 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
*/
#pragma once
#include "mirai-core/Mirai.h"
#include "mirai-core/Source.h"
#include "slint.h"
#include "ui.h"
class EditSourceWindow
{
public:
EditSourceWindow(mirai::Mirai *mirai);
void open(mirai::Source *source);
void close();
private:
void setupCallbacks();
std::shared_ptr<slint::VectorModel<ui::Source>> sources_;
slint::ComponentHandle<ui::EditSourceWindow> window_ = ui::EditSourceWindow::create();
mirai::Mirai *miraiInstance_;
};

View file

@ -0,0 +1,54 @@
import { VerticalBox, CheckBox } from "std-widgets.slint";
import { VButton, VText, VTextInput, Palette } from "@selenite";
export struct ModifySourceParam {
id: int,
name: string,
path: string
}
export component EditSourceWindow inherits Window {
in-out property <int> id;
in-out property name <=> nameInput.text;
in-out property path <=> pathInput.text;
title: "Mirai - Edit source";
min-height: 100px;
max-height: 4000px; // needed, otherwise the window wants to fit the content (on Swaywm)
min-width: 400px;
default-font-size: 16px;
background: Palette.background;
callback modify-source(ModifySourceParam);
callback delete-source(int);
VerticalLayout {
padding: 16px;
spacing: 8px;
nameInput := VTextInput {
label: "Name";
}
pathInput := VTextInput {
label: "Path";
}
VButton {
text: "Save";
clicked => {
root.modify-source({
id: root.id,
name: nameInput.text,
path: pathInput.text
})
}
}
VButton {
text: "Delete";
background-color: Palette.red;
double-clicked => {
root.delete-source(root.id)
}
}
}
}
export { Palette } // Export to make it visible to the C++ backend

View file

@ -0,0 +1,50 @@
/*
* 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 "SettingsWindow.h"
#include "../../SeleniteSetup.h"
#include "evalyte-cpp-common/evalyte.h"
#include "mirai-core/DataProvider.h"
#include "mirai-core/MarkdownDataProvider.h"
#include "mirai-core/Mirai.h"
#include "slint.h"
#include "slint_string.h"
#include <bits/chrono.h>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <memory>
#include <optional>
#include <print>
#include <string>
SettingsWindow::SettingsWindow(mirai::Mirai *miraiInstance) : miraiInstance_(miraiInstance)
{
const auto palettePath = std::string(getenv("HOME")) + "/.config/evalyte/theme.json";
const auto palette = selenite::parseJson(palettePath);
if (palette.has_value()) {
setSelenitePalette(window_->global<ui::Palette>(), palette.value());
}
setupCallbacks();
}
void SettingsWindow::setupCallbacks()
{
}
void SettingsWindow::open()
{
window_->show();
}
void SettingsWindow::close()
{
window_->hide();
}

View file

@ -0,0 +1,28 @@
/*
* 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
*/
#pragma once
#include "mirai-core/Mirai.h"
#include "slint.h"
#include "ui.h"
class SettingsWindow
{
public:
SettingsWindow(mirai::Mirai *mirai);
void open();
void close();
private:
void setupCallbacks();
std::shared_ptr<slint::VectorModel<ui::Source>> sources_;
slint::ComponentHandle<ui::SettingsWindow> window_ = ui::SettingsWindow::create();
mirai::Mirai *miraiInstance_;
};

View file

@ -0,0 +1,27 @@
import { AppWindowModels } from "../AppWindow/Models.slint";
import { Button, VerticalBox, CheckBox } from "std-widgets.slint";
import { VText, VTextInput, Palette } from "@selenite";
export component SettingsWindow inherits Window {
title: "Mirai - Settings";
min-height: 100px;
max-height: 4000px; // needed, otherwise the window wants to fit the content (on Swaywm)
default-font-size: 16px;
background: Palette.background;
VerticalLayout {
padding: 16px;
spacing: 8px;
for source[source-index] in AppWindowModels.sources-selected: VerticalLayout {
VText {
text: source.name;
}
VTextInput {
text: source.path;
}
}
}
}
export { AppWindowModels, Palette } // Export to make it visible to the C++ backend