import SnapKit import UIKit /// 拍摄中状态区域:对齐体验网页的蓝色视频提示和禁用状态按钮,不执行任何操作。 @MainActor final class DroneDockTaskShootingView: UIView { /// 当前内容采用的排版参考比例,不通过 transform 缩小整个视图。 private(set) var effectiveScale: CGFloat = 1 /// 固定区域较小时收紧图标、字号和间距。 private(set) var usesCompactLayout = false private let content = UIView() private let header = UIView() private let iconBackground = UIView() private let icon = UIImageView(image: UIImage(systemName: "video.fill")) private let titleLabel = DroneDockStyle.label(size: 23, weight: .semibold, color: UIColor(hex: 0x101523)) private let messageLabel = DroneDockStyle.label(size: 14, color: UIColor(hex: 0x6A79A0)) private let indicatorButton = UIButton(type: .system) private var presentation: DroneDockTaskPresentation? private var headerHeight: Constraint? private var iconSize: Constraint? private var textLeading: Constraint? private var titleHeight: Constraint? private var messageTop: Constraint? private var messageHeight: Constraint? private var indicatorTop: Constraint? private var indicatorHeight: Constraint? /// 建立纯展示的拍摄中面板,外部任务页决定固定区域大小。 override init(frame: CGRect) { super.init(frame: frame) setupUI() setupConstraints() } @available(*, unavailable) required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } /// 展示已确认的拍摄中文案,按钮始终仅作状态指示。 func apply(_ presentation: DroneDockTaskPresentation) { self.presentation = presentation titleLabel.text = presentation.title messageLabel.text = presentation.message indicatorButton.isEnabled = false setNeedsLayout() } /// 仅可见状态面板提供通用访问标识,避免匹配到隐藏的标题或说明。 func setIdentifiersActive(_ active: Bool) { titleLabel.accessibilityIdentifier = active ? "drone.task.remote.statusTitle" : nil messageLabel.accessibilityIdentifier = active ? "drone.task.remote.statusMessage" : nil } /// 按可用高度调整内部排版,状态按钮始终保留至少 44 点高度。 override func layoutSubviews() { updateContentLayout() super.layoutSubviews() } private func setupUI() { accessibilityIdentifier = "drone.task.remote.shooting" backgroundColor = .white addSubview(content) content.addSubview(header) content.addSubview(indicatorButton) [iconBackground, titleLabel, messageLabel].forEach(header.addSubview) iconBackground.addSubview(icon) iconBackground.backgroundColor = UIColor(hex: 0xE2EDFF) iconBackground.layer.cornerRadius = 20.5 icon.contentMode = .scaleAspectFit icon.tintColor = UIColor(hex: 0x0671FF) titleLabel.accessibilityTraits = .header titleLabel.numberOfLines = 1 titleLabel.lineBreakMode = .byTruncatingTail messageLabel.numberOfLines = 2 messageLabel.lineBreakMode = .byTruncatingTail var configuration = UIButton.Configuration.filled() configuration.title = "拍摄中" configuration.image = UIImage(systemName: "camera") configuration.imagePadding = 12 configuration.preferredSymbolConfigurationForImage = UIImage.SymbolConfiguration(pointSize: 24, weight: .regular) configuration.background.backgroundColor = UIColor(hex: 0xD7E7FF) configuration.background.cornerRadius = 8 configuration.cornerStyle = .fixed configuration.baseForegroundColor = UIColor(hex: 0x4D91FF) configuration.contentInsets = NSDirectionalEdgeInsets(top: 0, leading: 8, bottom: 0, trailing: 8) indicatorButton.configuration = configuration indicatorButton.isEnabled = false indicatorButton.accessibilityIdentifier = "drone.task.remote.shootingIndicator" indicatorButton.configurationUpdateHandler = { button in button.configuration?.background.backgroundColor = UIColor(hex: 0xD7E7FF) button.configuration?.baseForegroundColor = UIColor(hex: 0x4D91FF) button.configuration?.imageColorTransformer = UIConfigurationColorTransformer { _ in UIColor(hex: 0x4D91FF) } } } private func setupConstraints() { content.snp.makeConstraints { make in make.leading.trailing.equalToSuperview().inset(18) make.centerY.equalToSuperview() } header.snp.makeConstraints { make in make.top.leading.trailing.equalToSuperview() headerHeight = make.height.equalTo(81).constraint } iconBackground.snp.makeConstraints { make in make.top.leading.equalToSuperview() iconSize = make.size.equalTo(41).constraint } icon.snp.makeConstraints { make in make.center.equalToSuperview() make.size.equalTo(iconBackground.snp.width).multipliedBy(24.0 / 41.0) } titleLabel.snp.makeConstraints { make in make.top.trailing.equalToSuperview() textLeading = make.leading.equalTo(iconBackground.snp.trailing).offset(12).constraint titleHeight = make.height.equalTo(30).constraint } messageLabel.snp.makeConstraints { make in messageTop = make.top.equalTo(titleLabel.snp.bottom).offset(6).constraint make.leading.trailing.equalTo(titleLabel) messageHeight = make.height.equalTo(45).constraint } indicatorButton.snp.makeConstraints { make in indicatorTop = make.top.equalTo(header.snp.bottom).offset(16).constraint make.leading.trailing.bottom.equalToSuperview() indicatorHeight = make.height.equalTo(50).constraint } } private func updateContentLayout() { guard let presentation, bounds.width > 0, bounds.height > 0 else { return } usesCompactLayout = bounds.height < 190 let extreme = bounds.height < 120 let compact = usesCompactLayout let diameter: CGFloat = extreme ? 24 : compact ? 32 : 41 let iconGap: CGFloat = extreme ? 8 : compact ? 10 : 12 let titleSize: CGFloat = extreme ? 17 : compact ? 20 : 23 let messageSize: CGFloat = compact ? 13 : 14 let title = max(ceil(UIFont.systemFont(ofSize: titleSize, weight: .semibold).lineHeight), compact ? 0 : 30) let textGap: CGFloat = extreme ? 3 : compact ? 4 : 6 let actionGap: CGFloat = extreme ? 8 : compact ? 12 : 16 let actionHeight: CGFloat = compact ? 44 : 50 let inset: CGFloat = extreme ? 4 : 6 let textWidth = max(1, bounds.width - 36 - diameter - iconGap) let available = max(1, bounds.height - inset * 2) let paragraph = NSMutableParagraphStyle() paragraph.minimumLineHeight = messageSize * 1.6 paragraph.maximumLineHeight = messageSize * 1.6 paragraph.lineBreakMode = .byTruncatingTail titleLabel.font = .systemFont(ofSize: titleSize, weight: .semibold) let messageFont = UIFont.systemFont(ofSize: messageSize) messageLabel.font = messageFont let attributes: [NSAttributedString.Key: Any] = [ .font: messageFont, .foregroundColor: UIColor(hex: 0x6A79A0), .paragraphStyle: paragraph ] messageLabel.attributedText = NSAttributedString(string: presentation.message, attributes: attributes) let measured = (presentation.message as NSString).boundingRect( with: CGSize(width: textWidth, height: .greatestFiniteMagnitude), options: [.usesLineFragmentOrigin, .usesFontLeading], attributes: attributes, context: nil).height var lines = extreme || presentation.message.isEmpty ? 0 : 2 var body = lines == 0 ? 0 : ceil(min(measured, paragraph.maximumLineHeight * CGFloat(lines))) while max(diameter, title + (lines > 0 ? textGap + body : 0)) + actionGap + actionHeight > available, lines > 0 { lines -= 1 body = lines == 0 ? 0 : ceil(min(measured, paragraph.maximumLineHeight * CGFloat(lines))) } messageLabel.numberOfLines = max(1, lines) messageLabel.isHidden = lines == 0 let header = max(diameter, title + (lines > 0 ? textGap + body : 0)) let natural = title + textGap + ceil(messageSize * 3.2) + actionGap + actionHeight effectiveScale = min(1, max(0.8, available / max(1, natural))) headerHeight?.update(offset: header) iconSize?.update(offset: diameter) iconBackground.layer.cornerRadius = diameter / 2 textLeading?.update(offset: iconGap) titleHeight?.update(offset: title) messageTop?.update(offset: lines > 0 ? textGap : 0) messageHeight?.update(offset: body) indicatorTop?.update(offset: actionGap) indicatorHeight?.update(offset: actionHeight) indicatorButton.configuration?.titleTextAttributesTransformer = UIConfigurationTextAttributesTransformer { attributes in var result = attributes result.font = .systemFont(ofSize: compact ? 16 : 18, weight: .semibold) result.foregroundColor = UIColor(hex: 0x4D91FF) return result } } }