Skip to content

Commit

Permalink
Merge pull request #2529 from Syndelis/feat/hyprland-window-workspaces
Browse files Browse the repository at this point in the history
Feature: Hyprland dynamic window names on workspaces
  • Loading branch information
Alexays authored Oct 2, 2023
2 parents 4c0347d + 1b98a04 commit 58e506a
Show file tree
Hide file tree
Showing 8 changed files with 290 additions and 15 deletions.
35 changes: 33 additions & 2 deletions include/modules/hyprland/workspaces.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@

#include <gtkmm/button.h>
#include <gtkmm/label.h>
#include <json/value.h>

#include <cstddef>
#include <cstdint>
#include <map>
#include <memory>
#include <optional>
#include <string>
#include <vector>

Expand All @@ -13,13 +17,15 @@
#include "modules/hyprland/backend.hpp"
#include "util/enum.hpp"

using WindowAddress = std::string;
namespace waybar::modules::hyprland {

class Workspaces;

class Workspace {
public:
explicit Workspace(const Json::Value& workspace_data, Workspaces& workspace_manager);
explicit Workspace(const Json::Value& workspace_data, Workspaces& workspace_manager,
const Json::Value& clients_json = Json::Value::nullRef);
std::string& select_icon(std::map<std::string, std::string>& icons_map);
Gtk::Button& button() { return button_; };

Expand All @@ -40,6 +46,16 @@ class Workspace {
void set_visible(bool value = true) { is_visible_ = value; };
void set_windows(uint value) { windows_ = value; };
void set_name(std::string value) { name_ = value; };
bool contains_window(WindowAddress addr) { return window_map_.contains(addr); }
void insert_window(WindowAddress addr, std::string window_repr);
std::string remove_window(WindowAddress addr);
void initialize_window_map(const Json::Value& clients_data);

bool on_window_opened(WindowAddress& addr, std::string& workspace_name, std::string window_repr);
bool on_window_opened(WindowAddress& addr, std::string& workspace_name, std::string& window_class,
std::string& window_title);

std::optional<std::string> on_window_closed(WindowAddress& addr);

void update(const std::string& format, const std::string& icon);

Expand All @@ -56,6 +72,8 @@ class Workspace {
bool is_urgent_ = false;
bool is_visible_ = false;

std::map<WindowAddress, std::string> window_map_;

Gtk::Button button_;
Gtk::Box content_;
Gtk::Label label_;
Expand All @@ -74,16 +92,25 @@ class Workspaces : public AModule, public EventHandler {

auto get_bar_output() const -> std::string { return bar_.output->name; }

std::string get_rewrite(std::string window_class);
std::string& get_window_separator() { return format_window_separator_; }

private:
void onEvent(const std::string&) override;
void update_window_count();
void initialize_window_maps();
void sort_workspaces();
void create_workspace(Json::Value& value);
void create_workspace(Json::Value& workspace_data,
const Json::Value& clients_data = Json::Value::nullRef);
void remove_workspace(std::string name);
void set_urgent_workspace(std::string windowaddress);
void parse_config(const Json::Value& config);
void register_ipc();

void on_window_opened(std::string payload);
void on_window_closed(std::string payload);
void on_window_moved(std::string payload);

bool all_outputs_ = false;
bool show_special_ = false;
bool active_only_ = false;
Expand All @@ -103,6 +130,10 @@ class Workspaces : public AModule, public EventHandler {

std::string format_;
std::map<std::string, std::string> icons_map_;
Json::Value window_rewrite_rules_;
std::map<std::string, std::string> regex_cache_;
std::string format_window_separator_;
std::string window_rewrite_default_;
bool with_icon_;
uint64_t monitor_id_;
std::string active_workspace_name_;
Expand Down
4 changes: 3 additions & 1 deletion include/util/rewrite_string.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@

namespace waybar::util {
std::string rewriteString(const std::string&, const Json::Value&);
}
std::string rewriteStringOnce(const std::string& value, const Json::Value& rules,
bool& matched_any);
} // namespace waybar::util
28 changes: 28 additions & 0 deletions man/waybar-hyprland-workspaces.5.scd
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,21 @@ Addressed by *hyprland/workspaces*
typeof: array ++
Based on the workspace id and state, the corresponding icon gets selected. See *icons*.

*window-rewrite*: ++
typeof: object ++
Regex rules to map window class to an icon or preferred method of representation for a workspace's window.
Keys are the rules, while the values are the methods of representation.

*window-rewrite-default*:
typeof: string ++
default: "?" ++
The default method of representation for a workspace's window. This will be used for windows whose classes do not match any of the rules in *window-rewrite*.

*format-window-separator*: ++
typeof: string ++
default: " " ++
The separator to be used between windows in a workspace.

*show-special*: ++
typeof: bool ++
default: false ++
Expand Down Expand Up @@ -103,6 +118,19 @@ Additional to workspace name matching, the following *format-icons* can be set.
}
```

```
"hyprland/workspaces": {
"format": "{name}\n{windows}",
"format-window-separator": "\n",
"window-rewrite-default": "",
"window-rewrite": {
"firefox": "",
"foot": "",
"code": "󰨞",
}
}
```

# Style

- *#workspaces*
Expand Down
4 changes: 3 additions & 1 deletion src/modules/backlight.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,9 @@ void waybar::modules::Backlight::upsert_device(ForwardIt first, ForwardIt last,
check_nn(name);

const char *actual_brightness_attr =
strncmp(name, "amdgpu_bl", 9) == 0 || strcmp(name, "apple-panel-bl") == 0 ? "brightness" : "actual_brightness";
strncmp(name, "amdgpu_bl", 9) == 0 || strcmp(name, "apple-panel-bl") == 0
? "brightness"
: "actual_brightness";

const char *actual = udev_device_get_sysattr_value(dev, actual_brightness_attr);
const char *max = udev_device_get_sysattr_value(dev, "max_brightness");
Expand Down
2 changes: 1 addition & 1 deletion src/modules/battery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ const std::tuple<uint8_t, float, std::string, float> waybar::modules::Battery::g
std::string _status;

/* Check for adapter status if battery is not available */
if(!std::ifstream(bat / "status")) {
if (!std::ifstream(bat / "status")) {
std::getline(std::ifstream(adapter_ / "status"), _status);
} else {
std::getline(std::ifstream(bat / "status"), _status);
Expand Down
2 changes: 1 addition & 1 deletion src/modules/custom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ waybar::modules::Custom::Custom(const std::string& name, const std::string& id,
delayWorker();
} else if (config_["exec"].isString()) {
continuousWorker();
}
}
}

waybar::modules::Custom::~Custom() {
Expand Down
Loading

0 comments on commit 58e506a

Please sign in to comment.