-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
apps_list_cubit.dart
233 lines (192 loc) · 6.79 KB
/
apps_list_cubit.dart
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import 'dart:async';
import 'dart:io' as io;
import 'package:collection/collection.dart';
import 'package:equatable/equatable.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import '../../../settings/cubit/settings_cubit.dart';
import '../../../settings/settings_service.dart';
import '../../app_version/app_version.dart';
import '../../logs/logs.dart';
import '../../native_platform/native_platform.dart';
import '../apps_list.dart';
part 'apps_list_state.dart';
/// Convenience access to the main app cubit.
late AppsListCubit appsListCubit;
class AppsListCubit extends Cubit<AppsListState> {
final NativePlatform _nativePlatform;
final SettingsService _prefs;
final SettingsCubit _prefsCubit;
final ProcessRepository _processRepository;
final AppVersion _appVersion;
AppsListCubit({
required NativePlatform nativePlatform,
required SettingsService prefs,
required SettingsCubit prefsCubit,
required ProcessRepository processRepository,
required AppVersion appVersion,
bool testing = false,
}) : _nativePlatform = nativePlatform,
_prefs = prefs,
_prefsCubit = prefsCubit,
_processRepository = processRepository,
_appVersion = appVersion,
super(AppsListState.initial()) {
appsListCubit = this;
_initialize();
}
Future<void> _initialize() async {
await _fetchWindows();
emit(state.copyWith(loading: false));
setAutoRefresh(
autoRefresh: _prefsCubit.state.autoRefresh,
refreshInterval: _prefsCubit.state.refreshInterval,
);
await fetchVersionData();
}
/// Populate the list of visible windows.
Future<void> _fetchWindows() async {
var windows = await _nativePlatform.windows(
showHidden: _prefsCubit.state.showHiddenWindows,
);
// Filter out windows that are likely not desired or workable,
// for example the root window, unknown (0) pid, etc.
windows.removeWhere((element) => element.process.pid < 10);
for (var i = 0; i < windows.length; i++) {
windows[i] = await _refreshWindowProcess(windows[i]);
}
windows.sortWindows();
emit(state.copyWith(windows: windows));
}
Timer? _timer;
/// The timer which auto-refreshes the list of open windows.
///
/// [refreshInterval] is how often to refresh data, in seconds.
void setAutoRefresh({
required bool autoRefresh,
required int refreshInterval,
}) {
if (_timer != null) _timer?.cancel();
if (autoRefresh) {
_timer = Timer.periodic(
Duration(seconds: refreshInterval),
(timer) => _fetchWindows(),
);
}
}
/// Fetch version data so we can notify user of updates.
@visibleForTesting
Future<void> fetchVersionData() async {
final runningVersion = _appVersion.running();
final latestVersion = await _appVersion.latest();
final ignoredUpdate = _prefs.getString('ignoredUpdate');
final updateHasBeenIgnored = (latestVersion == ignoredUpdate);
final updateAvailable =
(updateHasBeenIgnored) ? false : await _appVersion.updateAvailable();
emit(state.copyWith(
runningVersion: runningVersion,
updateVersion: latestVersion,
updateAvailable: updateAvailable,
));
}
Future<void> manualRefresh() async {
emit(state.copyWith(loading: true));
await _fetchWindows();
emit(state.copyWith(loading: false));
}
/// Toggle suspend / resume for the process associated with the given window.
Future<bool> toggle(Window window) async {
window = await _refreshWindowProcess(window);
bool successful;
final interaction = (window.process.status == ProcessStatus.suspended)
? InteractionType.resume
: InteractionType.suspend;
log.v('Beginning ${interaction.name} for window: $window');
if (interaction == InteractionType.resume) {
successful = await _resume(window);
} else {
successful = await _suspend(window);
}
log.v('${interaction.name} was successful: $successful');
window = await _refreshWindowProcess(window);
log.v('Window after interaction: $window');
if (!successful) await _addInteractionError(window, interaction);
// Create a copy of the state of windows, with this window's info refreshed.
final windows = [...state.windows];
final index = windows.indexWhere((element) => element.id == window.id);
windows.replaceRange(
index,
index + 1,
[window],
);
emit(state.copyWith(
windows: windows,
));
return successful;
}
Future<bool> _resume(Window window) async {
final successful = await _processRepository.resume(window.process.pid);
// Restore the window _after_ resuming or it might not restore.
if (successful) await _restore(window);
return successful;
}
Future<bool> _suspend(Window window) async {
await _minimize(window);
final successful = await _processRepository.suspend(window.process.pid);
// If suspend failed, restore the window so the user won't mistakenly
// think that the suspend was successful.
if (!successful) await _restore(window);
return successful;
}
Future<void> _minimize(Window window) async {
if (!_prefsCubit.state.minimizeWindows) return;
// Minimize the window before suspending or it might not minimize.
await _nativePlatform.minimizeWindow(window.id);
// Small delay on Win32 to ensure the window actually minimizes.
// Doesn't seem to be necessary on Linux.
if (io.Platform.isWindows) {
await Future.delayed(const Duration(milliseconds: 500));
}
}
Future<void> _restore(Window window) async {
if (!_prefsCubit.state.minimizeWindows) return;
await _nativePlatform.restoreWindow(window.id);
}
/// Refresh the process status associated with [window].
Future<Window> _refreshWindowProcess(Window window) async {
final process = window.process;
return window.copyWith(
process: process.copyWith(
status: await _processRepository.getProcessStatus(process.pid),
),
);
}
/// Refresh the process status and add an [InteractionError].
Future<void> _addInteractionError(
Window window,
InteractionType interaction,
) async {
final interactionError = InteractionError(
interactionType: interaction,
statusAfterInteraction: window.process.status,
windowId: window.id,
);
final errors = [...state.interactionErrors] //
..addError(interactionError);
emit(state.copyWith(
interactionErrors: errors,
));
}
}
extension on List<InteractionError> {
void addError(InteractionError interactionError) {
removeWhere((e) => e.windowId == interactionError.windowId);
add(interactionError);
}
}
extension on List<Window> {
/// Sort the windows by executable name.
void sortWindows() {
sortBy((window) => window.process.executable.toLowerCase());
}
}