chore: lint

This commit is contained in:
Alex 2024-09-13 09:43:19 +02:00
parent 8a89706d03
commit 1142979581
6 changed files with 98 additions and 137 deletions

View File

@ -8,15 +8,16 @@
#include <sys/types.h>
#include <sys/un.h>
#include <unistd.h>
#include "giomm/datainputstream.h"
#include "giomm/dataoutputstream.h"
#include "giomm/unixinputstream.h"
#include "giomm/unixoutputstream.h"
#include <iostream>
#include <string>
#include <thread>
#include "giomm/datainputstream.h"
#include "giomm/dataoutputstream.h"
#include "giomm/unixinputstream.h"
#include "giomm/unixoutputstream.h"
namespace waybar::modules::niri {
int IPC::connectToSocket() {
@ -61,8 +62,7 @@ void IPC::startIPC() {
spdlog::error("Niri IPC: failed to start, reason: {}", e.what());
return;
}
if (socketfd == -1)
return;
if (socketfd == -1) return;
spdlog::info("Niri IPC starting");
@ -101,8 +101,7 @@ void IPC::startIPC() {
void IPC::parseIPC(const std::string &line) {
const auto ev = parser_.parse(line);
const auto members = ev.getMemberNames();
if (members.size() != 1)
throw std::runtime_error("Event must have a single member");
if (members.size() != 1) throw std::runtime_error("Event must have a single member");
{
auto lock = lockData();
@ -112,14 +111,12 @@ void IPC::parseIPC(const std::string& line) {
const auto &values = payload["workspaces"];
std::copy(values.begin(), values.end(), std::back_inserter(workspaces_));
std::sort(workspaces_.begin(), workspaces_.end(),
[](const auto &a, const auto &b) {
std::sort(workspaces_.begin(), workspaces_.end(), [](const auto &a, const auto &b) {
const auto &aOutput = a["output"].asString();
const auto &bOutput = b["output"].asString();
const auto aIdx = a["idx"].asUInt();
const auto bIdx = b["idx"].asUInt();
if (aOutput == bOutput)
return aIdx < bIdx;
if (aOutput == bOutput) return aIdx < bIdx;
return aOutput < bOutput;
});
} else if (const auto &payload = ev["WorkspaceActivated"]) {
@ -132,19 +129,18 @@ void IPC::parseIPC(const std::string& line) {
const auto &output = ws["output"].asString();
for (auto &ws : workspaces_) {
const auto got_activated = (ws["id"].asUInt64() == id);
if (ws["output"] == output)
ws["is_active"] = got_activated;
if (ws["output"] == output) ws["is_active"] = got_activated;
if (focused)
ws["is_focused"] = got_activated;
if (focused) ws["is_focused"] = got_activated;
}
} else {
spdlog::error("Activated unknown workspace");
}
} else if (const auto &payload = ev["WorkspaceActiveWindowChanged"]) {
const auto workspaceId = payload["workspace_id"].asUInt64();
auto it = std::find_if(workspaces_.begin(), workspaces_.end(),
[workspaceId](const auto &ws) { return ws["id"].asUInt64() == workspaceId; });
auto it = std::find_if(workspaces_.begin(), workspaces_.end(), [workspaceId](const auto &ws) {
return ws["id"].asUInt64() == workspaceId;
});
if (it != workspaces_.end()) {
auto &ws = *it;
ws["active_window_id"] = payload["active_window_id"];
@ -157,8 +153,7 @@ void IPC::parseIPC(const std::string& line) {
keyboardLayoutCurrent_ = layouts["current_idx"].asUInt();
keyboardLayoutNames_.clear();
for (const auto &fullName : names)
keyboardLayoutNames_.push_back(fullName.asString());
for (const auto &fullName : names) keyboardLayoutNames_.push_back(fullName.asString());
} else if (const auto &payload = ev["KeyboardLayoutSwitched"]) {
keyboardLayoutCurrent_ = payload["idx"].asUInt();
} else if (const auto &payload = ev["WindowsChanged"]) {
@ -236,8 +231,7 @@ void IPC::unregisterForIPC(EventHandler* ev_handler) {
Json::Value IPC::send(const Json::Value &request) {
int socketfd = connectToSocket();
if (socketfd == -1)
throw std::runtime_error("Niri is not running");
if (socketfd == -1) throw std::runtime_error("Niri is not running");
auto unix_istream = Gio::UnixInputStream::create(socketfd, true);
auto unix_ostream = Gio::UnixOutputStream::create(socketfd, false);
@ -256,8 +250,7 @@ Json::Value IPC::send(const Json::Value& request) {
throw std::runtime_error("error writing to niri socket");
std::string line;
if (!istream->read_line(line))
throw std::runtime_error("error reading from niri socket");
if (!istream->read_line(line)) throw std::runtime_error("error reading from niri socket");
std::istringstream iss(std::move(line));
Json::Value response;
@ -265,4 +258,4 @@ Json::Value IPC::send(const Json::Value& request) {
return response;
}
} // namespace waybar::modules::hyprland
} // namespace waybar::modules::niri

View File

@ -12,8 +12,7 @@ Language::Language(const std::string &id, const Bar &bar, const Json::Value &con
: ALabel(config, "language", id, "{}", 0, true), bar_(bar) {
label_.hide();
if (!gIPC)
gIPC = std::make_unique<IPC>();
if (!gIPC) gIPC = std::make_unique<IPC>();
gIPC->registerForIPC("KeyboardLayoutsChanged", this);
gIPC->registerForIPC("KeyboardLayoutSwitched", this);
@ -33,8 +32,7 @@ void Language::updateFromIPC() {
auto ipcLock = gIPC->lockData();
layouts_.clear();
for (const auto &fullName : gIPC->keyboardLayoutNames())
layouts_.push_back(getLayout(fullName));
for (const auto &fullName : gIPC->keyboardLayoutNames()) layouts_.push_back(getLayout(fullName));
current_idx_ = gIPC->keyboardLayoutCurrent();
}

View File

@ -11,8 +11,7 @@ namespace waybar::modules::niri {
Window::Window(const std::string &id, const Bar &bar, const Json::Value &config)
: AAppIconLabel(config, "window", id, "{title}", 0, true), bar_(bar) {
if (!gIPC)
gIPC = std::make_unique<IPC>();
if (!gIPC) gIPC = std::make_unique<IPC>();
gIPC->registerForIPC("WindowsChanged", this);
gIPC->registerForIPC("WindowOpenedOrChanged", this);
@ -22,13 +21,9 @@ Window::Window(const std::string &id, const Bar &bar, const Json::Value &config)
dp.emit();
}
Window::~Window() {
gIPC->unregisterForIPC(this);
}
Window::~Window() { gIPC->unregisterForIPC(this); }
void Window::onEvent(const Json::Value &ev) {
dp.emit();
}
void Window::onEvent(const Json::Value &ev) { dp.emit(); }
void Window::doUpdate() {
auto ipcLock = gIPC->lockData();
@ -37,8 +32,7 @@ void Window::doUpdate() {
const auto &workspaces = gIPC->workspaces();
const auto separateOutputs = config_["separate-outputs"].asBool();
const auto ws_it = std::find_if(workspaces.cbegin(), workspaces.cend(),
[&](const auto &ws) {
const auto ws_it = std::find_if(workspaces.cbegin(), workspaces.cend(), [&](const auto &ws) {
if (separateOutputs) {
return ws["is_active"].asBool() && ws["output"].asString() == bar_.output->name;
}
@ -67,37 +61,31 @@ void Window::doUpdate() {
label_.show();
label_.set_markup(waybar::util::rewriteString(
fmt::format(fmt::runtime(format_),
fmt::arg("title", sanitizedTitle),
fmt::format(fmt::runtime(format_), fmt::arg("title", sanitizedTitle),
fmt::arg("app_id", sanitizedAppId)),
config_["rewrite"]));
updateAppIconName(appId, "");
if (tooltipEnabled())
label_.set_tooltip_text(title);
if (tooltipEnabled()) label_.set_tooltip_text(title);
const auto id = window["id"].asUInt64();
const auto workspaceId = window["workspace_id"].asUInt64();
const auto isSolo = std::none_of(windows.cbegin(), windows.cend(),
[&](const auto &win) {
const auto isSolo = std::none_of(windows.cbegin(), windows.cend(), [&](const auto &win) {
return win["id"].asUInt64() != id && win["workspace_id"].asUInt64() == workspaceId;
});
setClass("solo", isSolo);
if (!appId.empty())
setClass(appId, isSolo);
if (!appId.empty()) setClass(appId, isSolo);
if (oldAppId_ != appId) {
if (!oldAppId_.empty())
setClass(oldAppId_, false);
if (!oldAppId_.empty()) setClass(oldAppId_, false);
oldAppId_ = appId;
}
} else {
label_.hide();
updateAppIconName("", "");
setClass("solo", false);
if (!oldAppId_.empty())
setClass(oldAppId_, false);
if (!oldAppId_.empty()) setClass(oldAppId_, false);
oldAppId_.clear();
}
}

View File

@ -7,9 +7,7 @@
namespace waybar::modules::niri {
Workspaces::Workspaces(const std::string &id, const Bar &bar, const Json::Value &config)
: AModule(config, "workspaces", id, false, false),
bar_(bar),
box_(bar.orientation, 0) {
: AModule(config, "workspaces", id, false, false), bar_(bar), box_(bar.orientation, 0) {
box_.set_name("workspaces");
if (!id.empty()) {
box_.get_style_context()->add_class(id);
@ -17,8 +15,7 @@ Workspaces::Workspaces(const std::string &id, const Bar &bar, const Json::Value
box_.get_style_context()->add_class(MODULE_CLASS);
event_box_.add(box_);
if (!gIPC)
gIPC = std::make_unique<IPC>();
if (!gIPC) gIPC = std::make_unique<IPC>();
gIPC->registerForIPC("WorkspacesChanged", this);
gIPC->registerForIPC("WorkspaceActivated", this);
@ -27,13 +24,9 @@ Workspaces::Workspaces(const std::string &id, const Bar &bar, const Json::Value
dp.emit();
}
Workspaces::~Workspaces() {
gIPC->unregisterForIPC(this);
}
Workspaces::~Workspaces() { gIPC->unregisterForIPC(this); }
void Workspaces::onEvent(const Json::Value &ev) {
dp.emit();
}
void Workspaces::onEvent(const Json::Value &ev) { dp.emit(); }
void Workspaces::doUpdate() {
auto ipcLock = gIPC->lockData();
@ -43,8 +36,7 @@ void Workspaces::doUpdate() {
const auto &workspaces = gIPC->workspaces();
std::copy_if(workspaces.cbegin(), workspaces.cend(), std::back_inserter(my_workspaces),
[&](const auto &ws) {
if (alloutputs)
return true;
if (alloutputs) return true;
return ws["output"].asString() == bar_.output->name;
});
@ -99,11 +91,8 @@ void Workspaces::doUpdate() {
if (config_["format"].isString()) {
auto format = config_["format"].asString();
name = fmt::format(
fmt::runtime(format),
fmt::arg("icon", getIcon(name, ws)),
fmt::arg("value", name),
fmt::arg("name", ws["name"].asString()),
name = fmt::format(fmt::runtime(format), fmt::arg("icon", getIcon(name, ws)),
fmt::arg("value", name), fmt::arg("name", ws["name"].asString()),
fmt::arg("index", ws["idx"].asUInt()),
fmt::arg("output", ws["output"].asString()));
}
@ -129,8 +118,7 @@ void Workspaces::doUpdate() {
const auto &ws = *it;
auto pos = ws["idx"].asUInt() - 1;
if (alloutputs)
pos = it - my_workspaces.cbegin();
if (alloutputs) pos = it - my_workspaces.cbegin();
auto &button = buttons_[ws["id"].asUInt64()];
box_.reorder_child(button, pos);
@ -176,27 +164,21 @@ Gtk::Button &Workspaces::addButton(const Json::Value &ws) {
std::string Workspaces::getIcon(const std::string &value, const Json::Value &ws) {
const auto &icons = config_["format-icons"];
if (!icons)
return value;
if (!icons) return value;
if (ws["is_focused"].asBool() && icons["focused"])
return icons["focused"].asString();
if (ws["is_focused"].asBool() && icons["focused"]) return icons["focused"].asString();
if (ws["is_active"].asBool() && icons["active"])
return icons["active"].asString();
if (ws["is_active"].asBool() && icons["active"]) return icons["active"].asString();
if (ws["name"]) {
const auto &name = ws["name"].asString();
if (icons[name])
return icons[name].asString();
if (icons[name]) return icons[name].asString();
}
const auto idx = ws["idx"].asString();
if (icons[idx])
return icons[idx].asString();
if (icons[idx]) return icons[idx].asString();
if (icons["default"])
return icons["default"].asString();
if (icons["default"]) return icons["default"].asString();
return value;
}