|
| 1 | +// Copyright 2014 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import '../base/common.dart'; |
| 6 | +import '../base/file_system.dart'; |
| 7 | +import '../device.dart'; |
| 8 | +import '../globals.dart' as globals; |
| 9 | +import '../runner/flutter_command.dart'; |
| 10 | + |
| 11 | +class CaptureCommand extends FlutterCommand { |
| 12 | + CaptureCommand({required FileSystem fs}) { |
| 13 | + addSubcommand(CaptureImageCommand(fs: fs)); |
| 14 | + addSubcommand(CaptureVideoCommand(fs: fs)); |
| 15 | + } |
| 16 | + |
| 17 | + @override |
| 18 | + String get name => 'capture'; |
| 19 | + |
| 20 | + @override |
| 21 | + String get description => 'Capture a screenshot or screen recording from a connected device.'; |
| 22 | + |
| 23 | + @override |
| 24 | + String get category => FlutterCommandCategory.tools; |
| 25 | + |
| 26 | + @override |
| 27 | + Future<FlutterCommandResult> runCommand() async => FlutterCommandResult.fail(); |
| 28 | +} |
| 29 | + |
| 30 | +const String _kOut = 'out'; |
| 31 | + |
| 32 | +class CaptureImageCommand extends FlutterCommand { |
| 33 | + CaptureImageCommand({required this.fs}) { |
| 34 | + argParser.addOption( |
| 35 | + _kOut, |
| 36 | + abbr: 'o', |
| 37 | + valueHelp: 'path/to/file', |
| 38 | + help: 'Location to write the screenshot.', |
| 39 | + ); |
| 40 | + usesDeviceTimeoutOption(); |
| 41 | + usesDeviceConnectionOption(); |
| 42 | + } |
| 43 | + |
| 44 | + final FileSystem fs; |
| 45 | + |
| 46 | + @override |
| 47 | + String get name => 'image'; |
| 48 | + |
| 49 | + @override |
| 50 | + String get description => 'Take a screenshot from a connected device.'; |
| 51 | + |
| 52 | + @override |
| 53 | + final String category = FlutterCommandCategory.tools; |
| 54 | + |
| 55 | + @override |
| 56 | + bool get refreshWirelessDevices => true; |
| 57 | + |
| 58 | + Device? _device; |
| 59 | + |
| 60 | + @override |
| 61 | + Future<FlutterCommandResult> verifyThenRunCommand(String? commandPath) async { |
| 62 | + _device = await findTargetDevice(includeDevicesUnsupportedByProject: true); |
| 63 | + if (_device == null) { |
| 64 | + throwToolExit('No connected device found.'); |
| 65 | + } |
| 66 | + if (!_device!.supportsScreenshot) { |
| 67 | + throwToolExit('Screenshot not supported for ${_device!.displayName}.'); |
| 68 | + } |
| 69 | + return super.verifyThenRunCommand(commandPath); |
| 70 | + } |
| 71 | + |
| 72 | + @override |
| 73 | + Future<FlutterCommandResult> runCommand() async { |
| 74 | + final File outputFile = argResults?.wasParsed(_kOut) == true |
| 75 | + ? fs.file(stringArg(_kOut)) |
| 76 | + : globals.fsUtils.getUniqueFile(fs.currentDirectory, 'flutter', 'png'); |
| 77 | + |
| 78 | + try { |
| 79 | + await _device!.takeScreenshot(outputFile); |
| 80 | + } on ToolExit { |
| 81 | + rethrow; |
| 82 | + } on Exception catch (error) { |
| 83 | + throwToolExit('Error taking screenshot: $error'); |
| 84 | + } |
| 85 | + |
| 86 | + if (!fs.file(outputFile.path).existsSync()) { |
| 87 | + throwToolExit( |
| 88 | + 'File was not created, ensure path is valid\n' |
| 89 | + 'Path provided: "${outputFile.path}"', |
| 90 | + ); |
| 91 | + } |
| 92 | + |
| 93 | + final int sizeKB = outputFile.lengthSync() ~/ 1024; |
| 94 | + globals.printStatus( |
| 95 | + 'Screenshot written to ${fs.path.relative(outputFile.path)} (${sizeKB}kB).', |
| 96 | + ); |
| 97 | + return FlutterCommandResult.success(); |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +class CaptureVideoCommand extends FlutterCommand { |
| 102 | + CaptureVideoCommand({required this.fs}) { |
| 103 | + argParser.addOption( |
| 104 | + _kOut, |
| 105 | + abbr: 'o', |
| 106 | + valueHelp: 'path/to/file', |
| 107 | + help: 'Location to write the recording.', |
| 108 | + ); |
| 109 | + argParser.addOption( |
| 110 | + 'duration', |
| 111 | + abbr: 'd', |
| 112 | + valueHelp: 'seconds', |
| 113 | + help: 'Maximum recording duration in seconds. ' |
| 114 | + 'If not specified, recording continues until Ctrl-C is pressed.', |
| 115 | + ); |
| 116 | + usesDeviceTimeoutOption(); |
| 117 | + usesDeviceConnectionOption(); |
| 118 | + } |
| 119 | + |
| 120 | + final FileSystem fs; |
| 121 | + |
| 122 | + @override |
| 123 | + String get name => 'video'; |
| 124 | + |
| 125 | + @override |
| 126 | + String get description => 'Record the screen of a connected device.'; |
| 127 | + |
| 128 | + @override |
| 129 | + final String category = FlutterCommandCategory.tools; |
| 130 | + |
| 131 | + @override |
| 132 | + bool get refreshWirelessDevices => true; |
| 133 | + |
| 134 | + Device? _device; |
| 135 | + |
| 136 | + @override |
| 137 | + Future<FlutterCommandResult> verifyThenRunCommand(String? commandPath) async { |
| 138 | + _device = await findTargetDevice(includeDevicesUnsupportedByProject: true); |
| 139 | + if (_device == null) { |
| 140 | + throwToolExit('No connected device found.'); |
| 141 | + } |
| 142 | + if (!_device!.supportsScreenRecording) { |
| 143 | + throwToolExit('Screen recording not supported for ${_device!.displayName}.'); |
| 144 | + } |
| 145 | + return super.verifyThenRunCommand(commandPath); |
| 146 | + } |
| 147 | + |
| 148 | + @override |
| 149 | + Future<FlutterCommandResult> runCommand() async { |
| 150 | + final File outputFile = argResults?.wasParsed(_kOut) == true |
| 151 | + ? fs.file(stringArg(_kOut)) |
| 152 | + : globals.fsUtils.getUniqueFile(fs.currentDirectory, 'flutter', 'mp4'); |
| 153 | + |
| 154 | + Duration? duration; |
| 155 | + final String? durationStr = stringArg('duration'); |
| 156 | + if (durationStr != null) { |
| 157 | + final int? seconds = int.tryParse(durationStr); |
| 158 | + if (seconds == null || seconds <= 0) { |
| 159 | + throwToolExit('Invalid duration: "$durationStr". Must be a positive integer.'); |
| 160 | + } |
| 161 | + duration = Duration(seconds: seconds); |
| 162 | + } |
| 163 | + |
| 164 | + if (duration != null) { |
| 165 | + globals.printStatus( |
| 166 | + 'Recording ${_device!.displayName} for ${duration.inSeconds} seconds...', |
| 167 | + ); |
| 168 | + } else { |
| 169 | + globals.printStatus( |
| 170 | + 'Recording ${_device!.displayName}... Press Ctrl-C to stop.', |
| 171 | + ); |
| 172 | + } |
| 173 | + |
| 174 | + try { |
| 175 | + await _device!.startScreenRecording(outputFile, duration: duration); |
| 176 | + } on ToolExit { |
| 177 | + rethrow; |
| 178 | + } on Exception catch (error) { |
| 179 | + throwToolExit('Error recording screen: $error'); |
| 180 | + } |
| 181 | + |
| 182 | + if (!fs.file(outputFile.path).existsSync()) { |
| 183 | + throwToolExit( |
| 184 | + 'Recording file was not created, ensure path is valid\n' |
| 185 | + 'Path provided: "${outputFile.path}"', |
| 186 | + ); |
| 187 | + } |
| 188 | + |
| 189 | + final int sizeKB = outputFile.lengthSync() ~/ 1024; |
| 190 | + globals.printStatus( |
| 191 | + 'Recording written to ${fs.path.relative(outputFile.path)} (${sizeKB}kB).', |
| 192 | + ); |
| 193 | + return FlutterCommandResult.success(); |
| 194 | + } |
| 195 | +} |
0 commit comments