File: peer_lists_box.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 (446 lines) | stat: -rw-r--r-- 11,386 bytes parent folder | download
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
/*
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 "boxes/peer_lists_box.h"

#include "lang/lang_keys.h"
#include "ui/wrap/slide_wrap.h"
#include "ui/wrap/vertical_layout.h"
#include "ui/widgets/multi_select.h"
#include "ui/widgets/scroll_area.h"
#include "main/main_session.h"
#include "data/data_session.h"
#include "data/data_peer.h"
#include "styles/style_boxes.h"
#include "styles/style_layers.h"

PeerListsBox::PeerListsBox(
	QWidget*,
	std::vector<std::unique_ptr<PeerListController>> controllers,
	Fn<void(not_null<PeerListsBox*>)> init)
: _lists(makeLists(std::move(controllers)))
, _init(std::move(init)) {
	Expects(!_lists.empty());
}

auto PeerListsBox::collectSelectedRows()
-> std::vector<not_null<PeerData*>> {
	auto result = std::vector<not_null<PeerData*>>();
	auto items = _select
		? _select->entity()->getItems()
		: QVector<uint64>();
	if (!items.empty()) {
		result.reserve(items.size());
		const auto session = &firstController()->session();
		for (const auto itemId : items) {
			const auto foreign = [&] {
				for (const auto &list : _lists) {
					if (list.controller->isForeignRow(itemId)) {
						return true;
					}
				}
				return false;
			}();
			if (!foreign) {
				result.push_back(session->data().peer(PeerId(itemId)));
			}
		}
	}
	return result;
}


PeerListsBox::List PeerListsBox::makeList(
		std::unique_ptr<PeerListController> controller) {
	auto delegate = std::make_unique<Delegate>(this, controller.get());
	return {
		std::move(controller),
		std::move(delegate),
	};
}

std::vector<PeerListsBox::List> PeerListsBox::makeLists(
		std::vector<std::unique_ptr<PeerListController>> controllers) {
	auto result = std::vector<List>();
	result.reserve(controllers.size());
	for (auto &controller : controllers) {
		result.push_back(makeList(std::move(controller)));
	}
	return result;
}

not_null<PeerListController*> PeerListsBox::firstController() const {
	return _lists.front().controller.get();
}

void PeerListsBox::createMultiSelect() {
	Expects(_select == nullptr);

	auto entity = object_ptr<Ui::MultiSelect>(
		this,
		(firstController()->selectSt()
			? *firstController()->selectSt()
			: st::defaultMultiSelect),
		tr::lng_participant_filter());
	_select.create(this, std::move(entity));
	_select->heightValue(
	) | rpl::start_with_next(
		[this] { updateScrollSkips(); },
		lifetime());
	_select->entity()->setSubmittedCallback([=](Qt::KeyboardModifiers) {
		for (const auto &list : _lists) {
			if (list.content->submitted()) {
				break;
			}
		}
	});
	_select->entity()->setQueryChangedCallback([=](const QString &query) {
		searchQueryChanged(query);
	});
	_select->entity()->setItemRemovedCallback([=](uint64 itemId) {
		for (const auto &list : _lists) {
			if (list.controller->handleDeselectForeignRow(itemId)) {
				return;
			}
		}
		const auto session = &firstController()->session();
		if (const auto peer = session->data().peerLoaded(PeerId(itemId))) {
			const auto id = peer->id;
			for (const auto &list : _lists) {
				if (const auto row = list.delegate->peerListFindRow(id.value)) {
					list.content->changeCheckState(
						row,
						false,
						anim::type::normal);
					update();
				}
				list.controller->itemDeselectedHook(peer);
			}
		}
	});
	_select->resizeToWidth(firstController()->contentWidth());
	_select->moveToLeft(0, 0);
}

int PeerListsBox::getTopScrollSkip() const {
	auto result = 0;
	if (_select && !_select->isHidden()) {
		result += _select->height();
	}
	return result;
}

void PeerListsBox::updateScrollSkips() {
	// If we show / hide the search field scroll top is fixed.
	// If we resize search field by bubbles scroll bottom is fixed.
	setInnerTopSkip(getTopScrollSkip(), _scrollBottomFixed);
	if (!_select->animating()) {
		_scrollBottomFixed = true;
	}
}

void PeerListsBox::prepare() {
	auto rows = setInnerWidget(
		object_ptr<Ui::VerticalLayout>(this),
		st::boxScroll);
	for (auto &list : _lists) {
		const auto content = rows->add(object_ptr<PeerListContent>(
			rows,
			list.controller.get()));
		list.content = content;
		list.delegate->setContent(content);
		list.controller->setDelegate(list.delegate.get());

		content->scrollToRequests(
		) | rpl::start_with_next([=](Ui::ScrollToRequest request) {
			const auto skip = content->y();
			scrollToY(
				skip + request.ymin,
				(request.ymax >= 0) ? (skip + request.ymax) : request.ymax);
		}, lifetime());

		content->selectedIndexValue(
		) | rpl::filter([=](int index) {
			return (index >= 0);
		}) | rpl::start_with_next([=] {
			for (const auto &list : _lists) {
				if (list.content && list.content != content) {
					list.content->clearSelection();
				}
			}
		}, lifetime());
	}
	rows->resizeToWidth(firstController()->contentWidth());

	setDimensions(firstController()->contentWidth(), st::boxMaxListHeight);
	if (_select) {
		_select->finishAnimating();
		Ui::SendPendingMoveResizeEvents(_select);
		_scrollBottomFixed = true;
		scrollToY(0);
	}

	if (_init) {
		_init(this);
	}
}

void PeerListsBox::keyPressEvent(QKeyEvent *e) {
	const auto skipRows = [&](int rows) {
		if (rows == 0) {
			return;
		}
		for (const auto &list : _lists) {
			if (list.content->hasPressed()) {
				return;
			}
		}
		const auto from = begin(_lists), till = end(_lists);
		auto i = from;
		for (; i != till; ++i) {
			if (i->content->hasSelection()) {
				break;
			}
		}
		if (i == till && rows < 0) {
			return;
		}
		if (rows > 0) {
			if (i == till) {
				i = from;
			}
			for (; i != till; ++i) {
				const auto result = i->content->selectSkip(rows);
				if (result.shouldMoveTo - result.reallyMovedTo >= rows) {
					continue;
				} else if (result.reallyMovedTo >= result.shouldMoveTo) {
					return;
				} else {
					rows = result.shouldMoveTo - result.reallyMovedTo;
				}
			}
		} else {
			for (++i; i != from;) {
				const auto result = (--i)->content->selectSkip(rows);
				if (result.shouldMoveTo - result.reallyMovedTo <= rows) {
					continue;
				} else if (result.reallyMovedTo <= result.shouldMoveTo) {
					return;
				} else {
					rows = result.shouldMoveTo - result.reallyMovedTo;
				}
			}
		}
	};
	const auto rowsInPage = [&] {
		const auto rowHeight = firstController()->computeListSt().item.height;
		return height() / rowHeight;
	};
	if (e->key() == Qt::Key_Down) {
		skipRows(1);
	} else if (e->key() == Qt::Key_Up) {
		skipRows(-1);
	} else if (e->key() == Qt::Key_PageDown) {
		skipRows(rowsInPage());
	} else if (e->key() == Qt::Key_PageUp) {
		skipRows(-rowsInPage());
	} else if (e->key() == Qt::Key_Escape && _select && !_select->entity()->getQuery().isEmpty()) {
		_select->entity()->clearQuery();
	} else {
		BoxContent::keyPressEvent(e);
	}
}

void PeerListsBox::searchQueryChanged(const QString &query) {
	scrollToY(0);
	for (const auto &list : _lists) {
		list.content->searchQueryChanged(query);
	}
}

void PeerListsBox::resizeEvent(QResizeEvent *e) {
	BoxContent::resizeEvent(e);

	if (_select) {
		_select->resizeToWidth(width());
		_select->moveToLeft(0, 0);

		updateScrollSkips();
	}

	for (const auto &list : _lists) {
		list.content->resizeToWidth(width());
	}
}

void PeerListsBox::paintEvent(QPaintEvent *e) {
	auto p = QPainter(this);

	const auto &bg = (firstController()->listSt()
		? *firstController()->listSt()
		: st::peerListBox).bg;
	for (const auto &rect : e->region()) {
		p.fillRect(rect, bg);
	}
}

void PeerListsBox::setInnerFocus() {
	if (!_select || !_select->toggled()) {
		_lists.front().content->setFocus();
	} else {
		_select->entity()->setInnerFocus();
	}
}

PeerListsBox::Delegate::Delegate(
	not_null<PeerListsBox*> box,
	not_null<PeerListController*> controller)
: _box(box)
, _controller(controller)
, _show(_box) {
}

void PeerListsBox::Delegate::peerListSetTitle(rpl::producer<QString> title) {
}

void PeerListsBox::Delegate::peerListSetAdditionalTitle(
	rpl::producer<QString> title) {
}

void PeerListsBox::Delegate::peerListSetRowChecked(
		not_null<PeerListRow*> row,
		bool checked) {
	if (checked) {
		_box->addSelectItem(row, anim::type::normal);
		PeerListContentDelegate::peerListSetRowChecked(row, checked);
		peerListUpdateRow(row);

		// This call deletes row from _searchRows.
		_box->_select->entity()->clearQuery();
	} else {
		// The itemRemovedCallback will call changeCheckState() here.
		_box->_select->entity()->removeItem(row->id());
		peerListUpdateRow(row);
	}
}

void PeerListsBox::Delegate::peerListSetForeignRowChecked(
		not_null<PeerListRow*> row,
		bool checked,
		anim::type animated) {
	if (checked) {
		_box->addSelectItem(row, animated);

		// This call deletes row from _searchRows.
		_box->_select->entity()->clearQuery();
	} else {
		// The itemRemovedCallback will call changeCheckState() here.
		_box->_select->entity()->removeItem(row->id());
	}
}

void PeerListsBox::Delegate::peerListScrollToTop() {
	_box->scrollToY(0);
}

void PeerListsBox::Delegate::peerListSetSearchMode(PeerListSearchMode mode) {
	PeerListContentDelegate::peerListSetSearchMode(mode);
	_box->setSearchMode(mode);
}

void PeerListsBox::setSearchMode(PeerListSearchMode mode) {
	auto selectVisible = (mode != PeerListSearchMode::Disabled);
	if (selectVisible && !_select) {
		createMultiSelect();
		_select->toggle(!selectVisible, anim::type::instant);
	}
	if (_select) {
		_select->toggle(selectVisible, anim::type::normal);
		_scrollBottomFixed = false;
		setInnerFocus();
	}
}

void PeerListsBox::Delegate::peerListFinishSelectedRowsBunch() {
	Expects(_box->_select != nullptr);

	_box->_select->entity()->finishItemsBunch();
}

void PeerListsBox::Delegate::peerListShowBox(
		object_ptr<Ui::BoxContent> content,
		Ui::LayerOptions options) {
	_show.showBox(std::move(content), options);
}

void PeerListsBox::Delegate::peerListHideLayer() {
	_show.hideLayer();
}

not_null<QWidget*> PeerListsBox::Delegate::peerListToastParent() {
	return _show.toastParent();
}

bool PeerListsBox::Delegate::peerListIsRowChecked(
		not_null<PeerListRow*> row) {
	return _box->_select
		? _box->_select->entity()->hasItem(row->id())
		: false;
}

int PeerListsBox::Delegate::peerListSelectedRowsCount() {
	return _box->_select ? _box->_select->entity()->getItemsCount() : 0;
}

void PeerListsBox::addSelectItem(
		not_null<PeerData*> peer,
		anim::type animated) {
	addSelectItem(
		peer->id.value,
		peer->shortName(),
		(peer->isForum()
			? ForceRoundUserpicCallback(peer)
			: PaintUserpicCallback(peer, false)),
		animated);
}

void PeerListsBox::addSelectItem(
		not_null<PeerListRow*> row,
		anim::type animated) {
	addSelectItem(
		row->id(),
		row->generateShortName(),
		row->generatePaintUserpicCallback(true),
		animated);
}

void PeerListsBox::addSelectItem(
		uint64 itemId,
		const QString &text,
		Ui::MultiSelect::PaintRoundImage paintUserpic,
		anim::type animated) {
	if (!_select) {
		createMultiSelect();
		_select->hide(anim::type::instant);
	}
	const auto &activeBg = (firstController()->selectSt()
		? *firstController()->selectSt()
		: st::defaultMultiSelect).item.textActiveBg;
	if (animated == anim::type::instant) {
		_select->entity()->addItemInBunch(
			itemId,
			text,
			activeBg,
			std::move(paintUserpic));
	} else {
		_select->entity()->addItem(
			itemId,
			text,
			activeBg,
			std::move(paintUserpic));
	}
}