@@ -8,22 +8,51 @@ class ScreenSelectDialog extends Dialog {
8
8
ScreenSelectDialog () {
9
9
Future .delayed (Duration (milliseconds: 100 ), () {
10
10
_getSources ();
11
- _timer = Timer .periodic (Duration (milliseconds: 2000 ), (timer) {
12
- _getSources ();
13
- });
14
11
});
12
+ _subscriptions.add (desktopCapturer.onAdded.stream.listen ((source) {
13
+ _sources[source.id] = source;
14
+ _stateSetter? .call (() {});
15
+ }));
16
+
17
+ _subscriptions.add (desktopCapturer.onRemoved.stream.listen ((source) {
18
+ _sources.remove (source.id);
19
+ _stateSetter? .call (() {});
20
+ }));
21
+
22
+ _subscriptions.add (desktopCapturer.onNameChanged.stream.listen ((source) {
23
+ _sources[source.id] = source;
24
+ _stateSetter? .call (() {});
25
+ }));
26
+
27
+ _subscriptions
28
+ .add (desktopCapturer.onThumbnailChanged.stream.listen ((source) {
29
+ _sources[source.id] = source;
30
+ _stateSetter? .call (() {});
31
+ }));
15
32
}
16
- List < DesktopCapturerSource > _sources = [] ;
33
+ final Map < String , DesktopCapturerSource > _sources = {} ;
17
34
SourceType _sourceType = SourceType .Screen ;
18
35
DesktopCapturerSource ? _selected_source;
36
+ final List <StreamSubscription <DesktopCapturerSource >> _subscriptions = [];
19
37
StateSetter ? _stateSetter;
20
38
Timer ? _timer;
21
39
22
- void _pop (context) {
40
+ void _ok (context) async {
23
41
_timer? .cancel ();
42
+ _subscriptions.forEach ((element) {
43
+ element.cancel ();
44
+ });
24
45
Navigator .pop <DesktopCapturerSource >(context, _selected_source);
25
46
}
26
47
48
+ void _cancel (context) async {
49
+ _timer? .cancel ();
50
+ _subscriptions.forEach ((element) {
51
+ element.cancel ();
52
+ });
53
+ Navigator .pop <DesktopCapturerSource >(context, null );
54
+ }
55
+
27
56
Future <void > _getSources () async {
28
57
try {
29
58
var sources = await desktopCapturer.getSources (types: [_sourceType]);
@@ -32,7 +61,13 @@ class ScreenSelectDialog extends Dialog {
32
61
'name: ${element .name }, id: ${element .id }, type: ${element .type }' );
33
62
});
34
63
_stateSetter? .call (() {
35
- _sources = sources;
64
+ sources.forEach ((element) {
65
+ _sources[element.id] = element;
66
+ });
67
+ });
68
+ _timer? .cancel ();
69
+ _timer = Timer .periodic (Duration (seconds: 3 ), (timer) {
70
+ desktopCapturer.updateSources (types: [_sourceType]);
36
71
});
37
72
return ;
38
73
} catch (e) {
@@ -66,7 +101,7 @@ class ScreenSelectDialog extends Dialog {
66
101
alignment: Alignment .topRight,
67
102
child: InkWell (
68
103
child: Icon (Icons .close),
69
- onTap: () => _pop (context),
104
+ onTap: () => _cancel (context),
70
105
),
71
106
),
72
107
],
@@ -97,7 +132,7 @@ class ScreenSelectDialog extends Dialog {
97
132
tabs: [
98
133
Tab (
99
134
child: Text (
100
- 'Entrire Screen' ,
135
+ 'Entire Screen' ,
101
136
style: TextStyle (color: Colors .black54),
102
137
)),
103
138
Tab (
@@ -119,9 +154,9 @@ class ScreenSelectDialog extends Dialog {
119
154
child: GridView .count (
120
155
crossAxisSpacing: 8 ,
121
156
crossAxisCount: 2 ,
122
- children: _sources
157
+ children: _sources.entries
123
158
.where ((element) =>
124
- element.type ==
159
+ element.value. type ==
125
160
SourceType .Screen )
126
161
.map ((e) => Column (
127
162
children: [
@@ -131,7 +166,7 @@ class ScreenSelectDialog extends Dialog {
131
166
null &&
132
167
_selected_source!
133
168
.id ==
134
- e.id)
169
+ e.value. id)
135
170
? BoxDecoration (
136
171
border: Border .all (
137
172
width: 2 ,
@@ -141,16 +176,18 @@ class ScreenSelectDialog extends Dialog {
141
176
child: InkWell (
142
177
onTap: () {
143
178
print (
144
- 'Selected screen id => ${e .id }' );
179
+ 'Selected screen id => ${e .value . id }' );
145
180
setState (() {
146
181
_selected_source =
147
- e;
182
+ e.value ;
148
183
});
149
184
},
150
185
child:
151
- e.thumbnail != null
186
+ e.value.thumbnail !=
187
+ null
152
188
? Image .memory (
153
- e.thumbnail! ,
189
+ e.value
190
+ .thumbnail! ,
154
191
scale: 1.0 ,
155
192
repeat: ImageRepeat
156
193
.noRepeat,
@@ -159,15 +196,16 @@ class ScreenSelectDialog extends Dialog {
159
196
),
160
197
)),
161
198
Text (
162
- e.name,
199
+ e.value. name,
163
200
style: TextStyle (
164
201
fontSize: 12 ,
165
202
color: Colors .black87,
166
203
fontWeight: (_selected_source !=
167
204
null &&
168
205
_selected_source!
169
206
.id ==
170
- e.id)
207
+ e.value
208
+ .id)
171
209
? FontWeight .bold
172
210
: FontWeight
173
211
.normal),
@@ -183,9 +221,9 @@ class ScreenSelectDialog extends Dialog {
183
221
child: GridView .count (
184
222
crossAxisSpacing: 8 ,
185
223
crossAxisCount: 3 ,
186
- children: _sources
224
+ children: _sources.entries
187
225
.where ((element) =>
188
- element.type ==
226
+ element.value. type ==
189
227
SourceType .Window )
190
228
.map ((e) => Column (
191
229
children: [
@@ -195,7 +233,7 @@ class ScreenSelectDialog extends Dialog {
195
233
null &&
196
234
_selected_source!
197
235
.id ==
198
- e.id)
236
+ e.value. id)
199
237
? BoxDecoration (
200
238
border: Border .all (
201
239
width: 2 ,
@@ -205,33 +243,38 @@ class ScreenSelectDialog extends Dialog {
205
243
child: InkWell (
206
244
onTap: () {
207
245
print (
208
- 'Selected window id => ${e .id }' );
246
+ 'Selected window id => ${e .value . id }' );
209
247
setState (() {
210
248
_selected_source =
211
- e;
249
+ e.value ;
212
250
});
213
251
},
214
- child:
215
- e.thumbnail != null
216
- ? Image .memory (
217
- e.thumbnail! ,
218
- scale: 1.0 ,
219
- repeat: ImageRepeat
252
+ child: e
253
+ .value
254
+ .thumbnail!
255
+ .isNotEmpty
256
+ ? Image .memory (
257
+ e.value
258
+ .thumbnail! ,
259
+ scale: 1.0 ,
260
+ repeat:
261
+ ImageRepeat
220
262
.noRepeat,
221
- )
222
- : Container (),
263
+ )
264
+ : Container (),
223
265
),
224
266
)),
225
267
Text (
226
- e.name,
268
+ e.value. name,
227
269
style: TextStyle (
228
270
fontSize: 12 ,
229
271
color: Colors .black87,
230
272
fontWeight: (_selected_source !=
231
273
null &&
232
274
_selected_source!
233
275
.id ==
234
- e.id)
276
+ e.value
277
+ .id)
235
278
? FontWeight .bold
236
279
: FontWeight
237
280
.normal),
@@ -261,7 +304,7 @@ class ScreenSelectDialog extends Dialog {
261
304
style: TextStyle (color: Colors .black54),
262
305
),
263
306
onPressed: () {
264
- _pop (context);
307
+ _cancel (context);
265
308
},
266
309
),
267
310
MaterialButton (
@@ -270,7 +313,7 @@ class ScreenSelectDialog extends Dialog {
270
313
'Share' ,
271
314
),
272
315
onPressed: () {
273
- _pop (context);
316
+ _ok (context);
274
317
},
275
318
),
276
319
],
0 commit comments