2017-10-04 12:39:59 +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.
|
2017-10-04 12:39:59 +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
|
2017-10-04 12:39:59 +00:00
|
|
|
*/
|
|
|
|
#include "info/media/info_media_list_widget.h"
|
|
|
|
|
2017-10-31 18:25:22 +00:00
|
|
|
#include "info/info_controller.h"
|
2017-10-04 17:15:08 +00:00
|
|
|
#include "overview/overview_layout.h"
|
2018-01-14 16:02:25 +00:00
|
|
|
#include "data/data_media_types.h"
|
|
|
|
#include "data/data_photo.h"
|
|
|
|
#include "data/data_document.h"
|
|
|
|
#include "data/data_session.h"
|
2019-12-05 08:32:33 +00:00
|
|
|
#include "data/data_file_origin.h"
|
2017-10-13 12:09:15 +00:00
|
|
|
#include "history/history_item.h"
|
2018-01-13 12:45:11 +00:00
|
|
|
#include "history/history.h"
|
2018-01-27 13:59:24 +00:00
|
|
|
#include "history/view/history_view_cursor_state.h"
|
2017-10-04 17:15:08 +00:00
|
|
|
#include "window/themes/window_theme.h"
|
2019-06-06 10:21:40 +00:00
|
|
|
#include "window/window_session_controller.h"
|
2017-12-07 13:15:27 +00:00
|
|
|
#include "window/window_peer_menu.h"
|
2017-10-25 16:32:22 +00:00
|
|
|
#include "ui/widgets/popup_menu.h"
|
2019-09-13 12:22:54 +00:00
|
|
|
#include "ui/ui_utility.h"
|
2019-09-16 11:14:06 +00:00
|
|
|
#include "ui/inactive_press.h"
|
2017-10-04 17:15:08 +00:00
|
|
|
#include "lang/lang_keys.h"
|
2019-07-24 11:45:24 +00:00
|
|
|
#include "main/main_session.h"
|
2017-10-25 16:32:22 +00:00
|
|
|
#include "mainwidget.h"
|
2019-08-16 12:44:20 +00:00
|
|
|
#include "mainwindow.h"
|
2017-10-04 17:15:08 +00:00
|
|
|
#include "styles/style_overview.h"
|
|
|
|
#include "styles/style_info.h"
|
2019-09-17 16:13:12 +00:00
|
|
|
#include "base/platform/base_platform_info.h"
|
2017-12-09 15:13:06 +00:00
|
|
|
#include "media/player/media_player_instance.h"
|
2017-11-07 13:13:41 +00:00
|
|
|
#include "boxes/peer_list_controllers.h"
|
2017-10-25 16:32:22 +00:00
|
|
|
#include "boxes/confirm_box.h"
|
|
|
|
#include "core/file_utilities.h"
|
2019-09-13 06:06:02 +00:00
|
|
|
#include "facades.h"
|
2017-10-04 17:15:08 +00:00
|
|
|
|
2019-09-04 07:19:15 +00:00
|
|
|
#include <QtWidgets/QApplication>
|
|
|
|
#include <QtGui/QClipboard>
|
|
|
|
|
2017-10-04 17:15:08 +00:00
|
|
|
namespace Layout = Overview::Layout;
|
|
|
|
|
2017-10-04 12:39:59 +00:00
|
|
|
namespace Info {
|
|
|
|
namespace Media {
|
2017-10-04 17:15:08 +00:00
|
|
|
namespace {
|
|
|
|
|
2017-10-05 17:32:34 +00:00
|
|
|
constexpr auto kPreloadedScreensCount = 4;
|
|
|
|
constexpr auto kPreloadIfLessThanScreens = 2;
|
|
|
|
constexpr auto kPreloadedScreensCountFull
|
|
|
|
= kPreloadedScreensCount + 1 + kPreloadedScreensCount;
|
2017-11-03 18:26:14 +00:00
|
|
|
constexpr auto kMediaCountForSearch = 10;
|
2017-10-04 17:15:08 +00:00
|
|
|
|
2017-10-05 15:35:52 +00:00
|
|
|
UniversalMsgId GetUniversalId(FullMsgId itemId) {
|
|
|
|
return (itemId.channel != 0)
|
2017-10-09 21:55:30 +00:00
|
|
|
? UniversalMsgId(itemId.msg)
|
|
|
|
: UniversalMsgId(itemId.msg - ServerMaxMsgId);
|
2017-10-05 15:35:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UniversalMsgId GetUniversalId(not_null<const HistoryItem*> item) {
|
|
|
|
return GetUniversalId(item->fullId());
|
|
|
|
}
|
|
|
|
|
2017-10-09 21:55:30 +00:00
|
|
|
UniversalMsgId GetUniversalId(not_null<const BaseLayout*> layout) {
|
2017-10-05 15:35:52 +00:00
|
|
|
return GetUniversalId(layout->getItem()->fullId());
|
|
|
|
}
|
2017-10-04 17:15:08 +00:00
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2017-10-13 19:07:04 +00:00
|
|
|
struct ListWidget::Context {
|
|
|
|
Layout::PaintContext layoutContext;
|
|
|
|
not_null<SelectedMap*> selected;
|
|
|
|
not_null<SelectedMap*> dragSelected;
|
|
|
|
DragSelectAction dragSelectAction;
|
|
|
|
};
|
2017-10-04 17:15:08 +00:00
|
|
|
|
|
|
|
class ListWidget::Section {
|
|
|
|
public:
|
|
|
|
Section(Type type) : _type(type) {
|
|
|
|
}
|
|
|
|
|
2017-10-09 21:55:30 +00:00
|
|
|
bool addItem(not_null<BaseLayout*> item);
|
2017-10-04 17:15:08 +00:00
|
|
|
bool empty() const {
|
|
|
|
return _items.empty();
|
|
|
|
}
|
|
|
|
|
2017-10-05 15:35:52 +00:00
|
|
|
UniversalMsgId minId() const {
|
|
|
|
Expects(!empty());
|
2019-03-15 12:09:05 +00:00
|
|
|
|
2017-10-05 15:35:52 +00:00
|
|
|
return _items.back().first;
|
|
|
|
}
|
|
|
|
UniversalMsgId maxId() const {
|
|
|
|
Expects(!empty());
|
2019-03-15 12:09:05 +00:00
|
|
|
|
2017-10-05 15:35:52 +00:00
|
|
|
return _items.front().first;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setTop(int top) {
|
|
|
|
_top = top;
|
|
|
|
}
|
|
|
|
int top() const {
|
|
|
|
return _top;
|
|
|
|
}
|
2017-10-04 17:15:08 +00:00
|
|
|
void resizeToWidth(int newWidth);
|
|
|
|
int height() const {
|
|
|
|
return _height;
|
|
|
|
}
|
|
|
|
|
2017-11-20 12:23:20 +00:00
|
|
|
int bottom() const {
|
|
|
|
return top() + height();
|
|
|
|
}
|
|
|
|
|
2017-10-05 15:35:52 +00:00
|
|
|
bool removeItem(UniversalMsgId universalId);
|
2017-10-05 17:32:34 +00:00
|
|
|
FoundItem findItemNearId(UniversalMsgId universalId) const;
|
2020-05-21 12:27:04 +00:00
|
|
|
FoundItem findItemDetails(not_null<BaseLayout*> item) const;
|
2017-10-05 17:32:34 +00:00
|
|
|
FoundItem findItemByPoint(QPoint point) const;
|
2017-10-05 15:35:52 +00:00
|
|
|
|
2017-10-04 17:15:08 +00:00
|
|
|
void paint(
|
|
|
|
Painter &p,
|
2017-10-13 19:07:04 +00:00
|
|
|
const Context &context,
|
2017-10-04 17:15:08 +00:00
|
|
|
QRect clip,
|
2017-10-13 19:07:04 +00:00
|
|
|
int outerWidth) const;
|
2017-10-04 17:15:08 +00:00
|
|
|
|
2017-10-05 17:32:34 +00:00
|
|
|
static int MinItemHeight(Type type, int width);
|
|
|
|
|
2017-10-04 17:15:08 +00:00
|
|
|
private:
|
2017-10-05 15:35:52 +00:00
|
|
|
using Items = base::flat_map<
|
|
|
|
UniversalMsgId,
|
2017-10-09 21:55:30 +00:00
|
|
|
not_null<BaseLayout*>,
|
2017-10-05 15:35:52 +00:00
|
|
|
std::greater<>>;
|
2017-10-04 17:15:08 +00:00
|
|
|
int headerHeight() const;
|
2017-10-09 21:55:30 +00:00
|
|
|
void appendItem(not_null<BaseLayout*> item);
|
|
|
|
void setHeader(not_null<BaseLayout*> item);
|
|
|
|
bool belongsHere(not_null<BaseLayout*> item) const;
|
2017-10-05 15:35:52 +00:00
|
|
|
Items::iterator findItemAfterTop(int top);
|
|
|
|
Items::const_iterator findItemAfterTop(int top) const;
|
|
|
|
Items::const_iterator findItemAfterBottom(
|
|
|
|
Items::const_iterator from,
|
|
|
|
int bottom) const;
|
2017-10-13 19:07:04 +00:00
|
|
|
QRect findItemRect(not_null<const BaseLayout*> item) const;
|
2017-10-05 17:32:34 +00:00
|
|
|
FoundItem completeResult(
|
2017-10-09 21:55:30 +00:00
|
|
|
not_null<BaseLayout*> item,
|
2017-10-05 17:32:34 +00:00
|
|
|
bool exact) const;
|
2017-10-13 19:07:04 +00:00
|
|
|
TextSelection itemSelection(
|
|
|
|
not_null<const BaseLayout*> item,
|
|
|
|
const Context &context) const;
|
2017-10-05 15:35:52 +00:00
|
|
|
|
|
|
|
int recountHeight() const;
|
|
|
|
void refreshHeight();
|
2017-10-04 17:15:08 +00:00
|
|
|
|
|
|
|
Type _type = Type::Photo;
|
2019-06-12 13:26:04 +00:00
|
|
|
Ui::Text::String _header;
|
2017-10-05 15:35:52 +00:00
|
|
|
Items _items;
|
2017-10-04 17:15:08 +00:00
|
|
|
int _itemsLeft = 0;
|
|
|
|
int _itemsTop = 0;
|
|
|
|
int _itemWidth = 0;
|
|
|
|
int _itemsInRow = 1;
|
2017-10-05 15:35:52 +00:00
|
|
|
mutable int _rowsCount = 0;
|
|
|
|
int _top = 0;
|
2017-10-04 17:15:08 +00:00
|
|
|
int _height = 0;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2017-10-13 19:07:04 +00:00
|
|
|
bool ListWidget::IsAfter(
|
2018-01-27 13:59:24 +00:00
|
|
|
const MouseState &a,
|
|
|
|
const MouseState &b) {
|
2017-10-13 19:07:04 +00:00
|
|
|
if (a.itemId != b.itemId) {
|
|
|
|
return (a.itemId < b.itemId);
|
|
|
|
}
|
|
|
|
auto xAfter = a.cursor.x() - b.cursor.x();
|
|
|
|
auto yAfter = a.cursor.y() - b.cursor.y();
|
|
|
|
return (xAfter + yAfter >= 0);
|
|
|
|
}
|
|
|
|
|
2018-01-27 13:59:24 +00:00
|
|
|
bool ListWidget::SkipSelectFromItem(const MouseState &state) {
|
2017-10-13 19:07:04 +00:00
|
|
|
if (state.cursor.y() >= state.size.height()
|
2017-10-20 16:19:42 +00:00
|
|
|
|| state.cursor.x() >= state.size.width()) {
|
2017-10-13 19:07:04 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-01-27 13:59:24 +00:00
|
|
|
bool ListWidget::SkipSelectTillItem(const MouseState &state) {
|
2017-10-20 16:19:42 +00:00
|
|
|
if (state.cursor.x() < 0 || state.cursor.y() < 0) {
|
2017-10-13 19:07:04 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ListWidget::CachedItem::CachedItem(std::unique_ptr<BaseLayout> item)
|
|
|
|
: item(std::move(item)) {
|
|
|
|
}
|
|
|
|
|
2020-05-21 12:27:04 +00:00
|
|
|
ListWidget::CachedItem::CachedItem(CachedItem &&other) = default;
|
|
|
|
|
|
|
|
ListWidget::CachedItem &ListWidget::CachedItem::operator=(
|
|
|
|
CachedItem && other) = default;
|
|
|
|
|
2017-10-13 19:07:04 +00:00
|
|
|
ListWidget::CachedItem::~CachedItem() = default;
|
|
|
|
|
2017-10-09 21:55:30 +00:00
|
|
|
bool ListWidget::Section::addItem(not_null<BaseLayout*> item) {
|
2017-10-04 17:15:08 +00:00
|
|
|
if (_items.empty() || belongsHere(item)) {
|
|
|
|
if (_items.empty()) setHeader(item);
|
|
|
|
appendItem(item);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-10-09 21:55:30 +00:00
|
|
|
void ListWidget::Section::setHeader(not_null<BaseLayout*> item) {
|
2017-10-04 17:15:08 +00:00
|
|
|
auto text = [&] {
|
2018-02-03 19:52:35 +00:00
|
|
|
auto date = item->dateTime().date();
|
2017-10-04 17:15:08 +00:00
|
|
|
switch (_type) {
|
|
|
|
case Type::Photo:
|
|
|
|
case Type::Video:
|
|
|
|
case Type::RoundFile:
|
2018-11-05 08:22:05 +00:00
|
|
|
case Type::RoundVoiceFile:
|
2017-10-04 17:15:08 +00:00
|
|
|
case Type::File:
|
|
|
|
return langMonthFull(date);
|
|
|
|
|
|
|
|
case Type::Link:
|
|
|
|
return langDayOfMonthFull(date);
|
|
|
|
|
|
|
|
case Type::MusicFile:
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
Unexpected("Type in ListWidget::Section::setHeader()");
|
|
|
|
}();
|
|
|
|
_header.setText(st::infoMediaHeaderStyle, text);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ListWidget::Section::belongsHere(
|
2017-10-09 21:55:30 +00:00
|
|
|
not_null<BaseLayout*> item) const {
|
2017-10-04 17:15:08 +00:00
|
|
|
Expects(!_items.empty());
|
2018-09-01 10:56:17 +00:00
|
|
|
|
2018-02-03 19:52:35 +00:00
|
|
|
auto date = item->dateTime().date();
|
|
|
|
auto myDate = _items.back().second->dateTime().date();
|
2017-10-04 17:15:08 +00:00
|
|
|
|
|
|
|
switch (_type) {
|
|
|
|
case Type::Photo:
|
|
|
|
case Type::Video:
|
|
|
|
case Type::RoundFile:
|
2018-11-05 08:22:05 +00:00
|
|
|
case Type::RoundVoiceFile:
|
2017-10-04 17:15:08 +00:00
|
|
|
case Type::File:
|
|
|
|
return date.year() == myDate.year()
|
|
|
|
&& date.month() == myDate.month();
|
|
|
|
|
|
|
|
case Type::Link:
|
|
|
|
return date.year() == myDate.year()
|
|
|
|
&& date.month() == myDate.month()
|
|
|
|
&& date.day() == myDate.day();
|
|
|
|
|
|
|
|
case Type::MusicFile:
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
Unexpected("Type in ListWidget::Section::belongsHere()");
|
|
|
|
}
|
|
|
|
|
2017-10-09 21:55:30 +00:00
|
|
|
void ListWidget::Section::appendItem(not_null<BaseLayout*> item) {
|
2017-10-05 15:35:52 +00:00
|
|
|
_items.emplace(GetUniversalId(item), item);
|
|
|
|
}
|
2017-10-04 17:15:08 +00:00
|
|
|
|
2017-10-05 15:35:52 +00:00
|
|
|
bool ListWidget::Section::removeItem(UniversalMsgId universalId) {
|
|
|
|
if (auto it = _items.find(universalId); it != _items.end()) {
|
|
|
|
it = _items.erase(it);
|
|
|
|
refreshHeight();
|
|
|
|
return true;
|
2017-10-04 17:15:08 +00:00
|
|
|
}
|
2017-10-05 15:35:52 +00:00
|
|
|
return false;
|
2017-10-04 17:15:08 +00:00
|
|
|
}
|
|
|
|
|
2017-10-05 15:35:52 +00:00
|
|
|
QRect ListWidget::Section::findItemRect(
|
2017-10-13 19:07:04 +00:00
|
|
|
not_null<const BaseLayout*> item) const {
|
2017-10-05 15:35:52 +00:00
|
|
|
auto position = item->position();
|
|
|
|
auto top = position / _itemsInRow;
|
|
|
|
auto indexInRow = position % _itemsInRow;
|
|
|
|
auto left = _itemsLeft
|
|
|
|
+ indexInRow * (_itemWidth + st::infoMediaSkip);
|
|
|
|
return QRect(left, top, _itemWidth, item->height());
|
|
|
|
}
|
|
|
|
|
2017-10-05 17:32:34 +00:00
|
|
|
auto ListWidget::Section::completeResult(
|
2017-10-09 21:55:30 +00:00
|
|
|
not_null<BaseLayout*> item,
|
2017-10-05 17:32:34 +00:00
|
|
|
bool exact) const -> FoundItem {
|
|
|
|
return { item, findItemRect(item), exact };
|
|
|
|
}
|
|
|
|
|
|
|
|
auto ListWidget::Section::findItemByPoint(
|
|
|
|
QPoint point) const -> FoundItem {
|
|
|
|
Expects(!_items.empty());
|
|
|
|
auto itemIt = findItemAfterTop(point.y());
|
|
|
|
if (itemIt == _items.end()) {
|
|
|
|
--itemIt;
|
|
|
|
}
|
|
|
|
auto item = itemIt->second;
|
|
|
|
auto rect = findItemRect(item);
|
2017-10-13 19:07:04 +00:00
|
|
|
if (point.y() >= rect.top()) {
|
|
|
|
auto shift = floorclamp(
|
|
|
|
point.x(),
|
|
|
|
(_itemWidth + st::infoMediaSkip),
|
|
|
|
0,
|
|
|
|
_itemsInRow);
|
|
|
|
while (shift-- && itemIt != _items.end()) {
|
|
|
|
++itemIt;
|
|
|
|
}
|
|
|
|
if (itemIt == _items.end()) {
|
|
|
|
--itemIt;
|
|
|
|
}
|
|
|
|
item = itemIt->second;
|
|
|
|
rect = findItemRect(item);
|
|
|
|
}
|
2017-10-05 17:32:34 +00:00
|
|
|
return { item, rect, rect.contains(point) };
|
|
|
|
}
|
|
|
|
|
2020-05-21 12:27:04 +00:00
|
|
|
auto ListWidget::Section::findItemNearId(UniversalMsgId universalId) const
|
|
|
|
-> FoundItem {
|
2017-10-05 17:32:34 +00:00
|
|
|
Expects(!_items.empty());
|
2020-05-21 12:27:04 +00:00
|
|
|
|
2017-11-20 12:23:20 +00:00
|
|
|
auto itemIt = ranges::lower_bound(
|
2017-10-05 17:32:34 +00:00
|
|
|
_items,
|
|
|
|
universalId,
|
2017-11-20 12:23:20 +00:00
|
|
|
std::greater<>(),
|
|
|
|
[](const auto &item) -> UniversalMsgId { return item.first; });
|
2017-10-05 17:32:34 +00:00
|
|
|
if (itemIt == _items.end()) {
|
|
|
|
--itemIt;
|
|
|
|
}
|
|
|
|
auto item = itemIt->second;
|
|
|
|
auto exact = (GetUniversalId(item) == universalId);
|
|
|
|
return { item, findItemRect(item), exact };
|
|
|
|
}
|
|
|
|
|
2020-05-21 12:27:04 +00:00
|
|
|
auto ListWidget::Section::findItemDetails(not_null<BaseLayout*> item) const
|
|
|
|
-> FoundItem {
|
|
|
|
return { item, findItemRect(item), true };
|
|
|
|
}
|
|
|
|
|
2017-10-05 15:35:52 +00:00
|
|
|
auto ListWidget::Section::findItemAfterTop(
|
|
|
|
int top) -> Items::iterator {
|
2017-11-20 12:23:20 +00:00
|
|
|
return ranges::lower_bound(
|
2017-10-05 15:35:52 +00:00
|
|
|
_items,
|
|
|
|
top,
|
2017-11-20 12:23:20 +00:00
|
|
|
std::less_equal<>(),
|
|
|
|
[this](const auto &item) {
|
2017-10-05 15:35:52 +00:00
|
|
|
auto itemTop = item.second->position() / _itemsInRow;
|
2017-11-20 12:23:20 +00:00
|
|
|
return itemTop + item.second->height();
|
2017-10-05 15:35:52 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
auto ListWidget::Section::findItemAfterTop(
|
|
|
|
int top) const -> Items::const_iterator {
|
2017-11-20 12:23:20 +00:00
|
|
|
return ranges::lower_bound(
|
2017-10-05 15:35:52 +00:00
|
|
|
_items,
|
|
|
|
top,
|
2017-11-20 12:23:20 +00:00
|
|
|
std::less_equal<>(),
|
|
|
|
[this](const auto &item) {
|
|
|
|
auto itemTop = item.second->position() / _itemsInRow;
|
|
|
|
return itemTop + item.second->height();
|
|
|
|
});
|
2017-10-05 15:35:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
auto ListWidget::Section::findItemAfterBottom(
|
|
|
|
Items::const_iterator from,
|
|
|
|
int bottom) const -> Items::const_iterator {
|
2017-11-20 12:23:20 +00:00
|
|
|
return ranges::lower_bound(
|
2017-10-05 15:35:52 +00:00
|
|
|
from,
|
|
|
|
_items.end(),
|
|
|
|
bottom,
|
2017-11-20 12:23:20 +00:00
|
|
|
std::less<>(),
|
|
|
|
[this](const auto &item) {
|
2017-10-05 15:35:52 +00:00
|
|
|
auto itemTop = item.second->position() / _itemsInRow;
|
2017-11-20 12:23:20 +00:00
|
|
|
return itemTop;
|
2017-10-05 15:35:52 +00:00
|
|
|
});
|
2017-10-04 17:15:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ListWidget::Section::paint(
|
|
|
|
Painter &p,
|
2017-10-13 19:07:04 +00:00
|
|
|
const Context &context,
|
2017-10-04 17:15:08 +00:00
|
|
|
QRect clip,
|
2017-10-13 19:07:04 +00:00
|
|
|
int outerWidth) const {
|
2017-10-04 17:15:08 +00:00
|
|
|
auto baseIndex = 0;
|
|
|
|
auto header = headerHeight();
|
2017-10-05 15:35:52 +00:00
|
|
|
if (QRect(0, 0, outerWidth, header).intersects(clip)) {
|
2017-10-04 17:15:08 +00:00
|
|
|
p.setPen(st::infoMediaHeaderFg);
|
|
|
|
_header.drawLeftElided(
|
|
|
|
p,
|
|
|
|
st::infoMediaHeaderPosition.x(),
|
|
|
|
st::infoMediaHeaderPosition.y(),
|
|
|
|
outerWidth - 2 * st::infoMediaHeaderPosition.x(),
|
|
|
|
outerWidth);
|
|
|
|
}
|
2017-10-05 15:35:52 +00:00
|
|
|
auto top = header + _itemsTop;
|
|
|
|
auto fromcol = floorclamp(
|
2017-10-04 17:15:08 +00:00
|
|
|
clip.x() - _itemsLeft,
|
|
|
|
_itemWidth,
|
|
|
|
0,
|
|
|
|
_itemsInRow);
|
2017-10-05 15:35:52 +00:00
|
|
|
auto tillcol = ceilclamp(
|
2017-10-04 17:15:08 +00:00
|
|
|
clip.x() + clip.width() - _itemsLeft,
|
|
|
|
_itemWidth,
|
|
|
|
0,
|
|
|
|
_itemsInRow);
|
2017-10-13 19:07:04 +00:00
|
|
|
auto localContext = context.layoutContext;
|
|
|
|
localContext.isAfterDate = (header > 0);
|
2017-10-04 17:15:08 +00:00
|
|
|
|
2017-10-05 15:35:52 +00:00
|
|
|
auto fromIt = findItemAfterTop(clip.y());
|
|
|
|
auto tillIt = findItemAfterBottom(
|
|
|
|
fromIt,
|
|
|
|
clip.y() + clip.height());
|
|
|
|
for (auto it = fromIt; it != tillIt; ++it) {
|
|
|
|
auto item = it->second;
|
|
|
|
auto rect = findItemRect(item);
|
2017-10-13 19:07:04 +00:00
|
|
|
localContext.isAfterDate = (header > 0)
|
2017-10-05 15:35:52 +00:00
|
|
|
&& (rect.y() <= header + _itemsTop);
|
|
|
|
if (rect.intersects(clip)) {
|
|
|
|
p.translate(rect.topLeft());
|
2017-10-04 17:15:08 +00:00
|
|
|
item->paint(
|
|
|
|
p,
|
2017-10-05 15:35:52 +00:00
|
|
|
clip.translated(-rect.topLeft()),
|
2017-10-13 19:07:04 +00:00
|
|
|
itemSelection(item, context),
|
|
|
|
&localContext);
|
2017-10-05 15:35:52 +00:00
|
|
|
p.translate(-rect.topLeft());
|
2017-10-04 17:15:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-13 19:07:04 +00:00
|
|
|
TextSelection ListWidget::Section::itemSelection(
|
|
|
|
not_null<const BaseLayout*> item,
|
|
|
|
const Context &context) const {
|
|
|
|
auto universalId = GetUniversalId(item);
|
|
|
|
auto dragSelectAction = context.dragSelectAction;
|
|
|
|
if (dragSelectAction != DragSelectAction::None) {
|
|
|
|
auto i = context.dragSelected->find(universalId);
|
|
|
|
if (i != context.dragSelected->end()) {
|
|
|
|
return (dragSelectAction == DragSelectAction::Selecting)
|
|
|
|
? FullSelection
|
|
|
|
: TextSelection();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
auto i = context.selected->find(universalId);
|
|
|
|
return (i == context.selected->cend())
|
|
|
|
? TextSelection()
|
|
|
|
: i->second.text;
|
|
|
|
}
|
|
|
|
|
2017-10-04 17:15:08 +00:00
|
|
|
int ListWidget::Section::headerHeight() const {
|
|
|
|
return _header.isEmpty() ? 0 : st::infoMediaHeaderHeight;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ListWidget::Section::resizeToWidth(int newWidth) {
|
|
|
|
auto minWidth = st::infoMediaMinGridSize + st::infoMediaSkip * 2;
|
|
|
|
if (newWidth < minWidth) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-10-09 21:55:30 +00:00
|
|
|
auto resizeOneColumn = [&](int itemsLeft, int itemWidth) {
|
|
|
|
_itemsLeft = itemsLeft;
|
|
|
|
_itemsTop = 0;
|
|
|
|
_itemsInRow = 1;
|
|
|
|
_itemWidth = itemWidth;
|
|
|
|
for (auto &item : _items) {
|
|
|
|
item.second->resizeGetHeight(_itemWidth);
|
|
|
|
}
|
|
|
|
};
|
2017-10-04 17:15:08 +00:00
|
|
|
switch (_type) {
|
|
|
|
case Type::Photo:
|
|
|
|
case Type::Video:
|
|
|
|
case Type::RoundFile: {
|
|
|
|
_itemsLeft = st::infoMediaSkip;
|
|
|
|
_itemsTop = st::infoMediaSkip;
|
|
|
|
_itemsInRow = (newWidth - _itemsLeft)
|
|
|
|
/ (st::infoMediaMinGridSize + st::infoMediaSkip);
|
|
|
|
_itemWidth = ((newWidth - _itemsLeft) / _itemsInRow)
|
|
|
|
- st::infoMediaSkip;
|
2017-10-05 15:35:52 +00:00
|
|
|
for (auto &item : _items) {
|
|
|
|
item.second->resizeGetHeight(_itemWidth);
|
|
|
|
}
|
2017-10-04 17:15:08 +00:00
|
|
|
} break;
|
|
|
|
|
2018-11-05 08:22:05 +00:00
|
|
|
case Type::RoundVoiceFile:
|
2017-10-05 15:35:52 +00:00
|
|
|
case Type::MusicFile:
|
2017-10-09 21:55:30 +00:00
|
|
|
resizeOneColumn(0, newWidth);
|
2017-10-04 17:15:08 +00:00
|
|
|
break;
|
2017-10-09 21:55:30 +00:00
|
|
|
case Type::File:
|
|
|
|
case Type::Link: {
|
|
|
|
auto itemsLeft = st::infoMediaHeaderPosition.x();
|
|
|
|
auto itemWidth = newWidth - 2 * itemsLeft;
|
|
|
|
resizeOneColumn(itemsLeft, itemWidth);
|
|
|
|
} break;
|
2017-10-04 17:15:08 +00:00
|
|
|
}
|
|
|
|
|
2017-10-05 15:35:52 +00:00
|
|
|
refreshHeight();
|
|
|
|
}
|
|
|
|
|
2017-10-05 17:32:34 +00:00
|
|
|
int ListWidget::Section::MinItemHeight(Type type, int width) {
|
|
|
|
auto &songSt = st::overviewFileLayout;
|
|
|
|
switch (type) {
|
|
|
|
case Type::Photo:
|
|
|
|
case Type::Video:
|
|
|
|
case Type::RoundFile: {
|
|
|
|
auto itemsLeft = st::infoMediaSkip;
|
|
|
|
auto itemsInRow = (width - itemsLeft)
|
|
|
|
/ (st::infoMediaMinGridSize + st::infoMediaSkip);
|
|
|
|
return (st::infoMediaMinGridSize + st::infoMediaSkip) / itemsInRow;
|
|
|
|
} break;
|
|
|
|
|
2018-11-05 08:22:05 +00:00
|
|
|
case Type::RoundVoiceFile:
|
2017-10-05 17:32:34 +00:00
|
|
|
return songSt.songPadding.top() + songSt.songThumbSize + songSt.songPadding.bottom() + st::lineWidth;
|
|
|
|
case Type::File:
|
|
|
|
return songSt.filePadding.top() + songSt.fileThumbSize + songSt.filePadding.bottom() + st::lineWidth;
|
|
|
|
case Type::MusicFile:
|
|
|
|
return songSt.songPadding.top() + songSt.songThumbSize + songSt.songPadding.bottom();
|
|
|
|
case Type::Link:
|
|
|
|
return st::linksPhotoSize + st::linksMargin.top() + st::linksMargin.bottom() + st::linksBorder;
|
|
|
|
}
|
|
|
|
Unexpected("Type in ListWidget::Section::MinItemHeight()");
|
|
|
|
}
|
|
|
|
|
2017-10-05 15:35:52 +00:00
|
|
|
int ListWidget::Section::recountHeight() const {
|
|
|
|
auto result = headerHeight();
|
|
|
|
|
|
|
|
switch (_type) {
|
|
|
|
case Type::Photo:
|
|
|
|
case Type::Video:
|
|
|
|
case Type::RoundFile: {
|
|
|
|
auto itemHeight = _itemWidth + st::infoMediaSkip;
|
|
|
|
auto index = 0;
|
|
|
|
result += _itemsTop;
|
|
|
|
for (auto &item : _items) {
|
|
|
|
item.second->setPosition(_itemsInRow * result + index);
|
|
|
|
if (++index == _itemsInRow) {
|
|
|
|
result += itemHeight;
|
|
|
|
index = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (_items.size() % _itemsInRow) {
|
|
|
|
_rowsCount = int(_items.size()) / _itemsInRow + 1;
|
|
|
|
result += itemHeight;
|
|
|
|
} else {
|
|
|
|
_rowsCount = int(_items.size()) / _itemsInRow;
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
|
2018-11-05 08:22:05 +00:00
|
|
|
case Type::RoundVoiceFile:
|
2017-10-05 15:35:52 +00:00
|
|
|
case Type::File:
|
|
|
|
case Type::MusicFile:
|
|
|
|
case Type::Link:
|
|
|
|
for (auto &item : _items) {
|
|
|
|
item.second->setPosition(result);
|
|
|
|
result += item.second->height();
|
2017-10-04 17:15:08 +00:00
|
|
|
}
|
2017-10-05 15:35:52 +00:00
|
|
|
_rowsCount = _items.size();
|
|
|
|
break;
|
2017-10-04 17:15:08 +00:00
|
|
|
}
|
2017-10-05 15:35:52 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ListWidget::Section::refreshHeight() {
|
|
|
|
_height = recountHeight();
|
2017-10-04 17:15:08 +00:00
|
|
|
}
|
2017-10-04 12:39:59 +00:00
|
|
|
|
|
|
|
ListWidget::ListWidget(
|
|
|
|
QWidget *parent,
|
2017-12-09 15:13:06 +00:00
|
|
|
not_null<AbstractController*> controller)
|
2017-10-04 12:39:59 +00:00
|
|
|
: RpWidget(parent)
|
|
|
|
, _controller(controller)
|
2018-01-22 19:51:38 +00:00
|
|
|
, _peer(_controller->key().peer())
|
2017-10-31 18:25:22 +00:00
|
|
|
, _migrated(_controller->migrated())
|
|
|
|
, _type(_controller->section().mediaType())
|
2017-10-05 17:32:34 +00:00
|
|
|
, _slice(sliceKey(_universalAroundId)) {
|
2017-12-25 19:26:08 +00:00
|
|
|
setMouseTracking(true);
|
2017-10-05 15:35:52 +00:00
|
|
|
start();
|
|
|
|
}
|
|
|
|
|
2019-07-24 11:45:24 +00:00
|
|
|
Main::Session &ListWidget::session() const {
|
2019-07-24 11:13:51 +00:00
|
|
|
return _controller->session();
|
|
|
|
}
|
|
|
|
|
2017-10-05 15:35:52 +00:00
|
|
|
void ListWidget::start() {
|
2017-11-03 18:26:14 +00:00
|
|
|
_controller->setSearchEnabledByContent(false);
|
2017-12-22 07:05:20 +00:00
|
|
|
ObservableViewer(
|
|
|
|
*Window::Theme::Background()
|
|
|
|
) | rpl::start_with_next([this](const auto &update) {
|
|
|
|
if (update.paletteChanged()) {
|
|
|
|
invalidatePaletteCache();
|
|
|
|
}
|
|
|
|
}, lifetime());
|
2020-07-01 08:03:34 +00:00
|
|
|
|
|
|
|
session().downloaderTaskFinished(
|
|
|
|
) | rpl::start_with_next([=] {
|
|
|
|
update();
|
|
|
|
}, lifetime());
|
|
|
|
|
2019-07-24 11:13:51 +00:00
|
|
|
session().data().itemLayoutChanged(
|
2018-01-21 19:52:44 +00:00
|
|
|
) | rpl::start_with_next([this](auto item) {
|
|
|
|
itemLayoutChanged(item);
|
|
|
|
}, lifetime());
|
2020-07-01 08:03:34 +00:00
|
|
|
|
2019-07-24 11:13:51 +00:00
|
|
|
session().data().itemRemoved(
|
2017-12-22 07:05:20 +00:00
|
|
|
) | rpl::start_with_next([this](auto item) {
|
|
|
|
itemRemoved(item);
|
|
|
|
}, lifetime());
|
2020-07-01 08:03:34 +00:00
|
|
|
|
2019-07-24 11:13:51 +00:00
|
|
|
session().data().itemRepaintRequest(
|
2017-12-22 07:05:20 +00:00
|
|
|
) | rpl::start_with_next([this](auto item) {
|
|
|
|
repaintItem(item);
|
|
|
|
}, lifetime());
|
2020-07-01 08:03:34 +00:00
|
|
|
|
2017-12-22 07:05:20 +00:00
|
|
|
_controller->mediaSourceQueryValue(
|
|
|
|
) | rpl::start_with_next([this]{
|
|
|
|
restart();
|
|
|
|
}, lifetime());
|
2017-10-05 15:35:52 +00:00
|
|
|
}
|
|
|
|
|
2017-11-21 09:20:56 +00:00
|
|
|
rpl::producer<int> ListWidget::scrollToRequests() const {
|
|
|
|
return _scrollToRequests.events();
|
|
|
|
}
|
|
|
|
|
|
|
|
rpl::producer<SelectedItems> ListWidget::selectedListValue() const {
|
|
|
|
return _selectedListStream.events_starting_with(
|
|
|
|
collectSelectedItems());
|
|
|
|
}
|
|
|
|
|
2017-12-09 15:13:06 +00:00
|
|
|
QRect ListWidget::getCurrentSongGeometry() {
|
|
|
|
const auto type = AudioMsgId::Type::Song;
|
|
|
|
const auto current = ::Media::Player::instance()->current(type);
|
|
|
|
const auto fullMsgId = current.contextId();
|
|
|
|
if (fullMsgId && isPossiblyMyId(fullMsgId)) {
|
|
|
|
if (const auto item = findItemById(GetUniversalId(fullMsgId))) {
|
|
|
|
return item->geometry;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return QRect(0, 0, width(), 0);
|
|
|
|
}
|
|
|
|
|
2017-10-30 19:24:20 +00:00
|
|
|
void ListWidget::restart() {
|
|
|
|
mouseActionCancel();
|
|
|
|
|
|
|
|
_overLayout = nullptr;
|
|
|
|
_sections.clear();
|
|
|
|
_layouts.clear();
|
2020-05-21 12:27:04 +00:00
|
|
|
_heavyLayouts.clear();
|
2017-10-30 19:24:20 +00:00
|
|
|
|
|
|
|
_universalAroundId = kDefaultAroundId;
|
|
|
|
_idsLimit = kMinimalIdsLimit;
|
|
|
|
_slice = SparseIdsMergedSlice(sliceKey(_universalAroundId));
|
|
|
|
|
|
|
|
refreshViewer();
|
|
|
|
}
|
|
|
|
|
2017-10-05 15:35:52 +00:00
|
|
|
void ListWidget::itemRemoved(not_null<const HistoryItem*> item) {
|
2020-10-06 11:20:49 +00:00
|
|
|
if (!isMyItem(item)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto id = GetUniversalId(item);
|
|
|
|
|
|
|
|
auto needHeightRefresh = false;
|
|
|
|
auto sectionIt = findSectionByItem(id);
|
|
|
|
if (sectionIt != _sections.end()) {
|
|
|
|
if (sectionIt->removeItem(id)) {
|
|
|
|
auto top = sectionIt->top();
|
|
|
|
if (sectionIt->empty()) {
|
|
|
|
_sections.erase(sectionIt);
|
2017-10-05 15:35:52 +00:00
|
|
|
}
|
2020-10-06 11:20:49 +00:00
|
|
|
needHeightRefresh = true;
|
2017-10-05 15:35:52 +00:00
|
|
|
}
|
2020-10-06 11:20:49 +00:00
|
|
|
}
|
2017-10-09 21:55:30 +00:00
|
|
|
|
2020-10-06 11:20:49 +00:00
|
|
|
if (isItemLayout(item, _overLayout)) {
|
|
|
|
_overLayout = nullptr;
|
|
|
|
}
|
2017-10-09 21:55:30 +00:00
|
|
|
|
2020-10-06 11:20:49 +00:00
|
|
|
if (const auto i = _layouts.find(id); i != _layouts.end()) {
|
|
|
|
_heavyLayouts.remove(i->second.item.get());
|
|
|
|
_layouts.erase(i);
|
|
|
|
}
|
|
|
|
_dragSelected.remove(id);
|
2017-10-13 19:07:04 +00:00
|
|
|
|
2020-10-06 11:20:49 +00:00
|
|
|
if (const auto i = _selected.find(id); i != _selected.cend()) {
|
|
|
|
removeItemSelection(i);
|
|
|
|
}
|
2017-10-13 19:07:04 +00:00
|
|
|
|
2020-10-06 11:20:49 +00:00
|
|
|
if (needHeightRefresh) {
|
|
|
|
refreshHeight();
|
2017-10-13 19:07:04 +00:00
|
|
|
}
|
2020-10-06 11:20:49 +00:00
|
|
|
mouseActionUpdate(_mousePosition);
|
2017-10-13 19:07:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
FullMsgId ListWidget::computeFullId(
|
|
|
|
UniversalMsgId universalId) const {
|
|
|
|
Expects(universalId != 0);
|
|
|
|
auto peerChannel = [&] {
|
|
|
|
return _peer->isChannel() ? _peer->bareId() : NoChannel;
|
|
|
|
};
|
|
|
|
return (universalId > 0)
|
|
|
|
? FullMsgId(peerChannel(), universalId)
|
|
|
|
: FullMsgId(NoChannel, ServerMaxMsgId + universalId);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto ListWidget::collectSelectedItems() const -> SelectedItems {
|
|
|
|
auto convert = [&](
|
|
|
|
UniversalMsgId universalId,
|
|
|
|
const SelectionData &selection) {
|
|
|
|
auto result = SelectedItem(computeFullId(universalId));
|
|
|
|
result.canDelete = selection.canDelete;
|
|
|
|
result.canForward = selection.canForward;
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
auto transformation = [&](const auto &item) {
|
|
|
|
return convert(item.first, item.second);
|
|
|
|
};
|
2017-10-20 16:19:42 +00:00
|
|
|
auto items = SelectedItems(_type);
|
|
|
|
if (hasSelectedItems()) {
|
|
|
|
items.list.reserve(_selected.size());
|
|
|
|
std::transform(
|
|
|
|
_selected.begin(),
|
|
|
|
_selected.end(),
|
|
|
|
std::back_inserter(items.list),
|
|
|
|
transformation);
|
|
|
|
}
|
2017-10-13 19:07:04 +00:00
|
|
|
return items;
|
|
|
|
}
|
|
|
|
|
2017-12-06 10:13:38 +00:00
|
|
|
MessageIdsList ListWidget::collectSelectedIds() const {
|
|
|
|
const auto selected = collectSelectedItems();
|
|
|
|
return ranges::view::all(
|
|
|
|
selected.list
|
|
|
|
) | ranges::view::transform([](const SelectedItem &item) {
|
|
|
|
return item.msgId;
|
|
|
|
}) | ranges::to_vector;
|
2017-10-25 16:32:22 +00:00
|
|
|
}
|
|
|
|
|
2017-10-13 19:07:04 +00:00
|
|
|
void ListWidget::pushSelectedItems() {
|
2017-10-20 16:19:42 +00:00
|
|
|
_selectedListStream.fire(collectSelectedItems());
|
2017-10-13 19:07:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ListWidget::hasSelected() const {
|
|
|
|
return !_selected.empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ListWidget::isSelectedItem(
|
|
|
|
const SelectedMap::const_iterator &i) const {
|
|
|
|
return (i != _selected.end())
|
|
|
|
&& (i->second.text == FullSelection);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ListWidget::removeItemSelection(
|
|
|
|
const SelectedMap::const_iterator &i) {
|
|
|
|
Expects(i != _selected.cend());
|
|
|
|
_selected.erase(i);
|
|
|
|
if (_selected.empty()) {
|
|
|
|
update();
|
2017-10-05 15:35:52 +00:00
|
|
|
}
|
2017-10-20 16:19:42 +00:00
|
|
|
pushSelectedItems();
|
2017-10-05 15:35:52 +00:00
|
|
|
}
|
|
|
|
|
2017-10-13 19:07:04 +00:00
|
|
|
bool ListWidget::hasSelectedText() const {
|
|
|
|
return hasSelected()
|
|
|
|
&& !hasSelectedItems();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ListWidget::hasSelectedItems() const {
|
|
|
|
return isSelectedItem(_selected.cbegin());
|
|
|
|
}
|
|
|
|
|
2017-10-09 21:55:30 +00:00
|
|
|
void ListWidget::itemLayoutChanged(
|
|
|
|
not_null<const HistoryItem*> item) {
|
2017-10-13 19:07:04 +00:00
|
|
|
if (isItemLayout(item, _overLayout)) {
|
|
|
|
mouseActionUpdate();
|
2017-10-09 21:55:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ListWidget::repaintItem(const HistoryItem *item) {
|
|
|
|
if (item && isMyItem(item)) {
|
2017-10-05 15:35:52 +00:00
|
|
|
repaintItem(GetUniversalId(item));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ListWidget::repaintItem(UniversalMsgId universalId) {
|
2017-10-09 21:55:30 +00:00
|
|
|
if (auto item = findItemById(universalId)) {
|
2017-10-13 19:07:04 +00:00
|
|
|
repaintItem(item->geometry);
|
2017-10-05 15:35:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-09 21:55:30 +00:00
|
|
|
void ListWidget::repaintItem(const BaseLayout *item) {
|
|
|
|
if (item) {
|
|
|
|
repaintItem(GetUniversalId(item));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-13 19:07:04 +00:00
|
|
|
void ListWidget::repaintItem(QRect itemGeometry) {
|
|
|
|
rtlupdate(itemGeometry);
|
|
|
|
}
|
|
|
|
|
2017-10-09 21:55:30 +00:00
|
|
|
bool ListWidget::isMyItem(not_null<const HistoryItem*> item) const {
|
2017-10-05 15:35:52 +00:00
|
|
|
auto peer = item->history()->peer;
|
2017-10-31 18:25:22 +00:00
|
|
|
return (_peer == peer) || (_migrated == peer);
|
2017-10-04 17:15:08 +00:00
|
|
|
}
|
|
|
|
|
2017-10-27 17:41:38 +00:00
|
|
|
bool ListWidget::isPossiblyMyId(FullMsgId fullId) const {
|
|
|
|
return (fullId.channel != 0)
|
|
|
|
? (_peer->isChannel() && _peer->bareId() == fullId.channel)
|
2017-10-31 18:25:22 +00:00
|
|
|
: (!_peer->isChannel() || _migrated);
|
2017-10-27 17:41:38 +00:00
|
|
|
}
|
|
|
|
|
2017-10-09 21:55:30 +00:00
|
|
|
bool ListWidget::isItemLayout(
|
|
|
|
not_null<const HistoryItem*> item,
|
|
|
|
BaseLayout *layout) const {
|
|
|
|
return layout && (layout->getItem() == item);
|
|
|
|
}
|
|
|
|
|
2017-10-04 17:15:08 +00:00
|
|
|
void ListWidget::invalidatePaletteCache() {
|
|
|
|
for (auto &layout : _layouts) {
|
|
|
|
layout.second.item->invalidateCache();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-21 13:13:29 +00:00
|
|
|
void ListWidget::registerHeavyItem(not_null<const BaseLayout*> item) {
|
|
|
|
if (!_heavyLayouts.contains(item)) {
|
|
|
|
_heavyLayouts.emplace(item);
|
|
|
|
_heavyLayoutsInvalidated = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ListWidget::unregisterHeavyItem(not_null<const BaseLayout*> item) {
|
|
|
|
const auto i = _heavyLayouts.find(item);
|
|
|
|
if (i != _heavyLayouts.end()) {
|
|
|
|
_heavyLayouts.erase(i);
|
|
|
|
_heavyLayoutsInvalidated = true;
|
|
|
|
}
|
2020-05-21 12:27:04 +00:00
|
|
|
}
|
|
|
|
|
2017-10-29 15:32:01 +00:00
|
|
|
SparseIdsMergedSlice::Key ListWidget::sliceKey(
|
2017-10-05 17:32:34 +00:00
|
|
|
UniversalMsgId universalId) const {
|
2017-10-29 15:32:01 +00:00
|
|
|
using Key = SparseIdsMergedSlice::Key;
|
2017-10-31 18:25:22 +00:00
|
|
|
if (_migrated) {
|
|
|
|
return Key(_peer->id, _migrated->id, universalId);
|
2017-10-04 17:15:08 +00:00
|
|
|
}
|
2017-10-05 17:32:34 +00:00
|
|
|
if (universalId < 0) {
|
|
|
|
// Convert back to plain id for non-migrated histories.
|
|
|
|
universalId += ServerMaxMsgId;
|
|
|
|
}
|
2017-10-29 15:32:01 +00:00
|
|
|
return Key(_peer->id, 0, universalId);
|
2017-10-04 17:15:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ListWidget::refreshViewer() {
|
2017-10-05 17:32:34 +00:00
|
|
|
_viewerLifetime.destroy();
|
2017-11-12 14:35:20 +00:00
|
|
|
auto idForViewer = sliceKey(_universalAroundId).universalId;
|
2017-10-31 18:25:22 +00:00
|
|
|
_controller->mediaSource(
|
2017-11-12 14:35:20 +00:00
|
|
|
idForViewer,
|
2017-10-31 18:25:22 +00:00
|
|
|
_idsLimit,
|
2017-12-22 07:05:20 +00:00
|
|
|
_idsLimit
|
2018-01-09 17:08:31 +00:00
|
|
|
) | rpl::start_with_next([=](SparseIdsMergedSlice &&slice) {
|
2017-12-22 07:05:20 +00:00
|
|
|
if (!slice.fullCount()) {
|
|
|
|
// Don't display anything while full count is unknown.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_slice = std::move(slice);
|
|
|
|
if (auto nearest = _slice.nearest(idForViewer)) {
|
|
|
|
_universalAroundId = GetUniversalId(*nearest);
|
|
|
|
}
|
|
|
|
refreshRows();
|
|
|
|
}, _viewerLifetime);
|
2017-10-04 17:15:08 +00:00
|
|
|
}
|
|
|
|
|
2017-10-13 19:07:04 +00:00
|
|
|
BaseLayout *ListWidget::getLayout(UniversalMsgId universalId) {
|
2017-10-05 15:35:52 +00:00
|
|
|
auto it = _layouts.find(universalId);
|
2017-10-04 17:15:08 +00:00
|
|
|
if (it == _layouts.end()) {
|
2017-10-13 19:07:04 +00:00
|
|
|
if (auto layout = createLayout(universalId, _type)) {
|
2017-10-04 17:15:08 +00:00
|
|
|
layout->initDimensions();
|
2017-10-05 15:35:52 +00:00
|
|
|
it = _layouts.emplace(
|
|
|
|
universalId,
|
|
|
|
std::move(layout)).first;
|
2017-10-04 17:15:08 +00:00
|
|
|
} else {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
it->second.stale = false;
|
|
|
|
return it->second.item.get();
|
|
|
|
}
|
|
|
|
|
2017-10-09 21:55:30 +00:00
|
|
|
BaseLayout *ListWidget::getExistingLayout(
|
2017-10-13 19:07:04 +00:00
|
|
|
UniversalMsgId universalId) const {
|
2017-10-09 21:55:30 +00:00
|
|
|
auto it = _layouts.find(universalId);
|
|
|
|
return (it != _layouts.end())
|
|
|
|
? it->second.item.get()
|
|
|
|
: nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<BaseLayout> ListWidget::createLayout(
|
2017-10-13 19:07:04 +00:00
|
|
|
UniversalMsgId universalId,
|
2017-10-04 17:15:08 +00:00
|
|
|
Type type) {
|
2019-07-24 11:13:51 +00:00
|
|
|
auto item = session().data().message(computeFullId(universalId));
|
2017-10-04 17:15:08 +00:00
|
|
|
if (!item) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
auto getPhoto = [&]() -> PhotoData* {
|
2018-01-14 16:02:25 +00:00
|
|
|
if (const auto media = item->media()) {
|
|
|
|
return media->photo();
|
2017-10-04 17:15:08 +00:00
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
};
|
|
|
|
auto getFile = [&]() -> DocumentData* {
|
2018-01-14 16:02:25 +00:00
|
|
|
if (auto media = item->media()) {
|
|
|
|
return media->document();
|
2017-10-04 17:15:08 +00:00
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
};
|
2017-10-05 15:35:52 +00:00
|
|
|
|
2017-10-05 17:32:34 +00:00
|
|
|
auto &songSt = st::overviewFileLayout;
|
2017-10-05 15:35:52 +00:00
|
|
|
using namespace Layout;
|
2017-10-04 17:15:08 +00:00
|
|
|
switch (type) {
|
|
|
|
case Type::Photo:
|
2018-11-05 08:22:05 +00:00
|
|
|
if (const auto photo = getPhoto()) {
|
2020-05-21 12:27:04 +00:00
|
|
|
return std::make_unique<Photo>(this, item, photo);
|
2017-10-04 17:15:08 +00:00
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
case Type::Video:
|
2018-11-05 08:22:05 +00:00
|
|
|
if (const auto file = getFile()) {
|
2020-05-21 12:27:04 +00:00
|
|
|
return std::make_unique<Video>(this, item, file);
|
2017-10-04 17:15:08 +00:00
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
case Type::File:
|
2018-11-05 08:22:05 +00:00
|
|
|
if (const auto file = getFile()) {
|
2020-05-21 12:27:04 +00:00
|
|
|
return std::make_unique<Document>(this, item, file, songSt);
|
2017-10-04 17:15:08 +00:00
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
case Type::MusicFile:
|
2018-11-05 08:22:05 +00:00
|
|
|
if (const auto file = getFile()) {
|
2020-05-21 12:27:04 +00:00
|
|
|
return std::make_unique<Document>(this, item, file, songSt);
|
2017-10-04 17:15:08 +00:00
|
|
|
}
|
|
|
|
return nullptr;
|
2018-11-05 08:22:05 +00:00
|
|
|
case Type::RoundVoiceFile:
|
|
|
|
if (const auto file = getFile()) {
|
2020-05-21 12:27:04 +00:00
|
|
|
return std::make_unique<Voice>(this, item, file, songSt);
|
2017-10-04 17:15:08 +00:00
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
case Type::Link:
|
2020-05-21 12:27:04 +00:00
|
|
|
return std::make_unique<Link>(this, item, item->media());
|
2017-10-04 17:15:08 +00:00
|
|
|
case Type::RoundFile:
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
Unexpected("Type in ListWidget::createLayout()");
|
|
|
|
}
|
|
|
|
|
|
|
|
void ListWidget::refreshRows() {
|
2017-10-05 17:32:34 +00:00
|
|
|
saveScrollState();
|
|
|
|
|
2017-10-04 17:15:08 +00:00
|
|
|
markLayoutsStale();
|
|
|
|
|
|
|
|
_sections.clear();
|
|
|
|
auto section = Section(_type);
|
|
|
|
auto count = _slice.size();
|
|
|
|
for (auto i = count; i != 0;) {
|
2017-10-13 19:07:04 +00:00
|
|
|
auto universalId = GetUniversalId(_slice[--i]);
|
|
|
|
if (auto layout = getLayout(universalId)) {
|
2017-10-04 17:15:08 +00:00
|
|
|
if (!section.addItem(layout)) {
|
|
|
|
_sections.push_back(std::move(section));
|
|
|
|
section = Section(_type);
|
|
|
|
section.addItem(layout);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!section.empty()) {
|
|
|
|
_sections.push_back(std::move(section));
|
|
|
|
}
|
|
|
|
|
2017-11-26 17:05:52 +00:00
|
|
|
if (auto count = _slice.fullCount()) {
|
|
|
|
if (*count > kMediaCountForSearch) {
|
|
|
|
_controller->setSearchEnabledByContent(true);
|
|
|
|
}
|
2017-11-03 18:26:14 +00:00
|
|
|
}
|
|
|
|
|
2017-10-04 17:15:08 +00:00
|
|
|
clearStaleLayouts();
|
|
|
|
|
|
|
|
resizeToWidth(width());
|
2017-10-05 17:32:34 +00:00
|
|
|
restoreScrollState();
|
2017-10-20 16:19:42 +00:00
|
|
|
mouseActionUpdate();
|
2017-10-04 17:15:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ListWidget::markLayoutsStale() {
|
|
|
|
for (auto &layout : _layouts) {
|
|
|
|
layout.second.stale = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-19 14:27:09 +00:00
|
|
|
bool ListWidget::preventAutoHide() const {
|
|
|
|
return (_contextMenu != nullptr) || (_actionBoxWeak != nullptr);
|
|
|
|
}
|
|
|
|
|
2017-10-27 17:41:38 +00:00
|
|
|
void ListWidget::saveState(not_null<Memento*> memento) {
|
|
|
|
if (_universalAroundId != kDefaultAroundId) {
|
|
|
|
auto state = countScrollState();
|
2017-11-30 08:15:21 +00:00
|
|
|
if (state.item) {
|
|
|
|
memento->setAroundId(computeFullId(_universalAroundId));
|
|
|
|
memento->setIdsLimit(_idsLimit);
|
|
|
|
memento->setScrollTopItem(computeFullId(state.item));
|
|
|
|
memento->setScrollTopShift(state.shift);
|
|
|
|
}
|
2017-10-27 17:41:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ListWidget::restoreState(not_null<Memento*> memento) {
|
|
|
|
if (auto limit = memento->idsLimit()) {
|
|
|
|
auto wasAroundId = memento->aroundId();
|
|
|
|
if (isPossiblyMyId(wasAroundId)) {
|
|
|
|
_idsLimit = limit;
|
|
|
|
_universalAroundId = GetUniversalId(wasAroundId);
|
|
|
|
_scrollTopState.item = GetUniversalId(memento->scrollTopItem());
|
|
|
|
_scrollTopState.shift = memento->scrollTopShift();
|
|
|
|
refreshViewer();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-04 17:15:08 +00:00
|
|
|
int ListWidget::resizeGetHeight(int newWidth) {
|
2017-10-05 15:35:52 +00:00
|
|
|
if (newWidth > 0) {
|
|
|
|
for (auto §ion : _sections) {
|
|
|
|
section.resizeToWidth(newWidth);
|
|
|
|
}
|
2017-10-04 17:15:08 +00:00
|
|
|
}
|
|
|
|
return recountHeight();
|
2017-10-04 12:39:59 +00:00
|
|
|
}
|
|
|
|
|
2017-10-27 17:41:38 +00:00
|
|
|
auto ListWidget::findItemByPoint(QPoint point) const -> FoundItem {
|
2017-10-05 17:32:34 +00:00
|
|
|
Expects(!_sections.empty());
|
|
|
|
auto sectionIt = findSectionAfterTop(point.y());
|
|
|
|
if (sectionIt == _sections.end()) {
|
|
|
|
--sectionIt;
|
|
|
|
}
|
|
|
|
auto shift = QPoint(0, sectionIt->top());
|
|
|
|
return foundItemInSection(
|
|
|
|
sectionIt->findItemByPoint(point - shift),
|
|
|
|
*sectionIt);
|
|
|
|
}
|
|
|
|
|
2017-10-09 21:55:30 +00:00
|
|
|
auto ListWidget::findItemById(
|
2018-09-21 16:28:46 +00:00
|
|
|
UniversalMsgId universalId) -> std::optional<FoundItem> {
|
2017-10-09 21:55:30 +00:00
|
|
|
auto sectionIt = findSectionByItem(universalId);
|
|
|
|
if (sectionIt != _sections.end()) {
|
|
|
|
auto item = sectionIt->findItemNearId(universalId);
|
|
|
|
if (item.exact) {
|
|
|
|
return foundItemInSection(item, *sectionIt);
|
|
|
|
}
|
|
|
|
}
|
2018-09-21 16:28:46 +00:00
|
|
|
return std::nullopt;
|
2017-10-09 21:55:30 +00:00
|
|
|
}
|
|
|
|
|
2020-05-21 12:27:04 +00:00
|
|
|
auto ListWidget::findItemDetails(not_null<BaseLayout*> item)
|
|
|
|
-> FoundItem {
|
|
|
|
const auto sectionIt = findSectionByItem(GetUniversalId(item));
|
|
|
|
Assert(sectionIt != _sections.end());
|
|
|
|
return foundItemInSection(sectionIt->findItemDetails(item), *sectionIt);
|
2017-10-09 21:55:30 +00:00
|
|
|
}
|
|
|
|
|
2017-10-05 17:32:34 +00:00
|
|
|
auto ListWidget::foundItemInSection(
|
2020-05-21 12:27:04 +00:00
|
|
|
const FoundItem &item,
|
|
|
|
const Section §ion) const
|
|
|
|
-> FoundItem {
|
2017-10-05 17:32:34 +00:00
|
|
|
return {
|
|
|
|
item.layout,
|
|
|
|
item.geometry.translated(0, section.top()),
|
|
|
|
item.exact };
|
|
|
|
}
|
|
|
|
|
2017-10-05 15:35:52 +00:00
|
|
|
void ListWidget::visibleTopBottomUpdated(
|
|
|
|
int visibleTop,
|
|
|
|
int visibleBottom) {
|
2017-10-05 17:32:34 +00:00
|
|
|
_visibleTop = visibleTop;
|
2017-10-09 21:55:30 +00:00
|
|
|
_visibleBottom = visibleBottom;
|
2017-10-05 17:32:34 +00:00
|
|
|
|
2017-10-13 19:07:04 +00:00
|
|
|
checkMoveToOtherViewer();
|
2020-05-21 12:27:04 +00:00
|
|
|
clearHeavyItems();
|
2017-10-13 19:07:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ListWidget::checkMoveToOtherViewer() {
|
|
|
|
auto visibleHeight = (_visibleBottom - _visibleTop);
|
|
|
|
if (width() <= 0
|
|
|
|
|| visibleHeight <= 0
|
|
|
|
|| _sections.empty()
|
2017-10-27 17:41:38 +00:00
|
|
|
|| _scrollTopState.item) {
|
2017-10-13 19:07:04 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto topItem = findItemByPoint({ 0, _visibleTop });
|
|
|
|
auto bottomItem = findItemByPoint({ 0, _visibleBottom });
|
2017-10-05 17:32:34 +00:00
|
|
|
|
|
|
|
auto preloadedHeight = kPreloadedScreensCountFull * visibleHeight;
|
|
|
|
auto minItemHeight = Section::MinItemHeight(_type, width());
|
|
|
|
auto preloadedCount = preloadedHeight / minItemHeight;
|
|
|
|
auto preloadIdsLimitMin = (preloadedCount / 2) + 1;
|
|
|
|
auto preloadIdsLimit = preloadIdsLimitMin
|
|
|
|
+ (visibleHeight / minItemHeight);
|
|
|
|
|
|
|
|
auto preloadBefore = kPreloadIfLessThanScreens * visibleHeight;
|
|
|
|
auto after = _slice.skippedAfter();
|
2017-10-13 19:07:04 +00:00
|
|
|
auto preloadTop = (_visibleTop < preloadBefore);
|
2017-10-05 17:32:34 +00:00
|
|
|
auto topLoaded = after && (*after == 0);
|
|
|
|
auto before = _slice.skippedBefore();
|
2017-10-13 19:07:04 +00:00
|
|
|
auto preloadBottom = (height() - _visibleBottom < preloadBefore);
|
2017-10-05 17:32:34 +00:00
|
|
|
auto bottomLoaded = before && (*before == 0);
|
|
|
|
|
|
|
|
auto minScreenDelta = kPreloadedScreensCount
|
|
|
|
- kPreloadIfLessThanScreens;
|
|
|
|
auto minUniversalIdDelta = (minScreenDelta * visibleHeight)
|
|
|
|
/ minItemHeight;
|
|
|
|
auto preloadAroundItem = [&](const FoundItem &item) {
|
|
|
|
auto preloadRequired = false;
|
|
|
|
auto universalId = GetUniversalId(item.layout);
|
|
|
|
if (!preloadRequired) {
|
|
|
|
preloadRequired = (_idsLimit < preloadIdsLimitMin);
|
|
|
|
}
|
|
|
|
if (!preloadRequired) {
|
|
|
|
auto delta = _slice.distance(
|
|
|
|
sliceKey(_universalAroundId),
|
|
|
|
sliceKey(universalId));
|
2018-09-21 16:28:46 +00:00
|
|
|
Assert(delta != std::nullopt);
|
2017-10-05 17:32:34 +00:00
|
|
|
preloadRequired = (qAbs(*delta) >= minUniversalIdDelta);
|
|
|
|
}
|
|
|
|
if (preloadRequired) {
|
|
|
|
_idsLimit = preloadIdsLimit;
|
|
|
|
_universalAroundId = universalId;
|
|
|
|
refreshViewer();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if (preloadTop && !topLoaded) {
|
|
|
|
preloadAroundItem(topItem);
|
|
|
|
} else if (preloadBottom && !bottomLoaded) {
|
|
|
|
preloadAroundItem(bottomItem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-21 12:27:04 +00:00
|
|
|
void ListWidget::clearHeavyItems() {
|
|
|
|
const auto visibleHeight = _visibleBottom - _visibleTop;
|
|
|
|
if (!visibleHeight) {
|
|
|
|
return;
|
|
|
|
}
|
2020-05-21 13:13:29 +00:00
|
|
|
_heavyLayoutsInvalidated = false;
|
2020-05-21 12:27:04 +00:00
|
|
|
const auto above = _visibleTop - visibleHeight;
|
|
|
|
const auto below = _visibleBottom + visibleHeight;
|
|
|
|
for (auto i = _heavyLayouts.begin(); i != _heavyLayouts.end();) {
|
2020-05-21 13:13:29 +00:00
|
|
|
const auto item = const_cast<BaseLayout*>(i->get());
|
2020-05-21 12:27:04 +00:00
|
|
|
const auto rect = findItemDetails(item).geometry;
|
|
|
|
if (rect.top() + rect.height() <= above || rect.top() >= below) {
|
|
|
|
i = _heavyLayouts.erase(i);
|
|
|
|
item->clearHeavyPart();
|
2020-05-21 13:13:29 +00:00
|
|
|
if (_heavyLayoutsInvalidated) {
|
|
|
|
break;
|
|
|
|
}
|
2020-05-21 12:27:04 +00:00
|
|
|
} else {
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
}
|
2020-05-21 13:13:29 +00:00
|
|
|
if (_heavyLayoutsInvalidated) {
|
|
|
|
clearHeavyItems();
|
|
|
|
}
|
2020-05-21 12:27:04 +00:00
|
|
|
}
|
|
|
|
|
2017-10-27 17:41:38 +00:00
|
|
|
auto ListWidget::countScrollState() const -> ScrollTopState {
|
2017-10-05 17:32:34 +00:00
|
|
|
if (_sections.empty()) {
|
2017-10-27 17:41:38 +00:00
|
|
|
return { 0, 0 };
|
2017-10-05 15:35:52 +00:00
|
|
|
}
|
2017-10-05 17:32:34 +00:00
|
|
|
auto topItem = findItemByPoint({ 0, _visibleTop });
|
2017-10-27 17:41:38 +00:00
|
|
|
return {
|
|
|
|
GetUniversalId(topItem.layout),
|
|
|
|
_visibleTop - topItem.geometry.y()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
void ListWidget::saveScrollState() {
|
|
|
|
if (!_scrollTopState.item) {
|
|
|
|
_scrollTopState = countScrollState();
|
|
|
|
}
|
2017-10-05 17:32:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ListWidget::restoreScrollState() {
|
2017-10-27 17:41:38 +00:00
|
|
|
if (_sections.empty() || !_scrollTopState.item) {
|
2017-10-05 17:32:34 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-10-27 17:41:38 +00:00
|
|
|
auto sectionIt = findSectionByItem(_scrollTopState.item);
|
2017-10-05 17:32:34 +00:00
|
|
|
if (sectionIt == _sections.end()) {
|
|
|
|
--sectionIt;
|
|
|
|
}
|
|
|
|
auto item = foundItemInSection(
|
2017-10-27 17:41:38 +00:00
|
|
|
sectionIt->findItemNearId(_scrollTopState.item),
|
2017-10-05 17:32:34 +00:00
|
|
|
*sectionIt);
|
2017-10-27 17:41:38 +00:00
|
|
|
auto newVisibleTop = item.geometry.y() + _scrollTopState.shift;
|
2017-10-05 17:32:34 +00:00
|
|
|
if (_visibleTop != newVisibleTop) {
|
|
|
|
_scrollToRequests.fire_copy(newVisibleTop);
|
|
|
|
}
|
2017-10-27 17:41:38 +00:00
|
|
|
_scrollTopState = ScrollTopState();
|
2017-10-05 15:35:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QMargins ListWidget::padding() const {
|
|
|
|
return st::infoMediaMargin;
|
|
|
|
}
|
|
|
|
|
2017-10-04 17:15:08 +00:00
|
|
|
void ListWidget::paintEvent(QPaintEvent *e) {
|
|
|
|
Painter p(this);
|
|
|
|
|
|
|
|
auto outerWidth = width();
|
|
|
|
auto clip = e->rect();
|
2019-02-19 06:57:53 +00:00
|
|
|
auto ms = crl::now();
|
2017-10-05 15:35:52 +00:00
|
|
|
auto fromSectionIt = findSectionAfterTop(clip.y());
|
|
|
|
auto tillSectionIt = findSectionAfterBottom(
|
|
|
|
fromSectionIt,
|
|
|
|
clip.y() + clip.height());
|
2017-10-13 19:07:04 +00:00
|
|
|
auto context = Context {
|
2017-10-20 16:19:42 +00:00
|
|
|
Layout::PaintContext(ms, hasSelectedItems()),
|
2017-10-13 19:07:04 +00:00
|
|
|
&_selected,
|
|
|
|
&_dragSelected,
|
|
|
|
_dragSelectAction
|
|
|
|
};
|
2017-10-05 15:35:52 +00:00
|
|
|
for (auto it = fromSectionIt; it != tillSectionIt; ++it) {
|
|
|
|
auto top = it->top();
|
|
|
|
p.translate(0, top);
|
2017-10-13 19:07:04 +00:00
|
|
|
it->paint(p, context, clip.translated(0, -top), outerWidth);
|
2017-10-05 15:35:52 +00:00
|
|
|
p.translate(0, -top);
|
2017-10-04 17:15:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-09 21:55:30 +00:00
|
|
|
void ListWidget::mousePressEvent(QMouseEvent *e) {
|
|
|
|
if (_contextMenu) {
|
|
|
|
e->accept();
|
|
|
|
return; // ignore mouse press, that was hiding context menu
|
|
|
|
}
|
|
|
|
mouseActionStart(e->globalPos(), e->button());
|
|
|
|
}
|
|
|
|
|
|
|
|
void ListWidget::mouseMoveEvent(QMouseEvent *e) {
|
|
|
|
auto buttonsPressed = (e->buttons() & (Qt::LeftButton | Qt::MiddleButton));
|
|
|
|
if (!buttonsPressed && _mouseAction != MouseAction::None) {
|
|
|
|
mouseReleaseEvent(e);
|
|
|
|
}
|
|
|
|
mouseActionUpdate(e->globalPos());
|
|
|
|
}
|
|
|
|
|
|
|
|
void ListWidget::mouseReleaseEvent(QMouseEvent *e) {
|
|
|
|
mouseActionFinish(e->globalPos(), e->button());
|
|
|
|
if (!rect().contains(e->pos())) {
|
|
|
|
leaveEvent(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ListWidget::mouseDoubleClickEvent(QMouseEvent *e) {
|
|
|
|
mouseActionStart(e->globalPos(), e->button());
|
2017-10-13 19:07:04 +00:00
|
|
|
trySwitchToWordSelection();
|
|
|
|
}
|
2017-10-09 21:55:30 +00:00
|
|
|
|
2017-10-25 16:32:22 +00:00
|
|
|
void ListWidget::showContextMenu(
|
|
|
|
QContextMenuEvent *e,
|
|
|
|
ContextMenuSource source) {
|
|
|
|
if (_contextMenu) {
|
|
|
|
_contextMenu = nullptr;
|
|
|
|
repaintItem(_contextUniversalId);
|
|
|
|
}
|
|
|
|
if (e->reason() == QContextMenuEvent::Mouse) {
|
|
|
|
mouseActionUpdate(e->globalPos());
|
|
|
|
}
|
|
|
|
|
2019-07-24 11:13:51 +00:00
|
|
|
auto item = session().data().message(computeFullId(_overState.itemId));
|
2017-10-25 16:32:22 +00:00
|
|
|
if (!item || !_overState.inside) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto universalId = _contextUniversalId = _overState.itemId;
|
|
|
|
|
|
|
|
enum class SelectionState {
|
|
|
|
NoSelectedItems,
|
|
|
|
NotOverSelectedItems,
|
|
|
|
OverSelectedItems,
|
|
|
|
NotOverSelectedText,
|
|
|
|
OverSelectedText,
|
|
|
|
};
|
|
|
|
auto overSelected = SelectionState::NoSelectedItems;
|
|
|
|
if (source == ContextMenuSource::Touch) {
|
|
|
|
if (hasSelectedItems()) {
|
|
|
|
overSelected = SelectionState::OverSelectedItems;
|
|
|
|
} else if (hasSelectedText()) {
|
|
|
|
overSelected = SelectionState::OverSelectedItems;
|
|
|
|
}
|
|
|
|
} else if (hasSelectedText()) {
|
|
|
|
// #TODO text selection
|
|
|
|
} else if (hasSelectedItems()) {
|
|
|
|
auto it = _selected.find(_overState.itemId);
|
|
|
|
if (isSelectedItem(it) && _overState.inside) {
|
|
|
|
overSelected = SelectionState::OverSelectedItems;
|
|
|
|
} else {
|
|
|
|
overSelected = SelectionState::NotOverSelectedItems;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auto canDeleteAll = [&] {
|
2020-05-18 19:33:14 +00:00
|
|
|
return ranges::none_of(_selected, [](auto &&item) {
|
2017-10-25 16:32:22 +00:00
|
|
|
return !item.second.canDelete;
|
2020-05-18 19:33:14 +00:00
|
|
|
});
|
2017-10-25 16:32:22 +00:00
|
|
|
};
|
|
|
|
auto canForwardAll = [&] {
|
2020-05-18 19:33:14 +00:00
|
|
|
return ranges::none_of(_selected, [](auto &&item) {
|
2017-10-25 16:32:22 +00:00
|
|
|
return !item.second.canForward;
|
2020-05-18 19:33:14 +00:00
|
|
|
});
|
2017-10-25 16:32:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
auto link = ClickHandler::getActive();
|
|
|
|
|
2018-07-13 21:25:47 +00:00
|
|
|
const auto itemFullId = item->fullId();
|
2019-07-24 11:13:51 +00:00
|
|
|
const auto owner = &session().data();
|
2018-07-19 14:27:09 +00:00
|
|
|
_contextMenu = base::make_unique_q<Ui::PopupMenu>(this);
|
2017-10-25 16:32:22 +00:00
|
|
|
_contextMenu->addAction(
|
2019-06-19 15:09:03 +00:00
|
|
|
tr::lng_context_to_msg(tr::now),
|
2019-07-24 11:13:51 +00:00
|
|
|
[=] {
|
|
|
|
if (const auto item = owner->message(itemFullId)) {
|
2017-10-25 16:32:22 +00:00
|
|
|
Ui::showPeerHistoryAtItem(item);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-12-18 09:07:18 +00:00
|
|
|
auto photoLink = dynamic_cast<PhotoClickHandler*>(link.get());
|
|
|
|
auto fileLink = dynamic_cast<DocumentClickHandler*>(link.get());
|
2017-10-25 16:32:22 +00:00
|
|
|
if (photoLink || fileLink) {
|
2017-12-10 10:26:58 +00:00
|
|
|
auto [isVideo, isVoice, isAudio] = [&] {
|
2017-10-25 16:32:22 +00:00
|
|
|
if (fileLink) {
|
|
|
|
auto document = fileLink->document();
|
|
|
|
return std::make_tuple(
|
2017-12-10 10:26:58 +00:00
|
|
|
document->isVideoFile(),
|
|
|
|
document->isVoiceMessage(),
|
|
|
|
document->isAudioFile()
|
2017-10-25 16:32:22 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
return std::make_tuple(false, false, false);
|
|
|
|
}();
|
|
|
|
|
|
|
|
if (photoLink) {
|
|
|
|
} else {
|
|
|
|
if (auto document = fileLink->document()) {
|
|
|
|
if (document->loading()) {
|
|
|
|
_contextMenu->addAction(
|
2019-06-19 15:09:03 +00:00
|
|
|
tr::lng_context_cancel_download(tr::now),
|
2017-10-25 16:32:22 +00:00
|
|
|
[document] {
|
|
|
|
document->cancel();
|
|
|
|
});
|
|
|
|
} else {
|
2020-04-09 14:02:09 +00:00
|
|
|
auto filepath = document->filepath(true);
|
2017-10-25 16:32:22 +00:00
|
|
|
if (!filepath.isEmpty()) {
|
|
|
|
auto handler = App::LambdaDelayed(
|
|
|
|
st::defaultDropdownMenu.menu.ripple.hideDuration,
|
|
|
|
this,
|
|
|
|
[filepath] {
|
|
|
|
File::ShowInFolder(filepath);
|
|
|
|
});
|
|
|
|
_contextMenu->addAction(
|
2019-06-19 15:09:03 +00:00
|
|
|
(Platform::IsMac()
|
|
|
|
? tr::lng_context_show_in_finder(tr::now)
|
|
|
|
: tr::lng_context_show_in_folder(tr::now)),
|
2017-10-25 16:32:22 +00:00
|
|
|
std::move(handler));
|
|
|
|
}
|
|
|
|
auto handler = App::LambdaDelayed(
|
|
|
|
st::defaultDropdownMenu.menu.ripple.hideDuration,
|
|
|
|
this,
|
2018-07-13 21:25:47 +00:00
|
|
|
[=] {
|
|
|
|
DocumentSaveClickHandler::Save(
|
|
|
|
itemFullId,
|
|
|
|
document,
|
2019-03-11 14:35:11 +00:00
|
|
|
DocumentSaveClickHandler::Mode::ToNewFile);
|
2017-10-25 16:32:22 +00:00
|
|
|
});
|
|
|
|
_contextMenu->addAction(
|
2019-06-19 15:09:03 +00:00
|
|
|
(isVideo
|
|
|
|
? tr::lng_context_save_video(tr::now)
|
2017-10-25 16:32:22 +00:00
|
|
|
: isVoice
|
2019-06-19 15:09:03 +00:00
|
|
|
? tr::lng_context_save_audio(tr::now)
|
2017-12-10 10:26:58 +00:00
|
|
|
: isAudio
|
2019-06-19 15:09:03 +00:00
|
|
|
? tr::lng_context_save_audio_file(tr::now)
|
|
|
|
: tr::lng_context_save_file(tr::now)),
|
2017-10-25 16:32:22 +00:00
|
|
|
std::move(handler));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (link) {
|
2018-01-25 10:10:52 +00:00
|
|
|
const auto actionText = link->copyToClipboardContextItemText();
|
|
|
|
if (!actionText.isEmpty()) {
|
2017-10-25 16:32:22 +00:00
|
|
|
_contextMenu->addAction(
|
2018-01-25 10:10:52 +00:00
|
|
|
actionText,
|
|
|
|
[text = link->copyToClipboardText()] {
|
2019-09-04 07:19:15 +00:00
|
|
|
QGuiApplication::clipboard()->setText(text);
|
2017-10-25 16:32:22 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (overSelected == SelectionState::OverSelectedItems) {
|
|
|
|
if (canForwardAll()) {
|
|
|
|
_contextMenu->addAction(
|
2019-06-19 15:09:03 +00:00
|
|
|
tr::lng_context_forward_selected(tr::now),
|
2018-06-04 15:35:11 +00:00
|
|
|
crl::guard(this, [this] {
|
2017-10-25 16:32:22 +00:00
|
|
|
forwardSelected();
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
if (canDeleteAll()) {
|
|
|
|
_contextMenu->addAction(
|
2019-06-19 15:09:03 +00:00
|
|
|
tr::lng_context_delete_selected(tr::now),
|
2018-06-04 15:35:11 +00:00
|
|
|
crl::guard(this, [this] {
|
2017-10-25 16:32:22 +00:00
|
|
|
deleteSelected();
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
_contextMenu->addAction(
|
2019-06-19 15:09:03 +00:00
|
|
|
tr::lng_context_clear_selection(tr::now),
|
2018-06-04 15:35:11 +00:00
|
|
|
crl::guard(this, [this] {
|
2017-10-25 16:32:22 +00:00
|
|
|
clearSelected();
|
|
|
|
}));
|
|
|
|
} else {
|
|
|
|
if (overSelected != SelectionState::NotOverSelectedItems) {
|
2018-01-14 16:02:25 +00:00
|
|
|
if (item->allowsForward()) {
|
2017-10-25 16:32:22 +00:00
|
|
|
_contextMenu->addAction(
|
2019-06-19 15:09:03 +00:00
|
|
|
tr::lng_context_forward_msg(tr::now),
|
2018-06-04 15:35:11 +00:00
|
|
|
crl::guard(this, [this, universalId] {
|
2017-10-25 16:32:22 +00:00
|
|
|
forwardItem(universalId);
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
if (item->canDelete()) {
|
|
|
|
_contextMenu->addAction(
|
2019-06-19 15:09:03 +00:00
|
|
|
tr::lng_context_delete_msg(tr::now),
|
2018-06-04 15:35:11 +00:00
|
|
|
crl::guard(this, [this, universalId] {
|
2017-10-25 16:32:22 +00:00
|
|
|
deleteItem(universalId);
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_contextMenu->addAction(
|
2019-06-19 15:09:03 +00:00
|
|
|
tr::lng_context_select_msg(tr::now),
|
2018-06-04 15:35:11 +00:00
|
|
|
crl::guard(this, [this, universalId] {
|
2017-10-25 16:32:22 +00:00
|
|
|
if (hasSelectedText()) {
|
|
|
|
clearSelected();
|
|
|
|
} else if (_selected.size() == MaxSelectedItems) {
|
|
|
|
return;
|
|
|
|
} else if (_selected.empty()) {
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
applyItemSelection(universalId, FullSelection);
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2018-06-04 15:35:11 +00:00
|
|
|
_contextMenu->setDestroyedCallback(crl::guard(
|
2017-10-25 16:32:22 +00:00
|
|
|
this,
|
|
|
|
[this, universalId] {
|
|
|
|
mouseActionUpdate(QCursor::pos());
|
|
|
|
repaintItem(universalId);
|
2017-12-09 15:13:06 +00:00
|
|
|
_checkForHide.fire({});
|
2017-10-25 16:32:22 +00:00
|
|
|
}));
|
|
|
|
_contextMenu->popup(e->globalPos());
|
|
|
|
e->accept();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ListWidget::contextMenuEvent(QContextMenuEvent *e) {
|
|
|
|
showContextMenu(
|
|
|
|
e,
|
|
|
|
(e->reason() == QContextMenuEvent::Mouse)
|
|
|
|
? ContextMenuSource::Mouse
|
|
|
|
: ContextMenuSource::Other);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ListWidget::forwardSelected() {
|
2017-12-07 13:15:27 +00:00
|
|
|
if (auto items = collectSelectedIds(); !items.empty()) {
|
|
|
|
forwardItems(std::move(items));
|
|
|
|
}
|
2017-10-25 16:32:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ListWidget::forwardItem(UniversalMsgId universalId) {
|
2019-07-24 11:13:51 +00:00
|
|
|
if (const auto item = session().data().message(computeFullId(universalId))) {
|
2017-12-06 10:13:38 +00:00
|
|
|
forwardItems({ 1, item->fullId() });
|
2017-10-25 16:32:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-06 10:13:38 +00:00
|
|
|
void ListWidget::forwardItems(MessageIdsList &&items) {
|
2019-09-13 12:22:54 +00:00
|
|
|
auto callback = [weak = Ui::MakeWeak(this)] {
|
2017-12-07 13:15:27 +00:00
|
|
|
if (const auto strong = weak.data()) {
|
|
|
|
strong->clearSelected();
|
2017-12-06 10:13:38 +00:00
|
|
|
}
|
2017-12-09 15:13:06 +00:00
|
|
|
};
|
|
|
|
setActionBoxWeak(Window::ShowForwardMessagesBox(
|
2019-07-25 18:55:11 +00:00
|
|
|
_controller,
|
2017-12-09 15:13:06 +00:00
|
|
|
std::move(items),
|
|
|
|
std::move(callback)));
|
2017-10-25 16:32:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ListWidget::deleteSelected() {
|
2018-01-26 15:40:11 +00:00
|
|
|
if (const auto box = deleteItems(collectSelectedIds())) {
|
2019-09-13 12:22:54 +00:00
|
|
|
const auto weak = Ui::MakeWeak(this);
|
2018-01-26 15:40:11 +00:00
|
|
|
box->setDeleteConfirmedCallback([=]{
|
|
|
|
if (const auto strong = weak.data()) {
|
|
|
|
strong->clearSelected();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2017-10-25 16:32:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ListWidget::deleteItem(UniversalMsgId universalId) {
|
2019-07-24 11:13:51 +00:00
|
|
|
if (const auto item = session().data().message(computeFullId(universalId))) {
|
2017-12-06 10:13:38 +00:00
|
|
|
deleteItems({ 1, item->fullId() });
|
2017-10-25 16:32:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-26 15:40:11 +00:00
|
|
|
DeleteMessagesBox *ListWidget::deleteItems(MessageIdsList &&items) {
|
2017-10-25 16:32:22 +00:00
|
|
|
if (!items.empty()) {
|
2018-01-26 15:40:11 +00:00
|
|
|
const auto box = Ui::show(
|
2019-07-24 14:00:30 +00:00
|
|
|
Box<DeleteMessagesBox>(
|
|
|
|
&_controller->session(),
|
|
|
|
std::move(items))).data();
|
2018-01-26 15:40:11 +00:00
|
|
|
setActionBoxWeak(box);
|
|
|
|
return box;
|
2017-12-09 15:13:06 +00:00
|
|
|
}
|
2018-01-26 15:40:11 +00:00
|
|
|
return nullptr;
|
2017-12-09 15:13:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ListWidget::setActionBoxWeak(QPointer<Ui::RpWidget> box) {
|
|
|
|
if ((_actionBoxWeak = box)) {
|
|
|
|
_actionBoxWeakLifetime = _actionBoxWeak->alive(
|
2019-09-13 12:22:54 +00:00
|
|
|
) | rpl::start_with_done([weak = Ui::MakeWeak(this)]{
|
2017-12-09 15:13:06 +00:00
|
|
|
if (weak) {
|
|
|
|
weak->_checkForHide.fire({});
|
|
|
|
}
|
|
|
|
});
|
2017-10-25 16:32:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-13 19:07:04 +00:00
|
|
|
void ListWidget::trySwitchToWordSelection() {
|
|
|
|
auto selectingSome = (_mouseAction == MouseAction::Selecting)
|
|
|
|
&& hasSelectedText();
|
|
|
|
auto willSelectSome = (_mouseAction == MouseAction::None)
|
|
|
|
&& !hasSelectedItems();
|
|
|
|
auto checkSwitchToWordSelection = _overLayout
|
|
|
|
&& (_mouseSelectType == TextSelectType::Letters)
|
|
|
|
&& (selectingSome || willSelectSome);
|
|
|
|
if (checkSwitchToWordSelection) {
|
|
|
|
switchToWordSelection();
|
|
|
|
}
|
|
|
|
}
|
2017-10-09 21:55:30 +00:00
|
|
|
|
2017-10-13 19:07:04 +00:00
|
|
|
void ListWidget::switchToWordSelection() {
|
|
|
|
Expects(_overLayout != nullptr);
|
|
|
|
|
2018-01-27 13:59:24 +00:00
|
|
|
StateRequest request;
|
2019-06-12 13:26:04 +00:00
|
|
|
request.flags |= Ui::Text::StateRequest::Flag::LookupSymbol;
|
2017-10-13 19:07:04 +00:00
|
|
|
auto dragState = _overLayout->getState(_pressState.cursor, request);
|
2018-01-27 13:59:24 +00:00
|
|
|
if (dragState.cursor != CursorState::Text) {
|
2017-10-13 19:07:04 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
_mouseTextSymbol = dragState.symbol;
|
|
|
|
_mouseSelectType = TextSelectType::Words;
|
|
|
|
if (_mouseAction == MouseAction::None) {
|
|
|
|
_mouseAction = MouseAction::Selecting;
|
|
|
|
clearSelected();
|
|
|
|
auto selStatus = TextSelection {
|
|
|
|
dragState.symbol,
|
|
|
|
dragState.symbol
|
|
|
|
};
|
|
|
|
applyItemSelection(_overState.itemId, selStatus);
|
|
|
|
}
|
|
|
|
mouseActionUpdate();
|
|
|
|
|
|
|
|
_trippleClickPoint = _mousePosition;
|
2019-02-19 06:57:53 +00:00
|
|
|
_trippleClickStartTime = crl::now();
|
2017-10-13 19:07:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ListWidget::applyItemSelection(
|
|
|
|
UniversalMsgId universalId,
|
|
|
|
TextSelection selection) {
|
|
|
|
if (changeItemSelection(
|
|
|
|
_selected,
|
|
|
|
universalId,
|
|
|
|
selection)) {
|
|
|
|
repaintItem(universalId);
|
2017-10-20 16:19:42 +00:00
|
|
|
pushSelectedItems();
|
2017-10-13 19:07:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ListWidget::toggleItemSelection(UniversalMsgId universalId) {
|
|
|
|
auto it = _selected.find(universalId);
|
|
|
|
if (it == _selected.cend()) {
|
|
|
|
applyItemSelection(universalId, FullSelection);
|
|
|
|
} else {
|
|
|
|
removeItemSelection(it);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ListWidget::changeItemSelection(
|
|
|
|
SelectedMap &selected,
|
|
|
|
UniversalMsgId universalId,
|
|
|
|
TextSelection selection) const {
|
|
|
|
auto changeExisting = [&](auto it) {
|
|
|
|
if (it == selected.cend()) {
|
|
|
|
return false;
|
|
|
|
} else if (it->second.text != selection) {
|
|
|
|
it->second.text = selection;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
if (selected.size() < MaxSelectedItems) {
|
|
|
|
auto [iterator, ok] = selected.try_emplace(
|
|
|
|
universalId,
|
|
|
|
selection);
|
|
|
|
if (ok) {
|
2019-07-24 11:13:51 +00:00
|
|
|
auto item = session().data().message(computeFullId(universalId));
|
2017-10-13 19:07:04 +00:00
|
|
|
if (!item) {
|
|
|
|
selected.erase(iterator);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
iterator->second.canDelete = item->canDelete();
|
2018-01-14 16:02:25 +00:00
|
|
|
iterator->second.canForward = item->allowsForward();
|
2017-10-13 19:07:04 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return changeExisting(iterator);
|
|
|
|
}
|
|
|
|
return changeExisting(selected.find(universalId));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ListWidget::isItemUnderPressSelected() const {
|
|
|
|
return itemUnderPressSelection() != _selected.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
auto ListWidget::itemUnderPressSelection() -> SelectedMap::iterator {
|
|
|
|
return (_pressState.itemId && _pressState.inside)
|
|
|
|
? _selected.find(_pressState.itemId)
|
|
|
|
: _selected.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
auto ListWidget::itemUnderPressSelection() const
|
|
|
|
-> SelectedMap::const_iterator {
|
|
|
|
return (_pressState.itemId && _pressState.inside)
|
|
|
|
? _selected.find(_pressState.itemId)
|
|
|
|
: _selected.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ListWidget::requiredToStartDragging(
|
|
|
|
not_null<BaseLayout*> layout) const {
|
2018-01-27 13:59:24 +00:00
|
|
|
if (_mouseCursorState == CursorState::Date) {
|
2017-10-13 19:07:04 +00:00
|
|
|
return true;
|
|
|
|
}
|
2019-08-02 13:21:09 +00:00
|
|
|
// return dynamic_cast<Sticker*>(layout->getMedia());
|
2017-10-13 19:07:04 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-01-27 13:59:24 +00:00
|
|
|
bool ListWidget::isPressInSelectedText(TextState state) const {
|
|
|
|
if (state.cursor != CursorState::Text) {
|
2017-10-13 19:07:04 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!hasSelectedText()
|
|
|
|
|| !isItemUnderPressSelected()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
auto pressedSelection = itemUnderPressSelection();
|
|
|
|
auto from = pressedSelection->second.text.from;
|
|
|
|
auto to = pressedSelection->second.text.to;
|
|
|
|
return (state.symbol >= from && state.symbol < to);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ListWidget::clearSelected() {
|
|
|
|
if (_selected.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (hasSelectedText()) {
|
|
|
|
repaintItem(_selected.begin()->first);
|
2017-10-20 16:19:42 +00:00
|
|
|
_selected.clear();
|
2017-10-13 19:07:04 +00:00
|
|
|
} else {
|
2017-10-20 16:19:42 +00:00
|
|
|
_selected.clear();
|
|
|
|
pushSelectedItems();
|
2017-10-13 19:07:04 +00:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ListWidget::validateTrippleClickStartTime() {
|
|
|
|
if (_trippleClickStartTime) {
|
2019-02-19 06:57:53 +00:00
|
|
|
auto elapsed = (crl::now() - _trippleClickStartTime);
|
2017-10-13 19:07:04 +00:00
|
|
|
if (elapsed >= QApplication::doubleClickInterval()) {
|
|
|
|
_trippleClickStartTime = 0;
|
|
|
|
}
|
|
|
|
}
|
2017-10-09 21:55:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ListWidget::enterEventHook(QEvent *e) {
|
|
|
|
mouseActionUpdate(QCursor::pos());
|
|
|
|
return RpWidget::enterEventHook(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ListWidget::leaveEventHook(QEvent *e) {
|
2018-01-26 15:40:11 +00:00
|
|
|
if (const auto item = _overLayout) {
|
2017-10-13 19:07:04 +00:00
|
|
|
if (_overState.inside) {
|
|
|
|
repaintItem(item);
|
|
|
|
_overState.inside = false;
|
|
|
|
}
|
2017-10-09 21:55:30 +00:00
|
|
|
}
|
|
|
|
ClickHandler::clearActive();
|
|
|
|
if (!ClickHandler::getPressed() && _cursor != style::cur_default) {
|
|
|
|
_cursor = style::cur_default;
|
|
|
|
setCursor(_cursor);
|
|
|
|
}
|
|
|
|
return RpWidget::leaveEventHook(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
QPoint ListWidget::clampMousePosition(QPoint position) const {
|
|
|
|
return {
|
|
|
|
std::clamp(position.x(), 0, qMax(0, width() - 1)),
|
|
|
|
std::clamp(position.y(), _visibleTop, _visibleBottom - 1)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-01-26 15:40:11 +00:00
|
|
|
void ListWidget::mouseActionUpdate(const QPoint &globalPosition) {
|
2017-10-20 16:19:42 +00:00
|
|
|
if (_sections.empty() || _visibleBottom <= _visibleTop) {
|
2017-10-09 21:55:30 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-26 15:40:11 +00:00
|
|
|
_mousePosition = globalPosition;
|
2017-10-09 21:55:30 +00:00
|
|
|
|
|
|
|
auto local = mapFromGlobal(_mousePosition);
|
|
|
|
auto point = clampMousePosition(local);
|
|
|
|
auto [layout, geometry, inside] = findItemByPoint(point);
|
2018-01-27 13:59:24 +00:00
|
|
|
auto state = MouseState{
|
2017-10-13 19:07:04 +00:00
|
|
|
GetUniversalId(layout),
|
|
|
|
geometry.size(),
|
|
|
|
point - geometry.topLeft(),
|
|
|
|
inside
|
|
|
|
};
|
2017-10-09 21:55:30 +00:00
|
|
|
auto item = layout ? layout->getItem() : nullptr;
|
2017-10-13 19:07:04 +00:00
|
|
|
if (_overLayout != layout) {
|
|
|
|
repaintItem(_overLayout);
|
|
|
|
_overLayout = layout;
|
|
|
|
repaintItem(geometry);
|
2017-10-09 21:55:30 +00:00
|
|
|
}
|
2017-10-13 19:07:04 +00:00
|
|
|
_overState = state;
|
2017-10-09 21:55:30 +00:00
|
|
|
|
2018-01-27 13:59:24 +00:00
|
|
|
TextState dragState;
|
2017-10-09 21:55:30 +00:00
|
|
|
ClickHandlerHost *lnkhost = nullptr;
|
2017-10-13 19:07:04 +00:00
|
|
|
auto inTextSelection = _overState.inside
|
|
|
|
&& (_overState.itemId == _pressState.itemId)
|
|
|
|
&& hasSelectedText();
|
|
|
|
if (_overLayout) {
|
2018-01-26 15:40:11 +00:00
|
|
|
auto cursorDeltaLength = [&] {
|
|
|
|
auto cursorDelta = (_overState.cursor - _pressState.cursor);
|
|
|
|
return cursorDelta.manhattanLength();
|
|
|
|
};
|
|
|
|
auto dragStartLength = [] {
|
|
|
|
return QApplication::startDragDistance();
|
|
|
|
};
|
2017-10-13 19:07:04 +00:00
|
|
|
if (_overState.itemId != _pressState.itemId
|
|
|
|
|| cursorDeltaLength() >= dragStartLength()) {
|
2017-10-09 21:55:30 +00:00
|
|
|
if (_mouseAction == MouseAction::PrepareDrag) {
|
|
|
|
_mouseAction = MouseAction::Dragging;
|
|
|
|
InvokeQueued(this, [this] { performDrag(); });
|
|
|
|
} else if (_mouseAction == MouseAction::PrepareSelect) {
|
|
|
|
_mouseAction = MouseAction::Selecting;
|
|
|
|
}
|
|
|
|
}
|
2018-01-27 13:59:24 +00:00
|
|
|
StateRequest request;
|
2017-10-09 21:55:30 +00:00
|
|
|
if (_mouseAction == MouseAction::Selecting) {
|
2019-06-12 13:26:04 +00:00
|
|
|
request.flags |= Ui::Text::StateRequest::Flag::LookupSymbol;
|
2017-10-09 21:55:30 +00:00
|
|
|
} else {
|
2017-10-13 19:07:04 +00:00
|
|
|
inTextSelection = false;
|
2017-10-09 21:55:30 +00:00
|
|
|
}
|
2017-10-13 19:07:04 +00:00
|
|
|
dragState = _overLayout->getState(_overState.cursor, request);
|
|
|
|
lnkhost = _overLayout;
|
2017-10-09 21:55:30 +00:00
|
|
|
}
|
2017-10-13 19:07:04 +00:00
|
|
|
ClickHandler::setActive(dragState.link, lnkhost);
|
2017-10-09 21:55:30 +00:00
|
|
|
|
|
|
|
if (_mouseAction == MouseAction::None) {
|
|
|
|
_mouseCursorState = dragState.cursor;
|
2017-10-13 19:07:04 +00:00
|
|
|
auto cursor = computeMouseCursor();
|
|
|
|
if (_cursor != cursor) {
|
|
|
|
setCursor(_cursor = cursor);
|
2017-10-09 21:55:30 +00:00
|
|
|
}
|
2017-10-13 19:07:04 +00:00
|
|
|
} else if (_mouseAction == MouseAction::Selecting) {
|
|
|
|
if (inTextSelection) {
|
|
|
|
auto second = dragState.symbol;
|
|
|
|
if (dragState.afterSymbol && _mouseSelectType == TextSelectType::Letters) {
|
|
|
|
++second;
|
2017-10-09 21:55:30 +00:00
|
|
|
}
|
2017-10-13 19:07:04 +00:00
|
|
|
auto selState = TextSelection {
|
|
|
|
qMin(second, _mouseTextSymbol),
|
|
|
|
qMax(second, _mouseTextSymbol)
|
|
|
|
};
|
|
|
|
if (_mouseSelectType != TextSelectType::Letters) {
|
|
|
|
selState = _overLayout->adjustSelection(selState, _mouseSelectType);
|
|
|
|
}
|
|
|
|
applyItemSelection(_overState.itemId, selState);
|
|
|
|
auto hasSelection = (selState == FullSelection)
|
|
|
|
|| (selState.from != selState.to);
|
|
|
|
if (!_wasSelectedText && hasSelection) {
|
|
|
|
_wasSelectedText = true;
|
|
|
|
setFocus();
|
|
|
|
}
|
|
|
|
clearDragSelection();
|
|
|
|
} else if (_pressState.itemId) {
|
|
|
|
updateDragSelection();
|
2017-10-09 21:55:30 +00:00
|
|
|
}
|
2017-10-13 19:07:04 +00:00
|
|
|
} else if (_mouseAction == MouseAction::Dragging) {
|
2017-10-09 21:55:30 +00:00
|
|
|
}
|
|
|
|
|
2017-10-13 19:07:04 +00:00
|
|
|
// #TODO scroll by drag
|
2017-10-09 21:55:30 +00:00
|
|
|
//if (_mouseAction == MouseAction::Selecting) {
|
|
|
|
// _widget->checkSelectingScroll(mousePos);
|
|
|
|
//} else {
|
2017-10-13 19:07:04 +00:00
|
|
|
// clearDragSelection();
|
2017-10-09 21:55:30 +00:00
|
|
|
// _widget->noSelectingScroll();
|
|
|
|
//}
|
2017-10-13 19:07:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
style::cursor ListWidget::computeMouseCursor() const {
|
|
|
|
if (ClickHandler::getPressed() || ClickHandler::getActive()) {
|
|
|
|
return style::cur_pointer;
|
|
|
|
} else if (!hasSelectedItems()
|
2018-01-27 13:59:24 +00:00
|
|
|
&& (_mouseCursorState == CursorState::Text)) {
|
2017-10-13 19:07:04 +00:00
|
|
|
return style::cur_text;
|
|
|
|
}
|
|
|
|
return style::cur_default;
|
|
|
|
}
|
2017-10-09 21:55:30 +00:00
|
|
|
|
2017-10-13 19:07:04 +00:00
|
|
|
void ListWidget::updateDragSelection() {
|
|
|
|
auto fromState = _pressState;
|
|
|
|
auto tillState = _overState;
|
|
|
|
auto swapStates = IsAfter(fromState, tillState);
|
|
|
|
if (swapStates) {
|
|
|
|
std::swap(fromState, tillState);
|
|
|
|
}
|
|
|
|
if (!fromState.itemId || !tillState.itemId) {
|
|
|
|
clearDragSelection();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto fromId = SkipSelectFromItem(fromState)
|
|
|
|
? (fromState.itemId - 1)
|
|
|
|
: fromState.itemId;
|
|
|
|
auto tillId = SkipSelectTillItem(tillState)
|
|
|
|
? tillState.itemId
|
|
|
|
: (tillState.itemId - 1);
|
|
|
|
for (auto i = _dragSelected.begin(); i != _dragSelected.end();) {
|
|
|
|
auto itemId = i->first;
|
|
|
|
if (itemId > fromId || itemId <= tillId) {
|
|
|
|
i = _dragSelected.erase(i);
|
|
|
|
} else {
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (auto &layoutItem : _layouts) {
|
|
|
|
auto &&universalId = layoutItem.first;
|
|
|
|
auto &&layout = layoutItem.second;
|
|
|
|
if (universalId <= fromId && universalId > tillId) {
|
|
|
|
changeItemSelection(
|
|
|
|
_dragSelected,
|
|
|
|
universalId,
|
|
|
|
FullSelection);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_dragSelectAction = [&] {
|
|
|
|
if (_dragSelected.empty()) {
|
|
|
|
return DragSelectAction::None;
|
|
|
|
}
|
|
|
|
auto &[firstDragItem, data] = swapStates
|
|
|
|
? _dragSelected.front()
|
|
|
|
: _dragSelected.back();
|
|
|
|
if (isSelectedItem(_selected.find(firstDragItem))) {
|
|
|
|
return DragSelectAction::Deselecting;
|
|
|
|
} else {
|
|
|
|
return DragSelectAction::Selecting;
|
|
|
|
}
|
|
|
|
}();
|
|
|
|
if (!_wasSelectedText
|
|
|
|
&& !_dragSelected.empty()
|
|
|
|
&& _dragSelectAction == DragSelectAction::Selecting) {
|
|
|
|
_wasSelectedText = true;
|
|
|
|
setFocus();
|
|
|
|
}
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ListWidget::clearDragSelection() {
|
|
|
|
_dragSelectAction = DragSelectAction::None;
|
|
|
|
if (!_dragSelected.empty()) {
|
|
|
|
_dragSelected.clear();
|
|
|
|
update();
|
2017-10-09 21:55:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-26 15:40:11 +00:00
|
|
|
void ListWidget::mouseActionStart(
|
|
|
|
const QPoint &globalPosition,
|
|
|
|
Qt::MouseButton button) {
|
|
|
|
mouseActionUpdate(globalPosition);
|
|
|
|
if (button != Qt::LeftButton) {
|
|
|
|
return;
|
|
|
|
}
|
2017-10-09 21:55:30 +00:00
|
|
|
|
|
|
|
ClickHandler::pressed();
|
2017-10-13 19:07:04 +00:00
|
|
|
if (_pressState != _overState) {
|
|
|
|
if (_pressState.itemId != _overState.itemId) {
|
|
|
|
repaintItem(_pressState.itemId);
|
|
|
|
}
|
|
|
|
_pressState = _overState;
|
|
|
|
repaintItem(_overLayout);
|
2017-10-09 21:55:30 +00:00
|
|
|
}
|
2017-10-13 19:07:04 +00:00
|
|
|
auto pressLayout = _overLayout;
|
2017-10-09 21:55:30 +00:00
|
|
|
|
|
|
|
_mouseAction = MouseAction::None;
|
2019-09-16 11:14:06 +00:00
|
|
|
_pressWasInactive = Ui::WasInactivePress(
|
|
|
|
_controller->parentController()->widget());
|
|
|
|
if (_pressWasInactive) {
|
|
|
|
Ui::MarkInactivePress(
|
|
|
|
_controller->parentController()->widget(),
|
|
|
|
false);
|
|
|
|
}
|
2017-10-09 21:55:30 +00:00
|
|
|
|
2017-10-20 18:52:48 +00:00
|
|
|
if (ClickHandler::getPressed() && !hasSelected()) {
|
2017-10-09 21:55:30 +00:00
|
|
|
_mouseAction = MouseAction::PrepareDrag;
|
2017-10-13 19:07:04 +00:00
|
|
|
} else if (hasSelectedItems()) {
|
2017-11-23 14:58:00 +00:00
|
|
|
if (isItemUnderPressSelected() && ClickHandler::getPressed()) {
|
|
|
|
// In shared media overview drag only by click handlers.
|
2017-10-13 19:07:04 +00:00
|
|
|
_mouseAction = MouseAction::PrepareDrag; // start items drag
|
|
|
|
} else if (!_pressWasInactive) {
|
|
|
|
_mouseAction = MouseAction::PrepareSelect; // start items select
|
2017-10-09 21:55:30 +00:00
|
|
|
}
|
|
|
|
}
|
2017-10-13 19:07:04 +00:00
|
|
|
if (_mouseAction == MouseAction::None && pressLayout) {
|
|
|
|
validateTrippleClickStartTime();
|
2018-01-27 13:59:24 +00:00
|
|
|
TextState dragState;
|
2018-01-26 15:40:11 +00:00
|
|
|
auto startDistance = (globalPosition - _trippleClickPoint).manhattanLength();
|
2017-10-13 19:07:04 +00:00
|
|
|
auto validStartPoint = startDistance < QApplication::startDragDistance();
|
|
|
|
if (_trippleClickStartTime != 0 && validStartPoint) {
|
2018-01-27 13:59:24 +00:00
|
|
|
StateRequest request;
|
2019-06-12 13:26:04 +00:00
|
|
|
request.flags = Ui::Text::StateRequest::Flag::LookupSymbol;
|
2017-10-13 19:07:04 +00:00
|
|
|
dragState = pressLayout->getState(_pressState.cursor, request);
|
2018-01-27 13:59:24 +00:00
|
|
|
if (dragState.cursor == CursorState::Text) {
|
2017-10-13 19:07:04 +00:00
|
|
|
TextSelection selStatus = { dragState.symbol, dragState.symbol };
|
|
|
|
if (selStatus != FullSelection && !hasSelectedItems()) {
|
|
|
|
clearSelected();
|
|
|
|
applyItemSelection(_pressState.itemId, selStatus);
|
|
|
|
_mouseTextSymbol = dragState.symbol;
|
|
|
|
_mouseAction = MouseAction::Selecting;
|
|
|
|
_mouseSelectType = TextSelectType::Paragraphs;
|
|
|
|
mouseActionUpdate(_mousePosition);
|
2019-02-19 06:57:53 +00:00
|
|
|
_trippleClickStartTime = crl::now();
|
2017-10-13 19:07:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2018-01-27 13:59:24 +00:00
|
|
|
StateRequest request;
|
2019-06-12 13:26:04 +00:00
|
|
|
request.flags = Ui::Text::StateRequest::Flag::LookupSymbol;
|
2017-10-13 19:07:04 +00:00
|
|
|
dragState = pressLayout->getState(_pressState.cursor, request);
|
2017-10-09 21:55:30 +00:00
|
|
|
}
|
|
|
|
if (_mouseSelectType != TextSelectType::Paragraphs) {
|
2017-10-13 19:07:04 +00:00
|
|
|
if (_pressState.inside) {
|
|
|
|
_mouseTextSymbol = dragState.symbol;
|
|
|
|
if (isPressInSelectedText(dragState)) {
|
|
|
|
_mouseAction = MouseAction::PrepareDrag; // start text drag
|
|
|
|
} else if (!_pressWasInactive) {
|
|
|
|
if (requiredToStartDragging(pressLayout)) {
|
|
|
|
_mouseAction = MouseAction::PrepareDrag;
|
|
|
|
} else {
|
|
|
|
if (dragState.afterSymbol) ++_mouseTextSymbol;
|
|
|
|
TextSelection selStatus = { _mouseTextSymbol, _mouseTextSymbol };
|
|
|
|
if (selStatus != FullSelection && !hasSelectedItems()) {
|
|
|
|
clearSelected();
|
|
|
|
applyItemSelection(_pressState.itemId, selStatus);
|
|
|
|
_mouseAction = MouseAction::Selecting;
|
|
|
|
repaintItem(pressLayout);
|
|
|
|
} else {
|
|
|
|
_mouseAction = MouseAction::PrepareSelect;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-10-09 21:55:30 +00:00
|
|
|
} else if (!_pressWasInactive) {
|
|
|
|
_mouseAction = MouseAction::PrepareSelect; // start items select
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-13 19:07:04 +00:00
|
|
|
if (!pressLayout) {
|
2017-10-09 21:55:30 +00:00
|
|
|
_mouseAction = MouseAction::None;
|
|
|
|
} else if (_mouseAction == MouseAction::None) {
|
2017-10-13 19:07:04 +00:00
|
|
|
mouseActionCancel();
|
2017-10-09 21:55:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ListWidget::mouseActionCancel() {
|
2018-01-27 13:59:24 +00:00
|
|
|
_pressState = MouseState();
|
2017-10-09 21:55:30 +00:00
|
|
|
_mouseAction = MouseAction::None;
|
2017-10-13 19:07:04 +00:00
|
|
|
clearDragSelection();
|
2017-10-09 21:55:30 +00:00
|
|
|
_wasSelectedText = false;
|
2017-10-13 19:07:04 +00:00
|
|
|
// _widget->noSelectingScroll(); // #TODO scroll by drag
|
2017-10-09 21:55:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ListWidget::performDrag() {
|
|
|
|
if (_mouseAction != MouseAction::Dragging) return;
|
|
|
|
|
2017-10-13 19:07:04 +00:00
|
|
|
auto uponSelected = false;
|
|
|
|
if (_pressState.itemId && _pressState.inside) {
|
|
|
|
if (hasSelectedItems()) {
|
|
|
|
uponSelected = isItemUnderPressSelected();
|
|
|
|
} else if (auto pressLayout = getExistingLayout(
|
|
|
|
_pressState.itemId)) {
|
2018-01-27 13:59:24 +00:00
|
|
|
StateRequest request;
|
2019-06-12 13:26:04 +00:00
|
|
|
request.flags |= Ui::Text::StateRequest::Flag::LookupSymbol;
|
2017-10-13 19:07:04 +00:00
|
|
|
auto dragState = pressLayout->getState(
|
|
|
|
_pressState.cursor,
|
|
|
|
request);
|
|
|
|
uponSelected = isPressInSelectedText(dragState);
|
2017-10-09 21:55:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
auto pressedHandler = ClickHandler::getPressed();
|
|
|
|
|
2017-12-18 09:07:18 +00:00
|
|
|
if (dynamic_cast<VoiceSeekClickHandler*>(pressedHandler.get())) {
|
2017-10-09 21:55:30 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
TextWithEntities sel;
|
|
|
|
QList<QUrl> urls;
|
|
|
|
if (uponSelected) {
|
|
|
|
// sel = getSelectedText();
|
|
|
|
} else if (pressedHandler) {
|
|
|
|
sel = { pressedHandler->dragText(), EntitiesInText() };
|
|
|
|
//if (!sel.isEmpty() && sel.at(0) != '/' && sel.at(0) != '@' && sel.at(0) != '#') {
|
|
|
|
// urls.push_back(QUrl::fromEncoded(sel.toUtf8())); // Google Chrome crashes in Mac OS X O_o
|
|
|
|
//}
|
|
|
|
}
|
2019-04-08 15:10:06 +00:00
|
|
|
//if (auto mimeData = MimeDataFromText(sel)) {
|
2017-10-13 19:07:04 +00:00
|
|
|
// clearDragSelection();
|
2017-10-09 21:55:30 +00:00
|
|
|
// _widget->noSelectingScroll();
|
|
|
|
|
|
|
|
// if (!urls.isEmpty()) mimeData->setUrls(urls);
|
|
|
|
// if (uponSelected && !Adaptive::OneColumn()) {
|
|
|
|
// auto selectedState = getSelectionState();
|
|
|
|
// if (selectedState.count > 0 && selectedState.count == selectedState.canForwardCount) {
|
2019-07-24 11:13:51 +00:00
|
|
|
// session().data().setMimeForwardIds(collectSelectedIds());
|
2018-01-11 15:51:59 +00:00
|
|
|
// mimeData->setData(qsl("application/x-td-forward"), "1");
|
2017-10-09 21:55:30 +00:00
|
|
|
// }
|
|
|
|
// }
|
2017-12-09 15:13:06 +00:00
|
|
|
// _controller->parentController()->window()->launchDrag(std::move(mimeData));
|
2017-10-09 21:55:30 +00:00
|
|
|
// return;
|
|
|
|
//} else {
|
|
|
|
// auto forwardMimeType = QString();
|
2019-08-02 13:21:09 +00:00
|
|
|
// auto pressedMedia = static_cast<HistoryView::Media*>(nullptr);
|
2017-10-13 19:07:04 +00:00
|
|
|
// if (auto pressedItem = _pressState.layout) {
|
2017-10-09 21:55:30 +00:00
|
|
|
// pressedMedia = pressedItem->getMedia();
|
2018-01-27 13:59:24 +00:00
|
|
|
// if (_mouseCursorState == CursorState::Date || (pressedMedia && pressedMedia->dragItem())) {
|
2019-07-24 11:13:51 +00:00
|
|
|
// session().data().setMimeForwardIds(session().data().itemOrItsGroup(pressedItem));
|
2018-01-11 15:51:59 +00:00
|
|
|
// forwardMimeType = qsl("application/x-td-forward");
|
2017-10-09 21:55:30 +00:00
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// if (auto pressedLnkItem = App::pressedLinkItem()) {
|
|
|
|
// if ((pressedMedia = pressedLnkItem->getMedia())) {
|
|
|
|
// if (forwardMimeType.isEmpty() && pressedMedia->dragItemByHandler(pressedHandler)) {
|
2019-07-24 11:13:51 +00:00
|
|
|
// session().data().setMimeForwardIds({ 1, pressedLnkItem->fullId() });
|
2018-01-11 15:51:59 +00:00
|
|
|
// forwardMimeType = qsl("application/x-td-forward");
|
2017-10-09 21:55:30 +00:00
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// if (!forwardMimeType.isEmpty()) {
|
|
|
|
// auto mimeData = std::make_unique<QMimeData>();
|
|
|
|
// mimeData->setData(forwardMimeType, "1");
|
|
|
|
// if (auto document = (pressedMedia ? pressedMedia->getDocument() : nullptr)) {
|
2020-04-09 14:02:09 +00:00
|
|
|
// auto filepath = document->filepath(true);
|
2017-10-09 21:55:30 +00:00
|
|
|
// if (!filepath.isEmpty()) {
|
|
|
|
// QList<QUrl> urls;
|
|
|
|
// urls.push_back(QUrl::fromLocalFile(filepath));
|
|
|
|
// mimeData->setUrls(urls);
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
// // This call enters event loop and can destroy any QObject.
|
2017-12-09 15:13:06 +00:00
|
|
|
// _controller->parentController()->window()->launchDrag(std::move(mimeData));
|
2017-10-09 21:55:30 +00:00
|
|
|
// return;
|
|
|
|
// }
|
|
|
|
//}
|
|
|
|
}
|
|
|
|
|
2018-01-26 15:40:11 +00:00
|
|
|
void ListWidget::mouseActionFinish(
|
|
|
|
const QPoint &globalPosition,
|
|
|
|
Qt::MouseButton button) {
|
|
|
|
mouseActionUpdate(globalPosition);
|
2017-10-09 21:55:30 +00:00
|
|
|
|
2017-10-13 19:07:04 +00:00
|
|
|
auto pressState = base::take(_pressState);
|
|
|
|
repaintItem(pressState.itemId);
|
|
|
|
|
|
|
|
auto simpleSelectionChange = pressState.itemId
|
|
|
|
&& pressState.inside
|
|
|
|
&& !_pressWasInactive
|
|
|
|
&& (button != Qt::RightButton)
|
|
|
|
&& (_mouseAction == MouseAction::PrepareDrag
|
|
|
|
|| _mouseAction == MouseAction::PrepareSelect);
|
|
|
|
auto needSelectionToggle = simpleSelectionChange
|
|
|
|
&& hasSelectedItems();
|
|
|
|
auto needSelectionClear = simpleSelectionChange
|
|
|
|
&& hasSelectedText();
|
|
|
|
|
|
|
|
auto activated = ClickHandler::unpressed();
|
2017-10-20 18:52:48 +00:00
|
|
|
if (_mouseAction == MouseAction::Dragging
|
|
|
|
|| _mouseAction == MouseAction::Selecting) {
|
2017-12-18 09:07:18 +00:00
|
|
|
activated = nullptr;
|
2017-10-13 19:07:04 +00:00
|
|
|
} else if (needSelectionToggle) {
|
2017-12-18 09:07:18 +00:00
|
|
|
activated = nullptr;
|
2017-10-09 21:55:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_wasSelectedText = false;
|
|
|
|
if (activated) {
|
|
|
|
mouseActionCancel();
|
2019-09-16 11:14:06 +00:00
|
|
|
ActivateClickHandler(window(), activated, button);
|
2017-10-09 21:55:30 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-10-13 19:07:04 +00:00
|
|
|
|
|
|
|
if (needSelectionToggle) {
|
|
|
|
toggleItemSelection(pressState.itemId);
|
|
|
|
} else if (needSelectionClear) {
|
|
|
|
clearSelected();
|
2017-10-09 21:55:30 +00:00
|
|
|
} else if (_mouseAction == MouseAction::Selecting) {
|
2017-10-13 19:07:04 +00:00
|
|
|
if (!_dragSelected.empty()) {
|
|
|
|
applyDragSelection();
|
|
|
|
} else if (!_selected.empty() && !_pressWasInactive) {
|
|
|
|
auto selection = _selected.cbegin()->second;
|
|
|
|
if (selection.text != FullSelection
|
|
|
|
&& selection.text.from == selection.text.to) {
|
|
|
|
clearSelected();
|
2017-12-09 15:13:06 +00:00
|
|
|
//_controller->parentController()->window()->setInnerFocus(); // #TODO focus
|
2017-10-13 19:07:04 +00:00
|
|
|
}
|
|
|
|
}
|
2017-10-09 21:55:30 +00:00
|
|
|
}
|
|
|
|
_mouseAction = MouseAction::None;
|
|
|
|
_mouseSelectType = TextSelectType::Letters;
|
2017-10-13 19:07:04 +00:00
|
|
|
//_widget->noSelectingScroll(); // #TODO scroll by drag
|
2017-10-09 21:55:30 +00:00
|
|
|
//_widget->updateTopBarSelection();
|
|
|
|
|
2020-11-06 05:12:18 +00:00
|
|
|
//if (QGuiApplication::clipboard()->supportsSelection() && hasSelectedText()) { // #TODO linux clipboard
|
2019-09-16 11:14:06 +00:00
|
|
|
// TextUtilities::SetClipboardText(_selected.cbegin()->first->selectedText(_selected.cbegin()->second), QClipboard::Selection);
|
2017-10-09 21:55:30 +00:00
|
|
|
//}
|
|
|
|
}
|
|
|
|
|
2017-10-13 19:07:04 +00:00
|
|
|
void ListWidget::applyDragSelection() {
|
|
|
|
applyDragSelection(_selected);
|
|
|
|
clearDragSelection();
|
2017-10-20 16:19:42 +00:00
|
|
|
pushSelectedItems();
|
2017-10-13 19:07:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ListWidget::applyDragSelection(SelectedMap &applyTo) const {
|
|
|
|
if (_dragSelectAction == DragSelectAction::Selecting) {
|
|
|
|
for (auto &[universalId,data] : _dragSelected) {
|
|
|
|
changeItemSelection(applyTo, universalId, FullSelection);
|
|
|
|
}
|
|
|
|
} else if (_dragSelectAction == DragSelectAction::Deselecting) {
|
|
|
|
for (auto &[universalId,data] : _dragSelected) {
|
|
|
|
applyTo.remove(universalId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-05 15:35:52 +00:00
|
|
|
void ListWidget::refreshHeight() {
|
|
|
|
resize(width(), recountHeight());
|
|
|
|
}
|
|
|
|
|
2017-10-04 17:15:08 +00:00
|
|
|
int ListWidget::recountHeight() {
|
2017-11-17 16:20:31 +00:00
|
|
|
if (_sections.empty()) {
|
|
|
|
if (auto count = _slice.fullCount()) {
|
|
|
|
if (*count == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-10-05 15:35:52 +00:00
|
|
|
auto cachedPadding = padding();
|
|
|
|
auto result = cachedPadding.top();
|
2017-10-04 17:15:08 +00:00
|
|
|
for (auto §ion : _sections) {
|
2017-10-05 15:35:52 +00:00
|
|
|
section.setTop(result);
|
2017-10-04 17:15:08 +00:00
|
|
|
result += section.height();
|
|
|
|
}
|
2017-11-17 16:20:31 +00:00
|
|
|
return result + cachedPadding.bottom();
|
2017-10-05 15:35:52 +00:00
|
|
|
}
|
|
|
|
|
2017-10-13 19:07:04 +00:00
|
|
|
void ListWidget::mouseActionUpdate() {
|
2017-10-09 21:55:30 +00:00
|
|
|
mouseActionUpdate(_mousePosition);
|
2017-10-04 17:15:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ListWidget::clearStaleLayouts() {
|
|
|
|
for (auto i = _layouts.begin(); i != _layouts.end();) {
|
|
|
|
if (i->second.stale) {
|
2017-10-20 16:19:42 +00:00
|
|
|
if (i->second.item.get() == _overLayout) {
|
|
|
|
_overLayout = nullptr;
|
|
|
|
}
|
2020-05-21 12:27:04 +00:00
|
|
|
_heavyLayouts.erase(i->second.item.get());
|
2017-10-04 17:15:08 +00:00
|
|
|
i = _layouts.erase(i);
|
|
|
|
} else {
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-05 15:35:52 +00:00
|
|
|
auto ListWidget::findSectionByItem(
|
|
|
|
UniversalMsgId universalId) -> std::vector<Section>::iterator {
|
2017-11-20 12:23:20 +00:00
|
|
|
return ranges::lower_bound(
|
2017-10-05 15:35:52 +00:00
|
|
|
_sections,
|
|
|
|
universalId,
|
2017-11-20 12:23:20 +00:00
|
|
|
std::greater<>(),
|
|
|
|
[](const Section §ion) { return section.minId(); });
|
2017-10-05 15:35:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
auto ListWidget::findSectionAfterTop(
|
|
|
|
int top) -> std::vector<Section>::iterator {
|
2017-11-20 12:23:20 +00:00
|
|
|
return ranges::lower_bound(
|
2017-10-05 15:35:52 +00:00
|
|
|
_sections,
|
|
|
|
top,
|
2017-11-20 12:23:20 +00:00
|
|
|
std::less_equal<>(),
|
|
|
|
[](const Section §ion) { return section.bottom(); });
|
2017-10-05 15:35:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
auto ListWidget::findSectionAfterTop(
|
|
|
|
int top) const -> std::vector<Section>::const_iterator {
|
2017-11-20 12:23:20 +00:00
|
|
|
return ranges::lower_bound(
|
2017-10-05 15:35:52 +00:00
|
|
|
_sections,
|
|
|
|
top,
|
2017-11-20 12:23:20 +00:00
|
|
|
std::less_equal<>(),
|
|
|
|
[](const Section §ion) { return section.bottom(); });
|
2017-10-05 15:35:52 +00:00
|
|
|
}
|
2017-10-13 19:07:04 +00:00
|
|
|
|
2017-10-05 15:35:52 +00:00
|
|
|
auto ListWidget::findSectionAfterBottom(
|
|
|
|
std::vector<Section>::const_iterator from,
|
|
|
|
int bottom) const -> std::vector<Section>::const_iterator {
|
2017-11-20 12:23:20 +00:00
|
|
|
return ranges::lower_bound(
|
2017-10-05 15:35:52 +00:00
|
|
|
from,
|
|
|
|
_sections.end(),
|
|
|
|
bottom,
|
2017-11-20 12:23:20 +00:00
|
|
|
std::less<>(),
|
|
|
|
[](const Section §ion) { return section.top(); });
|
2017-10-05 15:35:52 +00:00
|
|
|
}
|
|
|
|
|
2018-07-19 14:27:09 +00:00
|
|
|
ListWidget::~ListWidget() {
|
|
|
|
if (_contextMenu) {
|
|
|
|
// We don't want it to be called after ListWidget is destroyed.
|
|
|
|
_contextMenu->setDestroyedCallback(nullptr);
|
|
|
|
}
|
|
|
|
}
|
2017-10-04 17:15:08 +00:00
|
|
|
|
2017-10-04 12:39:59 +00:00
|
|
|
} // namespace Media
|
|
|
|
} // namespace Info
|