fix(banks): enforce unique bank display names in-model + B3 review minors
This commit is contained in:
+41
-13
@@ -331,11 +331,14 @@ gaccel_register_t g_accelBankCopySel{};
|
||||
gaccel_register_t g_accelBankPoolFull{};
|
||||
gaccel_register_t g_accelBankBanksFull{};
|
||||
|
||||
// Persists the book after a bank mutation. Mirrors the capture path (main.cpp
|
||||
// RunCapture): a bank change is held in-session and written to the active project's
|
||||
// ext state so it travels with the .rpp. No Save-As prompt here — saveToActiveProject
|
||||
// no-ops on an unsaved project (the change stays valid for the session and persists
|
||||
// on the user's next save), matching how capture persists.
|
||||
// Persists the book after a bank mutation. Mirrors the CAPTURE path (main.cpp
|
||||
// RunCapture), NOT the Design-View path: a bank change is held in-session and written
|
||||
// to the active project's ext state so it travels with the .rpp. Deliberately no
|
||||
// Save-As prompt — saveToActiveProject no-ops on an unsaved project (the change stays
|
||||
// valid for the session and persists on the user's next save), exactly as capture
|
||||
// persists. This is an intentional divergence from persistViewState (above), which
|
||||
// DOES prompt Save-As on an unsaved project; do not "align" the two — a bank mutation
|
||||
// follows capture's quiet-persist idiom, a Design-View mutation follows the prompt idiom.
|
||||
void persistBook() { g_session->saveToActiveProject(); }
|
||||
|
||||
// Prompts the user for a single line of text via REAPER's stock input dialog.
|
||||
@@ -343,12 +346,21 @@ void persistBook() { g_session->saveToActiveProject(); }
|
||||
// cancel (SDK ~3808). `initial` pre-fills the field. Returns false (leaving `out`
|
||||
// untouched) on cancel or an empty entry. Self-contained bindable-action name entry;
|
||||
// B4's panel affordances supersede this with in-panel editing.
|
||||
//
|
||||
// COMMA GUARD: GetUserInputs splits the returned values on a separator that defaults
|
||||
// to ',', so a bank name containing a comma would be truncated at the comma. We
|
||||
// override the return separator to \x1f (ASCII unit separator, un-typeable in the
|
||||
// dialog) via the documented `separator=X` extra caption field (SDK ~3806), so any
|
||||
// printable name — commas included — round-trips whole. The captions_csv itself stays
|
||||
// comma-joined: the single field caption, then the `separator=` directive as a
|
||||
// trailing pseudo-caption (the directive redefines only the RETURN separator).
|
||||
bool promptText(const char* title, const char* caption, const std::string& initial,
|
||||
std::string& out) {
|
||||
std::vector<char> buf(512, '\0');
|
||||
// Pre-fill: GetUserInputs seeds the field from the retvals buffer's initial value.
|
||||
std::snprintf(buf.data(), buf.size(), "%s", initial.c_str());
|
||||
if (!GetUserInputs(title, 1, caption, buf.data(), static_cast<int>(buf.size())))
|
||||
const std::string captions = std::string(caption) + ",separator=\x1f";
|
||||
if (!GetUserInputs(title, 1, captions.c_str(), buf.data(), static_cast<int>(buf.size())))
|
||||
return false; // user cancelled
|
||||
std::string s(buf.data());
|
||||
if (s.empty()) return false; // an empty name is not a valid bank name
|
||||
@@ -368,9 +380,11 @@ std::string mintBankId() {
|
||||
}
|
||||
|
||||
// Resolves a user-typed bank reference (a display name) to a bank id, scanning the
|
||||
// book's banks in ordinal order. Case-sensitive exact match on displayName; "Pool"
|
||||
// resolves the pool. Returns "" when no bank carries that name. Kept in the action
|
||||
// layer (not the model) — it is UI name-resolution, not a model rule.
|
||||
// book's banks in ordinal order. Exact match on displayName; "Pool" resolves the pool.
|
||||
// Returns "" when no bank carries that name. Kept in the action layer (not the model)
|
||||
// — it is UI name-resolution, not a model rule. First-match is unambiguous BY
|
||||
// CONSTRUCTION: the model enforces unique display names (trimmed + case-insensitive),
|
||||
// so at most one bank can carry a given name — no duplicate can shadow another here.
|
||||
std::string bankIdByDisplayName(const std::string& name) {
|
||||
for (const Bank& b : g_session->book().banks())
|
||||
if (b.displayName == name) return b.id;
|
||||
@@ -381,14 +395,18 @@ std::string bankIdByDisplayName(const std::string& name) {
|
||||
|
||||
// Create a named bank: prompt for a display name, mint a stable GUID id, create it in
|
||||
// the model, persist. The new bank is NOT auto-activated (create and activate are
|
||||
// distinct acts — mirrors capture/placement separation). A duplicate-name is allowed
|
||||
// (display names are not unique in the model); the fresh GUID keeps the id unique.
|
||||
// distinct acts — mirrors capture/placement separation). The model rejects a display
|
||||
// name that duplicates an existing bank's (trimmed + case-insensitive, incl. "Pool");
|
||||
// the create then fails and the user is told the name is taken.
|
||||
void doBankCreate() {
|
||||
std::string name;
|
||||
if (!promptText("ReaSampler: create bank", "Bank name:", "", name)) return;
|
||||
const std::string id = mintBankId();
|
||||
if (!g_session->book().createBank(id, name)) {
|
||||
ShowConsoleMsg("ReaSampler: could not create bank (id collision — try again).\n");
|
||||
ShowConsoleMsg(
|
||||
("ReaSampler: could not create bank \"" + name +
|
||||
"\" (a bank with that name already exists).\n")
|
||||
.c_str());
|
||||
return;
|
||||
}
|
||||
persistBook();
|
||||
@@ -411,7 +429,10 @@ void doBankRename() {
|
||||
std::string newName;
|
||||
if (!promptText("ReaSampler: rename bank", "New name:", which, newName)) return;
|
||||
if (!g_session->book().renameBank(id, newName)) {
|
||||
ShowConsoleMsg("ReaSampler: cannot rename that bank (the pool is un-renamable).\n");
|
||||
// renameBank rejects the pool (un-renamable) or a name already used by another
|
||||
// bank (unique display names, trimmed + case-insensitive).
|
||||
ShowConsoleMsg("ReaSampler: cannot rename that bank (the pool is un-renamable, "
|
||||
"or another bank already uses that name).\n");
|
||||
return;
|
||||
}
|
||||
persistBook();
|
||||
@@ -432,6 +453,13 @@ void doBankDelete() {
|
||||
ShowConsoleMsg(("ReaSampler: no bank named \"" + which + "\".\n").c_str());
|
||||
return;
|
||||
}
|
||||
// Pool early-out: the pool is un-deletable (the model rejects it). Catch it here,
|
||||
// BEFORE the non-empty confirm, so typing "Pool" never shows a misleading
|
||||
// "delete anyway?" prompt for an operation the model will refuse regardless.
|
||||
if (id == kPoolBankId) {
|
||||
ShowConsoleMsg("ReaSampler: the pool cannot be deleted.\n");
|
||||
return;
|
||||
}
|
||||
// Read member count BEFORE deleting (the Bank* is invalidated by deleteBank; we do
|
||||
// not cache it — resolve size to an int up front).
|
||||
const Bank* b = g_session->book().bank(id);
|
||||
|
||||
+45
-1
@@ -77,6 +77,43 @@ void BankBook::normalizeOrdinals() {
|
||||
banks_[i].ordinal = static_cast<int>(i);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Display-name uniqueness (trimmed + case-insensitive, ASCII)
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
namespace {
|
||||
|
||||
// Folds a display name to its uniqueness key: strip leading/trailing ASCII
|
||||
// whitespace, lower-case ASCII letters. So "Drums", "drums", and " Drums " share one
|
||||
// key and cannot coexist. ASCII-only by design — the pure core carries no locale
|
||||
// facility and must not grow one; bank names are short user labels, not full Unicode
|
||||
// case-folding candidates.
|
||||
std::string nameKey(const std::string& s) {
|
||||
std::size_t b = 0, e = s.size();
|
||||
auto isWs = [](char c) { return c == ' ' || c == '\t' || c == '\n' || c == '\r'; };
|
||||
while (b < e && isWs(s[b])) ++b;
|
||||
while (e > b && isWs(s[e - 1])) --e;
|
||||
std::string out;
|
||||
out.reserve(e - b);
|
||||
for (std::size_t i = b; i < e; ++i) {
|
||||
char c = s[i];
|
||||
if (c >= 'A' && c <= 'Z') c = static_cast<char>(c - 'A' + 'a');
|
||||
out += c;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
// True if any bank OTHER than `exceptId` already carries `name`'s uniqueness key. The
|
||||
// exception lets renameBank accept a bank keeping (or re-casing/-spacing) its own name.
|
||||
bool BankBook::displayNameTaken(const std::string& name, const std::string& exceptId) const {
|
||||
const std::string key = nameKey(name);
|
||||
for (const auto& b : banks_)
|
||||
if (b.id != exceptId && nameKey(b.displayName) == key) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Bank lifecycle
|
||||
// ---------------------------------------------------------------------------
|
||||
@@ -84,7 +121,10 @@ void BankBook::normalizeOrdinals() {
|
||||
bool BankBook::createBank(const std::string& id, const std::string& displayName) {
|
||||
if (id.empty()) return false; // ids key the registry
|
||||
if (id == kPoolBankId) return false; // reserved pool id
|
||||
if (bank(id) != nullptr) return false; // duplicate
|
||||
if (bank(id) != nullptr) return false; // duplicate id
|
||||
// Display names are unique (trimmed + case-insensitive); the pool's "Pool" is a
|
||||
// reserved name and is caught here like any other collision.
|
||||
if (displayNameTaken(displayName, /*exceptId=*/id)) return false;
|
||||
|
||||
Bank b;
|
||||
b.id = id;
|
||||
@@ -99,6 +139,10 @@ bool BankBook::renameBank(const std::string& id, const std::string& displayName)
|
||||
if (id == kPoolBankId) return false; // pool is un-renamable
|
||||
Bank* b = bank(id);
|
||||
if (b == nullptr) return false;
|
||||
// Reject a name already used by a DIFFERENT bank. Renaming a bank to its own
|
||||
// current name (or a case/space variant of it) is a no-op success, not a
|
||||
// rejection — exceptId=id excludes the bank itself from the collision scan.
|
||||
if (displayNameTaken(displayName, /*exceptId=*/id)) return false;
|
||||
b->displayName = displayName;
|
||||
return true;
|
||||
}
|
||||
|
||||
+11
-2
@@ -97,10 +97,14 @@ public:
|
||||
|
||||
// Creates a named bank with the caller-supplied stable id and display name,
|
||||
// assigning the next ordinal. Rejects (returns false, no mutation) an empty id,
|
||||
// a duplicate id, or the reserved pool id. Display name is not required unique.
|
||||
// a duplicate id, the reserved pool id, or a display name that duplicates an
|
||||
// existing bank's name (including the pool's "Pool"). Display-name uniqueness is
|
||||
// trimmed + case-insensitive (ASCII): "Drums", "drums", and " Drums " collide.
|
||||
bool createBank(const std::string& id, const std::string& displayName);
|
||||
|
||||
// Renames a named bank. Rejects (false, no mutation) an unknown id or the pool.
|
||||
// Renames a named bank. Rejects (false, no mutation) an unknown id, the pool, or a
|
||||
// target name already used by a DIFFERENT bank (trimmed + case-insensitive, as
|
||||
// createBank). Renaming a bank to its own current name is a no-op success.
|
||||
bool renameBank(const std::string& id, const std::string& displayName);
|
||||
|
||||
// Deletes a NAMED bank, removing it (and its member index entries) from the
|
||||
@@ -210,6 +214,11 @@ private:
|
||||
std::vector<Bank> banks_; // ordinal order; banks_[0] is always the pool
|
||||
std::string activeBankId_; // always names a live bank; defaults to pool
|
||||
|
||||
// True if a bank OTHER than `exceptId` already carries `name`'s uniqueness key
|
||||
// (trimmed + case-insensitive, ASCII). Backs the create/rename uniqueness check;
|
||||
// pass exceptId=id to let a bank keep (or re-case/-space) its own name.
|
||||
bool displayNameTaken(const std::string& name, const std::string& exceptId) const;
|
||||
|
||||
// Re-sorts banks_ by ordinal (pool pinned first) and rewrites ordinals to a
|
||||
// contiguous 0..N-1 so the pool is 0 and named banks are 1..N. Called after any
|
||||
// structural change (create / delete / reorder).
|
||||
|
||||
Reference in New Issue
Block a user