From 6d2ba7a75b2399d38c8ee8719f172ec09ec298fc Mon Sep 17 00:00:00 2001 From: Aleksei Bavshin Date: Fri, 19 Nov 2021 19:29:51 -0800 Subject: [PATCH] feat(bar): store modes as a map of presets This allows to apply the mode atomically and adds possibility of defining custom modes (to be implemented). --- include/bar.hpp | 21 +++++++++++++++-- src/bar.cpp | 62 +++++++++++++++++++++++++++++++++++-------------- 2 files changed, 63 insertions(+), 20 deletions(-) diff --git a/include/bar.hpp b/include/bar.hpp index 1f90f9a6..426c55c2 100644 --- a/include/bar.hpp +++ b/include/bar.hpp @@ -36,6 +36,13 @@ struct bar_margins { int left = 0; }; +struct bar_mode { + bar_layer layer; + bool exclusive; + bool passthrough; + bool visible; +}; + class BarSurface { protected: BarSurface() = default; @@ -54,18 +61,23 @@ class BarSurface { class Bar { public: + using bar_mode_map = std::map; + static const bar_mode_map PRESET_MODES; + static const std::string_view MODE_DEFAULT; + static const std::string_view MODE_INVISIBLE; + Bar(struct waybar_output *w_output, const Json::Value &); Bar(const Bar &) = delete; ~Bar() = default; - void setMode(const std::string &); + void setMode(const std::string_view &); void setVisible(bool visible); void toggle(); void handleSignal(int); struct waybar_output *output; Json::Value config; - struct wl_surface * surface; + struct wl_surface *surface; bool exclusive = true; bool visible = true; bool vertical = false; @@ -77,6 +89,11 @@ class Bar { void getModules(const Factory &, const std::string &); void setupAltFormatKeyForModule(const std::string &module_name); void setupAltFormatKeyForModuleList(const char *module_list_name); + void setMode(const bar_mode &); + + /* Copy initial set of modes to allow customization */ + bar_mode_map configured_modes = PRESET_MODES; + std::string last_mode_{MODE_DEFAULT}; std::unique_ptr surface_impl_; bar_layer layer_; diff --git a/src/bar.cpp b/src/bar.cpp index 3ce59e3a..2f8bc27b 100644 --- a/src/bar.cpp +++ b/src/bar.cpp @@ -23,6 +23,35 @@ static constexpr const char* BAR_SIZE_MSG = "Bar configured (width: {}, height: static constexpr const char* SIZE_DEFINED = "{} size is defined in the config file so it will stay like that"; +const Bar::bar_mode_map Bar::PRESET_MODES = { // + {"dock", + {// Modes supported by the sway config; see man sway-bar(5) + .layer = bar_layer::BOTTOM, + .exclusive = true, + .passthrough = false, + .visible = true}}, + {"hide", + {// + .layer = bar_layer::TOP, + .exclusive = false, + .passthrough = false, + .visible = true}}, + {"invisible", + {// + .layer = bar_layer::BOTTOM, + .exclusive = false, + .passthrough = true, + .visible = false}}, + {"overlay", + {// + .layer = bar_layer::TOP, + .exclusive = false, + .passthrough = true, + .visible = true}}}; + +const std::string_view Bar::MODE_DEFAULT = "dock"; +const std::string_view Bar::MODE_INVISIBLE = "invisible"; + #ifdef HAVE_GTK_LAYER_SHELL struct GLSSurfaceImpl : public BarSurface, public sigc::trackable { GLSSurfaceImpl(Gtk::Window& window, struct waybar_output& output) : window_{window} { @@ -533,28 +562,25 @@ waybar::Bar::Bar(struct waybar_output* w_output, const Json::Value& w_config) } } -void waybar::Bar::setMode(const std::string& mode) { - bool passthrough = false; - visible = true; - exclusive = true; - layer_ = bar_layer::BOTTOM; - - if (mode == "hide") { - exclusive = false; - layer_ = bar_layer::TOP; - visible = false; - } else if (mode == "invisible") { - visible = false; - } else if (mode == "overlay") { - exclusive = false; - layer_ = bar_layer::TOP; - passthrough = true; +void waybar::Bar::setMode(const std::string_view& mode) { + auto it = configured_modes.find(mode); + if (it != configured_modes.end()) { + last_mode_ = mode; + setMode(it->second); + } else { + spdlog::warn("Unknown mode \"{}\" requested", mode); + last_mode_ = MODE_DEFAULT; + setMode(configured_modes.at(MODE_DEFAULT)); } +} +void waybar::Bar::setMode(const struct bar_mode& mode) { + layer_ = mode.layer; + exclusive = mode.exclusive; surface_impl_->setLayer(layer_); surface_impl_->setExclusiveZone(exclusive); - surface_impl_->setPassThrough(passthrough); - setVisible(visible); + surface_impl_->setPassThrough(mode.passthrough); + setVisible(mode.visible); } void waybar::Bar::onMap(GdkEventAny*) {