magic_device 0.0.2
magic_device: ^0.0.2 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 plain Dart over battery_plus with no native code of its own; system-wide idle on macOS and Windows only. WindowFullscreen is macOS only and reports available == false elsewhere. |
| 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. 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(television: () => isTelevision);
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. Pass a television predicate and charging answers true without asking the platform. The default is () => false.
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(). |
Pass television: () => isTelevision on Android TV |
A batteryless TV reports unknown, which reads 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.