From 8d5e3567338594df34d0b411cd8eef56d3d0b311 Mon Sep 17 00:00:00 2001 From: BugLight Date: Tue, 1 Sep 2020 13:50:32 +0300 Subject: [PATCH] Add custom host input that replaces commas with dots --- Telegram/SourceFiles/boxes/connection_box.cpp | 50 +++++++++++++++++-- 1 file changed, 46 insertions(+), 4 deletions(-) diff --git a/Telegram/SourceFiles/boxes/connection_box.cpp b/Telegram/SourceFiles/boxes/connection_box.cpp index 49204b557..123341aa3 100644 --- a/Telegram/SourceFiles/boxes/connection_box.cpp +++ b/Telegram/SourceFiles/boxes/connection_box.cpp @@ -41,6 +41,48 @@ constexpr auto kSaveSettingsDelayedTimeout = crl::time(1000); using ProxyData = MTP::ProxyData; +class HostInput : public Ui::MaskedInputField { +public: + HostInput( + QWidget *parent, + const style::InputField &st, + rpl::producer placeholder, + const QString &val); + +protected: + void correctValue( + const QString &was, + int wasCursor, + QString &now, + int &nowCursor) override; +}; + +HostInput::HostInput( + QWidget *parent, + const style::InputField &st, + rpl::producer placeholder, + const QString &val) +: MaskedInputField(parent, st, std::move(placeholder), val) { +} + +void HostInput::correctValue( + const QString &was, + int wasCursor, + QString &now, + int &nowCursor) { + QString newText; + int newCursor = nowCursor; + newText.reserve(now.size()); + for (auto i = 0, l = now.size(); i < l; ++i) { + if (now[i] == ',') { + newText.append('.'); + } else { + newText.append(now[i]); + } + } + setCorrectedText(now, nowCursor, newText, newCursor); +} + class Base64UrlInput : public Ui::MaskedInputField { public: Base64UrlInput( @@ -209,7 +251,7 @@ private: std::shared_ptr> _type; QPointer> _aboutSponsored; - QPointer _host; + QPointer _host; QPointer _port; QPointer _user; QPointer _password; @@ -767,12 +809,12 @@ ProxyBox::ProxyBox( void ProxyBox::prepare() { setTitle(tr::lng_proxy_edit()); - connect(_host.data(), &Ui::InputField::changed, [=] { + connect(_host.data(), &HostInput::changed, [=] { Ui::PostponeCall(_host, [=] { const auto host = _host->getLastText().trimmed(); static const auto mask = u"^\\d+\\.\\d+\\.\\d+\\.\\d+:(\\d*)$"_q; const auto match = QRegularExpression(mask).match(host); - if (_host->textCursor().position() == host.size() + if (_host->cursorPosition() == host.size() && match.hasMatch()) { const auto port = match.captured(1); _port->setText(port); @@ -881,7 +923,7 @@ void ProxyBox::setupSocketAddress(const ProxyData &data) { _content, st::connectionHostInputField.heightMin), st::proxyEditInputPadding); - _host = Ui::CreateChild( + _host = Ui::CreateChild( address, st::connectionHostInputField, tr::lng_connection_host_ph(),