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