From 18409aa6469f380329cbf7e6995b78cc02989c5a Mon Sep 17 00:00:00 2001 From: crschnick Date: Fri, 8 Nov 2024 08:30:30 +0000 Subject: [PATCH] Focus terminal after askpass --- .../app/beacon/impl/AskpassExchangeImpl.java | 22 ++++++++++++ .../io/xpipe/app/terminal/TerminalView.java | 35 ++++++++++++++----- .../io/xpipe/beacon/api/AskpassExchange.java | 2 ++ lang/app/strings/translations_da.properties | 2 +- lang/app/strings/translations_de.properties | 2 +- lang/app/strings/translations_en.properties | 2 +- lang/app/strings/translations_es.properties | 2 +- lang/app/strings/translations_fr.properties | 2 +- lang/app/strings/translations_it.properties | 2 +- lang/app/strings/translations_ja.properties | 2 +- lang/app/strings/translations_nl.properties | 2 +- lang/app/strings/translations_pt.properties | 2 +- lang/app/strings/translations_ru.properties | 2 +- lang/app/strings/translations_tr.properties | 2 +- lang/app/strings/translations_zh.properties | 2 +- 15 files changed, 62 insertions(+), 21 deletions(-) diff --git a/app/src/main/java/io/xpipe/app/beacon/impl/AskpassExchangeImpl.java b/app/src/main/java/io/xpipe/app/beacon/impl/AskpassExchangeImpl.java index b19b14a5a..dc46a58fc 100644 --- a/app/src/main/java/io/xpipe/app/beacon/impl/AskpassExchangeImpl.java +++ b/app/src/main/java/io/xpipe/app/beacon/impl/AskpassExchangeImpl.java @@ -1,5 +1,6 @@ package io.xpipe.app.beacon.impl; +import io.xpipe.app.terminal.TerminalView; import io.xpipe.app.util.AskpassAlert; import io.xpipe.app.util.SecretManager; import io.xpipe.app.util.SecretQueryState; @@ -8,6 +9,8 @@ import io.xpipe.beacon.api.AskpassExchange; import com.sun.net.httpserver.HttpExchange; +import java.util.UUID; + public class AskpassExchangeImpl extends AskpassExchange { @Override @@ -34,9 +37,28 @@ public class AskpassExchangeImpl extends AskpassExchange { if (p.getState() != SecretQueryState.NORMAL) { throw new BeaconClientException(SecretQueryState.toErrorMessage(p.getState())); } + focusTerminalIfNeeded(msg.getPid()); return Response.builder().value(secret.inPlace()).build(); } + private void focusTerminalIfNeeded(long pid) { + if (!TerminalView.isSupported()) { + return; + } + + var found = TerminalView.get().findSession(pid); + if (found.isEmpty()) { + return; + } + + var term = TerminalView.get().getTerminalInstances().stream().filter(instance -> instance.getTerminalProcess().equals(found.get().getTerminal())).findFirst(); + if (term.isEmpty()) { + return; + } + + term.get().frontOfMainWindow(); + } + @Override public boolean requiresEnabledApi() { return false; 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 7265bd2da..8d5ab5515 100644 --- a/app/src/main/java/io/xpipe/app/terminal/TerminalView.java +++ b/app/src/main/java/io/xpipe/app/terminal/TerminalView.java @@ -6,6 +6,7 @@ import io.xpipe.app.prefs.AppPrefs; import io.xpipe.app.util.ThreadHelper; import io.xpipe.core.process.OsType; +import lombok.Getter; import lombok.Value; import java.util.ArrayList; @@ -41,6 +42,14 @@ public class TerminalView { private final List terminalInstances = new ArrayList<>(); private final List listeners = new ArrayList<>(); + public synchronized List getSessions() { + return new ArrayList<>(sessions); + } + + public synchronized List getTerminalInstances() { + return new ArrayList<>(terminalInstances); + } + public synchronized void addListener(Listener listener) { this.listeners.add(listener); } @@ -49,10 +58,6 @@ public class TerminalView { this.listeners.remove(listener); } - public boolean isEnabled() { - return isSupported(); - } - public synchronized void open(UUID request, long pid) { var processHandle = ProcessHandle.of(pid); if (processHandle.isEmpty() || !processHandle.get().isAlive()) { @@ -88,12 +93,7 @@ public class TerminalView { TrackEvent.withTrace("Terminal instance opened") .tag("terminalPid", terminal.get().pid()) - .tag("viewEnabled", isEnabled()) .handle(); - - if (!isEnabled()) { - return; - } } private Optional getTerminalProcess(ProcessHandle shell) { @@ -110,6 +110,23 @@ public class TerminalView { return current; } + public synchronized Optional findSession(long pid) { + var proc = ProcessHandle.of(pid); + while (true) { + if (proc.isEmpty()) { + return Optional.empty(); + } + + var finalProc = proc; + var found = TerminalView.get().getSessions().stream().filter(session -> session.getShell().equals(finalProc.get())).findFirst(); + if (found.isPresent()) { + return found; + } + + proc = proc.get().parent(); + } + } + public synchronized void tick() { for (Session session : new ArrayList<>(sessions)) { var alive = session.shell.isAlive() && session.terminal.isAlive(); diff --git a/beacon/src/main/java/io/xpipe/beacon/api/AskpassExchange.java b/beacon/src/main/java/io/xpipe/beacon/api/AskpassExchange.java index eb7587ef1..b53442408 100644 --- a/beacon/src/main/java/io/xpipe/beacon/api/AskpassExchange.java +++ b/beacon/src/main/java/io/xpipe/beacon/api/AskpassExchange.java @@ -26,6 +26,8 @@ public class AskpassExchange extends BeaconInterface { @Builder @Value public static class Request { + long pid; + UUID secretId; UUID request; diff --git a/lang/app/strings/translations_da.properties b/lang/app/strings/translations_da.properties index a9464c348..3353ea9c0 100644 --- a/lang/app/strings/translations_da.properties +++ b/lang/app/strings/translations_da.properties @@ -454,7 +454,7 @@ lockVaultOnHibernation=Lås Vault når computeren går i dvale lockVaultOnHibernationDescription=Når denne funktion er aktiveret, låses Vaulten automatisk, når computeren sættes i dvale. Når du bruger computeren igen, skal du indtaste din vault-adgangsætning igen. #custom overview=Oversigt -history=Browsing-historik +history=Historie skipAll=Spring alle over notes=Bemærkninger addNotes=Tilføj noter diff --git a/lang/app/strings/translations_de.properties b/lang/app/strings/translations_de.properties index f87e39a26..d972031b9 100644 --- a/lang/app/strings/translations_de.properties +++ b/lang/app/strings/translations_de.properties @@ -448,7 +448,7 @@ copyPassword=copyPassword lockVaultOnHibernation=Tresor im Ruhezustand des Computers sperren lockVaultOnHibernationDescription=Wenn diese Funktion aktiviert ist, wird der Tresor automatisch gesperrt, sobald dein Computer in den Ruhezustand versetzt wird. Nach dem Aufwachen musst du deine Tresor-Passphrase erneut eingeben. overview=Übersicht -history=Browsing-Verlauf +history=Geschichte skipAll=Alles überspringen notes=Anmerkungen addNotes=Notizen hinzufügen diff --git a/lang/app/strings/translations_en.properties b/lang/app/strings/translations_en.properties index d3d3b3953..167bb5b11 100644 --- a/lang/app/strings/translations_en.properties +++ b/lang/app/strings/translations_en.properties @@ -451,7 +451,7 @@ copyPassword=copyPassword lockVaultOnHibernation=Lock vault on computer hibernation lockVaultOnHibernationDescription=When enabled, the vault will automatically be locked once your computer is put into hibernation/to sleep. Upon wake up, you will have to enter your vault passphrase again. overview=Overview -history=Browsing history +history=History skipAll=Skip all notes=Notes addNotes=Add notes diff --git a/lang/app/strings/translations_es.properties b/lang/app/strings/translations_es.properties index 991a03684..03263d242 100644 --- a/lang/app/strings/translations_es.properties +++ b/lang/app/strings/translations_es.properties @@ -435,7 +435,7 @@ copyPassword=copiarContraseña lockVaultOnHibernation=Bloquear la bóveda al hibernar el ordenador lockVaultOnHibernationDescription=Si está activada, el almacén se bloqueará automáticamente cuando tu ordenador entre en hibernación/reposo. Al despertarte, tendrás que volver a introducir la contraseña de tu bóveda. overview=Visión general -history=Historial de navegación +history=Historia skipAll=Saltar todo notes=Notas addNotes=Añadir notas diff --git a/lang/app/strings/translations_fr.properties b/lang/app/strings/translations_fr.properties index daf805e97..fe8d623b6 100644 --- a/lang/app/strings/translations_fr.properties +++ b/lang/app/strings/translations_fr.properties @@ -435,7 +435,7 @@ copyPassword=copierMotdepasse lockVaultOnHibernation=Verrouille le coffre-fort lors de l'hibernation de l'ordinateur lockVaultOnHibernationDescription=Lorsque cette option est activée, le coffre-fort sera automatiquement verrouillé une fois que ton ordinateur sera mis en hibernation/en veille. Au réveil, tu devras saisir à nouveau la phrase de passe de ton coffre-fort. overview=Vue d'ensemble -history=Historique de navigation +history=Histoire skipAll=Sauter tout notes=Notes addNotes=Ajouter des notes diff --git a/lang/app/strings/translations_it.properties b/lang/app/strings/translations_it.properties index d2a207807..74c52e131 100644 --- a/lang/app/strings/translations_it.properties +++ b/lang/app/strings/translations_it.properties @@ -435,7 +435,7 @@ copyPassword=copiaPassword lockVaultOnHibernation=Blocca il caveau durante l'ibernazione del computer lockVaultOnHibernationDescription=Se abilitato, il vault si blocca automaticamente quando il computer viene messo in ibernazione o a riposo. Al risveglio, dovrai inserire nuovamente la passphrase del vault. overview=Panoramica -history=Cronologia di navigazione +history=La storia skipAll=Salta tutto notes=Note addNotes=Aggiungi note diff --git a/lang/app/strings/translations_ja.properties b/lang/app/strings/translations_ja.properties index 44b4a26f4..37a2a56a0 100644 --- a/lang/app/strings/translations_ja.properties +++ b/lang/app/strings/translations_ja.properties @@ -435,7 +435,7 @@ copyPassword=コピーパスワード lockVaultOnHibernation=コンピュータのハイバネーション時に保管庫をロックする lockVaultOnHibernationDescription=有効にすると、コンピュータが休止状態/スリープ状態になると、保管庫は自動的にロックされる。スリープ解除後、保管庫のパスフレーズを再度入力する必要がある。 overview=概要 -history=閲覧履歴 +history=歴史 skipAll=すべてスキップする notes=備考 addNotes=メモを追加する diff --git a/lang/app/strings/translations_nl.properties b/lang/app/strings/translations_nl.properties index 93761578b..fe4e6dfcb 100644 --- a/lang/app/strings/translations_nl.properties +++ b/lang/app/strings/translations_nl.properties @@ -435,7 +435,7 @@ copyPassword=kopieerwachtwoord lockVaultOnHibernation=Kluis op computer in slaapstand lockVaultOnHibernationDescription=Als deze optie is ingeschakeld, wordt de kluis automatisch vergrendeld zodra je computer in de slaapstand wordt gezet. Als je wakker wordt, moet je je wachtwoordzin voor de kluis opnieuw invoeren. overview=Overzicht -history=Browsegeschiedenis +history=Geschiedenis skipAll=Alles overslaan notes=Opmerkingen addNotes=Opmerkingen toevoegen diff --git a/lang/app/strings/translations_pt.properties b/lang/app/strings/translations_pt.properties index 4db664e67..a71878af2 100644 --- a/lang/app/strings/translations_pt.properties +++ b/lang/app/strings/translations_pt.properties @@ -435,7 +435,7 @@ copyPassword=copia a palavra-passe lockVaultOnHibernation=Bloqueia o cofre na hibernação do computador lockVaultOnHibernationDescription=Quando ativado, a abóbada é automaticamente bloqueada quando o computador é colocado em hibernação/sono. Quando acordares, terás de introduzir novamente a frase-chave do cofre. overview=Resumo -history=Histórico de navegação +history=História skipAll=Salta tudo notes=Nota addNotes=Adiciona notas diff --git a/lang/app/strings/translations_ru.properties b/lang/app/strings/translations_ru.properties index f264ad2ba..a46521d2c 100644 --- a/lang/app/strings/translations_ru.properties +++ b/lang/app/strings/translations_ru.properties @@ -435,7 +435,7 @@ copyPassword=copyPassword lockVaultOnHibernation=Блокировка хранилища при спящем режиме компьютера lockVaultOnHibernationDescription=Если эта функция включена, хранилище будет автоматически блокироваться, как только компьютер перейдет в спящий режим. После пробуждения тебе придется снова ввести кодовую фразу хранилища. overview=Обзор -history=История просмотров +history=История skipAll=Пропустить все notes=Заметки addNotes=Добавляй заметки diff --git a/lang/app/strings/translations_tr.properties b/lang/app/strings/translations_tr.properties index a189ce0d6..1a28b4e22 100644 --- a/lang/app/strings/translations_tr.properties +++ b/lang/app/strings/translations_tr.properties @@ -436,7 +436,7 @@ copyPassword=copyPassword lockVaultOnHibernation=Bilgisayar hazırda bekletme modunda kasayı kilitleme lockVaultOnHibernationDescription=Etkinleştirildiğinde, bilgisayarınız hazırda bekletme/uyku moduna geçtiğinde kasa otomatik olarak kilitlenecektir. Uyandığınızda, kasa parolanızı tekrar girmeniz gerekecektir. overview=Genel Bakış -history=Tarama geçmişi +history=Tarih skipAll=Tümünü atla notes=Notlar addNotes=Notlar ekleyin diff --git a/lang/app/strings/translations_zh.properties b/lang/app/strings/translations_zh.properties index 7e0236aed..c933c55c8 100644 --- a/lang/app/strings/translations_zh.properties +++ b/lang/app/strings/translations_zh.properties @@ -435,7 +435,7 @@ copyPassword=复制密码 lockVaultOnHibernation=电脑休眠时锁定保险库 lockVaultOnHibernationDescription=启用后,一旦电脑进入休眠/睡眠状态,保管库就会自动锁定。唤醒后,您必须再次输入保险库密码。 overview=概述 -history=浏览历史 +history=历史 skipAll=全部跳过 notes=说明 addNotes=添加注释