From 9daf362df6ca723788c24a035339f671e11a8c84 Mon Sep 17 00:00:00 2001
From: 23rd <23rd@vivaldi.net>
Date: Tue, 11 Feb 2020 12:11:09 +0300
Subject: [PATCH] Added label with state to "Manage dictionaries" button.

---
 .../chat_helpers/spellchecker_common.cpp      | 38 +++++++++++++++++++
 .../chat_helpers/spellchecker_common.h        |  2 +
 .../settings/settings_advanced.cpp            |  4 +-
 3 files changed, 43 insertions(+), 1 deletion(-)

diff --git a/Telegram/SourceFiles/chat_helpers/spellchecker_common.cpp b/Telegram/SourceFiles/chat_helpers/spellchecker_common.cpp
index 07e6ef012..05fd9837a 100644
--- a/Telegram/SourceFiles/chat_helpers/spellchecker_common.cpp
+++ b/Telegram/SourceFiles/chat_helpers/spellchecker_common.cpp
@@ -171,6 +171,44 @@ bool WriteDefaultDictionary() {
 	return false;
 }
 
+rpl::producer<QString> ButtonManageDictsState(
+	not_null<Main::Session*> session) {
+	if (Platform::Spellchecker::IsSystemSpellchecker()) {
+		return rpl::single(QString());
+	}
+	const auto computeString = [=] {
+		if (!session->settings().spellcheckerEnabled()) {
+			return QString();
+		}
+		if (!session->settings().dictionariesEnabled().size()) {
+			return QString();
+		}
+		const auto dicts = session->settings().dictionariesEnabled();
+		const auto filtered = ranges::view::all(
+			dicts
+		) | ranges::views::filter(
+			DictionaryExists
+		) | ranges::to_vector;
+		const auto active = Platform::Spellchecker::ActiveLanguages();
+
+		return (active.size() == filtered.size())
+			? QString::number(filtered.size())
+			: tr::lng_contacts_loading(tr::now);
+	};
+	const auto emptyValue = [] { return rpl::empty_value(); };
+	return rpl::single(
+		computeString()
+	) | rpl::then(
+		rpl::merge(
+			Spellchecker::SupportedScriptsChanged(),
+			session->settings().dictionariesEnabledChanges(
+			) | rpl::map(emptyValue),
+			session->settings().spellcheckerEnabledChanges(
+			) | rpl::map(emptyValue)
+		) | rpl::map(computeString)
+	);
+}
+
 void Start(not_null<Main::Session*> session) {
 	Spellchecker::SetPhrases({ {
 		{ &ph::lng_spellchecker_add, tr::lng_spellchecker_add() },
diff --git a/Telegram/SourceFiles/chat_helpers/spellchecker_common.h b/Telegram/SourceFiles/chat_helpers/spellchecker_common.h
index db486ebde..e7c560923 100644
--- a/Telegram/SourceFiles/chat_helpers/spellchecker_common.h
+++ b/Telegram/SourceFiles/chat_helpers/spellchecker_common.h
@@ -34,6 +34,8 @@ bool WriteDefaultDictionary();
 std::vector<Dict> Dictionaries();
 
 void Start(not_null<Main::Session*> session);
+[[nodiscard]] rpl::producer<QString> ButtonManageDictsState(
+	not_null<Main::Session*> session);
 
 } // namespace Spellchecker
 
diff --git a/Telegram/SourceFiles/settings/settings_advanced.cpp b/Telegram/SourceFiles/settings/settings_advanced.cpp
index d2e61809d..3b250a21f 100644
--- a/Telegram/SourceFiles/settings/settings_advanced.cpp
+++ b/Telegram/SourceFiles/settings/settings_advanced.cpp
@@ -34,6 +34,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
 
 #ifndef TDESKTOP_DISABLE_SPELLCHECK
 #include "boxes/dictionaries_manager.h"
+#include "chat_helpers/spellchecker_common.h"
 #include "spellcheck/platform/platform_spellcheck.h"
 #endif // !TDESKTOP_DISABLE_SPELLCHECK
 
@@ -288,9 +289,10 @@ void SetupSpellchecker(
 			container,
 			object_ptr<Ui::VerticalLayout>(container)));
 
-	AddButton(
+	AddButtonWithLabel(
 		sliding->entity(),
 		tr::lng_settings_manage_dictionaries(),
+		Spellchecker::ButtonManageDictsState(session),
 		st::settingsButton
 	)->addClickHandler([=] {
 		Ui::show(Box<Ui::ManageDictionariesBox>(session));