diff --git a/app/src/main/java/io/xpipe/app/prefs/ExternalApplicationType.java b/app/src/main/java/io/xpipe/app/prefs/ExternalApplicationType.java index 94beeabf..81efb8a9 100644 --- a/app/src/main/java/io/xpipe/app/prefs/ExternalApplicationType.java +++ b/app/src/main/java/io/xpipe/app/prefs/ExternalApplicationType.java @@ -5,6 +5,7 @@ import io.xpipe.app.issue.ErrorEvent; import io.xpipe.core.impl.LocalStore; import io.xpipe.core.process.OsType; import io.xpipe.core.process.ShellControl; +import io.xpipe.core.process.ShellDialects; import java.nio.file.Files; import java.nio.file.Path; @@ -90,13 +91,35 @@ public abstract class ExternalApplicationType implements PrefsChoiceValue { } } - public abstract static class WindowsFullPathType extends ExternalApplicationType { + public abstract static class WindowsType extends ExternalApplicationType { - public WindowsFullPathType(String id) { + private final String executable; + + public WindowsType(String id, String executable) { super(id); + this.executable = executable; } - protected abstract Optional determinePath(); + protected abstract Optional determineInstallationPath(); + + private Optional determineFromPath() { + // Try to locate if it is in the Path + try (var cc = LocalStore.getShell() + .command(ShellDialects.getPlatformDefault().getWhichCommand("code.cmd")) + .start()) { + var out = cc.readStdoutDiscardErr(); + var exit = cc.getExitCode(); + if (exit == 0) { + var first = out.lines().findFirst(); + if (first.isPresent()) { + return first.map(Path::of); + } + } + } catch (Exception ex) { + ErrorEvent.fromThrowable(ex).omit().handle(); + } + return Optional.empty(); + } @Override public boolean isSelectable() { @@ -105,7 +128,7 @@ public abstract class ExternalApplicationType implements PrefsChoiceValue { @Override public boolean isAvailable() { - var path = determinePath(); + var path = determineInstallationPath(); return path.isPresent() && Files.exists(path.get()); } } diff --git a/app/src/main/java/io/xpipe/app/prefs/ExternalEditorType.java b/app/src/main/java/io/xpipe/app/prefs/ExternalEditorType.java index 48eec746..3724e7ec 100644 --- a/app/src/main/java/io/xpipe/app/prefs/ExternalEditorType.java +++ b/app/src/main/java/io/xpipe/app/prefs/ExternalEditorType.java @@ -1,12 +1,9 @@ package io.xpipe.app.prefs; import io.xpipe.app.ext.PrefsChoiceValue; -import io.xpipe.app.issue.ErrorEvent; import io.xpipe.app.util.ApplicationHelper; import io.xpipe.app.util.WindowsRegistry; -import io.xpipe.core.impl.LocalStore; import io.xpipe.core.process.OsType; -import io.xpipe.core.process.ShellDialects; import java.io.IOException; import java.nio.file.Path; @@ -17,14 +14,14 @@ import java.util.function.Supplier; public interface ExternalEditorType extends PrefsChoiceValue { - ExternalEditorType NOTEPAD = new WindowsFullPathType("app.notepad") { + ExternalEditorType NOTEPAD = new WindowsType("app.notepad", "notepad") { @Override - protected Optional determinePath() { + protected Optional determineInstallationPath() { return Optional.of(Path.of(System.getenv("SystemRoot") + "\\System32\\notepad.exe")); } }; - ExternalEditorType VSCODE_WINDOWS = new WindowsFullPathType("app.vscode") { + ExternalEditorType VSCODE_WINDOWS = new WindowsType("app.vscode", "code.cmd") { @Override public boolean canOpenDirectory() { @@ -32,23 +29,7 @@ public interface ExternalEditorType extends PrefsChoiceValue { } @Override - protected Optional determinePath() { - // Try to locate if it is in the Path - try (var cc = LocalStore.getShell() - .command(ShellDialects.getPlatformDefault().getWhichCommand("code.cmd")) - .start()) { - var out = cc.readStdoutDiscardErr(); - var exit = cc.getExitCode(); - if (exit == 0) { - var first = out.lines().findFirst(); - if (first.isPresent()) { - return first.map(Path::of); - } - } - } catch (Exception ex) { - ErrorEvent.fromThrowable(ex).omit().handle(); - } - + protected Optional determineInstallationPath() { return Optional.of(Path.of(System.getenv("LOCALAPPDATA")) .resolve("Programs") .resolve("Microsoft VS Code") @@ -61,10 +42,10 @@ public interface ExternalEditorType extends PrefsChoiceValue { return false; } }; - ExternalEditorType NOTEPADPLUSPLUS_WINDOWS = new WindowsFullPathType("app.notepad++") { + ExternalEditorType NOTEPADPLUSPLUS_WINDOWS = new WindowsType("app.notepad++", "notepad++") { @Override - protected Optional determinePath() { + protected Optional determineInstallationPath() { Optional launcherDir; launcherDir = WindowsRegistry.readString(WindowsRegistry.HKEY_LOCAL_MACHINE, "SOFTWARE\\Notepad++", null) .map(p -> p + "\\notepad++.exe"); @@ -175,11 +156,14 @@ public interface ExternalEditorType extends PrefsChoiceValue { } } - abstract class WindowsFullPathType extends ExternalApplicationType.WindowsFullPathType + abstract class WindowsType extends ExternalApplicationType.WindowsType implements ExternalEditorType { - public WindowsFullPathType(String id) { - super(id); + private final String executable; + + public WindowsType(String id, String executable) { + super(id, executable); + this.executable = executable; } public boolean detach() { @@ -188,7 +172,7 @@ public interface ExternalEditorType extends PrefsChoiceValue { @Override public void launch(Path file) throws Exception { - var path = determinePath(); + var path = determineInstallationPath(); if (path.isEmpty()) { throw new IOException("Unable to find installation of " + toTranslatedString()); } diff --git a/app/src/main/java/io/xpipe/app/prefs/ExternalTerminalType.java b/app/src/main/java/io/xpipe/app/prefs/ExternalTerminalType.java index 9774d753..a46b679f 100644 --- a/app/src/main/java/io/xpipe/app/prefs/ExternalTerminalType.java +++ b/app/src/main/java/io/xpipe/app/prefs/ExternalTerminalType.java @@ -99,16 +99,16 @@ public interface ExternalTerminalType extends PrefsChoiceValue { return OsType.getLocal().equals(OsType.WINDOWS); } }; - abstract class WindowsFullPathType extends ExternalApplicationType.WindowsFullPathType + abstract class WindowsType extends ExternalApplicationType.WindowsType implements ExternalTerminalType { - public WindowsFullPathType(String id) { - super(id); + public WindowsType(String id, String executable) { + super(id, executable); } @Override public void launch(String name, String file, boolean elevated) throws Exception { - var path = determinePath(); + var path = determineInstallationPath(); if (path.isEmpty()) { throw new IOException("Unable to find installation of " + toTranslatedString()); } @@ -120,7 +120,7 @@ public interface ExternalTerminalType extends PrefsChoiceValue { protected abstract String createCommand(ShellControl shellControl, String name, String path, String file); } - ExternalTerminalType TABBY_WINDOWS = new WindowsFullPathType("app.tabbyWindows") { + ExternalTerminalType TABBY_WINDOWS = new WindowsType("app.tabbyWindows", "tabby") { @Override protected String createCommand(ShellControl shellControl, String name, String path, String file) { @@ -129,7 +129,7 @@ public interface ExternalTerminalType extends PrefsChoiceValue { } @Override - protected Optional determinePath() { + protected Optional determineInstallationPath() { Optional launcherDir; launcherDir = WindowsRegistry.readString( WindowsRegistry.HKEY_CURRENT_USER,