-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f8be673
commit 313375f
Showing
14 changed files
with
365 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
const initialApp = Application('System Events').applicationProcesses.where({ | ||
frontmost: true, | ||
}) | ||
|
||
const initialName = initialApp.name().toString() | ||
|
||
while (true) { | ||
const currentApp = Application('System Events').applicationProcesses.where({ | ||
frontmost: true, | ||
}) | ||
const currentAppName = currentApp.name().toString() | ||
if (initialName !== currentAppName) { | ||
console.log( | ||
JSON.stringify({ | ||
name: currentAppName, | ||
displayedName: currentApp.displayedName(), | ||
}) | ||
) | ||
break | ||
} | ||
delay(0.5) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
const initialApp = Application('System Events').applicationProcesses.where({ | ||
frontmost: true, | ||
}) | ||
|
||
const initialName = initialApp.name().toString() | ||
|
||
while (true) { | ||
const currentApp = Application('System Events').applicationProcesses.where({ | ||
frontmost: true, | ||
}) | ||
const currentAppName = currentApp.name().toString() | ||
if (initialName !== currentAppName) { | ||
console.log( | ||
JSON.stringify({ | ||
name: currentAppName, | ||
displayedName: currentApp.displayedName(), | ||
}) | ||
) | ||
break | ||
} | ||
delay(0.5) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
const apps = Application('System Events').applicationProcesses() | ||
|
||
console.log( | ||
JSON.stringify( | ||
Object.values(apps) | ||
.filter((x) => !x.backgroundOnly()) | ||
.map((x) => ({ | ||
id: x.id(), | ||
name: x.name(), | ||
displayedName: x.displayedName(), | ||
})) | ||
) | ||
) |
117 changes: 57 additions & 60 deletions
117
src/components/Plugins/ApplicationManager/ApplicationManager.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
135 changes: 135 additions & 0 deletions
135
src/components/Plugins/ApplicationManager/ApplicationSelector.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
import React, { ChangeEvent, EventHandler, useEffect, useState } from 'react' | ||
import { | ||
Box, | ||
Button, | ||
FormField, | ||
Layer, | ||
Paragraph, | ||
TextInput, | ||
TextInputProps, | ||
} from 'grommet' | ||
import { PluginProps } from '../index' | ||
import { FormClose, Target } from 'grommet-icons' | ||
|
||
interface Suggestion { | ||
label: string | ||
value: string | ||
} | ||
|
||
export type ApplicationType = 'open' | 'close' | ||
|
||
interface ApplicationSelectorProps { | ||
type: ApplicationType | ||
suggestions: Suggestion[] | ||
} | ||
|
||
const ApplicationSelector: React.FC<PluginProps & ApplicationSelectorProps> = ({ | ||
type, | ||
config, | ||
updatePluginConfig, | ||
suggestions, | ||
}) => { | ||
const pluginConfig: ApplicationManagerConfig = | ||
config.plugins['application-manager'] | ||
const [showModal, setShowModal] = useState<boolean>(false) | ||
const [selection, setSelection] = useState<string>('') | ||
const [runningApps, setRunningApps] = useState< | ||
{ | ||
label: string | ||
value: string | ||
}[] | ||
>(suggestions) | ||
|
||
useEffect(() => { | ||
if (!suggestions) return | ||
setRunningApps(suggestions) | ||
}, [suggestions]) | ||
|
||
const addApp = async (app: Application): Promise<void> => { | ||
if (pluginConfig[type].some((x) => x.name === app.name)) return | ||
|
||
return updatePluginConfig('application-manager', { | ||
...pluginConfig, | ||
[type]: [...pluginConfig[type], app], | ||
}) | ||
} | ||
|
||
const onRemoveAppClick = async (appName: string) => { | ||
const index = pluginConfig[type].findIndex((app) => app.name === appName) | ||
pluginConfig[type].splice(index, 1) | ||
await updatePluginConfig('application-manager', { | ||
...pluginConfig, | ||
}) | ||
} | ||
|
||
const selectApplication = async () => { | ||
setShowModal(true) | ||
const app = await window.electron.selectApplication() | ||
await addApp(app) | ||
setShowModal(false) | ||
} | ||
|
||
const onSuggestionSelect: TextInputProps['onSuggestionSelect'] = async (e: { | ||
suggestion: Suggestion | ||
}) => { | ||
setSelection('') | ||
await addApp({ | ||
name: e.suggestion.value, | ||
displayedName: e.suggestion.label, | ||
}) | ||
} | ||
|
||
const onAppChange: EventHandler<ChangeEvent<HTMLInputElement>> = (e) => { | ||
const nextValue = e.target.value | ||
setSelection(nextValue) | ||
if (!nextValue) { | ||
console.log('no next value, resetting suggestions') | ||
setRunningApps(suggestions) | ||
} else { | ||
const regexp = new RegExp(`^${nextValue}`, 'i') | ||
setRunningApps(suggestions.filter((s) => regexp.test(s.label))) | ||
} | ||
} | ||
|
||
return ( | ||
<Box margin={{ bottom: 'large' }}> | ||
{showModal && ( | ||
<Layer> | ||
<Box pad="large"> | ||
<Paragraph> | ||
Select the application you'd like by focusing its window. | ||
</Paragraph> | ||
</Box> | ||
</Layer> | ||
)} | ||
<FormField label={type === 'close' ? 'Close' : 'Open'}> | ||
<Box direction="row"> | ||
<Button icon={<Target />} onClick={selectApplication} /> | ||
<TextInput | ||
defaultSuggestion={0} | ||
value={selection} | ||
suggestions={runningApps} | ||
onSuggestionSelect={onSuggestionSelect} | ||
onChange={onAppChange} | ||
plain | ||
/> | ||
</Box> | ||
</FormField> | ||
<Box direction="row" wrap> | ||
{pluginConfig[type].map((app) => ( | ||
<Button | ||
onClick={() => onRemoveAppClick(app.name)} | ||
key={app.name} | ||
icon={<FormClose />} | ||
label={app.displayedName ?? app.name} | ||
size="small" | ||
margin="xsmall" | ||
primary | ||
/> | ||
))} | ||
</Box> | ||
</Box> | ||
) | ||
} | ||
|
||
export default ApplicationSelector |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.