Skip to content

Commit

Permalink
wlr-taskbar: allow sorting by app_id
Browse files Browse the repository at this point in the history
some users (maybe only myself) may want to sort the task bar by app_id
which then places occurrences of the same task next to each other.

Signed-off-by: Louis DeLosSantos <louis.delos@gmail.com>
  • Loading branch information
ldelossa committed Jan 24, 2023
1 parent d6bd440 commit 498e7f1
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 34 deletions.
3 changes: 2 additions & 1 deletion include/modules/wlr/taskbar.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ class Task {
FULLSCREEN = (1 << 3),
INVALID = (1 << 4)
};
// made public so TaskBar can reorder based on configuration.
Gtk::Button button;

private:
static uint32_t global_id;
Expand All @@ -53,7 +55,6 @@ class Task {

uint32_t id_;

Gtk::Button button_;
Gtk::Box content_;
Gtk::Image icon_;
Gtk::Label text_before_;
Expand Down
5 changes: 5 additions & 0 deletions man/waybar-wlr-taskbar.5.scd
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ Addressed by *wlr/taskbar*
default: false ++
If set to true, always reorder the tasks in the taskbar so that the currently active one is first. Otherwise don't reorder.

*sort-by-app-id*: ++
typeof: bool ++
default: false ++
If set to true, group tasks by their app_id. Cannot be used with 'active-first'.

*on-click*: ++
typeof: string ++
The action which should be triggered when clicking on the application button with the left mouse button.
Expand Down
77 changes: 44 additions & 33 deletions src/modules/wlr/taskbar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -265,14 +265,14 @@ Task::Task(const waybar::Bar &bar, const Json::Value &config, Taskbar *tbar,
content_{bar.vertical ? Gtk::ORIENTATION_VERTICAL : Gtk::ORIENTATION_HORIZONTAL, 0} {
zwlr_foreign_toplevel_handle_v1_add_listener(handle_, &toplevel_handle_impl, this);

button_.set_relief(Gtk::RELIEF_NONE);
button.set_relief(Gtk::RELIEF_NONE);

content_.add(text_before_);
content_.add(icon_);
content_.add(text_after_);

content_.show();
button_.add(content_);
button.add(content_);

format_before_.clear();
format_after_.clear();
Expand Down Expand Up @@ -314,19 +314,19 @@ Task::Task(const waybar::Bar &bar, const Json::Value &config, Taskbar *tbar,
config_["on-click-right"].isString()) {
}

button_.add_events(Gdk::BUTTON_PRESS_MASK);
button_.signal_button_press_event().connect(sigc::mem_fun(*this, &Task::handle_clicked), false);
button_.signal_button_release_event().connect(sigc::mem_fun(*this, &Task::handle_button_release),
button.add_events(Gdk::BUTTON_PRESS_MASK);
button.signal_button_press_event().connect(sigc::mem_fun(*this, &Task::handle_clicked), false);
button.signal_button_release_event().connect(sigc::mem_fun(*this, &Task::handle_button_release),
false);

button_.signal_motion_notify_event().connect(sigc::mem_fun(*this, &Task::handle_motion_notify),
button.signal_motion_notify_event().connect(sigc::mem_fun(*this, &Task::handle_motion_notify),
false);

button_.drag_source_set(target_entries, Gdk::BUTTON1_MASK, Gdk::ACTION_MOVE);
button_.drag_dest_set(target_entries, Gtk::DEST_DEFAULT_ALL, Gdk::ACTION_MOVE);
button.drag_source_set(target_entries, Gdk::BUTTON1_MASK, Gdk::ACTION_MOVE);
button.drag_dest_set(target_entries, Gtk::DEST_DEFAULT_ALL, Gdk::ACTION_MOVE);

button_.signal_drag_data_get().connect(sigc::mem_fun(*this, &Task::handle_drag_data_get), false);
button_.signal_drag_data_received().connect(
button.signal_drag_data_get().connect(sigc::mem_fun(*this, &Task::handle_drag_data_get), false);
button.signal_drag_data_received().connect(
sigc::mem_fun(*this, &Task::handle_drag_data_received), false);
}

Expand All @@ -336,7 +336,7 @@ Task::~Task() {
handle_ = nullptr;
}
if (button_visible_) {
tbar_->remove_button(button_);
tbar_->remove_button(button);
button_visible_ = false;
}
}
Expand Down Expand Up @@ -435,8 +435,8 @@ void Task::handle_output_enter(struct wl_output *output) {

if (!button_visible_ && (tbar_->all_outputs() || tbar_->show_output(output))) {
/* The task entered the output of the current bar make the button visible */
tbar_->add_button(button_);
button_.show();
tbar_->add_button(button);
button.show();
button_visible_ = true;
spdlog::debug("{} now visible on {}", repr(), bar_.output->name);
}
Expand All @@ -447,8 +447,8 @@ void Task::handle_output_leave(struct wl_output *output) {

if (button_visible_ && !tbar_->all_outputs() && tbar_->show_output(output)) {
/* The task left the output of the current bar, make the button invisible */
tbar_->remove_button(button_);
button_.hide();
tbar_->remove_button(button);
button.hide();
button_visible_ = false;
spdlog::debug("{} now invisible on {}", repr(), bar_.output->name);
}
Expand All @@ -470,31 +470,31 @@ void Task::handle_done() {
spdlog::debug("{} changed", repr());

if (state_ & MAXIMIZED) {
button_.get_style_context()->add_class("maximized");
button.get_style_context()->add_class("maximized");
} else if (!(state_ & MAXIMIZED)) {
button_.get_style_context()->remove_class("maximized");
button.get_style_context()->remove_class("maximized");
}

if (state_ & MINIMIZED) {
button_.get_style_context()->add_class("minimized");
button.get_style_context()->add_class("minimized");
} else if (!(state_ & MINIMIZED)) {
button_.get_style_context()->remove_class("minimized");
button.get_style_context()->remove_class("minimized");
}

if (state_ & ACTIVE) {
button_.get_style_context()->add_class("active");
button.get_style_context()->add_class("active");
} else if (!(state_ & ACTIVE)) {
button_.get_style_context()->remove_class("active");
button.get_style_context()->remove_class("active");
}

if (state_ & FULLSCREEN) {
button_.get_style_context()->add_class("fullscreen");
button.get_style_context()->add_class("fullscreen");
} else if (!(state_ & FULLSCREEN)) {
button_.get_style_context()->remove_class("fullscreen");
button.get_style_context()->remove_class("fullscreen");
}

if (config_["active-first"].isBool() && config_["active-first"].asBool() && active())
tbar_->move_button(button_, 0);
tbar_->move_button(button, 0);

tbar_->dp.emit();
}
Expand All @@ -504,7 +504,7 @@ void Task::handle_closed() {
zwlr_foreign_toplevel_handle_v1_destroy(handle_);
handle_ = nullptr;
if (button_visible_) {
tbar_->remove_button(button_);
tbar_->remove_button(button);
button_visible_ = false;
}
tbar_->remove_task(id_);
Expand Down Expand Up @@ -560,12 +560,12 @@ bool Task::handle_button_release(GdkEventButton *bt) {
bool Task::handle_motion_notify(GdkEventMotion *mn) {
if (drag_start_button == -1) return false;

if (button_.drag_check_threshold(drag_start_x, drag_start_y, mn->x, mn->y)) {
if (button.drag_check_threshold(drag_start_x, drag_start_y, mn->x, mn->y)) {
/* start drag in addition to other assigned action */
auto target_list = Gtk::TargetList::create(target_entries);
auto refptr = Glib::RefPtr<Gtk::TargetList>(target_list);
auto drag_context =
button_.drag_begin(refptr, Gdk::DragAction::ACTION_MOVE, drag_start_button, (GdkEvent *)mn);
button.drag_begin(refptr, Gdk::DragAction::ACTION_MOVE, drag_start_button, (GdkEvent *)mn);
}

return false;
Expand All @@ -574,7 +574,7 @@ bool Task::handle_motion_notify(GdkEventMotion *mn) {
void Task::handle_drag_data_get(const Glib::RefPtr<Gdk::DragContext> &context,
Gtk::SelectionData &selection_data, guint info, guint time) {
spdlog::debug("drag_data_get");
void *button_addr = (void *)&this->button_;
void *button_addr = (void *)&this->button;

selection_data.set("WAYBAR_TOPLEVEL", 32, (const guchar *)&button_addr, sizeof(gpointer));
}
Expand All @@ -585,16 +585,16 @@ void Task::handle_drag_data_received(const Glib::RefPtr<Gdk::DragContext> &conte
gpointer handle = *(gpointer *)selection_data.get_data();
auto dragged_button = (Gtk::Button *)handle;

if (dragged_button == &this->button_) return;
if (dragged_button == &this->button) return;

auto parent_of_dragged = dragged_button->get_parent();
auto parent_of_dest = this->button_.get_parent();
auto parent_of_dest = this->button.get_parent();

if (parent_of_dragged != parent_of_dest) return;

auto box = (Gtk::Box *)parent_of_dragged;

auto position_prop = box->child_property_position(this->button_);
auto position_prop = box->child_property_position(this->button);
auto position = position_prop.get_value();

box->reorder_child(*dragged_button, position);
Expand Down Expand Up @@ -640,9 +640,9 @@ void Task::update() {
fmt::arg("app_id", app_id), fmt::arg("state", state_string()),
fmt::arg("short_state", state_string(true)));
if (markup)
button_.set_tooltip_markup(txt);
button.set_tooltip_markup(txt);
else
button_.set_tooltip_text(txt);
button.set_tooltip_text(txt);
}
}

Expand Down Expand Up @@ -785,6 +785,17 @@ void Taskbar::update() {
t->update();
}

if (config_["sort-by-app-id"].asBool()) {
std::stable_sort(tasks_.begin(), tasks_.end(),
[](const std::unique_ptr<Task> &a, const std::unique_ptr<Task> &b) {
return a->app_id() < b->app_id();
});

for (unsigned long i = 0; i < tasks_.size(); i++) {
move_button(tasks_[i]->button, i);
}
}

AModule::update();
}

Expand Down

0 comments on commit 498e7f1

Please sign in to comment.