window_focus 1.0.0 copy "window_focus: ^1.0.0" to clipboard
window_focus: ^1.0.0 copied to clipboard

Window Focus is a convenient plugin for Flutter that allows you to track user inactivity and obtain information about the title of the active window on Mac OS and Windows.

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';

import 'package:window_focus/window_focus.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String activeWindowTitle = 'Unknown';
  bool userIdle=false;
  final _windowFocusPlugin = WindowFocus();
  final _messangerKey = GlobalKey<ScaffoldMessengerState>();

  @override
  void initState() {
    super.initState();

    _windowFocusPlugin.addFocusChangeListener((p0) {
      setState(() {
        activeWindowTitle='${p0.appName}';
      });
    });
    _windowFocusPlugin.addUserActiveListener((p0) {
      setState(() {
        userIdle=p0;
      });
    });
  }


  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        scaffoldMessengerKey: _messangerKey,
        home: Scaffold(
          appBar: AppBar(
            title: const Text('Window in focus plugin example app'),
          ),
          body: Center(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  Text('Window title in focus: $activeWindowTitle\n'),
                  Text('User is idle: ${!userIdle}\n'),
                  TextButton(
                      onPressed: ()=>addInactivityThreshold(context),
                      child: Text('Add 5 seconds to the inactivity threshold')
                  )
                ],
              )
          ),
          bottomNavigationBar: TextButton(
            child: Text('Subscribe to my telegram channel @kotelnikoff_dev'),
            onPressed: ()async{
              if(await canLaunchUrl(Uri.parse('https://telegram.me/kotelnikoff_dev'))){
                await launchUrl(Uri.parse('https://telegram.me/kotelnikoff_dev'));
              }else{
                _messangerKey.currentState!.showSnackBar(
                    SnackBar(
                      content: Text('Oh! My Telegram Chanel @kotelnikoff_dev!'),
                    )
                );
              }
            }
          ),
        )
    );
  }
  void addInactivityThreshold(BuildContext context) async {
    try {
      Duration duration = await _windowFocusPlugin.idleThreshold;
      duration += const Duration(seconds: 5);
      _windowFocusPlugin.setIdleThreshold(duration: duration);

      _messangerKey.currentState!.showSnackBar(
          SnackBar(
            content: Text('Great! new inactivity threshold ${duration.inSeconds} seconds'),
          )
      );
    } catch (e, s) {
      print(e);
      print(s);
    }
  }
}
4
likes
130
points
20
downloads

Publisher

verified publisherkotelnikoff.dev

Weekly Downloads

Window Focus is a convenient plugin for Flutter that allows you to track user inactivity and obtain information about the title of the active window on Mac OS and Windows.

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

Dependencies

flutter, plugin_platform_interface

More

Packages that depend on window_focus

Packages that implement window_focus