feat(sway/language): option to hide module with single layout

pull/2961/head
Aleksei Bavshin 2024-02-21 20:22:35 -08:00
parent 99c48bca36
commit 188789592e
No known key found for this signature in database
GPG Key ID: 4F071603387A382A
3 changed files with 11 additions and 0 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

@ -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),