From 5d32ba586770f96cbf130253e275bd67bdb95e86 Mon Sep 17 00:00:00 2001 From: John Preston Date: Tue, 30 Jun 2020 12:56:06 +0400 Subject: [PATCH] Accept IPv4:port in proxy host input field. --- Telegram/SourceFiles/boxes/connection_box.cpp | 35 ++++++++++++++++--- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/Telegram/SourceFiles/boxes/connection_box.cpp b/Telegram/SourceFiles/boxes/connection_box.cpp index 69cfea8f2..7db93ead2 100644 --- a/Telegram/SourceFiles/boxes/connection_box.cpp +++ b/Telegram/SourceFiles/boxes/connection_box.cpp @@ -171,7 +171,7 @@ private: }; -class ProxyBox : public Ui::BoxContent { +class ProxyBox final : public Ui::BoxContent { public: ProxyBox( QWidget*, @@ -179,12 +179,14 @@ public: Fn callback, Fn shareCallback); -protected: - void prepare() override; - private: using Type = ProxyData::Type; + void prepare() override; + void setInnerFocus() override { + _host->setFocusFast(); + } + void refreshButtons(); ProxyData collectData(); void save(); @@ -765,6 +767,31 @@ ProxyBox::ProxyBox( void ProxyBox::prepare() { setTitle(tr::lng_proxy_edit()); + connect(_host.data(), &Ui::InputField::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() + && match.hasMatch()) { + const auto port = match.captured(1); + _port->setText(port); + _port->setCursorPosition(port.size()); + _port->setFocus(); + _host->setText(host.mid(0, host.size() - port.size() - 1)); + } + }); + }); + _port.data()->events( + ) | rpl::start_with_next([=](not_null e) { + if (e->type() == QEvent::KeyPress + && (static_cast(e.get())->key() == Qt::Key_Backspace) + && _port->cursorPosition() == 0) { + _host->setCursorPosition(_host->getLastText().size()); + _host->setFocus(); + } + }, _port->lifetime()); + refreshButtons(); setDimensionsToContent(st::boxWideWidth, _content); }