Skip to content

Commit

Permalink
feat: add superprompt focus on quickopen shortcut (#252)
Browse files Browse the repository at this point in the history
  • Loading branch information
mwyywm authored Oct 5, 2023
1 parent 895bce9 commit 8619203
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ export interface ProviderInterface {
export interface Settings {
getGlobalShortcut: () => Promise<string>;
setGlobalShortcut: (shortcut: string) => Promise<boolean>;
getFocusSuperprompt: () => Promise<boolean>;
setFocusSuperprompt: (state: boolean) => Promise<boolean>;
getPlatform: () => Promise<string>;
}

Expand Down
21 changes: 21 additions & 0 deletions src/main/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,8 +339,11 @@ const quickOpenDefaultShortcut = store.get(
'CommandOrControl+Shift+G',
) as string;

const focusSuperpromptDefault = store.get('focusSuperpromptEnabled', false);

console.log(quickOpenDefaultShortcut);
console.log(isValidShortcut(quickOpenDefaultShortcut));
console.log('focus superprompt enabled', focusSuperpromptDefault);
/*
* Update the global shortcut to one provided
*/
Expand All @@ -351,6 +354,9 @@ function changeGlobalShortcut(newShortcut: string) {
globalShortcut.register(newShortcut, quickOpen);
}

function setSuperpromptFocusState(state: boolean) {
store.set('focusSuperpromptEnabled', state);
}
/*
* Open and focus the main window
*/
Expand All @@ -364,6 +370,11 @@ function quickOpen() {
}
mainWindow.show();
mainWindow.focus();
if (store.get('focusSuperpromptEnabled') === true) {
mainWindow.webContents.executeJavaScript(
`{document.querySelector('#prompt')?.focus()}`,
);
}
}
} else {
createWindow();
Expand All @@ -386,6 +397,15 @@ ipcMain.handle('set-global-shortcut', async (event, shortcut: string) => {
return true;
});

ipcMain.handle('get-focus-superprompt', () => {
return store.get('focusSuperpromptEnabled', false);
});

ipcMain.handle('set-focus-superprompt', async (_, state: boolean) => {
setSuperpromptFocusState(state);
return true;
});

app.on('ready', () => {
/*
* Register global shortcut on app ready
Expand All @@ -394,6 +414,7 @@ app.on('ready', () => {
store.set('quickOpenShortcut', quickOpenDefaultShortcut);
globalShortcut.register(quickOpenDefaultShortcut, quickOpen);
}
store.set('focusSuperpromptEnabled', focusSuperpromptDefault);

/*
* Re-register global shortcut when it is changed in settings
Expand Down
6 changes: 6 additions & 0 deletions src/main/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ contextBridge.exposeInMainWorld('settings', {
setGlobalShortcut: (shortcut: string) => {
return ipcRenderer.invoke('set-global-shortcut', shortcut);
},
getFocusSuperprompt: () => {
return ipcRenderer.invoke('get-focus-superprompt');
},
setFocusSuperprompt: (state: boolean) => {
return ipcRenderer.invoke('set-focus-superprompt', state);
},
getPlatform: () => {
return ipcRenderer.invoke('get-platform');
},
Expand Down
43 changes: 41 additions & 2 deletions src/renderer/components/settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export default function SettingsMenu({
const [isRecording, setIsRecording] = useState(false);
const [metaKey, setMetaKey] = useState('');
const [openAtLogin, setOpenAtLogin] = useState(false);
const [superpromptFocus, setSuperpromptFocus] = useState(false);

function classNames(...classes) {
return classes.filter(Boolean).join(' ');
Expand Down Expand Up @@ -81,14 +82,19 @@ export default function SettingsMenu({
if (isValidShortcut(shortcut)) fetchPlatform();
}, []);

// Set the shortcut from the main process on mount
// Set the initial super prompt focus and shortcut states from the main process on mount
useEffect(() => {
const displayShortcut = async () => {
const initialShortcut = await settings.getGlobalShortcut();
console.debug('initialShortcut', initialShortcut);
setShortcut(initialShortcut?.split('+'));
};
const setInitialSuperpromptFocus = async () => {
const initialState = await settings.getFocusSuperprompt();
setSuperpromptFocus(initialState);
};
displayShortcut();
setInitialSuperpromptFocus();
}, []);

// Whenever shortcut is updated, update it in the electron store in the main process via IPC
Expand All @@ -101,7 +107,13 @@ export default function SettingsMenu({
};
updateShortcut(validShortcut);
}, [validShortcut]);

// Toggle superprompt focus setting in electron store
useEffect(() => {
const updateSuperpromptFocus = async () => {
await settings.setFocusSuperprompt(superpromptFocus);
};
updateSuperpromptFocus();
}, [superpromptFocus]);
// Turn on key listeners when recording shortcuts
useEffect(() => {
if (isRecording && validShortcut.length === 0) {
Expand Down Expand Up @@ -171,6 +183,33 @@ export default function SettingsMenu({
/>
</Switch>
</Switch.Group>
<Switch.Group as="div" className="flex items-center justify-between">
<span className="flex flex-col flex-grow">
<Switch.Label
as="span"
className="text-sm font-medium leading-6 text-gray-900"
passive
>
Focus superprompt input on quick open shortcut
</Switch.Label>
</span>
<Switch
checked={superpromptFocus}
onChange={setSuperpromptFocus}
className={classNames(
superpromptFocus ? 'bg-indigo-600' : 'bg-gray-200',
'relative inline-flex h-6 w-11 flex-shrink-0 cursor-pointer rounded-full border-2 border-transparent transition-colors duration-200 ease-in-out focus:outline-none',
)}
>
<span
aria-hidden="true"
className={classNames(
superpromptFocus ? 'translate-x-5' : 'translate-x-0',
'pointer-events-none inline-block h-5 w-5 transform rounded-full bg-white shadow ring-0 transition duration-200 ease-in-out',
)}
/>
</Switch>
</Switch.Group>
<span className="flex flex-col flex-grow">
<div
as="span"
Expand Down

0 comments on commit 8619203

Please sign in to comment.