diff --git a/Telegram/SourceFiles/ui/widgets/fields/time_part_input_with_placeholder.cpp b/Telegram/SourceFiles/ui/widgets/fields/time_part_input_with_placeholder.cpp new file mode 100644 index 000000000..2f09f39ee --- /dev/null +++ b/Telegram/SourceFiles/ui/widgets/fields/time_part_input_with_placeholder.cpp @@ -0,0 +1,62 @@ +/* +This file is part of Telegram Desktop, +the official desktop application for the Telegram messaging service. + +For license and copyright information please follow this link: +https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL +*/ +#include "ui/widgets/fields/time_part_input_with_placeholder.h" + +#include "lang/lang_numbers_animation.h" + +namespace Ui { + +void TimePartWithPlaceholder::setPhrase( + const tr::phrase &phrase) { + _phrase = phrase; +} + +void TimePartWithPlaceholder::paintAdditionalPlaceholder(Painter &p) { + maybeUpdatePlaceholder(); + + p.setClipRect(rect()); + const auto phRect = placeholderRect(); + + if (_lastPlaceholder.width < phRect.width()) { + placeholderAdditionalPrepare(p); + p.drawText( + phRect.translated(-_lastPlaceholder.leftOffset, 0), + _lastPlaceholder.text, + style::al_left); + } + +} + +void TimePartWithPlaceholder::maybeUpdatePlaceholder() { + const auto displayedText = getDisplayedText(); + if (displayedText == _lastPlaceholder.displayedText) { + return; + } + const auto count = displayedText.toUInt(); + const auto textWithOffset = _phrase( + tr::now, + lt_count, + count, + Ui::StringWithNumbers::FromString); + _lastPlaceholder = { + .width = phFont()->width(textWithOffset.text), + .text = textWithOffset.text, + .leftOffset = phFont()->width( + textWithOffset.text.mid(0, textWithOffset.offset)), + .displayedText = displayedText, + }; + if (displayedText.size() > 1 && displayedText.startsWith(_zero)) { + _lastPlaceholder.text.insert(textWithOffset.offset, _zero); + } + + const auto leftMargins = (width() - _lastPlaceholder.width) / 2 + + _lastPlaceholder.leftOffset; + setTextMargins({ leftMargins, 0, 0, 0 }); +} + +} // namespace Ui diff --git a/Telegram/SourceFiles/ui/widgets/fields/time_part_input_with_placeholder.h b/Telegram/SourceFiles/ui/widgets/fields/time_part_input_with_placeholder.h new file mode 100644 index 000000000..98f448948 --- /dev/null +++ b/Telegram/SourceFiles/ui/widgets/fields/time_part_input_with_placeholder.h @@ -0,0 +1,40 @@ +/* +This file is part of Telegram Desktop, +the official desktop application for the Telegram messaging service. + +For license and copyright information please follow this link: +https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL +*/ +#pragma once + +#include "ui/widgets/fields/time_part_input.h" + +#include "lang_auto.h" + +namespace Ui { + +class TimePartWithPlaceholder final : public TimePart { +public: + using Ui::TimePart::TimePart; + + void setPhrase(const tr::phrase &phrase); + +protected: + void paintAdditionalPlaceholder(Painter &p) override; + +private: + void maybeUpdatePlaceholder(); + + const QChar _zero = QChar('0'); + tr::phrase _phrase; + + struct { + int width = 0; + QString text; + int leftOffset = 0; + QString displayedText; + } _lastPlaceholder; + +}; + +} // namespace Ui diff --git a/Telegram/cmake/td_ui.cmake b/Telegram/cmake/td_ui.cmake index a6e08c02e..69bbf3d8d 100644 --- a/Telegram/cmake/td_ui.cmake +++ b/Telegram/cmake/td_ui.cmake @@ -239,6 +239,8 @@ PRIVATE ui/grouped_layout.h ui/widgets/fields/special_fields.cpp ui/widgets/fields/special_fields.h + ui/widgets/fields/time_part_input_with_placeholder.cpp + ui/widgets/fields/time_part_input_with_placeholder.h ui/ui_pch.h ) diff --git a/Telegram/lib_ui b/Telegram/lib_ui index 62002f034..9f28a0b10 160000 --- a/Telegram/lib_ui +++ b/Telegram/lib_ui @@ -1 +1 @@ -Subproject commit 62002f034bbfd3e5f78a71317f37c685a8846b1f +Subproject commit 9f28a0b106311566d2adab8a7b82973accfdd944