Complete Modern desktop widgets and file libraries (by Codex)

This commit is contained in:
Codex committed 2026-10-26 00:08:50 +01:00
1 parent db89d889f7
commit 31bcf93205
43 files changed
+3134 -484

No files matched your search

+62 -220
View File
@@ -14,52 +14,41 @@ void App::render_context() {
root->SetProperty("display", context_open_ ? "block" : "none");
if (!context_open_ || !context_dirty_)
return;
std::string body;
if (!context_id_.empty()) {
const auto &o = desktop_["objects"].at(context_id_);
body += btn("context-pin",
o.value("pinned", true) ? "Fixierung lösen" : "Fixieren");
if (o.value("widget", false)) {
body += btn("context-remove", "Widget entfernen");
for (const auto &d : widget_definitions())
if (d.id == context_id_)
for (auto [w, h] : d.sizes)
body += btn("context-size",
std::to_string(w) + " × " + std::to_string(h), {},
"data-id=\"" + std::to_string(w) + "x" +
std::to_string(h) + "\"");
const auto item=[&](const std::string &action,const std::string &label,const std::string &symbol,const std::string &extra=std::string()){
return btn(action,glyph(symbol,19./scale_)+"<span>"+esc(label)+"</span>"+((extra.find("aria-checked=\"true\"")!=std::string::npos||extra.find("data-checked=\"true\"")!=std::string::npos)?"<span class=\"context-check\">"+glyph("check",14./scale_)+"</span>":""),"context-action",extra);
};
std::string body=item("context-undo","Rückgängig","undo",undo_.empty()?"disabled":"");
if(!context_id_.empty()){
const auto &o=desktop_.at("objects").at(context_id_);
const bool pinned=o.value("pinned",true);
body+=item("context-pin",pinned?"Fixierung lösen":"Fixieren","lock");
if(o.value("widget",false)){
body+="<div class=\"context-label\">Widgetgröße</div>";
for(const auto &d:widget_definitions())if(d.id==context_id_)for(const auto &[w,h]:d.sizes){
const bool selected=o.at("w")==w*248-24 && o.at("h")==h*248-24;
body+=item("context-size",std::to_string(w)+" × "+std::to_string(h),"grid","data-id=\""+std::to_string(w)+"x"+std::to_string(h)+"\" "+(pinned?"disabled":"")+(selected?" data-checked=\"true\"":""));
}
body+=item("context-remove","Widget entfernen","trash",pinned?"disabled":"");
}
body += "<div class=\"separator\"></div>";
}else{
bool pinned=true;for(const auto &[id,o]:desktop_.at("objects").items())if(o.at("visible")==true&&o.at("pinned")==false)pinned=false;
body+=item("context-pin-all","Alles fixieren","lock",std::string("role=\"menuitemcheckbox\" aria-checked=\"")+(pinned?"true":"false")+"\"");
body+=item("context-arrange","Am Raster anordnen","grid");
body+=item("context-grid",desktop_.value("grid",true)?"Raster deaktivieren":"Raster aktivieren","grid",std::string("role=\"menuitemcheckbox\" aria-checked=\"")+(desktop_.value("grid",true)?"true":"false")+"\"");
body+=item("context-widgets","Widget hinzufügen","plus")+item("context-view-save","Ansicht speichern","save")+item("context-view-load","Ansicht laden","folder")+item("context-background","Hintergrundbild ändern","image")+item("context-quit","Spiel beenden","power");
}
bool all_pinned = true;
for (const auto &[id, o] : desktop_["objects"].items()) {
(void)id;
if (o.value("visible", false) && !o.value("pinned", true))
all_pinned = false;
}
body +=
btn("context-undo", "Rückgängig", {}, undo_.empty() ? "disabled" : "") +
btn("context-pin-all", all_pinned ? "Alles lösen" : "Alles fixieren") +
btn("context-grid", desktop_.value("grid", true) ? "Raster ausschalten"
: "Raster einschalten") +
btn("context-arrange", "Am Raster anordnen") +
"<div class=\"separator\"></div>" +
btn("context-widgets", "Widget hinzufügen") +
btn("context-view-save", "Ansicht speichern") +
btn("context-view-load", "Ansicht laden") +
btn("context-background", "Hintergrundbild ändern") +
"<div class=\"separator\"></div>" + btn("context-quit", "Spiel beenden");
root->SetInnerRML(body);
context_->Update();
const auto size = context_->GetDimensions();
root->SetProperty(
"left",
scalar(std::clamp(context_x_, 4, std::max(4, size.x - 288))) + "px");
scalar(std::clamp(context_x_, 8, std::max(8, size.x - 278))) + "px");
root->SetProperty(
"top", scalar(std::clamp(
context_y_, 4,
std::max(4, size.y - int(root->GetBox().GetSize().y) - 8))) +
context_y_, 8,
std::max(8, size.y - int(root->GetBox().GetSize(Rml::BoxArea::Border).y) - 8))) +
"px");
Rml::ElementList buttons;root->QuerySelectorAll(buttons,"button");for(auto *button:buttons)if(!button->HasAttribute("disabled")){button->Focus(true);break;}
context_dirty_ = false;
}
void App::drag_event(Rml::Event &event) {
@@ -80,9 +69,8 @@ void App::drag_event(Rml::Event &event) {
return;
const auto id = n->GetAttribute<Rml::String>("data-object", "");
const auto &o = desktop_["objects"].at(id);
if (o.value("pinned", true))
return;
drag_ = {true, false, id, x, y, o.value("x", 0.0), o.value("y", 0.0)};
drag_.pinned=o.value("pinned",true);drag_undo_size_=undo_.size();
return;
}
}
@@ -92,10 +80,10 @@ void App::drag_event(Rml::Event &event) {
if (type == "mousemove") {
if (!drag_.moved && std::hypot(x - drag_.x, y - drag_.y) < 6)
return;
if(drag_.pinned){drag_.moved=true;return;}
if (!drag_.moved) {
undo_.push_back(desktop_);
if (undo_.size() > 40)
undo_.erase(undo_.begin());
desktop_checkpoint(desktop_);
desktop_raise(drag_.id);
drag_.moved = true;
}
auto &o = desktop_["objects"][drag_.id];
@@ -114,15 +102,19 @@ void App::drag_event(Rml::Event &event) {
dirty_ = true;
}
if (type == "mouseup") {
if(drag_.pinned){suppress_click_=drag_.moved;drag_.active=false;return;}
if (drag_.moved) {
suppress_click_ = true;
auto &o = desktop_["objects"][drag_.id];
auto proposed = undo_.back();
const int x = o.value("x", 64), y = o.value("y", 224);
if (place_desktop_object(proposed, drag_.id, x, y))
desktop_ = std::move(proposed);
else
desktop_ = undo_.back();
const auto &trash=desktop_.at("objects").at("trash");
const auto px=(event.GetParameter<float>("mouse_x",0)-canvas_left_)/scale_,py=(event.GetParameter<float>("mouse_y",0)-canvas_top_)/scale_;
const bool remove=drag_.id!="trash"&&o.value("widget",false)&&trash.at("visible")==true&&px>=trash.at("x").get<int>()&&px<=trash.at("x").get<int>()+trash.at("w").get<int>()&&py>=trash.at("y").get<int>()&&py<=trash.at("y").get<int>()+trash.at("h").get<int>();
if(remove){proposed["objects"][drag_.id]["visible"]=false;desktop_=std::move(proposed);}
else if (place_desktop_object(proposed, drag_.id, x, y))
{desktop_ = std::move(proposed);desktop_raise(drag_.id);}
else {desktop_ = undo_.back();if(undo_.size()>drag_undo_size_){undo_.pop_back();if(!undo_themes_.empty())undo_themes_.pop_back();}}
refresh();
}
@@ -133,7 +125,7 @@ void App::ProcessEvent(Rml::Event &event) try {
const auto type = event.GetType();
// Native widgets can emit intermediate selected values while their options
// are being constructed. Opening a squad menu must stay read-only.
if (setting_menu_markup_ && (menu_ == "lineup" || menu_ == "transfer" || menu_ == "news" || menu_ == "settings") &&
if (setting_menu_markup_ && (menu_ == "lineup" || menu_ == "transfer" || menu_ == "news" || menu_ == "settings" || menu_=="widgets" || menu_=="save" || menu_=="load" || menu_=="view-save" || menu_=="view-load") &&
(type == "change" || type == "input")) return;
if (type == "keydown")
keyboard_navigation_ = true;
@@ -155,6 +147,7 @@ void App::ProcessEvent(Rml::Event &event) try {
return;
if (amount_event(event)) return;
if (popup_.empty() && settings_event(event)) return;
if (popup_.empty() && (files_event(event) || widget_event(event))) return;
if ((menu_ == "lineup" || menu_ == "transfer") && (type == "change" || type == "input")) {
if (auto *select = dynamic_cast<Rml::ElementFormControlSelect *>(event.GetTargetElement());
select && select->IsSelectBoxVisible()) {
@@ -247,6 +240,8 @@ void App::ProcessEvent(Rml::Event &event) try {
if (type == "mouseup" && event.GetParameter<int>("button", 0) == 1) {
if (scene_ == "desktop" && !menu_.empty() && popup_.empty()) {
menu_.clear();
quit_after_save_=false;
quit_closes_app_=false;
reset_settings();
refresh();
} else if (scene_ == "desktop" && menu_.empty() && popup_.empty()) {
@@ -364,6 +359,8 @@ void App::ProcessEvent(Rml::Event &event) try {
dirty_ = true;
} else if (!menu_.empty()) {
menu_.clear();
quit_after_save_=false;
quit_closes_app_=false;
reset_settings();
refresh();
} else if (scene_ == "manager") {
@@ -461,6 +458,7 @@ void App::dispatch(const std::string &action, Rml::Element *target) {
const auto id =
target ? target->GetAttribute<Rml::String>("data-id", "") : std::string();
if (settings_action(action,target)) return;
if (files_action(action,target) || widget_action(action,target)) return;
if (lineup_action(action, target) || transfer_action(action, target)) return;
if (career_report_action(action, target) || rankings_action(action, target)) return;
if (statistics_action(action, target) || dispatch_news(action, target)) return;
@@ -557,6 +555,7 @@ void App::dispatch(const std::string &action, Rml::Element *target) {
}
return;
}
focus_after_render_=context_id_.empty()?"desktop-identity":"object-"+context_id_;
auto before = desktop_;
bool ok = true;
if (action == "context-pin")
@@ -588,97 +587,16 @@ void App::dispatch(const std::string &action, Rml::Element *target) {
std::stoi(id.substr(split + 1)));
}
if (ok && desktop_ != before) {
undo_.push_back(std::move(before));
desktop_checkpoint(before);
refresh();
} else if (!ok)
message("Kein freier Platz. Fixierte Widgets zuerst lösen oder andere "
"Elemente entfernen.");
return;
}
if (action == "widget-add") {
auto before = desktop_;
auto &o = desktop_["objects"].at(id);
o["pinned"] = false;
if (place_desktop_object(desktop_, id, 64, 224)) {
undo_.push_back(before);
menu_.clear();
refresh();
} else {
desktop_ = before;
message("Für dieses Widget ist kein freier Platz vorhanden.");
}
return;
}
if (action == "view-default") {
undo_.push_back(desktop_);
desktop_ = default_desktop();
menu_.clear();
refresh();
return;
}
if (action == "view-save-confirm") {
const auto name = field("view-name");
launch::ModLibrary::validate_name(name);
const auto file = user_dir() / "layouts" / editor::path(name + ".json");
const auto save = [this, file] {
const auto old_menu = menu_;
menu_.clear();
refresh();
frame();
auto image = file;
image.replace_extension(".png");
try {
screenshot(image);
editor::write_atomic(file, desktop_.dump(2));
menu_.clear();
refresh();
} catch (...) {
menu_ = old_menu;
refresh();
throw;
}
};
if (fs::exists(file))
confirmation("Gespeicherte Ansicht überschreiben?", save);
else
save();
return;
}
if (action == "view-select") {
selections_["view"] = id;
refresh();
return;
}
if (action == "view-load-confirm") {
launch::ModLibrary::validate_name(selections_["view"]);
const auto file =
user_dir() / "layouts" / editor::path(selections_["view"] + ".json");
auto next = Json::parse(editor::read_file(file));
if (!migrate_desktop(next))
throw editor::Error("Ungültige Desktopansicht oder kein freier Platz für den Almanach.");
undo_.push_back(desktop_);
desktop_ = std::move(next);
menu_.clear();
refresh();
return;
}
if (action == "view-delete") {
const auto name = selections_["view"];
if (name.empty())
return;
launch::ModLibrary::validate_name(name);
confirmation("Ansicht „" + name + "“ löschen?", [this, name] {
const auto file = user_dir() / "layouts" / editor::path(name + ".json");
fs::remove(file);
auto image = file;
image.replace_extension(".png");
fs::remove(image);
selections_["view"].clear();
refresh();
});
return;
}
if (action == "close") {
quit_after_save_=false;
quit_closes_app_=false;
menu_.clear();
reset_settings();
if (scene_ != "desktop")
@@ -1157,92 +1075,6 @@ void App::dispatch(const std::string &action, Rml::Element *target) {
message("Trainingslager für die Demo vorgemerkt.");
return;
}
if (action == "save-select") {
selections_["save"] = id;
refresh();
return;
}
if (action == "save-new") {
selections_["save"].clear();
refresh();
return;
}
if (action == "save-confirm") {
auto name =
selections_["save"].empty() ? field("save-name") : selections_["save"];
try {
launch::ModLibrary::validate_name(name);
const auto file = user_dir() / "saves" / editor::path(name + ".json");
fs::create_directories(file.parent_path());
const auto save = [this, file] {
init_desktop();
source_->save(file, desktop_);
saved_desktop_ = desktop_;
menu_.clear();
if (quit_after_save_)
closed_ = true;
refresh();
};
if (fs::exists(file))
confirmation("Vorhandenen Demo-Spielstand überschreiben?", save);
else
save();
} catch (const std::exception &e) {
message(e.what());
}
return;
}
if (action == "load-confirm") {
const auto name = selections_["save"];
if (name.empty())
return;
try {
launch::ModLibrary::validate_name(name);
desktop_ =
source_->load(user_dir() / "saves" / editor::path(name + ".json"));
saved_desktop_ = desktop_;
if(!ui_preferences_loaded_){ui_preferences_=legacy_ui_preferences(snap().state.at("preferences"));theme_=ui_preferences_.at("theme").get<std::string>();}
menu_.clear();
scene_ = "desktop";
refresh();
} catch (const std::exception &e) {
message(e.what());
}
return;
}
if (action == "save-rename") {
const auto previous = selections_["save"];
if (previous.empty())
return;
text_dialog(
"Speicherstand umbenennen", previous,
[this, previous](const std::string &name) {
launch::ModLibrary::validate_name(name);
if (name == previous)
return;
const auto folder = user_dir() / "saves",
old_file = folder / editor::path(previous + ".json"),
new_file = folder / editor::path(name + ".json");
if (fs::exists(new_file))
throw editor::Error("Diese Bezeichnung ist bereits vorhanden.");
fs::rename(old_file, new_file);
selections_["save"] = name;
refresh();
});
return;
}
if (action == "save-delete") {
const auto name = selections_["save"];
if (name.empty())
return;
confirmation("Demo-Spielstand „" + name + "“ löschen?", [this, name] {
launch::ModLibrary::validate_name(name);
fs::remove(user_dir() / "saves" / editor::path(name + ".json"));
selections_["save"].clear();
refresh();
});
return;
}
if (action == "squad-tactics") {
menu_ = "tactics";
refresh();
@@ -1250,7 +1082,7 @@ void App::dispatch(const std::string &action, Rml::Element *target) {
}
}
void App::change(Rml::Element *target) {
if(settings_change(target))return;
if(files_change(target) || widget_change(target) || settings_change(target))return;
const auto kind = target->GetAttribute<Rml::String>("data-change", "");
auto *control = dynamic_cast<Rml::ElementFormControl *>(target);
if (!control)
@@ -1314,7 +1146,7 @@ void App::change(Rml::Element *target) {
}
}
void App::input(Rml::Element *target) {
if(settings_change(target))return;
if(files_change(target) || widget_change(target) || settings_change(target))return;
const auto kind = target->GetAttribute<Rml::String>("data-input", "");
auto *control = dynamic_cast<Rml::ElementFormControl *>(target);
if (!control)
@@ -1385,6 +1217,11 @@ void App::script() {
context_->ProcessMouseMove(point.x, point.y, 0);
context_->ProcessMouseButtonDown(button, 0);
context_->ProcessMouseButtonUp(button, 0);
} else if(type=="pointer-down"||type=="pointer-move"||type=="pointer-up"){
const auto point=locate(command);context_->ProcessMouseMove(point.x,point.y,0);
const int button=command.value("button",0);
if(type=="pointer-down")context_->ProcessMouseButtonDown(button,0);
if(type=="pointer-up")context_->ProcessMouseButtonUp(button,0);
} else if (type == "drag") {
const auto point = locate(command),
end = Rml::Vector2i{command.at("to_x"), command.at("to_y")};
@@ -1404,7 +1241,7 @@ void App::script() {
{"LEFT", Rml::Input::KI_LEFT}, {"RIGHT", Rml::Input::KI_RIGHT},
{"UP", Rml::Input::KI_UP}, {"DOWN", Rml::Input::KI_DOWN},
{"A", Rml::Input::KI_A}, {"Z", Rml::Input::KI_Z}, {"V", Rml::Input::KI_V},
{"BACKSPACE", Rml::Input::KI_BACK}, {"DELETE", Rml::Input::KI_DELETE}, {"F4", Rml::Input::KI_F4},
{"BACKSPACE", Rml::Input::KI_BACK}, {"DELETE", Rml::Input::KI_DELETE}, {"F4", Rml::Input::KI_F4}, {"F10", Rml::Input::KI_F10}, {"CONTEXT", Rml::Input::KI_APPS},
{"0", Rml::Input::KI_0}, {"1", Rml::Input::KI_1}, {"2", Rml::Input::KI_2}, {"3", Rml::Input::KI_3},
{"4", Rml::Input::KI_4}, {"5", Rml::Input::KI_5}, {"6", Rml::Input::KI_6}, {"7", Rml::Input::KI_7},
{"8", Rml::Input::KI_8}, {"9", Rml::Input::KI_9}};
@@ -1437,7 +1274,7 @@ void App::script() {
{"transfer_view", transfer_view_}, {"transfer_dialog", transfer_dialog_}, {"transfer_draft", transfer_draft_},
{"career_view", career_view_}, {"rankings_view", rankings_view_},
{"stadium_view", stadium_view_},
{"ui_preferences",ui_preferences_}, {"settings_draft",settings_draft_}, {"settings_category",settings_category_}, {"settings_query",settings_query_}, {"settings_notice",settings_notice_},
{"files_view",{{"mode",files_mode_},{"selected",files_selected_},{"name",files_name_},{"feedback",files_feedback_}}}, {"widget_view",{{"query",widget_query_},{"notice",widget_notice_}}}, {"ui_preferences",ui_preferences_}, {"settings_draft",settings_draft_}, {"settings_category",settings_category_}, {"settings_query",settings_query_}, {"settings_notice",settings_notice_},
{"statistics_view", statistics_view_}, {"statistics_trophy", statistics_trophy_},
{"statistics_scrolls", statistics_scrolls_},
{"news_view", news_view_}, {"news_dialog", news_dialog_},
@@ -1455,6 +1292,11 @@ void App::script() {
{"desktop", desktop_},
{"demo", source_ ? snap().state : Json()}}
.dump(2));
else if(type=="metrics"){
context_->Update();Json rows=Json::array();
for(const auto &selector:command.at("selectors")){Rml::ElementList nodes;doc_->QuerySelectorAll(nodes,selector.get<std::string>());for(auto *node:nodes){auto at=node->GetAbsoluteOffset(Rml::BoxArea::Border),size=node->GetBox().GetSize(Rml::BoxArea::Border);rows.push_back({{"selector",selector},{"id",node->GetId()},{"x",at.x},{"y",at.y},{"width",size.x},{"height",size.y},{"scroll_top",node->GetScrollTop()},{"scroll_height",node->GetScrollHeight()},{"client_height",node->GetClientHeight()},{"visible",node->IsVisible(true)}});}}
editor::write_atomic(editor::path(command.at("path").get<std::string>()),rows.dump(2));
}
else if (type == "dom")
editor::write_atomic(
editor::path(command.at("path").get<std::string>()),