API 参考
TimeLens 前后端通过 Tauri IPC 通信。本文档列出所有可用的 Rust Command 接口,以及前端 tauriApi.ts 中的对应封装。
监控 (Monitor)
| Command | 参数 | 返回 | 说明 |
get_monitor_status |
— |
MonitorStatus |
获取当前监控状态(是否活跃、当前应用名) |
set_monitoring_active |
active: boolean |
void |
暂停或恢复屏幕时间追踪 |
事件监听
import { listen } from "@tauri-apps/api/event";
const unlisten = await listen("active-window-changed", (event) => {
console.log("前台窗口变更:", event.payload);
});
unlisten();
屏幕时间统计
| Command | 参数 | 返回 | 说明 |
get_today_app_totals | — | AppUsageSummary[] | 今日各应用总使用时长 |
get_app_totals_for_date | date: string | AppUsageSummary[] | 指定日期各应用总时长 |
get_app_totals_in_range | startDate, endDate | AppUsageSummary[] | 日期范围内各应用总时长 |
get_app_comparison_in_ranges | currentStart, currentEnd, previousStart, previousEnd | AppUsageComparison[] | 两段时间范围对比数据 |
get_today_hourly | — | HourlyDistribution[] | 今日 0-23 点每小时使用时长 |
get_hourly_distribution_for_date | date: string | HourlyDistribution[] | 指定日期的小时分布 |
get_recent_daily_totals | days: number | DailyUsage[] | 最近 N 天每日总时长 |
get_recent_daily_totals_range | startDate, endDate | DailyUsage[] | 指定日期范围每日总时长 |
get_app_usage_page | startDate?, endDate?, limit, offset | AppUsagePage | 分页查询原始使用记录 |
分类管理
| Command | 参数 | 返回 | 说明 |
get_app_categories | — | AppCategoryRule[] | 所有应用分类规则 |
upsert_app_category | appName, exePath, category, source | void | 添加或更新分类规则 |
remove_app_category | exePath: string | void | 删除某应用的分类规则 |
suggest_category_for_app | appName, exePath | CategorySuggestion | AI 智能推荐应用分类 |
get_category_totals_in_range | startDate, endDate | CategoryUsageSummary[] | 日期范围内各分类总时长 |
get_category_daily_totals_in_range | startDate, endDate | CategoryDailyUsage[] | 日期范围内每日各分类时长 |
get_app_category_map | — | Record | exe_path → category 映射表 |
目标与限额
| Command | 参数 | 返回 | 说明 |
get_usage_goals | — | UsageGoal[] | 所有使用目标 |
save_usage_goal | goal: UsageGoal | UsageGoal | 保存目标(新增或更新) |
remove_usage_goal | id: number | void | 删除目标 |
get_goal_progress | weekStartDay?: number | GoalProgress[] | 各目标当前进度 |
get_ignored_apps | — | string[] | 被忽略的应用列表 |
set_ignored_apps | exePaths: string[] | void | 设置忽略应用列表 |
专注模式
| Command | 参数 | 返回 | 说明 |
set_focus_mode_active | active: boolean | void | 开启/关闭专注模式 |
get_focus_mode_active | — | boolean | 专注模式是否开启 |
start_focus_session | reason?: string, triggerType?: "manual" | "rule" | number | 开始专注会话,返回 session ID |
stop_focus_session | id: number | void | 结束指定专注会话 |
list_focus_sessions | startAt?, endAt? | FocusSession[] | 查询专注会话历史 |
| Command | 参数 | 返回 | 说明 |
get_widget_registry | — | WidgetRegistryResponse | 获取所有可用小组件列表 |
create_widget | widgetType: string | WidgetConfig | 创建小组件窗口 |
open_widget | config: WidgetConfig | void | 根据配置打开已有小组件 |
close_widget | id: string | void | 关闭小组件窗口 |
set_widget_always_on_top | id: string, mode: string | void | 设置置顶模式 |
get_all_widgets | — | WidgetConfig[] | 获取所有已保存的小组件配置 |
save_widget_config | config: WidgetConfig | void | 保存小组件配置到数据库 |
remove_widget_config | id: string | void | 删除小组件配置 |
待办事项
| Command | 参数 | 返回 | 说明 |
get_todos | — | TodoItem[] | 所有待办事项 |
add_todo | content: string | TodoItem | 添加待办 |
toggle_todo | id: number | void | 切换完成状态 |
delete_todo | id: number | void | 删除待办 |
reorder_todos | orderedIds: number[] | void | 批量排序 |
浏览器扩展
| Command | 参数 | 返回 | 说明 |
get_browser_extension_status | — | BrowserExtensionStatus | 扩展连接状态 |
set_browser_extension_enabled | enabled: boolean | void | 启用/禁用扩展 |
get_browser_domain_stats | startDate?, endDate? | BrowserDomainStats[] | 域名使用统计 |
get_browser_ignored_domains | — | string[] | 忽略的域名列表 |
set_browser_ignored_domains | hosts: string[] | void | 设置忽略域名 |
get_browser_domain_limits | — | BrowserDomainLimit[] | 域名限额列表 |
save_browser_domain_limit | host, dailyLimitSeconds, enabled | void | 保存域名限额 |
remove_browser_domain_limit | host: string | void | 删除域名限额 |
应用设置
| Command | 参数 | 返回 | 说明 |
get_app_settings | — | AppSettingsPayload | 所有应用设置 |
set_launch_at_startup | enabled: boolean | void | 开机自启动 |
set_silent_startup | enabled: boolean | void | 静默启动(最小化到托盘) |
set_auto_open_widgets | enabled: boolean | void | 启动时自动打开小组件 |
set_ignore_system_processes | enabled: boolean | void | 忽略系统进程 |
set_idle_time_policy | policy: "count" | "exclude" | void | 空闲时间处理策略 |
set_track_window_titles | enabled: boolean | void | 是否记录窗口标题 |
set_shortcuts | shortcuts: ShortcutSettings | void | 全局快捷键设置 |
get_install_channel_info | — | InstallChannelInfo | 安装渠道信息 |
生产力分析
| Command | 参数 | 返回 | 说明 |
get_productivity_score | date: string | ProductivityScore | 指定日期的生产力评分 |
get_productivity_score_range | startDate, endDate | ProductivityScore[] | 日期范围内的评分列表 |
get_interruption_periods | date: string | InterruptionPeriod[] | 每小时的中断次数和碎片化评分 |
数据导入导出
| Command | 参数 | 返回 | 说明 |
export_data_csv | — | string | 导出所有数据为 CSV 字符串 |
export_data_json | — | string | 导出所有数据为 JSON 字符串 |
import_data_json | payload: string | void | 导入 JSON 数据 |
系统通知
| Command | 参数 | 返回 | 说明 |
send_native_notification | title: string, body: string, alarm?: boolean | void | 发送系统原生通知 |
核心类型定义
所有类型定义位于 src/types/index.ts,以下是常用类型:
MonitorStatus
interface MonitorStatus {
active: boolean; // 监控是否运行中
current_app: string; // 当前前台应用名
current_exe_path: string; // 当前应用可执行文件路径
current_title: string; // 当前窗口标题
}
AppUsageSummary
interface AppUsageSummary {
app_name: string; // 应用名称
exe_path: string; // 可执行文件路径
total_seconds: number; // 总使用秒数
}
WidgetConfig
interface WidgetConfig {
id: string;
widget_type: string;
monitor_index: number;
x: number;
y: number;
width: number;
height: number;
opacity: number;
always_on_top_mode: "always" | "focus" | "never";
pinned: boolean;
start_on_launch: boolean;
}
UsageGoal
interface UsageGoal {
id?: number;
scope_type: "category" | "app"; // 目标范围:分类 或 单个应用
scope_value: string; // 分类名或应用名
period: "daily" | "weekly"; // 统计周期
operator: "at_least" | "at_most"; // 约束类型
target_seconds: number; // 目标秒数
enabled: boolean;
}
AppSettingsPayload
interface AppSettingsPayload {
launch_at_startup: boolean;
silent_startup: boolean;
auto_open_widgets: boolean;
ignore_system_processes: boolean;
idle_time_policy: "count" | "exclude";
track_window_titles: boolean;
browser_extension_enabled: boolean;
shortcuts: ShortcutSettings;
}
前端调用示例
import * as api from "@/services/tauriApi";
const todayStats = await api.getTodayAppTotals();
await api.setMonitoringActive(false);
const config = await api.createWidget("clock");
const unlisten = await api.onActiveWindowChanged((info) => {
console.log(info.app_name, info.window_title);
});
await api.setLaunchAtStartup(true);
最后更新:2025-05-03 · TimeLens v1.1.0