Plugin Context Calls
Plugin Context Calls
PluginContext provides plugins with the following capabilities:
Important
Some capabilities may require permissions, see the Plugin Permissions page
page: Current FletPageinstance,可用于构建 UI 控件或注册事件。register_navigation(view): Register a main navigation view (see below).register_route(view): Register an additional route, accessible throughpage.go().register_startup_hook(hook): Register a callback to execute after application startup.set_initial_route(route): Optionally change the initial route.logger:loguru.Loggerbound with plugin identifier, simply uselogger.info("...").metadata: Dictionary,可用于在插件及核心系统之间传递额外信息;也可通过context.add_metadata(key, value)辅助写入。- Global data interfaces:
register_data_namespace(identifier, *, description="", permission=None): Register namespace, declare data ownership and access permissions.publish_data(namespace_id, entry_id, payload): Write/update data entries in the namespace.latest_data(namespace_id)/get_data(namespace_id, entry_id)/list_data(namespace_id): Read shared data snapshots,需被授予命名空间所要求的权限。
- Storage path factories:
plugin_data_path,plugin_config_path,plugin_cache_pathand corresponding*_dirmethods for accessing/initializing plugin-specific data, config, cache directories.传入create=True时会确保目录存在。 add_bing_action(factory): Append custom buttons or other controls to the “Resources → Bing Daily” action row.add_spotlight_action(factory): Append controls to the “Resources → Windows Spotlight” action row.register_settings_page(label, builder, *, icon=None, button_label="Plugin Settings", description=None): Register plugin’s exclusive settings page.插件管理面板会在对应插件卡片中显示一个"插件设置"按钮,并在新页面中渲染builder返回的控件。- Favorites management interfaces (available after core plugin activation,需在 manifest 中声明并获批对应的
favorites_*权限;使用前可通过context.has_favorite_support()判断):context.favorites: ReturnsFavoriteServiceinstance,包裹了全部收藏操作并在内部执行权限校验。历史属性context.favorite_manager仍可用,但会返回同一服务实例。- Read:
favorites.list_folders()/favorites.get_folder(id)/favorites.list_items(folder_id="__all__")/favorites.get_item(item_id)/favorites.find_by_source(source)(requiresfavorites_read). - Write:
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(...)(requiresfavorites_write). - Import/export:
favorites.export_folders(target_path, folder_ids=None, include_assets=True),favorites.import_package(path),favorites.localize_items_from_files(mapping),favorites.localization_root()(requiresfavorites_export). Related data types (FavoriteSource,FavoriteItem, etc.) can be directly imported fromapp.plugins. Related data types (FavoriteSource,FavoriteItem, etc.) can be directly imported fromapp.plugins.
- System operation interfaces (return
PluginOperationResult,详见下文):open_route(route: str): Request navigation to specified route.需声明并获得app_route权限。switch_home(navigation_id: str): Switch homepage sidebar navigation item (e.g.,home,resource). Requiresapp_homepermission.open_settings_tab(tab: int): Open settings page and locate to specified tab (按设置tabs顺序,从0开始). Requiresapp_settingspermission.set_wallpaper(path: str): Set local image as system wallpaper. Requireswallpaper_controlpermission.ipc_broadcast(channel: str, payload: dict): Publish messages through cross-process broadcast. Requiresipc_broadcastpermission.ipc_subscribe(channel: str): Subscribe to cross-process broadcast channel,成功时结果的data为IPCSubscription对象,可调用.get()读取队列消息。ipc_unsubscribe(subscription_id: str): Unsubscribe,传入ipc_subscribe返回的订阅 ID。