tdesktop/Telegram/SourceFiles/ui/search_field_controller.cpp

117 lines
2.7 KiB
C++
Raw Normal View History

/*
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/search_field_controller.h"
#include "styles/style_widgets.h"
#include "ui/wrap/padding_wrap.h"
#include "ui/widgets/fields/input_field.h"
#include "ui/widgets/shadow.h"
#include "ui/widgets/buttons.h"
#include "lang/lang_keys.h"
namespace Ui {
2017-11-03 15:47:08 +00:00
SearchFieldController::SearchFieldController(const QString &query)
: _query(query) {
}
2017-12-08 08:22:02 +00:00
auto SearchFieldController::createRowView(
QWidget *parent,
2017-12-08 08:22:02 +00:00
const style::SearchFieldRow &st) -> RowView {
auto result = base::make_unique_q<Ui::FixedHeightWidget>(
parent,
st.height);
2017-11-03 12:03:00 +00:00
auto wrap = result.get();
2017-11-03 12:03:00 +00:00
auto field = createField(wrap, st.field).release();
field->show();
field->cancelled(
) | rpl::start_with_next([=] {
2017-11-03 12:03:00 +00:00
field->setText(QString());
}, field->lifetime());
2017-11-03 12:03:00 +00:00
auto cancel = CreateChild<Ui::CrossButton>(
wrap,
st.fieldCancel);
cancel->addClickHandler([=] {
field->setText(QString());
});
queryValue(
) | rpl::map([](const QString &value) {
return !value.isEmpty();
}) | rpl::start_with_next([cancel](bool shown) {
cancel->toggle(shown, anim::type::normal);
}, cancel->lifetime());
cancel->finishAnimating();
2017-11-03 12:03:00 +00:00
auto shadow = CreateChild<Ui::PlainShadow>(wrap);
shadow->show();
wrap->widthValue(
) | rpl::start_with_next([=, &st](int newWidth) {
auto availableWidth = newWidth
- st.fieldIconSkip
- st.fieldCancelSkip;
field->setGeometryToLeft(
st.padding.left() + st.fieldIconSkip,
st.padding.top(),
availableWidth,
field->height());
cancel->moveToRight(0, 0);
shadow->setGeometry(
0,
st.height - st::lineWidth,
newWidth,
st::lineWidth);
}, wrap->lifetime());
wrap->paintRequest(
) | rpl::start_with_next([=, &st] {
2022-09-16 20:23:27 +00:00
auto p = QPainter(wrap);
st.fieldIcon.paint(
p,
st.padding.left(),
st.padding.top(),
wrap->width());
}, wrap->lifetime());
2017-11-03 12:03:00 +00:00
_view.release();
_view.reset(wrap);
2017-12-08 08:22:02 +00:00
return { std::move(result), field };
}
2017-11-21 09:20:56 +00:00
QString SearchFieldController::query() const {
return _query.current();
}
rpl::producer<QString> SearchFieldController::queryValue() const {
return _query.value();
}
rpl::producer<QString> SearchFieldController::queryChanges() const {
return _query.changes();
}
2017-11-03 12:03:00 +00:00
base::unique_qptr<Ui::InputField> SearchFieldController::createField(
QWidget *parent,
const style::InputField &st) {
auto result = base::make_unique_q<Ui::InputField>(
parent,
st,
tr::lng_dlg_filter(),
2017-11-03 12:03:00 +00:00
_query.current());
auto field = result.get();
field->changes(
) | rpl::start_with_next([=] {
2017-11-03 12:03:00 +00:00
_query = field->getLastText();
}, field->lifetime());
2017-11-03 12:03:00 +00:00
_view.reset(field);
return result;
}
} // namespace Ui