File: dialogs_list.cpp

package info (click to toggle)
telegram-desktop 4.6.5%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 53,300 kB
  • sloc: cpp: 605,857; python: 3,978; ansic: 1,636; sh: 965; makefile: 841; objc: 652; javascript: 187; xml: 165
file content (205 lines) | stat: -rw-r--r-- 5,036 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
This file is part of Telegram Desktop,
the official desktop application for the Telegram messaging service.

For license and copyright information please follow this link:
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/
#include "dialogs/dialogs_list.h"

#include "dialogs/dialogs_entry.h"
#include "dialogs/ui/dialogs_layout.h"
#include "data/data_session.h"

namespace Dialogs {

List::List(SortMode sortMode, FilterId filterId)
: _sortMode(sortMode)
, _filterId(filterId) {
}

List::const_iterator List::cfind(Row *value) const {
	return value
		? (cbegin() + value->index())
		: cend();
}

not_null<Row*> List::addToEnd(Key key) {
	if (const auto result = getRow(key)) {
		return result;
	}
	const auto result = _rowByKey.emplace(
		key,
		std::make_unique<Row>(key, _rows.size(), height())
	).first->second.get();
	result->recountHeight(_narrowRatio);
	_rows.emplace_back(result);
	if (_sortMode == SortMode::Date) {
		adjustByDate(result);
	}
	return result;
}

Row *List::adjustByName(Key key) {
	Expects(_sortMode == SortMode::Name);

	const auto row = getRow(key);
	if (!row) {
		return nullptr;
	}
	adjustByName(row);
	return row;
}

not_null<Row*> List::addByName(Key key) {
	Expects(_sortMode == SortMode::Name);

	const auto row = addToEnd(key);
	adjustByName(key);
	return row;
}

void List::adjustByName(not_null<Row*> row) {
	Expects(row->index() >= 0 && row->index() < _rows.size());

	const auto &key = row->entry()->chatListNameSortKey();
	const auto index = row->index();
	const auto i = _rows.begin() + index;
	const auto before = std::find_if(i + 1, _rows.end(), [&](Row *row) {
		return row->entry()->chatListNameSortKey().compare(key) >= 0;
	});
	if (before != i + 1) {
		rotate(i, i + 1, before);
	} else if (i != _rows.begin()) {
		const auto from = std::make_reverse_iterator(i);
		const auto after = std::find_if(from, _rows.rend(), [&](Row *row) {
			return row->entry()->chatListNameSortKey().compare(key) <= 0;
		}).base();
		if (after != i) {
			rotate(after, i, i + 1);
		}
	}
}

void List::adjustByDate(not_null<Row*> row) {
	Expects(_sortMode == SortMode::Date);

	const auto key = row->sortKey(_filterId);
	const auto index = row->index();
	const auto i = _rows.begin() + index;
	const auto before = std::find_if(i + 1, _rows.end(), [&](Row *row) {
		return (row->sortKey(_filterId) <= key);
	});
	if (before != i + 1) {
		rotate(i, i + 1, before);
	} else {
		const auto from = std::make_reverse_iterator(i);
		const auto after = std::find_if(from, _rows.rend(), [&](Row *row) {
			return (row->sortKey(_filterId) >= key);
		}).base();
		if (after != i) {
			rotate(after, i, i + 1);
		}
	}
}

bool List::updateHeight(Key key, float64 narrowRatio) {
	const auto i = _rowByKey.find(key);
	if (i == _rowByKey.cend()) {
		return false;
	}
	const auto row = i->second.get();
	const auto index = row->index();
	auto top = row->top();
	const auto was = row->height();
	row->recountHeight(narrowRatio);
	if (row->height() == was) {
		return false;
	}
	for (auto i = _rows.begin() + index, e = _rows.end(); i != e; ++i) {
		(*i)->_top = top;
		top += (*i)->height();
	}
	return true;
}

bool List::updateHeights(float64 narrowRatio) {
	_narrowRatio = narrowRatio;
	auto was = height();
	auto top = 0;
	for (const auto &row : _rows) {
		row->_top = top;
		row->recountHeight(narrowRatio);
		top += row->height();
	}
	return (height() != was);
}

bool List::moveToTop(Key key) {
	const auto i = _rowByKey.find(key);
	if (i == _rowByKey.cend()) {
		return false;
	}
	const auto index = i->second->index();
	const auto begin = _rows.begin();
	rotate(begin, begin + index, begin + index + 1);
	return true;
}

void List::rotate(
		std::vector<not_null<Row*>>::iterator first,
		std::vector<not_null<Row*>>::iterator middle,
		std::vector<not_null<Row*>>::iterator last) {
	auto top = (*first)->top();
	std::rotate(first, middle, last);

	auto count = (last - first);
	auto index = (first - _rows.begin());
	while (count--) {
		const auto row = *first++;
		row->_index = index++;
		row->_top = top;
		top += row->height();
	}
}

bool List::remove(Key key, Row *replacedBy) {
	auto i = _rowByKey.find(key);
	if (i == _rowByKey.cend()) {
		return false;
	}

	const auto row = i->second.get();
	row->entry()->owner().dialogsRowReplaced({ row, replacedBy });

	auto top = row->top();
	const auto index = row->index();
	_rows.erase(_rows.begin() + index);
	for (auto i = index, count = int(_rows.size()); i != count; ++i) {
		const auto row = _rows[i];
		row->_index = i;
		row->_top = top;
		top += row->height();
	}
	_rowByKey.erase(i);
	return true;
}

Row *List::rowAtY(int y) const {
	const auto i = findByY(y);
	if (i == cend()) {
		return nullptr;
	}
	const auto row = *i;
	const auto top = row->top();
	const auto bottom = top + row->height();
	return (top <= y && bottom > y) ? row.get() : nullptr;
}

List::iterator List::findByY(int y) const {
	return ranges::lower_bound(_rows, y, ranges::less(), [](const Row *row) {
		return row->top() + row->height();
	});
}

} // namespace Dialogs