插件上下文调用

PluginContext 向插件提供了以下能力:

Important

部分能力可能需要权限,详见 插件权限 页面

  • page: 当前 Flet Page 实例,可用于构建 UI 控件或注册事件。
  • register_navigation(view): 注册一个主导航视图(见下文)。
  • register_route(view): 注册一个附加路由,通过 page.go() 访问。
  • register_startup_hook(hook): 注册应用启动后执行的回调。
  • set_initial_route(route): 可选地改变初始路由。
  • logger: 已绑定插件 identifier 的 loguru.Logger,直接 logger.info("...") 即可。
  • metadata: 字典,可用于在插件及核心系统之间传递额外信息;也可通过 context.add_metadata(key, value) 辅助写入。
  • 全局数据接口:
    • register_data_namespace(identifier, *, description="", permission=None):注册命名空间,声明数据归属以及访问权限。
    • publish_data(namespace_id, entry_id, payload):写入 / 更新命名空间中的数据条目。
    • latest_data(namespace_id) / get_data(namespace_id, entry_id) / list_data(namespace_id):读取共享数据快照,需被授予命名空间所要求的权限。
  • 存储路径工厂:plugin_data_pathplugin_config_pathplugin_cache_path 以及对应的 *_dir 方法用于访问 / 初始化插件专属的数据、配置、缓存目录。传入 create=True 时会确保目录存在。
  • add_bing_action(factory): 在“资源 → Bing 每日”操作行追加自定义按钮或其他控件。
  • add_spotlight_action(factory): 在“资源 → Windows 聚焦”操作行追加控件。
  • register_settings_page(label, builder, *, icon=None, button_label="插件设置", description=None): 注册插件的专属设置页面。插件管理面板会在对应插件卡片中显示一个“插件设置”按钮,并在新页面中渲染 builder 返回的控件。
  • 收藏管理接口(核心插件激活后可用,需在 manifest 中声明并获批对应的 favorites_* 权限;使用前可通过 context.has_favorite_support() 判断):
    • context.favorites:返回 FavoriteService 实例,包裹了全部收藏操作并在内部执行权限校验。历史属性 context.favorite_manager 仍可用,但会返回同一服务实例。
    • 读取:favorites.list_folders() / favorites.get_folder(id) / favorites.list_items(folder_id="__all__") / favorites.get_item(item_id) / favorites.find_by_source(source)(需 favorites_read)。
    • 写入:favorites.create_folder(...)favorites.update_folder(...)favorites.delete_folder(...)favorites.add_or_update_item(...)favorites.update_item(...)favorites.remove_item(...)favorites.register_classifier(...)favorites.classify_item(...)(需 favorites_write)。
    • 导入导出:favorites.export_folders(target_path, folder_ids=None, include_assets=True)favorites.import_package(path)favorites.localize_items_from_files(mapping)favorites.localization_root()(需 favorites_export)。 相关数据类型(FavoriteSourceFavoriteItem 等)可直接从 app.plugins 导入。 相关数据类型(FavoriteSourceFavoriteItem 等)可直接从 app.plugins 导入。
  • 系统操作接口(返回 PluginOperationResult,详见下文):
    • open_route(route: str): 请求跳转到指定路由。需声明并获得 app_route 权限。
    • switch_home(navigation_id: str): 切换首页侧边栏导航项(例如 homeresource)。需 app_home 权限。
    • open_settings_tab(tab: int): 打开设置页并定位到指定标签(按设置tabs顺序,从0开始)。需 app_settings 权限。
    • set_wallpaper(path: str): 将本地图片设置为系统壁纸。需 wallpaper_control 权限。
    • ipc_broadcast(channel: str, payload: dict): 通过跨进程广播发布消息。需 ipc_broadcast 权限。
    • ipc_subscribe(channel: str): 订阅跨进程广播频道,成功时结果的 dataIPCSubscription 对象,可调用 .get() 读取队列消息。
    • ipc_unsubscribe(subscription_id: str): 取消订阅,传入 ipc_subscribe 返回的订阅 ID。