From 11d6997752b65b45f6c85adddea8e9c67422be76 Mon Sep 17 00:00:00 2001 From: crschnick Date: Thu, 21 Mar 2024 03:57:53 +0000 Subject: [PATCH] Refactor windows terminal classes --- .../java/io/xpipe/app/prefs/AppPrefs.java | 1 + .../app/prefs/ExternalApplicationType.java | 2 - .../io/xpipe/app/prefs/TerminalCategory.java | 1 + .../ExternalTerminalType.java | 89 +++---------------- .../app/terminal/WindowsTerminalType.java | 85 ++++++++++++++++++ .../java/io/xpipe/app/util/FileOpener.java | 2 +- .../io/xpipe/app/util/TerminalLauncher.java | 2 +- app/src/main/java/module-info.java | 1 + 8 files changed, 100 insertions(+), 83 deletions(-) rename app/src/main/java/io/xpipe/app/{prefs => terminal}/ExternalTerminalType.java (90%) create mode 100644 app/src/main/java/io/xpipe/app/terminal/WindowsTerminalType.java diff --git a/app/src/main/java/io/xpipe/app/prefs/AppPrefs.java b/app/src/main/java/io/xpipe/app/prefs/AppPrefs.java index a12315a8..84766fa3 100644 --- a/app/src/main/java/io/xpipe/app/prefs/AppPrefs.java +++ b/app/src/main/java/io/xpipe/app/prefs/AppPrefs.java @@ -10,6 +10,7 @@ import io.xpipe.app.fxcomps.Comp; import io.xpipe.app.fxcomps.util.PlatformThread; import io.xpipe.app.issue.ErrorEvent; import io.xpipe.app.storage.DataStorage; +import io.xpipe.app.terminal.ExternalTerminalType; import io.xpipe.app.util.ApplicationHelper; import io.xpipe.app.util.PasswordLockSecretValue; import io.xpipe.core.util.InPlaceSecretValue; 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 a6f6e2a5..62232138 100644 --- a/app/src/main/java/io/xpipe/app/prefs/ExternalApplicationType.java +++ b/app/src/main/java/io/xpipe/app/prefs/ExternalApplicationType.java @@ -25,8 +25,6 @@ public abstract class ExternalApplicationType implements PrefsChoiceValue { public abstract boolean isAvailable(); - public abstract boolean isSelectable(); - @Override public String getId() { return id; diff --git a/app/src/main/java/io/xpipe/app/prefs/TerminalCategory.java b/app/src/main/java/io/xpipe/app/prefs/TerminalCategory.java index 3bf51519..a95fcad7 100644 --- a/app/src/main/java/io/xpipe/app/prefs/TerminalCategory.java +++ b/app/src/main/java/io/xpipe/app/prefs/TerminalCategory.java @@ -7,6 +7,7 @@ import io.xpipe.app.fxcomps.Comp; import io.xpipe.app.fxcomps.impl.ChoiceComp; import io.xpipe.app.fxcomps.impl.StackComp; import io.xpipe.app.fxcomps.impl.TextFieldComp; +import io.xpipe.app.terminal.ExternalTerminalType; import io.xpipe.app.util.OptionsBuilder; import io.xpipe.app.util.TerminalLauncher; import io.xpipe.app.util.ThreadHelper; diff --git a/app/src/main/java/io/xpipe/app/prefs/ExternalTerminalType.java b/app/src/main/java/io/xpipe/app/terminal/ExternalTerminalType.java similarity index 90% rename from app/src/main/java/io/xpipe/app/prefs/ExternalTerminalType.java rename to app/src/main/java/io/xpipe/app/terminal/ExternalTerminalType.java index b7b04deb..8f05edbb 100644 --- a/app/src/main/java/io/xpipe/app/prefs/ExternalTerminalType.java +++ b/app/src/main/java/io/xpipe/app/terminal/ExternalTerminalType.java @@ -1,18 +1,21 @@ -package io.xpipe.app.prefs; +package io.xpipe.app.terminal; import io.xpipe.app.ext.PrefsChoiceValue; import io.xpipe.app.issue.ErrorEvent; +import io.xpipe.app.prefs.AppPrefs; +import io.xpipe.app.prefs.ExternalApplicationType; import io.xpipe.app.storage.DataStoreColor; -import io.xpipe.app.util.*; +import io.xpipe.app.util.ApplicationHelper; +import io.xpipe.app.util.LocalShell; +import io.xpipe.app.util.MacOsPermissions; +import io.xpipe.app.util.WindowsRegistry; import io.xpipe.core.process.*; -import io.xpipe.core.store.FileNames; import lombok.Getter; import lombok.Value; import lombok.With; import java.io.IOException; import java.nio.charset.StandardCharsets; -import java.nio.file.Files; import java.nio.file.Path; import java.util.*; import java.util.function.Supplier; @@ -92,69 +95,6 @@ public interface ExternalTerminalType extends PrefsChoiceValue { } }; - ExternalTerminalType WINDOWS_TERMINAL_PREVIEW = new ExternalTerminalType() { - - @Override - public boolean supportsTabs() { - return true; - } - - @Override - public void launch(LaunchConfiguration configuration) throws Exception { - // A weird behavior in Windows Terminal causes the trailing - // backslash of a filepath to escape the closing quote in the title argument - // So just remove that slash - var fixedName = FileNames.removeTrailingSlash(configuration.getColoredTitle()); - LocalShell.getShell() - .executeSimpleCommand(CommandBuilder.of() - .addFile(getPath().toString()) - .add("-w", "1", "nt", "--title") - .addQuoted(fixedName) - .addFile(configuration.getScriptFile())); - } - - private Path getPath() { - var local = System.getenv("LOCALAPPDATA"); - return Path.of(local) - .resolve("Microsoft\\WindowsApps\\Microsoft.WindowsTerminalPreview_8wekyb3d8bbwe\\wt.exe"); - } - - @Override - public boolean isAvailable() { - return Files.exists(getPath()); - } - - @Override - public String getId() { - return "app.windowsTerminalPreview"; - } - }; - - ExternalTerminalType WINDOWS_TERMINAL = new SimplePathType("app.windowsTerminal", "wt.exe") { - - @Override - public boolean supportsTabs() { - return true; - } - - @Override - protected CommandBuilder toCommand(LaunchConfiguration configuration) throws Exception { - // A weird behavior in Windows Terminal causes the trailing - // backslash of a filepath to escape the closing quote in the title argument - // So just remove that slash - var fixedName = FileNames.removeTrailingSlash(configuration.getColoredTitle()); - var toExec = !ShellDialects.isPowershell(LocalShell.getShell()) - ? CommandBuilder.of().addFile(configuration.getScriptFile()) - : CommandBuilder.of() - .add("powershell", "-ExecutionPolicy", "Bypass", "-File") - .addQuoted(configuration.getScriptFile()); - return CommandBuilder.of() - .add("-w", "1", "nt", "--title") - .addQuoted(fixedName) - .add(toExec); - } - }; - ExternalTerminalType ALACRITTY_WINDOWS = new SimplePathType("app.alacritty", "alacritty") { @Override @@ -692,8 +632,8 @@ public interface ExternalTerminalType extends PrefsChoiceValue { TABBY_WINDOWS, ALACRITTY_WINDOWS, WEZ_WINDOWS, - WINDOWS_TERMINAL_PREVIEW, - WINDOWS_TERMINAL, + WindowsTerminalType.WINDOWS_TERMINAL_PREVIEW, + WindowsTerminalType.WINDOWS_TERMINAL, CMD, PWSH, POWERSHELL); @@ -845,11 +785,6 @@ public interface ExternalTerminalType extends PrefsChoiceValue { public boolean isAvailable() { return true; } - - @Override - public boolean isSelectable() { - return true; - } } abstract class MacOsType extends ExternalApplicationType.MacApplication implements ExternalTerminalType { @@ -865,11 +800,6 @@ public interface ExternalTerminalType extends PrefsChoiceValue { public PathCheckType(String id, String executable) { super(id, executable); } - - @Override - public boolean isSelectable() { - return true; - } } @Getter @@ -887,4 +817,5 @@ public interface ExternalTerminalType extends PrefsChoiceValue { protected abstract CommandBuilder toCommand(LaunchConfiguration configuration) throws Exception; } + } diff --git a/app/src/main/java/io/xpipe/app/terminal/WindowsTerminalType.java b/app/src/main/java/io/xpipe/app/terminal/WindowsTerminalType.java new file mode 100644 index 00000000..0acf28f4 --- /dev/null +++ b/app/src/main/java/io/xpipe/app/terminal/WindowsTerminalType.java @@ -0,0 +1,85 @@ +package io.xpipe.app.terminal; + +import io.xpipe.app.util.LocalShell; +import io.xpipe.core.process.CommandBuilder; +import io.xpipe.core.process.ShellDialects; +import io.xpipe.core.store.FileNames; + +import java.nio.file.Files; +import java.nio.file.Path; + +public class WindowsTerminalType { + + + public static final ExternalTerminalType WINDOWS_TERMINAL = new ExternalTerminalType.SimplePathType("app.windowsTerminal", "wt.exe") { + + @Override + protected CommandBuilder toCommand(LaunchConfiguration configuration) throws Exception { + return WindowsTerminalType.toCommand(configuration); + } + + @Override + public boolean supportsTabs() { + return true; + } + + @Override + public boolean supportsColoredTitle() { + return false; + } + }; + + public static final ExternalTerminalType WINDOWS_TERMINAL_PREVIEW = new ExternalTerminalType() { + + @Override + public boolean supportsColoredTitle() { + return false; + } + + @Override + public boolean supportsTabs() { + return true; + } + + @Override + public void launch(ExternalTerminalType.LaunchConfiguration configuration) throws Exception { + LocalShell.getShell() + .executeSimpleCommand(CommandBuilder.of() + .addFile(getPath().toString()) + .add(toCommand(configuration))); + } + + private Path getPath() { + var local = System.getenv("LOCALAPPDATA"); + return Path.of(local) + .resolve("Microsoft\\WindowsApps\\Microsoft.WindowsTerminalPreview_8wekyb3d8bbwe\\wt.exe"); + } + + @Override + public boolean isAvailable() { + return Files.exists(getPath()); + } + + @Override + public String getId() { + return "app.windowsTerminalPreview"; + } + }; + + private static CommandBuilder toCommand(ExternalTerminalType.LaunchConfiguration configuration) throws Exception { + // A weird behavior in Windows Terminal causes the trailing + // backslash of a filepath to escape the closing quote in the title argument + // So just remove that slash + var fixedName = FileNames.removeTrailingSlash(configuration.getColoredTitle()); + + var toExec = !ShellDialects.isPowershell(LocalShell.getShell()) ? + CommandBuilder.of().addFile(configuration.getScriptFile()) : + CommandBuilder.of().add("powershell", "-ExecutionPolicy", "Bypass", "-File").addQuoted(configuration.getScriptFile()); + var cmd = CommandBuilder.of().add("-w", "1", "nt"); + + if (configuration.getColor() != null) { + cmd.add("--tabColor").addQuoted(configuration.getColor().toHexString()); + } + return cmd.add("--title").addQuoted(fixedName).add(toExec); + } +} diff --git a/app/src/main/java/io/xpipe/app/util/FileOpener.java b/app/src/main/java/io/xpipe/app/util/FileOpener.java index e71e0725..fb4bf139 100644 --- a/app/src/main/java/io/xpipe/app/util/FileOpener.java +++ b/app/src/main/java/io/xpipe/app/util/FileOpener.java @@ -45,7 +45,7 @@ public class FileOpener { public static void openInTextEditor(FileSystem.FileEntry entry) { var editor = AppPrefs.get().externalEditor().getValue(); - if (editor == null || !editor.isSelectable()) { + if (editor == null) { return; } diff --git a/app/src/main/java/io/xpipe/app/util/TerminalLauncher.java b/app/src/main/java/io/xpipe/app/util/TerminalLauncher.java index 11b2d4a0..9c99d4ca 100644 --- a/app/src/main/java/io/xpipe/app/util/TerminalLauncher.java +++ b/app/src/main/java/io/xpipe/app/util/TerminalLauncher.java @@ -3,7 +3,7 @@ package io.xpipe.app.util; import io.xpipe.app.core.AppI18n; import io.xpipe.app.issue.ErrorEvent; import io.xpipe.app.prefs.AppPrefs; -import io.xpipe.app.prefs.ExternalTerminalType; +import io.xpipe.app.terminal.ExternalTerminalType; import io.xpipe.app.storage.DataStorage; import io.xpipe.app.storage.DataStoreEntry; import io.xpipe.core.process.ProcessControl; diff --git a/app/src/main/java/module-info.java b/app/src/main/java/module-info.java index b5d542e1..5ebb43bc 100644 --- a/app/src/main/java/module-info.java +++ b/app/src/main/java/module-info.java @@ -41,6 +41,7 @@ open module io.xpipe.app { exports io.xpipe.app.browser; exports io.xpipe.app.browser.icon; exports io.xpipe.app.core.check; + exports io.xpipe.app.terminal; requires com.sun.jna; requires com.sun.jna.platform;