diff --git a/include/application.hpp b/include/application.hpp index 24291ac..efaa980 100644 --- a/include/application.hpp +++ b/include/application.hpp @@ -61,6 +61,7 @@ class Application void set_silent(); void socket_loop(); void daemonize(); + void init_visibility(); }; #endif diff --git a/include/tmux.hpp b/include/tmux.hpp index 5652660..0cf2d06 100644 --- a/include/tmux.hpp +++ b/include/tmux.hpp @@ -29,6 +29,7 @@ namespace tmux auto is_used() -> bool; auto is_window_focused() -> bool; + auto is_session_attached() -> bool; auto get_client_pids() -> std::optional>; diff --git a/src/application.cpp b/src/application.cpp index ceac581..af211bb 100644 --- a/src/application.cpp +++ b/src/application.cpp @@ -110,6 +110,7 @@ void Application::execute(const std::string_view cmd) return; } canvas->add_image(identifier, std::move(image)); + init_visibility(); } else if (action == "remove") { canvas->remove_image(identifier); } else { @@ -240,6 +241,14 @@ void Application::socket_loop() } } +void Application::init_visibility() { + if (tmux::is_used() && !tmux::is_window_focused()) { + canvas->hide(); + return; + } + canvas->show(); +} + void Application::print_header() { const auto log_tmp = util::get_log_filename(); diff --git a/src/canvas/x11/x11.cpp b/src/canvas/x11/x11.cpp index 5f1a04c..6c09997 100644 --- a/src/canvas/x11/x11.cpp +++ b/src/canvas/x11/x11.cpp @@ -198,7 +198,7 @@ void X11Canvas::add_image(const std::string &identifier, std::unique_ptr } windows.insert({window_id, window}); image_windows.at(identifier).insert({window_id, window}); - window->show(); + window->hide(); }); draw(identifier); diff --git a/src/tmux.cpp b/src/tmux.cpp index b919d7d..d6161e3 100644 --- a/src/tmux.cpp +++ b/src/tmux.cpp @@ -21,7 +21,6 @@ #include #include #include -#include constexpr auto session_hooks = std::to_array( {"session-window-changed", "client-detached", "window-layout-changed"}); @@ -46,6 +45,12 @@ auto tmux::is_window_focused() -> bool return os::exec(cmd) == "1,1,0"; } +auto tmux::is_session_attached() -> bool +{ + const auto cmd = fmt::format("tmux display -p -F '#{{session_attached}}' -t {}", tmux::get_pane()); + return os::exec(cmd) == "1"; +} + auto tmux::get_pane() -> std::string { return os::getenv("TMUX_PANE").value_or(""); @@ -56,12 +61,16 @@ auto tmux::get_client_pids() -> std::optional> if (!tmux::is_used()) { return {}; } - if (!tmux::is_window_focused()) { - return {}; - } std::vector pids; - const auto cmd = fmt::format("tmux list-clients -F '#{{client_pid}}' -t {}", tmux::get_pane()); + std::string cmd; + + if (is_session_attached()) { + cmd = fmt::format("tmux list-clients -F '#{{client_pid}}' -t {}", tmux::get_pane()); + } else { + cmd = fmt::format("tmux list-clients -F '#{{client_pid}}'"); + } + const auto output = os::exec(cmd); for (const auto &line : util::str_split(output, "\n")) {