From eb2f0d07309e87a9bc6274809c51cf22d96b3256 Mon Sep 17 00:00:00 2001 From: crschnick Date: Sat, 9 Nov 2024 07:05:28 +0000 Subject: [PATCH] Terminal fixes --- app/src/main/java/io/xpipe/app/core/AppTheme.java | 10 +++++++++- .../java/io/xpipe/app/core/mode/PlatformMode.java | 1 + app/src/main/java/io/xpipe/app/ext/LocalStore.java | 4 ---- app/src/main/java/io/xpipe/app/ext/ShellStore.java | 4 +++- .../java/io/xpipe/app/terminal/CmdTerminalType.java | 5 ++++- .../io/xpipe/app/terminal/ExternalTerminalType.java | 1 - .../xpipe/app/terminal/PowerShellTerminalType.java | 8 ++++++++ .../java/io/xpipe/app/terminal/PwshTerminalType.java | 3 +++ .../io/xpipe/app/terminal/TerminalLaunchRequest.java | 4 +++- .../java/io/xpipe/app/terminal/TerminalLauncher.java | 3 ++- .../xpipe/app/terminal/TerminalLauncherManager.java | 2 +- .../java/io/xpipe/app/terminal/TerminalView.java | 12 ++++++++++++ .../java/io/xpipe/app/terminal/WezTerminalType.java | 2 +- .../io/xpipe/app/terminal/WindowsTerminalType.java | 7 +++++++ 14 files changed, 54 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/io/xpipe/app/core/AppTheme.java b/app/src/main/java/io/xpipe/app/core/AppTheme.java index c3cdddb3f..344f31f12 100644 --- a/app/src/main/java/io/xpipe/app/core/AppTheme.java +++ b/app/src/main/java/io/xpipe/app/core/AppTheme.java @@ -129,13 +129,21 @@ public class AppTheme { TrackEvent.debug("Set theme " + t.getId() + " for scene"); AppPrefs.get().theme.addListener((c, o, n) -> { - AppCache.update("lastDarkTheme", n != null && n.isDark()); changeTheme(n); }); init = true; } + public static void reset() { + if (!init) { + return; + } + + var nowDark = Platform.getPreferences().getColorScheme() == ColorScheme.DARK; + AppCache.update("lastDarkTheme", nowDark); + } + private static void setDefault() { try { var colorScheme = Platform.getPreferences().getColorScheme(); diff --git a/app/src/main/java/io/xpipe/app/core/mode/PlatformMode.java b/app/src/main/java/io/xpipe/app/core/mode/PlatformMode.java index 4fae784bb..ac669019c 100644 --- a/app/src/main/java/io/xpipe/app/core/mode/PlatformMode.java +++ b/app/src/main/java/io/xpipe/app/core/mode/PlatformMode.java @@ -71,6 +71,7 @@ public abstract class PlatformMode extends OperationMode { onSwitchFrom(); StoreViewState.reset(); AppLayoutModel.reset(); + AppTheme.reset(); PlatformState.teardown(); TrackEvent.info("Platform shutdown finished"); BACKGROUND.finalTeardown(); diff --git a/app/src/main/java/io/xpipe/app/ext/LocalStore.java b/app/src/main/java/io/xpipe/app/ext/LocalStore.java index dd4d4d98d..3e8b3a217 100644 --- a/app/src/main/java/io/xpipe/app/ext/LocalStore.java +++ b/app/src/main/java/io/xpipe/app/ext/LocalStore.java @@ -18,10 +18,6 @@ public class LocalStore extends JacksonizedValue return ShellStoreState.class; } - public ShellControl control(ShellControl parent) { - return parent; - } - @Override public ShellControlFunction shellFunction() { return new ShellControlFunction() { diff --git a/app/src/main/java/io/xpipe/app/ext/ShellStore.java b/app/src/main/java/io/xpipe/app/ext/ShellStore.java index a8b763ca2..5745304fc 100644 --- a/app/src/main/java/io/xpipe/app/ext/ShellStore.java +++ b/app/src/main/java/io/xpipe/app/ext/ShellStore.java @@ -69,6 +69,8 @@ public interface ShellStore extends DataStore, FileSystemStore, ValidatableStore return func.control(); } - return p.control(p.getParentStore().getOrStartSession()); + // Don't reuse local shell + var parentSc = p.getParentStore() instanceof LocalStore l ? l.standaloneControl() : p.getParentStore().getOrStartSession(); + return p.control(parentSc); } } diff --git a/app/src/main/java/io/xpipe/app/terminal/CmdTerminalType.java b/app/src/main/java/io/xpipe/app/terminal/CmdTerminalType.java index a62662bb6..7cf9eec85 100644 --- a/app/src/main/java/io/xpipe/app/terminal/CmdTerminalType.java +++ b/app/src/main/java/io/xpipe/app/terminal/CmdTerminalType.java @@ -1,5 +1,7 @@ package io.xpipe.app.terminal; +import io.xpipe.app.ext.ProcessControlProvider; +import io.xpipe.app.prefs.AppPrefs; import io.xpipe.core.process.CommandBuilder; import io.xpipe.core.process.ShellDialects; @@ -11,7 +13,8 @@ public class CmdTerminalType extends ExternalTerminalType.SimplePathType impleme @Override public int getProcessHierarchyOffset() { - return -1; + var powershell = ShellDialects.isPowershell(ProcessControlProvider.get().getEffectiveLocalDialect()) || AppPrefs.get().enableTerminalLogging().get(); + return powershell ? 0 : -1; } @Override diff --git a/app/src/main/java/io/xpipe/app/terminal/ExternalTerminalType.java b/app/src/main/java/io/xpipe/app/terminal/ExternalTerminalType.java index 9ac30d3a9..e0c2b9e29 100644 --- a/app/src/main/java/io/xpipe/app/terminal/ExternalTerminalType.java +++ b/app/src/main/java/io/xpipe/app/terminal/ExternalTerminalType.java @@ -943,7 +943,6 @@ public interface ExternalTerminalType extends PrefsChoiceValue { WindowsTerminalType.WINDOWS_TERMINAL_CANARY, WindowsTerminalType.WINDOWS_TERMINAL_PREVIEW, WindowsTerminalType.WINDOWS_TERMINAL, - TabbyTerminalType.TABBY_WINDOWS, AlacrittyTerminalType.ALACRITTY_WINDOWS, WezTerminalType.WEZTERM_WINDOWS, CMD, diff --git a/app/src/main/java/io/xpipe/app/terminal/PowerShellTerminalType.java b/app/src/main/java/io/xpipe/app/terminal/PowerShellTerminalType.java index d94ad965d..cbcd1094f 100644 --- a/app/src/main/java/io/xpipe/app/terminal/PowerShellTerminalType.java +++ b/app/src/main/java/io/xpipe/app/terminal/PowerShellTerminalType.java @@ -1,5 +1,7 @@ package io.xpipe.app.terminal; +import io.xpipe.app.ext.ProcessControlProvider; +import io.xpipe.app.prefs.AppPrefs; import io.xpipe.core.process.CommandBuilder; import io.xpipe.core.process.ShellDialects; @@ -12,6 +14,12 @@ public class PowerShellTerminalType extends ExternalTerminalType.SimplePathType super("app.powershell", "powershell", true); } + @Override + public int getProcessHierarchyOffset() { + var powershell = ProcessControlProvider.get().getEffectiveLocalDialect() == POWERSHELL || AppPrefs.get().enableTerminalLogging().get(); + return powershell ? -1 : 0; + } + @Override public boolean supportsTabs() { return false; diff --git a/app/src/main/java/io/xpipe/app/terminal/PwshTerminalType.java b/app/src/main/java/io/xpipe/app/terminal/PwshTerminalType.java index dbe253dbc..dfcf87c68 100644 --- a/app/src/main/java/io/xpipe/app/terminal/PwshTerminalType.java +++ b/app/src/main/java/io/xpipe/app/terminal/PwshTerminalType.java @@ -1,6 +1,9 @@ package io.xpipe.app.terminal; +import io.xpipe.app.ext.ProcessControlProvider; +import io.xpipe.app.prefs.AppPrefs; import io.xpipe.core.process.CommandBuilder; +import io.xpipe.core.process.ShellDialects; import java.nio.charset.StandardCharsets; import java.util.Base64; diff --git a/app/src/main/java/io/xpipe/app/terminal/TerminalLaunchRequest.java b/app/src/main/java/io/xpipe/app/terminal/TerminalLaunchRequest.java index bfb228e85..be89ca65c 100644 --- a/app/src/main/java/io/xpipe/app/terminal/TerminalLaunchRequest.java +++ b/app/src/main/java/io/xpipe/app/terminal/TerminalLaunchRequest.java @@ -30,7 +30,8 @@ public class TerminalLaunchRequest { @NonFinal boolean setupCompleted; - CountDownLatch latch = new CountDownLatch(1); + @NonFinal + CountDownLatch latch; public Path waitForCompletion() throws BeaconServerException { while (true) { @@ -54,6 +55,7 @@ public class TerminalLaunchRequest { } public void setupRequestAsync() { + latch = new CountDownLatch(1); ThreadHelper.runAsync(() -> { setupRequest(); latch.countDown(); diff --git a/app/src/main/java/io/xpipe/app/terminal/TerminalLauncher.java b/app/src/main/java/io/xpipe/app/terminal/TerminalLauncher.java index d43a618d1..eb8c144ab 100644 --- a/app/src/main/java/io/xpipe/app/terminal/TerminalLauncher.java +++ b/app/src/main/java/io/xpipe/app/terminal/TerminalLauncher.java @@ -117,7 +117,8 @@ public class TerminalLauncher { var logFile = logDir.resolve(new FilePath(DataStorage.get().getStoreEntryDisplayName(entry) + " (" + DATE_FORMATTER.format(Instant.now()) + ").log") .fileSystemCompatible(OsType.getLocal()) - .toString()); + .toString() + .replaceAll(" ", "_")); try (var sc = LocalShell.getShell().start()) { if (OsType.getLocal() == OsType.WINDOWS) { var content = diff --git a/app/src/main/java/io/xpipe/app/terminal/TerminalLauncherManager.java b/app/src/main/java/io/xpipe/app/terminal/TerminalLauncherManager.java index d2758ed77..6a0774567 100644 --- a/app/src/main/java/io/xpipe/app/terminal/TerminalLauncherManager.java +++ b/app/src/main/java/io/xpipe/app/terminal/TerminalLauncherManager.java @@ -51,7 +51,7 @@ public class TerminalLauncherManager { synchronized (entries) { var req = entries.get(request); if (req == null) { - req = new TerminalLaunchRequest(request, processControl, config, directory, null, false); + req = new TerminalLaunchRequest(request, processControl, config, directory, null, false, null); entries.put(request, req); } else { req.setResult(null); diff --git a/app/src/main/java/io/xpipe/app/terminal/TerminalView.java b/app/src/main/java/io/xpipe/app/terminal/TerminalView.java index 419797164..a2bc1bbb4 100644 --- a/app/src/main/java/io/xpipe/app/terminal/TerminalView.java +++ b/app/src/main/java/io/xpipe/app/terminal/TerminalView.java @@ -1,11 +1,13 @@ package io.xpipe.app.terminal; import io.xpipe.app.core.window.NativeWinWindowControl; +import io.xpipe.app.ext.ProcessControlProvider; import io.xpipe.app.issue.TrackEvent; import io.xpipe.app.prefs.AppPrefs; import io.xpipe.app.util.ThreadHelper; import io.xpipe.core.process.OsType; +import io.xpipe.core.process.ShellDialects; import lombok.Getter; import lombok.Value; @@ -79,11 +81,18 @@ public class TerminalView { } var shell = processHandle.get().parent(); + TrackEvent.withTrace("Shell session opened") + .tag("pid", shell.map(p -> p.pid()).orElse(-1L)) + .handle(); if (shell.isEmpty()) { return; } var terminal = getTerminalProcess(shell.get()); + TrackEvent.withTrace("Terminal session opened") + .tag("pid", terminal.map(p -> p.pid()).orElse(-1L)) + .tag("exec", terminal.flatMap(p -> p.info().command()).orElse("?")) + .handle(); if (terminal.isEmpty()) { return; } @@ -131,9 +140,12 @@ public class TerminalView { return Optional.empty(); } + // Adjust for terminal logging script setup var off = trackableTerminalType.getProcessHierarchyOffset(); if (AppPrefs.get().enableTerminalLogging().get() && OsType.getLocal() != OsType.WINDOWS) { off += 2; + } else if (AppPrefs.get().enableTerminalLogging().get() && OsType.getLocal() == OsType.WINDOWS) { + off += ShellDialects.isPowershell(ProcessControlProvider.get().getEffectiveLocalDialect()) ? 0 : 1; } var current = Optional.of(shell); diff --git a/app/src/main/java/io/xpipe/app/terminal/WezTerminalType.java b/app/src/main/java/io/xpipe/app/terminal/WezTerminalType.java index 436aa899f..cf00e4337 100644 --- a/app/src/main/java/io/xpipe/app/terminal/WezTerminalType.java +++ b/app/src/main/java/io/xpipe/app/terminal/WezTerminalType.java @@ -12,7 +12,7 @@ import io.xpipe.core.process.ShellControl; import java.nio.file.Path; import java.util.Optional; -public interface WezTerminalType extends ExternalTerminalType { +public interface WezTerminalType extends ExternalTerminalType, TrackableTerminalType { ExternalTerminalType WEZTERM_WINDOWS = new Windows(); ExternalTerminalType WEZTERM_LINUX = new Linux(); diff --git a/app/src/main/java/io/xpipe/app/terminal/WindowsTerminalType.java b/app/src/main/java/io/xpipe/app/terminal/WindowsTerminalType.java index b9647405a..ab5193872 100644 --- a/app/src/main/java/io/xpipe/app/terminal/WindowsTerminalType.java +++ b/app/src/main/java/io/xpipe/app/terminal/WindowsTerminalType.java @@ -2,6 +2,7 @@ package io.xpipe.app.terminal; import io.xpipe.app.ext.ProcessControlProvider; import io.xpipe.app.issue.ErrorEvent; +import io.xpipe.app.prefs.AppPrefs; import io.xpipe.app.util.LocalShell; import io.xpipe.app.util.ScriptHelper; import io.xpipe.core.process.CommandBuilder; @@ -55,6 +56,12 @@ public interface WindowsTerminalType extends ExternalTerminalType, TrackableTerm return cmd; } + @Override + default int getProcessHierarchyOffset() { + var powershell = AppPrefs.get().enableTerminalLogging().get() && !ShellDialects.isPowershell(ProcessControlProvider.get().getEffectiveLocalDialect()); + return powershell ? 1 : 0; + } + @Override default boolean supportsTabs() { return true;