Skip to content

Commit

Permalink
optimize: modified sth about action and adaption of window (openai-tr…
Browse files Browse the repository at this point in the history
…anslator#370)

* fix: Fix the offset problem when arrow to right by setting placement

* fix: add 2 more offset postion

* feat: adaptive header width to prevent unnecessary horizontal-scroll

* fix: header gap changed smooth

* feat: overflow area

* feat: memorized widnow props

* fix: lint

* fix: lint

* fix: lint

* fix: lint 😓

* fix: lint 😓
  • Loading branch information
Jambo2018 authored Mar 18, 2023
1 parent f2e0c9c commit 9e59ace
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 70 deletions.
5 changes: 3 additions & 2 deletions src-tauri/tauri.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,13 @@
"fullscreen": false,
"height": 700,
"width": 600,
"minWidth": 560,
"minWidth": 430,
"minHeight": 600,
"resizable": true,
"title": "OpenAI Translator",
"titleBarStyle": "Overlay",
"hiddenTitle": true
"hiddenTitle": true,
"visible": false
}
],
"systemTray": {
Expand Down
48 changes: 48 additions & 0 deletions src/common/hooks/useMemoWindow.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { listen, TauriEvent } from '@tauri-apps/api/event'
import { appWindow, PhysicalPosition, PhysicalSize } from '@tauri-apps/api/window'
import { useEffect, useLayoutEffect } from 'react'

export type WindowMemoProps = {
size: boolean
position: boolean
}

/**
* memorized window props
*/
export const useMemoWindow = (props: WindowMemoProps) => {
useLayoutEffect(() => {
if (props.position) {
const storagePosition = localStorage.getItem('_position')
if (storagePosition) {
const { x, y } = JSON.parse(storagePosition)
appWindow.setPosition(new PhysicalPosition(x, y))
}
} else {
localStorage.removeItem('_position')
}
if (props.size) {
const storageSize = localStorage.getItem('_size')
if (storageSize) {
const { height, width } = JSON.parse(storageSize)
appWindow.setSize(new PhysicalSize(width, height))
}
} else {
localStorage.removeItem('_size')
}
appWindow.show()
}, [])

useEffect(() => {
const unListenMove = listen(TauriEvent.WINDOW_MOVED, (event: { payload: any }) => {
localStorage.setItem('_position', JSON.stringify(event.payload))
})
const unListenResize = listen(TauriEvent.WINDOW_RESIZED, (event: { payload: any }) => {
localStorage.setItem('_size', JSON.stringify(event.payload))
})
return () => {
unListenMove.then
unListenResize.then
}
}, [])
}
72 changes: 50 additions & 22 deletions src/content_script/PopupCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,22 +61,33 @@ const useStyles = createUseStyles({
'popupCard': {
height: '100%',
},
'settingsIcon': (props: IThemedStyleProps) => ({
'footer': (props: IThemedStyleProps) => ({
color: props.theme.colors.contentSecondary,
position: 'absolute',
position: 'fixed',
width: '100%',
height: '32px',
cursor: 'pointer',
bottom: '10px',
left: '10px',
lineHeight: '1',
left: '0',
bottom: '0',
paddingLeft: '10px',
display: 'flex',
alignItems: 'center',
background: props.themeType === 'dark' ? '#1f1f1f' : '#fff',
}),
'popupCardHeaderContainer': (props: IThemedStyleProps) => ({
'position': 'fixed',
'zIndex': 1,
'left': 0,
'top': '0',
'width': '100%',
'boxSizing': 'border-box',
'padding': '30px 10px 5px',
'background': props.themeType === 'dark' ? '#1f1f1f' : '#fff',
'display': 'flex',
'flexDirection': 'row',
'cursor': 'move',
'alignItems': 'center',
'padding': '5px 10px',
'borderBottom': `1px solid ${props.theme.colors.borderTransparent}`,
'minWidth': '550px',
'-ms-user-select': 'none',
'-webkit-user-select': 'none',
'user-select': 'none',
Expand Down Expand Up @@ -138,6 +149,7 @@ const useStyles = createUseStyles({
flexShrink: 0,
},
'popupCardContentContainer': {
paddingTop: '52px',
display: 'flex',
flexDirection: 'column',
},
Expand Down Expand Up @@ -258,6 +270,20 @@ const useStyles = createUseStyles({
'OCRStatusBar': (props: IThemedStyleProps) => ({
color: props.theme.colors.contentSecondary,
}),
'@media screen and (max-width: 570px)': {
iconText: {
display: 'none',
},
},
'@media screen and (max-width: 460px)': {
popupCardHeaderActionsContainer: {
padding: '5px 0',
gap: '5px',
},
popupCardHeaderButtonGroup: {
marginLeft: '5px',
},
},
})

interface IActionStrItem {
Expand Down Expand Up @@ -842,21 +868,6 @@ export function PopupCard(props: IPopupCardProps) {
paddingBottom: showSettings ? '0px' : '30px',
}}
>
{props.showSettings && (
<StatefulTooltip
content={showSettings ? t('Go to Translator') : t('Go to Settings')}
showArrow
placement='left'
>
<div className={styles.settingsIcon} onClick={() => setShowSettings((s) => !s)}>
{showSettings ? (
<AiOutlineTranslation size='14' />
) : (
<IoSettingsOutline size='14' />
)}
</div>
</StatefulTooltip>
)}
{showSettings ? (
<Settings
onSave={(oldSettings) => {
Expand Down Expand Up @@ -1326,6 +1337,23 @@ export function PopupCard(props: IPopupCardProps) {
</div>
</div>
)}
{props.showSettings && (
<div className={styles.footer}>
<StatefulTooltip
content={showSettings ? t('Go to Translator') : t('Go to Settings')}
showArrow
placement='right'
>
<div onClick={() => setShowSettings((s) => !s)}>
{showSettings ? (
<AiOutlineTranslation size='14' />
) : (
<IoSettingsOutline size='14' />
)}
</div>
</StatefulTooltip>
</div>
)}
<Toaster />
</div>
</BaseProvider>
Expand Down
9 changes: 7 additions & 2 deletions src/popup/Settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -447,16 +447,21 @@ export function Settings(props: IPopupProps) {
return (
<div
style={{
paddingTop: '98px',
paddingBottom: '32px',
background: themeType === 'dark' ? '#1f1f1f' : '#fff',
minWidth: 400,
}}
>
<style>{formStyles}</style>
<StyletronProvider value={engine}>
<BaseProvider theme={theme}>
<nav
style={{
position: 'relative',
position: 'fixed',
left: 0,
top: 0,
zIndex: 1,
width: '100%',
display: 'flex',
flexDirection: 'row',
alignItems: 'center',
Expand Down
4 changes: 4 additions & 0 deletions src/tauri/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { listen, Event } from '@tauri-apps/api/event'
import { invoke } from '@tauri-apps/api/tauri'
import { bindHotkey } from './utils'
import { useTheme } from '../common/hooks/useTheme'
import { useMemoWindow } from '../common/hooks/useMemoWindow'

const engine = new Styletron({
prefix: '__yetone-openai-translator-styletron-',
Expand All @@ -20,6 +21,9 @@ export function App() {
const closeIconRef = useRef<HTMLDivElement>(null)
const [text, setText] = React.useState('')
const [isPinned, setPinned] = React.useState(false)

useMemoWindow({ size: true, position: true })

useEffect(() => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
invoke('get_main_window_always_on_top').then((pinned: any) => {
Expand Down
95 changes: 51 additions & 44 deletions src/tauri/index.html
Original file line number Diff line number Diff line change
@@ -1,49 +1,56 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta
name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"
/>
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<link rel="stylesheet" href="index.css" />
<script src="index.js" type="application/javascript" defer></script>
<title>OpenAI Translator</title>
<style>
body {
overscroll-behavior: none;
}
.titlebar {
height: 30px;
background: transparent;
user-select: none;
display: flex;
justify-content: flex-end;
position: fixed;
top: 0;
left: 0;
right: 0;
z-index: 2147483647;
}
.titlebar-button {
display: inline-flex;
justify-content: center;
align-items: center;
width: 30px;
height: 30px;
}
<head>
<meta charset="UTF-8" />
<meta
name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"
/>
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<link rel="stylesheet" href="index.css" />
<script src="index.js" type="application/javascript" defer></script>
<title>OpenAI Translator</title>
<style>
body {
overscroll-behavior: none;
scrollbar-width: none;
-ms-overflow-style: none;
}

body::-webkit-scrollbar {
display: none;
}

.titlebar {
height: 30px;
background: transparent;
user-select: none;
display: flex;
justify-content: flex-end;
position: fixed;
top: 0;
left: 0;
right: 0;
z-index: 2147483647;
}
.titlebar-button {
display: inline-flex;
justify-content: center;
align-items: center;
width: 30px;
height: 30px;
}
.titlebar-button:hover {
background: #e9e9e9;
}
@media (prefers-color-scheme: dark) {
.titlebar-button:hover {
background: #e9e9e9;
}
@media (prefers-color-scheme: dark) {
.titlebar-button:hover {
background: #353535;
}
background: #353535;
}
</style>
</head>
<body style="position: relative; min-height: 100vh">
<div id="root"></div>
</body>
}
</style>
</head>
<body style="position: relative; min-height: 100vh">
<div id="root"></div>
</body>
</html>

0 comments on commit 9e59ace

Please sign in to comment.