Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refs #5097 Add context menu #5121

Merged
merged 2 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 71 additions & 1 deletion main/background.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import path from 'path'
import { app, ipcMain, shell, IpcMainInvokeEvent, BrowserWindow, Menu } from 'electron'
import { app, ipcMain, shell, IpcMainInvokeEvent, BrowserWindow, Menu, clipboard } from 'electron'
import serve from 'electron-serve'
import { createWindow } from './helpers'
import { menu } from './menu'
Expand Down Expand Up @@ -39,6 +39,76 @@ let main: BrowserWindow = null
await mainWindow.loadURL(`http://localhost:${port}/`)
mainWindow.webContents.openDevTools()
}

mainWindow.webContents.on('context-menu', (_event, properties) => {
const { editFlags } = properties
const hasText = properties.selectionText.length > 0
const can = (type: string) => editFlags[`can${type}`] && hasText
const contextMenu = Menu.buildFromTemplate([
{
label: 'Select All',
click: () => {
mainWindow.webContents.selectAll()
}
},
{
label: 'Copy',
enabled: can('Copy'),
visible: properties.isEditable || hasText,
click: () => {
const target = mainWindow.webContents
if (target) {
target.copy()
} else {
clipboard.writeText(properties.selectionText)
}
}
},
{
label: 'Cut',
enabled: can('Cut'),
visible: properties.isEditable || hasText,
click: () => {
const target = mainWindow.webContents
if (target) {
target.cut()
} else {
clipboard.writeText(properties.selectionText)
}
}
},
{
label: 'Paste',
enabled: editFlags.canPaste,
visible: properties.isEditable,
click: () => {
const target = mainWindow.webContents
if (target) {
target.paste()
}
}
},
{
label: 'Save Image As',
visible: properties.mediaType === 'image',
click: () => {
console.log(properties.srcURL)
mainWindow.webContents.downloadURL(properties.srcURL)
}
},
{
label: 'Copy Link',
visible: properties.linkURL.length > 0 && properties.mediaType === 'none',
click: () => {
clipboard.write({
bookmark: properties.linkText,
text: properties.linkURL
})
}
}
])
contextMenu.popup({ window: mainWindow })
})
})()

app.on('window-all-closed', () => {
Expand Down
20 changes: 9 additions & 11 deletions renderer/components/timelines/FollowRequests.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,15 @@ export default function FollowRequests(props: Props) {
</div>
<div className="timeline overflow-y-auto w-full overflow-x-hidden" style={{ height: 'calc(100% - 44px)' }}>
{requests.map(r => (
<>
<User
key={r.id}
user={r}
client={props.client}
refresh={async () => {
const data = await refreshRequests()
updateUnreads(data.length)
}}
/>
</>
<User
key={r.id}
user={r}
client={props.client}
refresh={async () => {
const data = await refreshRequests()
updateUnreads(data.length)
}}
/>
))}
</div>
</section>
Expand Down
30 changes: 1 addition & 29 deletions renderer/components/timelines/status/Status.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,29 +88,6 @@ export default function Status(props: Props) {
)
}

const onContextMenu: MouseEventHandler<HTMLDivElement> = e => {
e.preventDefault()
hideOthers()
const context = document.getElementById(`context-${props.status.id}`)
if (context) {
context.style.left = `${e.clientX}px`
context.style.top = `${e.clientY}px`
context.style.display = 'block'
}
}

const onClick: MouseEventHandler<HTMLDivElement> = e => {
e.preventDefault()
hideOthers()
}

const hideOthers = () => {
const menu = document.getElementsByClassName('context-menu')
for (let i = 0; i < menu.length; i++) {
;(menu[i] as HTMLElement).style.display = 'none'
}
}

const copyLink = () => {
navigator.clipboard.writeText(status.url)
}
Expand Down Expand Up @@ -141,12 +118,7 @@ export default function Status(props: Props) {
)}
/>
</div>
<div
className="text-gray-950 dark:text-gray-300 break-all overflow-hidden"
style={{ width: 'calc(100% - 56px)' }}
onContextMenu={onContextMenu}
onClick={onClick}
>
<div className="text-gray-950 dark:text-gray-300 break-all overflow-hidden" style={{ width: 'calc(100% - 56px)' }}>
<div className="flex justify-between">
<div className="flex cursor-pointer hover:underline" onClick={() => openUser(status.account.id)}>
<span
Expand Down
Loading