Skip to main content

ek_ui/
runtime.rs

1//! 运行时装配:工厂表(ui 工厂注册)、宿主观察线程(关窗→close_all)、
2//! 进程主入口(eframe 主线程 + 宿主观察线程)。
3//!
4//! 线程形貌(S1b 双线程纪律):
5//! - 主线程:eframe run_native(App 只读 [`UiShared`]);
6//! - 观察线程:sleep 轮询 `UiShared::is_stop` → `engine.close_all`;
7//! - 等价 headless 形貌见 ek-station bin(不带 ui 工厂)。
8
9use std::sync::Arc;
10use std::time::Duration;
11
12use ek_core::main_engine::MainEngine;
13use ek_core::plugin::Plugin;
14use ek_dm::plugin::DmConfig;
15use ek_station::{PlugKind, StationConfig, default_kinds};
16
17use crate::plugin::{UI_NAME, UiPlugin};
18use crate::state::UiShared;
19
20/// ek-station 缺省工厂表(dm/oms/ctp;0.1.12 起 sim 已退场)+ `ui` 工厂 +
21/// **rt-on 的 dm 工厂**(S4:UI 宿主观测面需要 `eBarUpdate.` 实时门;
22/// ek-station headless 保持 rt-off 缺省不动 —— 两宿主的 dm 配置差异在各自
23/// 工厂表,互不影响)。
24///
25/// `dm_rt` 是装配期参数(非配置文件;ek-ui 零配置口径,缺省 true);
26/// 多次 build 同享一个 `UiShared`(ui 插件被 svctl 摘/再注,观测不丢)。
27pub fn ui_kinds(shared: &Arc<UiShared>, dm_rt: bool) -> Vec<PlugKind> {
28    let mut kinds: Vec<PlugKind> = default_kinds()
29        .into_iter()
30        .filter(|k| k.name != "dm")
31        .collect();
32    kinds.push(PlugKind {
33        name: "dm",
34        doc: "数据管理器(KlineManager;real_time 更新门开,行情板/K线页取 eBarUpdate./eBar.)",
35        build: Arc::new(move || -> Arc<dyn Plugin> {
36            Arc::new(ek_dm::plugin::DmPlugin::new(DmConfig {
37                real_time: dm_rt,
38                ..DmConfig::minute("dm")
39            }))
40        }),
41        build_named: None,
42        name_validator: None,
43    });
44    kinds.push(PlugKind {
45        name: UI_NAME,
46        doc: "常驻观测界面(eLog + eTimer 1s 快照;关窗即 stop)",
47        build: {
48            let shared = Arc::clone(shared);
49            Arc::new(move || -> Arc<dyn Plugin> { Arc::new(UiPlugin::new(Arc::clone(&shared))) })
50        },
51        build_named: None,
52        name_validator: None,
53    });
54    kinds
55}
56
57/// 宿主观察线程:窗口不复用的引擎在这里按 stop 位收场(close_all join)。
58///
59/// 纪律:close 顺序也让宿主自己走(逆 start 序),观察线程只送信号不代走步骤。
60pub fn spawn_host_watcher(
61    engine: &Arc<MainEngine>,
62    shared: &Arc<UiShared>,
63) -> std::thread::JoinHandle<()> {
64    let engine = Arc::clone(engine);
65    let shared = Arc::clone(shared);
66    std::thread::spawn(move || {
67        while !shared.is_stop() {
68            std::thread::sleep(Duration::from_millis(120));
69        }
70        shared.log_line("[host] 窗口关闭,close_all …");
71        engine.close_all(Duration::from_secs(3));
72        shared.log_line("[host] 已收场");
73    })
74}
75
76/// 档案包操作(导出/导入)一行回执文本;根不可用 = 显形不炸。
77fn bundle_op(shared: &Arc<UiShared>, engine: &MainEngine, dir: &str, export: bool) -> String {
78    let host = crate::faces::CfgBridge::new(shared);
79    let dest = std::path::Path::new(dir);
80    if dir.trim().is_empty() {
81        return "目录为空(先填路径)".to_string();
82    }
83    if export {
84        let Some(root) = host.cfg.as_ref().map(|c| c.root().to_path_buf()) else {
85            return "导出失败:档案托底根不可用".to_string();
86        };
87        match crate::pluginwin::export_bundle(&root, dest) {
88            Ok((c, s)) => {
89                let msg = format!("导出完成:{c} 段 → {}(跳过非段文件 {s})", dest.display());
90                shared.log_line(format!("[ui] {msg}"));
91                msg
92            }
93            Err(e) => format!("导出失败:{e}"),
94        }
95    } else {
96        let lines = crate::pluginwin::import_bundle(&host, dest, &|ns, sec| {
97            crate::faces::apply_section(engine, ns, sec)
98        });
99        let msg = lines.join(" | ");
100        shared.log_line(format!("[ui] 档案包导入:{msg}"));
101        msg
102    }
103}
104
105/// eframe App(窗口面):只读 shared 渲染 + 1s 节流重绘。
106struct StationApp {
107    shared: Arc<UiShared>,
108    /// 宿主引擎(0.9.2「插件即 App」:挂载表按装配名单走)。
109    engine: Arc<MainEngine>,
110    /// 插件面状态包(各面自态 + 通用窗口态)。
111    fs: crate::faces::FaceStates,
112    /// 档案包窗口目录草稿与回执。
113    bundle_dir: String,
114    bundle_notice: Option<String>,
115}
116
117impl eframe::App for StationApp {
118    fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
119        let host = crate::faces::CfgBridge::new(&self.shared);
120        let mounted = crate::faces::mounted(&self.engine);
121        // 「插件配置」窗(菜单钮开合;CentralPanel 必须最后,BD-UI-29 纪律不破)。
122        if self.shared.pluginwin_open() {
123            for (name, title) in mounted.iter() {
124                let mut open = true;
125                egui::Window::new(title.as_str())
126                    .id(egui::Id::new(format!("pluginwin-{name}")))
127                    .open(&mut open)
128                    .default_width(460.0)
129                    .show(ctx, |ui| {
130                        crate::faces::render_face(ui, &self.engine, name, &host, &mut self.fs);
131                    });
132                let _ = open;
133            }
134        }
135        // 档案包窗口:导出/导入 + 逐段回执显形(口令只到引用,CLI 引导)。
136        if self.shared.bundle_open() {
137            let mut open = true;
138            egui::Window::new("档案包")
139                .id(egui::Id::new("bundlewin"))
140                .open(&mut open)
141                .default_width(430.0)
142                .show(ctx, |ui| {
143                    ui.label("目录(导出目标 / 导入来源,同一目录可两用):");
144                    ui.add(
145                        egui::TextEdit::singleline(&mut self.bundle_dir)
146                            .desired_width(400.0)
147                            .hint_text("C:\\Users\\<你>\\Documents\\ek-bundle"),
148                    );
149                    ui.horizontal(|ui| {
150                        if ui.button("导出(段文件整批复制)").clicked() {
151                            self.bundle_notice =
152                                Some(bundle_op(&self.shared, &self.engine, &self.bundle_dir, true));
153                        }
154                        if ui.button("导入(逐段收编+受理)").clicked() {
155                            self.bundle_notice = Some(bundle_op(
156                                &self.shared,
157                                &self.engine,
158                                &self.bundle_dir,
159                                false,
160                            ));
161                        }
162                    });
163                    if let Some(n) = &self.bundle_notice {
164                        ui.label(egui::RichText::new(n.as_str()).monospace().small());
165                    }
166                    ui.label(
167                        egui::RichText::new("密码不落包(在 Windows 凭据管理器);换机后口令走 ekapp add-account(stdin)")
168                            .small()
169                            .weak(),
170                    );
171                });
172            let _ = open;
173        }
174        // 面板组合在 render_body 内(官方纪律:CentralPanel 最后;BD-UI-29)。
175        crate::render_body(ctx, &self.shared);
176        ctx.request_repaint_after(Duration::from_secs(1));
177    }
178}
179
180/// 运行选项(S1c ready 标记 + S4 dm real_time 装配参数;ek-ui 零配置口径,
181/// 无任何配置文件输入 —— 结构只由 StationConfig 清单决定)。
182#[derive(Debug, Clone)]
183pub struct RunOpts {
184    /// launcher 协议:`start 成功后写 "ready\n"` 的标记路径。
185    pub ready_marker: Option<std::path::PathBuf>,
186    /// dm 工厂 real_time 门(装配参数,缺省 true —— UI 宿主观测面要实时门)。
187    pub dm_rt: bool,
188}
189
190impl Default for RunOpts {
191    /// 缺省:无 ready 标记 + **dm_rt = true**(UI 宿主缺省实时门;
192    /// 勿改回 derive(Default) —— bool 缺省 false 会静默关实时门)。
193    fn default() -> Self {
194        Self {
195            ready_marker: None,
196            dm_rt: true,
197        }
198    }
199}
200
201impl RunOpts {
202    /// 空选项(无 ready 标记)。
203    pub fn plain() -> Self {
204        Self::default()
205    }
206}
207
208/// 进程主入口:装配(dm/oms/ctp/ui 按清单)→ start → 就绪标记(可选)
209/// → eframe 主循环 → 观察线程收场。
210pub fn run_station_ui(cfg: &StationConfig, opts: &RunOpts) -> Result<(), String> {
211    let engine = Arc::new(MainEngine::new(None));
212    let shared = Arc::new(UiShared::new());
213
214    // 配置托底根(0.6.0/BD-CFG 环节8):UI 宿主与 headless 同口径 ——
215    // `Store::eco()` 成功 → `boot_with_cfg`(开机逐插件 apply_persisted,
216    // 受理三行序在插件 worker 面);eco 失败(平台缺配置目录,罕见)→
217    // 退回 `boot()`(cfg 收件壳仍在场,fallback 直写仍通,UI 行为即旧版零变)。
218    let store = ek_cfg::Store::eco();
219    let boot = match &store {
220        Ok(s) => ek_station::boot_with_cfg(&engine, &ui_kinds(&shared, opts.dm_rt), cfg, Some(s)),
221        Err(e) => {
222            let _ = e; // 根不可用为环境面问题;factories 的 eco_or_local 已显形
223            ek_station::boot(&engine, &ui_kinds(&shared, opts.dm_rt), cfg)
224        }
225    };
226    boot.map_err(|e| format!("boot: {e:?}"))?;
227    // start 失败也要走 close_all(0.4.3 BD-UI-64,审核 P3;与 ek-station headless
228    // main.rs 的 start 失败路径同口径):start 是逐个 launch 的,失败时已有插件在
229    // Running —— 不闭就没有 on_stop(网关侧的会话收尾与流目录处理被跳过)。
230    // boot 失败不用闭:那时插件只登记未 launch,没有 worker 要收。
231    if let Err(e) = engine.start() {
232        engine.close_all(Duration::from_secs(3));
233        return Err(format!("start: {e:?}"));
234    }
235
236    if let Some(marker) = &opts.ready_marker {
237        use std::io::Write;
238        if let Ok(mut f) = std::fs::File::create(marker) {
239            let _ = f.write_all(crate::launch::READY_TEXT.as_bytes());
240        } else {
241            eprintln!("ek-ui: ready marker write failed: {}", marker.display());
242        }
243    }
244
245    let watcher = spawn_host_watcher(&engine, &shared);
246
247    let opts = eframe::NativeOptions {
248        viewport: egui::ViewportBuilder::default()
249            .with_inner_size([1280.0, 860.0])
250            .with_title("ek-station"),
251        ..Default::default()
252    };
253    // 关窗即收:run_native 返回 = 窗口已关 → 置 stop → 观察线程 close_all(BD-UI-11)。
254    let result = eframe::run_native(
255        "ek-station",
256        opts,
257        Box::new(|cc| {
258            crate::fonts::apply_cjk_fonts(&cc.egui_ctx); // BD-UI-26:CJK 回退,中文不乱码
259            Ok(Box::new(StationApp {
260                shared: Arc::clone(&shared),
261                engine: Arc::clone(&engine),
262                fs: crate::faces::FaceStates::new(),
263                bundle_dir: String::new(),
264                bundle_notice: None,
265            }) as Box<dyn eframe::App>)
266        }),
267    );
268    shared.request_stop();
269    let _ = watcher.join();
270    result.map_err(|e| format!("ui: {e:?}"))
271}