Skip to content

Commit

Permalink
Add find window.
Browse files Browse the repository at this point in the history
  • Loading branch information
oldj committed May 6, 2021
1 parent f23815e commit f90e245
Show file tree
Hide file tree
Showing 16 changed files with 173 additions and 9 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"mkdirp": "^1.0.4",
"potdb": "^2.0.1",
"uuid": "^8.3.2"
},
},
"devDependencies": {
"@babel/plugin-proposal-class-properties": "^7.13.0",
"@babel/plugin-proposal-decorators": "^7.13.5",
Expand Down
10 changes: 9 additions & 1 deletion src/common/i18n/languages/en.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/**
* @author: oldj
* @homepage: https://oldj.net
* en
*/

export default {
Expand Down Expand Up @@ -43,6 +42,10 @@ export default {
fail: 'Fail!',
feedback: 'Feedback',
file: 'File',
find: 'Find',
find_all: 'Find all',
find_and_replace: 'Find and replace',
find_history: 'Find history',
folder: 'Folder',
front: 'Front',
general: 'General',
Expand All @@ -65,6 +68,7 @@ export default {
hours: 'hours',
http_api_on: 'HTTP API on',
http_api_on_desc: 'Runs on port {0}, can be used by third-party software such as Alfred to switch hosts',
ignore_case: 'Ignore case',
import: 'Import',
import_done: 'The import is complete.',
import_fail: 'Import failed!',
Expand Down Expand Up @@ -99,10 +103,14 @@ export default {
read_only: 'Read only',
redo: 'Redo',
refresh: 'Refresh',
regexp: 'Regular expression',
reload: 'Reload',
remote: 'Remote',
remove_duplicate_records: 'Remove duplicate records',
remove_duplicate_records_desc: 'If a domain points to multiple IPs, only the first one will take effect, and the following ones will be converted into comments.',
replace: 'Replace',
replace_all: 'Replace all',
replace_history: 'Replace history',
reset_zoom: 'Reset zoom',
search: 'Search',
select_all: 'Select all',
Expand Down
10 changes: 9 additions & 1 deletion src/common/i18n/languages/zh.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/**
* @author: oldj
* @homepage: https://oldj.net
* zh
*/

import { LanguageDict } from '@root/common/types'
Expand Down Expand Up @@ -45,6 +44,10 @@ const lang: LanguageDict = {
fail: '操作失败!',
feedback: '意见反馈',
file: '文件',
find: '查找',
find_all: '查找所有',
find_and_replace: '查找并替换',
find_history: '查找历史',
folder: '文件夹',
front: '前置',
general: '通用',
Expand All @@ -67,6 +70,7 @@ const lang: LanguageDict = {
hours: '小时',
http_api_on: '开启 HTTP API',
http_api_on_desc: '运行于 {0} 端口,可用于 Alfred 等第三方软件切换 hosts',
ignore_case: '忽略大小写',
import: '导入',
import_done: '导入已完成。',
import_fail: '导入失败!',
Expand Down Expand Up @@ -101,10 +105,14 @@ const lang: LanguageDict = {
read_only: '只读',
redo: '重做',
refresh: '刷新',
regexp: '正则表达式',
reload: '重载',
remote: '远程',
remove_duplicate_records: '移除重复的记录',
remove_duplicate_records_desc: '如果一个域名指向多个 IP,只有第一条会生效,后面的将被转为注释。',
replace: '替换',
replace_all: '替换所有',
replace_history: '替换历史',
reset_zoom: '重置缩放',
search: '搜索',
select_all: '全选',
Expand Down
2 changes: 1 addition & 1 deletion src/main/actions/config/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import { updateTrayTitle } from '@main/actions'
import { cfgdb } from '@main/data'
import { makeMainMenu } from '@main/libs/menu'
import { makeMainMenu } from '@main/ui/menu'
import { ConfigsType } from '@root/common/default_configs'
import * as http_api from '@main/http'

Expand Down
15 changes: 15 additions & 0 deletions src/main/actions/find/show.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* @author: oldj
* @homepage: https://oldj.net
*/

import { makeWindow } from '@main/ui/find'

export default async () => {
if (!global.find_win) {
global.find_win = await makeWindow()
}

global.find_win?.show()
global.find_win?.focus()
}
2 changes: 2 additions & 0 deletions src/main/actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ export { default as checkUpdate } from './checkUpdate'
export { default as closeMainWindow } from './closeMainWindow'
export { default as quit } from './quit'

export { default as findShow } from './find/show'

export { default as migrateCheck } from './migrate/checkIfMigration'
export { default as migrateData } from './migrate/migrateData'
export { default as exportData } from './migrate/export'
Expand Down
2 changes: 1 addition & 1 deletion src/main/actions/updateTrayTitle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import { getList } from '@main/actions/index'
import { cfgdb } from '@main/data'
import { tray } from '@main/tray'
import { tray } from '@main/ui/tray'
import { flatten } from '@root/common/hostsFn'

export default async (show?: boolean, title?: string) => {
Expand Down
7 changes: 5 additions & 2 deletions src/main/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import * as http_api from '@main/http'
import * as cron from '@main/libs/cron'
import getIndex from '@main/libs/getIndex'
import isDev from '@main/libs/isDev'
import { makeMainMenu } from '@main/libs/menu'
import { makeMainMenu } from '@main/ui/menu'
import Tracer from '@main/libs/tracer'
import '@main/tray'
import '@main/ui/tray'
import * as find from '@main/ui/find'
import version from '@root/version.json'
import { app, BrowserWindow } from 'electron'
import windowStateKeeper from 'electron-window-state'
Expand Down Expand Up @@ -125,6 +126,8 @@ app.on('ready', async () => {
if (http_api_on) {
http_api.start()
}

find.makeWindow()
})

app.on('window-all-closed', () => {
Expand Down
1 change: 1 addition & 0 deletions src/main/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ declare global {
ua: string; // user agent
session_id: string; // A random value, refreshed every time the app starts, used to identify different startup sessions.
main_win: BrowserWindow;
find_win?: BrowserWindow | null;
last_path?: string; // the last path opened by SwitchHosts
tracer: Tracer;
is_will_quit?: boolean;
Expand Down
67 changes: 67 additions & 0 deletions src/main/ui/find.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/**
* @author: oldj
* @homepage: https://oldj.net
*/

import getIndex from '@main/libs/getIndex'
import isDev from '@main/libs/isDev'
import { BrowserWindow } from 'electron'
import path from 'path'

const makeWindow = () => {
let win: BrowserWindow | null
win = new BrowserWindow({
// frame: false,
// titleBarStyle: 'hidden',
hasShadow: true,
// resizable: false,
// transparent: true,
width: 480,
height: 400,
minWidth: 300,
minHeight: 200,
maximizable: false,
minimizable: false,
skipTaskbar: true,
// show: false,
webPreferences: {
contextIsolation: true,
preload: path.join(__dirname, 'preload.js'),
spellcheck: true,
},
})

win.setVisibleOnAllWorkspaces(true, {
visibleOnFullScreen: true,
})

win.loadURL(`${getIndex()}#/find`)
.catch(e => console.error(e))

// win.on('blur', () => win?.hide())

win.on('close', (e: Electron.Event) => {
if (global.is_will_quit) {
win = null
global.find_win = null
} else {
e.preventDefault()
win?.hide()
}
})

if (isDev()) {
// Open DevTools, see https://github.com/electron/electron/issues/12438 for why we wait for dom-ready
win.webContents.once('dom-ready', () => {
win!.webContents.openDevTools()
})
}

global.find_win = win

return win
}

export {
makeWindow,
}
8 changes: 8 additions & 0 deletions src/main/libs/menu.ts → src/main/ui/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* @blog https://oldj.net
*/

import { findShow } from '@main/actions'
import isDev from '@main/libs/isDev'
import { BrowserWindow, Menu, MenuItem, MenuItemConstructorOptions, shell } from 'electron'
import { I18N, LocaleName } from '@root/common/i18n'
Expand Down Expand Up @@ -102,6 +103,13 @@ export const makeMainMenu = (locale: LocaleName = 'en') => {
broadcast('toggle_comment')
},
},
{
label: lang.find_and_replace,
accelerator: 'CommandOrControl+F',
click () {
findShow()
},
},
],
},
{
Expand Down
2 changes: 1 addition & 1 deletion src/main/tray/index.ts → src/main/ui/tray/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import { configGet, configSet, updateTrayTitle } from '@main/actions'
import { broadcast } from '@main/core/agent'
import { makeWindow } from '@main/tray/window'
import { makeWindow } from '@main/ui/tray/window'
import { I18N } from '@root/common/i18n'
import version from '@root/version.json'
import { app, BrowserWindow, Menu, screen, Tray } from 'electron'
Expand Down
File renamed without changes.
5 changes: 5 additions & 0 deletions src/renderer/pages/find.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@import "../styles/common";

.root {

}
47 changes: 47 additions & 0 deletions src/renderer/pages/find.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* @author: oldj
* @homepage: https://oldj.net
*/

import { useModel } from '@@/plugin-model/useModel'
import { agent } from '@renderer/core/agent'
import React, { useEffect } from 'react'
import styles from './find.less'

interface Props {

}

const find = (props: Props) => {
const { lang, setLocale } = useModel('useI18n')
const { configs } = useModel('useConfigs')

const init = async () => {
if (!configs) return

setLocale(configs.locale)

let theme = configs.theme
let cls = document.body.className
document.body.className = cls.replace(/\btheme-\w+/ig, '')
document.body.classList.add(`platform-${agent.platform}`, `theme-${theme}`)
}

useEffect(() => {
if (!configs) return
init().catch(e => console.error(e))
}, [configs])

useEffect(() => {
console.log(lang.find_and_replace)
document.title = lang.find_and_replace
}, [lang])

return (
<div className={styles.root}>
find
</div>
)
}

export default find
2 changes: 1 addition & 1 deletion src/version.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[4, 0, 0, 6029]
[4, 0, 1, 6030]

0 comments on commit f90e245

Please sign in to comment.