magic_device 1.1.0
magic_device: ^1.1.0 copied to clipboard
Gate background work in Flutter: detect mains power (charging), viewer idle and macOS window fullscreen. One import, no setup, no Runner edits.
Magic Device
Know when it is safe to run the heavy job.
Whether the device is on mains power, whether the viewer is away, and whether the macOS window is fullscreen. One import, no setup.
Documentation · pub.dev · Issues
At a glance #
| Fact | Value |
|---|---|
| Package | magic_device |
| Install | flutter pub add magic_device |
| Import | package:magic_device/magic_device.dart |
| Entry points | DevicePower (mains power, viewer away), WindowFullscreen (macOS window state) |
| Platforms | DevicePower is Dart over battery_plus, plus a small Android plugin that tells it whether the device is a television; system-wide idle on native macOS and Windows only. WindowFullscreen is macOS only and reports available == false elsewhere. The pubspec declares Android, iOS, Linux, macOS and Windows; a web build runs the same Dart code, but web is not declared, so the pub.dev page carries no web tag. |
| Requires | Dart >=3.6, Flutter >=3.27 |
| Dependencies | battery_plus, flutter_inactive_timer; WindowFullscreen uses the Flutter SDK only |
| License | MIT |
Install #
flutter pub add magic_device
DevicePower needs no setup: its Android plugin registers itself, so MainActivity stays untouched. WindowFullscreen registers its own macOS plugin, so a macOS host needs no edit to its Runner either.
DevicePower #
Two hardware signals a background job can gate on: whether the device draws from the mains, and whether the viewer has been away long enough that the work would not disturb them.
import 'package:magic_device/magic_device.dart';
final DevicePower power = DevicePower.instance;
power.start();
if (await power.charging && power.idle) {
// Safe to run the heavy job.
}
power.stop();
| Signal | Meaning |
|---|---|
charging |
On mains power. full, charging and connectedNotCharging count; discharging and unknown do not. A platform with no battery implementation answers false. |
idle |
Away. Native macOS and Windows read the system-wide last-input clock every pollInterval (30 seconds) and report idle after DevicePower.idleThreshold (2 minutes) without input. Every other platform, web included, reports idle whenever the app lifecycle is anything but resumed, immediately and with no threshold, read at start() and followed from there. |
Nothing is collected until start(), and stop() resets idle to false.
Televisions #
A batteryless Android TV reports unknown, which would read as not charging forever. On Android the default television reader asks the package's Android plugin (UiModeManager reports the television UI mode) and reuses a settled answer, and a television answers charging true without reading the battery, DevicePower.instance included. On every other platform the default television reader answers false without opening the channel, and charging then reads the battery state. Pass a television predicate to use another signal. In a testWidgets body pass television:, or a non-Android platform:: the platform reply never arrives under FakeAsync, and platform: TargetPlatform.android alone still asks the plugin.
Testing #
Every platform read except the app lifecycle is a constructor parameter: batteryState, idleDuration, platform and television. Drive the lifecycle through the test binding.
WindowFullscreen #
Whether the macOS window is fullscreen, and the one call that flips it. The package's macOS plugin registers the magic_device/window channel itself, so the host's MainFlutterWindow.swift stays Flutter's stock one.
import 'package:magic_device/magic_device.dart';
final WindowFullscreen fullscreen = WindowFullscreen.instance;
if (fullscreen.available) {
await fullscreen.toggle();
}
| Reading | Meaning |
|---|---|
available |
True on macOS only. Everywhere else toggle() sends nothing, so gate the control on it. |
active |
Fullscreen, as the window last confirmed (AppKit's did-notification). |
target |
Fullscreen or on its way there: moves on the will-notification, so a layout can animate with the window. |
transitioning |
Between the two: the window's size changes on every frame. |
The state follows the window, not the calls: the green button, Esc and the Window menu notify listeners just as toggle() does, and a window that opens already fullscreen is read once on first use.
toggle() is ignored while a press or an animation is under way, so a double press cannot undo itself. It throws a PlatformException (no_window) when the Flutter view is not in a window, and that refusal releases the press. AppKit silently ignores the call for an app that is not frontmost; when the window posts nothing within WindowFullscreen.pressTimeout (1 second), the press ends and the window's state is read again.
Use the one instance: the channel takes one handler. A view adds and removes a listener and never disposes it. A test calls WindowFullscreen.resetInstance() in tearDown.
The example/ app toggles the window from a button and logs each notification in order.
For AI coding agents #
Rules for an agent writing code against this package. Each one is a mistake the maintainers already made once.
| Do | Because |
|---|---|
Import only package:magic_device/magic_device.dart |
Everything under src/ is private. |
Use DevicePower.instance, or build one DevicePower(...) when you need to inject a reader; let one component own start() and stop() |
instance is shared and stop() has no reference count: it resets idle to false for every consumer. To fake or replace the battery read, pass batteryState: instead of calling Battery() yourself. |
await power.charging, read power.idle synchronously |
charging asks the platform; idle is cached after start(). |
Leave television at its default on Android TV; pass it only for another signal |
The default asks UiModeManager and reuses a settled answer. A batteryless TV reports unknown, which would read as not charging forever. |
Use WindowFullscreen.instance, add and remove a listener, never dispose() it, and gate the control on available |
The channel takes one handler, and disposing the shared instance leaves every other reader on a disposed notifier. Non-macOS platforms send nothing. |
Call WindowFullscreen.resetInstance() in tearDown; inject the four DevicePower readers in tests |
No mocking library is needed. |
Dependencies #
battery_plus for the battery state (Battery() is a singleton in battery_plus 7) and flutter_inactive_timer for the desktop idle clock, loaded through a conditional export so dart:ffi never reaches the web compiler. The web has no system-wide input clock. WindowFullscreen uses the Flutter SDK only.
Links #
- Documentation: installation,
DevicePower,WindowFullscreenand testing - API reference
- llms.txt and the agent skill for AI coding agents:
npx skills add anilcancakir/magic_device - Changelog
- Report an issue
- Security policy
License #
MIT, see LICENSE. Built by Anılcan Çakır.