Skip to content

Commit

Permalink
✨ Feature(custom): add tray tooltip
Browse files Browse the repository at this point in the history
  • Loading branch information
Kuingsmile committed Jun 7, 2024
1 parent 6432837 commit 8a565c1
Show file tree
Hide file tree
Showing 11 changed files with 42 additions and 104 deletions.
12 changes: 6 additions & 6 deletions src/main/apis/app/system/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import windowManager from 'apis/app/window/windowManager'
import { IPasteStyle, IWindowList } from '#/types/enum'
import pasteTemplate from '~/main/utils/pasteTemplate'
import pkg from 'root/package.json'
import { ensureFilePath, handleCopyUrl } from '~/main/utils/common'
import { ensureFilePath, handleCopyUrl, setTray, tray } from '~/main/utils/common'
import { T } from '~/main/i18n'
import { isMacOSVersionGreaterThanOrEqualTo } from '~/main/utils/getMacOSVersion'
import { buildPicBedListMenu } from '~/main/events/remotes/menu'
Expand All @@ -31,7 +31,6 @@ import { uploadClipboardFiles } from '../uploader/apis'
import { configPaths } from '~/universal/utils/configPaths'

let contextMenu: Menu | null
let tray: Tray | null

export function setDockMenu () {
const isListeningClipboard = db.get(configPaths.settings.isListeningClipboard) || false
Expand Down Expand Up @@ -336,9 +335,10 @@ const getTrayIcon = () => {
}
}

export function createTray () {
export function createTray (tooltip: string) {
const menubarPic = getTrayIcon()
tray = new Tray(menubarPic)
setTray(new Tray(menubarPic))
tray.setToolTip(tooltip)
// click事件在Mac和Windows上可以触发(在Ubuntu上无法触发,Unity不支持)
if (process.platform === 'darwin' || process.platform === 'win32') {
tray.on('right-click', () => {
Expand All @@ -348,7 +348,7 @@ export function createTray () {
createContextMenu()
tray!.popUpContextMenu(contextMenu!)
})
tray.on('click', (event, bounds) => {
tray.on('click', (_, bounds) => {
if (process.platform === 'darwin') {
toggleWindow(bounds)
setTimeout(async () => {
Expand Down Expand Up @@ -412,7 +412,7 @@ export function createTray () {

// drop-files only be supported in macOS
// so the tray window must be available
tray.on('drop-files', async (event: Event, files: string[]) => {
tray.on('drop-files', async (_: Event, files: string[]) => {
const pasteStyle = db.get(configPaths.settings.pasteStyle) || IPasteStyle.MARKDOWN
const rawInput = cloneDeep(files)
const trayWindow = windowManager.get(IWindowList.TRAY_WINDOW)!
Expand Down
6 changes: 5 additions & 1 deletion src/main/events/ipcList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ import {
import picgoCoreIPC from './picgoCoreIPC'

// 处理复制的 URL 和生成短链接的函数
import { handleCopyUrl, generateShortUrl } from '~/main/utils/common'
import { handleCopyUrl, generateShortUrl, setTrayToolTip } from '~/main/utils/common'

// 构建主页面、迷你页面、插件页面、图片床列表的菜单函数
import { buildMainPageMenu, buildMiniPageMenu, buildPluginPageMenu, buildPicBedListMenu } from './remotes/menu'
Expand Down Expand Up @@ -136,6 +136,10 @@ export default {
return uploadChoosedFiles(evt.sender, files)
})

ipcMain.on('setTrayToolTip', (_: IpcMainEvent, title: string) => {
setTrayToolTip(title)
})

// ShortKey Related IPC
ipcMain.on('updateShortKey', (evt: IpcMainEvent, item: IShortKeyConfig, oldKey: string, from: string) => {
const result = shortKeyHandler.updateShortKey(item, oldKey, from)
Expand Down
3 changes: 3 additions & 0 deletions src/main/events/remotes/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
import { PicGo as PicGoCore } from 'piclist'
import { T } from '~/main/i18n'
import { configPaths } from '~/universal/utils/configPaths'
import { setTrayToolTip } from '~/main/utils/common'

interface GuiMenuItem {
label: string
Expand Down Expand Up @@ -185,6 +186,7 @@ const buildPicBedListMenu = () => {
if (windowManager.has(IWindowList.SETTING_WINDOW)) {
windowManager.get(IWindowList.SETTING_WINDOW)!.webContents.send('syncPicBed')
}
setTrayToolTip(`${item.type} ${config._configName || 'Default'}`)
}
}
})
Expand All @@ -198,6 +200,7 @@ const buildPicBedListMenu = () => {
if (windowManager.has(IWindowList.SETTING_WINDOW)) {
windowManager.get(IWindowList.SETTING_WINDOW)!.webContents.send('syncPicBed')
}
setTrayToolTip(item.type)
}
: undefined
}
Expand Down
8 changes: 6 additions & 2 deletions src/main/lifeCycle/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,15 @@ class LifeCycle {
}
const isHideDock = db.get(configPaths.settings.isHideDock) || false
const startMode = db.get(configPaths.settings.startMode) || ISartMode.QUIET
const currentPicBed = db.get(configPaths.picBed.uploader) || db.get(configPaths.picBed.current) || 'smms'
// @ts-ignore
const currentPicBedConfig = db.get(`picBed.${currentPicBed}`)?._configName || 'Default'
const tooltip = `${currentPicBed} ${currentPicBedConfig}`
if (process.platform === 'darwin') {
isHideDock ? app.dock.hide() : setDockMenu()
startMode !== ISartMode.NO_TRAY && createTray()
startMode !== ISartMode.NO_TRAY && createTray(tooltip)
} else {
createTray()
createTray(tooltip)
}
db.set(configPaths.needReload, false)
updateChecker()
Expand Down
14 changes: 13 additions & 1 deletion src/main/utils/common.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
import fs from 'fs-extra'
import db from '~/main/apis/core/datastore'
import { clipboard, Notification, dialog } from 'electron'
import { clipboard, Notification, dialog, Tray } from 'electron'
import { handleUrlEncode } from '~/universal/utils/common'
import axios from 'axios'
import FormData from 'form-data'
import logger from '../apis/core/picgo/logger'
import { configPaths } from '~/universal/utils/configPaths'
import { IShortUrlServer } from '~/universal/types/enum'

export let tray: Tray

export const setTray = (t: Tray) => { tray = t }

export const getTray = () => tray

export function setTrayToolTip (title: string): void {
if (tray) {
tray.setToolTip(title)
}
}

export const handleCopyUrl = (str: string): void => {
if (db.get(configPaths.settings.autoCopy) !== false) {
clipboard.writeText(str)
Expand Down
2 changes: 2 additions & 0 deletions src/main/utils/handleUploaderConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { v4 as uuid } from 'uuid'
import { trimValues } from '#/utils/common'
import picgo from '@core/picgo'
import { configPaths } from '~/universal/utils/configPaths'
import { setTrayToolTip } from './common'

export const handleConfigWithFunction = (config: IPicGoPluginOriginConfig[]): IPicGoPluginConfig[] => {
for (const i in config) {
Expand Down Expand Up @@ -65,6 +66,7 @@ export const changeCurrentUploader = (type: string, config?: IStringKeyMap, id?:
[configPaths.picBed.current]: type,
[configPaths.picBed.uploader]: type
})
setTrayToolTip(`${type} ${config?._configName || ''}`)
}

export const selectUploaderConfig = (type: string, id: string) => {
Expand Down
43 changes: 0 additions & 43 deletions src/renderer/pages/Plugin.vue
Original file line number Diff line number Diff line change
Expand Up @@ -228,28 +228,15 @@
</div>
</template>
<script lang="ts" setup>
// Element Plus 图标
import { Close, Download, Refresh, Goods, Remove, Tools } from '@element-plus/icons-vue'
// 国际化函数
import { T as $T } from '@/i18n/index'
// 组件
import ConfigForm from '@/components/ConfigFormForPlugin.vue'
// Lodash 函数节流
import { debounce, DebouncedFunc } from 'lodash'
// Electron 相关
import {
ipcRenderer,
IpcRendererEvent
} from 'electron'
// 工具函数
import { handleStreamlinePluginName } from '~/universal/utils/common'
// 事件常量
import {
OPEN_URL,
PICGO_CONFIG_PLUGIN,
Expand All @@ -259,20 +246,10 @@ import {
GET_PICBEDS,
PICGO_HANDLE_PLUGIN_DONE
} from '#/events/constants'
// Vue 相关
import { computed, ref, onBeforeMount, onBeforeUnmount, watch, onMounted, reactive, toRaw } from 'vue'
// 数据发送工具函数
import { getConfig, saveConfig, sendRPC, sendToMain } from '@/utils/dataSender'
// Element Plus 消息框组件
import { ElMessageBox } from 'element-plus'
// Axios
import axios from 'axios'
// 枚举类型声明
import { IRPCActionType } from '~/universal/types/enum'
import { configPaths } from '~/universal/utils/configPaths'
Expand Down Expand Up @@ -461,26 +438,6 @@ function installPlugin (item: IPicGoPlugin) {
}
}
// function uninstallPlugin (val: string) {
// pluginList.value.forEach(item => {
// if (item.name === val) {
// item.ing = true
// }
// })
// loading.value = true
// sendToMain('uninstallPlugin', val)
// }
// function updatePlugin (val: string) {
// pluginList.value.forEach(item => {
// if (item.fullName === val) {
// item.ing = true
// }
// })
// loading.value = true
// sendToMain('updatePlugin', val)
// }
function reloadApp () {
sendRPC(IRPCActionType.RELOAD_APP)
}
Expand Down
18 changes: 2 additions & 16 deletions src/renderer/pages/RenamePage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -51,29 +51,15 @@
</el-row>
</div>
</template>

<script lang="ts" setup>
// Element Plus 图标
import { Close } from '@element-plus/icons-vue'
// 事件常量
import { GET_RENAME_FILE_NAME, RENAME_FILE_NAME } from '#/events/constants'
// 数据发送工具函数
import { sendToMain } from '@/utils/dataSender'
// 国际化函数
import { T as $T } from '@/i18n/index'
// Electron 相关
import { ipcRenderer, IpcRendererEvent } from 'electron'
// Vue 生命周期钩子
import { onBeforeUnmount, onBeforeMount, ref, reactive } from 'vue'
// 自定义钩子
import { useIPCOn } from '@/hooks/useIPC'
// Element Plus 表单实例类型
import { FormInstance } from 'element-plus'
const id = ref<string | null>(null)
Expand All @@ -84,7 +70,7 @@ const form = reactive({
originName: ''
})
const handleFileName = (event: IpcRendererEvent, newName: string, _originName: string, _id: string) => {
const handleFileName = (_: IpcRendererEvent, newName: string, _originName: string, _id: string) => {
form.fileName = newName
form.originName = _originName
id.value = _id
Expand Down
11 changes: 0 additions & 11 deletions src/renderer/pages/ShortKey.vue
Original file line number Diff line number Diff line change
Expand Up @@ -117,22 +117,11 @@
</div>
</template>
<script lang="ts" setup>
// 按键绑定工具函数
import keyBinding from '@/utils/key-binding'
// Electron 相关
import { ipcRenderer, IpcRendererEvent } from 'electron'
// 事件常量
import { TOGGLE_SHORTKEY_MODIFIED_MODE } from '#/events/constants'
// Vue 生命周期钩子
import { onBeforeUnmount, onBeforeMount, ref, watch } from 'vue'
// 数据发送工具函数
import { getConfig, sendToMain } from '@/utils/dataSender'
// 国际化函数
import { T as $T } from '@/i18n'
import { configPaths } from '~/universal/utils/configPaths'
Expand Down
23 changes: 0 additions & 23 deletions src/renderer/pages/TrayPage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -62,31 +62,14 @@
</template>

<script lang="ts" setup>
// Vue 相关
import { reactive, ref, onBeforeUnmount, onBeforeMount } from 'vue'
// Electron 相关
import { clipboard, ipcRenderer } from 'electron'
// 数据库操作
import $$db from '@/utils/db'
// 国际化函数
import { T as $T } from '@/i18n/index'
// Picgo Store 相关类型
import { IResult } from '@picgo/store/dist/types'
// 事件常量
import { OPEN_WINDOW } from '#/events/constants'
// 枚举类型声明
import { IPasteStyle, IWindowList } from '#/types/enum'
// 数据发送工具函数
import { getConfig, sendToMain } from '@/utils/dataSender'
// 工具函数
import { handleUrlEncode } from '#/utils/common'
import { configPaths } from '~/universal/utils/configPaths'
Expand All @@ -99,8 +82,6 @@ const notification = reactive({
const clipboardFiles = ref<ImgInfo[]>([])
const uploadFlag = ref(false)
// const reverseList = computed(() => files.value.slice().reverse())
function openSettingWindow () {
sendToMain(OPEN_WINDOW, IWindowList.SETTING_WINDOW)
}
Expand Down Expand Up @@ -166,10 +147,6 @@ async function pasteTemplate (style: IPasteStyle, item: ImgInfo, customLink: str
return tpl[style]
}
// function calcHeight (width: number, height: number): number {
// return height * 160 / width
// }
function disableDragFile () {
window.addEventListener('dragover', (e) => {
e = e || event
Expand Down
6 changes: 5 additions & 1 deletion src/renderer/pages/UploaderConfigPage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ import { PICBEDS_PAGE, UPLOADER_CONFIG_PAGE } from '@/router/config'
// 状态管理
import { useStore } from '@/hooks/useStore'
import { configPaths } from '~/universal/utils/configPaths'
import { ipcRenderer } from 'electron'
const $router = useRouter()
const $route = useRoute()
Expand All @@ -130,10 +131,11 @@ const store = useStore()
async function selectItem (id: string) {
await triggerRPC<void>(IRPCActionType.SELECT_UPLOADER, type.value, id)
ipcRenderer.send('setTrayToolTip', `${type.value} ${curConfigList.value.find(item => item._id === id)?._configName || ''}`)
defaultConfigId.value = id
}
onBeforeRouteUpdate((to, from, next) => {
onBeforeRouteUpdate((to, _, next) => {
if (to.params.type && (to.name === UPLOADER_CONFIG_PAGE)) {
type.value = to.params.type as string
getCurrentConfigList()
Expand Down Expand Up @@ -193,6 +195,8 @@ function setDefaultPicBed (type: string) {
})
store?.setDefaultPicBed(type)
const currentConfigName = curConfigList.value.find(item => item._id === defaultConfigId.value)?._configName
ipcRenderer.send('setTrayToolTip', `${type} ${currentConfigName || ''}`)
const successNotification = new Notification($T('SETTINGS_DEFAULT_PICBED'), {
body: $T('TIPS_SET_SUCCEED')
})
Expand Down

0 comments on commit 8a565c1

Please sign in to comment.