magic-device
Skill3 starsChanged 17 days agoMITView on GitHub
Gate background work on mains power and viewer idle, and drive macOS window fullscreen, with the magic_device package.
1 other project uses a version of this.
Adopted by
Most recently taken up by
- anilcancakir/aiword for word, first seen Nov 16, 2026
Other versions1 project uses a version of this.
Word for word · 1
- anilcancakir/ai★ 3100% same
Edited · 0
None found.
Wrote their own version · 0
None found.
How matches are found. Word for word: the exact same text. Edited: shares passages with this file (“99% same” means 99% of its five-word phrases also appear here; 10% or more counts). Their own version: similar meaning, or, weakest, the same name. We can’t tell which project had it first. Projects are counted among the 200 most-starred candidates, and mentions are links to the repository we found, up to 50 per site, so both counts are a floor.
In this repository19 other files it gives its agents.
- CLAUDE.mdCLAUDE.md
- crumbsSkill
- dowelSkill
- flutter-reviewSkill
- magic-analyticsSkill
- magic-deeplinkSkill
- magic-devtoolsSkill
- magic-encryptionSkill
- magic-frameworkSkill
- magic-local-storageSkill
- magic-notificationsSkill
- magic-paymentsSkill
- magic-pickerSkill
- magic-sentrySkill
- magic-social-authSkill
- magic-socketSkill
- magic-starterSkill
- tapirSkill
- wind-uiSkill
The file
We split this skill into the rules it gives an agent
4 rules. About 0 are common, 0 are distinctive, and 4 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈0
Distinctive · estimate
This author's own choices. Worth a look.
4
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 40
Use WindowFullscreen.instance only.
- Very specific: line 43
Read the right state. active is what the window last confirmed, target moves on AppKit's will-notification (the did-notification settles it …
- Very specific: line 45
Treat a toggle() that changes nothing as normal, not as an error: AppKit ignores the call for an app that is not frontmost.
- Very specific: line 99
No mocking library is needed.
≈ “Common” is an estimate: at least 1 in 5 of the 408,460 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
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, and the package does not depend on magic: 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.
Install
flutter pub add magic_deviceRequires Dart >=3.6 and Flutter >=3.27. 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:
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
- Call
start()before you readidle. Nothing is collected until then,idlestays false, andstop()resets it to false so a stopped instance claims no stale signal. Outside native macOS and Windowsstart()attaches a lifecycle observer, so call it from the main isolate once Flutter's binding exists, not from a background isolate. chargingis aFuture<bool>that asks the platform on every read;idleis a cachedbool. Await the first, never await or cache the second.- Let one component own
start()andstop().DevicePower.instanceis shared andstop()has no reference count, so one consumer stopping it closes every other consumer's idle gate. DevicePower.instanceanswers for an Android TV with no setup: on Android the defaulttelevisionreader asks the package's Android plugin (UiModeManager, channelmagic_device/television) and reuses a settled answer, and every other platform answers false without asking. Do not carry a host-side TV detector; passtelevision:only for another signal or to fix the answer in a test. Underflutter_test(Android by default) a plaintest()reads false, and atestWidgetsbody hangs on the channel unless it passestelevision:or a non-Androidplatform:(platform: TargetPlatform.androidalone still asks it).- To fake or replace the battery read, pass
batteryState:instead of callingBattery()yourself;DevicePoweralready holds it. BatteryState.unknownis not mains: it is what a batteryless Android reports. Do not widen the rule; a television answers throughtelevision.full,chargingandconnectedNotChargingcount (a batteryless Mac reports the last). A platform with nobattery_plusimplementation answers false.- State what
idlemeans per platform. On native macOS and Windows it is the system-wide last-input clock, read atstart()and then everyDevicePower.pollInterval(30 seconds) againstDevicePower.idleThreshold(2 minutes). Everywhere else, web included, it means the app is not in front (any state butresumed, soinactivecounts), immediately and with no threshold, never "no touches for two minutes".
WindowFullscreen rules
- Use
WindowFullscreen.instanceonly. 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. - A view adds and removes a listener and never disposes the shared instance, or every other reader is left on a disposed notifier.
- Gate every control on
available. It is true on macOS only (never on web); elsewheretoggle()sends nothing. - Read the right state.
activeis what the window last confirmed,targetmoves on AppKit's will-notification (the did-notification settles it withactive) so a layout can animate with the window, andtransitioningcovers the gap while the window size changes on every frame. - Expect
toggle()to be ignored while a press or a transition is under way, so a double press cannot undo itself. Catch thePlatformExceptionwith codeno_windowwhere a user can trigger it. - Treat a
toggle()that changes nothing as normal, not as an error: AppKit ignores the call for an app that is not frontmost. - 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:
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:
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:
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.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

