🐛 Fix chat newline on desktop
This commit is contained in:
@@ -1,7 +1,5 @@
|
|||||||
import "dart:async";
|
import "dart:async";
|
||||||
import "dart:io";
|
|
||||||
import "package:easy_localization/easy_localization.dart";
|
import "package:easy_localization/easy_localization.dart";
|
||||||
import "package:flutter/foundation.dart";
|
|
||||||
import "package:flutter/material.dart";
|
import "package:flutter/material.dart";
|
||||||
import "package:flutter/services.dart";
|
import "package:flutter/services.dart";
|
||||||
import "package:flutter_hooks/flutter_hooks.dart";
|
import "package:flutter_hooks/flutter_hooks.dart";
|
||||||
@@ -56,10 +54,6 @@ class ChatInput extends HookConsumerWidget {
|
|||||||
Widget build(BuildContext context, WidgetRef ref) {
|
Widget build(BuildContext context, WidgetRef ref) {
|
||||||
final inputFocusNode = useFocusNode();
|
final inputFocusNode = useFocusNode();
|
||||||
|
|
||||||
final enterToSend = ref.watch(appSettingsNotifierProvider).enterToSend;
|
|
||||||
|
|
||||||
final isMobile = !kIsWeb && (Platform.isAndroid || Platform.isIOS);
|
|
||||||
|
|
||||||
void send() {
|
void send() {
|
||||||
onSend.call();
|
onSend.call();
|
||||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||||
@@ -67,6 +61,18 @@ class ChatInput extends HookConsumerWidget {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void insertNewLine() {
|
||||||
|
final text = messageController.text;
|
||||||
|
final selection = messageController.selection;
|
||||||
|
final start = selection.start >= 0 ? selection.start : text.length;
|
||||||
|
final end = selection.end >= 0 ? selection.end : text.length;
|
||||||
|
final newText = text.replaceRange(start, end, '\n');
|
||||||
|
messageController.value = TextEditingValue(
|
||||||
|
text: newText,
|
||||||
|
selection: TextSelection.collapsed(offset: start + 1),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
Future<void> handlePaste() async {
|
Future<void> handlePaste() async {
|
||||||
final clipboard = await Pasteboard.image;
|
final clipboard = await Pasteboard.image;
|
||||||
if (clipboard == null) return;
|
if (clipboard == null) return;
|
||||||
@@ -80,32 +86,34 @@ class ChatInput extends HookConsumerWidget {
|
|||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
void handleKeyPress(
|
inputFocusNode.onKeyEvent = (node, event) {
|
||||||
BuildContext context,
|
if (event is! KeyDownEvent) return KeyEventResult.ignored;
|
||||||
WidgetRef ref,
|
|
||||||
RawKeyEvent event,
|
|
||||||
) {
|
|
||||||
if (event is! RawKeyDownEvent) return;
|
|
||||||
|
|
||||||
final isPaste = event.logicalKey == LogicalKeyboardKey.keyV;
|
final isPaste = event.logicalKey == LogicalKeyboardKey.keyV;
|
||||||
final isModifierPressed = event.isMetaPressed || event.isControlPressed;
|
final isModifierPressed =
|
||||||
|
HardwareKeyboard.instance.isMetaPressed ||
|
||||||
|
HardwareKeyboard.instance.isControlPressed;
|
||||||
|
|
||||||
if (isPaste && isModifierPressed) {
|
if (isPaste && isModifierPressed) {
|
||||||
handlePaste();
|
handlePaste();
|
||||||
return;
|
return KeyEventResult.handled;
|
||||||
}
|
}
|
||||||
|
|
||||||
final enterToSend = ref.read(appSettingsNotifierProvider).enterToSend;
|
final enterToSend = ref.read(appSettingsNotifierProvider).enterToSend;
|
||||||
final isEnter = event.logicalKey == LogicalKeyboardKey.enter;
|
final isEnter = event.logicalKey == LogicalKeyboardKey.enter;
|
||||||
|
|
||||||
if (isEnter) {
|
if (isEnter) {
|
||||||
if (enterToSend && !isModifierPressed) {
|
if (isModifierPressed) {
|
||||||
send();
|
insertNewLine();
|
||||||
} else if (!enterToSend && isModifierPressed) {
|
return KeyEventResult.handled;
|
||||||
|
} else if (enterToSend) {
|
||||||
send();
|
send();
|
||||||
|
return KeyEventResult.handled;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
return KeyEventResult.ignored;
|
||||||
|
};
|
||||||
|
|
||||||
return Material(
|
return Material(
|
||||||
elevation: 8,
|
elevation: 8,
|
||||||
@@ -260,32 +268,10 @@ class ChatInput extends HookConsumerWidget {
|
|||||||
],
|
],
|
||||||
),
|
),
|
||||||
Expanded(
|
Expanded(
|
||||||
child: RawKeyboardListener(
|
|
||||||
focusNode: FocusNode(),
|
|
||||||
onKey: (event) => handleKeyPress(context, ref, event),
|
|
||||||
child: TextField(
|
child: TextField(
|
||||||
focusNode: inputFocusNode,
|
focusNode: inputFocusNode,
|
||||||
controller: messageController,
|
controller: messageController,
|
||||||
onSubmitted:
|
keyboardType: TextInputType.multiline,
|
||||||
(enterToSend && isMobile)
|
|
||||||
? (_) {
|
|
||||||
send();
|
|
||||||
}
|
|
||||||
: null,
|
|
||||||
keyboardType:
|
|
||||||
(enterToSend && isMobile)
|
|
||||||
? TextInputType.text
|
|
||||||
: TextInputType.multiline,
|
|
||||||
textInputAction: TextInputAction.send,
|
|
||||||
inputFormatters: [
|
|
||||||
if (enterToSend && !isMobile)
|
|
||||||
TextInputFormatter.withFunction((oldValue, newValue) {
|
|
||||||
if (newValue.text.endsWith('\n')) {
|
|
||||||
return oldValue;
|
|
||||||
}
|
|
||||||
return newValue;
|
|
||||||
}),
|
|
||||||
],
|
|
||||||
decoration: InputDecoration(
|
decoration: InputDecoration(
|
||||||
hintText:
|
hintText:
|
||||||
(chatRoom.type == 1 && chatRoom.name == null)
|
(chatRoom.type == 1 && chatRoom.name == null)
|
||||||
@@ -314,7 +300,6 @@ class ChatInput extends HookConsumerWidget {
|
|||||||
(_) => FocusManager.instance.primaryFocus?.unfocus(),
|
(_) => FocusManager.instance.primaryFocus?.unfocus(),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
),
|
|
||||||
IconButton(
|
IconButton(
|
||||||
icon: const Icon(Icons.send),
|
icon: const Icon(Icons.send),
|
||||||
color: Theme.of(context).colorScheme.primary,
|
color: Theme.of(context).colorScheme.primary,
|
||||||
|
Reference in New Issue
Block a user