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).
This commit is contained in:
Aleksei Bavshin 2021-11-19 19:29:51 -08:00
parent 03a641ed83
commit 6d2ba7a75b
No known key found for this signature in database
GPG Key ID: 4F071603387A382A
2 changed files with 63 additions and 20 deletions

View File

@ -36,6 +36,13 @@ struct bar_margins {
int left = 0; int left = 0;
}; };
struct bar_mode {
bar_layer layer;
bool exclusive;
bool passthrough;
bool visible;
};
class BarSurface { class BarSurface {
protected: protected:
BarSurface() = default; BarSurface() = default;
@ -54,18 +61,23 @@ class BarSurface {
class Bar { class Bar {
public: public:
using bar_mode_map = std::map<std::string_view, struct bar_mode>;
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(struct waybar_output *w_output, const Json::Value &);
Bar(const Bar &) = delete; Bar(const Bar &) = delete;
~Bar() = default; ~Bar() = default;
void setMode(const std::string &); void setMode(const std::string_view &);
void setVisible(bool visible); void setVisible(bool visible);
void toggle(); void toggle();
void handleSignal(int); void handleSignal(int);
struct waybar_output *output; struct waybar_output *output;
Json::Value config; Json::Value config;
struct wl_surface * surface; struct wl_surface *surface;
bool exclusive = true; bool exclusive = true;
bool visible = true; bool visible = true;
bool vertical = false; bool vertical = false;
@ -77,6 +89,11 @@ class Bar {
void getModules(const Factory &, const std::string &); void getModules(const Factory &, const std::string &);
void setupAltFormatKeyForModule(const std::string &module_name); void setupAltFormatKeyForModule(const std::string &module_name);
void setupAltFormatKeyForModuleList(const char *module_list_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<BarSurface> surface_impl_; std::unique_ptr<BarSurface> surface_impl_;
bar_layer layer_; bar_layer layer_;

View File

@ -23,6 +23,35 @@ static constexpr const char* BAR_SIZE_MSG = "Bar configured (width: {}, height:
static constexpr const char* SIZE_DEFINED = static constexpr const char* SIZE_DEFINED =
"{} size is defined in the config file so it will stay like that"; "{} 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 #ifdef HAVE_GTK_LAYER_SHELL
struct GLSSurfaceImpl : public BarSurface, public sigc::trackable { struct GLSSurfaceImpl : public BarSurface, public sigc::trackable {
GLSSurfaceImpl(Gtk::Window& window, struct waybar_output& output) : window_{window} { 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) { void waybar::Bar::setMode(const std::string_view& mode) {
bool passthrough = false; auto it = configured_modes.find(mode);
visible = true; if (it != configured_modes.end()) {
exclusive = true; last_mode_ = mode;
layer_ = bar_layer::BOTTOM; setMode(it->second);
} else {
if (mode == "hide") { spdlog::warn("Unknown mode \"{}\" requested", mode);
exclusive = false; last_mode_ = MODE_DEFAULT;
layer_ = bar_layer::TOP; setMode(configured_modes.at(MODE_DEFAULT));
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 struct bar_mode& mode) {
layer_ = mode.layer;
exclusive = mode.exclusive;
surface_impl_->setLayer(layer_); surface_impl_->setLayer(layer_);
surface_impl_->setExclusiveZone(exclusive); surface_impl_->setExclusiveZone(exclusive);
surface_impl_->setPassThrough(passthrough); surface_impl_->setPassThrough(mode.passthrough);
setVisible(visible); setVisible(mode.visible);
} }
void waybar::Bar::onMap(GdkEventAny*) { void waybar::Bar::onMap(GdkEventAny*) {