-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(performance): dispose every widget on window hidden and re-initi…
…alize when shown
- Loading branch information
Showing
4 changed files
with
207 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter_hooks/flutter_hooks.dart'; | ||
import 'package:window_manager/window_manager.dart'; | ||
|
||
void useWindowListeners({ | ||
VoidCallback? onWindowClose, | ||
VoidCallback? onWindowFocus, | ||
VoidCallback? onWindowBlur, | ||
VoidCallback? onWindowMaximize, | ||
VoidCallback? onWindowUnmaximize, | ||
VoidCallback? onWindowMinimize, | ||
VoidCallback? onWindowRestore, | ||
VoidCallback? onWindowResize, | ||
VoidCallback? onWindowResized, | ||
VoidCallback? onWindowMove, | ||
VoidCallback? onWindowMoved, | ||
VoidCallback? onWindowEnterFullScreen, | ||
VoidCallback? onWindowLeaveFullScreen, | ||
ValueChanged<String>? onWindowEvent, | ||
}) { | ||
return use( | ||
UseWindowListeners( | ||
onWindowClose: onWindowClose, | ||
onWindowFocus: onWindowFocus, | ||
onWindowBlur: onWindowBlur, | ||
onWindowMaximize: onWindowMaximize, | ||
onWindowUnmaximize: onWindowUnmaximize, | ||
onWindowMinimize: onWindowMinimize, | ||
onWindowRestore: onWindowRestore, | ||
onWindowResize: onWindowResize, | ||
onWindowResized: onWindowResized, | ||
onWindowMove: onWindowMove, | ||
onWindowMoved: onWindowMoved, | ||
onWindowEnterFullScreen: onWindowEnterFullScreen, | ||
onWindowLeaveFullScreen: onWindowLeaveFullScreen, | ||
onWindowEvent: onWindowEvent, | ||
), | ||
); | ||
} | ||
|
||
class UseWindowListeners extends Hook { | ||
final VoidCallback? onWindowClose; | ||
final VoidCallback? onWindowFocus; | ||
final VoidCallback? onWindowBlur; | ||
final VoidCallback? onWindowMaximize; | ||
final VoidCallback? onWindowUnmaximize; | ||
final VoidCallback? onWindowMinimize; | ||
final VoidCallback? onWindowRestore; | ||
final VoidCallback? onWindowResize; | ||
final VoidCallback? onWindowResized; | ||
final VoidCallback? onWindowMove; | ||
final VoidCallback? onWindowMoved; | ||
final VoidCallback? onWindowEnterFullScreen; | ||
final VoidCallback? onWindowLeaveFullScreen; | ||
final ValueChanged<String>? onWindowEvent; | ||
|
||
const UseWindowListeners({ | ||
this.onWindowClose, | ||
this.onWindowFocus, | ||
this.onWindowBlur, | ||
this.onWindowMaximize, | ||
this.onWindowUnmaximize, | ||
this.onWindowMinimize, | ||
this.onWindowRestore, | ||
this.onWindowResize, | ||
this.onWindowResized, | ||
this.onWindowMove, | ||
this.onWindowMoved, | ||
this.onWindowEnterFullScreen, | ||
this.onWindowLeaveFullScreen, | ||
this.onWindowEvent, | ||
}); | ||
|
||
@override | ||
_UseWindowListenersState createState() => _UseWindowListenersState(); | ||
} | ||
|
||
class _UseWindowListenersState extends HookState<void, UseWindowListeners> | ||
implements WindowListener { | ||
@override | ||
void initHook() { | ||
super.initHook(); | ||
WindowManager.instance.addListener(this); | ||
} | ||
|
||
@override | ||
void dispose() { | ||
WindowManager.instance.removeListener(this); | ||
super.dispose(); | ||
} | ||
|
||
@override | ||
void build(BuildContext context) {} | ||
|
||
@override | ||
void onWindowBlur() { | ||
hook.onWindowBlur?.call(); | ||
} | ||
|
||
@override | ||
void onWindowClose() { | ||
hook.onWindowClose?.call(); | ||
} | ||
|
||
@override | ||
void onWindowEnterFullScreen() { | ||
hook.onWindowEnterFullScreen?.call(); | ||
} | ||
|
||
@override | ||
void onWindowEvent(String eventName) { | ||
hook.onWindowEvent?.call(eventName); | ||
} | ||
|
||
@override | ||
void onWindowFocus() { | ||
hook.onWindowFocus?.call(); | ||
} | ||
|
||
@override | ||
void onWindowLeaveFullScreen() { | ||
hook.onWindowLeaveFullScreen?.call(); | ||
} | ||
|
||
@override | ||
void onWindowMaximize() { | ||
hook.onWindowMaximize?.call(); | ||
} | ||
|
||
@override | ||
void onWindowMinimize() { | ||
hook.onWindowMinimize?.call(); | ||
} | ||
|
||
@override | ||
void onWindowMove() { | ||
hook.onWindowMove?.call(); | ||
} | ||
|
||
@override | ||
void onWindowMoved() { | ||
hook.onWindowMoved?.call(); | ||
} | ||
|
||
@override | ||
void onWindowResize() { | ||
hook.onWindowResize?.call(); | ||
} | ||
|
||
@override | ||
void onWindowResized() { | ||
hook.onWindowResized?.call(); | ||
} | ||
|
||
@override | ||
void onWindowRestore() { | ||
hook.onWindowRestore?.call(); | ||
} | ||
|
||
@override | ||
void onWindowUnmaximize() { | ||
hook.onWindowUnmaximize?.call(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters