From a57e431437da8bae31cf4bde1ede57d7d21609b6 Mon Sep 17 00:00:00 2001 From: dmitry Date: Tue, 17 Aug 2021 05:28:41 +0300 Subject: [PATCH] Add shortDescription --- include/modules/sway/language.hpp | 3 ++- man/waybar-sway-language.5.scd | 6 ++++-- src/modules/sway/language.cpp | 21 ++++++++++++++++----- 3 files changed, 22 insertions(+), 8 deletions(-) diff --git a/include/modules/sway/language.hpp b/include/modules/sway/language.hpp index b310b7f3..5cceee6b 100644 --- a/include/modules/sway/language.hpp +++ b/include/modules/sway/language.hpp @@ -25,6 +25,7 @@ class Language : public ALabel, public sigc::trackable { std::string full_name; std::string short_name; std::string variant; + std::string short_description; }; class XKBContext { @@ -36,6 +37,7 @@ class Language : public ALabel, public sigc::trackable { rxkb_context* context_ = nullptr; rxkb_layout* xkb_layout_ = nullptr; Layout* layout_ = nullptr; + std::map base_layouts_by_name_; }; void onEvent(const struct Ipc::ipc_response&); @@ -50,7 +52,6 @@ class Language : public ALabel, public sigc::trackable { Layout layout_; std::string tooltip_format_ = ""; std::map layouts_map_; - XKBContext xkb_context_; bool is_variant_displayed; util::JsonParser parser_; diff --git a/man/waybar-sway-language.5.scd b/man/waybar-sway-language.5.scd index 76bc7208..92a647e6 100644 --- a/man/waybar-sway-language.5.scd +++ b/man/waybar-sway-language.5.scd @@ -29,11 +29,13 @@ Addressed by *sway/language* # FORMAT REPLACEMENTS -*{short}*: Short name of layout (e.g. "en"). Equals to {}. +*{short}*: Short name of layout (e.g. "us"). Equals to {}. + +*{shortDescription}*: Short description of layout (e.g. "en"). *{long}*: Long name of layout (e.g. "English (Dvorak)"). -*{variant}*: Variant of layout (e.g. "Dvorak"). +*{variant}*: Variant of layout (e.g. "dvorak"). # EXAMPLES diff --git a/src/modules/sway/language.cpp b/src/modules/sway/language.cpp index 5f3ce065..f7be4cbc 100644 --- a/src/modules/sway/language.cpp +++ b/src/modules/sway/language.cpp @@ -90,6 +90,7 @@ void Language::onEvent(const struct Ipc::ipc_response& res) { auto Language::update() -> void { auto display_layout = trim(fmt::format(format_, fmt::arg("short", layout_.short_name), + fmt::arg("shortDescription", layout_.short_description), fmt::arg("long", layout_.full_name), fmt::arg("variant", layout_.variant))); label_.set_markup(display_layout); @@ -97,10 +98,10 @@ auto Language::update() -> void { if (tooltip_format_ != "") { auto tooltip_display_layout = trim(fmt::format(tooltip_format_, fmt::arg("short", layout_.short_name), + fmt::arg("shortDescription", layout_.short_description), fmt::arg("long", layout_.full_name), fmt::arg("variant", layout_.variant))); label_.set_tooltip_markup(tooltip_display_layout); - } else { label_.set_tooltip_markup(display_layout); } @@ -118,8 +119,9 @@ auto Language::set_current_layout(std::string current_layout) -> void { auto Language::init_layouts_map(const std::vector& used_layouts) -> void { std::map> found_by_short_names; - auto layout = xkb_context_.next_layout(); - for (; layout != nullptr; layout = xkb_context_.next_layout()) { + XKBContext* xkb_context_ = new XKBContext(); + auto layout = xkb_context_->next_layout(); + for (; layout != nullptr; layout = xkb_context_->next_layout()) { if (std::find(used_layouts.begin(), used_layouts.end(), layout->full_name) == used_layouts.end()) { continue; @@ -136,6 +138,7 @@ auto Language::init_layouts_map(const std::vector& used_layouts) -> layouts_map_.emplace(layout->full_name, *layout); } + //delete xkb_context_; if (is_variant_displayed || found_by_short_names.size() == 0) { return; @@ -145,7 +148,6 @@ auto Language::init_layouts_map(const std::vector& used_layouts) -> for (const auto& used_layout_name : used_layouts) { auto used_layout = &layouts_map_.find(used_layout_name)->second; auto layouts_with_same_name_list = found_by_short_names[used_layout->short_name]; - spdlog::info("SIZE: " + std::to_string(layouts_with_same_name_list.size())); if (layouts_with_same_name_list.size() < 2) { continue; } @@ -180,8 +182,17 @@ auto Language::XKBContext::next_layout() -> Layout* { auto name = std::string(rxkb_layout_get_name(xkb_layout_)); auto variant_ = rxkb_layout_get_variant(xkb_layout_); std::string variant = variant_ == nullptr ? "" : std::string(variant_); + auto short_description_ = rxkb_layout_get_brief(xkb_layout_); + std::string short_description; + if (short_description_ != nullptr) { + short_description = std::string(short_description_); + base_layouts_by_name_.emplace(name, xkb_layout_); + } else { + auto base_layout = base_layouts_by_name_[name]; + short_description = base_layout == nullptr ? "" : std::string(rxkb_layout_get_brief(base_layout)); + } - layout_ = new Layout{description, name, variant}; + layout_ = new Layout{description, name, variant, short_description}; return layout_; }