Add custom host input that replaces commas with dots

This commit is contained in:
BugLight 2020-09-01 13:50:32 +03:00 committed by John Preston
parent 4c2779bbaf
commit 8d5e356733

View file

@ -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<QString> 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<QString> 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<Ui::RadioenumGroup<Type>> _type;
QPointer<Ui::SlideWrap<>> _aboutSponsored;
QPointer<Ui::InputField> _host;
QPointer<HostInput> _host;
QPointer<Ui::PortInput> _port;
QPointer<Ui::InputField> _user;
QPointer<Ui::PasswordInput> _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<Ui::InputField>(
_host = Ui::CreateChild<HostInput>(
address,
st::connectionHostInputField,
tr::lng_connection_host_ph(),