2014-12-19 21:33:22 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2018-01-03 10:23:14 +00:00
|
|
|
the official desktop application for the Telegram messaging service.
|
2014-12-19 21:33:22 +00:00
|
|
|
|
2018-01-03 10:23:14 +00:00
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
2014-12-19 21:33:22 +00:00
|
|
|
*/
|
2017-04-06 14:38:10 +00:00
|
|
|
#include "boxes/language_box.h"
|
2014-12-19 21:33:22 +00:00
|
|
|
|
2017-04-13 08:27:10 +00:00
|
|
|
#include "lang/lang_keys.h"
|
2016-11-11 13:46:04 +00:00
|
|
|
#include "ui/widgets/checkbox.h"
|
|
|
|
#include "ui/widgets/buttons.h"
|
2018-10-30 11:23:54 +00:00
|
|
|
#include "ui/widgets/labels.h"
|
|
|
|
#include "ui/widgets/multi_select.h"
|
|
|
|
#include "ui/widgets/scroll_area.h"
|
2018-11-01 07:10:15 +00:00
|
|
|
#include "ui/widgets/dropdown_menu.h"
|
2018-10-30 11:23:54 +00:00
|
|
|
#include "ui/text/text_entity.h"
|
|
|
|
#include "ui/wrap/vertical_layout.h"
|
|
|
|
#include "ui/wrap/slide_wrap.h"
|
|
|
|
#include "ui/effects/ripple_animation.h"
|
2018-11-01 07:10:15 +00:00
|
|
|
#include "ui/toast/toast.h"
|
2018-10-30 11:23:54 +00:00
|
|
|
#include "ui/text_options.h"
|
2017-03-04 10:23:56 +00:00
|
|
|
#include "storage/localstorage.h"
|
2017-04-06 14:38:10 +00:00
|
|
|
#include "boxes/confirm_box.h"
|
2014-12-19 21:33:22 +00:00
|
|
|
#include "mainwidget.h"
|
2016-04-12 21:31:28 +00:00
|
|
|
#include "mainwindow.h"
|
2019-01-21 13:42:21 +00:00
|
|
|
#include "core/application.h"
|
2017-04-13 17:59:05 +00:00
|
|
|
#include "lang/lang_instance.h"
|
2017-04-18 15:21:03 +00:00
|
|
|
#include "lang/lang_cloud_manager.h"
|
2016-11-15 11:56:49 +00:00
|
|
|
#include "styles/style_boxes.h"
|
2018-11-01 07:10:15 +00:00
|
|
|
#include "styles/style_info.h"
|
2018-10-30 11:23:54 +00:00
|
|
|
#include "styles/style_passport.h"
|
2018-11-01 07:10:15 +00:00
|
|
|
#include "styles/style_chat_helpers.h"
|
2014-12-19 21:33:22 +00:00
|
|
|
|
2018-10-30 11:23:54 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
using Language = Lang::Language;
|
|
|
|
using Languages = Lang::CloudManager::Languages;
|
|
|
|
|
|
|
|
class Rows : public Ui::RpWidget {
|
2017-04-18 15:21:03 +00:00
|
|
|
public:
|
2018-11-01 07:10:15 +00:00
|
|
|
Rows(
|
|
|
|
QWidget *parent,
|
|
|
|
const Languages &data,
|
|
|
|
const QString &chosen,
|
|
|
|
bool areOfficial);
|
2018-10-30 11:23:54 +00:00
|
|
|
|
|
|
|
void filter(const QString &query);
|
|
|
|
|
|
|
|
int count() const;
|
|
|
|
int selected() const;
|
|
|
|
void setSelected(int selected);
|
2018-11-01 07:10:15 +00:00
|
|
|
rpl::producer<bool> hasSelection() const;
|
2018-11-13 09:14:22 +00:00
|
|
|
rpl::producer<bool> isEmpty() const;
|
2018-10-30 11:23:54 +00:00
|
|
|
|
|
|
|
void activateSelected();
|
|
|
|
rpl::producer<Language> activations() const;
|
2017-04-18 15:21:03 +00:00
|
|
|
|
2018-10-30 11:23:54 +00:00
|
|
|
Ui::ScrollToRequest rowScrollRequest(int index) const;
|
|
|
|
|
2018-11-01 07:10:15 +00:00
|
|
|
static int DefaultRowHeight();
|
|
|
|
|
2018-10-30 11:23:54 +00:00
|
|
|
protected:
|
|
|
|
int resizeGetHeight(int newWidth) override;
|
|
|
|
|
|
|
|
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;
|
2017-04-18 15:21:03 +00:00
|
|
|
|
|
|
|
private:
|
2018-10-30 11:23:54 +00:00
|
|
|
struct Row {
|
|
|
|
Language data;
|
|
|
|
Text title = { st::boxWideWidth / 2 };
|
|
|
|
Text description = { st::boxWideWidth / 2 };
|
|
|
|
int top = 0;
|
|
|
|
int height = 0;
|
|
|
|
mutable std::unique_ptr<Ui::RippleAnimation> ripple;
|
2018-11-01 07:10:15 +00:00
|
|
|
mutable std::unique_ptr<Ui::RippleAnimation> menuToggleRipple;
|
|
|
|
bool menuToggleForceRippled = false;
|
2018-10-30 11:23:54 +00:00
|
|
|
int titleHeight = 0;
|
|
|
|
int descriptionHeight = 0;
|
|
|
|
QStringList keywords;
|
2018-11-01 07:10:15 +00:00
|
|
|
std::unique_ptr<Ui::RadioView> check;
|
|
|
|
bool removed = false;
|
|
|
|
};
|
|
|
|
struct RowSelection {
|
|
|
|
int index = 0;
|
|
|
|
|
|
|
|
inline bool operator==(const RowSelection &other) const {
|
|
|
|
return (index == other.index);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
struct MenuSelection {
|
|
|
|
int index = 0;
|
|
|
|
|
|
|
|
inline bool operator==(const MenuSelection &other) const {
|
|
|
|
return (index == other.index);
|
|
|
|
}
|
2018-10-30 11:23:54 +00:00
|
|
|
};
|
2018-11-01 07:10:15 +00:00
|
|
|
using Selection = base::optional_variant<RowSelection, MenuSelection>;
|
2018-10-30 11:23:54 +00:00
|
|
|
|
2018-11-01 07:10:15 +00:00
|
|
|
void updateSelected(Selection selected);
|
|
|
|
void updatePressed(Selection pressed);
|
2018-10-30 11:23:54 +00:00
|
|
|
Rows::Row &rowByIndex(int index);
|
|
|
|
const Rows::Row &rowByIndex(int index) const;
|
2018-11-01 07:10:15 +00:00
|
|
|
Rows::Row &rowBySelection(Selection selected);
|
|
|
|
const Rows::Row &rowBySelection(Selection selected) const;
|
|
|
|
std::unique_ptr<Ui::RippleAnimation> &rippleBySelection(
|
|
|
|
Selection selected);
|
|
|
|
const std::unique_ptr<Ui::RippleAnimation> &rippleBySelection(
|
|
|
|
Selection selected) const;
|
|
|
|
std::unique_ptr<Ui::RippleAnimation> &rippleBySelection(
|
|
|
|
not_null<Row*> row,
|
|
|
|
Selection selected);
|
|
|
|
const std::unique_ptr<Ui::RippleAnimation> &rippleBySelection(
|
|
|
|
not_null<const Row*> row,
|
|
|
|
Selection selected) const;
|
|
|
|
void addRipple(Selection selected, QPoint position);
|
|
|
|
void ensureRippleBySelection(Selection selected);
|
|
|
|
void ensureRippleBySelection(not_null<Row*> row, Selection selected);
|
|
|
|
int indexFromSelection(Selection selected) const;
|
2018-10-30 11:23:54 +00:00
|
|
|
int countAvailableWidth() const;
|
|
|
|
int countAvailableWidth(int newWidth) const;
|
2018-11-01 07:10:15 +00:00
|
|
|
QRect menuToggleArea() const;
|
|
|
|
QRect menuToggleArea(not_null<const Row*> row) const;
|
|
|
|
void repaint(Selection selected);
|
2018-10-30 11:23:54 +00:00
|
|
|
void repaint(int index);
|
|
|
|
void repaint(const Row &row);
|
2018-11-01 07:10:15 +00:00
|
|
|
void repaintChecked(not_null<const Row*> row);
|
2018-10-30 11:23:54 +00:00
|
|
|
void activateByIndex(int index);
|
|
|
|
|
2018-11-01 07:10:15 +00:00
|
|
|
void showMenu(int index);
|
|
|
|
void setForceRippled(not_null<Row*> row, bool rippled);
|
|
|
|
bool canShare(not_null<const Row*> row) const;
|
|
|
|
bool canRemove(not_null<const Row*> row) const;
|
|
|
|
bool hasMenu(not_null<const Row*> row) const;
|
|
|
|
void share(not_null<const Row*> row) const;
|
|
|
|
void remove(not_null<Row*> row);
|
|
|
|
void restore(not_null<Row*> row);
|
|
|
|
|
2018-10-30 11:23:54 +00:00
|
|
|
std::vector<Row> _rows;
|
|
|
|
std::vector<not_null<Row*>> _filtered;
|
2018-11-01 07:10:15 +00:00
|
|
|
Selection _selected;
|
|
|
|
Selection _pressed;
|
2018-10-30 11:23:54 +00:00
|
|
|
QString _chosen;
|
|
|
|
QStringList _query;
|
|
|
|
|
2018-11-01 07:10:15 +00:00
|
|
|
bool _areOfficial = false;
|
2018-10-30 11:23:54 +00:00
|
|
|
bool _mouseSelection = false;
|
|
|
|
QPoint _globalMousePosition;
|
2018-11-01 07:10:15 +00:00
|
|
|
base::unique_qptr<Ui::DropdownMenu> _menu;
|
|
|
|
int _menuShownIndex = -1;
|
|
|
|
bool _menuOtherEntered = false;
|
2017-04-18 15:21:03 +00:00
|
|
|
|
2018-11-01 07:10:15 +00:00
|
|
|
rpl::event_stream<bool> _hasSelection;
|
2018-10-30 11:23:54 +00:00
|
|
|
rpl::event_stream<Language> _activations;
|
2018-11-13 09:14:22 +00:00
|
|
|
rpl::event_stream<bool> _isEmpty;
|
2017-04-18 15:21:03 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2018-10-30 11:23:54 +00:00
|
|
|
class Content : public Ui::RpWidget {
|
|
|
|
public:
|
|
|
|
Content(
|
|
|
|
QWidget *parent,
|
2018-11-13 09:14:22 +00:00
|
|
|
const Languages &recent,
|
|
|
|
const Languages &official);
|
2018-10-30 11:23:54 +00:00
|
|
|
|
|
|
|
Ui::ScrollToRequest jump(int rows);
|
|
|
|
void filter(const QString &query);
|
|
|
|
rpl::producer<Language> activations() const;
|
|
|
|
void activateBySubmit();
|
|
|
|
|
|
|
|
private:
|
|
|
|
void setupContent(
|
2018-11-13 09:14:22 +00:00
|
|
|
const Languages &recent,
|
|
|
|
const Languages &official);
|
2018-10-30 11:23:54 +00:00
|
|
|
|
|
|
|
Fn<Ui::ScrollToRequest(int rows)> _jump;
|
|
|
|
Fn<void(const QString &query)> _filter;
|
|
|
|
Fn<rpl::producer<Language>()> _activations;
|
|
|
|
Fn<void()> _activateBySubmit;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2018-11-13 09:14:22 +00:00
|
|
|
std::pair<Languages, Languages> PrepareLists() {
|
|
|
|
const auto projId = [](const Language &language) {
|
|
|
|
return language.id;
|
|
|
|
};
|
|
|
|
const auto current = Lang::LanguageIdOrDefault(Lang::Current().id());
|
|
|
|
auto official = Lang::CurrentCloudManager().languageList();
|
|
|
|
auto recent = Local::readRecentLanguages();
|
|
|
|
ranges::stable_partition(recent, [&](const Language &language) {
|
|
|
|
return (language.id == current);
|
|
|
|
});
|
|
|
|
if (recent.empty() || recent.front().id != current) {
|
|
|
|
if (ranges::find(official, current, projId) == end(official)) {
|
|
|
|
const auto generate = [&] {
|
|
|
|
const auto name = (current == "#custom")
|
|
|
|
? "Custom lang pack"
|
|
|
|
: Lang::Current().name();
|
|
|
|
return Language{
|
|
|
|
current,
|
|
|
|
QString(),
|
|
|
|
QString(),
|
|
|
|
name,
|
|
|
|
Lang::Current().nativeName()
|
|
|
|
};
|
|
|
|
};
|
|
|
|
const auto i = ranges::find(official, current, projId);
|
|
|
|
recent.insert(begin(recent), generate());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
auto i = begin(official), e = end(official);
|
|
|
|
const auto remover = [&](const Language &language) {
|
|
|
|
auto k = ranges::find(i, e, language.id, projId);
|
|
|
|
if (k == e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
for (; k != i; --k) {
|
|
|
|
std::swap(*k, *(k - 1));
|
|
|
|
}
|
|
|
|
++i;
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
recent.erase(ranges::remove_if(recent, remover), end(recent));
|
|
|
|
return { std::move(recent), std::move(official) };
|
|
|
|
}
|
|
|
|
|
2018-11-01 07:10:15 +00:00
|
|
|
Rows::Rows(
|
|
|
|
QWidget *parent,
|
|
|
|
const Languages &data,
|
|
|
|
const QString &chosen,
|
|
|
|
bool areOfficial)
|
2018-10-30 11:23:54 +00:00
|
|
|
: RpWidget(parent)
|
2018-11-01 07:10:15 +00:00
|
|
|
, _chosen(chosen)
|
|
|
|
, _areOfficial(areOfficial) {
|
2018-10-30 11:23:54 +00:00
|
|
|
const auto descriptionOptions = TextParseOptions{
|
|
|
|
TextParseMultiline,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
Qt::LayoutDirectionAuto
|
|
|
|
};
|
|
|
|
_rows.reserve(data.size());
|
|
|
|
for (const auto &item : data) {
|
|
|
|
_rows.push_back(Row{ item });
|
|
|
|
auto &row = _rows.back();
|
2018-11-01 07:10:15 +00:00
|
|
|
row.check = std::make_unique<Ui::RadioView>(
|
|
|
|
st::langsRadio,
|
|
|
|
(row.data.id == _chosen),
|
|
|
|
[=, row = &row] { repaint(*row); });
|
2018-10-30 11:23:54 +00:00
|
|
|
row.title.setText(
|
|
|
|
st::semiboldTextStyle,
|
|
|
|
item.nativeName,
|
|
|
|
Ui::NameTextOptions());
|
|
|
|
row.description.setText(
|
|
|
|
st::defaultTextStyle,
|
|
|
|
item.name,
|
|
|
|
descriptionOptions);
|
|
|
|
row.keywords = TextUtilities::PrepareSearchWords(
|
|
|
|
item.name + ' ' + item.nativeName);
|
|
|
|
}
|
|
|
|
resizeToWidth(width());
|
|
|
|
setAttribute(Qt::WA_MouseTracking);
|
|
|
|
update();
|
2017-04-18 15:21:03 +00:00
|
|
|
}
|
|
|
|
|
2018-10-30 11:23:54 +00:00
|
|
|
void Rows::mouseMoveEvent(QMouseEvent *e) {
|
|
|
|
const auto position = e->globalPos();
|
2018-11-01 07:10:15 +00:00
|
|
|
if (_menu) {
|
|
|
|
const auto rect = (_menuShownIndex >= 0)
|
|
|
|
? menuToggleArea(&rowByIndex(_menuShownIndex))
|
|
|
|
: QRect();
|
|
|
|
if (rect.contains(e->pos())) {
|
|
|
|
if (!_menuOtherEntered) {
|
|
|
|
_menuOtherEntered = true;
|
|
|
|
_menu->otherEnter();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (_menuOtherEntered) {
|
|
|
|
_menuOtherEntered = false;
|
|
|
|
_menu->otherLeave();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-10-30 11:23:54 +00:00
|
|
|
if (!_mouseSelection && position == _globalMousePosition) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_mouseSelection = true;
|
|
|
|
_globalMousePosition = position;
|
|
|
|
const auto index = [&] {
|
|
|
|
const auto y = e->pos().y();
|
|
|
|
if (y < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
for (auto i = 0, till = count(); i != till; ++i) {
|
|
|
|
const auto &row = rowByIndex(i);
|
|
|
|
if (row.top + row.height > y) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}();
|
2018-11-01 07:10:15 +00:00
|
|
|
const auto row = (index >= 0) ? &rowByIndex(index) : nullptr;
|
|
|
|
const auto inMenuToggle = (index >= 0 && hasMenu(row))
|
|
|
|
? menuToggleArea(row).contains(e->pos())
|
|
|
|
: false;
|
|
|
|
if (index < 0) {
|
|
|
|
updateSelected({});
|
|
|
|
} else if (inMenuToggle) {
|
|
|
|
updateSelected(MenuSelection{ index });
|
|
|
|
} else if (!row->removed) {
|
|
|
|
updateSelected(RowSelection{ index });
|
|
|
|
} else {
|
|
|
|
updateSelected({});
|
|
|
|
}
|
2017-04-18 15:21:03 +00:00
|
|
|
}
|
|
|
|
|
2018-10-30 11:23:54 +00:00
|
|
|
void Rows::mousePressEvent(QMouseEvent *e) {
|
|
|
|
updatePressed(_selected);
|
2018-11-13 09:14:22 +00:00
|
|
|
if (_pressed.has_value()
|
|
|
|
&& !rowBySelection(_pressed).menuToggleForceRippled) {
|
2018-11-01 07:10:15 +00:00
|
|
|
addRipple(_pressed, e->pos());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QRect Rows::menuToggleArea() const {
|
|
|
|
const auto size = st::topBarSearch.width;
|
|
|
|
const auto top = (DefaultRowHeight() - size) / 2;
|
|
|
|
const auto skip = st::boxLayerScroll.width
|
|
|
|
- st::boxLayerScroll.deltax
|
|
|
|
+ top;
|
|
|
|
const auto left = width() - skip - size;
|
|
|
|
return QRect(left, top, size, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
QRect Rows::menuToggleArea(not_null<const Row*> row) const {
|
|
|
|
return menuToggleArea().translated(0, row->top);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Rows::addRipple(Selection selected, QPoint position) {
|
|
|
|
Expects(selected.has_value());
|
|
|
|
|
|
|
|
ensureRippleBySelection(selected);
|
|
|
|
|
|
|
|
const auto menu = selected.is<MenuSelection>();
|
|
|
|
const auto &row = rowBySelection(selected);
|
|
|
|
const auto menuArea = menuToggleArea(&row);
|
|
|
|
auto &ripple = rippleBySelection(&row, selected);
|
|
|
|
const auto topleft = menu ? menuArea.topLeft() : QPoint(0, row.top);
|
|
|
|
ripple->add(position - topleft);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Rows::ensureRippleBySelection(Selection selected) {
|
|
|
|
ensureRippleBySelection(&rowBySelection(selected), selected);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Rows::ensureRippleBySelection(not_null<Row*> row, Selection selected) {
|
2018-11-13 09:14:22 +00:00
|
|
|
auto &ripple = rippleBySelection(row, selected);
|
2018-11-01 07:10:15 +00:00
|
|
|
if (ripple) {
|
|
|
|
return;
|
2017-04-18 15:21:03 +00:00
|
|
|
}
|
2018-11-01 07:10:15 +00:00
|
|
|
const auto menu = selected.is<MenuSelection>();
|
|
|
|
const auto menuArea = menuToggleArea(row);
|
|
|
|
auto mask = menu
|
|
|
|
? Ui::RippleAnimation::ellipseMask(menuArea.size())
|
|
|
|
: Ui::RippleAnimation::rectMask({ width(), row->height });
|
|
|
|
ripple = std::make_unique<Ui::RippleAnimation>(
|
|
|
|
st::defaultRippleAnimation,
|
|
|
|
std::move(mask),
|
|
|
|
[=] { repaintChecked(row); });
|
2018-10-30 11:23:54 +00:00
|
|
|
}
|
2017-04-18 15:21:03 +00:00
|
|
|
|
2018-10-30 11:23:54 +00:00
|
|
|
void Rows::mouseReleaseEvent(QMouseEvent *e) {
|
2018-11-13 09:14:22 +00:00
|
|
|
if (_menu && e->button() == Qt::LeftButton) {
|
|
|
|
if (_menu->isHiding()) {
|
|
|
|
_menu->otherEnter();
|
|
|
|
} else {
|
|
|
|
_menu->otherLeave();
|
|
|
|
}
|
|
|
|
}
|
2018-10-30 11:23:54 +00:00
|
|
|
const auto pressed = _pressed;
|
2018-11-01 07:10:15 +00:00
|
|
|
updatePressed({});
|
|
|
|
if (pressed == _selected) {
|
|
|
|
pressed.match([&](RowSelection data) {
|
|
|
|
activateByIndex(data.index);
|
|
|
|
}, [&](MenuSelection data) {
|
|
|
|
showMenu(data.index);
|
|
|
|
}, [](std::nullopt_t) {});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Rows::canShare(not_null<const Row*> row) const {
|
2018-12-07 08:42:48 +00:00
|
|
|
return !_areOfficial && !row->data.id.startsWith('#');
|
2018-11-01 07:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Rows::canRemove(not_null<const Row*> row) const {
|
|
|
|
return !_areOfficial && !row->check->checked();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Rows::hasMenu(not_null<const Row*> row) const {
|
|
|
|
return canShare(row) || canRemove(row);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Rows::share(not_null<const Row*> row) const {
|
|
|
|
const auto link = qsl("https://t.me/setlanguage/") + row->data.id;
|
|
|
|
QApplication::clipboard()->setText(link);
|
|
|
|
Ui::Toast::Show(lang(lng_username_copied));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Rows::remove(not_null<Row*> row) {
|
|
|
|
row->removed = true;
|
|
|
|
Local::removeRecentLanguage(row->data.id);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Rows::restore(not_null<Row*> row) {
|
|
|
|
row->removed = false;
|
|
|
|
Local::saveRecentLanguages(ranges::view::all(
|
|
|
|
_rows
|
|
|
|
) | ranges::view::filter([](const Row &row) {
|
|
|
|
return !row.removed;
|
|
|
|
}) | ranges::view::transform([](const Row &row) {
|
|
|
|
return row.data;
|
|
|
|
}) | ranges::to_vector);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Rows::showMenu(int index) {
|
|
|
|
const auto row = &rowByIndex(index);
|
|
|
|
if (_menu || !hasMenu(row)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_menu = base::make_unique_q<Ui::DropdownMenu>(window());
|
|
|
|
const auto weak = _menu.get();
|
|
|
|
_menu->setHiddenCallback([=] {
|
|
|
|
weak->deleteLater();
|
|
|
|
if (_menu == weak) {
|
|
|
|
setForceRippled(row, false);
|
|
|
|
_menuShownIndex = -1;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
_menu->setShowStartCallback([=] {
|
|
|
|
if (_menu == weak) {
|
|
|
|
setForceRippled(row, true);
|
|
|
|
_menuShownIndex = index;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
_menu->setHideStartCallback([=] {
|
|
|
|
if (_menu == weak) {
|
|
|
|
setForceRippled(row, false);
|
|
|
|
_menuShownIndex = -1;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
const auto addAction = [&](
|
|
|
|
const QString &text,
|
|
|
|
Fn<void()> callback) {
|
|
|
|
return _menu->addAction(text, std::move(callback));
|
|
|
|
};
|
|
|
|
const auto id = row->data.id;
|
|
|
|
if (canShare(row)) {
|
|
|
|
addAction(lang(lng_proxy_edit_share), [=] { share(row); });
|
|
|
|
}
|
|
|
|
if (canRemove(row)) {
|
|
|
|
if (row->removed) {
|
|
|
|
addAction(lang(lng_proxy_menu_restore), [=] {
|
|
|
|
restore(row);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
addAction(lang(lng_proxy_menu_delete), [=] {
|
|
|
|
remove(row);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const auto toggle = menuToggleArea(row);
|
|
|
|
const auto parentTopLeft = window()->mapToGlobal({ 0, 0 });
|
|
|
|
const auto buttonTopLeft = mapToGlobal(toggle.topLeft());
|
|
|
|
const auto parent = QRect(parentTopLeft, window()->size());
|
|
|
|
const auto button = QRect(buttonTopLeft, toggle.size());
|
|
|
|
const auto bottom = button.y()
|
|
|
|
+ st::proxyDropdownDownPosition.y()
|
|
|
|
+ _menu->height()
|
|
|
|
- parent.y();
|
|
|
|
const auto top = button.y()
|
|
|
|
+ st::proxyDropdownUpPosition.y()
|
|
|
|
- _menu->height()
|
|
|
|
- parent.y();
|
|
|
|
_menuShownIndex = index;
|
|
|
|
_menuOtherEntered = true;
|
|
|
|
if (bottom > parent.height() && top >= 0) {
|
|
|
|
const auto left = button.x()
|
|
|
|
+ button.width()
|
|
|
|
+ st::proxyDropdownUpPosition.x()
|
|
|
|
- _menu->width()
|
|
|
|
- parent.x();
|
|
|
|
_menu->move(left, top);
|
|
|
|
_menu->showAnimated(Ui::PanelAnimation::Origin::BottomRight);
|
|
|
|
} else {
|
|
|
|
const auto left = button.x()
|
|
|
|
+ button.width()
|
|
|
|
+ st::proxyDropdownDownPosition.x()
|
|
|
|
- _menu->width()
|
|
|
|
- parent.x();
|
|
|
|
_menu->move(left, bottom - _menu->height());
|
|
|
|
_menu->showAnimated(Ui::PanelAnimation::Origin::TopRight);
|
2017-04-18 15:21:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-01 07:10:15 +00:00
|
|
|
void Rows::setForceRippled(not_null<Row*> row, bool rippled) {
|
|
|
|
if (row->menuToggleForceRippled != rippled) {
|
|
|
|
row->menuToggleForceRippled = rippled;
|
|
|
|
auto &ripple = rippleBySelection(row, MenuSelection{});
|
|
|
|
if (row->menuToggleForceRippled) {
|
|
|
|
ensureRippleBySelection(row, MenuSelection{});
|
|
|
|
if (ripple->empty()) {
|
|
|
|
ripple->addFading();
|
|
|
|
} else {
|
|
|
|
ripple->lastUnstop();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (ripple) {
|
|
|
|
ripple->lastStop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
repaint(*row);
|
|
|
|
}
|
|
|
|
|
2018-10-30 11:23:54 +00:00
|
|
|
void Rows::activateByIndex(int index) {
|
|
|
|
_activations.fire_copy(rowByIndex(index).data);
|
|
|
|
}
|
2017-04-18 15:21:03 +00:00
|
|
|
|
2018-10-30 11:23:54 +00:00
|
|
|
void Rows::leaveEventHook(QEvent *e) {
|
2018-11-01 07:10:15 +00:00
|
|
|
updateSelected({});
|
|
|
|
if (_menu && _menuOtherEntered) {
|
|
|
|
_menuOtherEntered = false;
|
|
|
|
_menu->otherLeave();
|
|
|
|
}
|
2018-10-30 11:23:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Rows::filter(const QString &query) {
|
2018-11-01 07:10:15 +00:00
|
|
|
updateSelected({});
|
|
|
|
updatePressed({});
|
|
|
|
_menu = nullptr;
|
|
|
|
_menuShownIndex = -1;
|
2018-10-30 11:23:54 +00:00
|
|
|
|
|
|
|
_query = TextUtilities::PrepareSearchWords(query);
|
|
|
|
|
|
|
|
const auto skip = [](
|
|
|
|
const QStringList &haystack,
|
|
|
|
const QStringList &needles) {
|
|
|
|
const auto find = [](
|
|
|
|
const QStringList &haystack,
|
|
|
|
const QString &needle) {
|
|
|
|
for (const auto &item : haystack) {
|
|
|
|
if (item.startsWith(needle)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
for (const auto &needle : needles) {
|
|
|
|
if (!find(haystack, needle)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!_query.isEmpty()) {
|
|
|
|
_filtered.clear();
|
|
|
|
_filtered.reserve(_rows.size());
|
|
|
|
for (auto &row : _rows) {
|
|
|
|
if (!skip(row.keywords, _query)) {
|
|
|
|
_filtered.push_back(&row);
|
|
|
|
} else {
|
|
|
|
row.ripple = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resizeToWidth(width());
|
|
|
|
Ui::SendPendingMoveResizeEvents(this);
|
2018-11-13 09:14:22 +00:00
|
|
|
|
|
|
|
_isEmpty.fire(count() == 0);
|
2018-10-30 11:23:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int Rows::count() const {
|
|
|
|
return _query.isEmpty() ? _rows.size() : _filtered.size();
|
|
|
|
}
|
|
|
|
|
2018-11-01 07:10:15 +00:00
|
|
|
int Rows::indexFromSelection(Selection selected) const {
|
|
|
|
return selected.match([&](RowSelection data) {
|
|
|
|
return data.index;
|
|
|
|
}, [&](MenuSelection data) {
|
|
|
|
return data.index;
|
|
|
|
}, [](std::nullopt_t) {
|
|
|
|
return -1;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-10-30 11:23:54 +00:00
|
|
|
int Rows::selected() const {
|
2018-11-01 07:10:15 +00:00
|
|
|
return indexFromSelection(_selected);
|
2018-10-30 11:23:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Rows::activateSelected() {
|
|
|
|
const auto index = selected();
|
|
|
|
if (index >= 0) {
|
|
|
|
activateByIndex(index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rpl::producer<Language> Rows::activations() const {
|
|
|
|
return _activations.events();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Rows::setSelected(int selected) {
|
|
|
|
_mouseSelection = false;
|
|
|
|
const auto limit = count();
|
2018-11-01 07:10:15 +00:00
|
|
|
if (selected >= 0 && selected < limit) {
|
|
|
|
updateSelected(RowSelection{ selected });
|
|
|
|
} else {
|
|
|
|
updateSelected({});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rpl::producer<bool> Rows::hasSelection() const {
|
|
|
|
return _hasSelection.events();
|
2018-10-30 11:23:54 +00:00
|
|
|
}
|
|
|
|
|
2018-11-13 09:14:22 +00:00
|
|
|
rpl::producer<bool> Rows::isEmpty() const {
|
|
|
|
return _isEmpty.events_starting_with(
|
|
|
|
count() == 0
|
|
|
|
) | rpl::distinct_until_changed();
|
|
|
|
}
|
|
|
|
|
2018-11-01 07:10:15 +00:00
|
|
|
void Rows::repaint(Selection selected) {
|
|
|
|
selected.match([](std::nullopt_t) {
|
|
|
|
}, [&](const auto &data) {
|
|
|
|
repaint(data.index);
|
|
|
|
});
|
2018-10-30 11:23:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Rows::repaint(int index) {
|
|
|
|
if (index >= 0) {
|
|
|
|
repaint(rowByIndex(index));
|
2017-06-02 11:46:02 +00:00
|
|
|
}
|
2017-04-18 15:21:03 +00:00
|
|
|
}
|
|
|
|
|
2018-10-30 11:23:54 +00:00
|
|
|
void Rows::repaint(const Row &row) {
|
|
|
|
update(0, row.top, width(), row.height);
|
|
|
|
}
|
|
|
|
|
2018-11-01 07:10:15 +00:00
|
|
|
void Rows::repaintChecked(not_null<const Row*> row) {
|
2018-10-30 11:23:54 +00:00
|
|
|
const auto found = (ranges::find(_filtered, row) != end(_filtered));
|
|
|
|
if (_query.isEmpty() || found) {
|
|
|
|
repaint(*row);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-01 07:10:15 +00:00
|
|
|
void Rows::updateSelected(Selection selected) {
|
|
|
|
const auto changed = (_selected.has_value() != selected.has_value());
|
2018-10-30 11:23:54 +00:00
|
|
|
repaint(_selected);
|
|
|
|
_selected = selected;
|
|
|
|
repaint(_selected);
|
2018-11-01 07:10:15 +00:00
|
|
|
if (changed) {
|
|
|
|
_hasSelection.fire(_selected.has_value());
|
|
|
|
}
|
2018-10-30 11:23:54 +00:00
|
|
|
}
|
|
|
|
|
2018-11-01 07:10:15 +00:00
|
|
|
void Rows::updatePressed(Selection pressed) {
|
|
|
|
if (_pressed.has_value()) {
|
2018-11-13 09:14:22 +00:00
|
|
|
if (!rowBySelection(_pressed).menuToggleForceRippled) {
|
|
|
|
if (const auto ripple = rippleBySelection(_pressed).get()) {
|
|
|
|
ripple->lastStop();
|
|
|
|
}
|
2017-04-18 18:57:32 +00:00
|
|
|
}
|
|
|
|
}
|
2018-10-30 11:23:54 +00:00
|
|
|
_pressed = pressed;
|
2017-04-18 18:57:32 +00:00
|
|
|
}
|
|
|
|
|
2018-10-30 11:23:54 +00:00
|
|
|
Rows::Row &Rows::rowByIndex(int index) {
|
|
|
|
Expects(index >= 0 && index < count());
|
2017-04-18 15:21:03 +00:00
|
|
|
|
2018-10-30 11:23:54 +00:00
|
|
|
return _query.isEmpty() ? _rows[index] : *_filtered[index];
|
|
|
|
}
|
2017-04-18 15:21:03 +00:00
|
|
|
|
2018-10-30 11:23:54 +00:00
|
|
|
const Rows::Row &Rows::rowByIndex(int index) const {
|
|
|
|
Expects(index >= 0 && index < count());
|
|
|
|
|
|
|
|
return _query.isEmpty() ? _rows[index] : *_filtered[index];
|
2017-04-18 15:21:03 +00:00
|
|
|
}
|
|
|
|
|
2018-11-01 07:10:15 +00:00
|
|
|
Rows::Row &Rows::rowBySelection(Selection selected) {
|
|
|
|
return rowByIndex(indexFromSelection(selected));
|
|
|
|
}
|
|
|
|
|
|
|
|
const Rows::Row &Rows::rowBySelection(Selection selected) const {
|
|
|
|
return rowByIndex(indexFromSelection(selected));
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<Ui::RippleAnimation> &Rows::rippleBySelection(
|
|
|
|
Selection selected) {
|
|
|
|
return rippleBySelection(&rowBySelection(selected), selected);
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::unique_ptr<Ui::RippleAnimation> &Rows::rippleBySelection(
|
|
|
|
Selection selected) const {
|
|
|
|
return rippleBySelection(&rowBySelection(selected), selected);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<Ui::RippleAnimation> &Rows::rippleBySelection(
|
|
|
|
not_null<Row*> row,
|
|
|
|
Selection selected) {
|
|
|
|
return selected.is<MenuSelection>()
|
|
|
|
? row->menuToggleRipple
|
|
|
|
: row->ripple;
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::unique_ptr<Ui::RippleAnimation> &Rows::rippleBySelection(
|
|
|
|
not_null<const Row*> row,
|
|
|
|
Selection selected) const {
|
|
|
|
return const_cast<Rows*>(this)->rippleBySelection(
|
|
|
|
const_cast<Row*>(row.get()),
|
|
|
|
selected);
|
|
|
|
}
|
|
|
|
|
2018-10-30 11:23:54 +00:00
|
|
|
Ui::ScrollToRequest Rows::rowScrollRequest(int index) const {
|
|
|
|
const auto &row = rowByIndex(index);
|
|
|
|
return Ui::ScrollToRequest(row.top, row.top + row.height);
|
|
|
|
}
|
2014-12-19 21:33:22 +00:00
|
|
|
|
2018-11-01 07:10:15 +00:00
|
|
|
int Rows::DefaultRowHeight() {
|
|
|
|
return st::passportRowPadding.top()
|
|
|
|
+ st::semiboldFont->height
|
|
|
|
+ st::passportRowSkip
|
|
|
|
+ st::normalFont->height
|
|
|
|
+ st::passportRowPadding.bottom();
|
|
|
|
}
|
|
|
|
|
2018-10-30 11:23:54 +00:00
|
|
|
int Rows::resizeGetHeight(int newWidth) {
|
|
|
|
const auto availableWidth = countAvailableWidth(newWidth);
|
|
|
|
auto result = 0;
|
|
|
|
for (auto i = 0, till = count(); i != till; ++i) {
|
|
|
|
auto &row = rowByIndex(i);
|
|
|
|
row.top = result;
|
|
|
|
row.titleHeight = row.title.countHeight(availableWidth);
|
|
|
|
row.descriptionHeight = row.description.countHeight(availableWidth);
|
|
|
|
row.height = st::passportRowPadding.top()
|
|
|
|
+ row.titleHeight
|
|
|
|
+ st::passportRowSkip
|
|
|
|
+ row.descriptionHeight
|
|
|
|
+ st::passportRowPadding.bottom();
|
|
|
|
result += row.height;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2014-12-19 21:33:22 +00:00
|
|
|
|
2018-10-30 11:23:54 +00:00
|
|
|
int Rows::countAvailableWidth(int newWidth) const {
|
2018-11-01 07:10:15 +00:00
|
|
|
const auto right = width() - menuToggleArea().x();
|
2018-10-30 11:23:54 +00:00
|
|
|
return newWidth
|
|
|
|
- st::passportRowPadding.left()
|
2018-11-01 07:10:15 +00:00
|
|
|
- st::langsRadio.diameter
|
|
|
|
- st::passportRowPadding.left()
|
|
|
|
- right
|
2018-10-30 11:23:54 +00:00
|
|
|
- st::passportRowIconSkip;
|
2017-04-18 15:21:03 +00:00
|
|
|
}
|
2017-04-13 17:59:05 +00:00
|
|
|
|
2018-10-30 11:23:54 +00:00
|
|
|
int Rows::countAvailableWidth() const {
|
|
|
|
return countAvailableWidth(width());
|
|
|
|
}
|
2017-04-13 17:59:05 +00:00
|
|
|
|
2018-10-30 11:23:54 +00:00
|
|
|
void Rows::paintEvent(QPaintEvent *e) {
|
|
|
|
Painter p(this);
|
|
|
|
|
|
|
|
const auto ms = getms();
|
|
|
|
const auto clip = e->rect();
|
|
|
|
|
2018-11-01 07:10:15 +00:00
|
|
|
const auto checkLeft = st::passportRowPadding.left();
|
|
|
|
const auto left = checkLeft
|
|
|
|
+ st::langsRadio.diameter
|
|
|
|
+ st::passportRowPadding.left();
|
2018-10-30 11:23:54 +00:00
|
|
|
const auto availableWidth = countAvailableWidth();
|
2018-11-01 07:10:15 +00:00
|
|
|
const auto menu = menuToggleArea();
|
|
|
|
const auto selectedIndex = (_menuShownIndex >= 0)
|
|
|
|
? _menuShownIndex
|
|
|
|
: indexFromSelection(_pressed.has_value() ? _pressed : _selected);
|
2018-10-30 11:23:54 +00:00
|
|
|
for (auto i = 0, till = count(); i != till; ++i) {
|
|
|
|
const auto &row = rowByIndex(i);
|
|
|
|
if (row.top + row.height <= clip.y()) {
|
|
|
|
continue;
|
|
|
|
} else if (row.top >= clip.y() + clip.height()) {
|
|
|
|
break;
|
|
|
|
}
|
2018-11-01 07:10:15 +00:00
|
|
|
p.setOpacity(row.removed ? st::stickersRowDisabledOpacity : 1.);
|
2018-10-30 11:23:54 +00:00
|
|
|
p.translate(0, row.top);
|
|
|
|
const auto guard = gsl::finally([&] { p.translate(0, -row.top); });
|
|
|
|
|
2018-11-01 07:10:15 +00:00
|
|
|
const auto selected = (selectedIndex == i);
|
|
|
|
if (selected && !row.removed) {
|
2018-10-30 11:23:54 +00:00
|
|
|
p.fillRect(0, 0, width(), row.height, st::windowBgOver);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (row.ripple) {
|
|
|
|
row.ripple->paint(p, 0, 0, width(), ms);
|
|
|
|
if (row.ripple->empty()) {
|
|
|
|
row.ripple.reset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-01 07:10:15 +00:00
|
|
|
const auto checkTop = (row.height - st::defaultRadio.diameter) / 2;
|
|
|
|
row.check->paint(p, checkLeft, checkTop, width(), ms);
|
|
|
|
|
2018-10-30 11:23:54 +00:00
|
|
|
auto top = st::passportRowPadding.top();
|
|
|
|
|
|
|
|
p.setPen(st::passportRowTitleFg);
|
|
|
|
row.title.drawLeft(p, left, top, availableWidth, width());
|
|
|
|
top += row.titleHeight + st::passportRowSkip;
|
|
|
|
|
|
|
|
p.setPen(selected ? st::windowSubTextFgOver : st::windowSubTextFg);
|
|
|
|
row.description.drawLeft(p, left, top, availableWidth, width());
|
|
|
|
top += row.descriptionHeight + st::passportRowPadding.bottom();
|
|
|
|
|
2018-11-01 07:10:15 +00:00
|
|
|
if (hasMenu(&row)) {
|
|
|
|
p.setOpacity(1.);
|
|
|
|
if (selected && row.removed) {
|
|
|
|
PainterHighQualityEnabler hq(p);
|
|
|
|
p.setPen(Qt::NoPen);
|
|
|
|
p.setBrush(st::windowBgOver);
|
|
|
|
p.drawEllipse(menu);
|
|
|
|
}
|
|
|
|
if (row.menuToggleRipple) {
|
|
|
|
row.menuToggleRipple->paint(p, menu.x(), menu.y(), width(), ms);
|
|
|
|
if (row.menuToggleRipple->empty()) {
|
|
|
|
row.menuToggleRipple.reset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
(selected
|
|
|
|
? st::topBarMenuToggle.iconOver
|
|
|
|
: st::topBarMenuToggle.icon).paintInCenter(p, menu);
|
2018-10-30 11:23:54 +00:00
|
|
|
}
|
|
|
|
}
|
2015-01-06 11:13:43 +00:00
|
|
|
}
|
|
|
|
|
2018-10-30 11:23:54 +00:00
|
|
|
Content::Content(
|
|
|
|
QWidget *parent,
|
2018-11-13 09:14:22 +00:00
|
|
|
const Languages &recent,
|
|
|
|
const Languages &official)
|
2018-10-30 11:23:54 +00:00
|
|
|
: RpWidget(parent) {
|
2018-11-13 09:14:22 +00:00
|
|
|
setupContent(recent, official);
|
2018-10-30 11:23:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Content::setupContent(
|
2018-11-13 09:14:22 +00:00
|
|
|
const Languages &recent,
|
|
|
|
const Languages &official) {
|
|
|
|
using namespace rpl::mappers;
|
|
|
|
|
2018-10-30 11:23:54 +00:00
|
|
|
const auto current = Lang::LanguageIdOrDefault(Lang::Current().id());
|
|
|
|
const auto content = Ui::CreateChild<Ui::VerticalLayout>(this);
|
2018-11-13 09:14:22 +00:00
|
|
|
const auto add = [&](const Languages &list, bool areOfficial) {
|
|
|
|
if (list.empty()) {
|
|
|
|
return (Rows*)nullptr;
|
|
|
|
}
|
|
|
|
const auto wrap = content->add(
|
|
|
|
object_ptr<Ui::SlideWrap<Ui::VerticalLayout>>(
|
|
|
|
content,
|
|
|
|
object_ptr<Ui::VerticalLayout>(content)));
|
|
|
|
const auto inner = wrap->entity();
|
|
|
|
inner->add(object_ptr<Ui::FixedHeightWidget>(
|
2018-10-30 11:23:54 +00:00
|
|
|
inner,
|
2018-11-13 09:14:22 +00:00
|
|
|
st::boxVerticalMargin));
|
|
|
|
const auto rows = inner->add(object_ptr<Rows>(
|
|
|
|
inner,
|
|
|
|
list,
|
|
|
|
current,
|
|
|
|
areOfficial));
|
2018-10-30 11:23:54 +00:00
|
|
|
inner->add(object_ptr<Ui::FixedHeightWidget>(
|
|
|
|
inner,
|
|
|
|
st::boxVerticalMargin));
|
|
|
|
|
2018-11-13 09:14:22 +00:00
|
|
|
rows->isEmpty() | rpl::start_with_next([=](bool empty) {
|
|
|
|
wrap->toggle(!empty, anim::type::instant);
|
|
|
|
}, rows->lifetime());
|
|
|
|
|
|
|
|
return rows;
|
2018-10-30 11:23:54 +00:00
|
|
|
};
|
2018-11-13 09:14:22 +00:00
|
|
|
const auto main = add(recent, false);
|
|
|
|
const auto divider = content->add(
|
|
|
|
object_ptr<Ui::SlideWrap<BoxContentDivider>>(
|
|
|
|
content,
|
|
|
|
object_ptr<BoxContentDivider>(content)));
|
|
|
|
const auto other = add(official, true);
|
|
|
|
Ui::ResizeFitChild(this, content);
|
2018-10-30 11:23:54 +00:00
|
|
|
|
2018-11-13 09:14:22 +00:00
|
|
|
if (main && other) {
|
2018-10-30 11:23:54 +00:00
|
|
|
rpl::combine(
|
2018-11-13 09:14:22 +00:00
|
|
|
main->isEmpty(),
|
|
|
|
other->isEmpty(),
|
|
|
|
_1 || _2
|
|
|
|
) | rpl::start_with_next([=](bool empty) {
|
|
|
|
divider->toggle(!empty, anim::type::instant);
|
2018-10-30 11:23:54 +00:00
|
|
|
}, divider->lifetime());
|
|
|
|
|
|
|
|
const auto excludeSelections = [](Rows *a, Rows *b) {
|
2018-11-01 07:10:15 +00:00
|
|
|
a->hasSelection(
|
2018-10-30 11:23:54 +00:00
|
|
|
) | rpl::filter(
|
2018-11-01 07:10:15 +00:00
|
|
|
_1
|
2018-10-30 11:23:54 +00:00
|
|
|
) | rpl::start_with_next([=] {
|
|
|
|
b->setSelected(-1);
|
|
|
|
}, a->lifetime());
|
|
|
|
};
|
|
|
|
excludeSelections(main, other);
|
|
|
|
excludeSelections(other, main);
|
2018-11-13 09:14:22 +00:00
|
|
|
} else {
|
|
|
|
divider->hide(anim::type::instant);
|
2018-10-30 11:23:54 +00:00
|
|
|
}
|
|
|
|
|
2018-11-13 09:14:22 +00:00
|
|
|
const auto count = [](Rows *widget) {
|
|
|
|
return widget ? widget->count() : 0;
|
|
|
|
};
|
|
|
|
const auto selected = [](Rows *widget) {
|
|
|
|
return widget ? widget->selected() : -1;
|
|
|
|
};
|
2018-10-30 11:23:54 +00:00
|
|
|
const auto rowsCount = [=] {
|
2018-11-13 09:14:22 +00:00
|
|
|
return count(main) + count(other);
|
2018-10-30 11:23:54 +00:00
|
|
|
};
|
|
|
|
const auto selectedIndex = [=] {
|
2018-11-13 09:14:22 +00:00
|
|
|
if (const auto index = selected(main); index >= 0) {
|
2018-10-30 11:23:54 +00:00
|
|
|
return index;
|
2018-11-13 09:14:22 +00:00
|
|
|
} else if (const auto index = selected(other); index >= 0) {
|
|
|
|
return count(main) + index;
|
2018-10-30 11:23:54 +00:00
|
|
|
}
|
2018-11-13 09:14:22 +00:00
|
|
|
return -1;
|
2018-10-30 11:23:54 +00:00
|
|
|
};
|
|
|
|
const auto setSelectedIndex = [=](int index) {
|
2018-11-13 09:14:22 +00:00
|
|
|
const auto first = count(main);
|
|
|
|
if (index >= first) {
|
|
|
|
if (main) {
|
|
|
|
main->setSelected(-1);
|
|
|
|
}
|
2018-10-30 11:23:54 +00:00
|
|
|
if (other) {
|
2018-11-13 09:14:22 +00:00
|
|
|
other->setSelected(index - first);
|
2018-10-30 11:23:54 +00:00
|
|
|
}
|
|
|
|
} else {
|
2018-11-13 09:14:22 +00:00
|
|
|
if (main) {
|
|
|
|
main->setSelected(index);
|
|
|
|
}
|
2018-10-30 11:23:54 +00:00
|
|
|
if (other) {
|
|
|
|
other->setSelected(-1);
|
2017-04-18 15:21:03 +00:00
|
|
|
}
|
|
|
|
}
|
2018-10-30 11:23:54 +00:00
|
|
|
};
|
|
|
|
const auto selectedCoords = [=] {
|
|
|
|
const auto coords = [=](Rows *rows, int index) {
|
|
|
|
const auto result = rows->rowScrollRequest(index);
|
|
|
|
const auto shift = rows->mapToGlobal({ 0, 0 }).y()
|
|
|
|
- mapToGlobal({ 0, 0 }).y();
|
|
|
|
return Ui::ScrollToRequest(
|
|
|
|
result.ymin + shift,
|
|
|
|
result.ymax + shift);
|
|
|
|
};
|
2018-11-13 09:14:22 +00:00
|
|
|
if (const auto index = selected(main); index >= 0) {
|
2018-10-30 11:23:54 +00:00
|
|
|
return coords(main, index);
|
2018-11-13 09:14:22 +00:00
|
|
|
} else if (const auto index = selected(other); index >= 0) {
|
2018-10-30 11:23:54 +00:00
|
|
|
return coords(other, index);
|
|
|
|
}
|
|
|
|
return Ui::ScrollToRequest(-1, -1);
|
|
|
|
};
|
|
|
|
_jump = [=](int rows) {
|
|
|
|
const auto count = rowsCount();
|
|
|
|
const auto now = selectedIndex();
|
|
|
|
if (now >= 0) {
|
|
|
|
const auto changed = now + rows;
|
|
|
|
if (changed < 0) {
|
|
|
|
setSelectedIndex((now > 0) ? 0 : -1);
|
|
|
|
} else if (changed >= count) {
|
|
|
|
setSelectedIndex(count - 1);
|
|
|
|
} else {
|
|
|
|
setSelectedIndex(changed);
|
|
|
|
}
|
|
|
|
} else if (rows > 0) {
|
|
|
|
setSelectedIndex(0);
|
|
|
|
}
|
|
|
|
return selectedCoords();
|
|
|
|
};
|
2018-11-13 09:14:22 +00:00
|
|
|
const auto filter = [](Rows *widget, const QString &query) {
|
|
|
|
if (widget) {
|
|
|
|
widget->filter(query);
|
2018-10-30 11:23:54 +00:00
|
|
|
}
|
|
|
|
};
|
2018-11-13 09:14:22 +00:00
|
|
|
_filter = [=](const QString &query) {
|
|
|
|
filter(main, query);
|
|
|
|
filter(other, query);
|
|
|
|
};
|
2018-10-30 11:23:54 +00:00
|
|
|
_activations = [=] {
|
2018-11-13 09:14:22 +00:00
|
|
|
if (!main && !other) {
|
|
|
|
return rpl::never<Language>() | rpl::type_erased();
|
|
|
|
} else if (!main) {
|
|
|
|
return other->activations();
|
|
|
|
} else if (!other) {
|
2018-10-30 11:23:54 +00:00
|
|
|
return main->activations();
|
|
|
|
}
|
|
|
|
return rpl::merge(
|
|
|
|
main->activations(),
|
|
|
|
other->activations()
|
|
|
|
) | rpl::type_erased();
|
|
|
|
};
|
|
|
|
_activateBySubmit = [=] {
|
|
|
|
if (selectedIndex() < 0) {
|
|
|
|
_jump(1);
|
|
|
|
}
|
2018-11-13 09:14:22 +00:00
|
|
|
if (main) {
|
|
|
|
main->activateSelected();
|
|
|
|
}
|
2018-10-30 11:23:54 +00:00
|
|
|
if (other) {
|
|
|
|
other->activateSelected();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
void Content::filter(const QString &query) {
|
|
|
|
_filter(query);
|
|
|
|
}
|
|
|
|
|
|
|
|
rpl::producer<Language> Content::activations() const {
|
|
|
|
return _activations();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Content::activateBySubmit() {
|
|
|
|
_activateBySubmit();
|
|
|
|
}
|
|
|
|
|
|
|
|
Ui::ScrollToRequest Content::jump(int rows) {
|
|
|
|
return _jump(rows);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
void LanguageBox::prepare() {
|
|
|
|
addButton(langFactory(lng_box_ok), [=] { closeBox(); });
|
|
|
|
|
|
|
|
setTitle(langFactory(lng_languages));
|
|
|
|
|
|
|
|
const auto select = createMultiSelect();
|
|
|
|
|
|
|
|
using namespace rpl::mappers;
|
|
|
|
|
2018-11-13 09:14:22 +00:00
|
|
|
const auto [recent, official] = PrepareLists();
|
2018-10-30 11:23:54 +00:00
|
|
|
const auto inner = setInnerWidget(
|
2018-11-13 09:14:22 +00:00
|
|
|
object_ptr<Content>(this, recent, official),
|
2018-10-30 11:23:54 +00:00
|
|
|
st::boxLayerScroll,
|
|
|
|
select->height());
|
2018-11-01 07:10:15 +00:00
|
|
|
inner->resizeToWidth(st::boxWidth);
|
2018-10-30 11:23:54 +00:00
|
|
|
|
|
|
|
const auto max = lifetime().make_state<int>(0);
|
|
|
|
rpl::combine(
|
|
|
|
inner->heightValue(),
|
|
|
|
select->heightValue(),
|
|
|
|
_1 + _2
|
|
|
|
) | rpl::start_with_next([=](int height) {
|
|
|
|
accumulate_max(*max, height);
|
2018-11-01 07:10:15 +00:00
|
|
|
setDimensions(st::boxWidth, qMin(*max, st::boxMaxListHeight));
|
2018-10-30 11:23:54 +00:00
|
|
|
}, inner->lifetime());
|
|
|
|
|
|
|
|
select->setSubmittedCallback([=](Qt::KeyboardModifiers) {
|
|
|
|
inner->activateBySubmit();
|
|
|
|
});
|
|
|
|
select->setQueryChangedCallback([=](const QString &query) {
|
|
|
|
inner->filter(query);
|
|
|
|
});
|
|
|
|
select->setCancelledCallback([=] {
|
|
|
|
select->clearQuery();
|
|
|
|
});
|
|
|
|
|
|
|
|
inner->activations(
|
|
|
|
) | rpl::start_with_next([=](const Language &language) {
|
|
|
|
// "#custom" is applied each time it's passed to switchToLanguage().
|
|
|
|
// So we check that the language really has changed.
|
|
|
|
if (language.id != Lang::Current().id()) {
|
2018-11-13 09:14:22 +00:00
|
|
|
Lang::CurrentCloudManager().switchToLanguage(language);
|
2018-10-30 11:23:54 +00:00
|
|
|
}
|
|
|
|
}, inner->lifetime());
|
|
|
|
|
|
|
|
_setInnerFocus = [=] {
|
|
|
|
select->setInnerFocus();
|
|
|
|
};
|
|
|
|
_jump = [=](int rows) {
|
|
|
|
return inner->jump(rows);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
void LanguageBox::keyPressEvent(QKeyEvent *e) {
|
|
|
|
const auto key = e->key();
|
2018-11-07 10:03:50 +00:00
|
|
|
if (key == Qt::Key_Escape) {
|
|
|
|
closeBox();
|
|
|
|
return;
|
|
|
|
}
|
2018-10-30 11:23:54 +00:00
|
|
|
const auto selected = [&] {
|
|
|
|
if (key == Qt::Key_Up) {
|
|
|
|
return _jump(-1);
|
|
|
|
} else if (key == Qt::Key_Down) {
|
|
|
|
return _jump(1);
|
|
|
|
} else if (key == Qt::Key_PageUp) {
|
|
|
|
return _jump(-rowsInPage());
|
|
|
|
} else if (key == Qt::Key_PageDown) {
|
|
|
|
return _jump(rowsInPage());
|
|
|
|
}
|
|
|
|
return Ui::ScrollToRequest(-1, -1);
|
|
|
|
}();
|
|
|
|
if (selected.ymin >= 0 && selected.ymax >= 0) {
|
|
|
|
onScrollToY(selected.ymin, selected.ymax);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int LanguageBox::rowsInPage() const {
|
2018-11-01 07:10:15 +00:00
|
|
|
return std::max(height() / Rows::DefaultRowHeight(), 1);
|
2018-10-30 11:23:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void LanguageBox::setInnerFocus() {
|
|
|
|
_setInnerFocus();
|
|
|
|
}
|
|
|
|
|
|
|
|
not_null<Ui::MultiSelect*> LanguageBox::createMultiSelect() {
|
|
|
|
const auto result = Ui::CreateChild<Ui::MultiSelect>(
|
|
|
|
this,
|
|
|
|
st::contactsMultiSelect,
|
|
|
|
langFactory(lng_participant_filter));
|
2018-11-01 07:10:15 +00:00
|
|
|
result->resizeToWidth(st::boxWidth);
|
2018-10-30 11:23:54 +00:00
|
|
|
result->moveToLeft(0, 0);
|
|
|
|
return result;
|
2014-12-19 21:33:22 +00:00
|
|
|
}
|
2018-09-05 21:01:50 +00:00
|
|
|
|
|
|
|
base::binary_guard LanguageBox::Show() {
|
|
|
|
auto result = base::binary_guard();
|
|
|
|
|
2019-01-21 13:42:21 +00:00
|
|
|
const auto manager = Core::App().langCloudManager();
|
2018-11-01 07:10:15 +00:00
|
|
|
if (manager->languageList().empty()) {
|
2018-09-05 21:01:50 +00:00
|
|
|
auto guard = std::make_shared<base::binary_guard>();
|
|
|
|
std::tie(result, *guard) = base::make_binary_guard();
|
|
|
|
auto alive = std::make_shared<std::unique_ptr<base::Subscription>>(
|
|
|
|
std::make_unique<base::Subscription>());
|
|
|
|
**alive = manager->languageListChanged().add_subscription([=] {
|
|
|
|
const auto show = guard->alive();
|
|
|
|
*alive = nullptr;
|
|
|
|
if (show) {
|
|
|
|
Ui::show(Box<LanguageBox>());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
Ui::show(Box<LanguageBox>());
|
|
|
|
}
|
|
|
|
manager->requestLanguageList();
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|