Add persistent focus node for keyboard listener and improve focus handling
This commit is contained in:
@@ -308,6 +308,7 @@ class MainPageState extends State<MainPage> with WindowListener {
|
||||
final TextEditingController _intervalController = TextEditingController();
|
||||
final TextEditingController _soundController = TextEditingController();
|
||||
final TextEditingController _searchController = TextEditingController();
|
||||
final FocusNode _keyboardListenerFocusNode = FocusNode(); // Add persistent focus node
|
||||
|
||||
Note? previousNote;
|
||||
Note? _currentlyDisplayedNote;
|
||||
@@ -359,6 +360,7 @@ class MainPageState extends State<MainPage> with WindowListener {
|
||||
_previousEntryController.dispose();
|
||||
_currentEntryController.dispose();
|
||||
_currentEntryFocusNode.dispose();
|
||||
_keyboardListenerFocusNode.dispose(); // Dispose the keyboard listener focus node
|
||||
_scratchController.dispose();
|
||||
_intervalController.dispose();
|
||||
_soundController.dispose();
|
||||
@@ -424,13 +426,16 @@ class MainPageState extends State<MainPage> with WindowListener {
|
||||
await windowManager.setSize(const Size(1600, 900));
|
||||
await windowManager.center();
|
||||
|
||||
// Now show and focus
|
||||
// Show and focus immediately
|
||||
await windowManager.show();
|
||||
await Future.delayed(const Duration(milliseconds: 100)); // Short delay
|
||||
await windowManager.focus();
|
||||
|
||||
// Set input focus
|
||||
_currentEntryFocusNode.requestFocus();
|
||||
// Set input focus with a post-frame callback to ensure it sticks
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
if (mounted) {
|
||||
_currentEntryFocusNode.requestFocus();
|
||||
}
|
||||
});
|
||||
|
||||
// Play notification sound
|
||||
await _playSound();
|
||||
@@ -439,7 +444,11 @@ class MainPageState extends State<MainPage> with WindowListener {
|
||||
} else {
|
||||
// Already visible, just focus
|
||||
await windowManager.focus();
|
||||
_currentEntryFocusNode.requestFocus();
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
if (mounted) {
|
||||
_currentEntryFocusNode.requestFocus();
|
||||
}
|
||||
});
|
||||
debugPrint("Window already visible, just focused");
|
||||
}
|
||||
|
||||
@@ -1229,9 +1238,7 @@ class MainPageState extends State<MainPage> with WindowListener {
|
||||
Widget build(BuildContext context) {
|
||||
// Wrap Scaffold with RawKeyboardListener as workaround for Escape key
|
||||
return RawKeyboardListener(
|
||||
focusNode:
|
||||
FocusNode()
|
||||
..requestFocus(), // Request focus to ensure keyboard events are captured
|
||||
focusNode: _keyboardListenerFocusNode, // Use persistent focus node
|
||||
onKey: (RawKeyEvent event) {
|
||||
if (event is RawKeyDownEvent) {
|
||||
// Handle Escape to close window
|
||||
@@ -1240,8 +1247,7 @@ class MainPageState extends State<MainPage> with WindowListener {
|
||||
"Escape pressed inside MainPage (RawKeyboardListener - Workaround)",
|
||||
);
|
||||
// Call method directly since we are in the state
|
||||
FocusManager.instance.primaryFocus
|
||||
?.unfocus(); // Keep unfocus attempt
|
||||
FocusManager.instance.primaryFocus?.unfocus(); // Keep unfocus attempt
|
||||
onWindowClose();
|
||||
}
|
||||
// Handle Ctrl+F to open search
|
||||
|
Reference in New Issue
Block a user