diff --git a/.gitignore b/.gitignore index a6966ae..f85b77a 100644 --- a/.gitignore +++ b/.gitignore @@ -30,3 +30,7 @@ output package-lock.json yarn.lock pnpm-lock.yaml +dist +!dist/about_pake.html +!dist/cli.js +!dist/.gitkeep diff --git a/bin/README.md b/bin/README.md index b6b8bc4..8d4f99d 100644 --- a/bin/README.md +++ b/bin/README.md @@ -23,7 +23,7 @@ Note: 打包需要用 `Rust` 环境,如果没有 `Rust`,会提示确认安 ### url -url 为你需要打包的网页链接 🔗,必须提供。 +url 为你需要打包的网页链接 🔗或者本地html文件,必须提供。 ### [options] @@ -67,7 +67,7 @@ url 为你需要打包的网页链接 🔗,必须提供。 #### [transparent] -是否开启沉浸式头部,默认为 `false` 不开启。 +是否开启沉浸式头部,默认为 `false` 不开启,输入下面的命令则开启沉浸式,推荐MacOS用户开启。 ```shell --transparent @@ -75,7 +75,7 @@ url 为你需要打包的网页链接 🔗,必须提供。 #### [resize] -是否可以拖动大小,默认为 `true` 可拖动。 +是否可以拖动大小,默认为 `true` 可拖动,输入下面的命令则不能对窗口大小进行拉伸。 ```shell --no-resizable @@ -83,8 +83,49 @@ url 为你需要打包的网页链接 🔗,必须提供。 #### [fullscreen] -打开应用后是否开启全屏,默认为 `false`。 +打开应用后是否开启全屏,默认为 `false`,输入下面的命令则会自动全屏。 ```shell ---fullscreen +--fullscreen ``` + +#### [user-agent] + +自定义浏览器请求头, 默认为空。 + +```shell +--user-agent +``` + +#### [show-menu] + +显示菜单栏, 默认不显示,输入下面的命令则会显示,推荐MacOS用户开启。 + +```shell +--show-menu +``` + +#### [show-system-tray] + +显示通知栏托盘, 默认不显示,输入下面的命令则会显示。 + +```shell +--show-system-tray +``` + +#### [system-tray-icon] + +通知栏托盘图标,仅当显示通知栏托盘时有效, 图标必须为.ico或者.png格式的,512*512像素的图片。 + +```shell +--system-tray-icon +``` + + +#### [copy-iter-file] + +递归拷贝,当url为本地文件路径时候,若开启该选项,则将url路径文件所在文件夹以及所有子文件都拷贝到pake静态文件夹,默认不开启 + +```shell +--copy-iter-file +``` \ No newline at end of file diff --git a/bin/README_EN.md b/bin/README_EN.md index 683e9d7..27d5321 100644 --- a/bin/README_EN.md +++ b/bin/README_EN.md @@ -22,7 +22,7 @@ Note: The Rust environment is required for packaging. If you do not have Rust, y ### url -The url🔗 is the webpage link you need to package, Must be provided. +The url🔗 is the webpage link or local html file you need to package, Must be provided. ### [options] @@ -66,7 +66,7 @@ The width of the packaged application window. The default is `1200px`. #### [transparent] -Whether to enable the immersive header. The default is `false`. +Whether to enable the immersive header. The default is `false`, enter the following command to enable immersive, recommended for MacOS users to enable ```shell --transparent @@ -74,7 +74,7 @@ Whether to enable the immersive header. The default is `false`. #### [resize] -Whether the size can be dragged. The default value is `true`. +Whether the size can be dragged. The default value is `true`, the window size cannot be stretched by entering the following command. ```shell --no-resizable @@ -82,8 +82,49 @@ Whether the size can be dragged. The default value is `true`. #### [fullscreen] -Whether to open the full screen after opening the application. The default is `false`. +Whether to open the full screen after opening the application. The default is `false`, enter the following command to automatically full screen ```shell --fullscreen ``` + +#### [user-agent] + +Custom browser user agent, default is empty. + +```shell +--user-agent +``` + +#### [show-menu] + +Display the menu bar, not display it by default, enter the following command and it will be displayed. MacOS users are recommended to enable. + +```shell +--show-menu +``` + +#### [show-system-tray] + +Display the notification tray, not display it by default, entering the following command will display. + +```shell +--show-system-tray +``` + +#### [system-tray-icon] + +The notification tray icon is only valid when the notification tray is displayed. The icon must be a 512*512 pixel image in .ico or .png format. + +```shell +--system-tray-icon +``` + + +#### [copy-iter-file] + +Recursive copy, when the url is a local file path, if this option is enabled, the folder where the url path file is located and all sub-files are copied to the pake static folder, which is not enabled by default + +```shell +--copy-iter-file +``` diff --git a/bin/builders/LinuxBuilder.ts b/bin/builders/LinuxBuilder.ts index cec50b1..de8920a 100644 --- a/bin/builders/LinuxBuilder.ts +++ b/bin/builders/LinuxBuilder.ts @@ -54,20 +54,33 @@ export default class LinuxBuilder implements IBuilder { arch = process.arch; } const debName = `${name}_${tauriConf.package.version}_${arch}.deb`; - const appPath = this.getBuildedAppPath(npmDirectory, "deb", debName); + const debPath = this.getBuildedAppPath(npmDirectory, "deb", debName); const distPath = path.resolve(`${name}.deb`); - await fs.copyFile(appPath, distPath); - await fs.unlink(appPath); + // 增加文件是否存在验证,再决定是否copy文件 + const debExists = await fs.stat(debPath) + .then(() => true) + .catch(() => false); + if (debExists) { + await fs.copyFile(debPath, distPath); + await fs.unlink(debPath); + logger.success('Build success!'); + logger.success('You can find the deb app installer in', distPath); + } const appImageName = `${name}_${tauriConf.package.version}_${arch}.AppImage`; const appImagePath = this.getBuildedAppPath(npmDirectory, "appimage", appImageName); const distAppPath = path.resolve(`${name}.AppImage`); - await fs.copyFile(appImagePath, distAppPath); - await fs.unlink(appImagePath); - logger.success('Build success!'); - logger.success('You can find the deb app installer in', distPath); - logger.success('You can find the Appimage app installer in', distAppPath); + + const appExists = await fs.stat(appImagePath) + .then(() => true) + .catch(() => false); + if (appExists) { + await fs.copyFile(appImagePath, distAppPath); + await fs.unlink(appImagePath); + logger.success('Build success!'); + logger.success('You can find the Appimage app installer in', distAppPath); + } } getBuildedAppPath(npmDirectory: string,packageType: string, packageName: string) { diff --git a/bin/builders/common.ts b/bin/builders/common.ts index 06b2d19..6f60eb6 100644 --- a/bin/builders/common.ts +++ b/bin/builders/common.ts @@ -1,10 +1,12 @@ import { PakeAppOptions } from '@/types.js'; -import prompts from 'prompts'; +import prompts, { override } from 'prompts'; import path from 'path'; import fs from 'fs/promises'; +import fs2 from 'fs-extra'; import { npmDirectory } from '@/utils/dir.js'; import logger from '@/options/logger.js'; + export async function promptText(message: string, initial?: string) { const response = await prompts({ type: 'text', @@ -15,6 +17,7 @@ export async function promptText(message: string, initial?: string) { return response.content; } + export async function mergeTauriConfig( url: string, options: PakeAppOptions, @@ -26,6 +29,11 @@ export async function mergeTauriConfig( fullscreen, transparent, resizable, + userAgent, + showMenu, + showSystemTray, + systemTrayIcon, + iterCopyFile, identifier, name, } = options; @@ -56,10 +64,132 @@ export async function mergeTauriConfig( } } + // logger.warn(JSON.stringify(tauriConf.pake.windows, null, 4)); + Object.assign(tauriConf.pake.windows[0], { url, ...tauriConfWindowOptions }); + // 判断一下url类型,是文件还是网站 + // 如果是文件,并且开启了递归拷贝功能,则需要将该文件以及所在文件夹下的所有文件拷贝到src目录下,否则只拷贝单个文件。 + + const url_exists = await fs.stat(url) + .then(() => true) + .catch(() => false); + if (url_exists) { + logger.warn("you input may a local file"); + tauriConf.pake.windows[0].url_type = "local"; + const file_name = path.basename(url); + const dir_name = path.dirname(url); + if (!iterCopyFile) { + const url_path = path.join(npmDirectory,"dist/", file_name); + await fs.copyFile(url, url_path); + } else { + const old_dir = path.join(npmDirectory,"dist/"); + const new_dir = path.join(npmDirectory,"dist_bak/"); + fs2.moveSync(old_dir, new_dir, {"overwrite": true}); + fs2.copySync(dir_name, old_dir, {"overwrite": true}); + // logger.warn("dir name", dir_name); + // 将dist_bak里面的cli.js和about_pake.html拷贝回去 + const cli_path = path.join(new_dir, "cli.js") + const cli_path_target = path.join(old_dir, "cli.js") + const about_pake_path = path.join(new_dir, "about_pake.html"); + const about_patk_path_target = path.join(old_dir, "about_pake.html") + fs.copyFile(cli_path, cli_path_target); + fs.copyFile(about_pake_path, about_patk_path_target); + } + tauriConf.pake.windows[0].url = file_name; + tauriConf.pake.windows[0].url_type = "local"; + } else { + tauriConf.pake.windows[0].url_type = "web"; + } + + // 处理user-agent + logger.warn(userAgent); + if (userAgent.length > 0) { + if (process.platform === "win32") { + tauriConf.pake.user_agent.windows = userAgent; + } + + if (process.platform === "linux") { + tauriConf.pake.user_agent.linux = userAgent; + } + + if (process.platform === "darwin") { + tauriConf.pake.user_agent.macos = userAgent; + } + } + + // 处理菜单栏 + if (showMenu) { + if (process.platform === "win32") { + tauriConf.pake.menu.windows = true; + } + + if (process.platform === "linux") { + tauriConf.pake.menu.linux = true; + } + + if (process.platform === "darwin") { + tauriConf.pake.user_agent.macos = true; + } + } else { + if (process.platform === "win32") { + tauriConf.pake.menu.windows = false; + } + + if (process.platform === "linux") { + tauriConf.pake.menu.linux = false; + } + + if (process.platform === "darwin") { + tauriConf.pake.user_agent.macos = false; + } + } + + // 处理托盘 + if (showSystemTray) { + if (process.platform === "win32") { + tauriConf.pake.system_tray.windows = true; + } + + if (process.platform === "linux") { + tauriConf.pake.system_tray.linux = true; + } + + if (process.platform === "darwin") { + tauriConf.pake.system_tray.macos = true; + } + } else { + if (process.platform === "win32") { + tauriConf.pake.system_tray.windows = false; + } + + if (process.platform === "linux") { + tauriConf.pake.system_tray.linux = false; + } + + if (process.platform === "darwin") { + tauriConf.pake.system_tray.macos = false; + } + } + + // 处理targets 暂时只对linux开放 + if (process.platform === "linux") { + if (options.targets.length > 0) { + if (options.targets === "deb" || options.targets === "appimage" || options.targets === "all") { + tauriConf.tauri.bundle.targets = [options.targets]; + } + } + } else { + tauriConf.tauri.bundle.targets = ["deb"]; + } - Object.assign(tauriConf.tauri.windows[0], { url, ...tauriConfWindowOptions }); tauriConf.package.productName = name; tauriConf.tauri.bundle.identifier = identifier; + + // 删除映射关系 + if (process.platform === "linux") { + delete tauriConf.tauri.bundle.deb.files; + } + + // 处理应用图标 const exists = await fs.stat(options.icon) .then(() => true) .catch(() => false); @@ -74,19 +204,21 @@ export async function mergeTauriConfig( } else { updateIconPath = false; logger.warn(`icon file in Windows must be 256 * 256 pix with .ico type, but you give ${customIconExt}`); + tauriConf.tauri.bundle.icon = ["png/icon_256.ico"]; } } if (process.platform === "linux") { - delete tauriConf.tauri.bundle.deb.files; if (customIconExt != ".png") { updateIconPath = false; logger.warn(`icon file in Linux must be 512 * 512 pix with .png type, but you give ${customIconExt}`); + tauriConf.tauri.bundle.icon = ["png/icon_512.png"]; } } if (process.platform === "darwin" && customIconExt !== ".icns") { updateIconPath = false; logger.warn(`icon file in MacOS must be .icns type, but you give ${customIconExt}`); + tauriConf.tauri.bundle.icon = ["icons/icon.icns"]; } if (updateIconPath) { tauriConf.tauri.bundle.icon = [options.icon]; @@ -95,9 +227,51 @@ export async function mergeTauriConfig( } } else { logger.warn("the custom icon path may not exists. we will use default icon to replace it"); + if (process.platform === "win32") { + tauriConf.tauri.bundle.icon = ["png/icon_256.ico"]; + } + if (process.platform === "linux") { + tauriConf.tauri.bundle.icon = ["png/icon_512.png"]; + } + if (process.platform === "darwin") { + tauriConf.tauri.bundle.icon = ["icons/icon.icns"]; + } } + // 处理托盘自定义图标 + let useDefaultIcon = true; // 是否使用默认托盘图标 + if (systemTrayIcon.length > 0) { + const icon_exists = await fs.stat(systemTrayIcon) + .then(() => true) + .catch(() => false); + if (icon_exists) { + // 需要判断图标格式,默认只支持ico和png两种 + let iconExt = path.extname(systemTrayIcon).toLowerCase(); + if (iconExt == ".png" || iconExt == ".icon") { + useDefaultIcon = false; + const trayIcoPath = path.join(npmDirectory, `src-tauri/png/${name.toLowerCase()}${iconExt}`); + tauriConf.tauri.systemTray.iconPath = `png/${name.toLowerCase()}${iconExt}`; + await fs.copyFile(systemTrayIcon, trayIcoPath); + } else { + logger.warn(`file type for system tray icon mut be .ico or .png , but you give ${iconExt}`); + logger.warn(`system tray icon file will not change with default.`); + } + } else { + logger.warn(`${systemTrayIcon} not exists!`) + logger.warn(`system tray icon file will not change with default.`); + } + } + // 处理托盘默认图标 + if (useDefaultIcon) { + if (process.platform === "linux" || process.platform === "win32") { + tauriConf.tauri.systemTray.iconPath = tauriConf.tauri.bundle.icon[0]; + } else { + tauriConf.tauri.systemTray.iconPath = "png/icon_512.png"; + } + } + + // 保存配置文件 let configPath = ""; switch (process.platform) { case "win32": { @@ -117,13 +291,22 @@ export async function mergeTauriConfig( let bundleConf = {tauri: {bundle: tauriConf.tauri.bundle}}; await fs.writeFile( configPath, - Buffer.from(JSON.stringify(bundleConf), 'utf-8') + Buffer.from(JSON.stringify(bundleConf, null, 4), 'utf-8') ); + const pakeConfigPath = path.join(npmDirectory, 'src-tauri/pake.json') + await fs.writeFile( + pakeConfigPath, + Buffer.from(JSON.stringify(tauriConf.pake, null, 4), 'utf-8') + ); + + let tauriConf2 = JSON.parse(JSON.stringify(tauriConf)); + delete tauriConf2.pake; + delete tauriConf2.tauri.bundle; const configJsonPath = path.join(npmDirectory, 'src-tauri/tauri.conf.json') await fs.writeFile( configJsonPath, - Buffer.from(JSON.stringify(tauriConf), 'utf-8') + Buffer.from(JSON.stringify(tauriConf2, null, 4), 'utf-8') ); } diff --git a/bin/builders/tauriConf.js b/bin/builders/tauriConf.js index 2b5fd20..53122b8 100644 --- a/bin/builders/tauriConf.js +++ b/bin/builders/tauriConf.js @@ -1,11 +1,13 @@ import CommonConf from '../../src-tauri/tauri.conf.json'; +import pakeConf from '../../src-tauri/pake.json'; import WinConf from '../../src-tauri/tauri.windows.conf.json'; import MacConf from '../../src-tauri/tauri.macos.conf.json'; import LinuxConf from '../../src-tauri/tauri.linux.conf.json'; let tauriConf = { package: CommonConf.package, - tauri: CommonConf.tauri + tauri: CommonConf.tauri, + pake: pakeConf } switch (process.platform) { case "win32": { diff --git a/bin/cli.ts b/bin/cli.ts index 14a6d0e..294a06b 100644 --- a/bin/cli.ts +++ b/bin/cli.ts @@ -23,6 +23,17 @@ program .option('--no-resizable', 'whether the window can be resizable', DEFAULT_PAKE_OPTIONS.resizable) .option('--fullscreen', 'makes the packaged app start in full screen', DEFAULT_PAKE_OPTIONS.fullscreen) .option('--transparent', 'transparent title bar', DEFAULT_PAKE_OPTIONS.transparent) + .option('--user-agent ', 'custom user agent', DEFAULT_PAKE_OPTIONS.userAgent) + .option('--show-menu', 'show menu in app', DEFAULT_PAKE_OPTIONS.showMenu) + .option('--show-system-tray', 'show system tray in app', DEFAULT_PAKE_OPTIONS.showSystemTray) + .option('--system-tray-icon ', 'custom system tray icon', DEFAULT_PAKE_OPTIONS.systemTrayIcon) + .option('--iter-copy-file', + 'copy all static file to pake app when url is a local file', + DEFAULT_PAKE_OPTIONS.iterCopyFile) + .option( + '--targets ', + 'only for linux, default is "deb", option "appaimge" or "all"(deb & appimage)', + DEFAULT_PAKE_OPTIONS.targets) .option('--debug', 'debug', DEFAULT_PAKE_OPTIONS.transparent) .action(async (url: string, options: PakeCliOptions) => { checkUpdateTips(); @@ -39,9 +50,9 @@ program const builder = BuilderFactory.create(); await builder.prepare(); - + // logger.warn("you input url is ", url); const appOptions = await handleInputOptions(options, url); - + // logger.info(JSON.stringify(appOptions, null, 4)); builder.build(url, appOptions); }); diff --git a/bin/defaults.ts b/bin/defaults.ts index fb5cbff..4200bb4 100644 --- a/bin/defaults.ts +++ b/bin/defaults.ts @@ -7,6 +7,12 @@ export const DEFAULT_PAKE_OPTIONS: PakeCliOptions = { fullscreen: false, resizable: true, transparent: false, + userAgent: '', + showMenu: false, + showSystemTray: false, + targets: 'deb', + iterCopyFile: false, + systemTrayIcon: '', debug: false, }; diff --git a/bin/options/index.ts b/bin/options/index.ts index efcf4f6..f259dd5 100644 --- a/bin/options/index.ts +++ b/bin/options/index.ts @@ -3,15 +3,22 @@ import { getDomain } from '@/utils/url.js'; import { getIdentifier } from '../helpers/tauriConfig.js'; import { PakeAppOptions, PakeCliOptions } from '../types.js'; import { handleIcon } from './icon.js'; +import fs from 'fs/promises'; export default async function handleOptions(options: PakeCliOptions, url: string): Promise { const appOptions: PakeAppOptions = { ...options, identifier: '', }; - + const url_exists = await fs.stat(url) + .then(() => true) + .catch(() => false); if (!appOptions.name) { - appOptions.name = await promptText('please input your application name', getDomain(url)); + if (!url_exists) { + appOptions.name = await promptText('please input your application name', getDomain(url)); + } else { + appOptions.name = await promptText('please input your application name', ""); + } } appOptions.identifier = getIdentifier(appOptions.name, url); diff --git a/bin/types.ts b/bin/types.ts index 9610edb..dbb9672 100644 --- a/bin/types.ts +++ b/bin/types.ts @@ -20,6 +20,24 @@ export interface PakeCliOptions { /** 是否开启沉浸式头部,默认为 false 不开启 ƒ*/ transparent: boolean; + /** 自定义UA,默认为不开启 ƒ*/ + userAgent: string; + + /** 开启菜单栏,MacOS默认开启,Windows,Linux默认不开启 ƒ*/ + showMenu: boolean; + + /** 开启系统托盘,MacOS默认不开启,Windows,Linux默认开启 ƒ*/ + showSystemTray: boolean; + + /** 托盘图标, Windows、Linux默认和应用图标共用一样的,MacOS需要提别提供, 格式为png或者ico */ + systemTrayIcon: string; + + // /** 递归拷贝,当url为本地文件路径时候,若开启该选项,则将url路径文件所在文件夹以及所有子文件都拷贝到pake静态文件夹,默认不开启 */ + iterCopyFile: false; + + // 包输出产物,对linux用户有效,默认为deb,可选appimage, 或者all(即同时输出deb和all); + targets: string; + /** 调试模式,会输出更多日志 */ debug: boolean; } diff --git a/bin/utils/validate.ts b/bin/utils/validate.ts index a97821c..418c699 100644 --- a/bin/utils/validate.ts +++ b/bin/utils/validate.ts @@ -1,5 +1,6 @@ import * as Commander from 'commander'; import { normalizeUrl } from './url.js'; +import fs from 'fs'; export function validateNumberInput(value: string) { const parsedValue = Number(value); @@ -10,9 +11,13 @@ export function validateNumberInput(value: string) { } export function validateUrlInput(url: string) { - try { - return normalizeUrl(url); - } catch (error) { - throw new Commander.InvalidArgumentError(error.message); + if(!fs.existsSync(url)) { + try { + return normalizeUrl(url) + } catch (error) { + throw new Commander.InvalidArgumentError(error.message); + } + } else { + return url; } } diff --git a/dist/about_pake.html b/dist/about_pake.html index e1655dd..d7bb838 100644 --- a/dist/about_pake.html +++ b/dist/about_pake.html @@ -8,9 +8,9 @@
Welcome from Pake!

version: 1.0.1

- Project link
- Discussions
- Issues
+ Project link
+ Discussions
+ Issues

LICENSE: MIT

\ No newline at end of file diff --git a/dist/cli.js b/dist/cli.js index 468a13b..372bae8 100644 --- a/dist/cli.js +++ b/dist/cli.js @@ -3,9 +3,11 @@ import { program } from 'commander'; import log from 'loglevel'; import url, { fileURLToPath } from 'url'; import isurl from 'is-url'; +import fs from 'fs'; import prompts from 'prompts'; import path from 'path'; -import fs from 'fs/promises'; +import fs$1 from 'fs/promises'; +import fs2 from 'fs-extra'; import chalk from 'chalk'; import crypto from 'crypto'; import axios from 'axios'; @@ -47,6 +49,12 @@ const DEFAULT_PAKE_OPTIONS = { fullscreen: false, resizable: true, transparent: false, + userAgent: '', + showMenu: false, + showSystemTray: false, + targets: 'deb', + iterCopyFile: false, + systemTrayIcon: '', debug: false, }; @@ -1583,11 +1591,16 @@ function validateNumberInput(value) { return parsedValue; } function validateUrlInput(url) { - try { - return normalizeUrl(url); + if (!fs.existsSync(url)) { + try { + return normalizeUrl(url); + } + catch (error) { + throw new Commander.InvalidArgumentError(error.message); + } } - catch (error) { - throw new Commander.InvalidArgumentError(error.message); + else { + return url; } } @@ -1624,7 +1637,7 @@ function promptText(message, initial) { } function mergeTauriConfig(url, options, tauriConf) { return __awaiter(this, void 0, void 0, function* () { - const { width, height, fullscreen, transparent, resizable, identifier, name, } = options; + const { width, height, fullscreen, transparent, resizable, userAgent, showMenu, showSystemTray, systemTrayIcon, iterCopyFile, identifier, name, } = options; const tauriConfWindowOptions = { width, height, @@ -1650,10 +1663,120 @@ function mergeTauriConfig(url, options, tauriConf) { process.exit(); } } - Object.assign(tauriConf.tauri.windows[0], Object.assign({ url }, tauriConfWindowOptions)); + // logger.warn(JSON.stringify(tauriConf.pake.windows, null, 4)); + Object.assign(tauriConf.pake.windows[0], Object.assign({ url }, tauriConfWindowOptions)); + // 判断一下url类型,是文件还是网站 + // 如果是文件,并且开启了递归拷贝功能,则需要将该文件以及所在文件夹下的所有文件拷贝到src目录下,否则只拷贝单个文件。 + const url_exists = yield fs$1.stat(url) + .then(() => true) + .catch(() => false); + if (url_exists) { + logger.warn("you input may a local file"); + tauriConf.pake.windows[0].url_type = "local"; + const file_name = path.basename(url); + const dir_name = path.dirname(url); + if (!iterCopyFile) { + const url_path = path.join(npmDirectory, "dist/", file_name); + yield fs$1.copyFile(url, url_path); + } + else { + const old_dir = path.join(npmDirectory, "dist/"); + const new_dir = path.join(npmDirectory, "dist_bak/"); + fs2.moveSync(old_dir, new_dir, { "overwrite": true }); + fs2.copySync(dir_name, old_dir, { "overwrite": true }); + // logger.warn("dir name", dir_name); + // 将dist_bak里面的cli.js和about_pake.html拷贝回去 + const cli_path = path.join(new_dir, "cli.js"); + const cli_path_target = path.join(old_dir, "cli.js"); + const about_pake_path = path.join(new_dir, "about_pake.html"); + const about_patk_path_target = path.join(old_dir, "about_pake.html"); + fs$1.copyFile(cli_path, cli_path_target); + fs$1.copyFile(about_pake_path, about_patk_path_target); + } + tauriConf.pake.windows[0].url = file_name; + tauriConf.pake.windows[0].url_type = "local"; + } + else { + tauriConf.pake.windows[0].url_type = "web"; + } + // 处理user-agent + logger.warn(userAgent); + if (userAgent.length > 0) { + if (process.platform === "win32") { + tauriConf.pake.user_agent.windows = userAgent; + } + if (process.platform === "linux") { + tauriConf.pake.user_agent.linux = userAgent; + } + if (process.platform === "darwin") { + tauriConf.pake.user_agent.macos = userAgent; + } + } + // 处理菜单栏 + if (showMenu) { + if (process.platform === "win32") { + tauriConf.pake.menu.windows = true; + } + if (process.platform === "linux") { + tauriConf.pake.menu.linux = true; + } + if (process.platform === "darwin") { + tauriConf.pake.user_agent.macos = true; + } + } + else { + if (process.platform === "win32") { + tauriConf.pake.menu.windows = false; + } + if (process.platform === "linux") { + tauriConf.pake.menu.linux = false; + } + if (process.platform === "darwin") { + tauriConf.pake.user_agent.macos = false; + } + } + // 处理托盘 + if (showSystemTray) { + if (process.platform === "win32") { + tauriConf.pake.system_tray.windows = true; + } + if (process.platform === "linux") { + tauriConf.pake.system_tray.linux = true; + } + if (process.platform === "darwin") { + tauriConf.pake.system_tray.macos = true; + } + } + else { + if (process.platform === "win32") { + tauriConf.pake.system_tray.windows = false; + } + if (process.platform === "linux") { + tauriConf.pake.system_tray.linux = false; + } + if (process.platform === "darwin") { + tauriConf.pake.system_tray.macos = false; + } + } + // 处理targets 暂时只对linux开放 + if (process.platform === "linux") { + if (options.targets.length > 0) { + if (options.targets === "deb" || options.targets === "appimage" || options.targets === "all") { + tauriConf.tauri.bundle.targets = [options.targets]; + } + } + } + else { + tauriConf.tauri.bundle.targets = ["deb"]; + } tauriConf.package.productName = name; tauriConf.tauri.bundle.identifier = identifier; - const exists = yield fs.stat(options.icon) + // 删除映射关系 + if (process.platform === "linux") { + delete tauriConf.tauri.bundle.deb.files; + } + // 处理应用图标 + const exists = yield fs$1.stat(options.icon) .then(() => true) .catch(() => false); if (exists) { @@ -1663,23 +1786,25 @@ function mergeTauriConfig(url, options, tauriConf) { if (customIconExt === ".ico") { const ico_path = path.join(npmDirectory, `src-tauri/png/${name.toLowerCase()}_32.ico`); tauriConf.tauri.bundle.resources = [`png/${name.toLowerCase()}_32.ico`]; - yield fs.copyFile(options.icon, ico_path); + yield fs$1.copyFile(options.icon, ico_path); } else { updateIconPath = false; logger.warn(`icon file in Windows must be 256 * 256 pix with .ico type, but you give ${customIconExt}`); + tauriConf.tauri.bundle.icon = ["png/icon_256.ico"]; } } if (process.platform === "linux") { - delete tauriConf.tauri.bundle.deb.files; if (customIconExt != ".png") { updateIconPath = false; logger.warn(`icon file in Linux must be 512 * 512 pix with .png type, but you give ${customIconExt}`); + tauriConf.tauri.bundle.icon = ["png/icon_512.png"]; } } if (process.platform === "darwin" && customIconExt !== ".icns") { updateIconPath = false; logger.warn(`icon file in MacOS must be .icns type, but you give ${customIconExt}`); + tauriConf.tauri.bundle.icon = ["icons/icon.icns"]; } if (updateIconPath) { tauriConf.tauri.bundle.icon = [options.icon]; @@ -1690,7 +1815,51 @@ function mergeTauriConfig(url, options, tauriConf) { } else { logger.warn("the custom icon path may not exists. we will use default icon to replace it"); + if (process.platform === "win32") { + tauriConf.tauri.bundle.icon = ["png/icon_256.ico"]; + } + if (process.platform === "linux") { + tauriConf.tauri.bundle.icon = ["png/icon_512.png"]; + } + if (process.platform === "darwin") { + tauriConf.tauri.bundle.icon = ["icons/icon.icns"]; + } } + // 处理托盘自定义图标 + let useDefaultIcon = true; // 是否使用默认托盘图标 + if (systemTrayIcon.length > 0) { + const icon_exists = yield fs$1.stat(systemTrayIcon) + .then(() => true) + .catch(() => false); + if (icon_exists) { + // 需要判断图标格式,默认只支持ico和png两种 + let iconExt = path.extname(systemTrayIcon).toLowerCase(); + if (iconExt == ".png" || iconExt == ".icon") { + useDefaultIcon = false; + const trayIcoPath = path.join(npmDirectory, `src-tauri/png/${name.toLowerCase()}${iconExt}`); + tauriConf.tauri.systemTray.iconPath = `png/${name.toLowerCase()}${iconExt}`; + yield fs$1.copyFile(systemTrayIcon, trayIcoPath); + } + else { + logger.warn(`file type for system tray icon mut be .ico or .png , but you give ${iconExt}`); + logger.warn(`system tray icon file will not change with default.`); + } + } + else { + logger.warn(`${systemTrayIcon} not exists!`); + logger.warn(`system tray icon file will not change with default.`); + } + } + // 处理托盘默认图标 + if (useDefaultIcon) { + if (process.platform === "linux" || process.platform === "win32") { + tauriConf.tauri.systemTray.iconPath = tauriConf.tauri.bundle.icon[0]; + } + else { + tauriConf.tauri.systemTray.iconPath = "png/icon_512.png"; + } + } + // 保存配置文件 let configPath = ""; switch (process.platform) { case "win32": { @@ -1707,9 +1876,14 @@ function mergeTauriConfig(url, options, tauriConf) { } } let bundleConf = { tauri: { bundle: tauriConf.tauri.bundle } }; - yield fs.writeFile(configPath, Buffer.from(JSON.stringify(bundleConf), 'utf-8')); + yield fs$1.writeFile(configPath, Buffer.from(JSON.stringify(bundleConf, null, 4), 'utf-8')); + const pakeConfigPath = path.join(npmDirectory, 'src-tauri/pake.json'); + yield fs$1.writeFile(pakeConfigPath, Buffer.from(JSON.stringify(tauriConf.pake, null, 4), 'utf-8')); + let tauriConf2 = JSON.parse(JSON.stringify(tauriConf)); + delete tauriConf2.pake; + delete tauriConf2.tauri.bundle; const configJsonPath = path.join(npmDirectory, 'src-tauri/tauri.conf.json'); - yield fs.writeFile(configJsonPath, Buffer.from(JSON.stringify(tauriConf), 'utf-8')); + yield fs$1.writeFile(configJsonPath, Buffer.from(JSON.stringify(tauriConf2, null, 4), 'utf-8')); }); } @@ -1808,7 +1982,7 @@ function downloadIcon(iconUrl) { } const { path } = yield dir(); const iconPath = `${path}/icon.${fileDetails.ext}`; - yield fs.writeFile(iconPath, iconData); + yield fs$1.writeFile(iconPath, iconData); return iconPath; }); } @@ -1816,8 +1990,16 @@ function downloadIcon(iconUrl) { function handleOptions(options, url) { return __awaiter(this, void 0, void 0, function* () { const appOptions = Object.assign(Object.assign({}, options), { identifier: '' }); + const url_exists = yield fs$1.stat(url) + .then(() => true) + .catch(() => false); if (!appOptions.name) { - appOptions.name = yield promptText('please input your application name', getDomain(url)); + if (!url_exists) { + appOptions.name = yield promptText('please input your application name', getDomain(url)); + } + else { + appOptions.name = yield promptText('please input your application name', ""); + } } appOptions.identifier = getIdentifier(appOptions.name, url); appOptions.icon = yield handleIcon(appOptions); @@ -1859,21 +2041,15 @@ function checkRustInstalled() { } var tauri$3 = { - windows: [ - { - url: "https://weread.qq.com/", - transparent: true, - fullscreen: false, - width: 1200, - height: 780, - resizable: true - } - ], security: { csp: null }, updater: { active: false + }, + systemTray: { + iconPath: "png/weread_512.png", + iconAsTemplate: true } }; var build = { @@ -1891,6 +2067,39 @@ var CommonConf = { build: build }; +var windows = [ + { + url: "https://weread.qq.com/", + transparent: true, + fullscreen: false, + width: 1200, + height: 780, + resizable: true, + url_type: "web" + } +]; +var user_agent = { + macos: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.1 Safari/605.1.15", + linux: "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36", + windows: "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36" +}; +var menu = { + macos: true, + linux: false, + windows: false +}; +var system_tray = { + macos: false, + linux: true, + windows: true +}; +var pakeConf = { + windows: windows, + user_agent: user_agent, + menu: menu, + system_tray: system_tray +}; + var tauri$2 = { bundle: { icon: [ @@ -2005,7 +2214,8 @@ var LinuxConf = { let tauriConf = { package: CommonConf.package, - tauri: CommonConf.tauri + tauri: CommonConf.tauri, + pake: pakeConf }; switch (process.platform) { case "win32": { @@ -2059,8 +2269,8 @@ class MacBuilder { const dmgName = `${name}_${tauriConf.package.version}_${arch}.dmg`; const appPath = this.getBuildedAppPath(npmDirectory, dmgName); const distPath = path.resolve(`${name}.dmg`); - yield fs.copyFile(appPath, distPath); - yield fs.unlink(appPath); + yield fs$1.copyFile(appPath, distPath); + yield fs$1.unlink(appPath); logger.success('Build success!'); logger.success('You can find the app installer in', distPath); }); @@ -2104,8 +2314,8 @@ class WinBuilder { const msiName = `${name}_${tauriConf.package.version}_${arch}_${language}.msi`; const appPath = this.getBuildedAppPath(npmDirectory, msiName); const distPath = path.resolve(`${name}.msi`); - yield fs.copyFile(appPath, distPath); - yield fs.unlink(appPath); + yield fs$1.copyFile(appPath, distPath); + yield fs$1.unlink(appPath); logger.success('Build success!'); logger.success('You can find the app installer in', distPath); }); @@ -2152,18 +2362,30 @@ class LinuxBuilder { arch = process.arch; } const debName = `${name}_${tauriConf.package.version}_${arch}.deb`; - const appPath = this.getBuildedAppPath(npmDirectory, "deb", debName); + const debPath = this.getBuildedAppPath(npmDirectory, "deb", debName); const distPath = path.resolve(`${name}.deb`); - yield fs.copyFile(appPath, distPath); - yield fs.unlink(appPath); + // 增加文件是否存在验证,再决定是否copy文件 + const debExists = yield fs$1.stat(debPath) + .then(() => true) + .catch(() => false); + if (debExists) { + yield fs$1.copyFile(debPath, distPath); + yield fs$1.unlink(debPath); + logger.success('Build success!'); + logger.success('You can find the deb app installer in', distPath); + } const appImageName = `${name}_${tauriConf.package.version}_${arch}.AppImage`; const appImagePath = this.getBuildedAppPath(npmDirectory, "appimage", appImageName); const distAppPath = path.resolve(`${name}.AppImage`); - yield fs.copyFile(appImagePath, distAppPath); - yield fs.unlink(appImagePath); - logger.success('Build success!'); - logger.success('You can find the deb app installer in', distPath); - logger.success('You can find the Appimage app installer in', distAppPath); + const appExists = yield fs$1.stat(appImagePath) + .then(() => true) + .catch(() => false); + if (appExists) { + yield fs$1.copyFile(appImagePath, distAppPath); + yield fs$1.unlink(appImagePath); + logger.success('Build success!'); + logger.success('You can find the Appimage app installer in', distAppPath); + } }); } getBuildedAppPath(npmDirectory, packageType, packageName) { @@ -2239,6 +2461,7 @@ var dependencies = { chalk: "^5.1.2", commander: "^9.4.1", "file-type": "^18.0.0", + "fs-extra": "^11.1.0", "is-url": "^1.2.4", loglevel: "^1.8.1", ora: "^6.1.2", @@ -2253,6 +2476,7 @@ var devDependencies = { "@rollup/plugin-json": "^5.0.1", "@rollup/plugin-terser": "^0.1.0", "@rollup/plugin-typescript": "^9.0.2", + "@types/fs-extra": "^9.0.13", "@types/is-url": "^1.2.30", "@types/page-icon": "^0.3.4", "@types/prompts": "^2.4.1", @@ -2301,6 +2525,12 @@ program .option('--no-resizable', 'whether the window can be resizable', DEFAULT_PAKE_OPTIONS.resizable) .option('--fullscreen', 'makes the packaged app start in full screen', DEFAULT_PAKE_OPTIONS.fullscreen) .option('--transparent', 'transparent title bar', DEFAULT_PAKE_OPTIONS.transparent) + .option('--user-agent ', 'custom user agent', DEFAULT_PAKE_OPTIONS.userAgent) + .option('--show-menu', 'show menu in app', DEFAULT_PAKE_OPTIONS.showMenu) + .option('--show-system-tray', 'show system tray in app', DEFAULT_PAKE_OPTIONS.showSystemTray) + .option('--system-tray-icon ', 'custom system tray icon', DEFAULT_PAKE_OPTIONS.systemTrayIcon) + .option('--iter-copy-file', 'copy all static file to pake app when url is a local file', DEFAULT_PAKE_OPTIONS.iterCopyFile) + .option('--targets ', 'only for linux, default is "deb", option "appaimge" or "all"(deb & appimage)', DEFAULT_PAKE_OPTIONS.targets) .option('--debug', 'debug', DEFAULT_PAKE_OPTIONS.transparent) .action((url, options) => __awaiter(void 0, void 0, void 0, function* () { checkUpdateTips(); @@ -2314,7 +2544,9 @@ program } const builder = BuilderFactory.create(); yield builder.prepare(); + // logger.warn("you input url is ", url); const appOptions = yield handleOptions(options, url); + // logger.info(JSON.stringify(appOptions, null, 4)); builder.build(url, appOptions); })); program.parse(); diff --git a/package.json b/package.json index 0edaf8d..30fc33d 100644 --- a/package.json +++ b/package.json @@ -52,6 +52,7 @@ "chalk": "^5.1.2", "commander": "^9.4.1", "file-type": "^18.0.0", + "fs-extra": "^11.1.0", "is-url": "^1.2.4", "loglevel": "^1.8.1", "ora": "^6.1.2", @@ -66,6 +67,7 @@ "@rollup/plugin-json": "^5.0.1", "@rollup/plugin-terser": "^0.1.0", "@rollup/plugin-typescript": "^9.0.2", + "@types/fs-extra": "^9.0.13", "@types/is-url": "^1.2.30", "@types/page-icon": "^0.3.4", "@types/prompts": "^2.4.1", diff --git a/script/build.bat b/script/build.bat index 0624c12..7dccb34 100644 --- a/script/build.bat +++ b/script/build.bat @@ -53,6 +53,7 @@ for /f "skip=1 tokens=1-4 delims=," %%i in (app.csv) do ( .\script\sd.exe -s !old_url! !url! src-tauri\pake.json ::replace pacakge name .\script\sd.exe !old_title! !title! src-tauri\tauri.conf.json + .\script\sd.exe !old_name! !name! src-tauri\tauri.conf.json .\script\sd.exe !old_name! !name! src-tauri\tauri.windows.conf.json echo. @@ -92,6 +93,7 @@ for /f "skip=1 tokens=1-4 delims=," %%i in (app.csv) do ( echo "output dir is output\windows" ::recovery code -.\script\sd.exe %url% %init_url% src-tauri\tauri.conf.json +.\script\sd.exe %url% %init_url% src-tauri\pake.json .\script\sd.exe %title% %init_title% src-tauri\tauri.conf.json +.\script\sd.exe %name% %init_name% src-tauri\tauri.conf.json .\script\sd.exe %name% %init_name% src-tauri\tauri.windows.conf.json \ No newline at end of file diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index 48eb6f0..029ec7f 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -42,17 +42,14 @@ checksum = "2cb2f989d18dd141ab8ae82f64d1a8cdd37e0840f73a406896cf5e99502fab61" name = "app" version = "0.1.0" dependencies = [ - "cocoa 0.24.1 (git+https://github.com/servo/core-foundation-rs/)", "home", "image", - "objc", "serde", "serde_json", "tauri", "tauri-build", "tauri-plugin-window-state", "tauri-utils", - "webkit2gtk", ] [[package]] @@ -269,24 +266,9 @@ checksum = "f425db7937052c684daec3bd6375c8abe2d146dca4b8b143d6db777c39138f3a" dependencies = [ "bitflags", "block", - "cocoa-foundation 0.1.0", - "core-foundation 0.9.3 (registry+https://github.com/rust-lang/crates.io-index)", - "core-graphics 0.22.3 (registry+https://github.com/rust-lang/crates.io-index)", - "foreign-types", - "libc", - "objc", -] - -[[package]] -name = "cocoa" -version = "0.24.1" -source = "git+https://github.com/servo/core-foundation-rs/#8c2444066aac42c20443b696270f5b1afff18993" -dependencies = [ - "bitflags", - "block", - "cocoa-foundation 0.1.1", - "core-foundation 0.9.3 (git+https://github.com/servo/core-foundation-rs/)", - "core-graphics 0.22.3 (git+https://github.com/servo/core-foundation-rs/)", + "cocoa-foundation", + "core-foundation", + "core-graphics", "foreign-types", "libc", "objc", @@ -300,22 +282,8 @@ checksum = "7ade49b65d560ca58c403a479bb396592b155c0185eada742ee323d1d68d6318" dependencies = [ "bitflags", "block", - "core-foundation 0.9.3 (registry+https://github.com/rust-lang/crates.io-index)", - "core-graphics-types 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", - "foreign-types", - "libc", - "objc", -] - -[[package]] -name = "cocoa-foundation" -version = "0.1.1" -source = "git+https://github.com/servo/core-foundation-rs/#8c2444066aac42c20443b696270f5b1afff18993" -dependencies = [ - "bitflags", - "block", - "core-foundation 0.9.3 (git+https://github.com/servo/core-foundation-rs/)", - "core-graphics-types 0.1.1 (git+https://github.com/servo/core-foundation-rs/)", + "core-foundation", + "core-graphics-types", "foreign-types", "libc", "objc", @@ -349,16 +317,7 @@ version = "0.9.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "194a7a9e6de53fa55116934067c844d9d749312f75c6f6d0980e8c252f8c2146" dependencies = [ - "core-foundation-sys 0.8.3 (registry+https://github.com/rust-lang/crates.io-index)", - "libc", -] - -[[package]] -name = "core-foundation" -version = "0.9.3" -source = "git+https://github.com/servo/core-foundation-rs/#8c2444066aac42c20443b696270f5b1afff18993" -dependencies = [ - "core-foundation-sys 0.8.3 (git+https://github.com/servo/core-foundation-rs/)", + "core-foundation-sys", "libc", ] @@ -368,11 +327,6 @@ version = "0.8.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5827cebf4670468b8772dd191856768aedcb1b0278a04f989f7766351917b9dc" -[[package]] -name = "core-foundation-sys" -version = "0.8.3" -source = "git+https://github.com/servo/core-foundation-rs/#8c2444066aac42c20443b696270f5b1afff18993" - [[package]] name = "core-graphics" version = "0.22.3" @@ -380,20 +334,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2581bbab3b8ffc6fcbd550bf46c355135d16e9ff2a6ea032ad6b9bf1d7efe4fb" dependencies = [ "bitflags", - "core-foundation 0.9.3 (registry+https://github.com/rust-lang/crates.io-index)", - "core-graphics-types 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", - "foreign-types", - "libc", -] - -[[package]] -name = "core-graphics" -version = "0.22.3" -source = "git+https://github.com/servo/core-foundation-rs/#8c2444066aac42c20443b696270f5b1afff18993" -dependencies = [ - "bitflags", - "core-foundation 0.9.3 (git+https://github.com/servo/core-foundation-rs/)", - "core-graphics-types 0.1.1 (git+https://github.com/servo/core-foundation-rs/)", + "core-foundation", + "core-graphics-types", "foreign-types", "libc", ] @@ -405,18 +347,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3a68b68b3446082644c91ac778bf50cd4104bfb002b5a6a7c44cca5a2c70788b" dependencies = [ "bitflags", - "core-foundation 0.9.3 (registry+https://github.com/rust-lang/crates.io-index)", - "foreign-types", - "libc", -] - -[[package]] -name = "core-graphics-types" -version = "0.1.1" -source = "git+https://github.com/servo/core-foundation-rs/#8c2444066aac42c20443b696270f5b1afff18993" -dependencies = [ - "bitflags", - "core-foundation 0.9.3 (git+https://github.com/servo/core-foundation-rs/)", + "core-foundation", "foreign-types", "libc", ] @@ -2555,9 +2486,9 @@ dependencies = [ "bitflags", "cairo-rs", "cc", - "cocoa 0.24.1 (registry+https://github.com/rust-lang/crates.io-index)", - "core-foundation 0.9.3 (registry+https://github.com/rust-lang/crates.io-index)", - "core-graphics 0.22.3 (registry+https://github.com/rust-lang/crates.io-index)", + "cocoa", + "core-foundation", + "core-graphics", "crossbeam-channel", "dispatch", "gdk", @@ -2610,7 +2541,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5b48820ee3bb6a5031a83b2b6e11f8630bdc5a2f68cb841ab8ebc7a15a916679" dependencies = [ "anyhow", - "cocoa 0.24.1 (registry+https://github.com/rust-lang/crates.io-index)", + "cocoa", "dirs-next", "embed_plist", "encoding_rs", @@ -2740,7 +2671,7 @@ version = "0.12.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "36b1c5764a41a13176a4599b5b7bd0881bea7d94dfe45e1e755f789b98317e30" dependencies = [ - "cocoa 0.24.1 (registry+https://github.com/rust-lang/crates.io-index)", + "cocoa", "gtk", "percent-encoding", "rand 0.8.5", @@ -3443,8 +3374,8 @@ checksum = "4c1ad8e2424f554cc5bdebe8aa374ef5b433feff817aebabca0389961fc7ef98" dependencies = [ "base64", "block", - "cocoa 0.24.1 (registry+https://github.com/rust-lang/crates.io-index)", - "core-graphics 0.22.3 (registry+https://github.com/rust-lang/crates.io-index)", + "cocoa", + "core-graphics", "crossbeam-channel", "dunce", "gdk", diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 05d7e4b..4bad2b6 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -25,12 +25,12 @@ tauri-plugin-window-state = { git = "https://github.com/tauri-apps/tauri-plugin- # webbrowser = "0.8.2" # wry = "0.23.4" -[target.'cfg(target_os = "linux")'.dependencies] -webkit2gtk = "0.18.0" +# [target.'cfg(target_os = "linux")'.dependencies] +# webkit2gtk = "0.18.0" -[target.'cfg(target_os = "macos")'.dependencies] -objc = "0.2.7" -cocoa = {git = "https://github.com/servo/core-foundation-rs/"} +# [target.'cfg(target_os = "macos")'.dependencies] +# objc = "0.2.7" +# cocoa = {git = "https://github.com/servo/core-foundation-rs/"} [features] # by default Tauri runs in production mode diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index eed0990..9082958 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -1,16 +1,13 @@ #[cfg(target_os = "macos")] use tauri::MenuItem; -#[cfg(target_os = "macos")] -#[macro_use] -extern crate objc; - -#[cfg(any(target_os = "linux", target_os = "windows"))] -use tauri::{Manager, SystemTray, SystemTrayEvent, SystemTrayMenu}; +// #[cfg(target_os = "macos")] +// #[macro_use] +// extern crate objc; use tauri::{ - window::PlatformWebview, App, Config, CustomMenuItem, Menu, Submenu, Window, WindowBuilder, - WindowMenuEvent, WindowUrl, + window::PlatformWebview, App, Config, CustomMenuItem, Manager, Menu, Submenu, SystemTray, + SystemTrayEvent, SystemTrayMenu, Window, WindowBuilder, WindowMenuEvent, WindowUrl, }; mod pake; use pake::PakeConfig; @@ -50,19 +47,19 @@ pub fn get_menu() -> Menu { // let previous = CustomMenuItem::new("previous", "Previous (←)"); // let next = CustomMenuItem::new("next", "next (→)"); // let refresh = CustomMenuItem::new("refresh", "Refresh"); - let zoom_out = CustomMenuItem::new("zoom_out", "Zoom Out (125%)"); - let zoom_in = CustomMenuItem::new("zoom_in", "Zoom In (75%)"); - let zoom_reset = CustomMenuItem::new("reset", "Zoom Reset"); - let hot_key = Menu::new() - // .add_item(top) - // .add_item(buttom) - // .add_item(previous) - // .add_item(next) - // .add_item(refresh) - .add_item(zoom_in) - .add_item(zoom_out) - .add_item(zoom_reset); - let hot_key_menu = Submenu::new("Hot Key", hot_key); + // let zoom_out = CustomMenuItem::new("zoom_out", "Zoom Out (125%)"); + // let zoom_in = CustomMenuItem::new("zoom_in", "Zoom In (75%)"); + // let zoom_reset = CustomMenuItem::new("reset", "Zoom Reset"); + // let hot_key = Menu::new() + // .add_item(top) + // .add_item(buttom) + // .add_item(previous) + // .add_item(next) + // .add_item(refresh) + // .add_item(zoom_in) + // .add_item(zoom_out) + // .add_item(zoom_reset); + // let hot_key_menu = Submenu::new("Hot Key", hot_key); // Help // let instructions = CustomMenuItem::new("instruction", "Instruction"); @@ -71,47 +68,46 @@ pub fn get_menu() -> Menu { // .add_item(instructions) // .add_item(about); // let help_menu = Submenu::new("Help", help); - Menu::new() - .add_submenu(first_menu) - .add_submenu(hot_key_menu) + Menu::new().add_submenu(first_menu) + // .add_submenu(hot_key_menu) } -pub fn set_zoom(webview: PlatformWebview, zoom_value: f64) { - #[cfg(target_os = "linux")] - { - // see https://docs.rs/webkit2gtk/0.18.2/webkit2gtk/struct.WebView.html - // and https://docs.rs/webkit2gtk/0.18.2/webkit2gtk/trait.WebViewExt.html - use webkit2gtk::traits::WebViewExt; - webview.inner().set_zoom_level(zoom_value); - } +// pub fn set_zoom(webview: PlatformWebview, zoom_value: f64) { +// #[cfg(target_os = "linux")] +// { +// // see https://docs.rs/webkit2gtk/0.18.2/webkit2gtk/struct.WebView.html +// // and https://docs.rs/webkit2gtk/0.18.2/webkit2gtk/trait.WebViewExt.html +// use webkit2gtk::traits::WebViewExt; +// webview.inner().set_zoom_level(zoom_value); +// } - #[cfg(windows)] - unsafe { - // see https://docs.rs/webview2-com/0.19.1/webview2_com/Microsoft/Web/WebView2/Win32/struct.ICoreWebView2Controller.html - webview.controller().SetZoomFactor(zoom_value).unwrap(); - } +// #[cfg(windows)] +// unsafe { +// // see https://docs.rs/webview2-com/0.19.1/webview2_com/Microsoft/Web/WebView2/Win32/struct.ICoreWebView2Controller.html +// webview.controller().SetZoomFactor(zoom_value).unwrap(); +// } - #[cfg(target_os = "macos")] - unsafe { - let () = msg_send![webview.inner(), setPageZoom: zoom_value]; - let () = msg_send![webview.controller(), removeAllUserScripts]; - let bg_color: cocoa::base::id = - msg_send![class!(NSColor), colorWithDeviceRed:0.5 green:0.2 blue:0.4 alpha:1.]; - let () = msg_send![webview.ns_window(), setBackgroundColor: bg_color]; - } -} +// #[cfg(target_os = "macos")] +// unsafe { +// let () = msg_send![webview.inner(), setPageZoom: zoom_value]; +// let () = msg_send![webview.controller(), removeAllUserScripts]; +// let bg_color: cocoa::base::id = +// msg_send![class!(NSColor), colorWithDeviceRed:0.5 green:0.2 blue:0.4 alpha:1.]; +// let () = msg_send![webview.ns_window(), setBackgroundColor: bg_color]; +// } +// } -pub fn set_zoom_out(webview: PlatformWebview) { - set_zoom(webview, 1.25); -} +// pub fn set_zoom_out(webview: PlatformWebview) { +// set_zoom(webview, 1.25); +// } -pub fn set_zoom_in(webview: PlatformWebview) { - set_zoom(webview, 0.75); -} +// pub fn set_zoom_in(webview: PlatformWebview) { +// set_zoom(webview, 0.75); +// } -pub fn zoom_reset(webview: PlatformWebview) { - set_zoom(webview, 1.0); -} +// pub fn zoom_reset(webview: PlatformWebview) { +// set_zoom(webview, 1.0); +// } pub fn menu_event_handle(event: WindowMenuEvent) { match event.menu_item_id() { @@ -119,24 +115,24 @@ pub fn menu_event_handle(event: WindowMenuEvent) { "show" => event.window().show().expect("can't show window"), "close" => event.window().close().expect("can't close window"), "quit" => std::process::exit(0), - "zoom_out" => { - event - .window() - .with_webview(set_zoom_out) - .expect("can't set zoom out"); - } - "zoom_in" => { - event - .window() - .with_webview(set_zoom_in) - .expect("can't set zoom in"); - } - "reset" => { - event - .window() - .with_webview(zoom_reset) - .expect("can't reset zoom"); - } + // "zoom_out" => { + // event + // .window() + // .with_webview(set_zoom_out) + // .expect("can't set zoom out"); + // } + // "zoom_in" => { + // event + // .window() + // .with_webview(set_zoom_in) + // .expect("can't set zoom in"); + // } + // "reset" => { + // event + // .window() + // .with_webview(zoom_reset) + // .expect("can't reset zoom"); + // } _ => {} } } @@ -158,17 +154,18 @@ pub fn get_system_tray(show_menu: bool) -> SystemTray { let show_app = CustomMenuItem::new("show_app".to_string(), "Show App"); let quit = CustomMenuItem::new("quit".to_string(), "Quit"); let about = CustomMenuItem::new("about".to_string(), "About"); - let tray_menu = SystemTrayMenu::new() - .add_item(hide_app) - .add_item(show_app) - .add_item(quit) - .add_item(about); + let tray_menu = SystemTrayMenu::new().add_item(hide_app).add_item(show_app); if show_menu { let hide_menu = CustomMenuItem::new("hide_menu".to_string(), "Hide Menu"); let show_menu = CustomMenuItem::new("show_menu".to_string(), "Show Menu"); - let tray_menu = tray_menu.add_item(hide_menu).add_item(show_menu); + let tray_menu = tray_menu + .add_item(hide_menu) + .add_item(show_menu) + .add_item(quit) + .add_item(about); SystemTray::new().with_menu(tray_menu) } else { + let tray_menu = tray_menu.add_item(quit).add_item(about); SystemTray::new().with_menu(tray_menu) } } diff --git a/src-tauri/tauri.conf.json b/src-tauri/tauri.conf.json index 6395ef3..35ed045 100644 --- a/src-tauri/tauri.conf.json +++ b/src-tauri/tauri.conf.json @@ -9,6 +9,10 @@ }, "updater": { "active": false + }, + "systemTray": { + "iconPath": "png/weread_512.png", + "iconAsTemplate": true } }, "build": { diff --git a/src-tauri/tauri.linux.conf.json b/src-tauri/tauri.linux.conf.json index a78696c..c4c78b0 100644 --- a/src-tauri/tauri.linux.conf.json +++ b/src-tauri/tauri.linux.conf.json @@ -1,9 +1,5 @@ { "tauri": { - "systemTray": { - "iconPath": "png/weread_512.png", - "iconAsTemplate": true - }, "bundle": { "icon": [ "png/weread_256.ico", diff --git a/src-tauri/tauri.windows.conf.json b/src-tauri/tauri.windows.conf.json index 9b5a348..0c2eb5b 100644 --- a/src-tauri/tauri.windows.conf.json +++ b/src-tauri/tauri.windows.conf.json @@ -1,9 +1,5 @@ { "tauri": { - "systemTray": { - "iconPath": "png/weread_256.ico", - "iconAsTemplate": true - }, "bundle": { "icon": [ "png/weread_256.ico",