diff --git a/crates/alkane-gui/src/pool_prefs.rs b/crates/alkane-gui/src/pool_prefs.rs index 91c4cc16..623f726b 100644 --- a/crates/alkane-gui/src/pool_prefs.rs +++ b/crates/alkane-gui/src/pool_prefs.rs @@ -1,8 +1,8 @@ //! Dropdown presets + user-saved values for the Configure-pools dialog (round 15 //! #1). Each pool field (address / account / worker / password) has a "+" menu //! offering built-in presets (pool stratums, worker placeholders) plus values the -//! user has saved, and the user can add the current box value to the saved list. -//! Saved lists persist one-per-line under the config dir. +//! user has saved. "Add new..." opens a manager window that edits the whole saved +//! list at once (round 18 #8b). Saved lists persist one-per-line under the config dir. use std::path::PathBuf; @@ -72,42 +72,32 @@ pub fn load_saved(field: PoolField) -> Vec { .unwrap_or_default() } -/// Add `value` to `field`'s saved list (deduplicated, no-op if empty/already -/// present or already a preset). Returns true if it was newly added. -pub fn add_saved(field: PoolField, value: &str) -> bool { - let value = value.trim(); - if value.is_empty() { - return false; - } - if field.presets().iter().any(|p| *p == value) { - return false; - } - let mut saved = load_saved(field); - if saved.iter().any(|s| s == value) { - return false; - } - saved.push(value.to_string()); +/// Replace `field`'s entire saved list with `values` (round 18 #8b: the preset +/// manager edits the whole list at once). Each value is trimmed; empties, exact +/// duplicates, and any value that is already a built-in preset are dropped. +/// Returns the number of values actually written. +pub fn save_all(field: PoolField, values: &[String]) -> std::io::Result { + let out = dedupe_saved(&field.presets(), values); let p = config_path(field.saved_file()); if let Some(dir) = p.parent() { let _ = std::fs::create_dir_all(dir); } - let _ = std::fs::write(p, saved.join("\n")); - true + std::fs::write(&p, out.join("\n"))?; + Ok(out.len()) } -/// Remove `value` from `field`'s saved list (round 16 #5). Returns true if it was -/// present and removed. Presets cannot be removed (they are not in the saved file). -pub fn remove_saved(field: PoolField, value: &str) -> bool { - let value = value.trim(); - let mut saved = load_saved(field); - let before = saved.len(); - saved.retain(|s| s != value); - if saved.len() == before { - return false; +/// Trim every value, then drop empties, values equal to a built-in preset, and +/// later exact duplicates (first occurrence wins). Order is otherwise preserved. +fn dedupe_saved(presets: &[&str], values: &[String]) -> Vec { + let mut out: Vec = Vec::new(); + for v in values { + let v = v.trim(); + if v.is_empty() || presets.iter().any(|p| *p == v) || out.iter().any(|s| s == v) { + continue; + } + out.push(v.to_string()); } - let p = config_path(field.saved_file()); - let _ = std::fs::write(p, saved.join("\n")); - true + out } /// The full dropdown list for a field: presets first, then the user's saved @@ -129,3 +119,25 @@ pub fn dropdown_entries(field: PoolField) -> Vec<(String, String)> { } out } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn dedupe_saved_trims_drops_empties_presets_and_dupes() { + let presets = ["x", "{ip1}"]; + let vals: Vec = [" ", "myworker", "myworker", "x", " keep ", "{ip1}", "keep"] + .iter() + .map(|s| s.to_string()) + .collect(); + // " " (empty), the second "myworker", "x"/"{ip1}" (presets) and the second + // "keep" all drop; " keep " is trimmed and kept. + assert_eq!(dedupe_saved(&presets, &vals), vec!["myworker".to_string(), "keep".to_string()]); + } + + #[test] + fn dedupe_saved_empty_input_is_empty() { + assert!(dedupe_saved(&[], &[]).is_empty()); + } +} diff --git a/crates/alkane-gui/src/ui.rs b/crates/alkane-gui/src/ui.rs index 3e0f9f34..4e88f1a4 100644 --- a/crates/alkane-gui/src/ui.rs +++ b/crates/alkane-gui/src/ui.rs @@ -84,12 +84,14 @@ const ID_RESTORE_CANCEL: i32 = 787; const ID_CTX_POOLS: i32 = 736; const ID_POOLS_OK: i32 = 788; const ID_POOLS_CANCEL: i32 = 789; +// Pool-preset manager dialog (round 18 #8b): edit the whole saved list at once. +const ID_POOL_PRESETS_SAVE: i32 = 790; +const ID_POOL_PRESETS_CANCEL: i32 = 791; // Pool-field "+" dropdown buttons (round 15 #1): 12 buttons (3 rows x 4 fields), // id = base + row*4 + field. The dropdown menu items use their own id range. const ID_POOL_PLUS_BASE: i32 = 800; -const ID_POOL_MENU_ADD: i32 = 819; // "add new" (adds current box value to the dropdown) +const ID_POOL_MENU_ADD: i32 = 819; // "Add new..." -> opens the preset-manager window (round 18 #8b) const ID_POOL_MENU_BASE: i32 = 820; // one id per dropdown entry (820 + index) -const ID_POOL_MENU_REMOVE_BASE: i32 = 1000; // one id per removable saved entry (round 16 #5) // Value-prompt dialog for power-limit / tuning-percent / timezone / password. const ID_ACTION_OK: i32 = 740; const ID_ACTION_CANCEL: i32 = 741; @@ -742,6 +744,11 @@ pub struct App { pool_menu_target: usize, // which field the open "+" menu edits pool_menu_field: crate::pool_prefs::PoolField, pool_menu_values: Vec, // values behind the open menu, by item index + // The pool-preset manager dialog (round 18 #8b): a multiline editable list of + // the saved values for one field, reached from the "+" menu's "Manage saved". + pool_presets_dialog: Widget, + pool_presets_input: Widget, + pool_presets_field: crate::pool_prefs::PoolField, // Saved view presets + the Presets dialog widgets. presets: Vec, presets_dialog: Widget, @@ -1042,6 +1049,9 @@ pub fn build(frame: Widget) -> App { pool_menu_target: 0, pool_menu_field: crate::pool_prefs::PoolField::Address, pool_menu_values: Vec::new(), + pool_presets_dialog: frame, + pool_presets_input: frame, + pool_presets_field: crate::pool_prefs::PoolField::Address, presets: crate::presets::load_presets(), presets_dialog: frame, presets_choice: frame, @@ -1677,17 +1687,10 @@ impl App { self.pool_menu_values.push(value); } menu.menu_append_separator(); - menu.menu_append(ID_POOL_MENU_ADD, "Add new"); - // Let the user remove values they previously added (round 16 #5); presets - // are built-in and stay. Listed in a submenu keyed by saved-list index. - let saved = crate::pool_prefs::load_saved(field); - if !saved.is_empty() { - let sub = Widget::menu_new(); - for (i, val) in saved.iter().enumerate() { - sub.menu_append(ID_POOL_MENU_REMOVE_BASE + i as i32, val); - } - menu.menu_append_submenu("Remove saved value", sub); - } + // "Add new..." opens the preset-manager window: an editable list of all the + // saved values for this field, so the user can add, edit and remove them in + // one place (round 18 #8b -- replaces the old add-one / remove-submenu pair). + menu.menu_append(ID_POOL_MENU_ADD, "Add new..."); self.pool_menu_target = target; self.pool_menu_field = field; self.pending_popup = Some((self.pools_dialog, menu)); @@ -1695,29 +1698,12 @@ impl App { /// A pool dropdown item was chosen (round 15 #1). Worker values insert at the /// cursor (so several placeholders can be combined); other fields replace. + /// ("Add new..." is handled in on_menu_item, which opens the preset manager.) fn choose_pool_dropdown(&mut self, id: i32) { use crate::pool_prefs::PoolField; let Some(input) = self.pool_fields.get(self.pool_menu_target).copied() else { return; }; - if id == ID_POOL_MENU_ADD { - let val = input.get_value(); - if crate::pool_prefs::add_saved(self.pool_menu_field, &val) { - wx_rs::set_status_text("added to the dropdown list"); - } - return; - } - // Remove a previously-saved value (round 16 #5). - if id >= ID_POOL_MENU_REMOVE_BASE { - let ri = (id - ID_POOL_MENU_REMOVE_BASE) as usize; - let saved = crate::pool_prefs::load_saved(self.pool_menu_field); - if let Some(val) = saved.get(ri) { - if crate::pool_prefs::remove_saved(self.pool_menu_field, val) { - wx_rs::set_status_text("removed from the dropdown list"); - } - } - return; - } let idx = (id - ID_POOL_MENU_BASE) as usize; let Some(value) = self.pool_menu_values.get(idx).cloned() else { return; @@ -1729,6 +1715,73 @@ impl App { } } + /// Build (but do not show) the pool-preset manager (round 18 #8b): a multiline + /// editable list of the saved values for the field whose "+" menu is open. The + /// current box value (if non-empty and not already saved or a built-in preset) + /// is seeded as the last line, so "Add new..." pre-fills what the user typed. + fn build_pool_presets_dialog(&mut self) -> Widget { + use crate::pool_prefs::PoolField; + let field = self.pool_menu_field; + let field_name = match field { + PoolField::Address => "address", + PoolField::Account => "account", + PoolField::Worker => "worker", + PoolField::Password => "password", + }; + let mut lines = crate::pool_prefs::load_saved(field); + if let Some(input) = self.pool_fields.get(self.pool_menu_target).copied() { + let cur = input.get_value().trim().to_string(); + if !cur.is_empty() + && !lines.iter().any(|l| l == &cur) + && !field.presets().iter().any(|p| *p == cur) + { + lines.push(cur); + } + } + let frame = Widget::main_frame(); + let dlg = Widget::dialog(frame, &format!("Saved {field_name} values")); + let root = Widget::box_sizer(true); + root.add( + Widget::static_text( + dlg, + "One value per line. These appear in the + menu below the built-in\npresets. Edit freely, then Save.", + ), + 0, + sf::BORDER_ALL, + 8, + ); + let input = Widget::text_ctrl(dlg, true); + input.set_value(&lines.join("\n")); + input.set_min_size(360, 220); + root.add(input, 1, sf::EXPAND | sf::BORDER_LEFT | sf::BORDER_RIGHT, 8); + let btns = Widget::box_sizer(false); + btns.add_stretch(1); + btns.add(Widget::button(dlg, "Save", ID_POOL_PRESETS_SAVE), 0, sf::BORDER_ALL, 6); + btns.add(Widget::button(dlg, "Cancel", ID_POOL_PRESETS_CANCEL), 0, sf::BORDER_ALL, 6); + root.add_sizer(btns, 0, sf::EXPAND | sf::BORDER_ALL, 6); + root.set_sizer_on(dlg); + dlg.set_min_size(400, 340); + dlg.fit(dlg); + self.pool_presets_dialog = dlg; + self.pool_presets_input = input; + self.pool_presets_field = field; + dlg + } + + /// Save the edited pool-preset list back to disk (round 18 #8b). Blank lines, + /// duplicates and built-in presets are dropped by pool_prefs::save_all. + fn apply_pool_presets(&mut self) { + let text = self.pool_presets_input.get_value(); + let values: Vec = text.lines().map(|l| l.trim().to_string()).collect(); + match crate::pool_prefs::save_all(self.pool_presets_field, &values) { + Ok(n) => { + self.pool_presets_dialog.close(1); + wx_rs::set_status_text(&format!("saved {n} value(s)")); + } + Err(e) => wx_rs::set_status_text(&format!("failed to save: {e}")), + } + } + /// Read the 3 pool rows, expand each row's worker per selected miner, and /// start the background pool-config action (round 15 #1). Rows missing an /// address or account are skipped; a miner with no usable rows is skipped. @@ -2089,9 +2142,12 @@ impl App { /// A menu item was chosen. Direct actions start immediately; the value-input /// actions (power limit / tuning %) return a prompt dialog to show modally. fn on_menu_item(&mut self, id: i32) -> Option { - // Pool "+" dropdown selections (round 15 #1): the "add current" item and - // one id per dropdown entry from ID_POOL_MENU_BASE up. - if id == ID_POOL_MENU_ADD || id >= ID_POOL_MENU_BASE { + // Pool "+" dropdown selections (round 15 #1). "Add new..." opens the preset + // manager (round 18 #8b, shown modally); the rest fill the box in place. + if id == ID_POOL_MENU_ADD { + return Some(self.build_pool_presets_dialog()); + } + if id >= ID_POOL_MENU_BASE { self.choose_pool_dropdown(id); return None; } @@ -3880,6 +3936,9 @@ impl App { } ID_POOLS_OK => self.apply_pools(), ID_POOLS_CANCEL => self.pools_dialog.close(0), + // Pool-preset manager buttons (round 18 #8b). + ID_POOL_PRESETS_SAVE => self.apply_pool_presets(), + ID_POOL_PRESETS_CANCEL => self.pool_presets_dialog.close(0), // Pool-field "+" dropdown buttons (round 15 #1). id if (ID_POOL_PLUS_BASE..ID_POOL_PLUS_BASE + 12).contains(&id) => { self.open_pool_dropdown((id - ID_POOL_PLUS_BASE) as usize);