window_focus 1.1.0 copy "window_focus: ^1.1.0" to clipboard
window_focus: ^1.1.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.

Window focus plugin #

Language Versions #

Usage_example

Pub

Window Focus is a Flutter plugin that allows you to track user activity and focus on the active window for Windows and macOS platforms. The plugin provides features such as detecting user inactivity, identifying the active application, and enabling debug mode for enhanced logging.

Key Features: #

User Inactivity Tracking: #

The plugin enables you to detect periods of user inactivity within your Flutter application. You can customize the inactivity threshold and handle inactivity events according to your needs.

Active Window Title Retrieval: #

Provides the ability to retrieve the title of the active window of the operating system. For Mac OS, this is the application name, and for Windows, it's the window title.

Debug Mode #

Enable detailed logs for troubleshooting during development.

Set Custom Idle Threshold #

Define the timeout period after which the user is considered inactive.

Plugin Installation #

Windows #

No action required.

Mac OS #

You need to add the following code to the Info.plist file for MacOS:

<key>NSApplicationSupportsSecureRestorableState</key>
<true/>
copied to clipboard

Plugin Usage #

Import the plugin: #

import 'package:window_focus/window_focus.dart';
copied to clipboard

Example #

  void main() {
  final windowFocus = WindowFocus(debug: true, duration: Duration(seconds: 10));

  // Add focus change listener
  windowFocus.addFocusChangeListener((appWindow) {
    print('Active window: ${appWindow.appName}, Title: ${appWindow.windowTitle}');
  });

  // Add user activity listener
  windowFocus.addUserActiveListener((isActive) {
    if (isActive) {
      print('User is active');
    } else {
      print('User is inactive');
    }
  });
}
copied to clipboard

API Reference #

Constructor #

WindowFocus({bool debug = false, Duration duration = const Duration(seconds: 1)})
copied to clipboard
  • debug (optional): Enables debug mode for detailed logs.
  • duration (optional): Sets the user inactivity timeout. Default is 1 second.

Methods #

Future #

Sets the threshold for detecting user inactivity.

  • Parameters:
    • duration: The duration after which the user is considered inactive.
    await windowFocus.setIdleThreshold(Duration(seconds: 15));
    
    copied to clipboard

Future #

Gets the current idle threshold.

  • Returns: Duration - The currently set idle threshold.
final threshold = await windowFocus.getIdleThreshold();
print('Idle threshold: ${threshold.inSeconds} seconds');
copied to clipboard

void addFocusChangeListener(Function(AppWindowDto) listener) #

Adds a listener for changes in the focused window.

  • Parameters:
    • listener: A callback function that receives an AppWindowDto object containing:
      • appName: The name of the active application.
      • windowTitle: The title of the active window.

Platform-specific Details

  • Windows:
    • appName is the name of the executable file (e.g., chrome.exe).
    • windowTitle is the title of the active window (e.g., Flutter Documentation).
  • macOS:
    • appName and windowTitle are the same and represent the name of the active application (e.g., Safari).
windowFocus.addFocusChangeListener((appWindow) {
  print('Active application: ${appWindow.appName}, Window title: ${appWindow.windowTitle}');
});
copied to clipboard

void addUserActiveListener(Function(bool) listener) #

Adds a listener for user activity changes.

  • Parameters:
    • listener: A callback function that receives a bool indicating user activity (true for active, false for inactive).
windowFocus.addUserActiveListener((isActive) {
  if (isActive) {
    print('User is active');
  } else {
    print('User is inactive');
  }
});
copied to clipboard

Future #

Enables or disables debug mode.

  • Parameters:
    • value: true to enable debug mode, false to disable it.
await windowFocus.setDebug(true);
copied to clipboard

DTO: AppWindowDto #

Represents the active application and window information.

Properties

  • appName: String - The name of the active application.
  • windowTitle: String - The title of the active window.

Example

final appWindow = AppWindowDto(appName: "Chrome", windowTitle: "Flutter Documentation");
print(appWindow); // Output: Window title: Flutter Documentation. AppName: Chrome
copied to clipboard

About the author #

My telegram channel - @kotelnikoff_dev Contributions

Contributions are welcome! Feel free to open issues or create pull requests on the GitHub repository.

1
likes
150
points
31
downloads

Publisher

verified publisherkotelnikoff.dev

Weekly Downloads

2024.10.02 - 2025.04.16

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)

Documentation

API reference

License

MIT (license)

Dependencies

flutter, plugin_platform_interface

More

Packages that depend on window_focus