Skip to content

Commit

Permalink
*
Browse files Browse the repository at this point in the history
  • Loading branch information
showlotus committed Nov 26, 2024
1 parent 4aae0d6 commit d138e79
Show file tree
Hide file tree
Showing 12 changed files with 181 additions and 117 deletions.
7 changes: 4 additions & 3 deletions LOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,13 @@ npm install --global --production windows-build-tools@4.0.0
- [ ] 0% -- 如何检查更新,以及更新到最新版本。
- [ ] 0% -- 功能测试。
- [ ] 0% -- 图标更换,尽量不要中间留空,最好是实心的。
- [ ] 0% -- 首次启动时,首屏加载 loading 替换。
- [ ] 50% -- fix: 过期数据清理,<del>关闭应用前,清理过期的数据</del> 每次新增数据成功后,清理一次过期数据。
- [ ] -- fix: 开机启动配置始终开启,无法关闭。
- [x] 100% -- 首次启动时,首屏加载 loading 替换。
- [x] 100% -- fix: 过期数据清理,<del>关闭应用前,清理过期的数据</del> 每次新增数据成功后,清理一次过期数据。
- [x] 100% -- fix: 开机启动配置始终开启,无法关闭。
- [ ] -- fix: 某些系统应用获取不到当前应用程序,例如:计算器。
- [x] 100% -- feat: 记录文件的复制,并实现文件的粘贴。
- [ ] -- pref: 图片存储空间优化,改用 Buffer 存储而不是 Base64 字符串。
- [ ] -- feat: 接入日志管理,记录运行日志。

## log

Expand Down
60 changes: 9 additions & 51 deletions electron/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ import { initTray } from './tray'

const __dirname = path.dirname(fileURLToPath(import.meta.url))

console.log('process.versions.node', process.versions.node)

// The built directory structure
//
// ├─┬ dist-electron
Expand All @@ -46,8 +44,6 @@ process.env.VITE_PUBLIC = VITE_DEV_SERVER_URL
? path.join(process.env.APP_ROOT, 'public')
: RENDERER_DIST

process.argv.push('--openAsHidden')

// Disable GPU Acceleration for Windows 7
if (os.release().startsWith('6.1')) app.disableHardwareAcceleration()

Expand All @@ -63,26 +59,13 @@ let win: BrowserWindow | null = null
const preload = path.join(__dirname, '../preload/index.mjs')
const indexHtml = path.join(RENDERER_DIST, 'index.html')

// TODO 设置开机自启
// 设置开机自启
if (app.isPackaged) {
app.setLoginItemSettings({
openAtLogin: true,
args: ['--openAsHidden']
openAtLogin: true
})
}

if (app.isPackaged) {
const { openAtLogin } = app.getLoginItemSettings({
args: ['--openAsHidden']
})
console.log('openAtLogin', openAtLogin, process.argv)
}

// TODO 获取当前活动应用,监听当前活动应用是否更新,通知视图层更新
ipcMain.handle('get-active-app', (_event) => {
return Promise.resolve('Google Chrome')
})

export function getWinWebContents() {
return win?.webContents
}
Expand Down Expand Up @@ -126,18 +109,10 @@ export function toggleWindowVisible() {

async function createWindow() {
const { width, height } = screen.getPrimaryDisplay().workAreaSize
console.log(width, height)
const winWidth = width * 0.4
const winHeight = height * 1
const IS_DEV = !false
const wh =
VITE_DEV_SERVER_URL && IS_DEV
? { width: winWidth, height: winHeight }
: { width: width * 0.4, height: height * 0.5 }
win = new BrowserWindow({
title: 'Main window',
...wh,
// icon: path.join(process.env.VITE_PUBLIC, 'favicon.ico'),
width: width * 0.4,
height: height * 0.5,
webPreferences: {
preload,
// Warning: Enable nodeIntegration and disable contextIsolation is not secure in production
Expand All @@ -152,27 +127,16 @@ async function createWindow() {
backgroundThrottling: false
},
// transparent: true,
// TODO publish 时需要隐藏标题栏
// titleBarStyle: 'hidden',
// 无边框窗口,隐藏标题和菜单栏
frame: false
// 设置高斯模糊
// 会导致打开窗口时有闪烁问题
// backgroundMaterial: 'mica' // mica acrylic
})

// TEST 靠右显示
// win.setPosition(width - winWidth, 0)

// 隐藏菜单栏
// Menu.setApplicationMenu(null)
// for mac
// Menu.setApplicationMenu(Menu.buildFromTemplate([]))

// 禁用手动最大化
win.setMaximizable(false)
win.setMinimizable(false)
// win.setMovable(false)
// 禁用手动调整窗口大小
win.setResizable(false)
win.setSkipTaskbar(true)
Expand All @@ -187,6 +151,8 @@ async function createWindow() {
win.loadURL(VITE_DEV_SERVER_URL)
// Open devTool if the app is not packaged
win.webContents.openDevTools()
win.setSize(width * 0.4, height)
win.setResizable(true)
} else {
win.loadFile(indexHtml)
}
Expand Down Expand Up @@ -217,19 +183,11 @@ async function createWindow() {
win.webContents.send('hide-win')
})

// TODO 开机启动时隐藏窗口
// TODO 打包时,改为 once
const eventType = VITE_DEV_SERVER_URL ? 'on' : 'once'
win[eventType]('ready-to-show', () => {
console.log(process.argv.includes('--openAsHidden'))
if (!process.argv.includes('--openAsHidden')) {
updateActiveApp()
win.show()
}
win.once('ready-to-show', () => {
updateActiveApp()
win.show()
})

console.log(app.getPath('userData'))

return win
}

Expand Down
8 changes: 7 additions & 1 deletion electron/main/store.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { globalShortcut } from 'electron'
import { app, globalShortcut } from 'electron'
import ElectronStore from 'electron-store'

import { toggleWindowVisible } from '.'
Expand Down Expand Up @@ -30,3 +30,9 @@ SettingsStore.onDidChange('shortcutKey', (val) => {
globalShortcut.unregisterAll()
registerShortcut(val)
})

SettingsStore.onDidChange('startup', (val) => {
app.setLoginItemSettings({
openAtLogin: val
})
})
56 changes: 15 additions & 41 deletions electron/preload/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,61 +60,35 @@ const safeDOM = {
* https://matejkustec.github.io/SpinThatShit
*/
function useLoading() {
const className = `loaders-css__square-spin`
const styleContent = `
@keyframes square-spin {
25% { transform: perspective(100px) rotateX(180deg) rotateY(0); }
50% { transform: perspective(100px) rotateX(180deg) rotateY(180deg); }
75% { transform: perspective(100px) rotateX(0) rotateY(180deg); }
100% { transform: perspective(100px) rotateX(0) rotateY(0); }
}
.${className} > div {
animation-fill-mode: both;
width: 50px;
height: 50px;
background: #fff;
animation: square-spin 3s 0s cubic-bezier(0.09, 0.57, 0.49, 0.9) infinite;
}
.app-loading-wrap {
position: fixed;
top: 0;
left: 0;
width: 100vw;
height: 100vh;
display: flex;
align-items: center;
justify-content: center;
background: #282c34;
z-index: 9;
}
`
const oStyle = document.createElement('style')
const oDiv = document.createElement('div')
const isDarkMode = window.matchMedia('(prefers-color-scheme: dark)').matches
const loadingUrl = isDarkMode ? './loading-light.svg' : './loading-dark.svg'
const div = document.createElement('div')
div.innerHTML = /* html */ `<div class="preload-loading fixed inset-0 flex justify-center items-center">
<img src="${loadingUrl}" class="w-24" />
</div>`

oStyle.id = 'app-loading-style'
oStyle.innerHTML = styleContent
oDiv.className = 'app-loading-wrap'
oDiv.innerHTML = `<div class="${className}"><div></div></div>`
let isFinished = false

return {
appendLoading() {
safeDOM.append(document.head, oStyle)
safeDOM.append(document.body, oDiv)
!isFinished && safeDOM.append(document.body, div)
},
removeLoading() {
safeDOM.remove(document.head, oStyle)
safeDOM.remove(document.body, oDiv)
isFinished = true
safeDOM.remove(document.body, div)
}
}
}

// ----------------------------------------------------------------------

const { appendLoading, removeLoading } = useLoading()
domReady().then(appendLoading)
domReady().then(() => {
setTimeout(() => {
appendLoading()
}, 500)
})

window.onmessage = (ev) => {
ev.data.payload === 'removeLoading' && removeLoading()
}

// setTimeout(removeLoading, 4999)
6 changes: 3 additions & 3 deletions native/native-clipboard/lib/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ std::string removeExeSuffix(const std::string& str) {

// 获取进程名称
std::string GetProcessNameByPID(DWORD processID) {
HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, processID);
HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ | PROCESS_QUERY_LIMITED_INFORMATION, FALSE, processID);
if (NULL == hProcess) {
return "";
}
Expand All @@ -135,7 +135,7 @@ std::string GetProcessNameByPID(DWORD processID) {
// 获取进程可执行文件的路径
std::string GetProcessPath(DWORD processID) {
char processPath[MAX_PATH] = {0};
HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, processID);
HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ | PROCESS_QUERY_LIMITED_INFORMATION, FALSE, processID);
if (hProcess) {
if (GetModuleFileNameExA(hProcess, NULL, processPath, MAX_PATH)) {
CloseHandle(hProcess);
Expand All @@ -148,7 +148,7 @@ std::string GetProcessPath(DWORD processID) {

// 获取进程可执行文件的绝对路径
std::string GetProcessAbsolutePathByPID(DWORD processID) {
HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, processID);
HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ | PROCESS_QUERY_LIMITED_INFORMATION, FALSE, processID);
if (NULL == hProcess) {
return "";
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "NeedClipboard",
"version": "0.1.0-beta.5",
"version": "0.1.0-beta.6",
"main": "dist-electron/main/index.js",
"description": "A clipboard widget for windows.",
"author": "showlotus <showlotus2000@gmail.com>",
Expand Down
65 changes: 65 additions & 0 deletions public/loading-dark.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit d138e79

Please sign in to comment.