Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix X11 and Wayland - Images opened in unfocussed Tmux window overlaid on top of active window/session #215

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/application.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class Application
void set_silent();
void socket_loop();
void daemonize();
void init_visibility();
};

#endif
1 change: 1 addition & 0 deletions include/tmux.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::vector<int>>;

Expand Down
9 changes: 9 additions & 0 deletions src/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/canvas/x11/x11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ void X11Canvas::add_image(const std::string &identifier, std::unique_ptr<Image>
}
windows.insert({window_id, window});
image_windows.at(identifier).insert({window_id, window});
window->show();
window->hide();
});

draw(identifier);
Expand Down
19 changes: 14 additions & 5 deletions src/tmux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
#include <array>
#include <fmt/format.h>
#include <string>
#include <utility>

constexpr auto session_hooks = std::to_array<std::string_view>(
{"session-window-changed", "client-detached", "window-layout-changed"});
Expand All @@ -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("");
Expand All @@ -56,12 +61,16 @@ auto tmux::get_client_pids() -> std::optional<std::vector<int>>
if (!tmux::is_used()) {
return {};
}
if (!tmux::is_window_focused()) {
return {};
}

std::vector<int> 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")) {
Expand Down