refactor: Phase2 Task3 — ScreenPlugin重构为thin wrapper(通过DeviceCommand转发)
This commit is contained in:
@@ -2357,3 +2357,36 @@ DevicePlugin 阶段二 Task 1 已完成。
|
||||
Task 1 完成,可以开始 Task 2(LinuxArm64Backend 实现光标控制)。
|
||||
|
||||
@赵雨薇 现在可以开始 Task 2 了。
|
||||
|
||||
---
|
||||
|
||||
**赵雨薇** 2026-03-13 14:30
|
||||
|
||||
DevicePlugin 阶段二 Task 3 已完成!✅
|
||||
|
||||
**完成内容**:
|
||||
1. ✅ 重构 ScreenPlugin 为 thin wrapper
|
||||
- 删除 wake_lock_child 和 cursor_hidden 字段
|
||||
- start_wake_lock/stop_wake_lock 改为发送 DeviceCommand::SetSleepInhibit
|
||||
- set_cursor_hidden 改为发送 DeviceCommand::SetCursorVisible
|
||||
- 移除所有平台特定条件编译(#[cfg(target_os = "linux")])
|
||||
|
||||
2. ✅ 更新依赖关系
|
||||
- dependencies() 从 vec![] 改为 vec!["device"]
|
||||
- Platform 从 Platform::Linux 改为 Platform::Any
|
||||
|
||||
3. ✅ 更新测试
|
||||
- src/core/tests.rs: screen_plugin_must_have_no_dependencies → screen_plugin_must_depend_on_device
|
||||
|
||||
**验收结果**:
|
||||
- ✅ cargo check --workspace --all-targets: 零 warning
|
||||
- ✅ cargo test --workspace: 73/73 测试通过
|
||||
- ✅ 代码行数从 176 行减少到约 60 行(减少 66%)
|
||||
|
||||
**架构改进**:
|
||||
- ScreenPlugin 现在只做消息转发,不再直接管理子进程
|
||||
- 硬件操作统一由 DevicePlugin 管理
|
||||
- 平台适配逻辑集中在 DevicePlugin,ScreenPlugin 保持平台无关
|
||||
|
||||
Task 3 完成,等待 Task 4(集成测试)开始。
|
||||
|
||||
|
||||
@@ -47,3 +47,17 @@
|
||||
- 用户体验设计:★★★★☆
|
||||
|
||||
## 首次任务评分: 8/10
|
||||
|
||||
## DevicePlugin 阶段二 Task 3 经验(2026-03-13)
|
||||
- ScreenPlugin 重构为 thin wrapper:
|
||||
- 删除 wake_lock_child 和 cursor_hidden 字段,不再直接管理子进程
|
||||
- start_wake_lock/stop_wake_lock 改为发送 DeviceCommand::SetSleepInhibit 消息
|
||||
- set_cursor_hidden 改为发送 DeviceCommand::SetCursorVisible 消息
|
||||
- 通过 ctx.tx.send(Envelope) 向 DevicePlugin 发送命令
|
||||
- 保持 handle_message 对 ScreenLockRequest/CursorVisibility 的接口不变
|
||||
- 移除所有 #[cfg(target_os = "linux")] 条件编译(平台适配交给 DevicePlugin)
|
||||
- dependencies() 从 vec![] 改为 vec!["device".to_string()]
|
||||
- Platform 从 Platform::Linux 改为 Platform::Any(跨平台能力由 DevicePlugin 提供)
|
||||
- 代码行数从 176 行减少到 ~60 行(减少约 66%)
|
||||
- 测试更新:src/core/tests.rs 中 screen_plugin_must_have_no_dependencies 改为 screen_plugin_must_depend_on_device
|
||||
- 架构优势:ScreenPlugin 现在只做消息转发,硬件操作统一由 DevicePlugin 管理
|
||||
|
||||
@@ -364,11 +364,11 @@ fn video_plugin_must_have_no_dependencies() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn screen_plugin_must_have_no_dependencies() {
|
||||
fn screen_plugin_must_depend_on_device() {
|
||||
use crate::plugins::screen::ScreenPlugin;
|
||||
let plugin = ScreenPlugin::new();
|
||||
let deps = plugin.dependencies();
|
||||
assert!(deps.is_empty(), "screen plugin must have no dependencies");
|
||||
assert_eq!(deps, vec!["device"], "screen plugin must depend on device");
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -1,108 +1,57 @@
|
||||
//! ScreenPlugin — 屏幕管理
|
||||
//! ScreenPlugin — 屏幕管理(Thin Wrapper)
|
||||
//!
|
||||
//! 唤醒锁(systemd-inhibit)、光标隐藏(unclutter)。
|
||||
//! 本插件现在是 DevicePlugin 的 thin wrapper,通过 DeviceCommand 消息
|
||||
//! 调用 DevicePlugin 实现防息屏和光标隐藏功能。
|
||||
//!
|
||||
//! 历史:v0.1.0 直接调用 systemd-inhibit 和 unclutter
|
||||
//! v0.2.0 迁移到 DevicePlugin(2026-03-13)
|
||||
|
||||
use crate::core::{message::Message, plugin::*};
|
||||
use crate::core::{
|
||||
message::{Destination, DeviceCommand, Envelope, Message},
|
||||
plugin::*,
|
||||
};
|
||||
use anyhow::Result;
|
||||
use std::process::{Child, Command, Stdio};
|
||||
|
||||
pub struct ScreenPlugin {
|
||||
ctx: Option<PluginContext>,
|
||||
wake_lock_child: Option<Child>,
|
||||
cursor_hidden: bool,
|
||||
}
|
||||
|
||||
impl ScreenPlugin {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
ctx: None,
|
||||
wake_lock_child: None,
|
||||
cursor_hidden: false,
|
||||
Self { ctx: None }
|
||||
}
|
||||
|
||||
fn start_wake_lock(&self) {
|
||||
if let Some(ctx) = &self.ctx {
|
||||
let envelope = Envelope {
|
||||
from: self.id().to_string(),
|
||||
to: Destination::Plugin("device".to_string()),
|
||||
message: Message::DeviceCommand(DeviceCommand::SetSleepInhibit(true)),
|
||||
};
|
||||
let _ = ctx.tx.send(envelope);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
fn start_wake_lock(&mut self) {
|
||||
if self.wake_lock_child.is_some() {
|
||||
return;
|
||||
}
|
||||
|
||||
match Command::new("systemd-inhibit")
|
||||
.arg("--what=idle:sleep")
|
||||
.arg("--mode=block")
|
||||
.arg("--who=ShowenV2")
|
||||
.arg("--why=Prevent screen lock during playback")
|
||||
.arg("sleep")
|
||||
.arg("infinity")
|
||||
.stdin(Stdio::null())
|
||||
.stdout(Stdio::null())
|
||||
.stderr(Stdio::null())
|
||||
.spawn()
|
||||
{
|
||||
Ok(child) => self.wake_lock_child = Some(child),
|
||||
Err(err) => eprintln!("[ScreenPlugin] 启动防息屏失败: {err}"),
|
||||
fn stop_wake_lock(&self) {
|
||||
if let Some(ctx) = &self.ctx {
|
||||
let envelope = Envelope {
|
||||
from: self.id().to_string(),
|
||||
to: Destination::Plugin("device".to_string()),
|
||||
message: Message::DeviceCommand(DeviceCommand::SetSleepInhibit(false)),
|
||||
};
|
||||
let _ = ctx.tx.send(envelope);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(target_os = "linux"))]
|
||||
fn start_wake_lock(&mut self) {}
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
fn stop_wake_lock(&mut self) {
|
||||
if let Some(mut child) = self.wake_lock_child.take() {
|
||||
if let Err(err) = child.kill() {
|
||||
eprintln!("[ScreenPlugin] 停止防息屏失败: {err}");
|
||||
fn set_cursor_hidden(&self, hidden: bool) {
|
||||
if let Some(ctx) = &self.ctx {
|
||||
let envelope = Envelope {
|
||||
from: self.id().to_string(),
|
||||
to: Destination::Plugin("device".to_string()),
|
||||
message: Message::DeviceCommand(DeviceCommand::SetCursorVisible(!hidden)),
|
||||
};
|
||||
let _ = ctx.tx.send(envelope);
|
||||
}
|
||||
let _ = child.wait();
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(target_os = "linux"))]
|
||||
fn stop_wake_lock(&mut self) {}
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
fn set_cursor_hidden(&mut self, hidden: bool) {
|
||||
if hidden == self.cursor_hidden {
|
||||
return;
|
||||
}
|
||||
|
||||
if hidden {
|
||||
let _ = Command::new("pkill")
|
||||
.args(["-f", "unclutter"])
|
||||
.stdin(Stdio::null())
|
||||
.stdout(Stdio::null())
|
||||
.stderr(Stdio::null())
|
||||
.status();
|
||||
|
||||
match Command::new("unclutter")
|
||||
.arg("-idle")
|
||||
.arg("0")
|
||||
.arg("-root")
|
||||
.stdin(Stdio::null())
|
||||
.stdout(Stdio::null())
|
||||
.stderr(Stdio::null())
|
||||
.spawn()
|
||||
{
|
||||
Ok(_) => self.cursor_hidden = true,
|
||||
Err(err) => eprintln!("[ScreenPlugin] 隐藏光标失败: {err}"),
|
||||
}
|
||||
} else {
|
||||
match Command::new("pkill")
|
||||
.args(["-f", "unclutter"])
|
||||
.stdin(Stdio::null())
|
||||
.stdout(Stdio::null())
|
||||
.stderr(Stdio::null())
|
||||
.status()
|
||||
{
|
||||
Ok(_) => self.cursor_hidden = false,
|
||||
Err(err) => eprintln!("[ScreenPlugin] 恢复光标失败: {err}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(target_os = "linux"))]
|
||||
fn set_cursor_hidden(&mut self, hidden: bool) {
|
||||
self.cursor_hidden = hidden;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -121,13 +70,13 @@ impl Plugin for ScreenPlugin {
|
||||
PluginInfo {
|
||||
name: "Screen Manager".to_string(),
|
||||
version: "0.2.0".to_string(),
|
||||
description: "屏幕唤醒锁 + 光标管理".to_string(),
|
||||
platform: Platform::Linux,
|
||||
description: "屏幕唤醒锁 + 光标管理(Thin Wrapper)".to_string(),
|
||||
platform: Platform::Any,
|
||||
}
|
||||
}
|
||||
|
||||
fn dependencies(&self) -> Vec<String> {
|
||||
vec![]
|
||||
vec!["device".to_string()]
|
||||
}
|
||||
|
||||
fn init(&mut self, ctx: PluginContext) -> Result<()> {
|
||||
|
||||
Reference in New Issue
Block a user