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

Keyboard shortcut support #234 #454

Closed

Conversation

subin-chella
Copy link
Collaborator

@subin-chella subin-chella commented Oct 15, 2024

fixes #234

I have added keyboard shortcuts for some functionalities. We can continue to add new shortcuts if the current approach is good and approved.

Screenshot 2024-10-16 010253
Screenshot 2024-10-16 010204

recording-2024-10-16-013326_R0w2mvyd.mp4

- Implement shortcut definitions with OS-specific display values
- Create useShortcuts hook for managing shortcuts and their descriptions
- Update IconsSidebar to use dynamic shortcut descriptions
- Integrate with electron to detect OS and handle shortcut actions fixes reorproject#234
@subin-chella subin-chella changed the title Keyboard shortcut #234 Keyboard shortcut support #234 Oct 15, 2024
Copy link

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR Summary

This pull request implements keyboard shortcuts for various functionalities in the Reor application, addressing issue #234.

  • Added shortcutDefinitions.ts to define cross-platform keyboard shortcuts for common actions
  • Implemented shortcutManager.ts to handle shortcut registration and input matching in Electron
  • Created use-shortcut.ts custom React hook for managing shortcut actions and descriptions
  • Modified windowManager.ts to return the created BrowserWindow instance, enabling shortcut implementation
  • Updated index.ts to register global shortcuts when windows load and unregister them on app quit
  • Integrated shortcuts into MainPage.tsx and IconsSidebar.tsx components for improved user experience

7 file(s) reviewed, 12 comment(s)
Edit PR Review Bot Settings | Greptile

electron/main/common/windowManager.ts Show resolved Hide resolved
@@ -14,6 +14,7 @@ import registerFileHandlers from './filesystem/ipcHandlers'
import { ollamaService, registerLLMSessionHandlers } from './llm/ipcHandlers'
import registerPathHandlers from './path/ipcHandlers'
import { registerDBSessionHandlers } from './vector-database/ipcHandlers'
import { registerGlobalShortcuts, unregisterGlobalShortcuts } from '../../src/components/shortcuts/shortcutManager'
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: Consider using an absolute import path instead of a relative one for better maintainability

Comment on lines +55 to 58
app.on('will-quit', () => {
const windows = BrowserWindow.getAllWindows()
windows.forEach((window) => unregisterGlobalShortcuts(window))
})
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: Consider moving this logic to a separate function for better organization

@@ -16,6 +16,7 @@ import { FileProvider, useFileContext } from '@/contexts/FileContext'
import ModalProvider from '@/contexts/ModalContext'
import CustomContextMenu from './Common/CustomContextMenu'
import CommonModals from './Common/CommonModals'
import useShortcuts from './shortcuts/use-shortcut'
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: Consider adding a comment explaining the purpose of this import and its impact on the component.

@@ -24,6 +25,7 @@ const MainPageContent: React.FC = () => {

const { showChatbot } = useChatContext()

useShortcuts()
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: Consider moving this hook call to a more appropriate lifecycle stage if it's causing any unnecessary re-renders.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed this unwanted hook call

src/components/Sidebars/IconsSidebar.tsx Show resolved Hide resolved
import { BrowserWindow } from 'electron'
import { shortcuts } from './shortcutDefinitions'

function isShortcutMatch(input: Electron.Input, shortcutKey: string): boolean {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: isShortcutMatch function doesn't handle the case where mainKey is undefined

Comment on lines +25 to +34
export function registerGlobalShortcuts(mainWindow: BrowserWindow) {
mainWindow.webContents.on('before-input-event', (event, input) => {
shortcuts.forEach((shortcut) => {
if (input.type === 'keyDown' && isShortcutMatch(input, shortcut.key)) {
event.preventDefault()
mainWindow.webContents.send(shortcut.action)
}
})
})
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: registerGlobalShortcuts doesn't return a way to unregister individual shortcuts

src/components/shortcuts/shortcutManager.ts Show resolved Hide resolved
@joseplayero
Copy link
Collaborator

thanks for working on this. i like the overall readability of the code.

i have to qs on implementation:

  1. why do we need to register shortcuts in the main process and send them to renderer? it will simplify the implementation to just have keyboard shortcuts in the renderer process- we could just have a key press handler in the top level app.tsx file?
  2. why not use a library like react-hotkeys-hook?

@subin-chella
Copy link
Collaborator Author

@joseplayero I have attempted to fix the issue as per your comment and have created a new PR. I also encountered some challenges, which I have detailed in the PR. Please review.
#460

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Keyboard shortcut
2 participants