Skip to main content

ek_ui/
state.rs

1//! 共享观测状态:`UiPlugin`(总线面)写、`render_body`(窗口面)读。
2//!
3//! 线程形貌:总线侧在**插件 worker 线程**写;窗口侧在 **eframe 主线程**读;
4//! 锁粒度 = 每次 `lock().clone()/snapshot()`,不跨帧持锁。
5
6use std::collections::{BTreeMap, VecDeque};
7use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
8use std::sync::{Arc, Mutex};
9
10use ek_data::bar::BarData;
11use ek_dm::wire::BarsFrame;
12use ek_station::wire::PlugRow;
13
14/// 日志环形容量(S1b 门面;溢出不丢累计计数,见 [`LogRing::spilled`])。
15pub const RING_CAP: usize = 512;
16
17/// 环形日志:push 满则挤掉最旧一条,溢出计数单调(页面显示"已丢弃 N 条")。
18#[derive(Debug)]
19pub struct LogRing {
20    lines: VecDeque<String>,
21    cap: usize,
22    spilled: u64,
23}
24
25impl Default for LogRing {
26    fn default() -> Self {
27        Self::new(RING_CAP)
28    }
29}
30
31impl LogRing {
32    /// 定容环形(测试可传小容量逼溢出)。
33    pub fn new(cap: usize) -> Self {
34        Self {
35            lines: VecDeque::with_capacity(cap),
36            cap,
37            spilled: 0,
38        }
39    }
40
41    /// 追加一行;满则挤旧 + spilled+1。
42    pub fn push(&mut self, line: impl Into<String>) {
43        if self.cap == 0 {
44            self.spilled += 1;
45            return;
46        }
47        if self.lines.len() == self.cap {
48            self.lines.pop_front();
49            self.spilled += 1;
50        }
51        self.lines.push_back(line.into());
52    }
53
54    /// 当前长度。
55    pub fn len(&self) -> usize {
56        self.lines.len()
57    }
58
59    /// 是否空行。
60    pub fn is_empty(&self) -> bool {
61        self.lines.is_empty()
62    }
63
64    /// 累计已丢弃条数(只有溢出才涨)。
65    pub fn spilled(&self) -> u64 {
66        self.spilled
67    }
68
69    /// 只读快照(渲染用;不消费)。
70    pub fn snapshot(&self) -> Vec<String> {
71        self.lines.iter().cloned().collect()
72    }
73
74    /// 全清;spilled 保留(历史溢出事实不抹)。
75    pub fn clear(&mut self) {
76        self.lines.clear();
77    }
78}
79
80/// 一次 eTimer 拉取的服务/插件快照(渲染直接可用)。
81#[derive(Debug, Clone, Default)]
82pub struct BusSnap {
83    /// svctl.list 全行(svctl 独裁班;状态原文)。
84    pub rows: Vec<PlugRow>,
85    /// 逐插件服务目录(id 清单;无服务为空)。
86    pub services: Vec<(String, Vec<String>)>,
87}
88
89/// UI 操作指令(窗口面 → 总线面;eTimer 节拍被 ui 插件请辞)。
90#[derive(Debug, Clone, PartialEq)]
91pub enum UiCmd {
92    /// svctl.start(工厂名;v1 与插件注册名同,BD-U5)。
93    Start { factory: String },
94    /// svctl.remove(插件名)。
95    Remove { name: String },
96    /// dm.reload 回灌(S4;push=true 逐条广播,行情面板实时见 Rows)。
97    Reload {
98        interval: i64,
99        symbol: String,
100        bars: Vec<BarData>,
101    },
102    /// sim.send_order 下单(S5;纸面网关受理即 SUBMITTING 单上总线)。
103    /// S6 起网关可选(`gateway.send_order`,空 = sim)。
104    SendOrder {
105        req: ek_data::order_request::OrderRequest,
106        gateway: String,
107    },
108    /// sim.cancel_order 撤单(S5;携三段号所需的 orderid;S6 起网关同选)。
109    CancelOrder {
110        orderid: String,
111        symbol: String,
112        exchange: ek_data::constant::Exchange,
113        gateway: String,
114    },
115    /// sim.query_rate 流控查询(S5;回执进日志)。
116    QueryRate,
117    /// ctp.configure 网关配置(S6 测试面;界面手工输入表单直映,插件侧校验;
118    /// 出队时 `to_configure_req` 抬成线上契约)。
119    CtpConfigure(CtpForm),
120    /// ctp.subscribe 订阅行情(S6 测试面;逗号分隔标的)。
121    CtpSubscribe { symbols: Vec<String> },
122}
123
124/// 行情缓存单格容量(防涨;丢头保尾,bar 流单调)。
125pub const KLINE_CELL_CAP: usize = 4096;
126
127/// 二次确认步进:Armed = 首点待确认;Fired = 同名二点已发令;Switched = 目标迁移。
128#[derive(Debug, Clone, Copy, PartialEq, Eq)]
129pub enum ConfirmStep {
130    Armed,
131    Fired,
132    Switched,
133}
134
135/// 隔离态判定(svctl.list 状态原文含 Quarantin → 红;ePlugQDown.<名> 是定向
136/// 广播,未知名无法预订阅 —— 快照行扫描是唯一闭环面,BD-UI-17)。
137#[must_use]
138pub fn is_quarantined(state: &str) -> bool {
139    state.contains("Quarantin")
140}
141
142// ================= S4:行情库(eBar./eBarUpdate. 上屏地面) =================
143
144/// 单格 = (symbol, interval) → bar 升序序列(丢头保尾;datetime 单调)。
145#[derive(Debug, Default)]
146pub struct MarketStore {
147    cells: Mutex<BTreeMap<(String, i64), Vec<BarData>>>,
148    /// eBar. 帧接收计数(帧统计,刷新不丢帧观感面)。
149    frames: AtomicU64,
150    /// eBarUpdate. 接收计数。
151    updates: AtomicU64,
152    /// dm.reload 成功计数(回灌确认面)。
153    reloads: AtomicU64,
154}
155
156/// 一格的可读视图(渲染/测试用)。
157#[derive(Debug, Clone, PartialEq)]
158pub struct CellView {
159    pub symbol: String,
160    pub interval: i64,
161    pub bars: Vec<BarData>,
162}
163
164impl MarketStore {
165    /// eBar. 收帧:逐 bar 并格(同 dt 替尾;更旧丢弃;更晚追加,溢出丢头)。
166    pub fn on_frame(&self, frame: &BarsFrame) {
167        let mut cells = self.cells.lock().unwrap();
168        for b in &frame.bars {
169            let key = (b.symbol.clone(), frame.interval);
170            let cell = cells.entry(key).or_default();
171            match cell.last() {
172                Some(last) if b.datetime < last.datetime => continue,
173                Some(last) if b.datetime == last.datetime => {
174                    let slot = cell.last_mut().expect("len>1");
175                    *slot = b.clone();
176                }
177                _ => {
178                    cell.push(b.clone());
179                    if cell.len() > KLINE_CELL_CAP {
180                        cell.remove(0);
181                    }
182                }
183            }
184        }
185        self.frames.fetch_add(1, Ordering::AcqRel);
186    }
187
188    /// eBarUpdate. 收单 bar 快照(实时门;同 dt 替尾,新 dt 追加 = in-flight bar)。
189    pub fn on_update(&self, b: &BarData) {
190        let mut cells = self.cells.lock().unwrap();
191        let iv = b
192            .interval
193            .unwrap_or(ek_data::constant::Interval::MINUTE)
194            .seconds();
195        let key = (b.symbol.clone(), iv);
196        let cell = cells.entry(key).or_default();
197        match cell.last() {
198            Some(last) if b.datetime == last.datetime => {
199                let slot = cell.last_mut().expect("len>1");
200                *slot = b.clone();
201            }
202            Some(last) if b.datetime < last.datetime => {}
203            _ => {
204                cell.push(b.clone());
205                if cell.len() > KLINE_CELL_CAP {
206                    cell.remove(0);
207                }
208            }
209        }
210        self.updates.fetch_add(1, Ordering::AcqRel);
211    }
212
213    /// dm.reload 回灌登记(RELOAD OK 计数;dm 侧行为独立)。
214    pub fn note_reload(&self) {
215        self.reloads.fetch_add(1, Ordering::AcqRel);
216    }
217
218    /// 全格只读快照(键序 = BTreeMap 序,symbol/interval 稳定)。
219    pub fn cells(&self) -> Vec<CellView> {
220        self.cells
221            .lock()
222            .unwrap()
223            .iter()
224            .map(|((sym, iv), bars)| CellView {
225                symbol: sym.clone(),
226                interval: *iv,
227                bars: bars.clone(),
228            })
229            .collect()
230    }
231
232    /// 计算(Ai) 帧统计。
233    pub fn counts(&self) -> (u64, u64, u64) {
234        (
235            self.frames.load(Ordering::Acquire),
236            self.updates.load(Ordering::Acquire),
237            self.reloads.load(Ordering::Acquire),
238        )
239    }
240}
241
242/// 回灌表单(v4:界面面已退役,总线口 `UiCmd::Reload` 保留;解析器留作
243/// 测试与脚本面)。
244#[derive(Debug, Default, Clone, PartialEq)]
245pub struct ReloadForm {
246    pub interval: String,
247    pub symbol: String,
248    pub csv: String,
249}
250
251/// 票据网关选择子解析(空 → "sim";含空白 = 错)。
252pub fn parse_gateway(raw: &str) -> Result<String, String> {
253    let g = raw.trim();
254    if g.is_empty() {
255        return Ok("sim".to_string());
256    }
257    if g.chars().any(|c| c.is_whitespace()) {
258        return Err(format!("网关解析: 含空白:{raw:?}"));
259    }
260    Ok(g.to_string())
261}
262
263/// CTP 网关配置表单(S6 测试面;界面手工输入源 → UiCmd::CtpConfigure →
264/// ask `ctp.configure`。零配置口径:界面不读文件,值全部人敲;密码位掩码)。
265#[derive(Debug, Default, Clone, PartialEq)]
266pub struct CtpForm {
267    pub broker_id: String,
268    pub td_addr: String,
269    pub td_user: String,
270    pub td_password: String,
271    pub td_auth_code: String,
272    pub td_app_id: String,
273    pub md_addr: String,
274    pub md_user: String,
275    pub md_password: String,
276    /// 订阅标的(逗号分隔;`订阅行情` 钮解析)。
277    pub symbols: String,
278}
279
280impl CtpForm {
281    /// 表单 → 线上契约(字段直映;md 回落由插件侧裁决,表单不预处理)。
282    pub fn to_configure_req(&self) -> ek_ctp::plugin::ConfigureReq {
283        ek_ctp::plugin::ConfigureReq {
284            broker_id: self.broker_id.trim().to_string(),
285            td_addr: self.td_addr.trim().to_string(),
286            td_user: self.td_user.trim().to_string(),
287            td_password: self.td_password.clone(),
288            td_auth_code: self.td_auth_code.trim().to_string(),
289            td_app_id: self.td_app_id.trim().to_string(),
290            md_addr: self.md_addr.trim().to_string(),
291            md_user: self.md_user.trim().to_string(),
292            md_password: self.md_password.clone(),
293        }
294    }
295
296    /// 订阅位解析(逗号/空白分隔;空表 = Err)。
297    pub fn parse_symbols(&self) -> Result<Vec<String>, String> {
298        let list: Vec<String> = self
299            .symbols
300            .split([',', ',', ' '])
301            .map(|s| s.trim().to_string())
302            .filter(|s| !s.is_empty())
303            .collect();
304        if list.is_empty() {
305            return Err("订阅解析: 标的位空".to_string());
306        }
307        Ok(list)
308    }
309}
310
311/// 交易监控快照(S5;ui 插件 eTimer 拍 ask oms 全量查询组帧)。
312#[derive(Debug, Clone, PartialEq, Default)]
313pub struct TradeSnap {
314    pub orders: Vec<ek_data::order::OrderData>,
315    pub trades: Vec<ek_data::trade::TradeData>,
316    pub positions: Vec<ek_data::position::PositionData>,
317    /// 资金帧(oms.get_all_accounts;CTP configure 登录后有数)。
318    pub accounts: Vec<ek_data::account::AccountData>,
319}
320
321/// CSV(每行 `dt,open,high,low,close,volume`;dt 为 epoch 秒)→ BarData 批。
322/// 错行报行号;空 csv = Err;升序不强制(dm.reload 自会逐行入窗)。
323pub fn parse_reload_csv(form: &ReloadForm) -> Result<(i64, String, Vec<BarData>), String> {
324    let interval: i64 = form
325        .interval
326        .trim()
327        .parse()
328        .map_err(|_| format!("回灌 解析: interval 非整数:{:?}", form.interval))?;
329    let symbol = form.symbol.trim().to_string();
330    if symbol.is_empty() {
331        return Err("回灌解析: symbol 空".to_string());
332    }
333    if form.csv.trim().is_empty() {
334        return Err("回灌解析: csv 空".to_string());
335    }
336    let mut bars = Vec::new();
337    for (i, raw) in form.csv.lines().enumerate() {
338        let line = raw.trim();
339        if line.is_empty() || line.starts_with('#') {
340            continue;
341        }
342        let parts: Vec<&str> = line.split(',').map(str::trim).collect();
343        if parts.len() != 6 {
344            return Err(format!("回灌解析: 第 {} 列数 ≠ 6", i + 1));
345        }
346        let num = |s: &str| -> Result<f64, String> {
347            s.parse::<f64>()
348                .map_err(|_| format!("回灌解析: 第 {} 列数值坏:{:?}", i + 1, s))
349        };
350        let dt_s: i64 = parts[0]
351            .parse()
352            .map_err(|_| format!("回灌解析: 第 {} 列 dt 坏:{:?}", i + 1, parts[0]))?;
353        bars.push(BarData {
354            symbol: symbol.clone(),
355            datetime: dt_s * 1_000_000_000,
356            interval: Some(
357                ek_dm::wire::interval_of_seconds(interval)
358                    .unwrap_or(ek_data::constant::Interval::MINUTE),
359            ),
360            open_price: num(parts[1])?,
361            high_price: num(parts[2])?,
362            low_price: num(parts[3])?,
363            close_price: num(parts[4])?,
364            volume: num(parts[5])?,
365            ..BarData::default()
366        });
367    }
368    if bars.is_empty() {
369        return Err("回灌解析: 无有效行".to_string());
370    }
371    Ok((interval, symbol, bars))
372}
373
374// ================= v4 布局面(带/组 tab / tick 库 / 下单表单;docs/ek-ui-layout-v2-plan.md) =================
375
376/// 左上组 tab(Python [委托|未成交];审计修正:行情是带不是 tab,K线/运维/CTP
377/// 是弹窗不是 tab,故不设全局 10 值 UiTab——那是 v2 残影)。
378#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
379pub enum TopLeftTab {
380    #[default]
381    Orders,
382    Active,
383}
384
385impl TopLeftTab {
386    pub fn label(self) -> &'static str {
387        match self {
388            Self::Orders => "委托",
389            Self::Active => "未成交",
390        }
391    }
392}
393
394/// 右下组 tab(Python [资金|日志])。
395#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
396pub enum BottomRightTab {
397    #[default]
398    Account,
399    Logs,
400}
401
402impl BottomRightTab {
403    pub fn label(self) -> &'static str {
404        match self {
405            Self::Account => "资金",
406            Self::Logs => "日志",
407        }
408    }
409}
410
411/// tick 库(v2 行情 tab 唯一数据源):symbol → 最新 tick,逐 symbol 覆盖,
412/// 有界 = 订阅面(ek-ctp 只发已订阅合约;无 bar 回落,用户裁定)。
413#[derive(Debug, Default)]
414pub struct TickStore {
415    ticks: BTreeMap<String, ek_data::tick::TickData>,
416}
417
418impl TickStore {
419    /// 逐合约覆盖(审计修正:键 = en_symbol「交易所.合约」,防跨所同码相撞;
420    /// 交易所分组取 tick 自带 exchange)。
421    pub fn put(&mut self, t: &ek_data::tick::TickData) {
422        self.ticks
423            .insert(format!("{}.{}", t.exchange.as_str(), t.symbol), t.clone());
424    }
425
426    /// 只读视图(交易所 → 合约排序,tab 分组顺序稳定)。
427    pub fn rows(&self) -> Vec<ek_data::tick::TickData> {
428        let mut v: Vec<ek_data::tick::TickData> = self.ticks.values().cloned().collect();
429        v.sort_by(|a, b| {
430            (a.exchange.as_str().to_string(), a.symbol.clone())
431                .cmp(&(b.exchange.as_str().to_string(), b.symbol.clone()))
432        });
433        v
434    }
435
436    /// 按交易所分组(tab 键只列有 tick 的所,不预置全枚举)。
437    pub fn groups(&self) -> Vec<(String, Vec<ek_data::tick::TickData>)> {
438        let mut out: Vec<(String, Vec<ek_data::tick::TickData>)> = Vec::new();
439        for t in self.rows() {
440            let ex = t.exchange.as_str().to_string();
441            match out.last_mut() {
442                Some((e, v)) if *e == ex => v.push(t),
443                _ => out.push((ex, vec![t])),
444            }
445        }
446        out
447    }
448
449    pub fn len(&self) -> usize {
450        self.ticks.len()
451    }
452
453    pub fn is_empty(&self) -> bool {
454        self.ticks.is_empty()
455    }
456}
457
458/// 下单面板表单(v2 常驻右侧;Python TradingWidget 字段面等价;操作面非配置面)。
459#[derive(Debug, Clone, PartialEq)]
460pub struct OrderForm {
461    /// 交易所代码(下拉;from_str 抬成枚举)
462    pub exchange: String,
463    /// 合约代码(纯 symbol,不带前缀)
464    pub symbol: String,
465    pub direction: ek_data::constant::Direction,
466    pub offset: ek_data::constant::Offset,
467    pub order_type: ek_data::constant::OrderType,
468    /// 输入面保持字符串(同 Python QLineEdit),委托时解析
469    pub price: String,
470    pub volume: String,
471    /// 网关(下拉源 = trade_snap gateway 去重集 ∪ CTP 表单现值)
472    pub gateway: String,
473    /// 价格随行情勾选(勾选时价格框随选中合约最新 tick)
474    pub follow_price: bool,
475}
476
477impl Default for OrderForm {
478    fn default() -> Self {
479        Self {
480            exchange: "SHFE".to_string(),
481            symbol: String::new(),
482            direction: ek_data::constant::Direction::LONG,
483            offset: ek_data::constant::Offset::OPEN,
484            order_type: ek_data::constant::OrderType::LIMIT,
485            price: String::new(),
486            volume: "1".to_string(),
487            gateway: String::new(),
488            follow_price: true,
489        }
490    }
491}
492
493impl OrderForm {
494    /// 表单 → OrderRequest(与票据解析同一出口;错误红字就地,不进总线)。
495    pub fn to_request(&self) -> Result<ek_data::order_request::OrderRequest, String> {
496        use std::str::FromStr;
497        let symbol = self.symbol.trim();
498        if symbol.is_empty() {
499            return Err("下单: 代码为空".to_string());
500        }
501        if symbol.contains('.') {
502            return Err("下单: 代码不带交易所前缀(交易所走下拉)".to_string());
503        }
504        let exchange = ek_data::constant::Exchange::from_str(self.exchange.trim())
505            .map_err(|e| format!("下单: {e}"))?;
506        let price: f64 = self
507            .price
508            .trim()
509            .parse()
510            .map_err(|_| format!("下单: 价格无效 {:?}", self.price))?;
511        let volume: f64 = self
512            .volume
513            .trim()
514            .parse()
515            .map_err(|_| format!("下单: 数量无效 {:?}", self.volume))?;
516        if self.order_type != ek_data::constant::OrderType::MARKET && price <= 0.0 {
517            return Err("下单: 限价单价格须为正".to_string());
518        }
519        if volume <= 0.0 {
520            return Err("下单: 数量须为正".to_string());
521        }
522        Ok(ek_data::order_request::OrderRequest {
523            symbol: symbol.to_string(),
524            exchange,
525            direction: self.direction,
526            order_type: self.order_type,
527            volume,
528            price,
529            offset: self.offset,
530            ..ek_data::order_request::OrderRequest::default()
531        })
532    }
533}
534
535/// 合约库(v4 查询合约窗数据源):en_symbol → ContractData。
536///
537/// 数据面 = ek-ctp 登录后自动 reqQryInstrument 回流(`eContract.` 裸键逐条 +
538/// `eContractFinish` 完成线,driver.rs:779)——**无 ask 口,UI 只收不催**
539/// (用户裁定:订阅不在网关配置定死,查询窗勾选订阅 / 下单代码回车)。
540#[derive(Debug, Default)]
541pub struct ContractStore {
542    contracts: BTreeMap<String, ek_data::contract::ContractData>,
543}
544
545impl ContractStore {
546    pub fn put(&mut self, c: &ek_data::contract::ContractData) {
547        self.contracts
548            .insert(format!("{}.{}", c.exchange.as_str(), c.symbol), c.clone());
549    }
550
551    /// Python ContractManager 查询口径:代码/交易所/名称模糊,留空 = 全部。
552    pub fn query(&self, pattern: &str) -> Vec<ek_data::contract::ContractData> {
553        let p = pattern.trim();
554        self.contracts
555            .values()
556            .filter(|c| {
557                p.is_empty()
558                    || c.symbol.contains(p)
559                    || c.exchange.as_str().eq_ignore_ascii_case(p)
560                    || c.name.contains(p)
561            })
562            .cloned()
563            .collect()
564    }
565
566    pub fn len(&self) -> usize {
567        self.contracts.len()
568    }
569
570    pub fn is_empty(&self) -> bool {
571        self.contracts.is_empty()
572    }
573}
574
575/// 插件面板与宿主观察线程共用的观测状态。
576#[derive(Debug, Default)]
577pub struct UiShared {
578    logs: Mutex<LogRing>,
579    snap: Mutex<Arc<BusSnap>>,
580    market: MarketStore,
581    ctp_form: Mutex<CtpForm>,
582    trade_snap: Mutex<Arc<TradeSnap>>,
583    tick_store: Mutex<TickStore>,
584    order_form: Mutex<OrderForm>,
585    selected_exchange: Mutex<String>,
586    topleft_tab: Mutex<TopLeftTab>,
587    bottomright_tab: Mutex<BottomRightTab>,
588    ops_window: AtomicBool,
589    ctp_window: AtomicBool,
590    contracts_window: AtomicBool,
591    contract_store: Mutex<ContractStore>,
592    contract_query: Mutex<String>,
593    contract_selected: Mutex<Vec<String>>,
594    kline_windows: Mutex<Vec<String>>,
595    rev: AtomicU64,
596    /// 关窗/测试结束信号(窗口面置位,宿主观察线程消费)。
597    stop: AtomicBool,
598    /// svctl 操作指令队列(窗口面入,ui 插件 eTimer 出)。
599    cmds: Mutex<VecDeque<UiCmd>>,
600    /// 待二次确认的移除目标(单槽;再次同名点 = 发令)。
601    pending_confirm: Mutex<Option<String>>,
602}
603
604impl UiShared {
605    /// 空状态(快照空、日志空、rev 0)。
606    pub fn new() -> Self {
607        Self::default()
608    }
609
610    // ---- v2 布局面访问器(tick 库 / 表单 / 选中 / tab) ----
611
612    /// 落一条 tick(eTick. 裸键上屏;逐 symbol 覆盖)。
613    pub fn update_tick(&self, t: &ek_data::tick::TickData) {
614        self.tick_store.lock().unwrap().put(t);
615        self.bump();
616    }
617
618    /// tick 只读视图(排序后)。
619    pub fn tick_rows(&self) -> Vec<ek_data::tick::TickData> {
620        self.tick_store.lock().unwrap().rows()
621    }
622
623    /// tick 按交易所分组(行情 tab 二级 tab 键表)。
624    pub fn tick_groups(&self) -> Vec<(String, Vec<ek_data::tick::TickData>)> {
625        self.tick_store.lock().unwrap().groups()
626    }
627
628    /// tick 库格数。
629    pub fn tick_count(&self) -> usize {
630        self.tick_store.lock().unwrap().len()
631    }
632
633    /// 下单表单只读快照。
634    pub fn order_form(&self) -> OrderForm {
635        self.order_form.lock().unwrap().clone()
636    }
637
638    /// 下单表单写(渲染侧每控件改动即写)。
639    pub fn set_order_form(&self, f: OrderForm) {
640        *self.order_form.lock().unwrap() = f;
641        self.bump();
642    }
643
644    /// 选中交易所(行情二级 tab 态;行情行单击 = 带入下单面板见 render 侧)。
645    pub fn selected_exchange(&self) -> String {
646        self.selected_exchange.lock().unwrap().clone()
647    }
648
649    /// 选中交易所写。
650    pub fn set_selected_exchange(&self, s: impl Into<String>) {
651        *self.selected_exchange.lock().unwrap() = s.into();
652        self.bump();
653    }
654
655    /// 左上组当前 tab。
656    pub fn topleft_tab(&self) -> TopLeftTab {
657        *self.topleft_tab.lock().unwrap()
658    }
659
660    /// 左上组 tab 写。
661    pub fn set_topleft_tab(&self, t: TopLeftTab) {
662        *self.topleft_tab.lock().unwrap() = t;
663        self.bump();
664    }
665
666    /// 右下组当前 tab。
667    pub fn bottomright_tab(&self) -> BottomRightTab {
668        *self.bottomright_tab.lock().unwrap()
669    }
670
671    /// 右下组 tab 写。
672    pub fn set_bottomright_tab(&self, t: BottomRightTab) {
673        *self.bottomright_tab.lock().unwrap() = t;
674        self.bump();
675    }
676
677    // ---- v4 弹窗面(egui::Window 开关;kittest 可断言) ----
678
679    /// 运维窗开合。
680    pub fn ops_window_open(&self) -> bool {
681        self.ops_window.load(Ordering::Acquire)
682    }
683
684    /// 运维窗开合写。
685    pub fn set_ops_window_open(&self, v: bool) {
686        self.ops_window.store(v, Ordering::Release);
687        self.bump();
688    }
689
690    /// CTP 配置窗开合。
691    pub fn ctp_window_open(&self) -> bool {
692        self.ctp_window.load(Ordering::Acquire)
693    }
694
695    /// CTP 配置窗开合写。
696    pub fn set_ctp_window_open(&self, v: bool) {
697        self.ctp_window.store(v, Ordering::Release);
698        self.bump();
699    }
700
701    /// 已开 K线窗合约表(多开,去重)。
702    pub fn kline_symbols(&self) -> Vec<String> {
703        self.kline_windows.lock().unwrap().clone()
704    }
705
706    /// 开 K线窗(去重;日志留痕可断言)。
707    pub fn open_kline(&self, symbol: impl Into<String>) {
708        let s = symbol.into();
709        let mut v = self.kline_windows.lock().unwrap();
710        if !v.contains(&s) {
711            v.push(s.clone());
712        }
713        drop(v);
714        self.log_line(format!("[kline] 打开 K线窗 {s}"));
715    }
716
717    /// 关 K线窗(窗关闭钮 → 出表)。
718    pub fn close_kline(&self, symbol: &str) {
719        self.kline_windows.lock().unwrap().retain(|s| s != symbol);
720        self.bump();
721    }
722
723    // ---- v4 查询合约窗面(合约库 / 查询串 / 勾选集) ----
724
725    /// 查询合约窗开合。
726    pub fn contracts_window_open(&self) -> bool {
727        self.contracts_window.load(Ordering::Acquire)
728    }
729
730    /// 查询合约窗开合写。
731    pub fn set_contracts_window_open(&self, v: bool) {
732        self.contracts_window.store(v, Ordering::Release);
733        self.bump();
734    }
735
736    /// 落一条合约(`eContract.` 裸键上屏)。
737    pub fn update_contract(&self, c: &ek_data::contract::ContractData) {
738        self.contract_store.lock().unwrap().put(c);
739        self.bump();
740    }
741
742    /// 合约查询(Python ContractManager 口径:留空 = 全部)。
743    pub fn contracts(&self, pattern: &str) -> Vec<ek_data::contract::ContractData> {
744        self.contract_store.lock().unwrap().query(pattern)
745    }
746
747    /// 合约库格数。
748    pub fn contract_count(&self) -> usize {
749        self.contract_store.lock().unwrap().len()
750    }
751
752    /// 查询串读写。
753    pub fn contract_query(&self) -> String {
754        self.contract_query.lock().unwrap().clone()
755    }
756
757    /// 查询串写。
758    pub fn set_contract_query(&self, q: impl Into<String>) {
759        *self.contract_query.lock().unwrap() = q.into();
760        self.bump();
761    }
762
763    /// 勾选翻转(en_symbol)。
764    pub fn toggle_contract_selection(&self, en_symbol: &str) {
765        let mut v = self.contract_selected.lock().unwrap();
766        match v.iter().position(|s| s == en_symbol) {
767            Some(i) => {
768                v.remove(i);
769            }
770            None => v.push(en_symbol.to_string()),
771        }
772        self.bump();
773    }
774
775    /// 当前勾选集。
776    pub fn selected_contracts(&self) -> Vec<String> {
777        self.contract_selected.lock().unwrap().clone()
778    }
779
780    /// 追加一行日志(总线面/宿主观察线程写)。
781    pub fn log_line(&self, line: impl Into<String>) {
782        self.logs.lock().unwrap().push(line);
783        self.bump();
784    }
785
786    /// 清空日志(渲染"清空日志"按钮 / 测试)。
787    pub fn clear_logs(&self) {
788        self.logs.lock().unwrap().clear();
789        self.bump();
790    }
791
792    /// 日志只读快照 + 溢出计数。
793    pub fn logs_snapshot(&self) -> (Vec<String>, u64) {
794        let r = self.logs.lock().unwrap();
795        (r.snapshot(), r.spilled())
796    }
797
798    /// 日志当前长度(kittest 断言用)。
799    pub fn logs_len(&self) -> usize {
800        self.logs.lock().unwrap().len()
801    }
802
803    /// 落一次服务快照(eTimer 节流回调)。
804    pub fn set_snapshot(&self, snap: BusSnap) {
805        *self.snap.lock().unwrap() = Arc::new(snap);
806        self.bump();
807    }
808
809    /// 当前快照(Arc 克隆,零拷贝走锁)。
810    pub fn snapshot(&self) -> Arc<BusSnap> {
811        Arc::clone(&self.snap.lock().unwrap())
812    }
813
814    /// revision:任何写入 +1(插件面 ePlug 事件也用;测试判定"有事件进来过")。
815    pub fn rev(&self) -> u64 {
816        self.rev.load(Ordering::Acquire)
817    }
818
819    /// 手动推一次 revision(总线面 ePlugUp/Down 记账用)。
820    pub fn bump(&self) {
821        self.rev.fetch_add(1, Ordering::AcqRel);
822    }
823
824    /// 置关窗信号(eframe App 关窗钩子调用)。
825    pub fn request_stop(&self) {
826        self.stop.store(true, Ordering::Release);
827    }
828
829    /// 观察线程轮询用。
830    pub fn is_stop(&self) -> bool {
831        self.stop.load(Ordering::Acquire)
832    }
833
834    // ---- svctl 操作面(S2) ----
835
836    /// 入一条操作指令(窗口面离场动作唯一通道;左位不直调)。
837    pub fn push_cmd(&self, cmd: UiCmd) {
838        self.cmds.lock().unwrap().push_back(cmd);
839        self.bump();
840    }
841
842    /// 取走全部指令(eTimer 节拍辞行)。
843    pub fn drain_cmds(&self) -> Vec<UiCmd> {
844        self.cmds.lock().unwrap().drain(..).collect()
845    }
846
847    /// 当前待二次确认目标(Some = 二次确认中)。
848    pub fn pending_confirm(&self) -> Option<String> {
849        self.pending_confirm.lock().unwrap().clone()
850    }
851
852    /// 确认步进:同名二点 = 发令(Fired),异名 = 迁移(Switched),首点 = 进入待确认。
853    pub fn confirm_step(&self, name: &str) -> ConfirmStep {
854        let mut g = self.pending_confirm.lock().unwrap();
855        match g.take() {
856            Some(p) if p == name => ConfirmStep::Fired,
857            Some(_) => {
858                *g = Some(name.to_string());
859                ConfirmStep::Switched
860            }
861            None => {
862                *g = Some(name.to_string());
863                ConfirmStep::Armed
864            }
865        }
866    }
867
868    /// 清除待确认(取消钮)。
869    pub fn confirm_cancel(&self) {
870        *self.pending_confirm.lock().unwrap() = None;
871        self.bump();
872    }
873
874    // ---- S4 行情面 ----
875
876    /// 行情库直读(读写两用:worker 写,渲染读)。
877    pub fn market(&self) -> &MarketStore {
878        &self.market
879    }
880
881    // ---- S5 交易面 ----
882
883    /// 交易监控快照写(ui 插件 eTimer 拍)。
884    pub fn set_trade_snap(&self, ts: TradeSnap) {
885        *self.trade_snap.lock().unwrap() = Arc::new(ts);
886        self.bump();
887    }
888
889    /// 交易监控快照读(渲染)。
890    pub fn trade_snap(&self) -> Arc<TradeSnap> {
891        self.trade_snap.lock().unwrap().clone()
892    }
893
894    // ---- S6 CTP 配置面 ----
895
896    /// CTP 配置表单读(渲染克隆面)。
897    pub fn ctp_form(&self) -> CtpForm {
898        self.ctp_form.lock().unwrap().clone()
899    }
900
901    /// CTP 配置表单写。
902    pub fn set_ctp_form(&self, f: CtpForm) {
903        *self.ctp_form.lock().unwrap() = f;
904        self.bump();
905    }
906}