diff --git a/.vscode/settings.json b/.vscode/settings.json index 91dd3e0c..9a57519f 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -9,6 +9,10 @@ "type_traits": "cpp", "xlocmon": "cpp", "xlocnum": "cpp", - "xtr1common": "cpp" + "xtr1common": "cpp", + "__locale": "cpp", + "__string": "cpp", + "string": "cpp", + "string_view": "cpp" } } \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index 2d90abdd..93846751 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,12 @@ +## 0.1.6 + +- Implement `isPreventClose` & `setPreventClose` methods #69 +- Implement `close` event +- [macos & windows] Reimplement `close` method +- [windows] Fix Horizontal resizing not working on secondary display. #71 +- [macos] Implement `isFocused` method +- Implement `setAlignment` method #52 + ## 0.1.5 - Implement `close` method #56 diff --git a/README-ZH.md b/README-ZH.md index e223bb27..fbeb21ae 100644 --- a/README-ZH.md +++ b/README-ZH.md @@ -28,6 +28,7 @@ - [关闭时退出](#关闭时退出) - [macOS](#macos) - [Windows](#windows) + - [关闭前确认](#关闭前确认) - [在启动时隐藏](#在启动时隐藏) - [macOS](#macos-1) - [Windows](#windows-1) @@ -36,9 +37,11 @@ - [WindowManager](#windowmanager) - [Methods](#methods) - [close](#close) + - [isPreventClose](#ispreventclose) + - [setPreventClose](#setpreventclose) - [focus](#focus) - [blur `macos` `windows`](#blur--macos--windows) - - [isFocused `windows`](#isfocused--windows) + - [isFocused `macos` `windows`](#isfocused--macos--windows) - [show](#show) - [hide](#hide) - [isVisible](#isvisible) @@ -84,6 +87,7 @@ - [startDragging](#startdragging) - [WindowListener](#windowlistener) - [Methods](#methods-1) + - [onWindowClose](#onwindowclose) - [onWindowFocus](#onwindowfocus) - [onWindowBlur](#onwindowblur) - [onWindowMaximize](#onwindowmaximize) @@ -113,7 +117,7 @@ ```yaml dependencies: - window_manager: ^0.1.5 + window_manager: ^0.1.6 ``` 或 @@ -188,6 +192,11 @@ class _HomePageState extends State with WindowListener { print('[WindowManager] onWindowEvent: $eventName'); } + @override + void onWindowClose() { + // do something + } + @override void onWindowFocus() { // do something @@ -279,6 +288,72 @@ int APIENTRY wWinMain(_In_ HINSTANCE instance, _In_opt_ HINSTANCE prev, } ``` +### 关闭前确认 + +```dart +import 'package:flutter/cupertino.dart'; +import 'package:window_manager/window_manager.dart'; + +class HomePage extends StatefulWidget { + @override + _HomePageState createState() => _HomePageState(); +} + +class _HomePageState extends State with WindowListener { + @override + void initState() { + windowManager.addListener(this); + super.initState(); + } + + void _init() async { + await windowManager.setPreventClose(true); + setState(() {}); + } + + @override + void dispose() { + windowManager.removeListener(this); + super.dispose(); + } + + @override + Widget build(BuildContext context) { + // ... + } + + @override + void onWindowClose() async { + bool _isPreventClose = await windowManager.isPreventClose(); + if (_isPreventClose) { + showDialog( + context: context, + builder: (_) { + return AlertDialog( + title: Text('Are you sure you want to close this window?'), + actions: [ + TextButton( + child: Text('No'), + onPressed: () { + Navigator.of(context).pop(); + }, + ), + TextButton( + child: Text('Yes'), + onPressed: () { + Navigator.of(context).pop(); + exit(0); + }, + ), + ], + ); + }, + ); + } + } +} +``` + #### 在启动时隐藏 ##### macOS @@ -386,6 +461,15 @@ class _HomePageState extends State with WindowListener { Try to close the window. +##### isPreventClose + +Check if is intercepting the native close signal. + +##### setPreventClose + +Set if intercept the native close signal. May useful when combine with the onclose event listener. +This will also prevent the manually triggered close event. + ##### focus Focuses on the window. @@ -395,7 +479,7 @@ Focuses on the window. Removes focus from the window. -##### isFocused `windows` +##### isFocused `macos` `windows` Returns `bool` - Whether window is focused. @@ -589,6 +673,10 @@ Starts a window drag based on the specified mouse-down event. #### Methods +##### onWindowClose + +Emitted when the window is going to be closed. + ##### onWindowFocus Emitted when the window gains focus. diff --git a/README.md b/README.md index 43d5e1a0..22b38b7f 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,7 @@ English | [简体中文](./README-ZH.md) - [Quit on close](#quit-on-close) - [macOS](#macos) - [Windows](#windows) + - [Confirm before closing](#confirm-before-closing) - [Hidden at launch](#hidden-at-launch) - [macOS](#macos-1) - [Windows](#windows-1) @@ -36,9 +37,11 @@ English | [简体中文](./README-ZH.md) - [WindowManager](#windowmanager) - [Methods](#methods) - [close](#close) + - [isPreventClose](#ispreventclose) + - [setPreventClose](#setpreventclose) - [focus](#focus) - [blur `macos` `windows`](#blur--macos--windows) - - [isFocused `windows`](#isfocused--windows) + - [isFocused `macos` `windows`](#isfocused--macos--windows) - [show](#show) - [hide](#hide) - [isVisible](#isvisible) @@ -84,6 +87,7 @@ English | [简体中文](./README-ZH.md) - [startDragging](#startdragging) - [WindowListener](#windowlistener) - [Methods](#methods-1) + - [onWindowClose](#onwindowclose) - [onWindowFocus](#onwindowfocus) - [onWindowBlur](#onwindowblur) - [onWindowMaximize](#onwindowmaximize) @@ -113,7 +117,7 @@ Add this to your package's `pubspec.yaml` file: ```yaml dependencies: - window_manager: ^0.1.5 + window_manager: ^0.1.6 ``` Or @@ -188,6 +192,11 @@ class _HomePageState extends State with WindowListener { print('[WindowManager] onWindowEvent: $eventName'); } + @override + void onWindowClose() { + // do something + } + @override void onWindowFocus() { // do something @@ -279,6 +288,72 @@ int APIENTRY wWinMain(_In_ HINSTANCE instance, _In_opt_ HINSTANCE prev, } ``` +### Confirm before closing + +```dart +import 'package:flutter/cupertino.dart'; +import 'package:window_manager/window_manager.dart'; + +class HomePage extends StatefulWidget { + @override + _HomePageState createState() => _HomePageState(); +} + +class _HomePageState extends State with WindowListener { + @override + void initState() { + windowManager.addListener(this); + super.initState(); + } + + void _init() async { + await windowManager.setPreventClose(true); + setState(() {}); + } + + @override + void dispose() { + windowManager.removeListener(this); + super.dispose(); + } + + @override + Widget build(BuildContext context) { + // ... + } + + @override + void onWindowClose() async { + bool _isPreventClose = await windowManager.isPreventClose(); + if (_isPreventClose) { + showDialog( + context: context, + builder: (_) { + return AlertDialog( + title: Text('Are you sure you want to close this window?'), + actions: [ + TextButton( + child: Text('No'), + onPressed: () { + Navigator.of(context).pop(); + }, + ), + TextButton( + child: Text('Yes'), + onPressed: () { + Navigator.of(context).pop(); + exit(0); + }, + ), + ], + ); + }, + ); + } + } +} +``` + #### Hidden at launch ##### macOS @@ -386,6 +461,15 @@ class _HomePageState extends State with WindowListener { Try to close the window. +##### isPreventClose + +Check if is intercepting the native close signal. + +##### setPreventClose + +Set if intercept the native close signal. May useful when combine with the onclose event listener. +This will also prevent the manually triggered close event. + ##### focus Focuses on the window. @@ -395,7 +479,7 @@ Focuses on the window. Removes focus from the window. -##### isFocused `windows` +##### isFocused `macos` `windows` Returns `bool` - Whether window is focused. @@ -589,6 +673,10 @@ Starts a window drag based on the specified mouse-down event. #### Methods +##### onWindowClose + +Emitted when the window is going to be closed. + ##### onWindowFocus Emitted when the window gains focus. diff --git a/example/lib/pages/home.dart b/example/lib/pages/home.dart index ddfa9374..1de042cc 100644 --- a/example/lib/pages/home.dart +++ b/example/lib/pages/home.dart @@ -1,5 +1,4 @@ import 'dart:io'; -import 'dart:ui'; import 'package:bot_toast/bot_toast.dart'; import 'package:flutter/cupertino.dart'; diff --git a/example/pubspec.lock b/example/pubspec.lock index b4217975..1886014c 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -176,7 +176,7 @@ packages: path: ".." relative: true source: path - version: "0.1.5" + version: "0.1.6" sdks: dart: ">=2.14.0 <3.0.0" flutter: ">=1.20.0" diff --git a/lib/src/window_manager.dart b/lib/src/window_manager.dart index 614daaa8..8201c739 100644 --- a/lib/src/window_manager.dart +++ b/lib/src/window_manager.dart @@ -124,7 +124,7 @@ class WindowManager { /// Returns `bool` - Whether window is focused. /// - /// @platforms windows + /// @platforms macos,windows Future isFocused() async { return await _channel.invokeMethod('isFocused'); } diff --git a/pubspec.yaml b/pubspec.yaml index 3853fb1d..4455e0d1 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: window_manager description: This plugin allows Flutter desktop apps to resizing and repositioning the window. -version: 0.1.5 +version: 0.1.6 homepage: https://github.com/leanflutter/window_manager environment: diff --git a/windows/window_manager.cpp b/windows/window_manager.cpp index c24bbfef..110a66c6 100644 --- a/windows/window_manager.cpp +++ b/windows/window_manager.cpp @@ -48,8 +48,8 @@ class WindowManager { void WindowManager::SetAsFrameless(); void WindowManager::WaitUntilReadyToShow(); void WindowManager::Close(); - void WindowManager::SetPreventClose(const flutter::EncodableMap& args); bool WindowManager::IsPreventClose(); + void WindowManager::SetPreventClose(const flutter::EncodableMap& args); void WindowManager::Focus(); void WindowManager::Blur(); bool WindowManager::IsFocused(); @@ -144,16 +144,16 @@ void WindowManager::Close() { PostMessage(hWnd, WM_SYSCOMMAND, SC_CLOSE, 0); } +bool WindowManager::IsPreventClose() { + return prevent_close; +} + void WindowManager::SetPreventClose(const flutter::EncodableMap& args) { bool isPreventClose = std::get(args.at(flutter::EncodableValue("isPreventClose"))); prevent_close = isPreventClose; } -bool WindowManager::IsPreventClose() { - return prevent_close; -} - void WindowManager::Focus() { HWND hWnd = GetMainWindow(); if (IsMinimized()) { diff --git a/windows/window_manager_plugin.cpp b/windows/window_manager_plugin.cpp index 0199dbe3..63eec4f4 100644 --- a/windows/window_manager_plugin.cpp +++ b/windows/window_manager_plugin.cpp @@ -206,14 +206,14 @@ void WindowManagerPlugin::HandleMethodCall( } else if (method_name.compare("close") == 0) { window_manager->Close(); result->Success(flutter::EncodableValue(true)); + } else if (method_name.compare("isPreventClose") == 0) { + auto value = window_manager->IsPreventClose(); + result->Success(flutter::EncodableValue(value)); } else if (method_name.compare("setPreventClose") == 0) { const flutter::EncodableMap& args = std::get(*method_call.arguments()); window_manager->SetPreventClose(args); result->Success(flutter::EncodableValue(true)); - } else if (method_name.compare("isPreventClose") == 0) { - auto value = window_manager->IsPreventClose(); - result->Success(flutter::EncodableValue(value)); } else if (method_name.compare("focus") == 0) { window_manager->Focus(); result->Success(flutter::EncodableValue(true));