* publish new versions * torch 2.0.0 --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Lucas Nogueira <lucas@tauri.app>
Torch
Control the device flashlight (torch) on Android and iOS.
On Android, turning on the torch while another component holds the camera normally fails. This plugin also works while a barcode scanner scan is running, so apps can offer a flashlight button in their scanning UI.
| Platform | Supported |
|---|---|
| Linux | x |
| Windows | x |
| macOS | x |
| Android | ✓ |
| iOS | ✓ |
Install
This plugin requires a Rust version of at least 1.90
There are three general methods of installation that we can recommend.
- Use crates.io and npm (easiest, and requires you to trust that our publishing pipeline worked)
- Pull sources directly from Github using git tags / revision hashes (most secure)
- Git submodule install this repo in your tauri project and then use file protocol to ingest the source (most secure, but inconvenient to use)
Install the Core plugin by adding the following to your Cargo.toml file:
src-tauri/Cargo.toml
[dependencies]
tauri-plugin-torch = "2.0.0"
# alternatively with Git:
tauri-plugin-torch = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "v2" }
You can install the JavaScript Guest bindings using your preferred JavaScript package manager:
pnpm add @tauri-apps/plugin-torch
# or
npm add @tauri-apps/plugin-torch
# or
yarn add @tauri-apps/plugin-torch
Usage
First you need to register the core plugin with Tauri:
src-tauri/src/lib.rs
fn main() {
tauri::Builder::default()
.plugin(tauri_plugin_torch::init())
.run(tauri::generate_context!())
.expect("error while running tauri application");
}
Second, add the required permissions in the project:
src-tauri/capabilities/default.json
"permissions": ["torch:default"]
Afterwards all the plugin's APIs are available through the JavaScript guest bindings:
import {
enable,
disable,
toggle,
isAvailable,
isEnabled
} from '@tauri-apps/plugin-torch'
if (await isAvailable()) {
await enable()
console.log(await isEnabled()) // true
await toggle() // returns false, the torch is now off
await disable()
}
isEnabled reads the hardware state, so it also reflects the torch being turned off by the system, another app, or a barcode scan ending.
On iOS, apps can't see the torch state set by Control Center or another app: while the flashlight is on from there, isEnabled returns false and toggle tries to turn the torch on.
Contributing
PRs accepted. Please make sure to read the Contributing Guide before making a pull request.
Partners
|
|
For the complete list of sponsors please visit our website and Open Collective.
License
Code: (c) 2015 - Present - The Tauri Programme within The Commons Conservancy.
MIT or MIT/Apache 2.0 where applicable.