提交 c4be389e authored 作者: 蔡芬's avatar 蔡芬

3

上级 ce90d636
...@@ -108,11 +108,18 @@ class IpcEvents { ...@@ -108,11 +108,18 @@ class IpcEvents {
// 某个路径转成base64 // 某个路径转成base64
ipcMain.on("getBase64", (e, {imagePath,index}) => { ipcMain.on("getBase64", (e, {imagePath,index}) => {
try { try {
const imageBase64 = fs.readFileSync(imagePath, 'base64'); let src = '';
let src = `data:image/png;base64,${imageBase64}`; if(imagePath && imagePath.indexOf('.png') > 0) {
src = this.softList.pngToBase64(imagePath);
}else if(imagePath && imagePath.indexOf('.svg') > 0) {
console.log('svg获取突破base64')
src = this.softList.svgToBase64(imagePath);
}
// const imageBase64 = fs.readFileSync(imagePath, 'base64');
// let src = `data:image/png;base64,${imageBase64}`;
let apps = this.store.get("apps"); let apps = this.store.get("apps");
src = src.replace(/[\r\n]/g, ''); // src = src.replace(/[\r\n]/g, '');
apps[index].icon = src; apps[index].icon = src || imagePath;
this.store.set("apps", apps); this.store.set("apps", apps);
e.sender.send("base64Info", {url:src,index}); e.sender.send("base64Info", {url:src,index});
......
...@@ -11,19 +11,22 @@ class SoftList { ...@@ -11,19 +11,22 @@ class SoftList {
/* 获取app列表 */ /* 获取app列表 */
getList () { getList () {
console.log('获取app列表') console.log('获取app列表')
this.mainWindow.win.webContents.send('getMsg', '获取app列表')
let appsList = this.store.get('apps'); let appsList = this.store.get('apps');
if(appsList && appsList.length > 0) { // if(appsList && appsList.length > 0) {
// 已有app列表 // // 已有app列表
console.log('apps-length', appsList.length) // console.log('apps-length', appsList.length)
return; // return;
} // }
if (process.platform === 'linux') { if (process.platform === 'linux') {
// 当前是Linux平台 // 当前是Linux平台
console.log('当前是Linux平台') console.log('当前是Linux平台')
this.mainWindow.win.webContents.send('getMsg', '当前是Linux平台')
this.getLinuxList(); this.getLinuxList();
} else if (process.platform === 'darwin') { } else if (process.platform === 'darwin') {
// 当前是macOS平台 // 当前是macOS平台
console.log('当前是macOS平台') console.log('当前是macOS平台')
this.mainWindow.win.webContents.send('getMsg', '当前是macOS平台')
this.getMacList(); this.getMacList();
} else { } else {
// 其他平台 // 其他平台
...@@ -31,16 +34,62 @@ class SoftList { ...@@ -31,16 +34,62 @@ class SoftList {
} }
/* 查找linux icon */ /* 查找linux icon */
findIcon(iconName) { findIcon(iconName, pathBase) { ///usr/share/icons
try { try {
const stdout = execSync(`find /usr/share/icons -name "${iconName}.*"`); const stdout = execSync(`find ${pathBase} -name "${iconName}.*"`);
const iconPaths = stdout.toString().split('\n').filter(path => path.trim() !== ''); const iconPaths = stdout.toString().split('\n').filter(path => path && path.trim() !== '');
return iconPaths; return iconPaths;
} catch (error) { } catch (error) {
console.error(`命令执行失败: ${error}`); console.error(`命令执行失败: ${error}`);
return null; return null;
} }
} }
/* 获取最大像素图片 */
getMaxPix({name, iconList, icon}) {
let hicolorIcon = '';
let locolorIcon = '';
let hiMax = 0;
let loMax = 0;
iconList.forEach(v => {
if(v && typeof v =='string' && v.indexOf('/hicolor/')>0) {
// /usr/share/icons/hicolor/48x48/apps/gvim.png
let str = v.replace('/usr/share/icons/hicolor/','')
if(name && name.indexOf('Visual Studio Code') >= 0) console.log('str',str)
if(str.indexOf('/')>0 && str.split('/')[0] && str.split('/')[0].indexOf('x')>0) {
let curHi = Number(str.split('/')[0].split('x')[0]);
if(name && name.indexOf('Visual Studio Code') >= 0) console.log('curHi', curHi)
if(curHi > hiMax) {
hiMax = curHi;
hicolorIcon = v;
}
}
// hicolorIcon = v;
}
if(v && typeof v =='string' && v.indexOf('/locolor/')>0) {
// /usr/share/icons/locolor/48x48/apps/gvim.png
let str = v.replace('/usr/share/icons/locolor/','')
if(str.indexOf('/')>0 && str.split('/')[0] && str.split('/')[0].indexOf('x')>0) {
let curLo = Number(str.split('/')[0].split('x')[0]);
if(curLo > loMax) {
loMax = curLo;
locolorIcon = v;
}
}
// locolorIcon = v;
}
})
if(hicolorIcon) icon = hicolorIcon;
else if(locolorIcon) icon = locolorIcon;
else {
iconList = iconList.filter(v => v && (v.indexOf('.png')>0 || v.indexOf('.svg')>0))
icon = iconList[0];
}
console.log(name)
if(name && name.indexOf('Visual Studio Code') >= 0) console.log(iconList)
if(name && name.indexOf('Visual Studio Code') >= 0 ) console.log(hicolorIcon,locolorIcon)
return icon;
}
/* 获取linux 快捷方式 列表 */ /* 获取linux 快捷方式 列表 */
getLinuxList() { getLinuxList() {
exec('ls /usr/share/applications/*.desktop', (error, stdout, stderr) => { exec('ls /usr/share/applications/*.desktop', (error, stdout, stderr) => {
...@@ -48,52 +97,62 @@ class SoftList { ...@@ -48,52 +97,62 @@ class SoftList {
console.error(`exec error: ${error}`); console.error(`exec error: ${error}`);
return; return;
} }
const apps = stdout.trim().split('\n').map(file => { try {
const content = fs.readFileSync(file, 'utf8'); const apps = stdout.trim().split('\n').map(file => {
const lines = content.trim().split('\n'); const content = fs.readFileSync(file, 'utf8');
const nameLine = lines.find(line => line.startsWith('Name=')); const lines = content.trim().split('\n');
const name = nameLine ? nameLine.substr(5) : path.basename(file, '.desktop'); const nameLine = lines.find(line => line.startsWith('Name='));
const iconLine = lines.find(line => line.startsWith('Icon=')); const name = nameLine ? nameLine.substr(5) : path.basename(file, '.desktop');
let icon = iconLine ? iconLine.substr(5) : null; const iconLine = lines.find(line => line.startsWith('Icon='));
const execLine = lines.find(line => line.startsWith('Exec=')); let icon = iconLine ? iconLine.substr(5) : null;
const execCommand = execLine ? execLine.substr(5) : null; const execLine = lines.find(line => line.startsWith('Exec='));
const nameOfZhLine = lines.find(line => line.startsWith('Name[zh_CN]=')); const execCommand = execLine ? execLine.substr(5) : null;
const nameOfZh = nameOfZhLine ? nameOfZhLine.substr(5) : ''; const nameOfZhLine = lines.find(line => line.startsWith('Name[zh_CN]='));
// icon判断是否绝对路径 const nameOfZh = nameOfZhLine ? nameOfZhLine.substr(5) : '';
if( !path.isAbsolute( icon ) ) {
let iconList = this.findIcon(icon ); let iconList = [];
if(iconList.length > 0) { // icon判断是否绝对路径
icon = iconList[0]; if( icon && !path.isAbsolute( icon ) ) {
iconList = this.findIcon(icon,'/usr/share/icons');
if(iconList.length > 0) {
icon = this.getMaxPix({name, iconList, icon})
}else {
iconList = this.findIcon(icon,'/usr/share');
icon = this.getMaxPix({name, iconList, icon})
}
} }
} // 图标为SVG或PNG格式
// 图标为SVG或PNG格式 // if(icon && icon.indexOf(".png") == -1) {
// if(icon && icon.indexOf(".png") == -1) { // icon = `/usr/share/icons/hicolor/48x48/apps/${icon}.png`
// icon = `/usr/share/icons/hicolor/48x48/apps/${icon}.png` // }
// } // icon = `/usr/share/icons/hicolor/48x48/apps/${icon}`
// icon = `/usr/share/icons/hicolor/48x48/apps/${icon}` // 2. 打开应用程序的.desktop文件,并查找“Icon=”行。这一行包含应用程序的图标文件的路径。
// 2. 打开应用程序的.desktop文件,并查找“Icon=”行。这一行包含应用程序的图标文件的路径。
// 3. 使用以下代码来将图标文件转换为base64编码:
// ```
// base64 -w 0 /usr/share/icons/hicolor/48x48/apps/${icon}
// ```
// try {
// var res = execSync(`base64 -w 0 /usr/share/icons/hicolor/48x48/apps/${icon}.png`
// , { encoding: 'utf8' });
// if(res) icon = res;
// } catch (error) {
// console.error(error)
// }
return {
nameOfZh,
name,
icon,
execCommand,
};
});
this.store.set('apps', apps); // 3. 使用以下代码来将图标文件转换为base64编码:
this.mainWindow.win.webContents.send("appsList", apps); // ```
// base64 -w 0 /usr/share/icons/hicolor/48x48/apps/${icon}
// ```
// try {
// var res = execSync(`base64 -w 0 /usr/share/icons/hicolor/48x48/apps/${icon}.png`
// , { encoding: 'utf8' });
// if(res) icon = res;
// } catch (error) {
// console.error(error)
// }
return {
iconList: iconList || [],
nameOfZh,
name,
icon,
execCommand,
};
});
let newApps = apps.filter(v => !!v.icon);
this.store.set('apps', newApps);
this.mainWindow.win.webContents.send("appsList", newApps);
} catch (error) {
console.error(error)
}
}); });
} }
...@@ -219,19 +278,29 @@ class SoftList { ...@@ -219,19 +278,29 @@ class SoftList {
// png转base64 // png转base64
pngToBase64(imagePath) { pngToBase64(imagePath) {
let src = imagePath;
try {
const imageBase64 = fs.readFileSync(imagePath, 'base64'); const imageBase64 = fs.readFileSync(imagePath, 'base64');
let src = `data:image/png;base64,${imageBase64}`; src = `data:image/png;base64,${imageBase64}`;
src = src.replace(/[\r\n]/g, ''); src = src.replace(/[\r\n]/g, '');
return src; } catch (error) {
}
return src;
} }
// SVG文件转换为Base64 // SVG文件转换为Base64
svgToBase64(svgPath) { svgToBase64(svgPath) {
let src = svgPath;
try {
const svg = fs.readFileSync(svgPath, 'utf-8'); const svg = fs.readFileSync(svgPath, 'utf-8');
// 把SVG文件转换为Base64字符串 // 把SVG文件转换为Base64字符串
const base64 = Buffer.from(svg).toString('base64'); const base64 = Buffer.from(svg).toString('base64');
// <img src="data:image/svg+xml;base64,${base64}" /> src = `data:image/svg+xml;base64,${base64}`;
return `data:image/svg+xml;base64,${base64}`; } catch (error) {
console.log(error)
}
return src;
} }
// 获取linux软件信息 // 获取linux软件信息
......
...@@ -102,7 +102,7 @@ class MainWindow { ...@@ -102,7 +102,7 @@ class MainWindow {
this.setAboutPanel() this.setAboutPanel()
// 读取Linux系统中已经安装的软件 // 读取Linux系统中已经安装的软件
// this.softList.getList(); this.softList.getList();
} }
setWindowMenu () { setWindowMenu () {
......
...@@ -68,6 +68,7 @@ export default { ...@@ -68,6 +68,7 @@ export default {
} }
}, },
created() { created() {
}, },
mounted() { mounted() {
...@@ -155,19 +156,26 @@ export default { ...@@ -155,19 +156,26 @@ export default {
// 获取到app列表 // 获取到app列表
this.ipcRenderer.on("appsList", (event, list) => { this.ipcRenderer.on("appsList", (event, list) => {
// console.log('获取到app列表', list) console.log('获取到app列表', list)
this.appList = list; this.appList = list;
this.appList.forEach((item, index) => { this.appList.forEach((item, index) => {
item.icon && item.icon.indexOf('data:image/png') == -1 console.log(item.icon)
&& this.ipcRenderer.send("getBase64", {'imagePath':item.icon,index}); if(item.icon && item.icon.indexOf('data:image/png') == -1) {
this.ipcRenderer.send("getBase64", {'imagePath':item.icon,index});
}
}); });
}); });
this.ipcRenderer.on("appsList_update", (event, list) => { this.ipcRenderer.on("appsList_update", (event, list) => {
// console.log('刷新app列表', list) console.log('刷新app列表', list)
this.appList = list; this.appList = list;
}); });
// 获取到app消息
this.ipcRenderer.on("getMsg", (event, msgStr) => {
console.log('获取到app消息', msgStr)
});
// 右键菜单点击-切换模式 // 右键菜单点击-切换模式
this.ipcRenderer.on("context-menu-command",(event, mode) => { this.ipcRenderer.on("context-menu-command",(event, mode) => {
console.log('context-menu-command', mode) console.log('context-menu-command', mode)
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论