Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ xattr -dr com.apple.quarantine "/Applications/MirrorCast.app"

按 `Esc` 取消录制。如果组合键被其他应用占用,MirrorCast 会保留原设置。

### 演示辅助(Windows)
### 演示辅助

镜像开始后,可以从运行状态面板或全局快捷键切换以下效果:

Expand All @@ -117,10 +117,12 @@ xattr -dr com.apple.quarantine "/Applications/MirrorCast.app"
- 指针聚光灯:压暗副屏其他区域并突出鼠标附近,默认 `Ctrl + Alt + Shift + P`。
- 屏幕标注:在主屏源窗口上绘制画笔、荧光笔、直线、箭头、矩形和椭圆,副屏同步显示,默认 `Ctrl + Alt + Shift + A`。

放大倍数、指针效果范围和四组快捷键均可在控制面板中调整。屏幕放大与指针放大镜互斥,聚光灯可以与任一模式组合。标注工具栏支持撤销、重做、清空和橡皮擦;按 `Esc` 可退出标注。标注内容仅在本次镜像期间保留。
放大倍数和指针效果范围可在控制面板中调整。Windows 版还支持分别设置四组组合快捷键。屏幕放大与指针放大镜互斥,聚光灯可以与任一模式组合。标注工具栏支持撤销、重做、清空和橡皮擦;按 `Esc` 可退出标注。标注内容仅在本次镜像期间保留。

默认开启“演示快捷模式”:仅在镜像期间使用 `F1` 开关屏幕放大、`F2` 开关指针放大镜、`F3` 开关指针聚光灯、`F4` 开关屏幕标注。按 `Esc` 会先退出最近开启的辅助功能;全部辅助功能都关闭后,再按一次 `Esc` 才会停止镜像。停止镜像后,MirrorCast 会立即释放这些按键。

Mac 键盘若将功能键用于亮度或音量控制,需要按住 `Fn` 再按对应功能键,或在系统设置中启用标准功能键。

## 缩放模式

| 平台 | 模式 | 效果 |
Expand Down
6 changes: 6 additions & 0 deletions mac/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,18 @@ xcode-select --install
- 源窗口改变大小时动态更新捕获配置
- 镜像期间单击窗口列表即可切换源窗口
- 鼠标指针显示开关
- GPU 路径的全屏放大、指针放大镜和指针聚光灯
- 主屏矢量标注工具栏,副屏同步显示画笔、荧光笔、图形和箭头
- 镜像期间使用 `F1`、`F2`、`F3`、`F4` 快速切换演示功能,按 `Esc` 逐层退出
- 源窗口关闭或目标显示器断开时自动停止
- 跨 Space 保持副屏镜像
- 副屏镜像窗口点击穿透
- 菜单栏常驻与自定义全局快捷键
- 显示器、缩放、指针、窗口和快捷键设置持久化
- 屏幕录制权限引导与五步使用教学

部分 Mac 键盘默认将功能键用于亮度、音量等系统控制,此时需要按住 `Fn` 再按 `F1` 至 `F4`,或在系统设置中启用“将 F1、F2 等键用作标准功能键”。演示快捷模式可以在控制面板中关闭。

## 构建应用

```bash
Expand Down Expand Up @@ -103,6 +108,7 @@ Intel 架构名为 `x86_64`,Apple Silicon 为 `arm64`,双架构包为 `unive
mac/Sources/MirrorCast/
├── Capture/ # ScreenCaptureKit 捕获与帧输出
├── Mirror/ # 副屏窗口、CALayer 显示和缩放模式
├── Presentation/ # 放大、聚光灯和矢量标注
├── Services/ # 权限、偏好设置、菜单栏和全局快捷键
├── UI/ # 控制面板、权限引导和使用教学
├── AppDelegate.swift
Expand Down
36 changes: 35 additions & 1 deletion mac/Sources/MirrorCast/AppDelegate.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import AppKit
import Carbon
import Combine
import SwiftUI

Expand All @@ -17,7 +18,7 @@ final class AppDelegate: NSObject, NSApplicationDelegate {
let hosting = NSHostingView(rootView: ControlPanelView(state: state))

let window = NSWindow(
contentRect: NSRect(x: 0, y: 0, width: 440, height: 680),
contentRect: NSRect(x: 0, y: 0, width: 460, height: 780),
styleMask: [.titled, .closable, .miniaturizable, .resizable],
backing: .buffered,
defer: false)
Expand Down Expand Up @@ -89,11 +90,44 @@ final class AppDelegate: NSObject, NSApplicationDelegate {
state.onHotKeyChangeRequested = { [weak self] combination in
self?.globalHotKey.replace(with: combination) == true
}
state.onMirroringStateChange = { [weak self] _ in
self?.updatePresentationHotKeys()
}
state.onPresentationKeyModeChange = { [weak self] in
self?.updatePresentationHotKeys()
}
if !registered {
state.status = "全局快捷键 \(state.hotKey.displayName) 注册失败,可能与其他应用冲突"
}
}

private func updatePresentationHotKeys() {
globalHotKey.unregisterPresentationKeys()
guard state.isMirroring, state.presentationKeyMode else { return }

let keys: [(String, UInt32, String, () -> Void)] = [
("f1", UInt32(kVK_F1), "F1", { [weak self] in self?.state.toggleScreenZoom() }),
("f2", UInt32(kVK_F2), "F2", { [weak self] in self?.state.toggleMagnifier() }),
("f3", UInt32(kVK_F3), "F3", { [weak self] in self?.state.toggleSpotlight() }),
("f4", UInt32(kVK_F4), "F4", { [weak self] in self?.state.toggleAnnotations() }),
("escape", UInt32(kVK_Escape), "Esc", { [weak self] in
guard let self else { return }
Task { await self.state.exitPresentationModeOrStop() }
})
]

var unavailable: [String] = []
for (key, keyCode, label, action) in keys {
let combination = HotKeyCombination(keyCode: keyCode, modifiers: 0, keyLabel: label)
if !globalHotKey.register(key: key, combination: combination, action: action) {
unavailable.append(label)
}
}
if !unavailable.isEmpty {
state.status = "演示快捷键 \(unavailable.joined(separator: "、")) 已被系统占用"
}
}

private func observeState() {
state.objectWillChange
.receive(on: RunLoop.main)
Expand Down
173 changes: 173 additions & 0 deletions mac/Sources/MirrorCast/AppState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ struct ScreenItem: Identifiable, Hashable {
@MainActor
final class AppState: ObservableObject {

private enum PresentationFeature {
case screenZoom, magnifier, spotlight, annotation
}

@Published private(set) var windows: [WindowItem] = []
@Published private(set) var screens: [ScreenItem] = []
@Published var selectedWindowID: CGWindowID?
Expand All @@ -45,8 +49,26 @@ final class AppState: ObservableObject {
@Published var showsPermissionOnboarding = false
@Published var walkthroughStep: WalkthroughStep?
@Published private(set) var hotKey = HotKeyCombination.defaultValue
@Published var presentationKeyMode = true {
didSet {
preferences.presentationKeyMode = presentationKeyMode
onPresentationKeyModeChange?()
}
}
@Published var presentationZoomFactor = 2.0 {
didSet { preferences.presentationZoomFactor = presentationZoomFactor }
}
@Published var pointerEffectSize = 240.0 {
didSet { preferences.pointerEffectSize = pointerEffectSize }
}
@Published private(set) var isScreenZoomActive = false
@Published private(set) var isMagnifierActive = false
@Published private(set) var isSpotlightActive = false
@Published private(set) var isAnnotationActive = false

var onHotKeyChangeRequested: ((HotKeyCombination) -> Bool)?
var onMirroringStateChange: ((Bool) -> Void)?
var onPresentationKeyModeChange: (() -> Void)?

/// SwiftUI only ever sees `WindowItem`; the live SCWindow objects stay here because
/// the capture filter needs the real thing.
Expand All @@ -55,9 +77,15 @@ final class AppState: ObservableObject {
private let capture = CaptureEngine()
private let mirrorWindow = MirrorWindowController()
private let preferences = PreferencesStore()
private let annotationDocument = AnnotationDocument()
private var mirrorMonitorTask: Task<Void, Never>?
private var pointerMonitorTask: Task<Void, Never>?
private var activeWindowID: CGWindowID?
private var isSwitchingSource = false
private var featureOrder: [PresentationFeature] = []
private lazy var annotationOverlay = AnnotationOverlayController(
document: annotationDocument,
onExit: { [weak self] in self?.toggleAnnotations() })

var canStart: Bool {
hasPermission && selectedWindowID != nil && selectedScreenID != nil && screens.count > 1
Expand All @@ -67,6 +95,9 @@ final class AppState: ObservableObject {
showsCursor = preferences.showsCursor
scaleMode = preferences.scaleMode
hotKey = preferences.hotKey
presentationKeyMode = preferences.presentationKeyMode
presentationZoomFactor = preferences.presentationZoomFactor
pointerEffectSize = preferences.pointerEffectSize
hasPermission = PermissionService.hasScreenRecordingAccess()
showsPermissionOnboarding = !hasPermission
walkthroughStep = hasPermission && !preferences.completedWalkthrough ? .sourceWindow : nil
Expand All @@ -84,6 +115,10 @@ final class AppState: ObservableObject {
self.status = "镜像已停止"
}
}
annotationDocument.onChange = { [weak self] in
guard let self else { return }
self.mirrorWindow.updateAnnotations(self.annotationDocument.items)
}
}

// MARK: - Permission
Expand Down Expand Up @@ -216,9 +251,12 @@ final class AppState: ObservableObject {
showsCursor: showsCursor,
scale: sourceScale)
isMirroring = true
resetPresentationFeatures()
activeWindowID = windowID
rememberSource(windowID)
startMirrorMonitor(windowID: windowID, targetScreenID: screenID)
startPointerMonitor()
onMirroringStateChange?(true)
let title = windows.first(where: { $0.id == windowID })?.title ?? "所选窗口"
status = "正在镜像:\(title)"
} catch {
Expand Down Expand Up @@ -258,6 +296,12 @@ final class AppState: ObservableObject {
status = "正在切换到:\(title)"

do {
if isAnnotationActive {
annotationOverlay.close()
isAnnotationActive = false
track(.annotation, active: false)
annotationDocument.reset()
}
try await capture.start(
window: window,
showsCursor: showsCursor,
Expand All @@ -278,9 +322,15 @@ final class AppState: ObservableObject {
private func finishMirroring() {
mirrorMonitorTask?.cancel()
mirrorMonitorTask = nil
pointerMonitorTask?.cancel()
pointerMonitorTask = nil
annotationOverlay.close()
resetPresentationFeatures()
activeWindowID = nil
mirrorWindow.close()
let wasMirroring = isMirroring
isMirroring = false
if wasMirroring { onMirroringStateChange?(false) }
}

func toggleMirroring() async {
Expand Down Expand Up @@ -331,6 +381,129 @@ final class AppState: ObservableObject {
updateHotKey(.defaultValue)
}

func toggleScreenZoom() {
guard isMirroring else { return }
isScreenZoomActive.toggle()
if isScreenZoomActive {
isMagnifierActive = false
track(.magnifier, active: false)
}
track(.screenZoom, active: isScreenZoomActive)
updatePresentation()
}

func toggleMagnifier() {
guard isMirroring else { return }
isMagnifierActive.toggle()
if isMagnifierActive {
isScreenZoomActive = false
track(.screenZoom, active: false)
}
track(.magnifier, active: isMagnifierActive)
updatePresentation()
}

func toggleSpotlight() {
guard isMirroring else { return }
isSpotlightActive.toggle()
track(.spotlight, active: isSpotlightActive)
updatePresentation()
}

func toggleAnnotations() {
guard isMirroring else { return }
isAnnotationActive.toggle()
if isAnnotationActive, let frame = activeSourceFrame() {
annotationOverlay.show(sourceFrame: frame)
} else {
annotationOverlay.close()
}
track(.annotation, active: isAnnotationActive)
}

func exitPresentationModeOrStop() async {
guard isMirroring else { return }
reconcileFeatureOrder()
guard let feature = featureOrder.last else {
await stopMirroring()
return
}
switch feature {
case .screenZoom: toggleScreenZoom()
case .magnifier: toggleMagnifier()
case .spotlight: toggleSpotlight()
case .annotation: toggleAnnotations()
}
}

private func track(_ feature: PresentationFeature, active: Bool) {
featureOrder.removeAll { $0 == feature }
if active { featureOrder.append(feature) }
}

private func reconcileFeatureOrder() {
featureOrder.removeAll { feature in
switch feature {
case .screenZoom: !isScreenZoomActive
case .magnifier: !isMagnifierActive
case .spotlight: !isSpotlightActive
case .annotation: !isAnnotationActive
}
}
}

private func resetPresentationFeatures() {
isScreenZoomActive = false
isMagnifierActive = false
isSpotlightActive = false
isAnnotationActive = false
featureOrder.removeAll()
annotationDocument.reset()
updatePresentation(pointer: CGPoint(x: 0.5, y: 0.5))
}

private func startPointerMonitor() {
pointerMonitorTask?.cancel()
pointerMonitorTask = Task { [weak self] in
while !Task.isCancelled {
try? await Task.sleep(nanoseconds: 33_000_000)
guard let self, self.isMirroring, !Task.isCancelled else { return }
self.updatePresentation()
if self.isAnnotationActive, let frame = self.activeSourceFrame() {
self.annotationOverlay.update(sourceFrame: frame)
}
}
}
}

private func updatePresentation(pointer: CGPoint? = nil) {
let normalized = pointer ?? normalizedPointerInSource()
mirrorWindow.updatePresentation(
pointer: normalized,
zoomFactor: isScreenZoomActive ? presentationZoomFactor : 1,
magnifierVisible: isMagnifierActive,
spotlightVisible: isSpotlightActive,
effectSize: pointerEffectSize)
}

private func activeSourceFrame() -> CGRect? {
guard let activeWindowID else { return nil }
return scWindows[activeWindowID]?.frame
}

private func normalizedPointerInSource() -> CGPoint {
guard let frame = activeSourceFrame(), frame.width > 0, frame.height > 0 else {
return CGPoint(x: 0.5, y: 0.5)
}
guard let capturePoint = CGEvent(source: nil)?.location else {
return CGPoint(x: 0.5, y: 0.5)
}
let normalizedX = (capturePoint.x - frame.minX) / frame.width
let normalizedYFromTop = (capturePoint.y - frame.minY) / frame.height
return CGPoint(x: min(max(normalizedX, 0), 1),
y: min(max(1 - normalizedYFromTop, 0), 1))
}

func acceptDefaultHotKeyFallback() {
hotKey = .defaultValue
preferences.hotKey = .defaultValue
Expand Down
Loading
Loading