Skip to content

refactor window-list #156

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 7 additions & 54 deletions src/panel/widgets/window-list/toplevel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,16 @@ class WayfireToplevel::impl
zwlr_foreign_toplevel_handle_v1_add_listener(handle,
&toplevel_handle_v1_impl, this);

static constexpr auto SIZE_LIMIT_COEF = 0.01;
Gdk::Rectangle monitor_rect;
window_list->output->monitor->get_geometry(monitor_rect);
label.set_max_width_chars(monitor_rect.get_width() * SIZE_LIMIT_COEF);
label.set_ellipsize(Pango::EllipsizeMode::ELLIPSIZE_END);
button_contents.add(image);
button_contents.add(label);
button_contents.set_halign(Gtk::ALIGN_START);
button_contents.set_spacing(5);
button.add(button_contents);
button.set_tooltip_text("none");

button.signal_clicked().connect_notify(
sigc::mem_fun(this, &WayfireToplevel::impl::on_clicked));
Expand Down Expand Up @@ -265,7 +269,7 @@ class WayfireToplevel::impl
void on_allocation_changed(Gtk::Allocation& alloc)
{
send_rectangle_hint();
window_list->scrolled_window.queue_allocate();
window_list->box.queue_allocate();
}

void on_scale_update()
Expand Down Expand Up @@ -304,60 +308,10 @@ class WayfireToplevel::impl
panel->get_wl_surface(), x, y, width, height);
}

int32_t max_width = 0;
void set_title(std::string title)
{
this->title = title;
button.set_tooltip_text(title);

set_max_width(max_width);
}

Glib::ustring shorten_title(int show_chars)
{
if (show_chars == 0)
return "";

int title_len = title.length();
Glib::ustring short_title = title.substr(0, show_chars);
if (title_len - show_chars >= 2) {
short_title += "..";
} else if (title_len != show_chars) {
short_title += ".";
}

return short_title;
}

int get_button_preferred_width()
{
int min_width, preferred_width;
button.get_preferred_width(min_width, preferred_width);

return preferred_width;
}

void set_max_width(int width)
{
this->max_width = width;
if (max_width == 0)
{
this->button.set_size_request(-1, -1);
this->label.set_label(title);
return;
}

this->button.set_size_request(width, -1);

int show_chars = 0;
for (show_chars = title.length(); show_chars > 0; show_chars--)
{
this->label.set_text(shorten_title(show_chars));
if (get_button_preferred_width() <= max_width)
break;
}

label.set_text(shorten_title(show_chars));
label.set_text(title);
}

uint32_t get_state()
Expand Down Expand Up @@ -450,7 +404,6 @@ WayfireToplevel::WayfireToplevel(WayfireWindowList *window_list,
zwlr_foreign_toplevel_handle_v1 *handle)
:pimpl(new WayfireToplevel::impl(window_list, handle)) { }

void WayfireToplevel::set_width(int pixels) { return pimpl->set_max_width(pixels); }
std::vector<zwlr_foreign_toplevel_handle_v1 *>& WayfireToplevel::get_children() { return pimpl->get_children(); }
zwlr_foreign_toplevel_handle_v1 * WayfireToplevel::get_parent() { return pimpl->get_parent(); }
void WayfireToplevel::set_parent(zwlr_foreign_toplevel_handle_v1 *parent) { return pimpl->set_parent(parent); }
Expand Down
1 change: 0 additions & 1 deletion src/panel/widgets/window-list/toplevel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ class WayfireToplevel
WayfireToplevel(WayfireWindowList *window_list, zwlr_foreign_toplevel_handle_v1 *handle);

uint32_t get_state();
void set_width(int pixels);
zwlr_foreign_toplevel_handle_v1 * get_parent();
void set_parent(zwlr_foreign_toplevel_handle_v1 *);
std::vector<zwlr_foreign_toplevel_handle_v1 *>& get_children();
Expand Down
77 changes: 5 additions & 72 deletions src/panel/widgets/window-list/window-list.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
#include <iostream>
#include <memory>
#include <glibmm.h>
#include <gdk/gdkwayland.h>

#include "toplevel.hpp"
#include "window-list.hpp"
#include "panel.hpp"

WayfireWindowListBox::WayfireWindowListBox() : Gtk::HBox()
{
}
WayfireWindowListBox::WayfireWindowListBox() = default;

void WayfireWindowListBox::set_top_widget(Gtk::Widget *top)
{
Expand Down Expand Up @@ -116,8 +115,6 @@ Gtk::Widget* WayfireWindowListBox::get_widget_at(int x)
return nullptr;
}

#define DEFAULT_SIZE_PC 0.1

static void handle_manager_toplevel(void *data, zwlr_foreign_toplevel_manager_v1 *manager,
zwlr_foreign_toplevel_handle_v1 *toplevel)
{
Expand Down Expand Up @@ -178,64 +175,9 @@ void WayfireWindowList::init(Gtk::HBox *container)
zwlr_foreign_toplevel_manager_v1_add_listener(manager,
&toplevel_manager_v1_impl, this);

scrolled_window.signal_draw().connect_notify(
sigc::mem_fun(this, &WayfireWindowList::on_draw));

box.set_homogeneous(true);
scrolled_window.add(box);
scrolled_window.set_propagate_natural_width(true);
container->pack_start(scrolled_window, true, true);
scrolled_window.show_all();
}

void WayfireWindowList::set_button_width(int width)
{
std::cout << "set width " << width << std::endl;
for (auto& toplevel : toplevels)
{
if (toplevel.second)
toplevel.second->set_width(width);
}
}

int WayfireWindowList::get_default_button_width()
{
return DEFAULT_SIZE_PC *
WayfirePanelApp::get().panel_for_wl_output(output->wo)->get_window()
.get_allocated_width();
}

int WayfireWindowList::get_target_button_width()
{
int num_children = box.get_children().size();
int target_width = get_default_button_width();

if (num_children > 0)
{
target_width = std::min(target_width,
scrolled_window.get_allocated_width() / num_children);
std::cout << "target button " << scrolled_window.get_allocated_width() << std::endl;
}

return target_width;
}

void WayfireWindowList::on_draw(const Cairo::RefPtr<Cairo::Context>& ctx)
{
int allocated_width = scrolled_window.get_allocated_width();

int minimal_width, preferred_width;
scrolled_window.get_preferred_width(minimal_width, preferred_width);

/* We have changed the size/number of toplevels. On top of that, our list
* is longer that the max size, so we need to re-layout the buttons */
if (preferred_width > allocated_width && toplevels.size() > 0)
set_button_width(get_target_button_width());
}

void WayfireWindowList::add_output(WayfireOutput *output)
{
std::unique_ptr<WayfireWindowList>();
box.show_all();
container->pack_start(box, true, true);
}

void WayfireWindowList::handle_toplevel_manager(zwlr_foreign_toplevel_manager_v1 *manager)
Expand All @@ -245,21 +187,12 @@ void WayfireWindowList::handle_toplevel_manager(zwlr_foreign_toplevel_manager_v1

void WayfireWindowList::handle_new_toplevel(zwlr_foreign_toplevel_handle_v1 *handle)
{
toplevels[handle] = std::unique_ptr<WayfireToplevel> (new WayfireToplevel(this, handle));
/* The size will be updated in the next on_draw() if needed */
toplevels[handle]->set_width(get_default_button_width());
toplevels[handle] = std::make_unique<WayfireToplevel>(this, handle);
}

void WayfireWindowList::handle_toplevel_closed(zwlr_foreign_toplevel_handle_v1 *handle)
{
toplevels.erase(handle);

/* No size adjustments necessary in this case */
if (toplevels.size() == 0)
return;

/* Recalculate button size */
set_button_width(get_target_button_width());
}

WayfireWindowList::WayfireWindowList(WayfireOutput *output)
Expand Down
9 changes: 0 additions & 9 deletions src/panel/widgets/window-list/window-list.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ class WayfireWindowList : public WayfireWidget
zwlr_foreign_toplevel_manager_v1 *manager;
WayfireOutput *output;
WayfireWindowListBox box;
Gtk::ScrolledWindow scrolled_window;

WayfireWindowList(WayfireOutput *output);
virtual ~WayfireWindowList();
Expand All @@ -77,14 +76,6 @@ class WayfireWindowList : public WayfireWidget
wayfire_config *get_config();

void init(Gtk::HBox *container) override;
void add_output(WayfireOutput *output);

private:
void on_draw(const Cairo::RefPtr<Cairo::Context>&);

void set_button_width(int width);
int get_default_button_width();
int get_target_button_width();
};

#endif /* end of include guard: WIDGETS_WINDOW_LIST_HPP */