forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtext_selection_controls.dart
More file actions
119 lines (103 loc) · 3.06 KB
/
Copy pathtext_selection_controls.dart
File metadata and controls
119 lines (103 loc) · 3.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:omi/utils/l10n_extensions.dart';
class OmiTextSelectionToolbar extends StatelessWidget {
final Offset anchorAbove;
final Offset anchorBelow;
final List<Widget> children;
const OmiTextSelectionToolbar({
super.key,
required this.anchorAbove,
required this.anchorBelow,
required this.children,
});
@override
Widget build(BuildContext context) {
return CupertinoTheme(
data: const CupertinoThemeData(brightness: Brightness.dark, primaryColor: Colors.blue),
child: CupertinoTextSelectionToolbar(anchorAbove: anchorAbove, anchorBelow: anchorBelow, children: children),
);
}
}
class OmiToolbarAction extends StatelessWidget {
final String label;
final VoidCallback onPressed;
const OmiToolbarAction({super.key, required this.label, required this.onPressed});
@override
Widget build(BuildContext context) {
return CupertinoTextSelectionToolbarButton.text(onPressed: onPressed, text: label);
}
}
class OmiToolbarDivider extends StatelessWidget {
const OmiToolbarDivider({super.key});
@override
Widget build(BuildContext context) {
return Container(
width: 1.0 / MediaQuery.of(context).devicePixelRatio,
height: 20,
color: const Color.fromARGB(70, 255, 255, 255),
);
}
}
Widget omiSelectionMenuBuilder(
BuildContext context,
dynamic delegate,
Function(String) onAskOmi, {
String? selectedText,
}) {
final List<Widget> toolbarItems = [];
String text = selectedText ?? '';
if (delegate is TextSelectionDelegate && text.isEmpty) {
text = delegate.textEditingValue.selection.textInside(delegate.textEditingValue.text);
}
// Ask Omi
if (text.trim().isNotEmpty) {
toolbarItems.add(
OmiToolbarAction(
label: context.l10n.askOmi,
onPressed: () {
onAskOmi(text);
delegate.hideToolbar();
},
),
);
}
if (text.isNotEmpty) {
if (toolbarItems.isNotEmpty) {
toolbarItems.add(const OmiToolbarDivider());
}
toolbarItems.add(
OmiToolbarAction(
label: context.l10n.copy,
onPressed: () {
delegate.copySelection(SelectionChangedCause.toolbar);
delegate.hideToolbar();
ScaffoldMessenger.of(
context,
).showSnackBar(SnackBar(content: Text(context.l10n.messageCopied), duration: const Duration(seconds: 2)));
},
),
);
}
// Select All
if (toolbarItems.isNotEmpty) {
toolbarItems.add(const OmiToolbarDivider());
}
toolbarItems.add(
OmiToolbarAction(
label: context.l10n.selectAll,
onPressed: () {
delegate.selectAll(SelectionChangedCause.toolbar);
},
),
);
if (toolbarItems.isEmpty) {
return const SizedBox.shrink();
}
return OmiTextSelectionToolbar(
anchorAbove: delegate.contextMenuAnchors.primaryAnchor,
anchorBelow: delegate.contextMenuAnchors.secondaryAnchor ?? delegate.contextMenuAnchors.primaryAnchor,
children: toolbarItems,
);
}