docs: 逐模块细化中文逻辑注释并覆盖测试与示例
This commit is contained in:
+29
-3
@@ -21,6 +21,7 @@ use winit::event_loop::EventLoopProxy;
|
||||
/// 拖放目标窗口、目标布局版本和插入位置,源标签身份由外层 Input 保存。
|
||||
pub struct TabDrop {
|
||||
pub window: u64,
|
||||
// 事件所依据的已发布布局版本,由执行端校验过期帧;原生连续手势有单独刷新逻辑。
|
||||
pub layout_version: u64,
|
||||
pub index: usize,
|
||||
}
|
||||
@@ -31,15 +32,19 @@ pub struct TabDrop {
|
||||
pub struct Input {
|
||||
#[serde(default = "default_window")]
|
||||
pub window: u64,
|
||||
// 产生事件的运行时会话;重载后即使窗口或 Mount 数值相同也不能沿用旧事件。
|
||||
pub session: u64,
|
||||
pub layout_version: u64,
|
||||
// 捕获的具体出现身份;页面级相机事件使用零,独立视口相机事件要求非零。
|
||||
pub mount: u64,
|
||||
pub kind: String,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
// 事件位置或缩放锚点;click 与 press_point 配对,具体坐标语义由事件 kind 决定。
|
||||
pub point: Option<[f32; 2]>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub press_point: Option<[f32; 2]>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
// 平移/滚动增量,或缩放的 [倍率,0];不能无差别接受任意事件携带该字段。
|
||||
pub delta: Option<[f32; 2]>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub tab_drop: Option<TabDrop>,
|
||||
@@ -53,11 +58,12 @@ fn default_window() -> u64 {
|
||||
}
|
||||
|
||||
impl Input {
|
||||
/// 检查当前输入或发布值的不变量,调用方据校验结果决定是否继续。
|
||||
/// 按事件种类核对可携带字段、坐标范围和 Mount 是否为零;不替代执行端的会话/能力校验。
|
||||
pub fn validate(&self) -> Result<(), &'static str> {
|
||||
if self.window == 0 {
|
||||
return Err("window identity must be nonzero");
|
||||
}
|
||||
// 标签跨窗口放置仅接受目标元信息;禁止同时夹带指针/滚动/屏幕位置,避免混用两套操作。
|
||||
if self.kind == "tab-drop" {
|
||||
let Some(target) = &self.tab_drop else {
|
||||
return Err("tab-drop requires target metadata");
|
||||
@@ -73,9 +79,11 @@ impl Input {
|
||||
}
|
||||
return Ok(());
|
||||
}
|
||||
// 非 tab-drop 不得携带目标窗口信息,即使其他字段恰好合法也拒绝。
|
||||
if self.tab_drop.is_some() {
|
||||
return Err("tab_drop is only accepted for tab-drop");
|
||||
}
|
||||
// 屏幕位置只服务 tab-detach 的新窗口定位,允许负数以支持左侧/上方显示器,但限制绝对范围。
|
||||
if let Some(position) = self.screen_position
|
||||
&& (self.kind != "tab-detach"
|
||||
|| !position
|
||||
@@ -94,7 +102,9 @@ impl Input {
|
||||
| "scene-pan"
|
||||
| "scene-zoom"
|
||||
) {
|
||||
// 独立视口输入可包含负的局部位置(例如捕获拖动移出视口);页面级锚点要求非负。
|
||||
let viewport = self.kind.starts_with("viewport-") || self.kind.starts_with("scene-");
|
||||
// 所有相机操作必须带有限、有界的增量;point 在非缩放操作可省略。
|
||||
let valid_delta = self.delta.is_some_and(|delta| {
|
||||
delta
|
||||
.iter()
|
||||
@@ -117,6 +127,7 @@ impl Input {
|
||||
{
|
||||
return Err("invalid page camera input");
|
||||
}
|
||||
// 缩放必须给锚点并使用正倍率 0.1..=10;第二分量必须为零,不把缩放与二维平移混合编码。
|
||||
if self.kind.ends_with("-zoom")
|
||||
&& (self.point.is_none()
|
||||
|| self
|
||||
@@ -127,6 +138,7 @@ impl Input {
|
||||
}
|
||||
return Ok(());
|
||||
}
|
||||
// 滚动需要当前点与二维有限增量,不接受按下点;Mount 能力与身份由执行端进一步核对。
|
||||
if self.kind == "scroll" {
|
||||
return match (self.point, self.press_point, self.delta) {
|
||||
(Some(point), None, Some(delta))
|
||||
@@ -145,6 +157,8 @@ impl Input {
|
||||
if self.delta.is_some() {
|
||||
return Err("delta is only accepted for scroll input");
|
||||
}
|
||||
// 无指针坐标的离散事件可通过此结构校验;若有指针信息,必须是 click 的按下/释放成对坐标。
|
||||
// 这里不做完整 kind 白名单授权,具体操作仍由后续分发层检查。
|
||||
match (self.point, self.press_point) {
|
||||
(None, None) => Ok(()),
|
||||
(Some(point), Some(press))
|
||||
@@ -165,15 +179,17 @@ impl Input {
|
||||
/// 原生输入的有界发送端,后台线程串行发往本地 API;所有窗口共享权威发布状态。
|
||||
pub struct Client(SyncSender<Input>);
|
||||
impl Client {
|
||||
/// 构造本类型并初始化其状态;资源建立和默认字段以函数体为准。
|
||||
/// 建立容量 64 的发送队列和单个后台 HTTP 线程;线程创建失败则返回错误。
|
||||
pub fn new(
|
||||
proxy: EventLoopProxy<crate::app::AppEvent>,
|
||||
namespace: Arc<RwLock<crate::document::NamespaceState>>,
|
||||
) -> anyhow::Result<Self> {
|
||||
// 队列最多容纳 64 项;原生发送端使用 try_send,不阻塞 GUI 事件线程,也不保证满队列时无损。
|
||||
let (sender, receiver) = sync_channel::<Input>(64);
|
||||
thread::Builder::new()
|
||||
.name("tessera-native-events".into())
|
||||
.spawn(move || {
|
||||
// 后台线程按队列顺序逐个发送 HTTP 请求;命名空间锁只覆盖检查,不跨越网络等待。
|
||||
for mut input in receiver {
|
||||
if let Ok(current) = namespace.read() {
|
||||
if !refresh_continuous_input(&mut input, ¤t) {
|
||||
@@ -182,6 +198,7 @@ impl Client {
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
// 传输/服务错误通过事件代理回 GUI;代理已关闭时忽略通知失败,接收端退出后线程自然结束。
|
||||
if let Err(error) = send(&input) {
|
||||
let _ = proxy.send_event(crate::app::AppEvent::RuntimeFailed {
|
||||
message: error.to_string(),
|
||||
@@ -191,8 +208,9 @@ impl Client {
|
||||
})?;
|
||||
Ok(Self(sender))
|
||||
}
|
||||
/// 通过有界原生输入客户端或本地 HTTP 发送纯数据事件;过期版本按既定协议处理。
|
||||
/// 非阻塞尝试入队;成功仅代表队列接收,满队列或后台线程关闭返回 false。
|
||||
pub fn send(&self, input: Input) -> bool {
|
||||
// 只反馈是否成功入队;队列满或接收端关闭都返回 false,不代表 worker 已接受或执行事件。
|
||||
self.0.try_send(input).is_ok()
|
||||
}
|
||||
}
|
||||
@@ -208,6 +226,7 @@ fn refresh_continuous_input(input: &mut Input, current: &crate::document::Namesp
|
||||
) {
|
||||
return true;
|
||||
}
|
||||
// 选取事件原窗口的权威帧;窗口已经消失时丢弃,不能把手势改投主窗口。
|
||||
let frame = if input.window == crate::window::MAIN_WINDOW {
|
||||
Some(¤t.document.frame)
|
||||
} else {
|
||||
@@ -220,6 +239,7 @@ fn refresh_continuous_input(input: &mut Input, current: &crate::document::Namesp
|
||||
let Some(frame) = frame else {
|
||||
return false;
|
||||
};
|
||||
// 必须仍属同一运行时会话、帧无故障且原捕获 Mount 具备对应能力,才可更新布局版本。
|
||||
if current.runtime_session != input.session
|
||||
|| frame.session != input.session
|
||||
|| frame.fault.is_some()
|
||||
@@ -236,12 +256,14 @@ fn refresh_continuous_input(input: &mut Input, current: &crate::document::Namesp
|
||||
{
|
||||
return false;
|
||||
}
|
||||
// 只追随新版本,不改 session/window/mount,不重选目标;每条排队增量仍独立发送。
|
||||
input.layout_version = frame.layout_version;
|
||||
true
|
||||
}
|
||||
|
||||
/// 通过有界原生输入客户端或本地 HTTP 发送纯数据事件;过期版本按既定协议处理。
|
||||
fn send(input: &Input) -> anyhow::Result<()> {
|
||||
// 固定回环 API 的轻量客户端,使用 Content-Length 和 Connection: close 定界;不连接外部地址。
|
||||
let body = serde_json::to_string(input)?;
|
||||
let mut stream =
|
||||
TcpStream::connect_timeout(&crate::api::API_ADDRESS.parse()?, Duration::from_secs(2))?;
|
||||
@@ -254,9 +276,11 @@ fn send(input: &Input) -> anyhow::Result<()> {
|
||||
body
|
||||
)?;
|
||||
let mut response = String::new();
|
||||
// 响应读取最多 32 KiB,避免错误正文无限增长;状态从 HTTP 状态行提取,不实现通用 HTTP 客户端。
|
||||
stream.take(32768).read_to_string(&mut response)?;
|
||||
let status = response.split_whitespace().nth(1).unwrap_or("");
|
||||
// An old frame's event is deliberately discarded, with no automatic retry.
|
||||
// 409 代表过期帧冲突,按输入协议静默丢弃,不重试离散动作,避免重复点击或跨版本误操作。
|
||||
if status == "409" {
|
||||
return Ok(());
|
||||
}
|
||||
@@ -306,6 +330,7 @@ mod tests {
|
||||
document,
|
||||
..Default::default()
|
||||
};
|
||||
// 反复从旧帧原始手势克隆输入,验证每条增量都被刷新版本并累计,12×3 应得到 36。
|
||||
for _ in 0..12 {
|
||||
let mut queued = original.clone();
|
||||
assert!(refresh_continuous_input(&mut queued, ¤t));
|
||||
@@ -315,6 +340,7 @@ mod tests {
|
||||
session.execute("view.camera.x", "app.main").unwrap().value,
|
||||
"36"
|
||||
);
|
||||
// 离散点击不得自动刷新版本;随后验证会话替换和目标移除都会拒绝连续手势。
|
||||
let mut click = original.clone();
|
||||
click.kind = "click".into();
|
||||
assert!(refresh_continuous_input(&mut click, ¤t));
|
||||
|
||||
Reference in New Issue
Block a user