https://github.com/alihassan143/screenstate
Desktop Screen State
https://github.com/alihassan143/screenstate
flutter macos screen screenstate windows
Last synced: 2 months ago
JSON representation
Desktop Screen State
- Host: GitHub
- URL: https://github.com/alihassan143/screenstate
- Owner: alihassan143
- License: apache-2.0
- Created: 2023-01-25T11:53:15.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2025-04-22T03:42:55.000Z (about 1 year ago)
- Last Synced: 2025-04-22T06:08:12.829Z (about 1 year ago)
- Topics: flutter, macos, screen, screenstate, windows
- Language: C++
- Homepage: https://pub.dev/packages/desktop_screenstate
- Size: 234 KB
- Stars: 0
- Watchers: 1
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Desktop ScreenState
**Desktop ScreenState** is a Flutter desktop plugin that provides functionality for your application to accurately determine whether the screen is on or off, as well as to detect if it's locked or unlocked.
## Platform Support
| Linux | macOS | Windows |
| :-------: | :-------: | :-------: |
| ✅ | ✅ | ✅ |
### macOS
No changes are required.
### Windows
Make the following changes to the respective files:
#### `windows/runner/flutter_window.h`
```diff
#ifndef RUNNER_FLUTTER_WINDOW_H_
#define RUNNER_FLUTTER_WINDOW_H_
#include
#include
+ #include
#include
#include "win32_window.h"
// A window that does nothing but host a Flutter view.
class FlutterWindow : public Win32Window {
public:
// Creates a new FlutterWindow hosting a Flutter view running |project|.
explicit FlutterWindow(const flutter::DartProject& project);
virtual ~FlutterWindow();
protected:
// Win32Window:
bool OnCreate() override;
void OnDestroy() override;
LRESULT MessageHandler(HWND window, UINT const message, WPARAM const wparam,
LPARAM const lparam) noexcept override;
private:
// The project to run.
flutter::DartProject project_;
+ HPOWERNOTIFY power_notification_handle_ = nullptr;
// The Flutter instance hosted by this window.
std::unique_ptr flutter_controller_;
};
#endif // RUNNER_FLUTTER_WINDOW_H_
```
#### `windows/runner/flutter_window.cpp`
```diff
#include "flutter_window.h"
#include
+ #include
#include "flutter/generated_plugin_registrant.h"
+ #pragma comment( lib, "wtsapi32.lib" )
FlutterWindow::FlutterWindow(const flutter::DartProject& project)
: project_(project) {}
FlutterWindow::~FlutterWindow() {
+ if (power_notification_handle_) {
+ UnregisterPowerSettingNotification(power_notification_handle_);
+ }
}
bool FlutterWindow::OnCreate() {
if (!Win32Window::OnCreate()) {
return false;
}
RECT frame = GetClientArea();
// The size here must match the window dimensions to avoid unnecessary surface
// creation / destruction in the startup path.
flutter_controller_ = std::make_unique(
frame.right - frame.left, frame.bottom - frame.top, project_);
// Ensure that basic setup of the controller was successful.
if (!flutter_controller_->engine() || !flutter_controller_->view()) {
return false;
}
RegisterPlugins(flutter_controller_->engine());
SetChildContent(flutter_controller_->view()->GetNativeWindow());
+ power_notification_handle_ = RegisterPowerSettingNotification(GetHandle(), &GUID_CONSOLE_DISPLAY_STATE, DEVICE_NOTIFY_WINDOW_HANDLE);
+ WTSRegisterSessionNotification(GetHandle(),NOTIFY_FOR_THIS_SESSION);
return true;
}
void FlutterWindow::OnDestroy() {
if (flutter_controller_) {
flutter_controller_ = nullptr;
}
Win32Window::OnDestroy();
}
LRESULT
FlutterWindow::MessageHandler(HWND hwnd, UINT const message,
WPARAM const wparam,
LPARAM const lparam) noexcept {
// Give Flutter, including plugins, an opportunity to handle window messages.
if (flutter_controller_) {
std::optional result =
flutter_controller_->HandleTopLevelWindowProc(hwnd, message, wparam,
lparam);
if (result) {
return *result;
}
}
switch (message) {
case WM_FONTCHANGE:
flutter_controller_->engine()->ReloadSystemFonts();
break;
}
return Win32Window::MessageHandler(hwnd, message, wparam, lparam);
}
```
### Linux
This plugin relies on the gnome-screensaver to provide screen state information.
#### Linux Important Note:
If gnome-screensaver is not installed on your Linux system, please install it before using this plugin. The functionality of the plugin depends on the presence of gnome-screensaver.
```
sudo apt-get install gnome-screensaver
```
## Getting Started
1. Add `desktop_screenstate` to your `pubspec.yaml`.
```yaml
desktop_screenstate: $latest_version
```
2. **Linux ONLY** - Only needed if your app will run on Linux.
In your main() function, select the monitor service type to use. By default, the `dbus-monitor` service is used. However, you may find that the `gdbus` service is more reliable.
The `dbus-monitor` service is included in the standard D-Bus package (`dbus-utils` or equivalent). The `gdbus` requires GLib 2.26+ (typically installed via the `glib2` package).
```
sudo apt-get install glib2
```
In your `main.dart` file, add the following to your `main()` function. The process exit cleanup code is needed regardless of which type of monitor service you select. If this cleanup code is not added, then background processes will be left running, and they will accumulate over time as your application is started and stopped.
```dart
import 'package:desktop_screenstate/desktop_screenstate.dart';
void main() {
DesktopScreenState.linuxMonitor = ScreenStateMonitor.gdbus;
/// Hook into the process exit signals to clean up processes
ProcessSignal.sigint.watch().listen((_) => shutdownApp());
ProcessSignal.sigterm.watch().listen((_) => shutdownApp());
runApp(MyApp());
}
void shutdownApp() {
try {
DesktopScreenState.dispose();
// ... Add your custom cleanup logic here ...
} finally {
exit(0);
}
}
```
3. Then you can use `DesktopScreenState.instance.isActive` to listen window active event.
```dart
final ValueListenable event = DesktopScreenState.instance.isActive;
final bool active = event.value;
event.addListener(() {
debugPrint("screen is on or off: ${event.value}");
});
```
4. You can now utilize `DesktopScreenState.instance.state` to get the current state of the screen:
```dart
final ScreenState state = DesktopScreenState.instance.state;
switch (state) {
case ScreenState.awaked:
debugPrint("Screen is on");
break;
case ScreenState.sleep:
debugPrint("Screen is sleep");
break;
case ScreenState.locked:
debugPrint("Screen is locked");
break;
case ScreenState.unlocked:
debugPrint("Screen is unlocked");
break;
}
The DesktopScreenState instance provides an enum ScreenState with the following possible values:
```
see LICENSE file