Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
eb08a9ef97 | ||
|
|
297e21a519 | ||
|
|
7fac0ce726 |
@@ -75,7 +75,6 @@ jobs:
|
||||
|
||||
- name: Headless UI self-test
|
||||
if: steps.ver.outputs.release == 'true'
|
||||
continue-on-error: true
|
||||
run: |
|
||||
apt-get update -qq && apt-get install -y -qq --no-install-recommends \
|
||||
xvfb xauth dbus-x11 fonts-dejavu-core >/dev/null
|
||||
|
||||
Generated
+1
@@ -80,6 +80,7 @@ dependencies = [
|
||||
"alkane-discovery",
|
||||
"asic-rs",
|
||||
"futures-util",
|
||||
"measurements",
|
||||
"tokio",
|
||||
"wx-rs",
|
||||
]
|
||||
|
||||
@@ -14,5 +14,6 @@ alkane-discovery = { path = "../alkane-discovery" }
|
||||
# vendored + patched wx-rs (see vendor/wx-rs/patches/)
|
||||
wx-rs = { path = "../../thirdparty/wx-rs/wx-rs-0.2.0-patched" }
|
||||
asic-rs = "0.8.1"
|
||||
measurements = "0.11"
|
||||
tokio = { version = "1", features = ["rt"] }
|
||||
futures-util = "0.3"
|
||||
|
||||
@@ -69,6 +69,17 @@ impl Board {
|
||||
]
|
||||
},
|
||||
operating_state: if self.mining { "mining".to_string() } else { "idle".to_string() },
|
||||
// Synthetic capabilities so the right-click config menu is
|
||||
// exercisable offline: mining units support everything, idle ones
|
||||
// only the fault light + restart (to show the capability intersection).
|
||||
caps: crate::model::MinerCaps {
|
||||
fault_light: true,
|
||||
restart: true,
|
||||
pause: self.mining,
|
||||
resume: self.mining,
|
||||
power_limit: self.mining,
|
||||
tuning: self.mining,
|
||||
},
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
|
||||
+210
-11
@@ -78,6 +78,48 @@ fn ip_to_u32(s: &str) -> u32 {
|
||||
acc
|
||||
}
|
||||
|
||||
/// What a miner supports being told to do, probed from asic-rs' `supports_*`
|
||||
/// capability methods at scan time. The right-click menu offers an action only
|
||||
/// when every selected miner supports it (the intersection of capabilities).
|
||||
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
|
||||
pub struct MinerCaps {
|
||||
pub fault_light: bool,
|
||||
pub restart: bool,
|
||||
pub pause: bool,
|
||||
pub resume: bool,
|
||||
pub power_limit: bool,
|
||||
pub tuning: bool,
|
||||
}
|
||||
|
||||
impl MinerCaps {
|
||||
/// The capabilities common to `self` and `other` (logical AND per action).
|
||||
pub fn intersect(self, other: MinerCaps) -> MinerCaps {
|
||||
MinerCaps {
|
||||
fault_light: self.fault_light && other.fault_light,
|
||||
restart: self.restart && other.restart,
|
||||
pause: self.pause && other.pause,
|
||||
resume: self.resume && other.resume,
|
||||
power_limit: self.power_limit && other.power_limit,
|
||||
tuning: self.tuning && other.tuning,
|
||||
}
|
||||
}
|
||||
|
||||
/// The capabilities common to every miner in `caps` (all-true identity when
|
||||
/// empty is intentionally avoided: an empty selection has no common caps).
|
||||
pub fn intersect_all(caps: impl IntoIterator<Item = MinerCaps>) -> MinerCaps {
|
||||
let mut it = caps.into_iter();
|
||||
match it.next() {
|
||||
Some(first) => it.fold(first, MinerCaps::intersect),
|
||||
None => MinerCaps::default(),
|
||||
}
|
||||
}
|
||||
|
||||
/// True when no action is supported (nothing to offer in the menu).
|
||||
pub fn is_empty(self) -> bool {
|
||||
self == MinerCaps::default()
|
||||
}
|
||||
}
|
||||
|
||||
/// One miner's telemetry snapshot, flattened from asic-rs `MinerData` into the
|
||||
/// scalar fields the table can display. Absent metrics are `None` / empty /
|
||||
/// 0.0 and render as a blank cell.
|
||||
@@ -85,6 +127,8 @@ fn ip_to_u32(s: &str) -> u32 {
|
||||
pub struct MinerRow {
|
||||
pub ip: String,
|
||||
pub selected: bool,
|
||||
/// What this miner supports being told to do (for the right-click menu).
|
||||
pub caps: MinerCaps,
|
||||
// identity
|
||||
pub model: String,
|
||||
pub mac: String,
|
||||
@@ -353,6 +397,22 @@ impl Column {
|
||||
)
|
||||
}
|
||||
|
||||
/// The numeric value used for numeric filtering, or `None` for a text column
|
||||
/// or a blank/missing cell (so `> 0` never matches an absent metric). A
|
||||
/// "blank" cell is exactly one the table renders empty -- several value-type
|
||||
/// columns store an absent reading as 0.0, which `cell()` blanks; mirror that
|
||||
/// here so a filter like "ChipTemp <= 50" cannot match a miner with no
|
||||
/// temperature reading.
|
||||
pub fn num_value(&self, r: &MinerRow) -> Option<f64> {
|
||||
if !self.is_numeric() || self.cell(r).is_empty() {
|
||||
return None;
|
||||
}
|
||||
match self.sort_key(r) {
|
||||
SortKey::Num(v) if v.is_finite() => Some(v),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Sort key. Numeric columns compare by their underlying number (absent =
|
||||
/// -inf so blanks sort to the bottom of a descending sort); others compare
|
||||
/// as lowercase strings.
|
||||
@@ -474,14 +534,78 @@ pub enum SortKey {
|
||||
Str(String),
|
||||
}
|
||||
|
||||
/// Text-column filter operators (Excel-style text filters).
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum TextOp {
|
||||
Contains,
|
||||
NotContains,
|
||||
BeginsWith,
|
||||
EndsWith,
|
||||
Equals,
|
||||
}
|
||||
|
||||
/// Numeric-column filter operators (Excel-style number filters).
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum NumOp {
|
||||
Between,
|
||||
Eq,
|
||||
Ne,
|
||||
Gt,
|
||||
Ge,
|
||||
Lt,
|
||||
Le,
|
||||
}
|
||||
|
||||
/// One column's active filter. Text columns can filter by a value checklist
|
||||
/// ("is any of") or a text predicate; numeric columns by a numeric predicate.
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum ColFilter {
|
||||
Values(HashSet<String>),
|
||||
Text { op: TextOp, needle: String },
|
||||
Num { op: NumOp, a: f64, b: f64 },
|
||||
}
|
||||
|
||||
impl ColFilter {
|
||||
/// Does `r`'s value in `col` satisfy this filter?
|
||||
pub fn matches(&self, col: Column, r: &MinerRow) -> bool {
|
||||
match self {
|
||||
ColFilter::Values(set) => set.contains(&col.cell(r)),
|
||||
ColFilter::Text { op, needle } => {
|
||||
let hay = col.cell(r).to_lowercase();
|
||||
let n = needle.to_lowercase();
|
||||
match op {
|
||||
TextOp::Contains => hay.contains(&n),
|
||||
TextOp::NotContains => !hay.contains(&n),
|
||||
TextOp::BeginsWith => hay.starts_with(&n),
|
||||
TextOp::EndsWith => hay.ends_with(&n),
|
||||
TextOp::Equals => hay == n,
|
||||
}
|
||||
}
|
||||
ColFilter::Num { op, a, b } => {
|
||||
let Some(v) = col.num_value(r) else { return false };
|
||||
let (a, b) = (*a, *b);
|
||||
match op {
|
||||
NumOp::Between => v >= a.min(b) && v <= a.max(b),
|
||||
NumOp::Eq => v == a,
|
||||
NumOp::Ne => v != a,
|
||||
NumOp::Gt => v > a,
|
||||
NumOp::Ge => v >= a,
|
||||
NumOp::Lt => v < a,
|
||||
NumOp::Le => v <= a,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Display state: order, visibility, sort, filter.
|
||||
pub struct ViewState {
|
||||
/// Model column order as displayed (Ip is always the first visible column).
|
||||
pub order: Vec<Column>,
|
||||
pub visible: HashSet<Column>,
|
||||
pub sort: Option<(Column, bool)>, // (column, ascending)
|
||||
/// Per-column allowed values; None = unfiltered.
|
||||
pub filters: HashMap<Column, HashSet<String>>,
|
||||
/// Active per-column filters (AND across columns); empty = unfiltered.
|
||||
pub filters: HashMap<Column, ColFilter>,
|
||||
}
|
||||
|
||||
impl Default for ViewState {
|
||||
@@ -532,11 +656,9 @@ impl ViewState {
|
||||
.collect();
|
||||
}
|
||||
|
||||
/// Rows that pass every active filter (AND across columns, OR within one).
|
||||
/// Rows that pass every active filter (AND across columns).
|
||||
pub fn passes_filters(&self, r: &MinerRow) -> bool {
|
||||
self.filters
|
||||
.iter()
|
||||
.all(|(col, allowed)| allowed.contains(&col.cell(r)))
|
||||
self.filters.iter().all(|(col, f)| f.matches(*col, r))
|
||||
}
|
||||
|
||||
/// Filtered + sorted rows in display order.
|
||||
@@ -684,7 +806,7 @@ mod tests {
|
||||
let mut view = ViewState::default();
|
||||
let mut allowed = HashSet::new();
|
||||
allowed.insert("slow".to_string());
|
||||
view.filters.insert(Column::Status, allowed);
|
||||
view.filters.insert(Column::Status, ColFilter::Values(allowed));
|
||||
let idx = view.view(&rows);
|
||||
assert_eq!(idx.len(), 1);
|
||||
assert_eq!(rows[idx[0]].ip, "10.255.1.149");
|
||||
@@ -938,8 +1060,8 @@ mod tests {
|
||||
MinerRow { ip: "10.0.0.3".into(), is_mining: false, model: "A".into(), ..Default::default() },
|
||||
];
|
||||
let mut view = ViewState::default();
|
||||
view.filters.insert(Column::Status, HashSet::from(["hashing".to_string()]));
|
||||
view.filters.insert(Column::Model, HashSet::from(["A".to_string()]));
|
||||
view.filters.insert(Column::Status, ColFilter::Values(HashSet::from(["hashing".to_string()])));
|
||||
view.filters.insert(Column::Model, ColFilter::Values(HashSet::from(["A".to_string()])));
|
||||
let idx = view.view(&rows);
|
||||
assert_eq!(idx.len(), 1, "only the hashing AND model-A row passes");
|
||||
assert_eq!(rows[idx[0]].ip, "10.0.0.1");
|
||||
@@ -951,7 +1073,7 @@ mod tests {
|
||||
let mut view = ViewState::default();
|
||||
view.filters.insert(
|
||||
Column::Status,
|
||||
HashSet::from(["slow".to_string(), "not hashing".to_string()]),
|
||||
ColFilter::Values(HashSet::from(["slow".to_string(), "not hashing".to_string()])),
|
||||
);
|
||||
let idx = view.view(&rows);
|
||||
// fixtures: 1 slow + 2 not-hashing.
|
||||
@@ -1027,7 +1149,7 @@ mod tests {
|
||||
use std::time::Instant;
|
||||
let rows = synthetic_fleet(5000);
|
||||
let mut view = ViewState::default();
|
||||
view.filters.insert(Column::Model, HashSet::from(["Model-3".to_string()]));
|
||||
view.filters.insert(Column::Model, ColFilter::Values(HashSet::from(["Model-3".to_string()])));
|
||||
view.sort = Some((Column::Hashrate, false));
|
||||
let iters = 200u32;
|
||||
let t = Instant::now();
|
||||
@@ -1057,4 +1179,81 @@ mod tests {
|
||||
println!("bench_upsert_cold_start_population: {n} rows cold-start = {:?}", elapsed);
|
||||
assert!(elapsed.as_millis() < 2000, "cold-start upsert regressed: {elapsed:?} for {n} rows");
|
||||
}
|
||||
|
||||
// ---- Excel-style column filters (round 3) ----
|
||||
|
||||
#[test]
|
||||
fn text_filter_operators_are_case_insensitive() {
|
||||
let r = MinerRow { firmware: "WMOS H616".into(), ..Default::default() };
|
||||
let c = Column::Firmware;
|
||||
let t = |op, needle: &str| ColFilter::Text { op, needle: needle.into() }.matches(c, &r);
|
||||
assert!(t(TextOp::Contains, "wmos"));
|
||||
assert!(!t(TextOp::Contains, "vnish"));
|
||||
assert!(!t(TextOp::NotContains, "wmos"));
|
||||
assert!(t(TextOp::NotContains, "vnish"));
|
||||
assert!(t(TextOp::BeginsWith, "WMOS"));
|
||||
assert!(!t(TextOp::BeginsWith, "616"));
|
||||
assert!(t(TextOp::EndsWith, "h616"));
|
||||
assert!(t(TextOp::Equals, "wmos h616"));
|
||||
assert!(!t(TextOp::Equals, "wmos"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn numeric_filter_operators() {
|
||||
let r = MinerRow { hashrate_ths: 100.0, is_mining: true, ..Default::default() };
|
||||
let c = Column::Hashrate;
|
||||
let n = |op, a: f64, b: f64| ColFilter::Num { op, a, b }.matches(c, &r);
|
||||
assert!(n(NumOp::Gt, 50.0, 0.0));
|
||||
assert!(!n(NumOp::Gt, 150.0, 0.0));
|
||||
assert!(n(NumOp::Ge, 100.0, 0.0));
|
||||
assert!(n(NumOp::Lt, 150.0, 0.0));
|
||||
assert!(n(NumOp::Le, 100.0, 0.0));
|
||||
assert!(n(NumOp::Eq, 100.0, 0.0));
|
||||
assert!(n(NumOp::Ne, 99.0, 0.0));
|
||||
assert!(n(NumOp::Between, 90.0, 110.0));
|
||||
assert!(n(NumOp::Between, 110.0, 90.0), "bounds are order-agnostic");
|
||||
assert!(!n(NumOp::Between, 0.0, 50.0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn num_value_none_for_text_and_blank() {
|
||||
let r = MinerRow { firmware: "x".into(), efficiency: 0.0, ..Default::default() };
|
||||
assert_eq!(Column::Firmware.num_value(&r), None, "text column");
|
||||
assert_eq!(Column::Ip.num_value(&r), None, "IP is text, not numeric");
|
||||
assert_eq!(Column::Efficiency.num_value(&r), None, "blank numeric -> None");
|
||||
let r2 = MinerRow { hashrate_ths: 42.0, ..Default::default() };
|
||||
assert_eq!(Column::Hashrate.num_value(&r2), Some(42.0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn caps_intersection_is_the_common_actions() {
|
||||
let full = MinerCaps { fault_light: true, restart: true, pause: true, resume: true, power_limit: true, tuning: true };
|
||||
let idle = MinerCaps { fault_light: true, restart: true, ..Default::default() };
|
||||
// Two full miners keep every action.
|
||||
assert_eq!(MinerCaps::intersect_all([full, full]), full);
|
||||
// A full + idle mix keeps only what both support.
|
||||
assert_eq!(MinerCaps::intersect_all([full, idle]), idle);
|
||||
// A single miner's caps are its own.
|
||||
assert_eq!(MinerCaps::intersect_all([full]), full);
|
||||
// Empty selection -> nothing common.
|
||||
assert_eq!(MinerCaps::intersect_all([]), MinerCaps::default());
|
||||
assert!(MinerCaps::default().is_empty());
|
||||
assert!(!full.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn numeric_filter_excludes_blank_cells() {
|
||||
// A blank-displaying value-type cell (absent reading stored as 0.0) must
|
||||
// not match a numeric predicate -- it has no value to compare.
|
||||
let blank = MinerRow { hashrate_ths: 0.0, chip_temp_c: 0.0, power_w: 0.0, is_mining: false, ..Default::default() };
|
||||
assert_eq!(Column::Hashrate.num_value(&blank), None);
|
||||
assert_eq!(Column::ChipTemp.num_value(&blank), None);
|
||||
assert_eq!(Column::Power.num_value(&blank), None);
|
||||
// "<= 50" and "<= 1000" must NOT match a miner with no temp / power.
|
||||
assert!(!ColFilter::Num { op: NumOp::Le, a: 50.0, b: 0.0 }.matches(Column::ChipTemp, &blank));
|
||||
assert!(!ColFilter::Num { op: NumOp::Lt, a: 1000.0, b: 0.0 }.matches(Column::Power, &blank));
|
||||
// A real reading still matches.
|
||||
let live = MinerRow { chip_temp_c: 45.0, is_mining: true, ..Default::default() };
|
||||
assert!(ColFilter::Num { op: NumOp::Le, a: 50.0, b: 0.0 }.matches(Column::ChipTemp, &live));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
//! Scan engine: drives vendored asic-rs over the configured subnets and
|
||||
//! streams MinerRow snapshots to the UI thread via a std channel.
|
||||
|
||||
use crate::model::MinerRow;
|
||||
use crate::model::{MinerCaps, MinerRow};
|
||||
use asic_rs::MinerFactory;
|
||||
use asic_rs::core::traits::miner::Miner;
|
||||
use futures_util::StreamExt;
|
||||
use measurements::Power;
|
||||
use std::collections::HashSet;
|
||||
use std::net::Ipv4Addr;
|
||||
use std::path::PathBuf;
|
||||
@@ -419,9 +420,19 @@ async fn to_row(miner: &dyn Miner) -> MinerRow {
|
||||
.fold(0.0f64, f64::max);
|
||||
let is_mining = data.is_mining;
|
||||
let slow = crate::model::is_slow(is_mining, hashrate_ths, expected_ths);
|
||||
// Probe what this miner supports being told to do (sync, no extra I/O).
|
||||
let caps = MinerCaps {
|
||||
fault_light: miner.supports_set_fault_light(),
|
||||
restart: miner.supports_restart(),
|
||||
pause: miner.supports_pause(),
|
||||
resume: miner.supports_resume(),
|
||||
power_limit: miner.supports_set_power_limit(),
|
||||
tuning: miner.supports_set_tuning_percent(),
|
||||
};
|
||||
MinerRow {
|
||||
ip: data.ip.to_string(),
|
||||
selected: false,
|
||||
caps,
|
||||
model,
|
||||
mac: data.mac.map(|m| m.to_string()).unwrap_or_default(),
|
||||
hostname: data.hostname.clone().unwrap_or_default(),
|
||||
@@ -458,6 +469,136 @@ async fn to_row(miner: &dyn Miner) -> MinerRow {
|
||||
}
|
||||
}
|
||||
|
||||
/// A one-shot configuration action applied to a set of miners from the
|
||||
/// right-click menu.
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum ConfigAction {
|
||||
FaultLight(bool),
|
||||
Restart,
|
||||
Pause,
|
||||
Resume,
|
||||
PowerLimit(f64), // watts
|
||||
TuningPercent(u8),
|
||||
}
|
||||
|
||||
impl ConfigAction {
|
||||
/// Short label for status text and summaries.
|
||||
pub fn label(&self) -> String {
|
||||
match self {
|
||||
ConfigAction::FaultLight(true) => "fault light on".into(),
|
||||
ConfigAction::FaultLight(false) => "fault light off".into(),
|
||||
ConfigAction::Restart => "restart".into(),
|
||||
ConfigAction::Pause => "pause".into(),
|
||||
ConfigAction::Resume => "resume".into(),
|
||||
ConfigAction::PowerLimit(w) => format!("power limit {w:.0} W"),
|
||||
ConfigAction::TuningPercent(p) => format!("tuning {p}%"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Progress/results from a running config action.
|
||||
pub enum ActionMsg {
|
||||
Started { total: usize },
|
||||
Result { ip: String, ok: bool, detail: String },
|
||||
Done { ok: usize, total: usize },
|
||||
}
|
||||
|
||||
/// A running config action; `stop` cancels the remaining miners.
|
||||
pub struct ActionHandle {
|
||||
pub rx: Receiver<ActionMsg>,
|
||||
cancel: Arc<AtomicBool>,
|
||||
}
|
||||
|
||||
impl ActionHandle {
|
||||
pub fn stop(&self) {
|
||||
self.cancel.store(true, Ordering::Relaxed);
|
||||
}
|
||||
}
|
||||
|
||||
async fn apply_action(miner: &dyn Miner, action: &ConfigAction) -> Result<bool, String> {
|
||||
let result = match action {
|
||||
ConfigAction::FaultLight(on) => miner.set_fault_light(*on).await,
|
||||
ConfigAction::Restart => miner.restart().await,
|
||||
ConfigAction::Pause => miner.pause(None).await,
|
||||
ConfigAction::Resume => miner.resume(None).await,
|
||||
ConfigAction::PowerLimit(w) => miner.set_power_limit(Power::from_watts(*w)).await,
|
||||
ConfigAction::TuningPercent(p) => miner.set_tuning_percent(*p).await,
|
||||
};
|
||||
result.map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
/// Apply `action` to each miner in `ips` on a background thread: re-discover the
|
||||
/// miner (asic-rs `scan_miner`), run the setter, and report per-IP results. In
|
||||
/// fake-scan mode it reports simulated successes so the flow works offline.
|
||||
pub fn start_config_action(ips: Vec<String>, action: ConfigAction) -> ActionHandle {
|
||||
let (tx, rx) = channel();
|
||||
let cancel = Arc::new(AtomicBool::new(false));
|
||||
let total = ips.len();
|
||||
let _ = tx.send(ActionMsg::Started { total });
|
||||
|
||||
if fake_scan_enabled() {
|
||||
let err_tx = tx.clone();
|
||||
let res = std::thread::Builder::new().name("fake-action".into()).spawn(move || {
|
||||
let mut ok = 0usize;
|
||||
for ip in ips {
|
||||
let _ = tx.send(ActionMsg::Result { ip, ok: true, detail: "simulated".into() });
|
||||
ok += 1;
|
||||
}
|
||||
let _ = tx.send(ActionMsg::Done { ok, total });
|
||||
});
|
||||
if res.is_err() {
|
||||
let _ = err_tx.send(ActionMsg::Done { ok: 0, total });
|
||||
}
|
||||
return ActionHandle { rx, cancel };
|
||||
}
|
||||
|
||||
let thread_cancel = cancel.clone();
|
||||
let err_tx = tx.clone();
|
||||
let res = std::thread::Builder::new().name("config-action".into()).spawn(move || {
|
||||
// A single-threaded runtime: the loop applies to one miner at a time, so
|
||||
// a worker-per-CPU pool would be wasted.
|
||||
let rt = match tokio::runtime::Builder::new_current_thread().enable_all().build() {
|
||||
Ok(rt) => rt,
|
||||
Err(_) => {
|
||||
let _ = tx.send(ActionMsg::Done { ok: 0, total });
|
||||
return;
|
||||
}
|
||||
};
|
||||
rt.block_on(async move {
|
||||
let factory = MinerFactory::new();
|
||||
let mut ok = 0usize;
|
||||
for ip_s in ips {
|
||||
if thread_cancel.load(Ordering::Relaxed) {
|
||||
break;
|
||||
}
|
||||
let (success, detail) = match ip_s.parse::<std::net::IpAddr>() {
|
||||
Err(_) => (false, "bad IP".to_string()),
|
||||
Ok(ip) => match factory.scan_miner(ip).await {
|
||||
Ok(Some(miner)) => match apply_action(&*miner, &action).await {
|
||||
Ok(true) => (true, "ok".to_string()),
|
||||
Ok(false) => (false, "refused".to_string()),
|
||||
Err(e) => (false, format!("{e}")),
|
||||
},
|
||||
Ok(None) => (false, "no miner at IP".to_string()),
|
||||
Err(e) => (false, format!("{e}")),
|
||||
},
|
||||
};
|
||||
if success {
|
||||
ok += 1;
|
||||
}
|
||||
if tx.send(ActionMsg::Result { ip: ip_s, ok: success, detail }).is_err() {
|
||||
return; // UI gone
|
||||
}
|
||||
}
|
||||
let _ = tx.send(ActionMsg::Done { ok, total });
|
||||
});
|
||||
});
|
||||
if res.is_err() {
|
||||
let _ = err_tx.send(ActionMsg::Done { ok: 0, total });
|
||||
}
|
||||
ActionHandle { rx, cancel }
|
||||
}
|
||||
|
||||
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
+755
-108
File diff suppressed because it is too large
Load Diff
+84
-1
@@ -10,7 +10,7 @@
|
||||
+pub use widgets::*;
|
||||
--- a/src/widgets.rs
|
||||
+++ b/src/widgets.rs
|
||||
@@ -0,0 +1,561 @@
|
||||
@@ -0,0 +1,644 @@
|
||||
+// widgets.rs — alkane-toolkit extension: Rust bindings for the widget bridge
|
||||
+// in cpp_src/widgets_bridge.cpp. Handles are opaque pointers owned by the
|
||||
+// wxWidgets object tree (parents own children; nothing to free in Rust).
|
||||
@@ -77,6 +77,7 @@
|
||||
+ pub fn wxbr_dvlc_get_selections(win: *mut c_void, buf: *mut c_int, cap: c_int) -> c_int;
|
||||
+ pub fn wxbr_dvlc_select_rows(win: *mut c_void, rows: *const c_int, n: c_int);
|
||||
+ pub fn wxbr_dvlc_set_alt_row_color(win: *mut c_void, r: c_int, g: c_int, b: c_int);
|
||||
+ pub fn wxbr_dvlc_set_row_color(win: *mut c_void, row: usize, r: c_int, g: c_int, b: c_int);
|
||||
+ pub fn wxbr_apply_zebra_css(r: c_int, g: c_int, b: c_int);
|
||||
+ pub fn wxbr_rearrange_list(parent: *mut c_void, n: c_int, order: *const c_int, items: *const *const c_char) -> *mut c_void;
|
||||
+ pub fn wxbr_rearrange_get_order(win: *mut c_void, buf: *mut c_int, cap: c_int) -> c_int;
|
||||
@@ -96,7 +97,16 @@
|
||||
+ pub fn wxbr_system_color(which: c_int, r: *mut u8, g: *mut u8, b: *mut u8);
|
||||
+ pub fn wxbr_set_bg(win: *mut c_void, r: c_int, g: c_int, b: c_int);
|
||||
+ pub fn wxbr_set_fg(win: *mut c_void, r: c_int, g: c_int, b: c_int);
|
||||
+ pub fn wxbr_choice(parent: *mut c_void, id: c_int) -> *mut c_void;
|
||||
+ pub fn wxbr_choice_append(win: *mut c_void, label: *const c_char);
|
||||
+ pub fn wxbr_choice_clear(win: *mut c_void);
|
||||
+ pub fn wxbr_choice_get_selection(win: *mut c_void) -> c_int;
|
||||
+ pub fn wxbr_choice_set_selection(win: *mut c_void, i: c_int);
|
||||
+ pub fn wxbr_media_button(parent: *mut c_void, id: c_int, kind: c_int, r: c_int, g: c_int, b: c_int) -> *mut c_void;
|
||||
+ pub fn wxbr_bind_sys_colour(win: *mut c_void);
|
||||
+ pub fn wxbr_destroy(win: *mut c_void);
|
||||
+ pub fn wxbr_destroy_children(win: *mut c_void);
|
||||
+ pub fn wxbr_call_after(id: c_int);
|
||||
+ }
|
||||
+}
|
||||
+
|
||||
@@ -129,6 +139,9 @@
|
||||
+ DvlcSelectionChanged,
|
||||
+ DvlcContextMenu,
|
||||
+ MenuItem,
|
||||
+ ChoiceChanged,
|
||||
+ SysColour,
|
||||
+ Deferred,
|
||||
+}
|
||||
+
|
||||
+impl WxEventKind {
|
||||
@@ -144,6 +157,9 @@
|
||||
+ 9 => WxEventKind::DvlcSelectionChanged,
|
||||
+ 10 => WxEventKind::DvlcContextMenu,
|
||||
+ 11 => WxEventKind::MenuItem,
|
||||
+ 12 => WxEventKind::ChoiceChanged,
|
||||
+ 13 => WxEventKind::SysColour,
|
||||
+ 14 => WxEventKind::Deferred,
|
||||
+ _ => return None,
|
||||
+ })
|
||||
+ }
|
||||
@@ -154,6 +170,13 @@
|
||||
+ unsafe { notsafe::wxbr_apply_zebra_css(r as c_int, g as c_int, b as c_int) }
|
||||
+}
|
||||
+
|
||||
+/// Schedule a deferred action: a `Deferred` event carrying `id` is delivered
|
||||
+/// from the event loop after the current event finishes. Use this to rebuild or
|
||||
+/// destroy a control from outside its own event handler.
|
||||
+pub fn call_after(id: i32) {
|
||||
+ unsafe { notsafe::wxbr_call_after(id as c_int) }
|
||||
+}
|
||||
+
|
||||
+/// Open a URL in the system default browser. Returns true on success.
|
||||
+pub fn launch_browser(url: &str) -> bool {
|
||||
+ let c = CString::new(url).unwrap_or_default();
|
||||
@@ -472,6 +495,15 @@
|
||||
+ unsafe { notsafe::wxbr_dvlc_set_alt_row_color(self.0, r as c_int, g as c_int, b as c_int) }
|
||||
+ }
|
||||
+
|
||||
+ /// Set a row's text colour (e.g. by miner status). Pass `None` to clear it.
|
||||
+ pub fn dvlc_set_row_color(&self, row: usize, color: Option<(u8, u8, u8)>) {
|
||||
+ let (r, g, b) = match color {
|
||||
+ Some((r, g, b)) => (r as c_int, g as c_int, b as c_int),
|
||||
+ None => (-1, -1, -1),
|
||||
+ };
|
||||
+ unsafe { notsafe::wxbr_dvlc_set_row_color(self.0, row, r, g, b) }
|
||||
+ }
|
||||
+
|
||||
+ // ---- rearrangeable check-list ----
|
||||
+
|
||||
+ /// Create a rearrangeable checklist. `order` uses wx encoding: a value `v`
|
||||
@@ -566,9 +598,60 @@
|
||||
+ unsafe { notsafe::wxbr_set_fg(self.0, r as c_int, g as c_int, b as c_int) }
|
||||
+ }
|
||||
+
|
||||
+ // ---- choice (dropdown) ----
|
||||
+
|
||||
+ /// Create a dropdown; selection changes emit `ChoiceChanged` with this id.
|
||||
+ pub fn choice(parent: Widget, id: i32) -> Widget {
|
||||
+ Widget(unsafe { notsafe::wxbr_choice(parent.0, id) })
|
||||
+ }
|
||||
+
|
||||
+ pub fn choice_append(&self, label: &str) {
|
||||
+ let c = CString::new(label).unwrap_or_default();
|
||||
+ unsafe { notsafe::wxbr_choice_append(self.0, c.as_ptr()) }
|
||||
+ }
|
||||
+
|
||||
+ pub fn choice_clear(&self) {
|
||||
+ unsafe { notsafe::wxbr_choice_clear(self.0) }
|
||||
+ }
|
||||
+
|
||||
+ pub fn choice_get_selection(&self) -> i32 {
|
||||
+ unsafe { notsafe::wxbr_choice_get_selection(self.0) }
|
||||
+ }
|
||||
+
|
||||
+ pub fn choice_set_selection(&self, i: i32) {
|
||||
+ unsafe { notsafe::wxbr_choice_set_selection(self.0, i) }
|
||||
+ }
|
||||
+
|
||||
+ // ---- media (icon) button ----
|
||||
+
|
||||
+ /// A coloured icon button: `kind` 0=play, 1=pause, 2=stop. Clicks emit
|
||||
+ /// `ButtonClicked` with `id`, like a normal button.
|
||||
+ pub fn media_button(parent: Widget, id: i32, kind: i32, color: (u8, u8, u8)) -> Widget {
|
||||
+ Widget(unsafe {
|
||||
+ notsafe::wxbr_media_button(
|
||||
+ parent.0,
|
||||
+ id,
|
||||
+ kind,
|
||||
+ color.0 as c_int,
|
||||
+ color.1 as c_int,
|
||||
+ color.2 as c_int,
|
||||
+ )
|
||||
+ })
|
||||
+ }
|
||||
+
|
||||
+ /// Listen for OS light/dark theme changes on this window (emits `SysColour`).
|
||||
+ pub fn bind_sys_colour(&self) {
|
||||
+ unsafe { notsafe::wxbr_bind_sys_colour(self.0) }
|
||||
+ }
|
||||
+
|
||||
+ /// Deferred destruction (safe from event handlers); used to rebuild
|
||||
+ /// the table when its column set changes.
|
||||
+ pub fn destroy(&self) {
|
||||
+ unsafe { notsafe::wxbr_destroy(self.0) }
|
||||
+ }
|
||||
+
|
||||
+ /// Destroy this window's children (to rebuild dynamic panel contents).
|
||||
+ pub fn destroy_children(&self) {
|
||||
+ unsafe { notsafe::wxbr_destroy_children(self.0) }
|
||||
+ }
|
||||
+}
|
||||
|
||||
+206
-62
@@ -1,6 +1,6 @@
|
||||
--- a/cpp_src/widgets_bridge.cpp
|
||||
+++ b/cpp_src/widgets_bridge.cpp
|
||||
@@ -0,0 +1,598 @@
|
||||
@@ -0,0 +1,742 @@
|
||||
+// widgets_bridge.cpp — alkane-toolkit extension to wx-rs
|
||||
+// Adds the widget/event surface the toolkit UI needs on top of the upstream
|
||||
+// render-callback bridge (which is kept untouched).
|
||||
@@ -23,10 +23,19 @@
|
||||
+#include <wx/app.h>
|
||||
+#include <wx/rearrangectrl.h>
|
||||
+#include <wx/menu.h>
|
||||
+#include <wx/choice.h>
|
||||
+#include <wx/bmpbuttn.h>
|
||||
+#include <wx/bitmap.h>
|
||||
+#include <wx/image.h>
|
||||
+#include <wx/dcmemory.h>
|
||||
+#include <wx/dcgraph.h>
|
||||
+#include <wx/graphics.h>
|
||||
+#include <cstring>
|
||||
+#ifdef __WXGTK__
|
||||
+#include <gtk/gtk.h>
|
||||
+#endif
|
||||
+#include <cstdio>
|
||||
+#include <vector>
|
||||
+
|
||||
+typedef void (*WxWidgetEventFn)(int kind, long p1, long p2);
|
||||
+static WxWidgetEventFn g_widget_event = nullptr;
|
||||
@@ -43,6 +52,27 @@
|
||||
+ WxEV_DVLC_SELECTION_CHANGED = 9,
|
||||
+ WxEV_DVLC_CONTEXT_MENU = 10,
|
||||
+ WxEV_MENU_ITEM = 11,
|
||||
+ WxEV_CHOICE_CHANGED = 12,
|
||||
+ WxEV_SYS_COLOUR = 13,
|
||||
+ WxEV_DEFERRED = 14,
|
||||
+};
|
||||
+
|
||||
+// A list store that carries an optional per-row foreground colour, surfaced to
|
||||
+// the native control via GetAttrByRow. This is how the miner table paints each
|
||||
+// row's text by status and separates rows on GTK (where SetAlternateRowColour
|
||||
+// and :nth-child CSS are unreliable and fight the selection highlight).
|
||||
+class AttrListStore : public wxDataViewListStore {
|
||||
+ public:
|
||||
+ std::vector<wxColour> row_fg;
|
||||
+ bool GetAttrByRow(unsigned int row, unsigned int col,
|
||||
+ wxDataViewItemAttr& attr) const override {
|
||||
+ (void)col;
|
||||
+ if (row < row_fg.size() && row_fg[row].IsOk()) {
|
||||
+ attr.SetColour(row_fg[row]);
|
||||
+ return true;
|
||||
+ }
|
||||
+ return false;
|
||||
+ }
|
||||
+};
|
||||
+
|
||||
+extern "C" {
|
||||
@@ -269,63 +299,73 @@
|
||||
+
|
||||
+// ---- data view list ctrl ------------------------------------------------------
|
||||
+
|
||||
+static AttrListStore* dvlc_store(void* win) {
|
||||
+ return static_cast<AttrListStore*>(((wxDataViewCtrl*)win)->GetModel());
|
||||
+}
|
||||
+
|
||||
+void* wxbr_dvlc(void* parent) {
|
||||
+ // Multi-select (ctrl/shift-click) + row lines. Selection replaces the old
|
||||
+ // checkbox column.
|
||||
+ wxDataViewListCtrl* lc =
|
||||
+ new wxDataViewListCtrl((wxWindow*)parent, wxID_ANY, wxDefaultPosition,
|
||||
+ wxSize(-1, -1), wxDV_ROW_LINES | wxDV_MULTIPLE);
|
||||
+ lc->Bind(wxEVT_DATAVIEW_SELECTION_CHANGED, [](wxDataViewEvent&) {
|
||||
+ // checkbox column. A custom AttrListStore backs it so rows can be coloured by
|
||||
+ // status while the native selection highlight still shows through.
|
||||
+ wxDataViewCtrl* dv =
|
||||
+ new wxDataViewCtrl((wxWindow*)parent, wxID_ANY, wxDefaultPosition,
|
||||
+ wxSize(-1, -1), wxDV_ROW_LINES | wxDV_MULTIPLE);
|
||||
+ AttrListStore* store = new AttrListStore();
|
||||
+ dv->AssociateModel(store);
|
||||
+ store->DecRef(); // the control owns a reference now
|
||||
+ dv->Bind(wxEVT_DATAVIEW_SELECTION_CHANGED, [](wxDataViewEvent&) {
|
||||
+ if (g_widget_event) g_widget_event(WxEV_DVLC_SELECTION_CHANGED, 0, 0);
|
||||
+ });
|
||||
+ lc->Bind(wxEVT_DATAVIEW_ITEM_CONTEXT_MENU, [](wxDataViewEvent&) {
|
||||
+ dv->Bind(wxEVT_DATAVIEW_ITEM_CONTEXT_MENU, [](wxDataViewEvent&) {
|
||||
+ if (g_widget_event) g_widget_event(WxEV_DVLC_CONTEXT_MENU, 0, 0);
|
||||
+ });
|
||||
+ lc->Bind(wxEVT_DATAVIEW_ITEM_VALUE_CHANGED, [](wxDataViewEvent& e) {
|
||||
+ dv->Bind(wxEVT_DATAVIEW_ITEM_VALUE_CHANGED, [](wxDataViewEvent& e) {
|
||||
+ if (g_widget_event) {
|
||||
+ wxDataViewListCtrl* lc =
|
||||
+ dynamic_cast<wxDataViewListCtrl*>((wxWindow*)e.GetEventObject());
|
||||
+ if (!lc) return;
|
||||
+ wxDataViewCtrl* dv = (wxDataViewCtrl*)e.GetEventObject();
|
||||
+ if (!dv) return;
|
||||
+ wxDataViewItem item = e.GetItem();
|
||||
+ if (!item.IsOk()) return;
|
||||
+ int row = lc->GetStore()->GetRow(item);
|
||||
+ int row = ((wxDataViewIndexListModel*)dv->GetModel())->GetRow(item);
|
||||
+ g_widget_event(WxEV_DVLC_VALUE_CHANGED, row, (long)e.GetColumn());
|
||||
+ }
|
||||
+ });
|
||||
+ lc->Bind(wxEVT_DATAVIEW_COLUMN_HEADER_CLICK, [](wxDataViewEvent& e) {
|
||||
+ dv->Bind(wxEVT_DATAVIEW_COLUMN_HEADER_CLICK, [](wxDataViewEvent& e) {
|
||||
+ if (g_widget_event) g_widget_event(WxEV_DVLC_HEADER_CLICK, (long)e.GetColumn(), 0);
|
||||
+ });
|
||||
+ lc->Bind(wxEVT_DATAVIEW_COLUMN_REORDERED, [](wxDataViewEvent&) {
|
||||
+ dv->Bind(wxEVT_DATAVIEW_COLUMN_REORDERED, [](wxDataViewEvent&) {
|
||||
+ if (g_widget_event) g_widget_event(WxEV_DVLC_REORDERED, 0, 0);
|
||||
+ });
|
||||
+ // Row activation (double-click / Enter): report the row + column so the app
|
||||
+ // can, e.g., open the miner's IP in a browser.
|
||||
+ lc->Bind(wxEVT_DATAVIEW_ITEM_ACTIVATED, [](wxDataViewEvent& e) {
|
||||
+ dv->Bind(wxEVT_DATAVIEW_ITEM_ACTIVATED, [](wxDataViewEvent& e) {
|
||||
+ if (g_widget_event) {
|
||||
+ wxDataViewListCtrl* lc =
|
||||
+ dynamic_cast<wxDataViewListCtrl*>((wxWindow*)e.GetEventObject());
|
||||
+ if (!lc) return;
|
||||
+ wxDataViewCtrl* dv = (wxDataViewCtrl*)e.GetEventObject();
|
||||
+ if (!dv) return;
|
||||
+ wxDataViewItem item = e.GetItem();
|
||||
+ if (!item.IsOk()) return;
|
||||
+ int row = lc->GetStore()->GetRow(item);
|
||||
+ int row = ((wxDataViewIndexListModel*)dv->GetModel())->GetRow(item);
|
||||
+ g_widget_event(WxEV_DVLC_ACTIVATED, row, (long)e.GetColumn());
|
||||
+ }
|
||||
+ });
|
||||
+ return (void*)lc;
|
||||
+ return (void*)dv;
|
||||
+}
|
||||
+
|
||||
+void wxbr_dvlc_append_text_col(void* win, const char* title, int width, int reorderable) {
|
||||
+ wxDataViewCtrl* dv = (wxDataViewCtrl*)win;
|
||||
+ int flags = wxDATAVIEW_COL_RESIZABLE;
|
||||
+ if (reorderable) flags |= wxDATAVIEW_COL_REORDERABLE;
|
||||
+ ((wxDataViewListCtrl*)win)
|
||||
+ ->AppendTextColumn(wxString::FromUTF8(title), wxDATAVIEW_CELL_INERT,
|
||||
+ width, wxALIGN_NOT, flags);
|
||||
+ unsigned col = dv->GetColumnCount(); // next model column index
|
||||
+ dvlc_store(win)->AppendColumn("string");
|
||||
+ dv->AppendTextColumn(wxString::FromUTF8(title), col, wxDATAVIEW_CELL_INERT,
|
||||
+ width, wxALIGN_NOT, flags);
|
||||
+}
|
||||
+
|
||||
+void wxbr_dvlc_append_toggle_col(void* win, int width) {
|
||||
+ ((wxDataViewListCtrl*)win)
|
||||
+ ->AppendToggleColumn(wxT(""), wxDATAVIEW_CELL_ACTIVATABLE, width,
|
||||
+ wxALIGN_CENTRE, 0);
|
||||
+ wxDataViewCtrl* dv = (wxDataViewCtrl*)win;
|
||||
+ unsigned col = dv->GetColumnCount();
|
||||
+ dvlc_store(win)->AppendColumn("bool");
|
||||
+ dv->AppendToggleColumn(wxT(""), col, wxDATAVIEW_CELL_ACTIVATABLE, width,
|
||||
+ wxALIGN_CENTRE, 0);
|
||||
+}
|
||||
+
|
||||
+void wxbr_dvlc_append_row(void* win, int nvals, const char** vals) {
|
||||
@@ -334,28 +374,45 @@
|
||||
+ for (int i = 0; i < nvals; i++) {
|
||||
+ values.push_back(wxVariant(vals[i] ? wxString::FromUTF8(vals[i]) : wxString()));
|
||||
+ }
|
||||
+ ((wxDataViewListCtrl*)win)->AppendItem(values);
|
||||
+ AttrListStore* store = dvlc_store(win);
|
||||
+ store->AppendItem(values);
|
||||
+ store->row_fg.push_back(wxColour()); // no per-row colour until set
|
||||
+}
|
||||
+
|
||||
+void wxbr_dvlc_delete_row(void* win, unsigned row) {
|
||||
+ ((wxDataViewListCtrl*)win)->DeleteItem(row);
|
||||
+ AttrListStore* store = dvlc_store(win);
|
||||
+ store->DeleteItem(row);
|
||||
+ if (row < store->row_fg.size()) store->row_fg.erase(store->row_fg.begin() + row);
|
||||
+}
|
||||
+
|
||||
+void wxbr_dvlc_clear(void* win) { ((wxDataViewListCtrl*)win)->DeleteAllItems(); }
|
||||
+void wxbr_dvlc_clear(void* win) {
|
||||
+ AttrListStore* store = dvlc_store(win);
|
||||
+ store->DeleteAllItems();
|
||||
+ store->row_fg.clear();
|
||||
+}
|
||||
+
|
||||
+// Set (or clear, when r<0) a row's text colour; used to paint rows by status.
|
||||
+void wxbr_dvlc_set_row_color(void* win, unsigned row, int r, int g, int b) {
|
||||
+ AttrListStore* store = dvlc_store(win);
|
||||
+ if (row >= store->row_fg.size()) return;
|
||||
+ store->row_fg[row] = (r < 0) ? wxColour() : wxColour(r, g, b);
|
||||
+ store->RowChanged(row);
|
||||
+}
|
||||
+
|
||||
+int wxbr_dvlc_row_count(void* win) {
|
||||
+ return (int)((wxDataViewListCtrl*)win)->GetStore()->GetItemCount();
|
||||
+ return (int)dvlc_store(win)->GetItemCount();
|
||||
+}
|
||||
+
|
||||
+void wxbr_dvlc_set_cell(void* win, unsigned row, unsigned col, const char* value) {
|
||||
+ ((wxDataViewListCtrl*)win)
|
||||
+ ->SetValue(wxVariant(wxString::FromUTF8(value)), row, col);
|
||||
+ AttrListStore* store = dvlc_store(win);
|
||||
+ store->SetValueByRow(wxVariant(wxString::FromUTF8(value)), row, col);
|
||||
+ store->RowValueChanged(row, col);
|
||||
+}
|
||||
+
|
||||
+int wxbr_dvlc_get_cell(void* win, unsigned row, unsigned col, char* buf, int cap) {
|
||||
+ wxDataViewListCtrl* lc = (wxDataViewListCtrl*)win;
|
||||
+ wxDataViewListStore* store = lc->GetStore();
|
||||
+ if (row >= store->GetItemCount() || col >= lc->GetColumnCount())
|
||||
+ wxDataViewCtrl* dv = (wxDataViewCtrl*)win;
|
||||
+ AttrListStore* store = dvlc_store(win);
|
||||
+ if (row >= store->GetItemCount() || (int)col >= (int)dv->GetColumnCount())
|
||||
+ return 0;
|
||||
+ wxVariant v;
|
||||
+ store->GetValueByRow(v, row, col);
|
||||
@@ -369,7 +426,7 @@
|
||||
+}
|
||||
+
|
||||
+int wxbr_dvlc_col_count(void* win) {
|
||||
+ return (int)((wxDataViewListCtrl*)win)->GetColumnCount();
|
||||
+ return (int)((wxDataViewCtrl*)win)->GetColumnCount();
|
||||
+}
|
||||
+
|
||||
+// Update a column's header text in place (creation index) without recreating
|
||||
@@ -409,58 +466,51 @@
|
||||
+}
|
||||
+
|
||||
+long wxbr_dvlc_selected_row(void* win) {
|
||||
+ return (long)((wxDataViewListCtrl*)win)->GetSelectedRow();
|
||||
+ wxDataViewCtrl* dv = (wxDataViewCtrl*)win;
|
||||
+ wxDataViewItem it = dv->GetSelection();
|
||||
+ if (!it.IsOk()) return -1;
|
||||
+ return (long)((wxDataViewIndexListModel*)dv->GetModel())->GetRow(it);
|
||||
+}
|
||||
+
|
||||
+// Fill `buf` with the selected row indices (multi-select); returns the count.
|
||||
+int wxbr_dvlc_get_selections(void* win, int* buf, int cap) {
|
||||
+ wxDataViewListCtrl* lc = (wxDataViewListCtrl*)win;
|
||||
+ wxDataViewCtrl* dv = (wxDataViewCtrl*)win;
|
||||
+ wxDataViewIndexListModel* m = (wxDataViewIndexListModel*)dv->GetModel();
|
||||
+ wxDataViewItemArray items;
|
||||
+ int n = (int)lc->GetSelections(items);
|
||||
+ int n = (int)dv->GetSelections(items);
|
||||
+ int out = 0;
|
||||
+ for (int i = 0; i < n && out < cap; i++) {
|
||||
+ int row = lc->GetStore()->GetRow(items[i]);
|
||||
+ int row = m->GetRow(items[i]);
|
||||
+ if (row >= 0) buf[out++] = row;
|
||||
+ }
|
||||
+ return out;
|
||||
+}
|
||||
+
|
||||
+void wxbr_dvlc_select_rows(void* win, const int* rows, int n) {
|
||||
+ wxDataViewListCtrl* lc = (wxDataViewListCtrl*)win;
|
||||
+ lc->UnselectAll();
|
||||
+ wxDataViewCtrl* dv = (wxDataViewCtrl*)win;
|
||||
+ wxDataViewIndexListModel* m = (wxDataViewIndexListModel*)dv->GetModel();
|
||||
+ dv->UnselectAll();
|
||||
+ for (int i = 0; i < n; i++) {
|
||||
+ if (rows[i] >= 0) lc->SelectRow((unsigned)rows[i]);
|
||||
+ if (rows[i] >= 0) dv->Select(m->GetItem((unsigned)rows[i]));
|
||||
+ }
|
||||
+}
|
||||
+
|
||||
+// Retained for ABI; per-row colour is handled by the store (GetAttrByRow) and
|
||||
+// the native selection highlight, so these row-background hacks are no-ops now
|
||||
+// (they fought the selection highlight on GTK 3.3).
|
||||
+void wxbr_dvlc_set_alt_row_color(void* win, int r, int g, int b) {
|
||||
+ // No-op on GTK (native dataview lacks it); see wxbr_apply_zebra_css.
|
||||
+ ((wxDataViewListCtrl*)win)->SetAlternateRowColour(wxColour(r, g, b));
|
||||
+ (void)win; (void)r; (void)g; (void)b;
|
||||
+}
|
||||
+
|
||||
+// GTK's native dataview does not implement SetAlternateRowColour, so apply
|
||||
+// zebra striping app-wide via a CSS provider (GTK 3.20+ supports :nth-child on
|
||||
+// treeview rows). No-op off GTK.
|
||||
+void wxbr_apply_zebra_css(int r, int g, int b) {
|
||||
+#ifdef __WXGTK__
|
||||
+ GtkCssProvider* p = gtk_css_provider_new();
|
||||
+ char css[192];
|
||||
+ std::snprintf(css, sizeof(css),
|
||||
+ "treeview.view:nth-child(even){background-color:rgb(%d,%d,%d);}",
|
||||
+ r, g, b);
|
||||
+ gtk_css_provider_load_from_data(p, css, -1, NULL);
|
||||
+ gtk_style_context_add_provider_for_screen(
|
||||
+ gdk_screen_get_default(), GTK_STYLE_PROVIDER(p),
|
||||
+ GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
|
||||
+ g_object_unref(p);
|
||||
+#else
|
||||
+ (void)r; (void)g; (void)b;
|
||||
+#endif
|
||||
+}
|
||||
+
|
||||
+void wxbr_dvlc_select_row(void* win, long row) {
|
||||
+ if (row < 0) return;
|
||||
+ ((wxDataViewListCtrl*)win)->SelectRow((unsigned)row);
|
||||
+ wxDataViewCtrl* dv = (wxDataViewCtrl*)win;
|
||||
+ wxDataViewIndexListModel* m = (wxDataViewIndexListModel*)dv->GetModel();
|
||||
+ dv->Select(m->GetItem((unsigned)row));
|
||||
+}
|
||||
+
|
||||
+// ---- timer -----------------------------------------------------------------
|
||||
@@ -594,8 +644,102 @@
|
||||
+ delete m;
|
||||
+}
|
||||
+
|
||||
+// ---- choice (dropdown) ------------------------------------------------------
|
||||
+
|
||||
+void* wxbr_choice(void* parent, int id) {
|
||||
+ wxChoice* c = new wxChoice((wxWindow*)parent, id);
|
||||
+ c->Bind(wxEVT_CHOICE, [id](wxCommandEvent& e) {
|
||||
+ if (g_widget_event)
|
||||
+ g_widget_event(WxEV_CHOICE_CHANGED, id, (long)e.GetSelection());
|
||||
+ });
|
||||
+ return (void*)c;
|
||||
+}
|
||||
+
|
||||
+void wxbr_choice_append(void* win, const char* label) {
|
||||
+ ((wxChoice*)win)->Append(wxString::FromUTF8(label));
|
||||
+}
|
||||
+
|
||||
+void wxbr_choice_clear(void* win) { ((wxChoice*)win)->Clear(); }
|
||||
+
|
||||
+int wxbr_choice_get_selection(void* win) {
|
||||
+ return ((wxChoice*)win)->GetSelection();
|
||||
+}
|
||||
+
|
||||
+void wxbr_choice_set_selection(void* win, int i) {
|
||||
+ ((wxChoice*)win)->SetSelection(i);
|
||||
+}
|
||||
+
|
||||
+// ---- media (icon) button: play / pause / stop -------------------------------
|
||||
+
|
||||
+// kind: 0=play (triangle), 1=pause (two bars), 2=stop (square). Drawn in the
|
||||
+// given colour on a transparent bitmap so it reads as a coloured glyph on the
|
||||
+// native button. Emits WxEV_BUTTON_CLICKED with `id`, like a normal button.
|
||||
+void* wxbr_media_button(void* parent, int id, int kind, int r, int g, int b) {
|
||||
+ const int sz = 20;
|
||||
+ wxImage img(sz, sz);
|
||||
+ img.InitAlpha();
|
||||
+ unsigned char* a = img.GetAlpha();
|
||||
+ if (a) memset(a, 0, (size_t)sz * sz); // fully transparent
|
||||
+ wxBitmap bmp(img);
|
||||
+ {
|
||||
+ wxMemoryDC mdc(bmp);
|
||||
+ wxGraphicsContext* gc = wxGraphicsContext::Create(mdc);
|
||||
+ if (gc) {
|
||||
+ gc->SetAntialiasMode(wxANTIALIAS_DEFAULT);
|
||||
+ wxColour col(r, g, b);
|
||||
+ gc->SetBrush(wxBrush(col));
|
||||
+ gc->SetPen(wxPen(col, 1.0));
|
||||
+ if (kind == 0) {
|
||||
+ wxGraphicsPath p = gc->CreatePath();
|
||||
+ p.MoveToPoint(5, 4);
|
||||
+ p.AddLineToPoint(16, 10);
|
||||
+ p.AddLineToPoint(5, 16);
|
||||
+ p.CloseSubpath();
|
||||
+ gc->FillPath(p);
|
||||
+ } else if (kind == 1) {
|
||||
+ gc->DrawRectangle(5, 4, 3.5, 12);
|
||||
+ gc->DrawRectangle(11.5, 4, 3.5, 12);
|
||||
+ } else {
|
||||
+ gc->DrawRectangle(5, 5, 10, 10);
|
||||
+ }
|
||||
+ delete gc;
|
||||
+ }
|
||||
+ }
|
||||
+ wxBitmapButton* btn = new wxBitmapButton((wxWindow*)parent, id, bmp);
|
||||
+ btn->Bind(wxEVT_BUTTON, [id](wxCommandEvent&) {
|
||||
+ if (g_widget_event) g_widget_event(WxEV_BUTTON_CLICKED, id, 0);
|
||||
+ });
|
||||
+ return (void*)btn;
|
||||
+}
|
||||
+
|
||||
+// ---- live theme changes -----------------------------------------------------
|
||||
+
|
||||
+// Emit WxEV_SYS_COLOUR when the OS light/dark theme changes so the app can
|
||||
+// repaint custom-coloured widgets (KPI cards, table rows).
|
||||
+void wxbr_bind_sys_colour(void* win) {
|
||||
+ ((wxWindow*)win)->Bind(wxEVT_SYS_COLOUR_CHANGED, [](wxSysColourChangedEvent& e) {
|
||||
+ e.Skip();
|
||||
+ if (g_widget_event) g_widget_event(WxEV_SYS_COLOUR, 0, 0);
|
||||
+ });
|
||||
+}
|
||||
+
|
||||
+// Run a deferred action after the current event finishes unwinding: emits
|
||||
+// WxEV_DEFERRED with `id` from the main event loop. Used to rebuild a control
|
||||
+// from outside its own event handler (destroying a widget mid-event is unsafe).
|
||||
+void wxbr_call_after(int id) {
|
||||
+ if (wxTheApp) {
|
||||
+ wxTheApp->CallAfter([id]() {
|
||||
+ if (g_widget_event) g_widget_event(WxEV_DEFERRED, id, 0);
|
||||
+ });
|
||||
+ }
|
||||
+}
|
||||
+
|
||||
+// ---- deferred destroy (safe from event handlers) ----------------------------
|
||||
+
|
||||
+void wxbr_destroy(void* win) { ((wxWindow*)win)->Destroy(); }
|
||||
+
|
||||
+// Destroy a window's children immediately (used to rebuild a panel's dynamic
|
||||
+// contents in place, e.g. the filter dialog's per-column controls).
|
||||
+void wxbr_destroy_children(void* win) { ((wxWindow*)win)->DestroyChildren(); }
|
||||
+
|
||||
+} // extern "C"
|
||||
|
||||
+205
-61
@@ -20,10 +20,19 @@
|
||||
#include <wx/app.h>
|
||||
#include <wx/rearrangectrl.h>
|
||||
#include <wx/menu.h>
|
||||
#include <wx/choice.h>
|
||||
#include <wx/bmpbuttn.h>
|
||||
#include <wx/bitmap.h>
|
||||
#include <wx/image.h>
|
||||
#include <wx/dcmemory.h>
|
||||
#include <wx/dcgraph.h>
|
||||
#include <wx/graphics.h>
|
||||
#include <cstring>
|
||||
#ifdef __WXGTK__
|
||||
#include <gtk/gtk.h>
|
||||
#endif
|
||||
#include <cstdio>
|
||||
#include <vector>
|
||||
|
||||
typedef void (*WxWidgetEventFn)(int kind, long p1, long p2);
|
||||
static WxWidgetEventFn g_widget_event = nullptr;
|
||||
@@ -40,6 +49,27 @@ enum WxWidgetEventKind {
|
||||
WxEV_DVLC_SELECTION_CHANGED = 9,
|
||||
WxEV_DVLC_CONTEXT_MENU = 10,
|
||||
WxEV_MENU_ITEM = 11,
|
||||
WxEV_CHOICE_CHANGED = 12,
|
||||
WxEV_SYS_COLOUR = 13,
|
||||
WxEV_DEFERRED = 14,
|
||||
};
|
||||
|
||||
// A list store that carries an optional per-row foreground colour, surfaced to
|
||||
// the native control via GetAttrByRow. This is how the miner table paints each
|
||||
// row's text by status and separates rows on GTK (where SetAlternateRowColour
|
||||
// and :nth-child CSS are unreliable and fight the selection highlight).
|
||||
class AttrListStore : public wxDataViewListStore {
|
||||
public:
|
||||
std::vector<wxColour> row_fg;
|
||||
bool GetAttrByRow(unsigned int row, unsigned int col,
|
||||
wxDataViewItemAttr& attr) const override {
|
||||
(void)col;
|
||||
if (row < row_fg.size() && row_fg[row].IsOk()) {
|
||||
attr.SetColour(row_fg[row]);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
extern "C" {
|
||||
@@ -266,63 +296,73 @@ void wxbr_dialog_close(void* win, int code) {
|
||||
|
||||
// ---- data view list ctrl ------------------------------------------------------
|
||||
|
||||
static AttrListStore* dvlc_store(void* win) {
|
||||
return static_cast<AttrListStore*>(((wxDataViewCtrl*)win)->GetModel());
|
||||
}
|
||||
|
||||
void* wxbr_dvlc(void* parent) {
|
||||
// Multi-select (ctrl/shift-click) + row lines. Selection replaces the old
|
||||
// checkbox column.
|
||||
wxDataViewListCtrl* lc =
|
||||
new wxDataViewListCtrl((wxWindow*)parent, wxID_ANY, wxDefaultPosition,
|
||||
wxSize(-1, -1), wxDV_ROW_LINES | wxDV_MULTIPLE);
|
||||
lc->Bind(wxEVT_DATAVIEW_SELECTION_CHANGED, [](wxDataViewEvent&) {
|
||||
// checkbox column. A custom AttrListStore backs it so rows can be coloured by
|
||||
// status while the native selection highlight still shows through.
|
||||
wxDataViewCtrl* dv =
|
||||
new wxDataViewCtrl((wxWindow*)parent, wxID_ANY, wxDefaultPosition,
|
||||
wxSize(-1, -1), wxDV_ROW_LINES | wxDV_MULTIPLE);
|
||||
AttrListStore* store = new AttrListStore();
|
||||
dv->AssociateModel(store);
|
||||
store->DecRef(); // the control owns a reference now
|
||||
dv->Bind(wxEVT_DATAVIEW_SELECTION_CHANGED, [](wxDataViewEvent&) {
|
||||
if (g_widget_event) g_widget_event(WxEV_DVLC_SELECTION_CHANGED, 0, 0);
|
||||
});
|
||||
lc->Bind(wxEVT_DATAVIEW_ITEM_CONTEXT_MENU, [](wxDataViewEvent&) {
|
||||
dv->Bind(wxEVT_DATAVIEW_ITEM_CONTEXT_MENU, [](wxDataViewEvent&) {
|
||||
if (g_widget_event) g_widget_event(WxEV_DVLC_CONTEXT_MENU, 0, 0);
|
||||
});
|
||||
lc->Bind(wxEVT_DATAVIEW_ITEM_VALUE_CHANGED, [](wxDataViewEvent& e) {
|
||||
dv->Bind(wxEVT_DATAVIEW_ITEM_VALUE_CHANGED, [](wxDataViewEvent& e) {
|
||||
if (g_widget_event) {
|
||||
wxDataViewListCtrl* lc =
|
||||
dynamic_cast<wxDataViewListCtrl*>((wxWindow*)e.GetEventObject());
|
||||
if (!lc) return;
|
||||
wxDataViewCtrl* dv = (wxDataViewCtrl*)e.GetEventObject();
|
||||
if (!dv) return;
|
||||
wxDataViewItem item = e.GetItem();
|
||||
if (!item.IsOk()) return;
|
||||
int row = lc->GetStore()->GetRow(item);
|
||||
int row = ((wxDataViewIndexListModel*)dv->GetModel())->GetRow(item);
|
||||
g_widget_event(WxEV_DVLC_VALUE_CHANGED, row, (long)e.GetColumn());
|
||||
}
|
||||
});
|
||||
lc->Bind(wxEVT_DATAVIEW_COLUMN_HEADER_CLICK, [](wxDataViewEvent& e) {
|
||||
dv->Bind(wxEVT_DATAVIEW_COLUMN_HEADER_CLICK, [](wxDataViewEvent& e) {
|
||||
if (g_widget_event) g_widget_event(WxEV_DVLC_HEADER_CLICK, (long)e.GetColumn(), 0);
|
||||
});
|
||||
lc->Bind(wxEVT_DATAVIEW_COLUMN_REORDERED, [](wxDataViewEvent&) {
|
||||
dv->Bind(wxEVT_DATAVIEW_COLUMN_REORDERED, [](wxDataViewEvent&) {
|
||||
if (g_widget_event) g_widget_event(WxEV_DVLC_REORDERED, 0, 0);
|
||||
});
|
||||
// Row activation (double-click / Enter): report the row + column so the app
|
||||
// can, e.g., open the miner's IP in a browser.
|
||||
lc->Bind(wxEVT_DATAVIEW_ITEM_ACTIVATED, [](wxDataViewEvent& e) {
|
||||
dv->Bind(wxEVT_DATAVIEW_ITEM_ACTIVATED, [](wxDataViewEvent& e) {
|
||||
if (g_widget_event) {
|
||||
wxDataViewListCtrl* lc =
|
||||
dynamic_cast<wxDataViewListCtrl*>((wxWindow*)e.GetEventObject());
|
||||
if (!lc) return;
|
||||
wxDataViewCtrl* dv = (wxDataViewCtrl*)e.GetEventObject();
|
||||
if (!dv) return;
|
||||
wxDataViewItem item = e.GetItem();
|
||||
if (!item.IsOk()) return;
|
||||
int row = lc->GetStore()->GetRow(item);
|
||||
int row = ((wxDataViewIndexListModel*)dv->GetModel())->GetRow(item);
|
||||
g_widget_event(WxEV_DVLC_ACTIVATED, row, (long)e.GetColumn());
|
||||
}
|
||||
});
|
||||
return (void*)lc;
|
||||
return (void*)dv;
|
||||
}
|
||||
|
||||
void wxbr_dvlc_append_text_col(void* win, const char* title, int width, int reorderable) {
|
||||
wxDataViewCtrl* dv = (wxDataViewCtrl*)win;
|
||||
int flags = wxDATAVIEW_COL_RESIZABLE;
|
||||
if (reorderable) flags |= wxDATAVIEW_COL_REORDERABLE;
|
||||
((wxDataViewListCtrl*)win)
|
||||
->AppendTextColumn(wxString::FromUTF8(title), wxDATAVIEW_CELL_INERT,
|
||||
width, wxALIGN_NOT, flags);
|
||||
unsigned col = dv->GetColumnCount(); // next model column index
|
||||
dvlc_store(win)->AppendColumn("string");
|
||||
dv->AppendTextColumn(wxString::FromUTF8(title), col, wxDATAVIEW_CELL_INERT,
|
||||
width, wxALIGN_NOT, flags);
|
||||
}
|
||||
|
||||
void wxbr_dvlc_append_toggle_col(void* win, int width) {
|
||||
((wxDataViewListCtrl*)win)
|
||||
->AppendToggleColumn(wxT(""), wxDATAVIEW_CELL_ACTIVATABLE, width,
|
||||
wxALIGN_CENTRE, 0);
|
||||
wxDataViewCtrl* dv = (wxDataViewCtrl*)win;
|
||||
unsigned col = dv->GetColumnCount();
|
||||
dvlc_store(win)->AppendColumn("bool");
|
||||
dv->AppendToggleColumn(wxT(""), col, wxDATAVIEW_CELL_ACTIVATABLE, width,
|
||||
wxALIGN_CENTRE, 0);
|
||||
}
|
||||
|
||||
void wxbr_dvlc_append_row(void* win, int nvals, const char** vals) {
|
||||
@@ -331,28 +371,45 @@ void wxbr_dvlc_append_row(void* win, int nvals, const char** vals) {
|
||||
for (int i = 0; i < nvals; i++) {
|
||||
values.push_back(wxVariant(vals[i] ? wxString::FromUTF8(vals[i]) : wxString()));
|
||||
}
|
||||
((wxDataViewListCtrl*)win)->AppendItem(values);
|
||||
AttrListStore* store = dvlc_store(win);
|
||||
store->AppendItem(values);
|
||||
store->row_fg.push_back(wxColour()); // no per-row colour until set
|
||||
}
|
||||
|
||||
void wxbr_dvlc_delete_row(void* win, unsigned row) {
|
||||
((wxDataViewListCtrl*)win)->DeleteItem(row);
|
||||
AttrListStore* store = dvlc_store(win);
|
||||
store->DeleteItem(row);
|
||||
if (row < store->row_fg.size()) store->row_fg.erase(store->row_fg.begin() + row);
|
||||
}
|
||||
|
||||
void wxbr_dvlc_clear(void* win) { ((wxDataViewListCtrl*)win)->DeleteAllItems(); }
|
||||
void wxbr_dvlc_clear(void* win) {
|
||||
AttrListStore* store = dvlc_store(win);
|
||||
store->DeleteAllItems();
|
||||
store->row_fg.clear();
|
||||
}
|
||||
|
||||
// Set (or clear, when r<0) a row's text colour; used to paint rows by status.
|
||||
void wxbr_dvlc_set_row_color(void* win, unsigned row, int r, int g, int b) {
|
||||
AttrListStore* store = dvlc_store(win);
|
||||
if (row >= store->row_fg.size()) return;
|
||||
store->row_fg[row] = (r < 0) ? wxColour() : wxColour(r, g, b);
|
||||
store->RowChanged(row);
|
||||
}
|
||||
|
||||
int wxbr_dvlc_row_count(void* win) {
|
||||
return (int)((wxDataViewListCtrl*)win)->GetStore()->GetItemCount();
|
||||
return (int)dvlc_store(win)->GetItemCount();
|
||||
}
|
||||
|
||||
void wxbr_dvlc_set_cell(void* win, unsigned row, unsigned col, const char* value) {
|
||||
((wxDataViewListCtrl*)win)
|
||||
->SetValue(wxVariant(wxString::FromUTF8(value)), row, col);
|
||||
AttrListStore* store = dvlc_store(win);
|
||||
store->SetValueByRow(wxVariant(wxString::FromUTF8(value)), row, col);
|
||||
store->RowValueChanged(row, col);
|
||||
}
|
||||
|
||||
int wxbr_dvlc_get_cell(void* win, unsigned row, unsigned col, char* buf, int cap) {
|
||||
wxDataViewListCtrl* lc = (wxDataViewListCtrl*)win;
|
||||
wxDataViewListStore* store = lc->GetStore();
|
||||
if (row >= store->GetItemCount() || col >= lc->GetColumnCount())
|
||||
wxDataViewCtrl* dv = (wxDataViewCtrl*)win;
|
||||
AttrListStore* store = dvlc_store(win);
|
||||
if (row >= store->GetItemCount() || (int)col >= (int)dv->GetColumnCount())
|
||||
return 0;
|
||||
wxVariant v;
|
||||
store->GetValueByRow(v, row, col);
|
||||
@@ -366,7 +423,7 @@ int wxbr_dvlc_get_cell(void* win, unsigned row, unsigned col, char* buf, int cap
|
||||
}
|
||||
|
||||
int wxbr_dvlc_col_count(void* win) {
|
||||
return (int)((wxDataViewListCtrl*)win)->GetColumnCount();
|
||||
return (int)((wxDataViewCtrl*)win)->GetColumnCount();
|
||||
}
|
||||
|
||||
// Update a column's header text in place (creation index) without recreating
|
||||
@@ -406,58 +463,51 @@ int wxbr_dvlc_col_at_pos(void* win, int pos) {
|
||||
}
|
||||
|
||||
long wxbr_dvlc_selected_row(void* win) {
|
||||
return (long)((wxDataViewListCtrl*)win)->GetSelectedRow();
|
||||
wxDataViewCtrl* dv = (wxDataViewCtrl*)win;
|
||||
wxDataViewItem it = dv->GetSelection();
|
||||
if (!it.IsOk()) return -1;
|
||||
return (long)((wxDataViewIndexListModel*)dv->GetModel())->GetRow(it);
|
||||
}
|
||||
|
||||
// Fill `buf` with the selected row indices (multi-select); returns the count.
|
||||
int wxbr_dvlc_get_selections(void* win, int* buf, int cap) {
|
||||
wxDataViewListCtrl* lc = (wxDataViewListCtrl*)win;
|
||||
wxDataViewCtrl* dv = (wxDataViewCtrl*)win;
|
||||
wxDataViewIndexListModel* m = (wxDataViewIndexListModel*)dv->GetModel();
|
||||
wxDataViewItemArray items;
|
||||
int n = (int)lc->GetSelections(items);
|
||||
int n = (int)dv->GetSelections(items);
|
||||
int out = 0;
|
||||
for (int i = 0; i < n && out < cap; i++) {
|
||||
int row = lc->GetStore()->GetRow(items[i]);
|
||||
int row = m->GetRow(items[i]);
|
||||
if (row >= 0) buf[out++] = row;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
void wxbr_dvlc_select_rows(void* win, const int* rows, int n) {
|
||||
wxDataViewListCtrl* lc = (wxDataViewListCtrl*)win;
|
||||
lc->UnselectAll();
|
||||
wxDataViewCtrl* dv = (wxDataViewCtrl*)win;
|
||||
wxDataViewIndexListModel* m = (wxDataViewIndexListModel*)dv->GetModel();
|
||||
dv->UnselectAll();
|
||||
for (int i = 0; i < n; i++) {
|
||||
if (rows[i] >= 0) lc->SelectRow((unsigned)rows[i]);
|
||||
if (rows[i] >= 0) dv->Select(m->GetItem((unsigned)rows[i]));
|
||||
}
|
||||
}
|
||||
|
||||
// Retained for ABI; per-row colour is handled by the store (GetAttrByRow) and
|
||||
// the native selection highlight, so these row-background hacks are no-ops now
|
||||
// (they fought the selection highlight on GTK 3.3).
|
||||
void wxbr_dvlc_set_alt_row_color(void* win, int r, int g, int b) {
|
||||
// No-op on GTK (native dataview lacks it); see wxbr_apply_zebra_css.
|
||||
((wxDataViewListCtrl*)win)->SetAlternateRowColour(wxColour(r, g, b));
|
||||
(void)win; (void)r; (void)g; (void)b;
|
||||
}
|
||||
|
||||
// GTK's native dataview does not implement SetAlternateRowColour, so apply
|
||||
// zebra striping app-wide via a CSS provider (GTK 3.20+ supports :nth-child on
|
||||
// treeview rows). No-op off GTK.
|
||||
void wxbr_apply_zebra_css(int r, int g, int b) {
|
||||
#ifdef __WXGTK__
|
||||
GtkCssProvider* p = gtk_css_provider_new();
|
||||
char css[192];
|
||||
std::snprintf(css, sizeof(css),
|
||||
"treeview.view:nth-child(even){background-color:rgb(%d,%d,%d);}",
|
||||
r, g, b);
|
||||
gtk_css_provider_load_from_data(p, css, -1, NULL);
|
||||
gtk_style_context_add_provider_for_screen(
|
||||
gdk_screen_get_default(), GTK_STYLE_PROVIDER(p),
|
||||
GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
|
||||
g_object_unref(p);
|
||||
#else
|
||||
(void)r; (void)g; (void)b;
|
||||
#endif
|
||||
}
|
||||
|
||||
void wxbr_dvlc_select_row(void* win, long row) {
|
||||
if (row < 0) return;
|
||||
((wxDataViewListCtrl*)win)->SelectRow((unsigned)row);
|
||||
wxDataViewCtrl* dv = (wxDataViewCtrl*)win;
|
||||
wxDataViewIndexListModel* m = (wxDataViewIndexListModel*)dv->GetModel();
|
||||
dv->Select(m->GetItem((unsigned)row));
|
||||
}
|
||||
|
||||
// ---- timer -----------------------------------------------------------------
|
||||
@@ -591,8 +641,102 @@ void wxbr_popup_menu(void* win, void* menu) {
|
||||
delete m;
|
||||
}
|
||||
|
||||
// ---- choice (dropdown) ------------------------------------------------------
|
||||
|
||||
void* wxbr_choice(void* parent, int id) {
|
||||
wxChoice* c = new wxChoice((wxWindow*)parent, id);
|
||||
c->Bind(wxEVT_CHOICE, [id](wxCommandEvent& e) {
|
||||
if (g_widget_event)
|
||||
g_widget_event(WxEV_CHOICE_CHANGED, id, (long)e.GetSelection());
|
||||
});
|
||||
return (void*)c;
|
||||
}
|
||||
|
||||
void wxbr_choice_append(void* win, const char* label) {
|
||||
((wxChoice*)win)->Append(wxString::FromUTF8(label));
|
||||
}
|
||||
|
||||
void wxbr_choice_clear(void* win) { ((wxChoice*)win)->Clear(); }
|
||||
|
||||
int wxbr_choice_get_selection(void* win) {
|
||||
return ((wxChoice*)win)->GetSelection();
|
||||
}
|
||||
|
||||
void wxbr_choice_set_selection(void* win, int i) {
|
||||
((wxChoice*)win)->SetSelection(i);
|
||||
}
|
||||
|
||||
// ---- media (icon) button: play / pause / stop -------------------------------
|
||||
|
||||
// kind: 0=play (triangle), 1=pause (two bars), 2=stop (square). Drawn in the
|
||||
// given colour on a transparent bitmap so it reads as a coloured glyph on the
|
||||
// native button. Emits WxEV_BUTTON_CLICKED with `id`, like a normal button.
|
||||
void* wxbr_media_button(void* parent, int id, int kind, int r, int g, int b) {
|
||||
const int sz = 20;
|
||||
wxImage img(sz, sz);
|
||||
img.InitAlpha();
|
||||
unsigned char* a = img.GetAlpha();
|
||||
if (a) memset(a, 0, (size_t)sz * sz); // fully transparent
|
||||
wxBitmap bmp(img);
|
||||
{
|
||||
wxMemoryDC mdc(bmp);
|
||||
wxGraphicsContext* gc = wxGraphicsContext::Create(mdc);
|
||||
if (gc) {
|
||||
gc->SetAntialiasMode(wxANTIALIAS_DEFAULT);
|
||||
wxColour col(r, g, b);
|
||||
gc->SetBrush(wxBrush(col));
|
||||
gc->SetPen(wxPen(col, 1.0));
|
||||
if (kind == 0) {
|
||||
wxGraphicsPath p = gc->CreatePath();
|
||||
p.MoveToPoint(5, 4);
|
||||
p.AddLineToPoint(16, 10);
|
||||
p.AddLineToPoint(5, 16);
|
||||
p.CloseSubpath();
|
||||
gc->FillPath(p);
|
||||
} else if (kind == 1) {
|
||||
gc->DrawRectangle(5, 4, 3.5, 12);
|
||||
gc->DrawRectangle(11.5, 4, 3.5, 12);
|
||||
} else {
|
||||
gc->DrawRectangle(5, 5, 10, 10);
|
||||
}
|
||||
delete gc;
|
||||
}
|
||||
}
|
||||
wxBitmapButton* btn = new wxBitmapButton((wxWindow*)parent, id, bmp);
|
||||
btn->Bind(wxEVT_BUTTON, [id](wxCommandEvent&) {
|
||||
if (g_widget_event) g_widget_event(WxEV_BUTTON_CLICKED, id, 0);
|
||||
});
|
||||
return (void*)btn;
|
||||
}
|
||||
|
||||
// ---- live theme changes -----------------------------------------------------
|
||||
|
||||
// Emit WxEV_SYS_COLOUR when the OS light/dark theme changes so the app can
|
||||
// repaint custom-coloured widgets (KPI cards, table rows).
|
||||
void wxbr_bind_sys_colour(void* win) {
|
||||
((wxWindow*)win)->Bind(wxEVT_SYS_COLOUR_CHANGED, [](wxSysColourChangedEvent& e) {
|
||||
e.Skip();
|
||||
if (g_widget_event) g_widget_event(WxEV_SYS_COLOUR, 0, 0);
|
||||
});
|
||||
}
|
||||
|
||||
// Run a deferred action after the current event finishes unwinding: emits
|
||||
// WxEV_DEFERRED with `id` from the main event loop. Used to rebuild a control
|
||||
// from outside its own event handler (destroying a widget mid-event is unsafe).
|
||||
void wxbr_call_after(int id) {
|
||||
if (wxTheApp) {
|
||||
wxTheApp->CallAfter([id]() {
|
||||
if (g_widget_event) g_widget_event(WxEV_DEFERRED, id, 0);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// ---- deferred destroy (safe from event handlers) ----------------------------
|
||||
|
||||
void wxbr_destroy(void* win) { ((wxWindow*)win)->Destroy(); }
|
||||
|
||||
// Destroy a window's children immediately (used to rebuild a panel's dynamic
|
||||
// contents in place, e.g. the filter dialog's per-column controls).
|
||||
void wxbr_destroy_children(void* win) { ((wxWindow*)win)->DestroyChildren(); }
|
||||
|
||||
} // extern "C"
|
||||
|
||||
@@ -64,6 +64,7 @@ mod notsafe {
|
||||
pub fn wxbr_dvlc_get_selections(win: *mut c_void, buf: *mut c_int, cap: c_int) -> c_int;
|
||||
pub fn wxbr_dvlc_select_rows(win: *mut c_void, rows: *const c_int, n: c_int);
|
||||
pub fn wxbr_dvlc_set_alt_row_color(win: *mut c_void, r: c_int, g: c_int, b: c_int);
|
||||
pub fn wxbr_dvlc_set_row_color(win: *mut c_void, row: usize, r: c_int, g: c_int, b: c_int);
|
||||
pub fn wxbr_apply_zebra_css(r: c_int, g: c_int, b: c_int);
|
||||
pub fn wxbr_rearrange_list(parent: *mut c_void, n: c_int, order: *const c_int, items: *const *const c_char) -> *mut c_void;
|
||||
pub fn wxbr_rearrange_get_order(win: *mut c_void, buf: *mut c_int, cap: c_int) -> c_int;
|
||||
@@ -83,7 +84,16 @@ mod notsafe {
|
||||
pub fn wxbr_system_color(which: c_int, r: *mut u8, g: *mut u8, b: *mut u8);
|
||||
pub fn wxbr_set_bg(win: *mut c_void, r: c_int, g: c_int, b: c_int);
|
||||
pub fn wxbr_set_fg(win: *mut c_void, r: c_int, g: c_int, b: c_int);
|
||||
pub fn wxbr_choice(parent: *mut c_void, id: c_int) -> *mut c_void;
|
||||
pub fn wxbr_choice_append(win: *mut c_void, label: *const c_char);
|
||||
pub fn wxbr_choice_clear(win: *mut c_void);
|
||||
pub fn wxbr_choice_get_selection(win: *mut c_void) -> c_int;
|
||||
pub fn wxbr_choice_set_selection(win: *mut c_void, i: c_int);
|
||||
pub fn wxbr_media_button(parent: *mut c_void, id: c_int, kind: c_int, r: c_int, g: c_int, b: c_int) -> *mut c_void;
|
||||
pub fn wxbr_bind_sys_colour(win: *mut c_void);
|
||||
pub fn wxbr_destroy(win: *mut c_void);
|
||||
pub fn wxbr_destroy_children(win: *mut c_void);
|
||||
pub fn wxbr_call_after(id: c_int);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -116,6 +126,9 @@ pub enum WxEventKind {
|
||||
DvlcSelectionChanged,
|
||||
DvlcContextMenu,
|
||||
MenuItem,
|
||||
ChoiceChanged,
|
||||
SysColour,
|
||||
Deferred,
|
||||
}
|
||||
|
||||
impl WxEventKind {
|
||||
@@ -131,6 +144,9 @@ impl WxEventKind {
|
||||
9 => WxEventKind::DvlcSelectionChanged,
|
||||
10 => WxEventKind::DvlcContextMenu,
|
||||
11 => WxEventKind::MenuItem,
|
||||
12 => WxEventKind::ChoiceChanged,
|
||||
13 => WxEventKind::SysColour,
|
||||
14 => WxEventKind::Deferred,
|
||||
_ => return None,
|
||||
})
|
||||
}
|
||||
@@ -141,6 +157,13 @@ pub fn apply_zebra_css(r: u8, g: u8, b: u8) {
|
||||
unsafe { notsafe::wxbr_apply_zebra_css(r as c_int, g as c_int, b as c_int) }
|
||||
}
|
||||
|
||||
/// Schedule a deferred action: a `Deferred` event carrying `id` is delivered
|
||||
/// from the event loop after the current event finishes. Use this to rebuild or
|
||||
/// destroy a control from outside its own event handler.
|
||||
pub fn call_after(id: i32) {
|
||||
unsafe { notsafe::wxbr_call_after(id as c_int) }
|
||||
}
|
||||
|
||||
/// Open a URL in the system default browser. Returns true on success.
|
||||
pub fn launch_browser(url: &str) -> bool {
|
||||
let c = CString::new(url).unwrap_or_default();
|
||||
@@ -459,6 +482,15 @@ impl Widget {
|
||||
unsafe { notsafe::wxbr_dvlc_set_alt_row_color(self.0, r as c_int, g as c_int, b as c_int) }
|
||||
}
|
||||
|
||||
/// Set a row's text colour (e.g. by miner status). Pass `None` to clear it.
|
||||
pub fn dvlc_set_row_color(&self, row: usize, color: Option<(u8, u8, u8)>) {
|
||||
let (r, g, b) = match color {
|
||||
Some((r, g, b)) => (r as c_int, g as c_int, b as c_int),
|
||||
None => (-1, -1, -1),
|
||||
};
|
||||
unsafe { notsafe::wxbr_dvlc_set_row_color(self.0, row, r, g, b) }
|
||||
}
|
||||
|
||||
// ---- rearrangeable check-list ----
|
||||
|
||||
/// Create a rearrangeable checklist. `order` uses wx encoding: a value `v`
|
||||
@@ -553,9 +585,60 @@ impl Widget {
|
||||
unsafe { notsafe::wxbr_set_fg(self.0, r as c_int, g as c_int, b as c_int) }
|
||||
}
|
||||
|
||||
// ---- choice (dropdown) ----
|
||||
|
||||
/// Create a dropdown; selection changes emit `ChoiceChanged` with this id.
|
||||
pub fn choice(parent: Widget, id: i32) -> Widget {
|
||||
Widget(unsafe { notsafe::wxbr_choice(parent.0, id) })
|
||||
}
|
||||
|
||||
pub fn choice_append(&self, label: &str) {
|
||||
let c = CString::new(label).unwrap_or_default();
|
||||
unsafe { notsafe::wxbr_choice_append(self.0, c.as_ptr()) }
|
||||
}
|
||||
|
||||
pub fn choice_clear(&self) {
|
||||
unsafe { notsafe::wxbr_choice_clear(self.0) }
|
||||
}
|
||||
|
||||
pub fn choice_get_selection(&self) -> i32 {
|
||||
unsafe { notsafe::wxbr_choice_get_selection(self.0) }
|
||||
}
|
||||
|
||||
pub fn choice_set_selection(&self, i: i32) {
|
||||
unsafe { notsafe::wxbr_choice_set_selection(self.0, i) }
|
||||
}
|
||||
|
||||
// ---- media (icon) button ----
|
||||
|
||||
/// A coloured icon button: `kind` 0=play, 1=pause, 2=stop. Clicks emit
|
||||
/// `ButtonClicked` with `id`, like a normal button.
|
||||
pub fn media_button(parent: Widget, id: i32, kind: i32, color: (u8, u8, u8)) -> Widget {
|
||||
Widget(unsafe {
|
||||
notsafe::wxbr_media_button(
|
||||
parent.0,
|
||||
id,
|
||||
kind,
|
||||
color.0 as c_int,
|
||||
color.1 as c_int,
|
||||
color.2 as c_int,
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
/// Listen for OS light/dark theme changes on this window (emits `SysColour`).
|
||||
pub fn bind_sys_colour(&self) {
|
||||
unsafe { notsafe::wxbr_bind_sys_colour(self.0) }
|
||||
}
|
||||
|
||||
/// Deferred destruction (safe from event handlers); used to rebuild
|
||||
/// the table when its column set changes.
|
||||
pub fn destroy(&self) {
|
||||
unsafe { notsafe::wxbr_destroy(self.0) }
|
||||
}
|
||||
|
||||
/// Destroy this window's children (to rebuild dynamic panel contents).
|
||||
pub fn destroy_children(&self) {
|
||||
unsafe { notsafe::wxbr_destroy_children(self.0) }
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user