Skip to content

Updates #35

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
17 changes: 17 additions & 0 deletions src/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,23 @@ declare global {
openExternal: (url: string) => Promise<void>
choosePluginsDir: () => Promise<string>
getPluginsDir: () => Promise<string>
getPlugins: () => Promise<PluginT[]>
setDisabledPlugins: (pluginName: string, isDisabled: boolean) => Promise<void>
getDisabledPlugins: () => Promise<string[]>
getHotkeys: () => Promise<{ [key: string]: string }>
setHotkey: (type: string, hotkey: string) => Promise<void>
showMainWindow: () => Promise<void>
hideMainWindow: () => Promise<void>
reloadApp: () => Promise<void>
}

type PluginT = {
name: string
label: string
version: string
author: string
}

type ApplicationT = {
command: string
isImmediate: boolean
Expand Down Expand Up @@ -62,6 +72,13 @@ declare global {
icon: string
label: (query: string) => JSX.Element
name: string
bang?: BangT
}

export type BangT = {
bang: string
name: string
url: string
}

type ResultT = {
Expand Down
69 changes: 68 additions & 1 deletion src/main/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,20 @@ const DEPS = {
*/
export const getCommands = async () => {
const currentPluginsDir = await getPluginsDir()
const plugins = fs.readdirSync(currentPluginsDir).filter((plugin) => plugin !== '.git')
const disabledPlugins = await getDisabledPlugins()
const plugins = fs
.readdirSync(currentPluginsDir)
.filter((plugin) => plugin !== '.git')
.filter((plugin) => {
const pluginPath = path.join(currentPluginsDir, plugin)
return fs.statSync(pluginPath).isDirectory()
})

return plugins.flatMap((plugin) => {
if (disabledPlugins.includes(plugin)) {
return []
}

const manifestPath = path.join(currentPluginsDir, plugin, 'manifest.yml')
const manifest = yaml.load(fs.readFileSync(manifestPath, 'utf8')) as ManifestT

Expand Down Expand Up @@ -289,6 +300,35 @@ export const getPluginsDir = (): Promise<string> => {
})
}

export const getPlugins = async (): Promise<PluginT[]> => {
const pluginsDir = await getPluginsDir()
const plugins = fs
.readdirSync(pluginsDir)
.filter((plugin) => plugin !== '.git')
.filter((plugin) => {
const pluginPath = path.join(pluginsDir, plugin)
return fs.statSync(pluginPath).isDirectory()
})

return plugins
.map((plugin) => {
try {
const manifestPath = path.join(pluginsDir, plugin, 'manifest.yml')
const manifest = yaml.load(fs.readFileSync(manifestPath, 'utf8')) as ManifestT
return {
name: plugin,
label: manifest.label,
version: manifest.version,
author: manifest.author
}
} catch (error) {
console.warn(`Failed to load plugin ${plugin}:`, error)
return null
}
})
.filter((plugin): plugin is PluginT => plugin !== null)
}

/**
* Sets the directory where plugins are stored.
* @param newPath the path to the plugins directory to set.
Expand Down Expand Up @@ -335,3 +375,30 @@ export const setHotkey = (type: string, hotkey: string): Promise<void> => {
})
})
}

export const setDisabledPlugins = (pluginName: string, isDisabled: boolean): Promise<void> => {
return new Promise((resolve, reject) => {
getDisabledPlugins()
.then((disabledPlugins) => {
const updatedPlugins = isDisabled
? [...disabledPlugins, pluginName]
: disabledPlugins.filter((n) => n !== pluginName)
storage.set('disabledPlugins', updatedPlugins, (error) => {
if (error) reject(error)
else resolve()
})
})
.catch(reject)
})
}

export const getDisabledPlugins = (): Promise<string[]> => {
return new Promise((resolve) => {
storage.get('disabledPlugins', (error, data) => {
if (error) throw error
// Ensure we always return an array, even if data is null, undefined, or not an array
const disabledPlugins = Array.isArray(data) ? data : []
resolve(disabledPlugins)
})
})
}
15 changes: 15 additions & 0 deletions src/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@ import icon from '../../resources/icon.png?asset'
import {
choosePluginsDir,
getCommands,
getDisabledPlugins,
getHotkeys,
getPluginActions,
getPluginsDir,
getPlugins,
listInstalledApplications,
openApplication,
openExternal,
runCommand,
runPluginAction,
setDisabledPlugins,
setHotkey
} from './handlers'
import { setupAutoUpdater } from './autoUpdater'
Expand Down Expand Up @@ -156,6 +159,18 @@ if (!gotTheLock) {
return getPluginsDir()
})

ipcMain.handle('get-plugins', async () => {
return getPlugins()
})

ipcMain.handle('get-disabled-plugins', async () => {
return getDisabledPlugins()
})

ipcMain.handle('set-disabled-plugins', async (_, pluginName, isDisabled) => {
return setDisabledPlugins(pluginName, isDisabled)
})

ipcMain.handle('set-hotkey', async (_, type, hotkey) => {
return setHotkey(type, hotkey)
})
Expand Down
9 changes: 9 additions & 0 deletions src/preload/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,21 @@ if (process.contextIsolated) {
getPluginsDir: () => {
return ipcRenderer.invoke('get-plugins-dir')
},
getPlugins: () => {
return ipcRenderer.invoke('get-plugins')
},
getHotkeys: () => {
return ipcRenderer.invoke('get-hotkeys')
},
setHotkey: (type, hotkey) => {
return ipcRenderer.invoke('set-hotkey', type, hotkey)
},
setDisabledPlugins: (pluginName, isDisabled) => {
return ipcRenderer.invoke('set-disabled-plugins', pluginName, isDisabled)
},
getDisabledPlugins: () => {
return ipcRenderer.invoke('get-disabled-plugins')
},
showMainWindow: () => {
return ipcRenderer.send('show-main-window')
},
Expand Down
10 changes: 7 additions & 3 deletions src/renderer/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ const App = () => {
const [selectedCommand, setSelectedCommand] = useState<CommandT | null>(null)
const [commandSearch, setCommandSearch] = useState('')
const commandListRef = useRef<HTMLDivElement | null>(null)
const [currentBangName, setCurrentBangName] = useState<string | null>(null)

useScrollToTop(commandListRef, [commandSearch])

const handleInputKeyDown = (e: KeyboardEvent<HTMLInputElement>) => {
if (e.key === 'Escape') {
e.preventDefault()
Expand Down Expand Up @@ -44,15 +44,19 @@ const App = () => {
onKeyDown={handleInputKeyDown}
placeholder="Search commands..."
/>

{currentBangName && (
<span className="ml-2 px-2 py-0.5 rounded-full bg-gradient-to-r from-zinc-700 to-zinc-900 text-zinc-100 text-xs font-semibold shadow-sm border border-zinc-700">
{currentBangName}
</span>
)}
<Settings />
</div>

<CommandList ref={commandListRef}>
<CommandEmpty />
<Commands commandSearch={commandSearch} setSelectedCommand={setSelectedCommand} />
<CommandApplications commandSearch={commandSearch} />
<CommandShortcuts commandSearch={commandSearch} />
<CommandShortcuts commandSearch={commandSearch} setCurrentBang={setCurrentBangName} />
</CommandList>

<Footer>
Expand Down
67 changes: 54 additions & 13 deletions src/renderer/src/components/CommandShortcuts.tsx
Original file line number Diff line number Diff line change
@@ -1,32 +1,73 @@
import { CommandGroup, CommandItem } from '@renderer/elements/Command'
import { SHORTCUTS, winElectron } from '@renderer/lib/utils'
import { BANGS, SHORTCUTS, winElectron } from '@renderer/lib/utils'

type CommandShortcutsProps = { commandSearch: string }
type CommandShortcutsProps = {
commandSearch: string
setCurrentBang: (bangName: string | null) => void
}

function parseBangAndQuery(
commandSearch: string,
setCurrentBang: (bangName: string | null) => void
): { bangPart: string | null; query: string } {
if (!commandSearch.startsWith('!')) {
return { bangPart: null, query: commandSearch }
}
let i = 1
let bangPart = ''
while (i < commandSearch.length && commandSearch[i] !== ' ') {
bangPart += commandSearch[i]
i++
}
let query = ''
if (i < commandSearch.length) {
query = commandSearch.slice(i).trim()
}
setCurrentBang(BANGS.find((bang) => bang.bang === bangPart)?.name || null)
return { bangPart, query }
}

export const CommandShortcuts = ({ commandSearch, setCurrentBang }: CommandShortcutsProps) => {
const { bangPart, query } = parseBangAndQuery(commandSearch, setCurrentBang)

export const CommandShortcuts = ({ commandSearch }: CommandShortcutsProps) => {
const onSelect = (shortcut: ShortcutT) => {
if (winElectron && winElectron.openExternal) {
const url = shortcut.getUrl(commandSearch)
const handleSelect = (shortcut: ShortcutT) => {
if (winElectron?.openExternal) {
const url = shortcut.getUrl(query)
winElectron.openExternal(url)
winElectron.hideMainWindow()
winElectron.hideMainWindow?.()
}
}

const filteredShortcuts = (() => {
if (bangPart) {
return SHORTCUTS.filter((shortcut) => shortcut.bang?.bang === bangPart)
}
return SHORTCUTS
})()

if (filteredShortcuts.length === 0) return null

return (
<CommandGroup heading="Shortcuts">
{SHORTCUTS.map((shortcut) => (
<CommandItem key={shortcut.name} onSelect={() => onSelect(shortcut)} value={shortcut.name}>
{filteredShortcuts.map((shortcut) => (
<CommandItem
key={shortcut.name}
onSelect={() => handleSelect(shortcut)}
value={shortcut.name}
>
<div className="flex flex-1 items-center justify-between">
<div className="flex gap-2 items-center">
<div className="flex gap-2 items-center min-w-0">
<div
className="flex items-center justify-center h-5 w-5 rounded-sm"
className="flex items-center justify-center h-5 w-5 rounded-sm shrink-0"
style={{ backgroundColor: shortcut.bgColor, color: shortcut.color }}
aria-label={shortcut.bang?.name || shortcut.name}
title={shortcut.bang?.name || shortcut.name}
>
<i className={`ph ph-${shortcut.icon}`} />
</div>
<span>{shortcut.label(commandSearch)}</span>
<span className="truncate">{shortcut.label(query)}</span>
</div>
<span className="text-xs text-zinc-300">Shortcut</span>
<span className="text-xs text-zinc-300 ml-2">Shortcut</span>
</div>
</CommandItem>
))}
Expand Down
66 changes: 66 additions & 0 deletions src/renderer/src/components/Settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ export const Settings = () => {
const [open, setOpen] = useState(false)
const [activeTab, setActiveTab] = useState('hotkeys')
const [pluginsDir, setPluginsDir] = useState<string | null>(null)
const [plugins, setPlugins] = useState<PluginT[]>([])
const [disabledPlugins, setDisabledPluginsState] = useState<string[]>([])

useEffect(() => {
const fetchPluginsDir = async () => {
Expand All @@ -47,6 +49,18 @@ export const Settings = () => {

fetchPluginsDir()

const fetchPlugins = async () => {
const plugins = await winElectron.getPlugins()
setPlugins(plugins)
}
fetchPlugins()

const fetchDisabledPlugins = async () => {
const disabledPlugins = await winElectron.getDisabledPlugins()
setDisabledPluginsState(disabledPlugins)
}
fetchDisabledPlugins()

winElectron.ipcRenderer.send('enable-global-shortcuts')
}, [open])

Expand All @@ -61,6 +75,19 @@ export const Settings = () => {
if (!state) await winElectron.reloadApp()
}

const handleDisablePluginChange = async (plugin: PluginT, isDisabled: boolean) => {
try {
await winElectron.setDisabledPlugins(plugin.name, isDisabled)
setDisabledPluginsState((prev) => {
const newDisabled = isDisabled
? [...prev, plugin.name]
: prev.filter((n) => n !== plugin.name)
return newDisabled
})
} catch (error) {
console.error('Failed to update plugin state:', error)
}
}
const hasAlreadyPluginsDir = typeof pluginsDir === 'string'

return (
Expand Down Expand Up @@ -124,6 +151,45 @@ export const Settings = () => {
value={hasAlreadyPluginsDir ? pluginsDir : ''}
/>
</div>
<div className="flex flex-col gap-2 mt-3">
<Label>Installed plugins ({plugins.length})</Label>
{plugins.length === 0 ? (
<div className="text-sm text-muted-foreground">
No plugins found. Select a plugins directory above.
</div>
) : (
<div className="space-y-1.5">
{plugins.map((plugin) => {
const isDisabled = disabledPlugins.includes(plugin.name)
return (
<div
key={plugin.name}
className="flex items-center justify-between rounded-md border p-2 hover:bg-accent/50 transition-colors"
>
<div className="flex flex-col gap-0.5">
<div className="font-medium text-foreground text-sm">
{plugin.label}
</div>
<div className="text-xs text-muted-foreground">
v{plugin.version} β€’ by {plugin.author}
</div>
</div>
<button
className={`ml-4 px-4 py-1.5 rounded-lg text-xs font-semibold transition-all duration-200 shadow-sm border ${
isDisabled
? 'bg-emerald-900/10 text-emerald-600 border-emerald-800/20 hover:bg-emerald-900/20 hover:border-emerald-800/30 hover:shadow-md'
: 'bg-red-900/10 text-red-600 border-red-800/20 hover:bg-red-900/20 hover:border-red-800/30 hover:shadow-md'
}`}
onClick={() => handleDisablePluginChange(plugin, !isDisabled)}
>
{isDisabled ? 'Enable' : 'Disable'}
</button>
</div>
)
})}
</div>
)}
</div>
</div>
)}

Expand Down
Loading