Merge pull request #2961 from alebastr/sway-language-single-layout

feat(sway/language): option to hide module with single layout
pull/2972/head
Alexis Rouillard 2024-02-25 17:10:50 +01:00 committed by GitHub
commit fb193529fc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 11 additions and 1 deletions

View File

@ -56,6 +56,7 @@ class Language : public ALabel, public sigc::trackable {
Layout layout_; Layout layout_;
std::string tooltip_format_ = ""; std::string tooltip_format_ = "";
std::map<std::string, Layout> layouts_map_; std::map<std::string, Layout> layouts_map_;
bool hide_single_;
bool is_variant_displayed; bool is_variant_displayed;
std::byte displayed_short_flag = static_cast<std::byte>(DispayedShortFlag::None); std::byte displayed_short_flag = static_cast<std::byte>(DispayedShortFlag::None);

View File

@ -17,6 +17,11 @@ Addressed by *sway/language*
default: {} ++ default: {} ++
The format, how layout should be displayed. The format, how layout should be displayed.
*hide-single-layout*: ++
typeof: bool ++
default: false ++
Defines visibility of the module if a single layout is configured
*tooltip-format*: ++ *tooltip-format*: ++
typeof: string ++ typeof: string ++
default: {} ++ default: {} ++

View File

@ -1,5 +1,4 @@
#include <spdlog/spdlog.h> #include <spdlog/spdlog.h>
#include <sys/sysctl.h> #include <sys/sysctl.h>
#include "modules/cpu_frequency.hpp" #include "modules/cpu_frequency.hpp"

View File

@ -19,6 +19,7 @@ const std::string Language::XKB_ACTIVE_LAYOUT_NAME_KEY = "xkb_active_layout_name
Language::Language(const std::string& id, const Json::Value& config) Language::Language(const std::string& id, const Json::Value& config)
: ALabel(config, "language", id, "{}", 0, true) { : ALabel(config, "language", id, "{}", 0, true) {
hide_single_ = config["hide-single-layout"].isBool() && config["hide-single-layout"].asBool();
is_variant_displayed = format_.find("{variant}") != std::string::npos; is_variant_displayed = format_.find("{variant}") != std::string::npos;
if (format_.find("{}") != std::string::npos || format_.find("{short}") != std::string::npos) { if (format_.find("{}") != std::string::npos || format_.find("{short}") != std::string::npos) {
displayed_short_flag |= static_cast<std::byte>(DispayedShortFlag::ShortName); displayed_short_flag |= static_cast<std::byte>(DispayedShortFlag::ShortName);
@ -95,6 +96,10 @@ void Language::onEvent(const struct Ipc::ipc_response& res) {
auto Language::update() -> void { auto Language::update() -> void {
std::lock_guard<std::mutex> lock(mutex_); std::lock_guard<std::mutex> lock(mutex_);
if (hide_single_ && layouts_map_.size() <= 1) {
event_box_.hide();
return;
}
auto display_layout = trim(fmt::format( auto display_layout = trim(fmt::format(
fmt::runtime(format_), fmt::arg("short", layout_.short_name), fmt::runtime(format_), fmt::arg("short", layout_.short_name),
fmt::arg("shortDescription", layout_.short_description), fmt::arg("long", layout_.full_name), fmt::arg("shortDescription", layout_.short_description), fmt::arg("long", layout_.full_name),