mirror of
				https://github.com/rad4day/Waybar.git
				synced 2025-11-04 01:32:42 +01:00 
			
		
		
		
	refactor: avoid unneeded json parsing
This commit is contained in:
		@@ -104,9 +104,7 @@ struct Ipc::ipc_response Ipc::recv(int fd) {
 | 
			
		||||
    }
 | 
			
		||||
    total += res;
 | 
			
		||||
  }
 | 
			
		||||
  std::lock_guard<std::mutex> lock(mutex_parser_);
 | 
			
		||||
  auto parsed = parser_.parse(&payload.front());
 | 
			
		||||
  return {data32[0], data32[1], parsed};
 | 
			
		||||
  return {data32[0], data32[1], &payload.front()};
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
struct Ipc::ipc_response Ipc::send(int fd, uint32_t type, const std::string& payload) {
 | 
			
		||||
@@ -135,7 +133,7 @@ void Ipc::sendCmd(uint32_t type, const std::string& payload) {
 | 
			
		||||
void Ipc::subscribe(const std::string& payload) {
 | 
			
		||||
  std::lock_guard<std::mutex> lock(mutex_event_);
 | 
			
		||||
  auto res = Ipc::send(fd_event_, IPC_SUBSCRIBE, payload);
 | 
			
		||||
  if (!res.payload["success"].asBool()) {
 | 
			
		||||
  if (res.payload != "{\"success\": true}") {
 | 
			
		||||
    throw std::runtime_error("Unable to subscribe ipc event");
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -2,8 +2,7 @@
 | 
			
		||||
 | 
			
		||||
namespace waybar::modules::sway {
 | 
			
		||||
 | 
			
		||||
Mode::Mode(const std::string& id, const Json::Value& config)
 | 
			
		||||
    : ALabel(config, "{}") {
 | 
			
		||||
Mode::Mode(const std::string& id, const Json::Value& config) : ALabel(config, "{}") {
 | 
			
		||||
  label_.set_name("mode");
 | 
			
		||||
  if (!id.empty()) {
 | 
			
		||||
    label_.get_style_context()->add_class(id);
 | 
			
		||||
@@ -15,9 +14,10 @@ Mode::Mode(const std::string& id, const Json::Value& config)
 | 
			
		||||
  dp.emit();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void Mode::onEvent(const struct Ipc::ipc_response &res) {
 | 
			
		||||
  if (res.payload["change"] != "default") {
 | 
			
		||||
    mode_ = res.payload["change"].asString();
 | 
			
		||||
void Mode::onEvent(const struct Ipc::ipc_response& res) {
 | 
			
		||||
  auto payload = parser_.parse(res.payload);
 | 
			
		||||
  if (payload["change"] != "default") {
 | 
			
		||||
    mode_ = payload["change"].asString();
 | 
			
		||||
  } else {
 | 
			
		||||
    mode_.clear();
 | 
			
		||||
  }
 | 
			
		||||
 
 | 
			
		||||
@@ -21,18 +21,11 @@ Window::Window(const std::string& id, const Bar& bar, const Json::Value& config)
 | 
			
		||||
  worker();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void Window::onEvent(const struct Ipc::ipc_response& res) {
 | 
			
		||||
  auto data = res.payload;
 | 
			
		||||
  // Check for waybar prevents flicker when hovering window module
 | 
			
		||||
  if (((data["change"] == "focus" || data["change"] == "title") &&
 | 
			
		||||
       data["container"]["focused"].asBool() && data["container"]["name"].asString() != "waybar") ||
 | 
			
		||||
      data["change"] == "close") {
 | 
			
		||||
    getTree();
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
void Window::onEvent(const struct Ipc::ipc_response& res) { getTree(); }
 | 
			
		||||
 | 
			
		||||
void Window::onCmd(const struct Ipc::ipc_response& res) {
 | 
			
		||||
  auto [nb, id, name, app_id] = getFocusedNode(res.payload);
 | 
			
		||||
  auto payload = parser_.parse(res.payload);
 | 
			
		||||
  auto [nb, id, name, app_id] = getFocusedNode(payload);
 | 
			
		||||
  if (!app_id_.empty()) {
 | 
			
		||||
    bar_.window.get_style_context()->remove_class(app_id_);
 | 
			
		||||
  }
 | 
			
		||||
 
 | 
			
		||||
@@ -23,11 +23,12 @@ void Workspaces::onEvent(const struct Ipc::ipc_response &res) { ipc_.sendCmd(IPC
 | 
			
		||||
 | 
			
		||||
void Workspaces::onCmd(const struct Ipc::ipc_response &res) {
 | 
			
		||||
  if (res.type == IPC_GET_WORKSPACES) {
 | 
			
		||||
    if (res.payload.isArray()) {
 | 
			
		||||
    auto payload = parser_.parse(res.payload);
 | 
			
		||||
    if (payload.isArray()) {
 | 
			
		||||
      std::lock_guard<std::mutex> lock(mutex_);
 | 
			
		||||
      workspaces_.clear();
 | 
			
		||||
      std::copy_if(res.payload.begin(),
 | 
			
		||||
                   res.payload.end(),
 | 
			
		||||
      std::copy_if(payload.begin(),
 | 
			
		||||
                   payload.end(),
 | 
			
		||||
                   std::back_inserter(workspaces_),
 | 
			
		||||
                   [&](const auto &workspace) {
 | 
			
		||||
                     return !config_["all-outputs"].asBool()
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user