Skip to content
Back to skills

Magic Device

ASecurity

Gate background work on mains power and viewer idle, and drive macOS window fullscreen, with the magic_device package.

  • 3 stars
  • 0 votes
  • 0 copies
  • 0 views
  • Added November 14, 2026
ai-agentsswiftbashtesting

Works with

  • cli

Security analysis

A100/100

Scanned December 1, 2026

npx -y skills add anilcancakir/ai --skill magic-device --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Magic Device?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Magic Device
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/anilcancakir-magic-device/badge)](https://www.skillsdirectory.com/skills/anilcancakir-magic-device)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: magic-device
description: "Gate background work on mains power and viewer idle, and drive macOS window fullscreen, with the magic_device package."
version: 1.2.0
when_to_use: "Use when code imports package:magic_device or needs DevicePower, WindowFullscreen, a charging or away check, or a macOS fullscreen toggle."
---

# magic_device

Two classes answer three device questions. `DevicePower` says whether the device is on mains power and whether the viewer is away, so a heavy background job knows when it is safe to run. `WindowFullscreen` says whether the macOS window is fullscreen and flips it. There is no service provider, config file, facade or CLI command. The package depends on `magic` only for `MagicPlatform.isTvOS`: construct the classes where you need them (a controller, a provider's `boot()`, a background job).

This skill covers `magic_device` only. The Magic framework itself is the `magic-framework` skill; this package needs none of it beyond that one platform check.

## Install

```bash
flutter pub add magic_device
```

Requires Dart >=3.13 and Flutter >=3.47. There is no `plugin:install` step. The macOS plugin registers the `magic_device/window` channel by itself, so the host's `MainFlutterWindow.swift` stays Flutter's stock one; do not edit it. The Android plugin registers `magic_device/television` the same way, so `MainActivity` needs no edit either. Import only the barrel, because everything under `src/` is private:

```dart
import 'package:magic_device/magic_device.dart';
```

`BatteryState` is re-exported from the barrel, so a custom `batteryState` reader names the type without adding `battery_plus` to the app's own dependencies.

## DevicePower rules

1. Call `start()` before you read `idle`. Nothing is collected until then, `idle` stays false, and `stop()` resets it to false so a stopped instance claims no stale signal. Outside native macOS and Windows `start()` attaches a lifecycle observer, so call it from the main isolate once Flutter's binding exists, not from a background isolate.
2. `charging` is a `Future<bool>` that asks the platform on every read; `idle` is a cached `bool`. Await the first, never await or cache the second.
3. Let one component own `start()` and `stop()`. `DevicePower.instance` is shared and `stop()` has no reference count, so one consumer stopping it closes every other consumer's idle gate.
4. `DevicePower.instance` answers for an Android TV with no setup: on Android the default `television` reader asks the package's Android plugin (`UiModeManager`, channel `magic_device/television`) and reuses a settled answer, tvOS answers true without asking, and every other platform answers false without asking. Do not carry a host-side TV detector; pass `television:` only for another signal or to fix the answer in a test. Under `flutter_test` (Android by default) a plain `test()` reads false, and a `testWidgets` body hangs on the channel unless it passes `television:` or a non-Android `platform:` (`platform: TargetPlatform.android` alone still asks it).
5. To fake or replace the battery read, pass `batteryState:` instead of calling `Battery()` yourself; `DevicePower` already holds it.
6. `BatteryState.unknown` is not mains: it is what a batteryless Android reports. Do not widen the rule; a television answers through `television`. `full`, `charging` and `connectedNotCharging` count (a batteryless Mac reports the last). A platform with no `battery_plus` implementation answers false.
7. State what `idle` means per platform. On native macOS and Windows it is the system-wide last-input clock, read at `start()` and then every `DevicePower.pollInterval` (30 seconds) against `DevicePower.idleThreshold` (2 minutes). Everywhere else, web included, it means the app is not in front (any state but `resumed`, so `inactive` counts), immediately and with no threshold, never "no touches for two minutes".

## WindowFullscreen rules

1. Use `WindowFullscreen.instance` only. There is no public constructor, because the channel takes one handler and a second object would replace the first one's. The first use reads the window once, so a window that opens already fullscreen is picked up.
2. A view adds and removes a listener and never disposes the shared instance, or every other reader is left on a disposed notifier.
3. Gate every control on `available`. It is true on macOS only (never on web); elsewhere `toggle()` sends nothing.
4. Read the right state. `active` is what the window last confirmed, `target` moves on AppKit's will-notification (the did-notification settles it with `active`) so a layout can animate with the window, and `transitioning` covers the gap while the window size changes on every frame.
5. Expect `toggle()` to be ignored while a press or a transition is under way, so a double press cannot undo itself. Catch the `PlatformException` with code `no_window` where a user can trigger it.
6. Treat a `toggle()` that changes nothing as normal, not as an error: AppKit ignores the call for an app that is not frontmost.
7. The state follows the window, not the calls: the green button, Esc and the Window menu notify listeners exactly as `toggle()` does.

## Recipes

Gate a heavy job on mains power and an away viewer:

```dart
import 'package:magic_device/magic_device.dart';

Future<bool> safeToRunJob() async {
  final DevicePower power = DevicePower();
  power.start();

  try {
    return await power.charging && power.idle;
  } finally {
    power.stop();
  }
}
```

A fullscreen button that follows the window. `ListenableBuilder` adds and removes the listener itself, and the handler catches the one refusal a user can cause:

```dart
import 'package:flutter/material.dart';
import 'package:flutter/services.dart'; // PlatformException
import 'package:magic_device/magic_device.dart';

Widget fullscreenButton() {
  if (!WindowFullscreen.instance.available) return const SizedBox.shrink();

  return ListenableBuilder(
    listenable: WindowFullscreen.instance,
    builder: (BuildContext context, Widget? child) => IconButton(
      icon: Icon(
        WindowFullscreen.instance.active
            ? Icons.fullscreen_exit
            : Icons.fullscreen,
      ),
      onPressed: () async {
        try {
          await WindowFullscreen.instance.toggle();
        } on PlatformException catch (error) {
          if (error.code != 'no_window') rethrow;
        }
      },
    ),
  );
}
```

## Testing

No mocking library is needed. Every `DevicePower` platform read is a constructor parameter except the app lifecycle. `idleDuration` returns milliseconds:

```dart
import 'package:flutter/foundation.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:magic_device/magic_device.dart';

void main() {
  test('a discharging device with an away viewer', () async {
    final DevicePower power = DevicePower(
      batteryState: () async => BatteryState.discharging,
      idleDuration: () async => const Duration(minutes: 5).inMilliseconds,
      platform: TargetPlatform.macOS,
    );

    power.start();
    await power.refreshIdle(); // @visibleForTesting: drive the poll without waiting

    expect(await power.charging, isFalse);
    expect(power.idle, isTrue);

    power.stop();
  });
}
```

For the lifecycle path (any platform but native macOS and Windows), call `TestWidgetsFlutterBinding.ensureInitialized()` and then `binding.handleAppLifecycleStateChanged(AppLifecycleState.paused)`.

For `WindowFullscreen`, set `debugDefaultTargetPlatformOverride = TargetPlatform.macOS` in `setUp` and stand a fake window on the `magic_device/window` channel through `TestDefaultBinaryMessengerBinding`, because the first use of `instance` on macOS reads the window over that channel. In `tearDown` call `WindowFullscreen.resetInstance()` first, then clear the mock handler, then set the override back to null; the other order leaves the channel handler installed. Inside `testWidgets` end the body with `resetInstance()` and `debugDefaultTargetPlatformOverride = null` instead, because the binding checks the override before `tearDown` runs.

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in ai-agents →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.