backend: separated agent classes. frontend: chat message context menu (does nothing)

This commit is contained in:
Shivering Ocelot committed 2025-06-01 23:59:38 -05:00
1 parent 3f37f727af
commit 22fe5619d8
5 files changed
+205 -82

No files matched your search

+89 -50
View File
@@ -6,11 +6,12 @@ import 'backend.dart';
import 'dart:async';
class Message {
final int? id;
final String sender;
final String text;
final bool isFromUser;
Message({required this.sender, required this.text}) : isFromUser = sender == "user";
Message({required this.sender, required this.text, this.id}) : isFromUser = sender == "user";
Alignment get alignment => isFromUser ? Alignment.centerRight : Alignment.centerLeft;
EdgeInsets get padding => isFromUser ? EdgeInsets.zero : const EdgeInsets.only(left: 10);
@@ -23,9 +24,8 @@ class ChatScreen extends StatefulWidget {
static Future<List<Message>> getChatMessages(int chatId) async {
final response = await Backend.sendAction("get-chat-messages", {'chatId': chatId});
print("get-chat-messages response: $response");
List<dynamic> messageObjects = response['messages'];
List<Message> messages = messageObjects.map((msg) => Message(sender: msg['sender'], text: msg['content'] as String)).toList();
List<Message> messages = messageObjects.map((msg) => Message(sender: msg['sender'], text: msg['content'])).toList();
return messages;
}
@@ -36,9 +36,10 @@ class ChatScreen extends StatefulWidget {
class _ChatScreenState extends State<ChatScreen> {
String? _title;
List<Message> _messages = [];
Offset? _longPressOffset;
StreamSubscription? _chatSubscription;
final TextEditingController _textController = TextEditingController();
final FocusNode _inputFocusNode = FocusNode();
StreamSubscription? _chatSubscription;
@override
void initState() {
@@ -50,11 +51,16 @@ class _ChatScreenState extends State<ChatScreen> {
addMessage(message);
}
});
BrowserContextMenu.disableContextMenu();
}
@override
void dispose() {
_chatSubscription?.cancel(); // Cancel the subscription
Backend.removeChatListener(widget.chatId);
BrowserContextMenu.enableContextMenu();
_textController.dispose();
_inputFocusNode.dispose();
super.dispose();
}
@@ -67,15 +73,6 @@ class _ChatScreenState extends State<ChatScreen> {
});
}
void _handleNewMessage(Map<String, dynamic> message) {
final newMessage = Message(sender: message['sender'], text: message['content'] as String);
if (newMessage.sender!= "user") {
setState(() {
_messages.add(newMessage);
});
}
}
void addMessage(Message message) {
setState(() {
_messages.add(message);
@@ -83,6 +80,7 @@ class _ChatScreenState extends State<ChatScreen> {
}
void sendMessage(String content) {
if (content.trim().isEmpty) return; // Don't send empty messages
_textController.clear();
final newMessage = Message(sender: "user", text: content);
Backend.sendAction("send-chat-message", {'chatId': widget.chatId, 'content': content}).then((response) {
@@ -99,6 +97,36 @@ class _ChatScreenState extends State<ChatScreen> {
return messageColor;
}
void _showMessageContextMenu(BuildContext context, Offset position) {
showMenu(
context: context,
position: RelativeRect.fromLTRB(position.dx, position.dy, position.dx + 1, position.dy + 1),
items: <PopupMenuEntry>[
PopupMenuItem(
child: Text('Pin'),
onTap: () {
// TODO: Implement Pin functionality
print('Pin clicked');
},
),
PopupMenuItem(
child: Text('Edit'),
onTap: () {
// TODO: Implement Edit functionality
print('Edit clicked');
},
),
PopupMenuItem(
child: Text('Delete'),
onTap: () {
// TODO: Implement Delete functionality
print('Delete clicked');
},
),
],
);
}
@override
Widget build(BuildContext context) {
final isWideScreen = MediaQuery.of(context).size.width > 600;
@@ -119,18 +147,30 @@ class _ChatScreenState extends State<ChatScreen> {
itemCount: _messages.length,
itemBuilder: (context, index) {
final message = _messages[index];
return Padding(
padding: isWideScreen ? message.padding : EdgeInsets.zero,
child: Align(
alignment: isWideScreen ? Alignment.centerLeft : message.alignment,
child: Container(
margin: EdgeInsets.symmetric(vertical: 2, horizontal: 8),
padding: EdgeInsets.all(10),
decoration: BoxDecoration(
color: getMessageColor(context),
borderRadius: BorderRadius.circular(12),
return GestureDetector(
onLongPressStart: (details) {
_longPressOffset = details.globalPosition;
},
onLongPress: () {
_showMessageContextMenu(context, _longPressOffset!);
_longPressOffset = null;
},
onSecondaryTapUp: (details) {
_showMessageContextMenu(context, details.globalPosition);
},
child: Padding(
padding: isWideScreen ? message.padding : EdgeInsets.zero,
child: Align(
alignment: isWideScreen ? Alignment.centerLeft : message.alignment,
child: Container(
margin: EdgeInsets.symmetric(vertical: 2, horizontal: 8),
padding: EdgeInsets.all(10),
decoration: BoxDecoration(
color: getMessageColor(context),
borderRadius: BorderRadius.circular(12),
),
child: Text(message.text),
),
child: Text(message.text),
),
),
);
@@ -139,36 +179,35 @@ class _ChatScreenState extends State<ChatScreen> {
),
Padding(
padding: const EdgeInsets.all(8.0),
child: KeyboardListener(
focusNode: _inputFocusNode,
onKeyEvent: (event) {
// TODO: Probably doing this in the input focus node would be cleaner
if (HardwareKeyboard.instance.isControlPressed && HardwareKeyboard.instance.isLogicalKeyPressed(LogicalKeyboardKey.enter)) {
sendMessage(_textController.text);
_textController.clear();
//return KeyEventResult.handled;
}
//return KeyEventResult.ignored;
},
child: Column(
children: [
Expanded(
child: Row(
children: [
Expanded(
child: KeyboardListener(
focusNode: _inputFocusNode,
onKeyEvent: (event) {
// TODO: Probably doing this in the input focus node would be cleaner
if (HardwareKeyboard.instance.isControlPressed && HardwareKeyboard.instance.isLogicalKeyPressed(LogicalKeyboardKey.enter)) {
sendMessage(_textController.text);
//return KeyEventResult.handled;
}
//return KeyEventResult.ignored;
},
child: TextField(
controller: _textController,
maxLines: null,
decoration: inputDecoration(),
maxLines: null, // Allows multiline input
keyboardType: TextInputType.multiline, // For better multiline support on mobile
decoration: inputDecoration().copyWith(hintText: 'Type a message...'), // Added a hint
),
),
SizedBox(width: 8),
ElevatedButton(
onPressed: () {
sendMessage(_textController.text);
_textController.clear();
},
child: Text('Send'),
),
],
),
),
SizedBox(width: 8),
ElevatedButton(
onPressed: () {
sendMessage(_textController.text);
},
child: Text('Send'),
),
],
),
),
],