using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;
using Flow.Launcher.Plugin.SharedModels;
using JetBrains.Annotations;
namespace Flow.Launcher.Plugin
{
///
/// Public APIs that plugin can use
///
public interface IPublicAPI
{
///
/// Change Flow.Launcher query.
/// When current results are from context menu or history, it will go back to query results before changing query.
///
/// query text
///
/// Force requery. By default, Flow Launcher will not fire query if your query is same with existing one.
/// Set this to to force Flow Launcher requerying
///
void ChangeQuery(string query, bool requery = false);
///
/// Restart Flow Launcher
///
void RestartApp();
///
/// Run a shell command
///
/// The command or program to run
/// the shell type to run, e.g. powershell.exe
/// Thrown when unable to find the file specified in the command
/// Thrown when error occurs during the execution of the command
void ShellRun(string cmd, string filename = "cmd.exe");
///
/// Copies the passed in text and shows a message indicating whether the operation was completed successfully.
/// When directCopy is set to true and passed in text is the path to a file or directory,
/// the actual file/directory will be copied to clipboard. Otherwise the text itself will still be copied to clipboard.
///
/// Text to save on clipboard
/// When true it will directly copy the file/folder from the path specified in text
/// Whether to show the default notification from this method after copy is done.
/// It will show file/folder/text is copied successfully.
/// Turn this off to show your own notification after copy is done.>
public void CopyToClipboard(string text, bool directCopy = false, bool showDefaultNotification = true);
///
/// Save everything, all of Flow Launcher and plugins' data and settings
///
void SaveAppAllSettings();
///
/// Save all Flow's plugins settings
///
void SavePluginSettings();
///
/// Reloads any Plugins that have the
/// IReloadable implemented. It refeshes
/// Plugin's in memory data with new content
/// added by user.
///
Task ReloadAllPluginData();
///
/// Check for new Flow Launcher update
///
void CheckForNewUpdate();
///
/// Show the error message using Flow's standard error icon.
///
/// Message title
/// Optional message subtitle
void ShowMsgError(string title, string subTitle = "");
///
/// Show the MainWindow when hiding
///
void ShowMainWindow();
///
/// Focus the query text box in the main window
///
void FocusQueryTextBox();
///
/// Hide MainWindow
///
void HideMainWindow();
///
/// Representing whether the main window is visible
///
///
bool IsMainWindowVisible();
///
/// Invoked when the visibility of the main window has changed. Currently, the plugin will continue to be subscribed even if it is turned off.
///
event VisibilityChangedEventHandler VisibilityChanged;
///
/// Show message box
///
/// Message title
/// Message subtitle
/// Message icon path (relative path to your plugin folder)
void ShowMsg(string title, string subTitle = "", string iconPath = "");
///
/// Show message box
///
/// Message title
/// Message subtitle
/// Message icon path (relative path to your plugin folder)
/// when true will use main windows as the owner
void ShowMsg(string title, string subTitle, string iconPath, bool useMainWindowAsOwner = true);
///
/// Open setting dialog
///
void OpenSettingDialog();
///
/// Get translation of current language
/// You need to implement IPluginI18n if you want to support multiple languages for your plugin
///
///
///
string GetTranslation(string key);
///
/// Get all loaded plugins
///
///
List GetAllPlugins();
///
/// Registers a callback function for global keyboard events.
///
///
/// The callback function to invoke when a global keyboard event occurs.
///
/// Parameters:
///
/// - int: The type of (key down, key up, etc.)
/// - int: The virtual key code of the pressed/released key
/// - : The state of modifier keys (Ctrl, Alt, Shift, etc.)
///
///
///
/// Returns: true to allow normal system processing of the key event,
/// or false to intercept and prevent default handling.
///
///
///
/// This callback will be invoked for all keyboard events system-wide.
/// Use with caution as intercepting system keys may affect normal system operation.
///
public void RegisterGlobalKeyboardCallback(Func callback);
///
/// Remove a callback for Global Keyboard Event
///
///
/// The callback function to invoke when a global keyboard event occurs.
///
/// Parameters:
///
/// - int: The type of (key down, key up, etc.)
/// - int: The virtual key code of the pressed/released key
/// - : The state of modifier keys (Ctrl, Alt, Shift, etc.)
///
///
///
/// Returns: true to allow normal system processing of the key event,
/// or false to intercept and prevent default handling.
///
///
public void RemoveGlobalKeyboardCallback(Func callback);
///
/// Fuzzy Search the string with the given query. This is the core search mechanism Flow uses
///
/// Query string
/// The string that will be compared against the query
/// Match results
MatchResult FuzzySearch(string query, string stringToCompare);
///
/// Http download the spefic url and return as string
///
/// URL to call Http Get
/// Cancellation Token
/// Task to get string result
Task HttpGetStringAsync(string url, CancellationToken token = default);
///
/// Http download the spefic url and return as stream
///
/// URL to call Http Get
/// Cancellation Token
/// Task to get stream result
Task HttpGetStreamAsync(string url, CancellationToken token = default);
///
/// Download the specific url to a cretain file path
///
/// URL to download file
/// path to save downloaded file
///
/// Action to report progress. The input of the action is the progress value which is a double value between 0 and 100.
/// It will be called if url support range request and the reportProgress is not null.
///
/// place to store file
/// Task showing the progress
Task HttpDownloadAsync([NotNull] string url, [NotNull] string filePath, Action reportProgress = null, CancellationToken token = default);
///
/// Add ActionKeyword and update action keyword metadata for specific plugin.
/// Before adding, please check if action keyword is already assigned by
///
/// ID for plugin that needs to add action keyword
/// The actionkeyword that is supposed to be added
///
/// If new action keyword contains any whitespace, FL will still add it but it will not work for users.
/// So plugin should check the whitespace before calling this function.
///
void AddActionKeyword(string pluginId, string newActionKeyword);
///
/// Remove ActionKeyword and update action keyword metadata for specific plugin
///
/// ID for plugin that needs to remove action keyword
/// The actionkeyword that is supposed to be removed
void RemoveActionKeyword(string pluginId, string oldActionKeyword);
///
/// Check whether specific ActionKeyword is assigned to any of the plugin
///
/// The actionkeyword for checking
/// True if the actionkeyword is already assigned, False otherwise
bool ActionKeywordAssigned(string actionKeyword);
///
/// Log debug message
/// Message will only be logged in Debug mode
///
void LogDebug(string className, string message, [CallerMemberName] string methodName = "");
///
/// Log info message
///
void LogInfo(string className, string message, [CallerMemberName] string methodName = "");
///
/// Log warning message
///
void LogWarn(string className, string message, [CallerMemberName] string methodName = "");
///
/// Log error message. Preferred error logging method for plugins.
///
void LogError(string className, string message, [CallerMemberName] string methodName = "");
///
/// Log an Exception. Will throw if in debug mode so developer will be aware,
/// otherwise logs the eror message. This is the primary logging method used for Flow
///
void LogException(string className, string message, Exception e, [CallerMemberName] string methodName = "");
///
/// Load JsonStorage for current plugin's setting. This is the method used to load settings from json in Flow.
/// When the file is not exist, it will create a new instance for the specific type.
///
/// Type for deserialization
///
T LoadSettingJsonStorage() where T : new();
///
/// Save JsonStorage for current plugin's setting. This is the method used to save settings to json in Flow.
/// This method will save the original instance loaded with LoadJsonStorage.
/// This API call is for manually Save.
/// Flow will automatically save all setting type that has called or previously.
///
/// Type for Serialization
///
void SaveSettingJsonStorage() where T : new();
///
/// Open directory in an explorer configured by user via Flow's Settings. The default is Windows Explorer
///
/// Directory Path to open
/// Extra FileName Info
public void OpenDirectory(string DirectoryPath, string FileNameOrFilePath = null);
///
/// Opens the URL with the given Uri object.
/// The browser and mode used is based on what's configured in Flow's default browser settings.
///
public void OpenUrl(Uri url, bool? inPrivate = null);
///
/// Opens the URL with the given string.
/// The browser and mode used is based on what's configured in Flow's default browser settings.
/// Non-C# plugins should use this method.
///
public void OpenUrl(string url, bool? inPrivate = null);
///
/// Opens the application URI with the given Uri object, e.g. obsidian://search-query-example
///
public void OpenAppUri(Uri appUri);
///
/// Opens the application URI with the given string, e.g. obsidian://search-query-example
/// Non-C# plugins should use this method
///
public void OpenAppUri(string appUri);
///
/// Toggles Game Mode. off -> on and backwards
///
public void ToggleGameMode();
///
/// Switches Game Mode to given value
///
/// New Game Mode status
public void SetGameMode(bool value);
///
/// Representing Game Mode status
///
///
public bool IsGameModeOn();
///
/// Reloads the query.
/// When current results are from context menu or history, it will go back to query results before requerying.
///
/// Choose the first result after reload if true; keep the last selected result if false. Default is true.
public void ReQuery(bool reselect = true);
///
/// Back to the query results.
/// This method should run when selected item is from context menu or history.
///
public void BackToQueryResults();
///
/// Displays a standardised Flow message box.
///
/// The message of the message box.
/// The caption of the message box.
/// Specifies which button or buttons to display.
/// Specifies the icon to display.
/// Specifies the default result of the message box.
/// Specifies which message box button is clicked by the user.
public MessageBoxResult ShowMsgBox(string messageBoxText, string caption = "", MessageBoxButton button = MessageBoxButton.OK, MessageBoxImage icon = MessageBoxImage.None, MessageBoxResult defaultResult = MessageBoxResult.OK);
///
/// Displays a standardised Flow progress box.
///
/// The caption of the progress box.
///
/// Time-consuming task function, whose input is the action to report progress.
/// The input of the action is the progress value which is a double value between 0 and 100.
/// If there are any exceptions, this action will be null.
///
/// When user cancel the progress, this action will be called.
///
public Task ShowProgressBoxAsync(string caption, Func, Task> reportProgressAsync, Action cancelProgress = null);
///
/// Start the loading bar in main window
///
public void StartLoadingBar();
///
/// Stop the loading bar in main window
///
public void StopLoadingBar();
///
/// Get all available themes
///
///
public List GetAvailableThemes();
///
/// Get the current theme
///
///
public ThemeData GetCurrentTheme();
///
/// Set the current theme
///
///
///
/// True if the theme is set successfully, false otherwise.
///
public bool SetCurrentTheme(ThemeData theme);
///
/// Save all Flow's plugins caches
///
void SavePluginCaches();
///
/// Load BinaryStorage for current plugin's cache. This is the method used to load cache from binary in Flow.
/// When the file is not exist, it will create a new instance for the specific type.
///
/// Type for deserialization
/// Cache file name
/// Cache directory from plugin metadata
/// Default data to return
///
///
/// BinaryStorage utilizes MemoryPack, which means the object must be MemoryPackSerializable
///
Task LoadCacheBinaryStorageAsync(string cacheName, string cacheDirectory, T defaultData) where T : new();
///
/// Save BinaryStorage for current plugin's cache. This is the method used to save cache to binary in Flow.
/// This method will save the original instance loaded with LoadCacheBinaryStorageAsync.
/// This API call is for manually Save.
/// Flow will automatically save all cache type that has called or previously.
///
/// Type for Serialization
/// Cache file name
/// Cache directory from plugin metadata
///
///
/// BinaryStorage utilizes MemoryPack, which means the object must be MemoryPackSerializable
///
Task SaveCacheBinaryStorageAsync(string cacheName, string cacheDirectory) where T : new();
///
/// Load image from path.
/// Support local, remote and data:image url.
/// Support png, jpg, jpeg, gif, bmp, tiff, ico, svg image files.
/// If image path is missing, it will return a missing icon.
///
/// The path of the image.
///
/// Load full image or not.
///
///
/// Cache the image or not. Cached image will be stored in FL cache.
/// If the image is just used one time, it's better to set this to false.
///
///
ValueTask LoadImageAsync(string path, bool loadFullImage = false, bool cacheImage = true);
///
/// Update the plugin manifest
///
///
/// FL has multiple urls to download the plugin manifest. Set this to true to only use the primary url.
///
///
/// True if the manifest is updated successfully, false otherwise
public Task UpdatePluginManifestAsync(bool usePrimaryUrlOnly = false, CancellationToken token = default);
///
/// Get the plugin manifest.
///
///
/// If Flow cannot get manifest data, this could be null
///
///
public IReadOnlyList GetPluginManifest();
///
/// Check if the plugin has been modified.
/// If this plugin is updated, installed or uninstalled and users do not restart the app,
/// it will be marked as modified
///
/// Plugin id
///
public bool PluginModified(string id);
///
/// Update a plugin to new version, from a zip file. By default will remove the zip file if update is via url,
/// unless it's a local path installation
///
/// The metadata of the old plugin to update
/// The new plugin to update
///
/// Path to the zip file containing the plugin. It will be unzipped to the temporary directory, removed and installed.
///
///
public Task UpdatePluginAsync(PluginMetadata pluginMetadata, UserPlugin plugin, string zipFilePath);
///
/// Install a plugin. By default will remove the zip file if installation is from url,
/// unless it's a local path installation
///
/// The plugin to install
///
/// Path to the zip file containing the plugin. It will be unzipped to the temporary directory, removed and installed.
///
public void InstallPlugin(UserPlugin plugin, string zipFilePath);
///
/// Uninstall a plugin
///
/// The metadata of the plugin to uninstall
///
/// Plugin has their own settings. If this is set to true, the plugin settings will be removed.
///
///
public Task UninstallPluginAsync(PluginMetadata pluginMetadata, bool removePluginSettings = false);
///
/// Log debug message of the time taken to execute a method
/// Message will only be logged in Debug mode
///
/// The time taken to execute the method in milliseconds
public long StopwatchLogDebug(string className, string message, Action action, [CallerMemberName] string methodName = "");
///
/// Log debug message of the time taken to execute a method asynchronously
/// Message will only be logged in Debug mode
///
/// The time taken to execute the method in milliseconds
public Task StopwatchLogDebugAsync(string className, string message, Func action, [CallerMemberName] string methodName = "");
///
/// Log info message of the time taken to execute a method
///
/// The time taken to execute the method in milliseconds
public long StopwatchLogInfo(string className, string message, Action action, [CallerMemberName] string methodName = "");
///
/// Log info message of the time taken to execute a method asynchronously
///
/// The time taken to execute the method in milliseconds
public Task StopwatchLogInfoAsync(string className, string message, Func action, [CallerMemberName] string methodName = "");
}
}