style: clean class

This commit is contained in:
Alexis 2018-08-09 10:50:16 +02:00
parent a00894a5a6
commit b987668f97
8 changed files with 49 additions and 51 deletions

View File

@ -13,18 +13,17 @@ namespace waybar {
Client& client;
Gtk::Window window;
struct wl_surface *surface;
struct zwlr_layer_surface_v1 *layer_surface;
struct zwlr_layer_surface_v1 *layerSurface;
std::unique_ptr<struct wl_output *> output;
bool visible = true;
auto set_width(int) -> void;
auto setWidth(int) -> void;
auto toggle() -> void;
private:
auto setup_widgets() -> void;
auto setup_css() -> void;
int width = 10;
Glib::RefPtr<Gtk::StyleContext> style_context;
Glib::RefPtr<Gtk::CssProvider> css_provider;
auto _setupWidgets() -> void;
auto _setupCss() -> void;
int _width = 10;
Glib::RefPtr<Gtk::StyleContext> _styleContext;
Glib::RefPtr<Gtk::CssProvider> _cssProvider;
};
}

View File

@ -11,42 +11,42 @@ window {
color: white;
}
.workspace-selector button {
.workspaces button {
padding: 0 5px;
background: transparent;
color: white;
border-bottom: 3px solid transparent;
}
.workspace-selector button.current {
.workspaces button.current {
background: #64727D;
border-bottom: 3px solid white;
}
.clock-widget, .battery-status, .cpu-widget, .memory-widget {
.clock, .battery, .cpu, .memory {
padding: 0 10px;
margin: 0 5px;
}
.clock-widget {
.clock {
background-color: #64727D;
}
.battery-status {
.battery {
background-color: #ffffff;
color: black;
}
.battery-status.battery-charging {
.battery.charging {
color: white;
background-color: #26A65B;
}
.cpu-widget {
.cpu {
background: #2ecc71;
color: #000000;
}
.memory-widget {
.memory {
background: #9b59b6;
}

View File

@ -21,8 +21,7 @@ static void handle_mode(void *data, struct wl_output *wl_output, uint32_t f,
int32_t w, int32_t h, int32_t refresh)
{
auto o = reinterpret_cast<waybar::Bar *>(data);
std::cout << fmt::format("Bar width configured: {}", w) << std::endl;
o->set_width(w);
o->setWidth(w);
}
static void handle_done(void *data, struct wl_output *)
@ -64,10 +63,10 @@ static void layer_surface_handle_closed(void *data,
struct zwlr_layer_surface_v1 *surface)
{
auto o = reinterpret_cast<waybar::Bar *>(data);
zwlr_layer_surface_v1_destroy(o->layer_surface);
o->layer_surface = NULL;
zwlr_layer_surface_v1_destroy(o->layerSurface);
o->layerSurface = nullptr;
wl_surface_destroy(o->surface);
o->surface = NULL;
o->surface = nullptr;
o->window.close();
}
@ -83,45 +82,46 @@ waybar::Bar::Bar(Client &client, std::unique_ptr<struct wl_output *> &&p_output)
wl_output_add_listener(*output, &outputListener, this);
window.set_title("waybar");
window.set_decorated(false);
// window.set_resizable(false);
setup_css();
setup_widgets();
_setupCss();
_setupWidgets();
gtk_widget_realize(GTK_WIDGET(window.gobj()));
GdkWindow *gdkWindow = gtk_widget_get_window(GTK_WIDGET(window.gobj()));
gdk_wayland_window_set_use_custom_surface(gdkWindow);
surface = gdk_wayland_window_get_wl_surface(gdkWindow);
layer_surface = zwlr_layer_shell_v1_get_layer_surface(
layerSurface = zwlr_layer_shell_v1_get_layer_surface(
client.layer_shell, surface, *output, ZWLR_LAYER_SHELL_V1_LAYER_TOP,
"waybar");
zwlr_layer_surface_v1_set_anchor(layer_surface,
ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT |ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP |
zwlr_layer_surface_v1_set_anchor(layerSurface,
ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT | ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP |
ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT);
zwlr_layer_surface_v1_set_size(layer_surface, width, client.height);
zwlr_layer_surface_v1_add_listener(layer_surface, &layerSurfaceListener,
zwlr_layer_surface_v1_set_size(layerSurface, _width, client.height);
zwlr_layer_surface_v1_add_listener(layerSurface, &layerSurfaceListener,
this);
wl_surface_commit(surface);
}
auto waybar::Bar::setup_css() -> void
auto waybar::Bar::_setupCss() -> void
{
css_provider = Gtk::CssProvider::create();
style_context = Gtk::StyleContext::create();
_cssProvider = Gtk::CssProvider::create();
_styleContext = Gtk::StyleContext::create();
// load our css file, wherever that may be hiding
if (css_provider->load_from_path(client.css_file))
if (_cssProvider->load_from_path(client.css_file))
{
Glib::RefPtr<Gdk::Screen> screen = window.get_screen();
style_context->add_provider_for_screen(screen, css_provider,
_styleContext->add_provider_for_screen(screen, _cssProvider,
GTK_STYLE_PROVIDER_PRIORITY_USER);
}
}
auto waybar::Bar::set_width(int width) -> void
auto waybar::Bar::setWidth(int width) -> void
{
this->width = width;
std::cout << fmt::format("Bar width configured: {}", width) << std::endl;
if (width == this->_width) return;
this->_width = width;
window.set_size_request(width);
window.resize(width, client.height);
zwlr_layer_surface_v1_set_size(layer_surface, width, 40);
zwlr_layer_surface_v1_set_size(layerSurface, width, 40);
wl_surface_commit(surface);
}
@ -129,11 +129,11 @@ auto waybar::Bar::toggle() -> void
{
visible = !visible;
auto zone = visible ? client.height : 0;
zwlr_layer_surface_v1_set_exclusive_zone(layer_surface, zone);
zwlr_layer_surface_v1_set_exclusive_zone(layerSurface, zone);
wl_surface_commit(surface);
}
auto waybar::Bar::setup_widgets() -> void
auto waybar::Bar::_setupWidgets() -> void
{
auto &left = *Gtk::manage(new Gtk::Box(Gtk::ORIENTATION_HORIZONTAL, 0));
auto &center = *Gtk::manage(new Gtk::Box(Gtk::ORIENTATION_HORIZONTAL, 0));

View File

@ -13,7 +13,7 @@ waybar::modules::Battery::Battery()
std::cerr << e.what() << std::endl;
}
_label.get_style_context()->add_class("battery-status");
_label.get_style_context()->add_class("battery");
_thread = [this] {
update();
@ -31,9 +31,9 @@ auto waybar::modules::Battery::update() -> void
std::ifstream(bat / "charge_full") >> full;
std::ifstream(bat / "status") >> status;
if (status == "Charging") {
_label.get_style_context()->add_class("battery-charging");
_label.get_style_context()->add_class("charging");
} else {
_label.get_style_context()->remove_class("battery-charging");
_label.get_style_context()->remove_class("charging");
}
int pct = float(now) / float(full) * 100.f;
_label.set_text_with_mnemonic(fmt::format("{}% {}", pct, ""));

View File

@ -2,7 +2,7 @@
waybar::modules::Clock::Clock()
{
_label.get_style_context()->add_class("clock-widget");
_label.get_style_context()->add_class("clock");
_thread = [this] {
auto now = waybar::chrono::clock::now();
auto t = std::time(nullptr);

View File

@ -3,15 +3,15 @@
waybar::modules::Cpu::Cpu()
{
_label.get_style_context()->add_class("cpu-widget");
_label.get_style_context()->add_class("cpu");
_thread = [this] {
struct sysinfo info;
if (!sysinfo(&info)) {
float f_load = 1.f / (1 << SI_LOAD_SHIFT);
_label.set_text(fmt::format("{:.{}f}% ",
info.loads[0] * f_load * 100 / get_nprocs(), 1));
info.loads[0] * f_load * 100 / get_nprocs(), 0));
}
_thread.sleep_for(chrono::seconds(30));
_thread.sleep_for(chrono::seconds(10));
};
};

View File

@ -3,13 +3,12 @@
waybar::modules::Memory::Memory()
{
_label.get_style_context()->add_class("memory-widget");
_label.get_style_context()->add_class("memory");
_thread = [this] {
struct sysinfo info;
if (!sysinfo(&info)) {
double available = (double)info.freeram / (double)info.totalram;
std::cout << available << std::endl;
_label.set_text(fmt::format("{:.{}f}% ", available * 100, 1));
_label.set_text(fmt::format("{:.{}f}% ", available * 100, 0));
}
_thread.sleep_for(chrono::seconds(30));
};

View File

@ -24,7 +24,7 @@ static const struct org_kde_kwin_idle_timeout_listener idle_timer_listener = {
waybar::modules::WorkspaceSelector::WorkspaceSelector(Bar &bar)
: thread(nullptr), _bar(bar), _box(Gtk::manage(new Gtk::Box))
{
_box->get_style_context()->add_class("workspace-selector");
_box->get_style_context()->add_class("workspaces");
std::string socketPath = get_socketpath();
_ipcSocketfd = ipc_open_socket(socketPath);
_ipcEventSocketfd = ipc_open_socket(socketPath);