Skip to content

Commit

Permalink
feat: enhance the Open With VSCode feature
Browse files Browse the repository at this point in the history
Signed-off-by: The1111mp <The1111mp@outlook.com>
  • Loading branch information
1111mp committed Nov 10, 2024
1 parent ac8f42a commit 25dc18b
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 22 deletions.
1 change: 1 addition & 0 deletions @types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ declare global {

interface Setting {
closer: Closer;
coder: string;
directory: string;
enable_silent_start?: boolean;
locale: string;
Expand Down
7 changes: 1 addition & 6 deletions src-tauri/src/cmds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,12 +160,7 @@ pub async fn configration_import(
/// open project with VsCode
#[tauri::command]
pub async fn open_with_vscode(path: String) -> CmdResult<()> {
#[cfg(windows)]
let cmd = "code.cmd";

#[cfg(unix)]
let cmd = "code";

let cmd = { Config::settings().latest().coder.clone() }.unwrap();
wrap_err!(Command::new(cmd).arg(&path).status())?;
Ok(())
}
Expand Down
12 changes: 12 additions & 0 deletions src-tauri/src/config/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ pub struct ISettings {
/// value: `minimize` or `close`
pub closer: Option<String>,

/// open the project file using the code editor
/// designed by default for VSCode's 'code' command
#[serde(default = "default_coder")]
pub coder: Option<String>,

/// installation directory
pub directory: Option<String>,

Expand All @@ -36,6 +41,10 @@ pub struct ISettings {
pub theme: Option<String>,
}

fn default_coder() -> Option<String> {
Some("code".to_string())
}

impl ISettings {
pub fn new() -> Self {
match dirs::settings_path().and_then(|path| help::read_json::<Self>(&path)) {
Expand All @@ -50,6 +59,8 @@ impl ISettings {
/// return the default settings config
pub fn template() -> Self {
Self {
closer: Some("minimize".into()),
coder: Some("code".into()),
directory: Some(dirs::default_install_dir().to_string_lossy().to_string()),
enable_silent_start: Some(false),
locale: Some("en".into()),
Expand Down Expand Up @@ -107,6 +118,7 @@ impl ISettings {
}

patch!(closer);
patch!(coder);
patch!(directory);
patch!(enable_silent_start);
patch!(locale);
Expand Down
11 changes: 2 additions & 9 deletions src/app-context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -125,17 +125,10 @@ export function AppProvider({

dispatch({
type: Actions.UpdateSetting,
payload: { ...settings, ...setting },
payload: { ...setting },
});
},
[
settings.locale,
settings.theme,
settings.closer,
settings.directory,
settings.mirror,
settings.proxy,
]
[]
);

const setColorHandler = useMemo(
Expand Down
6 changes: 3 additions & 3 deletions src/components/ui/tooltip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ const TooltipContent = forwardRef<
ref={ref}
sideOffset={sideOffset}
className={cn(
'z-50 overflow-hidden rounded-md bg-primary px-3 py-1.5 text-sm text-primary-foreground animate-in fade-in-0 zoom-in-95 data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=closed]:zoom-out-95 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2',
className
)}
"z-50 overflow-hidden rounded-md border bg-popover px-3 py-1.5 text-sm text-popover-foreground shadow-md animate-in fade-in-0 zoom-in-95 data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=closed]:zoom-out-95 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2",
className
)}
{...props}
/>
));
Expand Down
2 changes: 2 additions & 0 deletions src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -136,5 +136,7 @@
"Unzipping": "Unzipping",
"Set-as-default": "Set as the default version",
"open-with-vscode": "Open with VsCode",
"VSCode-Code-Command": "VSCode Code Command",
"VSCode-Code-Command-tip": "In most cases the default value of 'code' is sufficient. However, on MacOS you must fill in the full path, for example: '/usr/local/bin/code' ( you can get it by running 'where code' )",
"VSCode-code-command-not-found": "VSCode 'code' command not found"
}
2 changes: 2 additions & 0 deletions src/locales/zh_CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -136,5 +136,7 @@
"Unzipping": "解压中",
"Set-as-default": "设置为默认版本",
"open-with-vscode": "使用 VsCode 打开",
"VSCode-Code-Command": "VSCode Code 命令",
"VSCode-Code-Command-tip": "绝大部分情况该值为默认的'code'即可。但是在 MacOS 上你必须填完整的路径,比如:‘/usr/local/bin/code’(可以通过运行‘where code’获得)",
"VSCode-code-command-not-found": "未找到 VSCode 的‘code’命令"
}
3 changes: 1 addition & 2 deletions src/pages/home/configration.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -297,13 +297,12 @@ const ConfigrationImport = forwardRef<Importer, {}>(({}, ref) => {
const data = await configrationImport(sync);
if (!data) return;

console.log(data);
const { color, mirrors, setting } = data;
toast.success(t('Configration-import-success'), { duration: 5000 });
color && setColor(color);
mirrors && localStorage.setItem('nvmd-mirror', mirrors);
setting && onUpdateSetting(setting);
setOpen(false);
toast.success(t('Configration-import-success'), { duration: 5000 });
} catch (err) {
toast.error(err?.message || err.toString());
}
Expand Down
29 changes: 28 additions & 1 deletion src/pages/home/setting.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ const formSchema = z.object({
locale: z.string(),
theme: z.nativeEnum(Themes),
closer: z.nativeEnum(Closer),
coder: z.string(),
directory: z.string().min(1),
mirror: z.string().url({ message: 'Invalid mirror url' }),
proxy: z
Expand Down Expand Up @@ -117,6 +118,7 @@ const Setting: React.FC<Props> = memo(() => {
locale: newLocale,
theme: newTheme,
closer: newCloser,
coder: newCoder,
directory: newDirectory,
mirror: newMirror,
proxy: newProxy,
Expand All @@ -125,6 +127,7 @@ const Setting: React.FC<Props> = memo(() => {
settings.locale === newLocale &&
settings.theme === newTheme &&
settings.closer === newCloser &&
settings.coder === newCoder &&
settings.directory === newDirectory &&
settings.mirror === newMirror &&
compareObject(settings.proxy, newProxy)
Expand Down Expand Up @@ -153,6 +156,7 @@ const Setting: React.FC<Props> = memo(() => {
locale: newLocale,
theme: newTheme,
closer: newCloser,
coder: newCoder,
directory: newDirectory,
mirror: newMirror,
proxy: newProxy,
Expand Down Expand Up @@ -181,7 +185,7 @@ const Setting: React.FC<Props> = memo(() => {
icon={<GearIcon />}
/>
</SheetTrigger>
<SheetContent className='flex flex-col'>
<SheetContent className='flex flex-col [overflow-y:overlay]'>
<SheetHeader>
<SheetTitle>{t('Setting')}</SheetTitle>
<SheetDescription></SheetDescription>
Expand Down Expand Up @@ -306,6 +310,29 @@ const Setting: React.FC<Props> = memo(() => {
</FormItem>
)}
/>
<FormField
control={form.control}
name='coder'
render={({ field }) => (
<FormItem>
<FormLabel className='flex items-center gap-1 text-muted-foreground'>
{t('VSCode-Code-Command')}
<Tooltip>
<TooltipTrigger asChild>
<InfoCircledIcon className='text-primary cursor-pointer' />
</TooltipTrigger>
<TooltipContent className='w-96 text-accent-foreground bg-accent'>
{t('VSCode-Code-Command-tip')}
</TooltipContent>
</Tooltip>
</FormLabel>
<FormControl>
<Input className='h-8' {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name='directory'
Expand Down
2 changes: 1 addition & 1 deletion src/pages/projects/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ export const Component: React.FC = () => {
try {
await openWithVSCode(path);
} catch {
toast.warning(t('VSCode-code-command-not-found'));
toast.error(t('VSCode-code-command-not-found'));
}
}}
/>
Expand Down

0 comments on commit 25dc18b

Please sign in to comment.