Skip to content
Open
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@ build/
*.dylib
*.log
.DS_Store

# 构建产物:由 scripts/gen-icons.js 从 src/assets/*.svg 生成
src/generated/
5 changes: 3 additions & 2 deletions binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
[
"OS=='win'",
{
"sources": ["src/binding_windows.cpp"],
"sources": ["src/binding_windows.cpp", "src/screenshot_windows.cpp"],
"libraries": [
"user32.lib",
"kernel32.lib",
Expand All @@ -38,7 +38,8 @@
"uiautomationcore.lib",
"gdiplus.lib",
"dwmapi.lib",
"gdi32.lib"
"gdi32.lib",
"imm32.lib"
],
"msvs_settings": {
"VCCLCompilerTool": {
Expand Down
8 changes: 6 additions & 2 deletions scripts/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@ const platform = os.platform();
console.log(`🔨 Building for ${platform}...\n`);

try {
// Step 1: 编译 C++ 原生模块 (跨平台)
// Step 1: 把 SVG 资源生成成 C 头文件(截图工具栏图标)
console.log('🎨 Generating icon header from SVG...');
execSync('node scripts/gen-icons.js', { stdio: 'inherit' });

// Step 2: 编译 C++ 原生模块 (跨平台)
console.log('📦 Running node-gyp rebuild...');
execSync('npx node-gyp rebuild', { stdio: 'inherit' });

// Step 2: macOS 需要额外编译 Swift
// Step 3: macOS 需要额外编译 Swift
if (platform === 'darwin') {
console.log('\n🍎 Building Swift library for macOS...');
execSync('npm run build:swift', { stdio: 'inherit' });
Expand Down
90 changes: 90 additions & 0 deletions scripts/gen-icons.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#!/usr/bin/env node
/**
* 把 src/assets/*.svg 生成成 C 头文件 src/generated/icon_svgs.h。
*
* 输出形如:
* static const char* kIconSvg_Rect = "<svg ...>...</svg>";
*
* SVG 原文原样保留(含 currentColor),运行时再把 currentColor 替换成
* 目标颜色(normal/hover/active 三态),从而一套文本支持多色。
*
* 自动生成,请勿手动编辑。
*/
const fs = require('fs');
const path = require('path');

// 工具栏按钮 -> SVG 文件名映射(顺序与 src/screenshot_windows.cpp 的 ToolButton 枚举一致)
// 分隔线(Separator)对应 null,不生成图标。
const ICON_MAP = {
Rect: 'square',
Circle: 'circle',
Arrow: 'arrow-up-right',
Brush: 'pencil',
Mosaic: 'mosaic',
Text: 'text',
Translate: 'translate',
Undo: 'arrow-back-up',
Redo: 'arrow-forward-up',
Save: 'download',
Cancel: 'x',
Confirm: 'check',
};

const ASSETS_DIR = path.join(__dirname, '..', 'src', 'assets');
const OUT_DIR = path.join(__dirname, '..', 'src', 'generated');
const OUT_FILE = path.join(OUT_DIR, 'icon_svgs.h');

// C 字符串字面量转义:处理 " \ 和换行
function toCLiteral(str) {
// 把字符串按行拆开,每行用独立字面量拼接,保证可读性且避免超长行
return str
.replace(/\\/g, '\\\\')
.replace(/"/g, '\\"')
.split(/\r?\n/)
.map((line) => ` "${line}"`)
.join('\n');
}
Comment on lines +37 to +46

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

scripts/gen-icons.js 中,函数 toCLiteral 被定义了但从未在代码中被调用。为了保持代码的整洁和可维护性,建议将这段未使用的死代码删除。


function main() {
if (!fs.existsSync(ASSETS_DIR)) {
console.error(`✖ assets dir not found: ${ASSETS_DIR}`);
process.exit(1);
}
fs.mkdirSync(OUT_DIR, { recursive: true });

const lines = [];
lines.push('#pragma once');
lines.push('// AUTO-GENERATED by scripts/gen-icons.js — DO NOT EDIT.');
lines.push('// SVG 原文保留 currentColor,运行时替换为目标颜色。');
lines.push('');

for (const [key, fileBase] of Object.entries(ICON_MAP)) {
const svgPath = path.join(ASSETS_DIR, `${fileBase}.svg`);
if (!fs.existsSync(svgPath)) {
console.error(`✖ missing svg: ${svgPath}`);
process.exit(1);
}
const raw = fs.readFileSync(svgPath, 'utf8').trim();
// 折叠成单行,去掉换行,再按 C 字面量拼接(更紧凑)
const oneLine = raw.replace(/\r?\n/g, '').replace(/\s+/g, ' ');
lines.push(`static const char* kIconSvg_${key} =`);
lines.push(` "${oneLine.replace(/\\/g, '\\\\').replace(/"/g, '\\"')}";`);
lines.push('');
}

// 所有图标文本的数组(按 ToolButton 枚举顺序,分隔线为 nullptr)
lines.push('// 按 ToolButton 枚举顺序的 SVG 文本数组,分隔线为 nullptr。');
lines.push('static const char* const kIconSvgs[] = {');
// 顺序:Rect Circle Arrow Brush Mosaic Text Translate SEP1 Undo Redo SEP2 Save Cancel Confirm
lines.push(' kIconSvg_Rect, kIconSvg_Circle, kIconSvg_Arrow, kIconSvg_Brush,');
lines.push(' kIconSvg_Mosaic, kIconSvg_Text, kIconSvg_Translate, nullptr,');
lines.push(' kIconSvg_Undo, kIconSvg_Redo, nullptr,');
lines.push(' kIconSvg_Save, kIconSvg_Cancel, kIconSvg_Confirm');
lines.push('};');
lines.push('');

fs.writeFileSync(OUT_FILE, lines.join('\n'), 'utf8');
console.log(`✓ generated ${path.relative(path.join(__dirname, '..'), OUT_FILE)} (${Object.keys(ICON_MAP).length} icons)`);
}

main();
1 change: 1 addition & 0 deletions src/assets/arrow-back-up.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/assets/arrow-forward-up.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/assets/arrow-up-right.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/assets/check.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/assets/circle.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions src/assets/download.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions src/assets/mosaic.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/assets/pencil.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/assets/square.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/assets/text.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/assets/translate.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/assets/x.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Loading