Skip to content

Commit

Permalink
Merge pull request #60 from pangeachat/toolbar-testing
Browse files Browse the repository at this point in the history
fix to update state of defintion button in toolbar
  • Loading branch information
ggurdin authored Feb 10, 2024
2 parents 23055e6 + da64ed1 commit a4ba1c8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
7 changes: 4 additions & 3 deletions lib/pangea/widgets/chat/message_text_selection.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import 'dart:async';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

class MessageTextSelection {
String? selectedText;
String messageText = "";
final StreamController<String?> selectionStream =
StreamController<String?>.broadcast();

void setMessageText(String text) {
messageText = text;
Expand All @@ -21,14 +22,14 @@ class MessageTextSelection {
if (BrowserContextMenu.enabled && kIsWeb) {
BrowserContextMenu.disableContextMenu();
}
// selectionStream.add(selectedText);
selectionStream.add(selectedText);
}

void clearTextSelection() {
selectedText = null;
if (kIsWeb && !BrowserContextMenu.enabled) {
BrowserContextMenu.enableContextMenu();
}
// selectionStream.add(selectedText);
selectionStream.add(selectedText);
}
}
15 changes: 15 additions & 0 deletions lib/pangea/widgets/chat/message_toolbar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ class MessageToolbar extends StatefulWidget {
class MessageToolbarState extends State<MessageToolbar> {
Widget? child;
MessageMode? _currentMode;
bool hasSelectedText = false;
late StreamSubscription<String?> _selectionStream;
late StreamSubscription<MessageMode> _toolbarModeStream;

Expand Down Expand Up @@ -233,9 +234,23 @@ class MessageToolbarState extends State<MessageToolbar> {
@override
void initState() {
super.initState();
if (widget.textSelection.selectedText != null) {
hasSelectedText = true;
}

_toolbarModeStream = widget.toolbarModeStream.stream.listen((mode) {
updateMode(mode);
});

_selectionStream =
widget.textSelection.selectionStream.stream.listen((value) {
final bool shouldSetState =
value != null && !hasSelectedText || value == null && hasSelectedText;
hasSelectedText = value != null;
if (shouldSetState) {
setState(() {});
}
});
}

@override
Expand Down

0 comments on commit a4ba1c8

Please sign in to comment.