GUI round 3: Excel-style filters, status row colours, icon controls, context menu, live theme
build-test-release / release (push) Successful in 7m10s
build-test-release / release (push) Successful in 7m10s
Addresses the round-3 feedback batch:
- Right-click now works: a context menu (open selected in browser, select all,
clear) pops up from the table. It was never wired before. (The live-config
intersection over the selection remains a follow-up; no dead menu items.)
- Selection is readable again in both themes: the alt-row/zebra CSS hack (which
painted row backgrounds over the native highlight, leaving white-on-white) is
gone; the native selection highlight shows through.
- Row text is coloured by status (green = hashing, amber = slow, grey = not
hashing), which also separates rows without fragile GTK zebra striping.
- Dark mode: KPI cards and rows follow the system theme (no more white boxes in
dark mode); a system-colour-changed event repaints them live if the OS toggles
light/dark while the app is open.
- Play / Pause / Stop are coloured media icons (green triangle / amber bars /
red square) instead of text buttons.
- Filter panel redesigned Excel-style: a column dropdown at the top selects the
column; controls adapt to its type. Text columns: a value checklist ("is any
of") or a predicate (contains / does not contain / begins with / ends with /
equals). Numeric columns: between / = / != / > / >= / < / <=. Filters AND
across columns; edits held in a working copy, committed on Apply.
- Auto-rescan label shortened to "Rescan (s):" so the value field has room.
Table: replaced wxDataViewListCtrl with a raw wxDataViewCtrl + a custom
AttrListStore (per-row foreground via GetAttrByRow), which keeps in-table column
drag while enabling status row colours. New wx-rs bridge primitives: wxChoice
dropdown, coloured media buttons (drawn on a transparent bitmap via
wxGraphicsContext), a wxEVT_SYS_COLOUR_CHANGED event, destroy_children, and
call_after (deferred rebuilds). Patches 0002/0003 regenerated + round-trip
validated.
Model: ColFilter { Values, Text{op}, Num{op} } with TextOp/NumOp and
passes_filters -> ColFilter::matches; Column::num_value for numeric filtering.
Review fixes (one review pass):
- Deferred the two rebuilds that destroyed a control from inside its own event
(filter op-dropdown change; column drag-reorder) via call_after, avoiding a
use-after-free.
- num_value returns None for a blank-displaying value-type cell (Hashrate,
Power, ChipTemp, ...), so "ChipTemp <= 50" no longer matches a miner with no
temperature reading.
- Value checklist maps a checked entry back to its real value by index, so a
literal "(blank)" value is not confused with an empty cell.
Verified off-screen: 45 unit tests, headless self-test (14 checks), and
interaction screenshots of selection, right-click, the text/numeric filter, the
comparator swap, and dark mode.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Y8h447BPyJR4fioYZvQ9bh
This commit is contained in:
+481
-108
@@ -4,7 +4,7 @@
|
||||
use crate::model::*;
|
||||
use crate::scan::{self, ScanMsg};
|
||||
use std::cell::RefCell;
|
||||
use std::collections::HashSet;
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use wx_rs::{Widget, WxEvent, WxEventKind, sizer_flags as sf};
|
||||
|
||||
pub const ID_RANGES: i32 = 101;
|
||||
@@ -24,7 +24,18 @@ const ID_FILTER_RESET: i32 = 210;
|
||||
// column index (and the None equivalent). 100 columns of headroom each.
|
||||
const ID_FILTER_ALL_BASE: i32 = 300;
|
||||
const ID_FILTER_NONE_BASE: i32 = 500;
|
||||
// Right-click context-menu item ids.
|
||||
const ID_CTX_OPEN: i32 = 720;
|
||||
const ID_CTX_SELALL: i32 = 721;
|
||||
const ID_CTX_CLEAR: i32 = 722;
|
||||
// Filter dialog: the column-selector dropdown + the comparator dropdown.
|
||||
const ID_FILTER_COL: i32 = 730;
|
||||
const ID_FILTER_OP: i32 = 731;
|
||||
const TIMER_SCAN: i32 = 1;
|
||||
// Deferred-rebuild ids: run a rebuild from outside the triggering widget's own
|
||||
// event (destroying a control mid-event is unsafe). See `wx_rs::call_after`.
|
||||
const DEFER_TABLE: i32 = 1;
|
||||
const DEFER_FILTER: i32 = 2;
|
||||
|
||||
/// Theme-aware palette for the KPI cards. Health is encoded in colour so a red
|
||||
/// "Not hashing" or amber "Slow" draws the eye immediately. Colours derive from
|
||||
@@ -49,6 +60,15 @@ fn nudge(c: (u8, u8, u8), d: i32) -> (u8, u8, u8) {
|
||||
(f(c.0), f(c.1), f(c.2))
|
||||
}
|
||||
|
||||
/// Format a filter's numeric bound without a trailing ".0" for whole numbers.
|
||||
fn fmt_num(v: f64) -> String {
|
||||
if v.is_finite() && v == v.trunc() && v.abs() < 1e15 {
|
||||
format!("{}", v as i64)
|
||||
} else {
|
||||
format!("{v}")
|
||||
}
|
||||
}
|
||||
|
||||
/// Short mm:ss-ish duration for the rescan countdown ("4m 12s", "45s").
|
||||
fn fmt_secs(s: u64) -> String {
|
||||
if s >= 60 {
|
||||
@@ -58,6 +78,40 @@ fn fmt_secs(s: u64) -> String {
|
||||
}
|
||||
}
|
||||
|
||||
/// Which palette colour a KPI card's value uses, so the card can be re-themed
|
||||
/// when the system switches light/dark while the app is open.
|
||||
#[derive(Clone, Copy)]
|
||||
enum ColorKind {
|
||||
Neutral,
|
||||
Green,
|
||||
Amber,
|
||||
Red,
|
||||
Accent,
|
||||
}
|
||||
|
||||
fn kind_color(pal: &Palette, k: ColorKind) -> (u8, u8, u8) {
|
||||
match k {
|
||||
ColorKind::Neutral => pal.neutral,
|
||||
ColorKind::Green => pal.green,
|
||||
ColorKind::Amber => pal.amber,
|
||||
ColorKind::Red => pal.red,
|
||||
ColorKind::Accent => pal.accent,
|
||||
}
|
||||
}
|
||||
|
||||
/// A miner row's text colour, by status: green = hashing, amber = slow, muted
|
||||
/// grey = not hashing. This both conveys status and separates rows visually
|
||||
/// (GTK's native zebra/alt-row support is unreliable and fights selection).
|
||||
fn status_color(pal: &Palette, r: &MinerRow) -> (u8, u8, u8) {
|
||||
if r.slow {
|
||||
pal.amber
|
||||
} else if r.is_mining {
|
||||
pal.green
|
||||
} else {
|
||||
pal.label
|
||||
}
|
||||
}
|
||||
|
||||
fn palette() -> Palette {
|
||||
let win = wx_rs::system_color(0); // window background
|
||||
let text = wx_rs::system_color(1); // window text
|
||||
@@ -100,23 +154,36 @@ pub struct App {
|
||||
// selection (replaces the old checkbox column), tracked by IP so it
|
||||
// survives table refreshes during a scan.
|
||||
selected_ips: HashSet<String>,
|
||||
alt_row: (u8, u8, u8),
|
||||
// current theme palette + the KPI cards (panel, value, caption, colour role)
|
||||
// so both can be repainted when the system light/dark theme changes.
|
||||
pal: Palette,
|
||||
kpi_cards: Vec<(Widget, Widget, Widget, ColorKind)>,
|
||||
// A context menu built on right-click, popped up outside the App borrow.
|
||||
pending_popup: Option<(Widget, Widget)>,
|
||||
ranges_dialog: Widget,
|
||||
ranges_edit: Widget,
|
||||
ranges_auto: Widget,
|
||||
columns_dialog: Widget,
|
||||
columns_list: Widget,
|
||||
// Filter dialog (Excel-style, one column at a time via a dropdown).
|
||||
filter_dialog: Widget,
|
||||
filter_lists: Vec<(Column, Widget)>,
|
||||
filter_col_choice: Widget, // which column to filter
|
||||
filter_controls: Widget, // panel rebuilt per column with its criterion controls
|
||||
filter_op: Widget, // comparator dropdown
|
||||
filter_values: Widget, // value checklist (text "is any of")
|
||||
filter_values_data: Vec<String>, // the checklist's real values, by index
|
||||
filter_text: Widget, // text input (text predicates)
|
||||
filter_num_a: Widget, // numeric input a
|
||||
filter_num_b: Widget, // numeric input b (Between)
|
||||
filter_cols: Vec<Column>, // columns offered in the dropdown, in order
|
||||
filter_cur: usize, // index into filter_cols currently edited
|
||||
filter_active_op: i32, // the op index the visible controls were built for
|
||||
filter_pending_op: Option<i32>, // op to rebuild for, deferred off the choice event
|
||||
filter_working: HashMap<Column, ColFilter>, // edits, committed on Apply
|
||||
}
|
||||
|
||||
pub fn build(frame: Widget) -> App {
|
||||
let pal = palette();
|
||||
// Subtle alternating-row tint derived from the theme. GTK's native dataview
|
||||
// ignores SetAlternateRowColour, so also install an app-wide zebra CSS.
|
||||
let win = wx_rs::system_color(0);
|
||||
let alt_row = if luminance(win) < 128.0 { nudge(win, 12) } else { nudge(win, -12) };
|
||||
wx_rs::apply_zebra_css(alt_row.0, alt_row.1, alt_row.2);
|
||||
let panel = Widget::panel(frame);
|
||||
let root = Widget::box_sizer(true);
|
||||
|
||||
@@ -124,14 +191,16 @@ pub fn build(frame: Widget) -> App {
|
||||
// label. Health KPIs are colour-coded (green/amber/red); hashrate uses the
|
||||
// accent colour so totals stand apart from counts.
|
||||
let kpi_row = Widget::box_sizer(false);
|
||||
let mk_card = |label: &str, value_color: (u8, u8, u8)| -> Widget {
|
||||
let mut kpi_cards: Vec<(Widget, Widget, Widget, ColorKind)> = Vec::new();
|
||||
let mut mk_card = |label: &str, kind: ColorKind| -> Widget {
|
||||
let vc = kind_color(&pal, kind);
|
||||
let card = Widget::panel(panel);
|
||||
card.set_bg(pal.card_bg.0, pal.card_bg.1, pal.card_bg.2);
|
||||
let cs = Widget::box_sizer(true);
|
||||
let value = Widget::static_text(card, "0");
|
||||
value.set_font_size(19);
|
||||
value.set_bold();
|
||||
value.set_fg(value_color.0, value_color.1, value_color.2);
|
||||
value.set_fg(vc.0, vc.1, vc.2);
|
||||
value.set_bg(pal.card_bg.0, pal.card_bg.1, pal.card_bg.2);
|
||||
let lbl = Widget::static_text(card, label);
|
||||
lbl.set_font_size(9);
|
||||
@@ -141,14 +210,16 @@ pub fn build(frame: Widget) -> App {
|
||||
cs.add(lbl, 0, sf::BORDER_LEFT | sf::BORDER_BOTTOM | sf::BORDER_RIGHT, 12);
|
||||
cs.set_sizer_on(card);
|
||||
kpi_row.add(card, 1, sf::EXPAND | sf::BORDER_ALL, 4);
|
||||
kpi_cards.push((card, value, lbl, kind));
|
||||
value
|
||||
};
|
||||
let kpi_total = mk_card("Total", pal.neutral);
|
||||
let kpi_hashing = mk_card("Hashing", pal.green);
|
||||
let kpi_slow = mk_card("Slow", pal.amber);
|
||||
let kpi_not = mk_card("Not hashing", pal.red);
|
||||
let kpi_hashrate = mk_card("Hashrate", pal.accent);
|
||||
let kpi_power = mk_card("Est. power", pal.neutral);
|
||||
let kpi_total = mk_card("Total", ColorKind::Neutral);
|
||||
let kpi_hashing = mk_card("Hashing", ColorKind::Green);
|
||||
let kpi_slow = mk_card("Slow", ColorKind::Amber);
|
||||
let kpi_not = mk_card("Not hashing", ColorKind::Red);
|
||||
let kpi_hashrate = mk_card("Hashrate", ColorKind::Accent);
|
||||
let kpi_power = mk_card("Est. power", ColorKind::Neutral);
|
||||
drop(mk_card);
|
||||
root.add_sizer(kpi_row, 0, sf::EXPAND | sf::BORDER_ALL, 6);
|
||||
|
||||
// Controls row: scan-ranges + play/pause/stop on the left; Filter / Columns
|
||||
@@ -157,12 +228,14 @@ pub fn build(frame: Widget) -> App {
|
||||
let ranges_btn = Widget::button(panel, "Scan ranges", ID_RANGES);
|
||||
ctl.add(ranges_btn, 0, sf::ALIGN_CENTRE_VERTICAL | sf::BORDER_ALL, 4);
|
||||
ctl.add_spacer(8);
|
||||
let play_btn = Widget::button(panel, "Play", ID_PLAY);
|
||||
ctl.add(play_btn, 0, sf::ALIGN_CENTRE_VERTICAL | sf::BORDER_ALL, 4);
|
||||
let pause_btn = Widget::button(panel, "Pause", ID_PAUSE);
|
||||
ctl.add(pause_btn, 0, sf::ALIGN_CENTRE_VERTICAL | sf::BORDER_ALL, 4);
|
||||
let stop_btn = Widget::button(panel, "Stop", ID_STOP);
|
||||
ctl.add(stop_btn, 0, sf::ALIGN_CENTRE_VERTICAL | sf::BORDER_ALL, 4);
|
||||
// Play / pause / stop as coloured media icons (green / amber / red). Vivid
|
||||
// fixed colours read well on both light and dark themes.
|
||||
let play_btn = Widget::media_button(panel, ID_PLAY, 0, (56, 190, 108));
|
||||
ctl.add(play_btn, 0, sf::ALIGN_CENTRE_VERTICAL | sf::BORDER_ALL, 2);
|
||||
let pause_btn = Widget::media_button(panel, ID_PAUSE, 1, (240, 172, 40));
|
||||
ctl.add(pause_btn, 0, sf::ALIGN_CENTRE_VERTICAL | sf::BORDER_ALL, 2);
|
||||
let stop_btn = Widget::media_button(panel, ID_STOP, 2, (226, 72, 60));
|
||||
ctl.add(stop_btn, 0, sf::ALIGN_CENTRE_VERTICAL | sf::BORDER_ALL, 2);
|
||||
ctl.add_stretch(1);
|
||||
let filter_btn = Widget::button(panel, "Filter", ID_FILTER);
|
||||
ctl.add(filter_btn, 0, sf::ALIGN_CENTRE_VERTICAL | sf::BORDER_ALL, 4);
|
||||
@@ -188,6 +261,9 @@ pub fn build(frame: Widget) -> App {
|
||||
wx_rs::create_status_bar();
|
||||
wx_rs::set_status_text("starting scan...");
|
||||
|
||||
// Repaint custom-coloured widgets when the OS switches light/dark.
|
||||
frame.bind_sys_colour();
|
||||
|
||||
let ranges = scan::load_ranges();
|
||||
let mut app = App {
|
||||
rows: Vec::new(),
|
||||
@@ -211,14 +287,28 @@ pub fn build(frame: Widget) -> App {
|
||||
auto_secs: scan::load_auto_secs(),
|
||||
countdown: 0,
|
||||
selected_ips: HashSet::new(),
|
||||
alt_row,
|
||||
pal,
|
||||
kpi_cards,
|
||||
pending_popup: None,
|
||||
ranges_dialog: frame,
|
||||
ranges_edit: frame,
|
||||
ranges_auto: frame,
|
||||
columns_dialog: frame,
|
||||
columns_list: frame,
|
||||
filter_dialog: frame,
|
||||
filter_lists: Vec::new(),
|
||||
filter_col_choice: frame,
|
||||
filter_controls: frame,
|
||||
filter_op: frame,
|
||||
filter_values: frame,
|
||||
filter_values_data: Vec::new(),
|
||||
filter_text: frame,
|
||||
filter_num_a: frame,
|
||||
filter_num_b: frame,
|
||||
filter_cols: Vec::new(),
|
||||
filter_cur: 0,
|
||||
filter_active_op: 0,
|
||||
filter_pending_op: None,
|
||||
filter_working: HashMap::new(),
|
||||
};
|
||||
app.rebuild_table();
|
||||
Widget::timer(TIMER_SCAN, 1000);
|
||||
@@ -249,7 +339,6 @@ impl App {
|
||||
}
|
||||
t.dvlc_append_text_col(&title, c.width(), !fixed);
|
||||
}
|
||||
t.dvlc_set_alt_row_color(self.alt_row.0, self.alt_row.1, self.alt_row.2);
|
||||
// Add the table to the root sizer (proportion 1 => fills the area under
|
||||
// the KPI + controls rows) and re-run the panel's layout so it is
|
||||
// positioned, not left floating at (0,0).
|
||||
@@ -270,6 +359,10 @@ impl App {
|
||||
let refs: Vec<&str> = cells.iter().map(|s| s.as_str()).collect();
|
||||
t.dvlc_append_row(&refs);
|
||||
}
|
||||
// Colour each row's text by status (also separates rows visually).
|
||||
for (disp, &ri) in self.view_idx.iter().enumerate() {
|
||||
t.dvlc_set_row_color(disp, Some(status_color(&self.pal, &self.rows[ri])));
|
||||
}
|
||||
// Re-apply the selection (a refresh clears the control's selection).
|
||||
let sel: Vec<usize> = self
|
||||
.view_idx
|
||||
@@ -294,6 +387,78 @@ impl App {
|
||||
self.kpi_power.set_label(&format_si(k.total_power_w, "W"));
|
||||
}
|
||||
|
||||
/// Re-apply the theme palette to the KPI cards and table rows after the OS
|
||||
/// switches light/dark while the app is open.
|
||||
fn repaint_theme(&mut self) {
|
||||
self.pal = palette();
|
||||
for (card, value, caption, kind) in &self.kpi_cards {
|
||||
let vc = kind_color(&self.pal, *kind);
|
||||
card.set_bg(self.pal.card_bg.0, self.pal.card_bg.1, self.pal.card_bg.2);
|
||||
value.set_bg(self.pal.card_bg.0, self.pal.card_bg.1, self.pal.card_bg.2);
|
||||
value.set_fg(vc.0, vc.1, vc.2);
|
||||
caption.set_bg(self.pal.card_bg.0, self.pal.card_bg.1, self.pal.card_bg.2);
|
||||
caption.set_fg(self.pal.label.0, self.pal.label.1, self.pal.label.2);
|
||||
}
|
||||
self.fill_rows(); // re-colour rows with the new palette
|
||||
self.table_parent.layout();
|
||||
}
|
||||
|
||||
/// Right-click: build a context menu of actions for the current selection.
|
||||
/// The menu is popped up outside the App borrow (see `dispatch`), because
|
||||
/// `popup_menu` runs a nested event loop that re-enters us.
|
||||
fn show_context_menu(&mut self) {
|
||||
let Some(t) = self.table else { return };
|
||||
let n = self.selected_ips.len();
|
||||
let menu = Widget::menu_new();
|
||||
if n >= 1 {
|
||||
let open = if n == 1 { "Open in browser".to_string() } else { format!("Open {n} miners in browser") };
|
||||
menu.menu_append(ID_CTX_OPEN, &open);
|
||||
menu.menu_append_separator();
|
||||
}
|
||||
menu.menu_append(ID_CTX_SELALL, "Select all");
|
||||
if n >= 1 {
|
||||
menu.menu_append(ID_CTX_CLEAR, &format!("Clear selection ({n})"));
|
||||
}
|
||||
self.pending_popup = Some((t, menu));
|
||||
}
|
||||
|
||||
fn take_pending_popup(&mut self) -> Option<(Widget, Widget)> {
|
||||
self.pending_popup.take()
|
||||
}
|
||||
|
||||
fn on_menu_item(&mut self, id: i32) {
|
||||
match id {
|
||||
ID_CTX_OPEN => {
|
||||
let ips: Vec<String> = self
|
||||
.rows
|
||||
.iter()
|
||||
.filter(|r| !r.ip.is_empty() && self.selected_ips.contains(&r.ip))
|
||||
.map(|r| r.ip.clone())
|
||||
.collect();
|
||||
for ip in ips {
|
||||
wx_rs::launch_browser(&format!("http://{ip}"));
|
||||
}
|
||||
}
|
||||
ID_CTX_SELALL => {
|
||||
let all: Vec<usize> = (0..self.view_idx.len()).collect();
|
||||
if let Some(t) = self.table {
|
||||
t.dvlc_select_rows(&all);
|
||||
}
|
||||
self.on_selection_changed();
|
||||
}
|
||||
ID_CTX_CLEAR => {
|
||||
self.selected_ips.clear();
|
||||
for r in &mut self.rows {
|
||||
r.selected = false;
|
||||
}
|
||||
if let Some(t) = self.table {
|
||||
t.dvlc_select_rows(&[]);
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn drain_scan(&mut self) {
|
||||
let Some(handle) = &self.scan else { return };
|
||||
let mut changed = false;
|
||||
@@ -492,10 +657,12 @@ impl App {
|
||||
}
|
||||
}
|
||||
self.view.apply_visible_reorder(&new_visible);
|
||||
// Rebuild so the control's column creation order matches the new
|
||||
// display order again; fill_rows/header_click/update_headers all assume
|
||||
// creation order == display_columns(), which the drag alone breaks.
|
||||
self.rebuild_table();
|
||||
// Rebuild so the control's column creation order matches the new display
|
||||
// order again (fill_rows/header_click/update_headers assume creation
|
||||
// order == display_columns()). Deferred: rebuild_table destroys the very
|
||||
// wxDataViewCtrl now delivering this reorder event, which is unsafe to do
|
||||
// from inside the event -- run it after the event unwinds.
|
||||
wx_rs::call_after(DEFER_TABLE);
|
||||
}
|
||||
|
||||
/// Build (but do not show) the ranges editor. The caller shows it modally
|
||||
@@ -514,7 +681,7 @@ impl App {
|
||||
root.add(edit, 1, sf::EXPAND | sf::BORDER_ALL, 6);
|
||||
// Auto-rescan interval (seconds; 0 = off).
|
||||
let auto_row = Widget::box_sizer(false);
|
||||
let auto_lbl = Widget::static_text(dlg, "Auto-rescan every (seconds, 0 = off):");
|
||||
let auto_lbl = Widget::static_text(dlg, "Rescan (s):");
|
||||
auto_row.add(auto_lbl, 0, sf::ALIGN_CENTRE_VERTICAL | sf::BORDER_ALL, 6);
|
||||
let auto_edit = Widget::text_ctrl(dlg, false);
|
||||
auto_edit.set_value(&self.auto_secs.to_string());
|
||||
@@ -634,103 +801,298 @@ impl App {
|
||||
self.rebuild_table();
|
||||
}
|
||||
|
||||
/// Build (but do not show) the Excel-style filter panel. Every visible
|
||||
/// column with more than one distinct value gets a per-column value
|
||||
/// checklist with All / None controls; filters AND across columns and OR
|
||||
/// within a column. Blank cells show as "(blank)".
|
||||
/// Operator labels offered for a column, by type.
|
||||
fn filter_ops(col: Column) -> &'static [&'static str] {
|
||||
if col.is_numeric() {
|
||||
&["between", "=", "!=", ">", ">=", "<", "<="]
|
||||
} else {
|
||||
&["is any of", "contains", "does not contain", "begins with", "ends with", "equals"]
|
||||
}
|
||||
}
|
||||
|
||||
/// The operator index that matches an existing filter (for prefilling).
|
||||
fn filter_op_index(existing: Option<&ColFilter>) -> i32 {
|
||||
match existing {
|
||||
Some(ColFilter::Values(_)) => 0,
|
||||
Some(ColFilter::Text { op, .. }) => match op {
|
||||
TextOp::Contains => 1,
|
||||
TextOp::NotContains => 2,
|
||||
TextOp::BeginsWith => 3,
|
||||
TextOp::EndsWith => 4,
|
||||
TextOp::Equals => 5,
|
||||
},
|
||||
Some(ColFilter::Num { op, .. }) => match op {
|
||||
NumOp::Between => 0,
|
||||
NumOp::Eq => 1,
|
||||
NumOp::Ne => 2,
|
||||
NumOp::Gt => 3,
|
||||
NumOp::Ge => 4,
|
||||
NumOp::Lt => 5,
|
||||
NumOp::Le => 6,
|
||||
},
|
||||
None => 0,
|
||||
}
|
||||
}
|
||||
|
||||
/// Build (but do not show) the filter dialog: an Excel-style, one-column-at-
|
||||
/// a-time filter. A dropdown at the top-left picks the column; the controls
|
||||
/// below adapt to its type -- numeric columns get a comparator
|
||||
/// (between / = / != / > / >= / < / <=) with one or two number inputs; text
|
||||
/// columns get a value checklist ("is any of") or a text predicate (contains
|
||||
/// / does not contain / begins with / ends with / equals). Filters AND across
|
||||
/// columns; edits are held in a working copy and committed on Apply.
|
||||
fn build_filter_dialog(&mut self) -> Widget {
|
||||
let frame = Widget::main_frame();
|
||||
let dlg = Widget::dialog(frame, "Filter");
|
||||
let root = Widget::box_sizer(true);
|
||||
let content = Widget::box_sizer(false); // side-by-side column filters
|
||||
let mut lists = Vec::new();
|
||||
for c in self.view.display_columns() {
|
||||
// Value checklists only make sense for categorical columns; skip the
|
||||
// continuous numeric metrics.
|
||||
if c.is_numeric() {
|
||||
continue;
|
||||
}
|
||||
let mut values: Vec<String> = self.rows.iter().map(|r| c.cell(r)).collect();
|
||||
values.sort();
|
||||
values.dedup();
|
||||
if values.len() < 2 {
|
||||
continue;
|
||||
}
|
||||
let idx = lists.len() as i32;
|
||||
let colbox = Widget::box_sizer(true);
|
||||
let head = Widget::box_sizer(false);
|
||||
let lbl = Widget::static_text(dlg, c.title());
|
||||
lbl.set_bold();
|
||||
head.add(lbl, 1, sf::ALIGN_CENTRE_VERTICAL | sf::BORDER_ALL, 2);
|
||||
head.add(Widget::button(dlg, "All", ID_FILTER_ALL_BASE + idx), 0, sf::BORDER_ALL, 1);
|
||||
head.add(Widget::button(dlg, "None", ID_FILTER_NONE_BASE + idx), 0, sf::BORDER_ALL, 1);
|
||||
colbox.add_sizer(head, 0, sf::EXPAND | sf::BORDER_ALL, 2);
|
||||
let lst = Widget::check_list_box(dlg);
|
||||
lst.set_min_size(140, 220); // so Fit gives the checklist real space
|
||||
let current: Option<HashSet<String>> = self.view.filters.get(&c).cloned();
|
||||
for v in &values {
|
||||
let shown = if v.is_empty() { "(blank)" } else { v.as_str() };
|
||||
lst.clb_append(shown);
|
||||
let checked = match ¤t {
|
||||
Some(allowed) => allowed.contains(v),
|
||||
None => true,
|
||||
};
|
||||
lst.clb_set_checked(lst.clb_count() - 1, checked);
|
||||
}
|
||||
colbox.add(lst, 1, sf::EXPAND | sf::BORDER_ALL, 2);
|
||||
content.add_sizer(colbox, 1, sf::EXPAND | sf::BORDER_ALL, 4);
|
||||
lists.push((c, lst));
|
||||
|
||||
self.filter_cols = self.view.display_columns();
|
||||
self.filter_working = self.view.filters.clone();
|
||||
self.filter_cur = 0;
|
||||
|
||||
let top = Widget::box_sizer(false);
|
||||
top.add(Widget::static_text(dlg, "Column:"), 0, sf::ALIGN_CENTRE_VERTICAL | sf::BORDER_ALL, 4);
|
||||
let col_choice = Widget::choice(dlg, ID_FILTER_COL);
|
||||
for c in &self.filter_cols {
|
||||
col_choice.choice_append(c.title());
|
||||
}
|
||||
if lists.is_empty() {
|
||||
let lbl = Widget::static_text(dlg, "Nothing to filter yet - run a scan first.");
|
||||
content.add(lbl, 0, sf::BORDER_ALL, 8);
|
||||
}
|
||||
root.add_sizer(content, 1, sf::EXPAND | sf::BORDER_ALL, 4);
|
||||
col_choice.choice_set_selection(0);
|
||||
top.add(col_choice, 1, sf::ALIGN_CENTRE_VERTICAL | sf::BORDER_ALL, 4);
|
||||
root.add_sizer(top, 0, sf::EXPAND | sf::BORDER_ALL, 6);
|
||||
self.filter_col_choice = col_choice;
|
||||
|
||||
// The criterion controls live in this panel, rebuilt on column/op change.
|
||||
let controls = Widget::panel(dlg);
|
||||
controls.set_min_size(380, 250);
|
||||
root.add(controls, 1, sf::EXPAND | sf::BORDER_ALL, 6);
|
||||
self.filter_controls = controls;
|
||||
|
||||
let btns = Widget::box_sizer(false);
|
||||
btns.add_stretch(1);
|
||||
btns.add(Widget::button(dlg, "Apply", ID_FILTER_OK), 0, sf::BORDER_ALL, 6);
|
||||
btns.add(Widget::button(dlg, "Reset", ID_FILTER_RESET), 0, sf::BORDER_ALL, 6);
|
||||
btns.add(Widget::button(dlg, "Cancel", ID_FILTER_CANCEL), 0, sf::BORDER_ALL, 6);
|
||||
root.add_sizer(btns, 0, sf::BORDER_ALL, 6);
|
||||
root.add_sizer(btns, 0, sf::EXPAND | sf::BORDER_ALL, 6);
|
||||
root.set_sizer_on(dlg);
|
||||
// Size the dialog to fit however many category columns there are.
|
||||
dlg.set_min_size(460, 400);
|
||||
dlg.fit(dlg);
|
||||
self.filter_dialog = dlg;
|
||||
self.filter_lists = lists;
|
||||
|
||||
self.build_filter_controls(None);
|
||||
dlg
|
||||
}
|
||||
|
||||
/// Check or uncheck every value in one column's list (0-based index into
|
||||
/// `filter_lists`).
|
||||
fn filter_check_all(&self, idx: usize, checked: bool) {
|
||||
if let Some((_c, lst)) = self.filter_lists.get(idx) {
|
||||
for i in 0..lst.clb_count() {
|
||||
lst.clb_set_checked(i, checked);
|
||||
/// Rebuild the criterion controls for the current column (`filter_cur`). `op`
|
||||
/// picks the operator; `None` derives it from the working filter (or 0).
|
||||
fn build_filter_controls(&mut self, op: Option<i32>) {
|
||||
let col = self.filter_cols[self.filter_cur];
|
||||
let panel = self.filter_controls;
|
||||
panel.destroy_children();
|
||||
let ops = Self::filter_ops(col);
|
||||
let existing = self.filter_working.get(&col).cloned();
|
||||
let op_idx = op
|
||||
.unwrap_or_else(|| Self::filter_op_index(existing.as_ref()))
|
||||
.clamp(0, ops.len() as i32 - 1);
|
||||
self.filter_active_op = op_idx;
|
||||
|
||||
let cs = Widget::box_sizer(true);
|
||||
let oprow = Widget::box_sizer(false);
|
||||
oprow.add(Widget::static_text(panel, "Keep rows where value is"), 0, sf::ALIGN_CENTRE_VERTICAL | sf::BORDER_ALL, 4);
|
||||
let op_choice = Widget::choice(panel, ID_FILTER_OP);
|
||||
for o in ops {
|
||||
op_choice.choice_append(o);
|
||||
}
|
||||
op_choice.choice_set_selection(op_idx);
|
||||
oprow.add(op_choice, 0, sf::ALIGN_CENTRE_VERTICAL | sf::BORDER_ALL, 4);
|
||||
cs.add_sizer(oprow, 0, sf::EXPAND | sf::BORDER_ALL, 4);
|
||||
self.filter_op = op_choice;
|
||||
// Unused input handles point at the panel and are never read (commit
|
||||
// dispatches on column type + active op).
|
||||
self.filter_values = panel;
|
||||
self.filter_values_data.clear();
|
||||
self.filter_text = panel;
|
||||
self.filter_num_a = panel;
|
||||
self.filter_num_b = panel;
|
||||
|
||||
if col.is_numeric() {
|
||||
let row = Widget::box_sizer(false);
|
||||
let a = Widget::text_ctrl(panel, false);
|
||||
a.set_min_size(120, -1);
|
||||
row.add(a, 0, sf::ALIGN_CENTRE_VERTICAL | sf::BORDER_ALL, 4);
|
||||
self.filter_num_a = a;
|
||||
if op_idx == 0 {
|
||||
row.add(Widget::static_text(panel, "and"), 0, sf::ALIGN_CENTRE_VERTICAL | sf::BORDER_ALL, 4);
|
||||
let b = Widget::text_ctrl(panel, false);
|
||||
b.set_min_size(120, -1);
|
||||
row.add(b, 0, sf::ALIGN_CENTRE_VERTICAL | sf::BORDER_ALL, 4);
|
||||
self.filter_num_b = b;
|
||||
}
|
||||
cs.add_sizer(row, 0, sf::EXPAND | sf::BORDER_ALL, 4);
|
||||
if let Some(ColFilter::Num { a: wa, b: wb, .. }) = &existing {
|
||||
self.filter_num_a.set_value(&fmt_num(*wa));
|
||||
if op_idx == 0 {
|
||||
self.filter_num_b.set_value(&fmt_num(*wb));
|
||||
}
|
||||
}
|
||||
} else if op_idx == 0 {
|
||||
let head = Widget::box_sizer(false);
|
||||
head.add(Widget::button(panel, "All", ID_FILTER_ALL_BASE), 0, sf::BORDER_ALL, 2);
|
||||
head.add(Widget::button(panel, "None", ID_FILTER_NONE_BASE), 0, sf::BORDER_ALL, 2);
|
||||
cs.add_sizer(head, 0, sf::BORDER_ALL, 2);
|
||||
let clb = Widget::check_list_box(panel);
|
||||
clb.set_min_size(340, 180);
|
||||
let mut values: Vec<String> = self.rows.iter().map(|r| col.cell(r)).collect();
|
||||
values.sort();
|
||||
values.dedup();
|
||||
let checked_set = match &existing {
|
||||
Some(ColFilter::Values(set)) => Some(set.clone()),
|
||||
_ => None,
|
||||
};
|
||||
for v in &values {
|
||||
let shown = if v.is_empty() { "(blank)" } else { v.as_str() };
|
||||
clb.clb_append(shown);
|
||||
let checked = match &checked_set {
|
||||
Some(s) => s.contains(v),
|
||||
None => true,
|
||||
};
|
||||
clb.clb_set_checked(clb.clb_count() - 1, checked);
|
||||
}
|
||||
cs.add(clb, 1, sf::EXPAND | sf::BORDER_ALL, 4);
|
||||
self.filter_values = clb;
|
||||
self.filter_values_data = values; // real values, indexed like the checklist
|
||||
} else {
|
||||
let inp = Widget::text_ctrl(panel, false);
|
||||
inp.set_min_size(340, -1);
|
||||
if let Some(ColFilter::Text { needle, .. }) = &existing {
|
||||
inp.set_value(needle);
|
||||
}
|
||||
cs.add(inp, 0, sf::EXPAND | sf::BORDER_ALL, 4);
|
||||
self.filter_text = inp;
|
||||
}
|
||||
cs.set_sizer_on(panel);
|
||||
panel.layout();
|
||||
self.filter_dialog.layout();
|
||||
}
|
||||
|
||||
/// Read the visible criterion controls into the working filter for the
|
||||
/// current column (removing it when the criterion is empty / matches all).
|
||||
fn commit_filter_current(&mut self) {
|
||||
if self.filter_cols.is_empty() {
|
||||
return;
|
||||
}
|
||||
let col = self.filter_cols[self.filter_cur];
|
||||
let op_idx = self.filter_active_op;
|
||||
let f: Option<ColFilter> = if col.is_numeric() {
|
||||
match self.filter_num_a.get_value().trim().parse::<f64>() {
|
||||
Err(_) => None,
|
||||
Ok(a) => {
|
||||
let op = match op_idx {
|
||||
0 => NumOp::Between,
|
||||
1 => NumOp::Eq,
|
||||
2 => NumOp::Ne,
|
||||
3 => NumOp::Gt,
|
||||
4 => NumOp::Ge,
|
||||
5 => NumOp::Lt,
|
||||
_ => NumOp::Le,
|
||||
};
|
||||
let b = if op_idx == 0 {
|
||||
self.filter_num_b.get_value().trim().parse::<f64>().unwrap_or(a)
|
||||
} else {
|
||||
a
|
||||
};
|
||||
Some(ColFilter::Num { op, a, b })
|
||||
}
|
||||
}
|
||||
} else if op_idx == 0 {
|
||||
let clb = self.filter_values;
|
||||
let count = clb.clb_count();
|
||||
let mut allowed = HashSet::new();
|
||||
for i in 0..count {
|
||||
if clb.clb_checked(i) {
|
||||
// Map back to the real value by index, so a genuine cell whose
|
||||
// text is literally "(blank)" is not confused with an empty one.
|
||||
allowed.insert(self.filter_values_data.get(i).cloned().unwrap_or_default());
|
||||
}
|
||||
}
|
||||
if allowed.is_empty() || allowed.len() == count {
|
||||
None // all (or none) checked == no filter
|
||||
} else {
|
||||
Some(ColFilter::Values(allowed))
|
||||
}
|
||||
} else {
|
||||
let needle = self.filter_text.get_value().trim().to_string();
|
||||
if needle.is_empty() {
|
||||
None
|
||||
} else {
|
||||
let op = match op_idx {
|
||||
1 => TextOp::Contains,
|
||||
2 => TextOp::NotContains,
|
||||
3 => TextOp::BeginsWith,
|
||||
4 => TextOp::EndsWith,
|
||||
_ => TextOp::Equals,
|
||||
};
|
||||
Some(ColFilter::Text { op, needle })
|
||||
}
|
||||
};
|
||||
match f {
|
||||
Some(f) => {
|
||||
self.filter_working.insert(col, f);
|
||||
}
|
||||
None => {
|
||||
self.filter_working.remove(&col);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Check or uncheck every value in the current column's checklist.
|
||||
fn filter_check_all(&self, checked: bool) {
|
||||
let clb = self.filter_values;
|
||||
for i in 0..clb.clb_count() {
|
||||
clb.clb_set_checked(i, checked);
|
||||
}
|
||||
}
|
||||
|
||||
fn on_choice_changed(&mut self, id: i32, sel: i32) {
|
||||
match id {
|
||||
ID_FILTER_COL => {
|
||||
self.commit_filter_current();
|
||||
self.filter_cur = (sel.max(0) as usize).min(self.filter_cols.len().saturating_sub(1));
|
||||
self.build_filter_controls(None);
|
||||
}
|
||||
ID_FILTER_OP => {
|
||||
// The op dropdown lives inside the controls panel that
|
||||
// build_filter_controls destroys, so rebuilding here would delete
|
||||
// this very wxChoice mid-event. Defer it off the choice event.
|
||||
self.commit_filter_current();
|
||||
self.filter_pending_op = Some(sel);
|
||||
wx_rs::call_after(DEFER_FILTER);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
/// A deferred rebuild requested via `wx_rs::call_after`, run after the
|
||||
/// triggering widget's own event has unwound.
|
||||
fn on_deferred(&mut self, id: i32) {
|
||||
match id {
|
||||
DEFER_TABLE => self.rebuild_table(),
|
||||
DEFER_FILTER => {
|
||||
let op = self.filter_pending_op.take();
|
||||
self.build_filter_controls(op);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
fn apply_filter(&mut self) {
|
||||
for (c, lst) in &self.filter_lists {
|
||||
let count = lst.clb_count();
|
||||
let mut allowed = HashSet::new();
|
||||
for i in 0..count {
|
||||
if lst.clb_checked(i) {
|
||||
// map the shown "(blank)" back to the empty string
|
||||
let v = lst.clb_string(i).unwrap_or_default();
|
||||
allowed.insert(if v == "(blank)" { String::new() } else { v });
|
||||
}
|
||||
}
|
||||
if allowed.is_empty() || allowed.len() == count {
|
||||
self.view.filters.remove(c); // all (or none) checked = no filter
|
||||
} else {
|
||||
self.view.filters.insert(*c, allowed);
|
||||
}
|
||||
}
|
||||
self.commit_filter_current();
|
||||
self.view.filters = self.filter_working.clone();
|
||||
self.filter_dialog.close(1);
|
||||
self.fill_rows();
|
||||
}
|
||||
|
||||
fn reset_filters(&mut self) {
|
||||
self.filter_working.clear();
|
||||
self.view.filters.clear();
|
||||
self.filter_dialog.close(1);
|
||||
self.fill_rows();
|
||||
@@ -780,12 +1142,8 @@ impl App {
|
||||
self.table_parent.layout();
|
||||
wx_rs::set_status_text(&format!("{} miners - stopped", self.rows.len()));
|
||||
}
|
||||
_ if (ID_FILTER_ALL_BASE..ID_FILTER_NONE_BASE).contains(&id) => {
|
||||
self.filter_check_all((id - ID_FILTER_ALL_BASE) as usize, true);
|
||||
}
|
||||
_ if (ID_FILTER_NONE_BASE..ID_FILTER_NONE_BASE + 200).contains(&id) => {
|
||||
self.filter_check_all((id - ID_FILTER_NONE_BASE) as usize, false);
|
||||
}
|
||||
ID_FILTER_ALL_BASE => self.filter_check_all(true),
|
||||
ID_FILTER_NONE_BASE => self.filter_check_all(false),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
@@ -793,6 +1151,11 @@ impl App {
|
||||
WxEventKind::DvlcSelectionChanged => self.on_selection_changed(),
|
||||
WxEventKind::DvlcActivated => self.activate_row(ev.p1 as usize),
|
||||
WxEventKind::DvlcReordered => self.column_reordered(),
|
||||
WxEventKind::DvlcContextMenu => self.show_context_menu(),
|
||||
WxEventKind::MenuItem => self.on_menu_item(ev.p1 as i32),
|
||||
WxEventKind::ChoiceChanged => self.on_choice_changed(ev.p1 as i32, ev.p2 as i32),
|
||||
WxEventKind::SysColour => self.repaint_theme(),
|
||||
WxEventKind::Deferred => self.on_deferred(ev.p1 as i32),
|
||||
WxEventKind::TimerTick if ev.p1 as i32 == TIMER_SCAN => self.tick(),
|
||||
_ => {}
|
||||
}
|
||||
@@ -926,4 +1289,14 @@ pub fn dispatch(ev: WxEvent) {
|
||||
dlg.show_modal(); // nested loop, no App borrow held; blocks until EndModal
|
||||
dlg.destroy(); // tear the dialog down (needs no App borrow, so none is taken)
|
||||
}
|
||||
// Phase 3: a pending right-click context menu. popup_menu runs its own nested
|
||||
// loop and its MenuItem event re-enters dispatch, so it must run with no App
|
||||
// borrow held (like a modal). The menu is deleted by popup_menu itself.
|
||||
let popup = APP.with(|cell| {
|
||||
let Ok(mut app_ref) = cell.try_borrow_mut() else { return None };
|
||||
app_ref.as_mut().and_then(|app| app.take_pending_popup())
|
||||
});
|
||||
if let Some((on, menu)) = popup {
|
||||
on.popup_menu(menu);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user