code review

pull/2592/head
zjeffer 2023-10-21 18:15:22 +02:00
parent 7576611782
commit 2d614c68f5
2 changed files with 25 additions and 27 deletions

View File

@ -26,13 +26,13 @@ namespace waybar::modules::hyprland {
class Workspaces;
class WorkspaceWindow {
class WindowCreationPayload {
public:
WorkspaceWindow(std::string workspace_name, WindowAddress window_address,
WindowCreationPayload(std::string workspace_name, WindowAddress window_address,
std::string window_repr);
WorkspaceWindow(std::string workspace_name, WindowAddress window_address,
WindowCreationPayload(std::string workspace_name, WindowAddress window_address,
std::string window_class, std::string window_title);
WorkspaceWindow(Json::Value const& client_data);
WindowCreationPayload(Json::Value const& client_data);
int increment_time_spent_uncreated();
bool is_empty(Workspaces& workspace_manager);
@ -83,11 +83,11 @@ class Workspace {
void set_windows(uint value) { windows_ = value; };
void set_name(std::string const& value) { name_ = value; };
bool contains_window(WindowAddress const& addr) const { return window_map_.contains(addr); }
void insert_window(WorkspaceWindow create_window_paylod);
void insert_window(WindowCreationPayload create_window_paylod);
std::string remove_window(WindowAddress const& addr);
void initialize_window_map(const Json::Value& clients_data);
bool on_window_opened(WorkspaceWindow const& create_window_paylod);
bool on_window_opened(WindowCreationPayload const& create_window_paylod);
std::optional<std::string> on_window_closed(WindowAddress const& addr);
void update(const std::string& format, const std::string& icon);
@ -178,7 +178,7 @@ class Workspaces : public AModule, public EventHandler {
std::vector<std::unique_ptr<Workspace>> workspaces_;
std::vector<Json::Value> workspaces_to_create_;
std::vector<std::string> workspaces_to_remove_;
std::vector<WorkspaceWindow> windows_to_create_;
std::vector<WindowCreationPayload> windows_to_create_;
std::vector<std::regex> ignore_workspaces_;

View File

@ -14,10 +14,6 @@
namespace waybar::modules::hyprland {
namespace {
auto constexpr WINDOW_CREATION_TIMEOUT = 2;
}
int Workspaces::window_rewrite_priority_function(std::string const &window_rule) {
// Rules that match against title are prioritized
// Rules that don't specify if they're matching against either title or class are deprioritized
@ -200,7 +196,7 @@ auto Workspaces::update() -> void {
}
bool any_window_created = false;
std::vector<WorkspaceWindow> not_created;
std::vector<WindowCreationPayload> not_created;
for (auto &window_payload : windows_to_create_) {
bool created = false;
@ -212,6 +208,7 @@ auto Workspaces::update() -> void {
}
}
if (!created) {
static auto const WINDOW_CREATION_TIMEOUT = 2;
if (window_payload.increment_time_spent_uncreated() < WINDOW_CREATION_TIMEOUT) {
not_created.push_back(window_payload);
}
@ -429,7 +426,7 @@ void Workspace::initialize_window_map(const Json::Value &clients_data) {
}
}
void Workspace::insert_window(WorkspaceWindow create_window_paylod) {
void Workspace::insert_window(WindowCreationPayload create_window_paylod) {
if (!create_window_paylod.is_empty(workspace_manager_)) {
window_map_[create_window_paylod.addr()] = create_window_paylod.repr(workspace_manager_);
}
@ -441,7 +438,7 @@ std::string Workspace::remove_window(WindowAddress const &addr) {
return window_repr;
}
bool Workspace::on_window_opened(WorkspaceWindow const &create_window_paylod) {
bool Workspace::on_window_opened(WindowCreationPayload const &create_window_paylod) {
if (create_window_paylod.workspace_name() == name()) {
insert_window(create_window_paylod);
return true;
@ -864,8 +861,8 @@ std::string Workspaces::get_rewrite(std::string window_class, std::string window
return window_rewrite_rules_.get(window_repr_key);
}
WorkspaceWindow::WorkspaceWindow(std::string workspace_name, WindowAddress window_address,
std::string window_repr)
WindowCreationPayload::WindowCreationPayload(std::string workspace_name,
WindowAddress window_address, std::string window_repr)
: window_(std::move(window_repr)),
window_address_(std::move(window_address)),
workspace_name_(std::move(workspace_name)) {
@ -873,8 +870,9 @@ WorkspaceWindow::WorkspaceWindow(std::string workspace_name, WindowAddress windo
clear_workspace_name();
}
WorkspaceWindow::WorkspaceWindow(std::string workspace_name, WindowAddress window_address,
std::string window_class, std::string window_title)
WindowCreationPayload::WindowCreationPayload(std::string workspace_name,
WindowAddress window_address, std::string window_class,
std::string window_title)
: window_(std::make_pair(std::move(window_class), std::move(window_title))),
window_address_(std::move(window_address)),
workspace_name_(std::move(workspace_name)) {
@ -882,7 +880,7 @@ WorkspaceWindow::WorkspaceWindow(std::string workspace_name, WindowAddress windo
clear_workspace_name();
}
WorkspaceWindow::WorkspaceWindow(Json::Value const &client_data)
WindowCreationPayload::WindowCreationPayload(Json::Value const &client_data)
: window_(std::make_pair(client_data["class"].asString(), client_data["title"].asString())),
window_address_(client_data["address"].asString()),
workspace_name_(client_data["workspace"]["name"].asString()) {
@ -890,7 +888,7 @@ WorkspaceWindow::WorkspaceWindow(Json::Value const &client_data)
clear_workspace_name();
}
std::string WorkspaceWindow::repr(Workspaces &workspace_manager) {
std::string WindowCreationPayload::repr(Workspaces &workspace_manager) {
if (std::holds_alternative<Repr>(window_)) {
return std::get<Repr>(window_);
}
@ -903,7 +901,7 @@ std::string WorkspaceWindow::repr(Workspaces &workspace_manager) {
throw std::runtime_error("WorkspaceWindow::repr: Unreachable");
}
bool WorkspaceWindow::is_empty(Workspaces &workspace_manager) {
bool WindowCreationPayload::is_empty(Workspaces &workspace_manager) {
if (std::holds_alternative<Repr>(window_)) {
return std::get<Repr>(window_).empty();
}
@ -917,9 +915,9 @@ bool WorkspaceWindow::is_empty(Workspaces &workspace_manager) {
throw std::runtime_error("WorkspaceWindow::is_empty: Unreachable");
}
int WorkspaceWindow::increment_time_spent_uncreated() { return time_spent_uncreated_++; }
int WindowCreationPayload::increment_time_spent_uncreated() { return time_spent_uncreated_++; }
void WorkspaceWindow::clear_addr() {
void WindowCreationPayload::clear_addr() {
// substr(2, ...) is necessary because Hyprland's JSON follows this format:
// 0x{ADDR}
// While Hyprland's IPC follows this format:
@ -933,7 +931,7 @@ void WorkspaceWindow::clear_addr() {
}
}
void WorkspaceWindow::clear_workspace_name() {
void WindowCreationPayload::clear_workspace_name() {
// The workspace name may optionally feature "special:" at the beginning.
// If so, we need to remove it because the workspace is saved WITHOUT the
// special qualifier. The reasoning is that not all of Hyprland's IPC events
@ -948,7 +946,7 @@ void WorkspaceWindow::clear_workspace_name() {
}
}
void WorkspaceWindow::move_to_worksace(std::string &new_workspace_name) {
void WindowCreationPayload::move_to_worksace(std::string &new_workspace_name) {
workspace_name_ = new_workspace_name;
}