@@ -8,22 +8,51 @@ class ScreenSelectDialog extends Dialog {
88 ScreenSelectDialog () {
99 Future .delayed (Duration (milliseconds: 100 ), () {
1010 _getSources ();
11- _timer = Timer .periodic (Duration (milliseconds: 2000 ), (timer) {
12- _getSources ();
13- });
1411 });
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+ }));
1532 }
16- List < DesktopCapturerSource > _sources = [] ;
33+ final Map < String , DesktopCapturerSource > _sources = {} ;
1734 SourceType _sourceType = SourceType .Screen ;
1835 DesktopCapturerSource ? _selected_source;
36+ final List <StreamSubscription <DesktopCapturerSource >> _subscriptions = [];
1937 StateSetter ? _stateSetter;
2038 Timer ? _timer;
2139
22- void _pop (context) {
40+ void _ok (context) async {
2341 _timer? .cancel ();
42+ _subscriptions.forEach ((element) {
43+ element.cancel ();
44+ });
2445 Navigator .pop <DesktopCapturerSource >(context, _selected_source);
2546 }
2647
48+ void _cancel (context) async {
49+ _timer? .cancel ();
50+ _subscriptions.forEach ((element) {
51+ element.cancel ();
52+ });
53+ Navigator .pop <DesktopCapturerSource >(context, null );
54+ }
55+
2756 Future <void > _getSources () async {
2857 try {
2958 var sources = await desktopCapturer.getSources (types: [_sourceType]);
@@ -32,7 +61,13 @@ class ScreenSelectDialog extends Dialog {
3261 'name: ${element .name }, id: ${element .id }, type: ${element .type }' );
3362 });
3463 _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]);
3671 });
3772 return ;
3873 } catch (e) {
@@ -66,7 +101,7 @@ class ScreenSelectDialog extends Dialog {
66101 alignment: Alignment .topRight,
67102 child: InkWell (
68103 child: Icon (Icons .close),
69- onTap: () => _pop (context),
104+ onTap: () => _cancel (context),
70105 ),
71106 ),
72107 ],
@@ -97,7 +132,7 @@ class ScreenSelectDialog extends Dialog {
97132 tabs: [
98133 Tab (
99134 child: Text (
100- 'Entrire Screen' ,
135+ 'Entire Screen' ,
101136 style: TextStyle (color: Colors .black54),
102137 )),
103138 Tab (
@@ -119,9 +154,9 @@ class ScreenSelectDialog extends Dialog {
119154 child: GridView .count (
120155 crossAxisSpacing: 8 ,
121156 crossAxisCount: 2 ,
122- children: _sources
157+ children: _sources.entries
123158 .where ((element) =>
124- element.type ==
159+ element.value. type ==
125160 SourceType .Screen )
126161 .map ((e) => Column (
127162 children: [
@@ -131,7 +166,7 @@ class ScreenSelectDialog extends Dialog {
131166 null &&
132167 _selected_source!
133168 .id ==
134- e.id)
169+ e.value. id)
135170 ? BoxDecoration (
136171 border: Border .all (
137172 width: 2 ,
@@ -141,16 +176,18 @@ class ScreenSelectDialog extends Dialog {
141176 child: InkWell (
142177 onTap: () {
143178 print (
144- 'Selected screen id => ${e .id }' );
179+ 'Selected screen id => ${e .value . id }' );
145180 setState (() {
146181 _selected_source =
147- e;
182+ e.value ;
148183 });
149184 },
150185 child:
151- e.thumbnail != null
186+ e.value.thumbnail !=
187+ null
152188 ? Image .memory (
153- e.thumbnail! ,
189+ e.value
190+ .thumbnail! ,
154191 scale: 1.0 ,
155192 repeat: ImageRepeat
156193 .noRepeat,
@@ -159,15 +196,16 @@ class ScreenSelectDialog extends Dialog {
159196 ),
160197 )),
161198 Text (
162- e.name,
199+ e.value. name,
163200 style: TextStyle (
164201 fontSize: 12 ,
165202 color: Colors .black87,
166203 fontWeight: (_selected_source !=
167204 null &&
168205 _selected_source!
169206 .id ==
170- e.id)
207+ e.value
208+ .id)
171209 ? FontWeight .bold
172210 : FontWeight
173211 .normal),
@@ -183,9 +221,9 @@ class ScreenSelectDialog extends Dialog {
183221 child: GridView .count (
184222 crossAxisSpacing: 8 ,
185223 crossAxisCount: 3 ,
186- children: _sources
224+ children: _sources.entries
187225 .where ((element) =>
188- element.type ==
226+ element.value. type ==
189227 SourceType .Window )
190228 .map ((e) => Column (
191229 children: [
@@ -195,7 +233,7 @@ class ScreenSelectDialog extends Dialog {
195233 null &&
196234 _selected_source!
197235 .id ==
198- e.id)
236+ e.value. id)
199237 ? BoxDecoration (
200238 border: Border .all (
201239 width: 2 ,
@@ -205,33 +243,38 @@ class ScreenSelectDialog extends Dialog {
205243 child: InkWell (
206244 onTap: () {
207245 print (
208- 'Selected window id => ${e .id }' );
246+ 'Selected window id => ${e .value . id }' );
209247 setState (() {
210248 _selected_source =
211- e;
249+ e.value ;
212250 });
213251 },
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
220262 .noRepeat,
221- )
222- : Container (),
263+ )
264+ : Container (),
223265 ),
224266 )),
225267 Text (
226- e.name,
268+ e.value. name,
227269 style: TextStyle (
228270 fontSize: 12 ,
229271 color: Colors .black87,
230272 fontWeight: (_selected_source !=
231273 null &&
232274 _selected_source!
233275 .id ==
234- e.id)
276+ e.value
277+ .id)
235278 ? FontWeight .bold
236279 : FontWeight
237280 .normal),
@@ -261,7 +304,7 @@ class ScreenSelectDialog extends Dialog {
261304 style: TextStyle (color: Colors .black54),
262305 ),
263306 onPressed: () {
264- _pop (context);
307+ _cancel (context);
265308 },
266309 ),
267310 MaterialButton (
@@ -270,7 +313,7 @@ class ScreenSelectDialog extends Dialog {
270313 'Share' ,
271314 ),
272315 onPressed: () {
273- _pop (context);
316+ _ok (context);
274317 },
275318 ),
276319 ],
0 commit comments