From e230e4e7ebfdef719395598774696e5c6b80eaf5 Mon Sep 17 00:00:00 2001 From: Peterino Date: Thu, 26 Nov 2026 18:21:06 -0700 Subject: [PATCH] Ask for the server on the desktop client's first run The ADD panel no longer starts on lore://localhost:41337. On first run it asks which Lore server you work on, takes the address as typed (lore.example.com, host:port, the forge's https:// address or a lore(s):// remote) and makes it lores://host:41337, or lore:// for a loopback host. CONNECT signs in unless a sign-in for that host is kept, then lists the repositories. The server is kept in `server` beside desktop.toml; the panel then shows it with CHANGE, SIGN IN and LIST. --test checks the address handling. README and the design doc describe the first run; the README drops the milestone 1 notes. --- .ai/designs/desktop-client.md | 9 +- desktop/README.md | 19 ++- desktop/gui/src/main.cpp | 264 ++++++++++++++++++++++++++++++---- 3 files changed, 251 insertions(+), 41 deletions(-) diff --git a/.ai/designs/desktop-client.md b/.ai/designs/desktop-client.md index b0d263c..6e2850d 100644 --- a/.ai/designs/desktop-client.md +++ b/.ai/designs/desktop-client.md @@ -220,8 +220,13 @@ or submit finishing. - **Sidebar:** workspaces with their sync state and change count, ADD, FORGET, then the branches. -- **ADD:** open an existing working copy (CHOOSE FOLDER), or clone: a SERVER with SIGN IN and - LIST, a repository from the list, INTO a folder, CLONE. +- **ADD:** shown on first run, with nothing to configure beforehand. It asks which server + first: the address as typed (`lore.example.com`, `host:port`, the forge's `https://` address + or a `lore(s)://` remote) becomes `lores://host:41337`, or `lore://` for a loopback host. + CONNECT signs in unless a sign-in for that host is kept, then lists the repositories. The + server is kept in `server` beside `desktop.toml`, and the panel then shows it with CHANGE, + SIGN IN (AGAIN) and LIST. Below: open an existing working copy (CHOOSE FOLDER), or a + repository from the list, INTO a folder, CLONE. - **Sign-in:** a banner with the device code, OPEN (the forge's device page, also opened automatically) and COPY CODE. - **Workspace bar:** branch, revision, AHEAD, BEHIND or OFFLINE, LOCK ON EDIT, SIGN IN when diff --git a/desktop/README.md b/desktop/README.md index 0e1fb91..e9a4b8a 100644 --- a/desktop/README.md +++ b/desktop/README.md @@ -9,13 +9,9 @@ to. The design is in [.ai/designs/desktop-client.md](../.ai/designs/desktop-clie | `forge-service` | The client service: does the Lore work and runs policies (Rust) | | `forge-tui` | A terminal frontend. Not written yet | -**Milestone 1:** the service syncs nothing yet. It simulates sync for the workspaces listed in -its `desktop.toml`. liblore comes in milestone 2. - -The window is a repository explorer: workspaces and branches on the left, and three tabs: -FILES (folder tree, file list, details and history of the selected file), GRAPH (the revision -graph) and TREE (branches, their revisions and each revision's changes). Until the service can -report repositories, all three show a built-in **sample repository**, labelled SAMPLE DATA. +On first run the window asks which Lore server you work on (for example +`lore.example.com`), signs you in through the forge in your browser, and lists the +repositories you can clone. Nothing needs configuring first. ## Build and run @@ -45,15 +41,16 @@ The service keeps its socket, `desktop.toml` and `service.log` in: Set `LORE_FORGE_DESKTOP_DIR` to use one directory instead. A Unix socket path must stay under about 100 bytes, so keep that directory's path short. -Workspaces, until the GUI can add them: +The GUI keeps the server you chose in `server` in the same directory. Workspaces are added +from the window; the service records them in `desktop.toml`: ```toml # desktop.toml [[workspace]] name = "my-game" path = "/Users/me/lore/my-game" -remote = "lore://localhost:41337/me/my-game" -autosync = "off" # off, interval or on_change; not acted on yet +remote = "lores://lore.example.com:41337/me/my-game" +autosync = "off" # off, interval or on_change ``` ## Self-test @@ -84,7 +81,7 @@ Platform limits come from UHDK: | Path | Contents | | --- | --- | -| `gui/src/` | The GUI: frame loop, sidebar and service handling (`main.cpp`), the explorer tabs (`explorer.cpp`), the sample repository (`sample_repo.cpp`), protocol client, platform glue, the pixel-art theme and icons (`theme.cpp`), the compiled-in font and licences (`embedded.cpp`) | +| `gui/src/` | The GUI: frame loop, sidebar and service handling (`main.cpp`), the explorer tabs (`explorer.cpp`), protocol client, platform glue, the pixel-art theme and icons (`theme.cpp`), the compiled-in font and licences (`embedded.cpp`) | | `gui/uhdk_imgui/` | UHDK's ImGui platform backend, copied from UHDK's examples | | `protocol/` | Message types and socket location; the reference the C++ client follows | | `service/` | The client service | diff --git a/desktop/gui/src/main.cpp b/desktop/gui/src/main.cpp index 22ed9c0..d5ab98d 100644 --- a/desktop/gui/src/main.cpp +++ b/desktop/gui/src/main.cpp @@ -29,6 +29,7 @@ #include "service_client.hpp" #include "theme.hpp" +#include #include #include #include @@ -119,6 +120,90 @@ std::string RemoteBase(const std::string &url) { return slash == std::string::npos ? url : url.substr(0, slash); } +// The host of a URL such as `lores://host:41337/a/b` or `oidc+https://host`, lower-cased. +std::string HostOf(const std::string &url) { + size_t scheme = url.find("://"); + std::string rest = scheme == std::string::npos ? url : url.substr(scheme + 3); + rest = rest.substr(0, rest.find('/')); + if (rest.size() && rest[0] == '[') + rest = rest.substr(0, rest.find(']') + 1); + else + rest = rest.substr(0, rest.find(':')); + for (char &c : rest) + c = char(std::tolower((unsigned char)c)); + return rest; +} + +// The Lore remote for what someone typed as a server: `lore.example.com`, `host:port`, the +// forge's `https://` address or a `lores://` remote, with or without a repository after it. +// Lore's port is 41337 unless given. Loopback hosts get `lore://`, as lore-forge's pages do; +// everywhere else `lores://`, which checks the certificate. Empty when it is not an address. +std::string NormalizeServer(const std::string &typed) { + size_t begin = typed.find_first_not_of(" \t\r\n"), end = typed.find_last_not_of(" \t\r\n"); + if (begin == std::string::npos) + return {}; + std::string text = typed.substr(begin, end - begin + 1); + std::string scheme; + size_t sep = text.find("://"); + if (sep != std::string::npos) { + scheme = text.substr(0, sep); + for (char &c : scheme) + c = char(std::tolower((unsigned char)c)); + text = text.substr(sep + 3); + } + std::string authority = text.substr(0, text.find('/')); + if (authority.empty() || authority.find_first_of(" @?#") != std::string::npos) + return {}; + // The forge's web address names the host, not Lore's port. + bool web = scheme == "http" || scheme == "https"; + if (!scheme.empty() && !web && scheme != "lore" && scheme != "lores") + return {}; + std::string host = HostOf(authority), port; + if (host.empty()) + return {}; + size_t host_end = authority[0] == '[' ? authority.find(']') + 1 : authority.find(':'); + if (!web && host_end != std::string::npos && host_end < authority.size() && authority[host_end] == ':') + port = authority.substr(host_end + 1); + if (port.empty()) + port = "41337"; + if (port.find_first_not_of("0123456789") != std::string::npos) + return {}; + bool loopback = host == "localhost" || host == "127.0.0.1" || host == "[::1]"; + if (scheme.empty() || web) + scheme = loopback ? "lore" : "lores"; + std::string shown = authority[0] == '[' ? authority.substr(0, host_end) : host; + return scheme + "://" + shown + ":" + port; +} + +// NormalizeServer's cases, checked by --test. +bool NormalizeServerWorks() { + const char *cases[][2] = { + {"lore.example.com", "lores://lore.example.com:41337"}, + {" Lore.Example.com ", "lores://lore.example.com:41337"}, + {"lore.example.com:5000", "lores://lore.example.com:5000"}, + {"https://lore.example.com/alice/demo", "lores://lore.example.com:41337"}, + {"https://lore.example.com:8443", "lores://lore.example.com:41337"}, + {"lores://lore.example.com:41337/alice/demo", "lores://lore.example.com:41337"}, + {"lore://lore.example.com", "lore://lore.example.com:41337"}, + {"localhost:42337", "lore://localhost:42337"}, + {"http://127.0.0.1:18081", "lore://127.0.0.1:41337"}, + {"[::1]:9", "lore://[::1]:9"}, + {"", ""}, + {"ftp://host", ""}, + {"host:port", ""}, + {"user@host", ""}, + }; + bool ok = true; + for (const auto &c : cases) { + std::string got = NormalizeServer(c[0]); + if (got != c[1]) { + std::fprintf(stderr, "forge-desktop: --test: server \"%s\" gave \"%s\", not \"%s\"\n", c[0], got.c_str(), c[1]); + ok = false; + } + } + return ok; +} + // A sign-in in progress or just finished. struct LoginState { std::string remote, url, code, phase, message; @@ -175,6 +260,10 @@ class App : public ExplorerHost { void SyncAll(); void StartLogin(const std::string &remote); void ListRepositories(); + void ConnectServer(); + bool SignedInTo(const std::string &remote) const; + void LoadServer(); + void SaveServer(); void ChooseFolder(uhdk_request_tag tag, const char *title); void OpenUrl(const std::string &url); void Show(); @@ -212,7 +301,14 @@ class App : public ExplorerHost { // The ADD panel. bool adding_ = false; - char server_[256] = "lore://localhost:41337"; + // The Lore server to sign in to and clone from, as `lores://host:port`: asked for on first + // run, kept in `server` beside the service's settings, and empty until chosen. + std::string server_; + char server_input_[256] = {}; + bool editing_server_ = false; + bool focus_server_ = true; + bool list_after_login_ = false; + std::vector signed_in_hosts_; char clone_into_[1024] = {}; std::vector> repositories_; // name, id std::string repositories_note_; @@ -259,6 +355,7 @@ bool App::Initialize() { error_ = error; return true; } + LoadServer(); uhdk_session *session = session_; client_ = std::make_unique(paths_, [session] { uhdk_wake(session); }); client_->Start(); @@ -608,6 +705,8 @@ void App::ListRepositories() { chosen_repository_.clear(); repositories_note_ = "reading"; std::string remote = server_; + if (remote.empty()) + return; Call("repositories", "{\"remote\":" + Q(remote) + "}", [this](const json::Value &result, const json::Value &error) { if (error.is_object()) { repositories_note_ = error["message"].str(); @@ -619,6 +718,65 @@ void App::ListRepositories() { }); } +// Uses the server typed into the ADD panel: signs in to it unless already signed in there, +// then lists its repositories. +void App::ConnectServer() { + std::string remote = NormalizeServer(server_input_); + if (remote.empty()) { + error_ = std::string("not a server address: ") + server_input_ + " (try lore.example.com)"; + return; + } + error_.clear(); + bool changed = remote != server_; + server_ = remote; + editing_server_ = false; + SaveServer(); + if (changed) { + repositories_.clear(); + chosen_repository_.clear(); + repositories_note_.clear(); + } + if (SignedInTo(server_)) { + ListRepositories(); + } else { + list_after_login_ = true; + StartLogin(server_); + } +} + +// Whether a sign-in is kept for `remote`. Lore keys sign-ins by the forge's address, which +// lore-forge serves under the same host name as Lore. +bool App::SignedInTo(const std::string &remote) const { + std::string host = HostOf(remote); + for (const auto &signed_in : signed_in_hosts_) + if (signed_in == host) + return true; + return false; +} + +void App::LoadServer() { + if (paths_.config_dir.empty()) + return; + std::ifstream in(paths_.config_dir + "/server"); + std::string line; + if (in && std::getline(in, line)) + server_ = NormalizeServer(line); +} + +void App::SaveServer() { + if (paths_.config_dir.empty()) + return; + std::string path = paths_.config_dir + "/server"; + std::ofstream out(path + ".partial", std::ios::trunc); + out << server_ << "\n"; + out.close(); +#if defined(_WIN32) + std::remove(path.c_str()); // rename does not replace a file there +#endif + if (!out || std::rename((path + ".partial").c_str(), path.c_str()) != 0) + std::fprintf(stderr, "forge-desktop: could not save the server to %s\n", path.c_str()); +} + // ---- Events void App::HandleEvent(const uhdk_event &event) { @@ -784,8 +942,13 @@ void App::HandleMessage(const json::Value &message) { } else if (login_.phase == "failed") { error_ = "sign-in failed: " + login_.message; login_ = {}; + list_after_login_ = false; } else if (login_.phase == "done") { Call("status", "{}"); + if (list_after_login_ && login_.remote == server_) { + list_after_login_ = false; + ListRepositories(); + } } } return; @@ -824,17 +987,21 @@ void App::HandleMessage(const json::Value &message) { liblore_ = liblore["loaded"]["version"].str(); liblore_error_ = liblore["missing"]["error"].str(); accounts_.clear(); + signed_in_hosts_.clear(); for (const auto &account : result["accounts"].items()) { std::string where = account["auth_url"].str(); size_t scheme = where.find("://"); if (scheme != std::string::npos) where = where.substr(scheme + 3); + signed_in_hosts_.push_back(HostOf(account["auth_url"].str())); std::string name = account["name"].str(); accounts_.push_back((name.empty() ? std::string("signed in") : name) + " at " + where); } - // Offer the server of the first workspace for cloning. - if (!workspaces_.empty() && !workspaces_[0].remote.empty() && !std::strcmp(server_, "lore://localhost:41337")) - std::snprintf(server_, sizeof server_, "%s", RemoteBase(workspaces_[0].remote).c_str()); + // With no server chosen yet, the first workspace's is the one to clone from. + if (server_.empty() && !workspaces_.empty() && !workspaces_[0].remote.empty()) { + server_ = RemoteBase(workspaces_[0].remote); + SaveServer(); + } } } @@ -1004,41 +1171,80 @@ void App::DrawAdd() { } theme::Rule(); + const bool asking = server_.empty() || editing_server_; + const bool signing_in = !login_.phase.empty() && login_.phase != "done"; + if (asking) { + // First run, or CHANGE: which server, then sign-in and its repositories follow. + theme::TextColoured(theme::kPaper, "Which Lore server do you work on?"); + theme::TextColoured(theme::kMuted, "Its address, as on the forge's pages: lore.example.com, or lores://host:port"); + ImGui::AlignTextToFramePadding(); + theme::TextColoured(theme::kMuted, "SERVER"); + ImGui::SameLine(80); + ImGui::PushStyleColor(ImGuiCol_FrameBg, theme::kInk); + ImGui::SetNextItemWidth(320); + if (focus_server_ && Connected()) { + ImGui::SetKeyboardFocusHere(); + focus_server_ = false; + } + bool enter = ImGui::InputTextWithHint("##server", "lore.example.com", server_input_, sizeof server_input_, + ImGuiInputTextFlags_EnterReturnsTrue); + ImGui::PopStyleColor(); + ImGui::SameLine(0, 12); + bool can = Connected() && server_input_[0] && !signing_in; + if ((theme::Button("CONNECT", can, true) || (enter && can))) + ConnectServer(); + if (editing_server_) { + ImGui::SameLine(0, 12); + if (theme::Button("CANCEL")) + editing_server_ = false; + } + } else { + ImGui::AlignTextToFramePadding(); + theme::TextColoured(theme::kMuted, "SERVER"); + ImGui::SameLine(80); + theme::TextColoured(theme::kPaper, server_.c_str()); + ImGui::SameLine(0, 12); + bool signed_in = SignedInTo(server_); + if (signing_in) + theme::TextColoured(theme::kSpark, "signing in"); + else + theme::TextColoured(signed_in ? theme::kGreen : theme::kMuted, signed_in ? "signed in" : "not signed in"); + ImGui::SameLine(0, 12); + if (theme::Button("CHANGE", !signing_in)) { + std::snprintf(server_input_, sizeof server_input_, "%s", server_.c_str()); + editing_server_ = true; + focus_server_ = true; + } + ImGui::SameLine(0, 12); + if (theme::Button(signed_in ? "SIGN IN AGAIN" : "SIGN IN", Connected() && !signing_in)) { + list_after_login_ = true; + StartLogin(server_); + } + ImGui::SameLine(0, 12); + if (theme::Button("LIST", Connected(), signed_in)) + ListRepositories(); + // Signed in already, as after a restart: show what there is without being asked. + if (signed_in && Connected() && repositories_.empty() && repositories_note_.empty()) + ListRepositories(); + } + theme::Rule(); + theme::TextColoured(theme::kMuted, "OPEN A WORKING COPY"); if (theme::Button("CHOOSE FOLDER", Connected())) ChooseFolder(kOpenFolderTag, "Open a Lore working copy"); theme::Rule(); - theme::TextColoured(theme::kMuted, "CLONE"); - ImGui::AlignTextToFramePadding(); - theme::TextColoured(theme::kMuted, "SERVER"); - ImGui::SameLine(80); - ImGui::PushStyleColor(ImGuiCol_FrameBg, theme::kInk); - ImGui::SetNextItemWidth(320); - ImGui::InputText("##server", server_, sizeof server_); - ImGui::PopStyleColor(); - ImGui::SameLine(0, 12); - if (theme::Button("SIGN IN", Connected() && server_[0])) - StartLogin(RemoteBase(server_)); - ImGui::SameLine(0, 12); - if (theme::Button("LIST", Connected() && server_[0], true)) - ListRepositories(); - if (!accounts_.empty()) { - std::string who; - for (const auto &account : accounts_) - who += (who.empty() ? "" : ", ") + account; - theme::TextColoured(theme::kMuted, who.c_str()); - } - + theme::TextColoured(theme::kMuted, server_.empty() ? "CLONE" : ("CLONE FROM " + HostOf(server_)).c_str()); const float list_h = std::max(120.0f, ImGui::GetContentRegionAvail().y - 110); ImGui::PushStyleColor(ImGuiCol_ChildBg, theme::kInk); ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, {1, 1}); ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, {0, 0}); ImGui::BeginChild("repositories", {0, list_h}, ImGuiChildFlags_Borders); - if (!repositories_note_.empty()) { + if (!repositories_note_.empty() || server_.empty()) { + const char *note = repositories_note_.empty() ? "connect to a server above" : repositories_note_.c_str(); ImVec2 p = ImGui::GetCursorScreenPos(); ImGui::Dummy({0, 20}); - theme::TextAt({p.x + 8, p.y + 2}, theme::kMuted, repositories_note_.c_str(), p.x + ImGui::GetContentRegionAvail().x); + theme::TextAt({p.x + 8, p.y + 2}, theme::kMuted, note, p.x + ImGui::GetContentRegionAvail().x); } for (const auto &[name, id] : repositories_) { ImGui::PushID(id.c_str()); @@ -1069,7 +1275,7 @@ void App::DrawAdd() { ChooseFolder(kCloneFolderTag, "Clone into a folder in"); ImGui::SameLine(0, 12); if (theme::Button("CLONE", Connected() && !chosen_repository_.empty() && clone_into_[0], true)) { - std::string url = RemoteBase(server_) + "/" + chosen_repository_; + std::string url = server_ + "/" + chosen_repository_; Call("workspace_clone", "{\"url\":" + Q(url) + ",\"path\":" + Q(clone_into_) + "}", [this](const json::Value &result, const json::Value &error) { if (error.is_object()) { @@ -1378,6 +1584,8 @@ int main(int argc, char **argv) { } } if (test) { + if (!NormalizeServerWorks()) + return 1; // Never touch the user's own service: the spawned one inherits this directory. std::string dir, error; if (!platform::MakeTempDir(dir, error)) {