Skip to content

Commit 0c200e9

Browse files
author
while
committed
调整 icon
1 parent 691f4c9 commit 0c200e9

9 files changed

Lines changed: 123 additions & 19 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424

2525
### 修复
2626

27+
- 修复 macOS 应用图标在 Dock 中偏大的问题,为图标添加透明留白并重新生成各分辨率资源。
28+
- 修复 macOS 截图框选层被系统菜单栏和程序坞遮挡、无法选取这些区域的问题,并显式开启菜单栏捕获。
2729
- 修复批量重命名规则卡片拖拽排序失效的问题:Windows 上 Tauri 接管了 WebView2 的原生拖放处理导致 HTML5 拖放的放置事件不触发,现改为 pointer 事件手动换位实现,规则与文件行拖拽排序均不受影响。
2830
- 修复批量重命名搜索替换勾选「同时作用于扩展名」时,查找内容带前导点(如 `.js`)无法命中扩展名的问题。
2931

‎packages/app/src-tauri/Cargo.lock‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎packages/app/src-tauri/Cargo.toml‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,9 @@ sysinfo = "0.39.6"
4646
windows = { version = "0.62", features = ["Win32_Foundation", "Win32_Graphics_Dwm", "Win32_NetworkManagement_IpHelper", "Win32_Networking_WinSock", "Win32_Security", "Win32_Storage", "Win32_System_IO", "Win32_System_Registry", "Win32_System_RestartManager", "Win32_System_Threading", "Win32_UI_Shell", "Win32_UI_WindowsAndMessaging"] }
4747

4848
[target.'cfg(target_os = "macos")'.dependencies]
49+
objc2-app-kit = { version = "0.3.2", default-features = false, features = ["std", "NSWindow", "NSResponder"] }
4950
objc2-foundation = { version = "0.3.2", features = ["NSProcessInfo"] }
50-
screencapturekit = { version = "8", features = ["macos_14_0"] }
51+
screencapturekit = { version = "8", features = ["macos_14_2"] }
5152

5253
[target.'cfg(not(any(target_os = "android", target_os = "ios")))'.dependencies]
5354
tauri-plugin-single-instance = "2"

‎packages/app/src-tauri/build.rs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ fn main() {
1010
// /usr/lib/swift 是系统自带 Swift 运行时(dyld shared cache)的解析路径,随系统存在;
1111
// @executable_path/../Frameworks 兜底,以便日后向 .app/Contents/Frameworks 内嵌库时无需改动。
1212
if std::env::var("CARGO_CFG_TARGET_OS").as_deref() == Ok("macos") {
13+
// Tauri 的开发版会把 icns 缓存后嵌入二进制,显式跟踪源文件以免继续使用旧图标。
14+
println!("cargo:rerun-if-changed=icons/icon.icns");
1315
println!("cargo:rustc-link-arg=-Wl,-rpath,/usr/lib/swift");
1416
println!("cargo:rustc-link-arg=-Wl,-rpath,@executable_path/../Frameworks");
1517
}
-606 KB
Binary file not shown.

‎packages/app/src-tauri/src/lib.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ pub fn run() {
8080
tools::screenshot::screenshot_capture,
8181
tools::screenshot::screenshot_latest,
8282
tools::screenshot::screenshot_latest_pixels,
83-
tools::screenshot::screenshot_overlay_disable_animation,
83+
tools::screenshot::screenshot_overlay_prepare,
8484
tools::screenshot::screenshot_save_selection,
8585
tools::ssh_keygen::ssh_key_generate,
8686
tools::system_info::system_info_summary,

‎packages/app/src-tauri/src/tools/screenshot/mod.rs‎

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -533,11 +533,20 @@ fn capture_monitor(monitor: &Monitor) -> Result<(Vec<u8>, u32, u32), String> {
533533
.round()
534534
.clamp(1.0, u32::MAX as f64) as u32;
535535

536-
let filter = SCContentFilter::create()
536+
// 使用整屏排除过滤器,保留桌面和程序坞;不要改为只包含应用窗口。
537+
let mut filter = SCContentFilter::create()
537538
.with_display(display)
538539
.with_excluding_windows(&[])
539540
.try_build()
540541
.map_err(|e| e.to_string())?;
542+
// includeMenuBar 从 macOS 14.2 开始可用,旧系统继续使用整屏过滤器的默认行为。
543+
if NSProcessInfo::processInfo().isOperatingSystemAtLeastVersion(NSOperatingSystemVersion {
544+
majorVersion: 14,
545+
minorVersion: 2,
546+
patchVersion: 0,
547+
}) {
548+
filter.set_include_menu_bar(true);
549+
}
541550
let configuration = SCStreamConfiguration::new()
542551
.with_width(output_width)
543552
.with_height(output_height)
@@ -578,17 +587,14 @@ pub fn screenshot_capture(
578587
Ok(())
579588
}
580589

581-
/// 关闭覆盖层窗口的系统入场/退场动画。
590+
/// 配置截图覆盖层的原生窗口行为,必须在展示前完成。
582591
///
583592
/// Windows 首次 show 一个窗口时,DWM 会播放「窗口打开」过渡(覆盖层表现为从小到大的缩放),
584593
/// 之后 hide/show 不再播放,所以只有第一次按快捷键能看到。DWMWA_TRANSITIONS_FORCEDISABLED
585594
/// 把这个窗口从 DWM 过渡动画里摘出去,覆盖层直接整屏出现。
586595
#[cfg(target_os = "windows")]
587596
#[tauri::command]
588-
pub fn screenshot_overlay_disable_animation(
589-
app: tauri::AppHandle,
590-
label: String,
591-
) -> Result<(), String> {
597+
pub fn screenshot_overlay_prepare(app: tauri::AppHandle, label: String) -> Result<(), String> {
592598
use tauri::Manager;
593599
use windows::Win32::Foundation::HWND;
594600
use windows::Win32::Graphics::Dwm::{DwmSetWindowAttribute, DWMWA_TRANSITIONS_FORCEDISABLED};
@@ -610,12 +616,37 @@ pub fn screenshot_overlay_disable_animation(
610616
.map_err(|e| e.to_string())
611617
}
612618

613-
#[cfg(not(target_os = "windows"))]
619+
/// 普通 alwaysOnTop 在 macOS 上只到 floating 层级,低于菜单栏和程序坞。
620+
/// 将框选窗口提升到 screenSaver 层级,确保屏幕边缘也能绘制选区并接收鼠标事件。
621+
#[cfg(target_os = "macos")]
614622
#[tauri::command]
615-
pub fn screenshot_overlay_disable_animation(
616-
_app: tauri::AppHandle,
617-
_label: String,
623+
pub async fn screenshot_overlay_prepare(
624+
app: tauri::AppHandle,
625+
label: String,
618626
) -> Result<(), String> {
627+
use objc2_app_kit::{NSScreenSaverWindowLevel, NSWindow, NSWindowAnimationBehavior};
628+
use tauri::Manager;
629+
630+
let window = app
631+
.get_webview_window(&label)
632+
.ok_or_else(|| format!("窗口不存在:{label}"))?;
633+
let (sender, receiver) = tokio::sync::oneshot::channel();
634+
window
635+
.with_webview(move |webview| {
636+
// SAFETY: Tauri 在主线程执行此回调,ns_window 是该 WebView 所属的有效 NSWindow。
637+
let window = unsafe { &*webview.ns_window().cast::<NSWindow>() };
638+
window.setLevel(NSScreenSaverWindowLevel);
639+
window.setAnimationBehavior(NSWindowAnimationBehavior::None);
640+
let _ = sender.send(());
641+
})
642+
.map_err(|e| e.to_string())?;
643+
// 等原生配置真正应用后再允许前端定位和展示窗口。
644+
receiver.await.map_err(|e| e.to_string())
645+
}
646+
647+
#[cfg(not(any(target_os = "windows", target_os = "macos")))]
648+
#[tauri::command]
649+
pub fn screenshot_overlay_prepare(_app: tauri::AppHandle, _label: String) -> Result<(), String> {
619650
Ok(())
620651
}
621652

‎packages/app/src/tools/media/screenshot/screenshot.ts‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,12 @@ async function makeOverlayIdle(win: Window): Promise<void> {
111111
await win.hide()
112112
}
113113

114-
/** 关闭覆盖层的系统入场动画(Windows DWM 首次 show 会有从小到大的缩放过渡)。 */
115-
async function disableOverlayAnimation(): Promise<void> {
114+
/** 展示前配置原生覆盖层:macOS 覆盖菜单栏/程序坞,Windows 关闭 DWM 入场动画。 */
115+
async function prepareOverlay(): Promise<void> {
116116
try {
117-
await invoke('screenshot_overlay_disable_animation', { label: OVERLAY_LABEL })
117+
await invoke('screenshot_overlay_prepare', { label: OVERLAY_LABEL })
118118
} catch (error) {
119-
console.warn('关闭截图覆盖层入场动画失败:', error)
119+
console.warn('配置截图覆盖层失败:', error)
120120
}
121121
}
122122

@@ -158,8 +158,8 @@ async function createOverlay(): Promise<WebviewWindow> {
158158
reject(new Error(`创建截图覆盖层失败:${event.payload}`))
159159
})
160160
})
161-
// 原生窗口存在后立刻摘掉系统过渡动画,早于任何 show。
162-
await disableOverlayAnimation()
161+
// 原生窗口存在后立刻设置层级并关闭系统过渡动画,早于定位和任何 show。
162+
await prepareOverlay()
163163
// 首次 show 前先给隐藏窗口一个真实的显示器尺寸。否则原生窗口会以默认尺寸创建,
164164
// 再在第一张截图时放大到整屏,Windows 上会出现明显的缩放入场效果。
165165
await primeOverlayBounds(overlay)
@@ -174,7 +174,7 @@ export async function ensureOverlay(): Promise<WebviewWindow> {
174174
const existing = await WebviewWindow.getByLabel(OVERLAY_LABEL)
175175
if (existing) {
176176
// 保持隐藏;真正的 show 由 CAPTURE_READY 回调在定位并绘制画面后执行。
177-
await disableOverlayAnimation()
177+
await prepareOverlay()
178178
await primeOverlayBounds(existing)
179179
await makeOverlayIdle(existing)
180180
return existing

‎scripts/mac-icon.swift‎

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#!/usr/bin/env swift
2+
// Regenerate the macOS icon without changing the shared source or Windows/Linux icons.
3+
import Foundation
4+
import CoreGraphics
5+
import ImageIO
6+
7+
let root = URL(fileURLWithPath: #filePath).deletingLastPathComponent().deletingLastPathComponent()
8+
let icons = root.appendingPathComponent("packages/app/src-tauri/icons")
9+
10+
guard let source = CGImageSourceCreateWithURL(icons.appendingPathComponent("icon.png") as CFURL, nil),
11+
let image = CGImageSourceCreateImageAtIndex(source, 0, nil) else {
12+
fatalError("Cannot read icons/icon.png")
13+
}
14+
15+
// Keep the rounded-square artwork inside an 856 px square on a 1024 px canvas.
16+
// The transparent inset prevents it from looking oversized in the Dock.
17+
let representations = [
18+
(16, 1, "icp4"), (16, 2, "ic11"),
19+
(32, 1, "icp5"), (32, 2, "ic12"),
20+
(128, 1, "ic07"), (128, 2, "ic13"),
21+
(256, 1, "ic08"), (256, 2, "ic14"),
22+
(512, 1, "ic09"), (512, 2, "ic10"),
23+
]
24+
25+
func chunkHeader(_ type: String, _ length: Int) -> Data {
26+
var size = UInt32(length).bigEndian
27+
var header = Data(type.utf8)
28+
withUnsafeBytes(of: &size) { header.append(contentsOf: $0) }
29+
return header
30+
}
31+
32+
var chunks = Data()
33+
var tableOfContents = Data()
34+
for (points, scale, type) in representations {
35+
let pixels = points * scale
36+
guard let context = CGContext(
37+
data: nil, width: pixels, height: pixels, bitsPerComponent: 8, bytesPerRow: pixels * 4,
38+
space: CGColorSpace(name: CGColorSpace.sRGB)!,
39+
bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue
40+
) else { fatalError("Cannot create \(pixels) px canvas") }
41+
let canvas = CGFloat(pixels)
42+
let artwork = (canvas * 856 / 1024).rounded()
43+
let inset = (canvas - artwork) / 2
44+
context.clear(CGRect(x: 0, y: 0, width: canvas, height: canvas))
45+
context.interpolationQuality = .high
46+
context.draw(image, in: CGRect(x: inset, y: inset, width: artwork, height: artwork))
47+
48+
let png = NSMutableData()
49+
guard let result = context.makeImage(),
50+
let destination = CGImageDestinationCreateWithData(png, "public.png" as CFString, 1, nil) else {
51+
fatalError("Cannot encode \(pixels) px icon")
52+
}
53+
CGImageDestinationAddImage(destination, result, nil)
54+
guard CGImageDestinationFinalize(destination) else { fatalError("Cannot write \(pixels) px icon") }
55+
let header = chunkHeader(type, png.length + 8)
56+
tableOfContents.append(header)
57+
chunks.append(header)
58+
chunks.append(png as Data)
59+
}
60+
61+
// ICNS stores PNG representations in typed chunks with big-endian lengths.
62+
var output = chunkHeader("icns", 8 + 8 + tableOfContents.count + chunks.count)
63+
output.append(chunkHeader("TOC ", tableOfContents.count + 8))
64+
output.append(tableOfContents)
65+
output.append(chunks)
66+
try output.write(to: icons.appendingPathComponent("icon.icns"), options: .atomic)
67+
print("Generated icons/icon.icns (856/1024 artwork, transparent padding)")

0 commit comments

Comments
 (0)