classifySurface function
Classifies scene (a native reader's snapshot) with its foreign owner window, when the platform names one.
Implementation
NativeSurface classifySurface(Scene scene, {String? owner}) {
final nodes = [
for (final n in _walk(scene.root))
if (n.axFrame != null && (n.textPreview?.trim().isNotEmpty ?? false)) n,
];
final photoCells = pickerCells(nodes);
final isPicker = _showsPicker(photoCells, owner);
final cellLabels = {for (final c in photoCells) c.label};
final choices = <SurfaceChoice>[
for (final n in nodes)
if (_isChoice(n, isPicker))
(
text: n.textPreview!.trim(),
id: n.glintId ?? '',
x: n.axFrame!.x + n.axFrame!.w / 2,
y: n.axFrame!.y + n.axFrame!.h / 2,
),
];
final texts = [
for (final n in nodes)
if (!choices.any((c) => c.id == n.glintId) && !(isPicker && cellLabels.contains(n.textPreview)))
n.textPreview!.trim(),
];
final question = texts.where(_permissionQuestion.hasMatch).firstOrNull;
final permissionOwner =
owner != null && RegExp('permissioncontroller|GrantPermissions', caseSensitive: false).hasMatch(owner);
final kind = question != null || permissionOwner
? SurfaceKind.permissionPrompt
: isPicker
? SurfaceKind.picker
: choices.isNotEmpty
? (_hasAlertStructure(texts, choices)
? SurfaceKind.alert
: (owner != null ? SurfaceKind.otherApp : SurfaceKind.sheet))
: owner != null
? SurfaceKind.otherApp
: SurfaceKind.unknown;
final title = question ?? texts.where((t) => t.endsWith('?') || t.length > 24).firstOrNull;
return NativeSurface(
kind: kind, title: title, choices: choices, texts: texts.where((t) => t != title).toList(), owner: owner);
}