2018-10-02 20:39:54 +00:00
|
|
|
/*
|
|
|
|
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 "support/support_autocomplete.h"
|
|
|
|
|
2021-08-26 15:02:21 +00:00
|
|
|
#include "ui/chat/chat_theme.h"
|
2021-08-27 20:44:47 +00:00
|
|
|
#include "ui/chat/chat_style.h"
|
2018-10-02 20:39:54 +00:00
|
|
|
#include "ui/widgets/scroll_area.h"
|
|
|
|
#include "ui/widgets/input_fields.h"
|
2018-10-08 14:08:17 +00:00
|
|
|
#include "ui/widgets/buttons.h"
|
2018-10-02 20:39:54 +00:00
|
|
|
#include "ui/wrap/padding_wrap.h"
|
2022-09-16 20:23:27 +00:00
|
|
|
#include "ui/painter.h"
|
2018-10-02 20:39:54 +00:00
|
|
|
#include "support/support_templates.h"
|
2018-10-08 14:08:17 +00:00
|
|
|
#include "support/support_common.h"
|
2018-10-05 16:02:33 +00:00
|
|
|
#include "history/view/history_view_message.h"
|
|
|
|
#include "history/view/history_view_service_message.h"
|
2022-12-14 12:15:46 +00:00
|
|
|
#include "history/history_item.h"
|
2018-10-05 16:02:33 +00:00
|
|
|
#include "lang/lang_keys.h"
|
2019-07-10 17:28:33 +00:00
|
|
|
#include "base/unixtime.h"
|
2019-09-26 10:55:35 +00:00
|
|
|
#include "base/call_delayed.h"
|
2019-07-24 11:45:24 +00:00
|
|
|
#include "main/main_session.h"
|
2020-06-18 12:47:09 +00:00
|
|
|
#include "main/main_session_settings.h"
|
2018-10-05 16:02:33 +00:00
|
|
|
#include "apiwrap.h"
|
2021-08-27 20:44:47 +00:00
|
|
|
#include "window/window_session_controller.h"
|
2018-10-02 20:39:54 +00:00
|
|
|
#include "styles/style_chat_helpers.h"
|
|
|
|
#include "styles/style_window.h"
|
2019-09-18 11:19:05 +00:00
|
|
|
#include "styles/style_layers.h"
|
2018-10-02 20:39:54 +00:00
|
|
|
|
|
|
|
namespace Support {
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class Inner : public Ui::RpWidget {
|
|
|
|
public:
|
|
|
|
Inner(QWidget *parent);
|
|
|
|
|
|
|
|
using Question = details::TemplatesQuestion;
|
|
|
|
void showRows(std::vector<Question> &&rows);
|
|
|
|
|
|
|
|
std::pair<int, int> moveSelection(int delta);
|
|
|
|
|
|
|
|
std::optional<Question> selected() const;
|
|
|
|
|
|
|
|
auto activated() const {
|
|
|
|
return _activated.events();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
void paintEvent(QPaintEvent *e) override;
|
|
|
|
void mouseMoveEvent(QMouseEvent *e) override;
|
|
|
|
void mousePressEvent(QMouseEvent *e) override;
|
|
|
|
void mouseReleaseEvent(QMouseEvent *e) override;
|
|
|
|
void leaveEventHook(QEvent *e) override;
|
|
|
|
|
|
|
|
int resizeGetHeight(int newWidth) override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
struct Row {
|
|
|
|
Question data;
|
2019-06-12 13:26:04 +00:00
|
|
|
Ui::Text::String question = { st::windowMinWidth / 2 };
|
|
|
|
Ui::Text::String keys = { st::windowMinWidth / 2 };
|
|
|
|
Ui::Text::String answer = { st::windowMinWidth / 2 };
|
2018-10-02 20:39:54 +00:00
|
|
|
int top = 0;
|
|
|
|
int height = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
void prepareRow(Row &row);
|
|
|
|
int resizeRowGetHeight(Row &row, int newWidth);
|
|
|
|
void setSelected(int selected);
|
|
|
|
|
|
|
|
std::vector<Row> _rows;
|
|
|
|
int _selected = -1;
|
|
|
|
int _pressed = -1;
|
|
|
|
bool _selectByKeys = false;
|
2018-10-11 11:17:51 +00:00
|
|
|
rpl::event_stream<> _activated;
|
2018-10-02 20:39:54 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2019-06-12 13:26:04 +00:00
|
|
|
int TextHeight(const Ui::Text::String &text, int available, int lines) {
|
2018-10-07 09:33:10 +00:00
|
|
|
Expects(text.style() != nullptr);
|
|
|
|
|
|
|
|
const auto st = text.style();
|
|
|
|
const auto line = st->lineHeight ? st->lineHeight : st->font->height;
|
|
|
|
return std::min(text.countHeight(available), lines * line);
|
|
|
|
};
|
|
|
|
|
2018-10-02 20:39:54 +00:00
|
|
|
Inner::Inner(QWidget *parent) : RpWidget(parent) {
|
|
|
|
setMouseTracking(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Inner::showRows(std::vector<Question> &&rows) {
|
|
|
|
_rows.resize(0);
|
|
|
|
_rows.reserve(rows.size());
|
|
|
|
for (auto &row : rows) {
|
|
|
|
_rows.push_back({ std::move(row) });
|
|
|
|
auto &added = _rows.back();
|
|
|
|
prepareRow(added);
|
|
|
|
}
|
|
|
|
resizeToWidth(width());
|
|
|
|
_selected = _pressed = -1;
|
2018-10-09 13:32:32 +00:00
|
|
|
moveSelection(1);
|
|
|
|
update();
|
2018-10-02 20:39:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::pair<int, int> Inner::moveSelection(int delta) {
|
|
|
|
const auto selected = _selected + delta;
|
|
|
|
if (selected >= 0 && selected < _rows.size()) {
|
|
|
|
_selectByKeys = true;
|
|
|
|
setSelected(selected);
|
|
|
|
const auto top = _rows[_selected].top;
|
|
|
|
return { top, top + _rows[_selected].height };
|
|
|
|
}
|
|
|
|
return { -1, -1 };
|
|
|
|
}
|
|
|
|
|
|
|
|
auto Inner::selected() const -> std::optional<Question> {
|
|
|
|
if (_rows.empty()) {
|
|
|
|
return std::nullopt;
|
|
|
|
} else if (_selected < 0) {
|
|
|
|
return _rows[0].data;
|
|
|
|
}
|
|
|
|
return _rows[_selected].data;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Inner::prepareRow(Row &row) {
|
|
|
|
row.question.setText(st::autocompleteRowTitle, row.data.question);
|
|
|
|
row.keys.setText(
|
|
|
|
st::autocompleteRowKeys,
|
2022-11-26 21:20:17 +00:00
|
|
|
row.data.originalKeys.join(u", "_q));
|
2018-10-02 20:39:54 +00:00
|
|
|
row.answer.setText(st::autocompleteRowAnswer, row.data.value);
|
|
|
|
}
|
|
|
|
|
|
|
|
int Inner::resizeRowGetHeight(Row &row, int newWidth) {
|
|
|
|
const auto available = newWidth
|
|
|
|
- st::autocompleteRowPadding.left()
|
|
|
|
- st::autocompleteRowPadding.right();
|
|
|
|
return row.height = st::autocompleteRowPadding.top()
|
2018-10-07 09:33:10 +00:00
|
|
|
+ TextHeight(row.question, available, 1)
|
|
|
|
+ TextHeight(row.keys, available, 1)
|
|
|
|
+ TextHeight(row.answer, available, 2)
|
2018-10-02 20:39:54 +00:00
|
|
|
+ st::autocompleteRowPadding.bottom()
|
|
|
|
+ st::lineWidth;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Inner::resizeGetHeight(int newWidth) {
|
|
|
|
auto top = 0;
|
|
|
|
for (auto &row : _rows) {
|
|
|
|
row.top = top;
|
|
|
|
top += resizeRowGetHeight(row, newWidth);
|
|
|
|
}
|
|
|
|
return top ? (top - st::lineWidth) : (3 * st::mentionHeight);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Inner::paintEvent(QPaintEvent *e) {
|
|
|
|
Painter p(this);
|
|
|
|
|
|
|
|
if (_rows.empty()) {
|
|
|
|
p.setFont(st::boxTextFont);
|
|
|
|
p.setPen(st::windowSubTextFg);
|
|
|
|
p.drawText(
|
|
|
|
rect(),
|
|
|
|
"Search by question, keys or value",
|
|
|
|
style::al_center);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto clip = e->rect();
|
|
|
|
const auto from = ranges::upper_bound(
|
|
|
|
_rows,
|
|
|
|
clip.y(),
|
|
|
|
std::less<>(),
|
|
|
|
[](const Row &row) { return row.top + row.height; });
|
|
|
|
const auto till = ranges::lower_bound(
|
|
|
|
_rows,
|
|
|
|
clip.y() + clip.height(),
|
|
|
|
std::less<>(),
|
|
|
|
[](const Row &row) { return row.top; });
|
|
|
|
if (from == end(_rows)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
p.translate(0, from->top);
|
|
|
|
const auto padding = st::autocompleteRowPadding;
|
|
|
|
const auto available = width() - padding.left() - padding.right();
|
|
|
|
auto top = padding.top();
|
2019-06-12 13:26:04 +00:00
|
|
|
const auto drawText = [&](const Ui::Text::String &text, int lines) {
|
2018-10-07 09:33:10 +00:00
|
|
|
text.drawLeftElided(
|
2018-10-02 20:39:54 +00:00
|
|
|
p,
|
|
|
|
padding.left(),
|
|
|
|
top,
|
|
|
|
available,
|
2018-10-07 09:33:10 +00:00
|
|
|
width(),
|
|
|
|
lines);
|
|
|
|
top += TextHeight(text, available, lines);
|
2018-10-02 20:39:54 +00:00
|
|
|
};
|
|
|
|
for (auto i = from; i != till; ++i) {
|
|
|
|
const auto over = (i - begin(_rows) == _selected);
|
|
|
|
if (over) {
|
|
|
|
p.fillRect(0, 0, width(), i->height, st::windowBgOver);
|
|
|
|
}
|
|
|
|
p.setPen(st::mentionNameFg);
|
2018-10-07 09:33:10 +00:00
|
|
|
drawText(i->question, 1);
|
2018-10-02 20:39:54 +00:00
|
|
|
p.setPen(over ? st::mentionFgOver : st::mentionFg);
|
2018-10-07 09:33:10 +00:00
|
|
|
drawText(i->keys, 1);
|
2018-10-02 20:39:54 +00:00
|
|
|
p.setPen(st::windowFg);
|
2018-10-07 09:33:10 +00:00
|
|
|
drawText(i->answer, 2);
|
2018-10-02 20:39:54 +00:00
|
|
|
|
|
|
|
p.translate(0, i->height);
|
|
|
|
top = padding.top();
|
|
|
|
|
|
|
|
if (i - begin(_rows) + 1 == _selected) {
|
|
|
|
p.fillRect(
|
|
|
|
0,
|
|
|
|
-st::lineWidth,
|
|
|
|
width(),
|
|
|
|
st::lineWidth,
|
|
|
|
st::windowBgOver);
|
|
|
|
} else if (!over) {
|
|
|
|
p.fillRect(
|
|
|
|
padding.left(),
|
|
|
|
-st::lineWidth,
|
|
|
|
available,
|
|
|
|
st::lineWidth,
|
|
|
|
st::shadowFg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Inner::mouseMoveEvent(QMouseEvent *e) {
|
|
|
|
static auto lastGlobalPos = QPoint();
|
|
|
|
const auto moved = (e->globalPos() != lastGlobalPos);
|
|
|
|
if (!moved && _selectByKeys) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_selectByKeys = false;
|
|
|
|
lastGlobalPos = e->globalPos();
|
|
|
|
const auto i = ranges::upper_bound(
|
|
|
|
_rows,
|
|
|
|
e->pos().y(),
|
|
|
|
std::less<>(),
|
|
|
|
[](const Row &row) { return row.top + row.height; });
|
|
|
|
setSelected((i == end(_rows)) ? -1 : (i - begin(_rows)));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Inner::leaveEventHook(QEvent *e) {
|
|
|
|
setSelected(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Inner::setSelected(int selected) {
|
|
|
|
if (_selected != selected) {
|
|
|
|
_selected = selected;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Inner::mousePressEvent(QMouseEvent *e) {
|
|
|
|
_pressed = _selected;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Inner::mouseReleaseEvent(QMouseEvent *e) {
|
|
|
|
const auto pressed = base::take(_pressed);
|
|
|
|
if (pressed == _selected && pressed >= 0) {
|
2018-10-11 11:17:51 +00:00
|
|
|
_activated.fire({});
|
2018-10-02 20:39:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-05 16:02:33 +00:00
|
|
|
AdminLog::OwnedItem GenerateCommentItem(
|
|
|
|
not_null<HistoryView::ElementDelegate*> delegate,
|
|
|
|
not_null<History*> history,
|
|
|
|
const Contact &data) {
|
|
|
|
if (data.comment.isEmpty()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2021-07-28 11:55:02 +00:00
|
|
|
const auto flags = MessageFlag::HasFromId
|
|
|
|
| MessageFlag::Outgoing
|
|
|
|
| MessageFlag::FakeHistoryItem;
|
2023-05-25 09:32:13 +00:00
|
|
|
const auto replyTo = FullReplyTo();
|
2021-07-28 11:55:02 +00:00
|
|
|
const auto viaBotId = UserId();
|
|
|
|
const auto groupedId = uint64();
|
2020-02-20 08:45:25 +00:00
|
|
|
const auto item = history->makeMessage(
|
2021-09-30 11:30:39 +00:00
|
|
|
history->nextNonHistoryEntryId(),
|
2018-10-05 16:02:33 +00:00
|
|
|
flags,
|
|
|
|
replyTo,
|
|
|
|
viaBotId,
|
2019-07-10 17:28:33 +00:00
|
|
|
base::unixtime::now(),
|
2019-01-17 08:18:23 +00:00
|
|
|
history->session().userId(),
|
2018-10-05 16:02:33 +00:00
|
|
|
QString(),
|
2022-01-06 23:04:26 +00:00
|
|
|
TextWithEntities{ data.comment },
|
2021-07-28 11:55:02 +00:00
|
|
|
MTP_messageMediaEmpty(),
|
2021-10-02 11:28:21 +00:00
|
|
|
HistoryMessageMarkupData(),
|
2021-07-28 11:55:02 +00:00
|
|
|
groupedId);
|
2018-10-05 16:02:33 +00:00
|
|
|
return AdminLog::OwnedItem(delegate, item);
|
|
|
|
}
|
|
|
|
|
|
|
|
AdminLog::OwnedItem GenerateContactItem(
|
|
|
|
not_null<HistoryView::ElementDelegate*> delegate,
|
|
|
|
not_null<History*> history,
|
|
|
|
const Contact &data) {
|
2023-05-25 09:32:13 +00:00
|
|
|
const auto replyTo = FullReplyTo();
|
2021-07-28 11:55:02 +00:00
|
|
|
const auto viaBotId = UserId();
|
|
|
|
const auto postAuthor = QString();
|
|
|
|
const auto groupedId = uint64();
|
|
|
|
const auto item = history->makeMessage(
|
2021-09-30 11:30:39 +00:00
|
|
|
history->nextNonHistoryEntryId(),
|
2021-07-28 11:55:02 +00:00
|
|
|
(MessageFlag::HasFromId
|
|
|
|
| MessageFlag::Outgoing
|
|
|
|
| MessageFlag::FakeHistoryItem),
|
|
|
|
replyTo,
|
|
|
|
viaBotId,
|
|
|
|
base::unixtime::now(),
|
|
|
|
history->session().userPeerId(),
|
|
|
|
postAuthor,
|
|
|
|
TextWithEntities(),
|
2018-10-05 16:02:33 +00:00
|
|
|
MTP_messageMediaContact(
|
|
|
|
MTP_string(data.phone),
|
|
|
|
MTP_string(data.firstName),
|
|
|
|
MTP_string(data.lastName),
|
2021-07-28 11:55:02 +00:00
|
|
|
MTP_string(), // vcard
|
2021-08-25 08:15:05 +00:00
|
|
|
MTP_long(0)), // user_id
|
2021-10-02 11:28:21 +00:00
|
|
|
HistoryMessageMarkupData(),
|
2021-07-28 11:55:02 +00:00
|
|
|
groupedId);
|
2018-10-05 16:02:33 +00:00
|
|
|
return AdminLog::OwnedItem(delegate, item);
|
|
|
|
}
|
|
|
|
|
2018-10-02 20:39:54 +00:00
|
|
|
} // namespace
|
|
|
|
|
2019-07-24 11:45:24 +00:00
|
|
|
Autocomplete::Autocomplete(QWidget *parent, not_null<Main::Session*> session)
|
2018-10-02 20:39:54 +00:00
|
|
|
: RpWidget(parent)
|
|
|
|
, _session(session) {
|
|
|
|
setupContent();
|
|
|
|
}
|
|
|
|
|
2018-10-11 11:17:51 +00:00
|
|
|
void Autocomplete::activate(not_null<Ui::InputField*> field) {
|
2018-11-15 15:36:04 +00:00
|
|
|
if (_session->settings().supportTemplatesAutocomplete()) {
|
2018-10-11 11:17:51 +00:00
|
|
|
_activate();
|
|
|
|
} else {
|
2018-11-15 15:36:04 +00:00
|
|
|
const auto &templates = _session->supportTemplates();
|
|
|
|
const auto max = templates.maxKeyLength();
|
2018-10-11 11:17:51 +00:00
|
|
|
auto cursor = field->textCursor();
|
|
|
|
const auto position = cursor.position();
|
|
|
|
const auto anchor = cursor.anchor();
|
|
|
|
const auto text = (position != anchor)
|
|
|
|
? field->getTextWithTagsPart(
|
|
|
|
std::min(position, anchor),
|
|
|
|
std::max(position, anchor))
|
|
|
|
: field->getTextWithTagsPart(
|
|
|
|
std::max(position - max, 0),
|
|
|
|
position);
|
|
|
|
const auto result = (position != anchor)
|
2018-11-15 15:36:04 +00:00
|
|
|
? templates.matchExact(text.text)
|
|
|
|
: templates.matchFromEnd(text.text);
|
2018-10-11 11:17:51 +00:00
|
|
|
if (result) {
|
|
|
|
const auto till = std::max(position, anchor);
|
|
|
|
const auto from = till - result->key.size();
|
|
|
|
cursor.setPosition(from);
|
|
|
|
cursor.setPosition(till, QTextCursor::KeepAnchor);
|
|
|
|
field->setTextCursor(cursor);
|
|
|
|
submitValue(result->question.value);
|
|
|
|
}
|
|
|
|
}
|
2018-10-02 20:39:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Autocomplete::deactivate() {
|
|
|
|
_deactivate();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Autocomplete::setBoundings(QRect rect) {
|
|
|
|
const auto maxHeight = int(4.5 * st::mentionHeight);
|
|
|
|
const auto height = std::min(rect.height(), maxHeight);
|
|
|
|
setGeometry(
|
|
|
|
rect.x(),
|
|
|
|
rect.y() + rect.height() - height,
|
|
|
|
rect.width(),
|
|
|
|
height);
|
|
|
|
}
|
|
|
|
|
2018-10-05 16:02:33 +00:00
|
|
|
rpl::producer<QString> Autocomplete::insertRequests() const {
|
2018-10-02 20:39:54 +00:00
|
|
|
return _insertRequests.events();
|
|
|
|
}
|
|
|
|
|
2018-10-05 16:02:33 +00:00
|
|
|
rpl::producer<Contact> Autocomplete::shareContactRequests() const {
|
|
|
|
return _shareContactRequests.events();
|
|
|
|
}
|
|
|
|
|
2018-10-02 20:39:54 +00:00
|
|
|
void Autocomplete::keyPressEvent(QKeyEvent *e) {
|
|
|
|
if (e->key() == Qt::Key_Up) {
|
|
|
|
_moveSelection(-1);
|
|
|
|
} else if (e->key() == Qt::Key_Down) {
|
|
|
|
_moveSelection(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Autocomplete::setupContent() {
|
|
|
|
const auto inputWrap = Ui::CreateChild<Ui::PaddingWrap<Ui::InputField>>(
|
|
|
|
this,
|
|
|
|
object_ptr<Ui::InputField>(
|
|
|
|
this,
|
2023-01-24 13:08:35 +00:00
|
|
|
st::defaultMultiSelectSearchField,
|
2022-11-29 21:46:36 +00:00
|
|
|
rpl::single(u"Search for templates"_q)), // #TODO hard_lang
|
2018-10-02 20:39:54 +00:00
|
|
|
st::autocompleteSearchPadding);
|
|
|
|
const auto input = inputWrap->entity();
|
2021-04-23 11:07:12 +00:00
|
|
|
const auto scroll = Ui::CreateChild<Ui::ScrollArea>(this);
|
2018-10-02 20:39:54 +00:00
|
|
|
|
|
|
|
const auto inner = scroll->setOwnedWidget(object_ptr<Inner>(scroll));
|
|
|
|
|
2018-10-11 11:17:51 +00:00
|
|
|
const auto submit = [=] {
|
2018-10-02 20:39:54 +00:00
|
|
|
if (const auto question = inner->selected()) {
|
2018-10-11 11:17:51 +00:00
|
|
|
submitValue(question->value);
|
2018-10-02 20:39:54 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto refresh = [=] {
|
|
|
|
inner->showRows(
|
2018-11-15 15:36:04 +00:00
|
|
|
_session->supportTemplates().query(input->getLastText()));
|
2018-10-02 20:39:54 +00:00
|
|
|
scroll->scrollToY(0);
|
|
|
|
};
|
|
|
|
|
|
|
|
inner->activated() | rpl::start_with_next(submit, lifetime());
|
|
|
|
connect(input, &Ui::InputField::blurred, [=] {
|
2019-09-26 10:55:35 +00:00
|
|
|
base::call_delayed(10, this, [=] {
|
2018-10-02 20:39:54 +00:00
|
|
|
if (!input->hasFocus()) {
|
|
|
|
deactivate();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
connect(input, &Ui::InputField::cancelled, [=] { deactivate(); });
|
|
|
|
connect(input, &Ui::InputField::changed, refresh);
|
|
|
|
connect(input, &Ui::InputField::submitted, submit);
|
|
|
|
input->customUpDown(true);
|
|
|
|
|
|
|
|
_activate = [=] {
|
|
|
|
input->setText(QString());
|
|
|
|
show();
|
|
|
|
input->setFocus();
|
|
|
|
};
|
|
|
|
_deactivate = [=] {
|
|
|
|
hide();
|
|
|
|
};
|
|
|
|
_moveSelection = [=](int delta) {
|
|
|
|
const auto range = inner->moveSelection(delta);
|
|
|
|
if (range.second > range.first) {
|
|
|
|
scroll->scrollToY(range.first, range.second);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
paintRequest(
|
|
|
|
) | rpl::start_with_next([=](QRect clip) {
|
|
|
|
QPainter p(this);
|
|
|
|
p.fillRect(
|
|
|
|
clip.intersected(QRect(0, st::lineWidth, width(), height())),
|
|
|
|
st::mentionBg);
|
|
|
|
p.fillRect(
|
|
|
|
clip.intersected(QRect(0, 0, width(), st::lineWidth)),
|
|
|
|
st::shadowFg);
|
|
|
|
}, lifetime());
|
|
|
|
|
|
|
|
sizeValue(
|
|
|
|
) | rpl::start_with_next([=](QSize size) {
|
|
|
|
inputWrap->resizeToWidth(size.width());
|
|
|
|
inputWrap->moveToLeft(0, st::lineWidth, size.width());
|
|
|
|
scroll->setGeometry(
|
|
|
|
0,
|
|
|
|
inputWrap->height(),
|
|
|
|
size.width(),
|
|
|
|
size.height() - inputWrap->height() - st::lineWidth);
|
|
|
|
inner->resizeToWidth(size.width());
|
|
|
|
}, lifetime());
|
|
|
|
}
|
|
|
|
|
2018-10-11 11:17:51 +00:00
|
|
|
void Autocomplete::submitValue(const QString &value) {
|
2022-11-26 21:20:17 +00:00
|
|
|
const auto prefix = u"contact:"_q;
|
2018-10-05 16:02:33 +00:00
|
|
|
if (value.startsWith(prefix)) {
|
|
|
|
const auto line = value.indexOf('\n');
|
|
|
|
const auto text = (line > 0) ? value.mid(line + 1) : QString();
|
|
|
|
const auto contact = value.mid(
|
|
|
|
prefix.size(),
|
|
|
|
(line > 0) ? (line - prefix.size()) : -1);
|
2021-10-19 13:00:21 +00:00
|
|
|
const auto parts = contact.split(' ', Qt::SkipEmptyParts);
|
2018-10-05 16:02:33 +00:00
|
|
|
if (parts.size() > 1) {
|
|
|
|
const auto phone = parts[0];
|
|
|
|
const auto firstName = parts[1];
|
|
|
|
const auto lastName = (parts.size() > 2)
|
|
|
|
? QStringList(parts.mid(2)).join(' ')
|
|
|
|
: QString();
|
|
|
|
_shareContactRequests.fire(Contact{
|
|
|
|
text,
|
|
|
|
phone,
|
|
|
|
firstName,
|
2018-10-11 11:17:51 +00:00
|
|
|
lastName });
|
2018-10-05 16:02:33 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
_insertRequests.fire_copy(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ConfirmContactBox::ConfirmContactBox(
|
|
|
|
QWidget*,
|
2020-06-23 17:21:58 +00:00
|
|
|
not_null<Window::SessionController*> controller,
|
2018-10-05 16:02:33 +00:00
|
|
|
not_null<History*> history,
|
|
|
|
const Contact &data,
|
2018-10-08 14:08:17 +00:00
|
|
|
Fn<void(Qt::KeyboardModifiers)> submit)
|
2021-07-02 15:29:13 +00:00
|
|
|
: SimpleElementDelegate(controller, [=] { update(); })
|
2021-08-27 20:44:47 +00:00
|
|
|
, _chatStyle(std::make_unique<Ui::ChatStyle>())
|
2020-06-23 17:21:58 +00:00
|
|
|
, _comment(GenerateCommentItem(this, history, data))
|
2018-10-05 16:02:33 +00:00
|
|
|
, _contact(GenerateContactItem(this, history, data))
|
|
|
|
, _submit(submit) {
|
2021-08-27 20:44:47 +00:00
|
|
|
_chatStyle->apply(controller->defaultChatTheme().get());
|
2018-10-05 16:02:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ConfirmContactBox::prepare() {
|
2022-11-29 21:46:36 +00:00
|
|
|
setTitle(rpl::single(u"Confirmation"_q)); // #TODO hard_lang
|
2018-10-05 16:02:33 +00:00
|
|
|
|
|
|
|
auto maxWidth = 0;
|
|
|
|
if (_comment) {
|
2022-10-04 07:10:17 +00:00
|
|
|
_comment->setAttachToNext(true, _contact.get());
|
|
|
|
_contact->setAttachToPrevious(true, _comment.get());
|
2018-10-05 16:02:33 +00:00
|
|
|
_comment->initDimensions();
|
|
|
|
accumulate_max(maxWidth, _comment->maxWidth());
|
|
|
|
}
|
|
|
|
_contact->initDimensions();
|
|
|
|
accumulate_max(maxWidth, _contact->maxWidth());
|
|
|
|
maxWidth += st::boxPadding.left() + st::boxPadding.right();
|
2021-01-23 03:29:50 +00:00
|
|
|
const auto width = std::clamp(maxWidth, st::boxWidth, st::boxWideWidth);
|
2018-10-05 16:02:33 +00:00
|
|
|
const auto available = width
|
|
|
|
- st::boxPadding.left()
|
|
|
|
- st::boxPadding.right();
|
|
|
|
auto height = 0;
|
|
|
|
if (_comment) {
|
|
|
|
height += _comment->resizeGetHeight(available);
|
|
|
|
}
|
|
|
|
height += _contact->resizeGetHeight(available);
|
|
|
|
setDimensions(width, height);
|
|
|
|
_contact->initDimensions();
|
|
|
|
|
2018-10-08 14:08:17 +00:00
|
|
|
_submit = [=, original = std::move(_submit)](Qt::KeyboardModifiers m) {
|
2019-09-13 12:22:54 +00:00
|
|
|
const auto weak = Ui::MakeWeak(this);
|
2018-10-08 14:08:17 +00:00
|
|
|
original(m);
|
2018-10-05 16:02:33 +00:00
|
|
|
if (weak) {
|
|
|
|
closeBox();
|
|
|
|
}
|
2018-10-08 14:08:17 +00:00
|
|
|
};
|
|
|
|
|
2019-06-18 16:53:27 +00:00
|
|
|
const auto button = addButton(tr::lng_send_button(), [] {});
|
2018-10-08 14:08:17 +00:00
|
|
|
button->clicks(
|
|
|
|
) | rpl::start_with_next([=](Qt::MouseButton which) {
|
|
|
|
_submit((which == Qt::RightButton)
|
|
|
|
? SkipSwitchModifiers()
|
|
|
|
: button->clickModifiers());
|
|
|
|
}, button->lifetime());
|
|
|
|
button->setAcceptBoth(true);
|
|
|
|
|
2019-06-18 16:53:27 +00:00
|
|
|
addButton(tr::lng_cancel(), [=] { closeBox(); });
|
2018-10-05 16:02:33 +00:00
|
|
|
}
|
|
|
|
|
2018-10-08 14:08:17 +00:00
|
|
|
void ConfirmContactBox::keyPressEvent(QKeyEvent *e) {
|
|
|
|
if (e->key() == Qt::Key_Enter || e->key() == Qt::Key_Return) {
|
|
|
|
_submit(e->modifiers());
|
|
|
|
} else {
|
|
|
|
BoxContent::keyPressEvent(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-05 16:02:33 +00:00
|
|
|
void ConfirmContactBox::paintEvent(QPaintEvent *e) {
|
|
|
|
Painter p(this);
|
|
|
|
|
|
|
|
p.fillRect(e->rect(), st::boxBg);
|
|
|
|
|
2021-08-27 20:44:47 +00:00
|
|
|
const auto theme = controller()->defaultChatTheme().get();
|
2021-09-03 14:18:03 +00:00
|
|
|
auto context = theme->preparePaintContext(
|
2021-08-27 20:44:47 +00:00
|
|
|
_chatStyle.get(),
|
|
|
|
rect(),
|
2022-09-09 16:09:51 +00:00
|
|
|
rect(),
|
|
|
|
controller()->isGifPausedAtLeastFor(Window::GifPauseReason::Layer));
|
2018-10-05 16:02:33 +00:00
|
|
|
p.translate(st::boxPadding.left(), 0);
|
|
|
|
if (_comment) {
|
2021-09-03 14:18:03 +00:00
|
|
|
context.outbg = _comment->hasOutLayout();
|
2021-08-19 14:22:12 +00:00
|
|
|
_comment->draw(p, context);
|
2018-10-05 16:02:33 +00:00
|
|
|
p.translate(0, _comment->height());
|
|
|
|
}
|
2021-09-03 14:18:03 +00:00
|
|
|
context.outbg = _contact->hasOutLayout();
|
2021-08-19 14:22:12 +00:00
|
|
|
_contact->draw(p, context);
|
2018-10-05 16:02:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
HistoryView::Context ConfirmContactBox::elementContext() {
|
|
|
|
return HistoryView::Context::ContactPreview;
|
|
|
|
}
|
|
|
|
|
2018-10-02 20:39:54 +00:00
|
|
|
} // namespace Support
|