ZaStoGram_desktop/Telegram/SourceFiles/settings/sections/settings_folders_local.cpp
loop-uh 1c949a1036 Папки: любые значки и цвета, теги на чатах, новый список в настройках
- Модель: у папки есть цвет (#RRGGBB, синхронизируется) рядом со значком-эмодзи.
- Значок идёт перед названием на вкладках, в боковой панели, в чипах подпапок.
- Теги папок ZaStoGram всегда видны на строках чатов, цвет — собственный
  цвет папки: так видно, какие папки пересекаются. Теги Telegram по-прежнему
  зависят от своей настройки.
- Вкладка и чип подпапки окрашены в цвет папки.
- Редактор: предпросмотр, готовые значки и цвета, поля для любого эмодзи и
  любого #RRGGBB.
- Список в настройках переписан по образцу страницы папок Telegram: круглый
  значок цвета папки, название, строка «N чатов · M подпапок», корзина;
  перетаскивание и меню по правой кнопке сохранены.
- Исправлены ошибки сборки списка (указатели VerticalLayout::add, заголовок меню).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-04 10:10:49 -05:00

405 lines
12 KiB
C++

/*
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 "settings/sections/settings_folders_local.h"
#include "base/event_filter.h"
#include "boxes/filters/edit_local_folder_box.h"
#include "boxes/filters/local_folder_menu.h"
#include "data/data_chat_filters.h"
#include "data/data_chat_filters_tree.h"
#include "data/data_session.h"
#include "lang/lang_keys.h"
#include "main/main_session.h"
#include "settings/settings_common.h"
#include "ui/layers/generic_box.h"
#include "ui/vertical_list.h"
#include "ui/widgets/buttons.h"
#include "ui/widgets/labels.h"
#include "ui/widgets/menu/menu_add_action_callback.h"
#include "ui/widgets/menu/menu_add_action_callback_factory.h"
#include "ui/widgets/popup_menu.h"
#include "ui/wrap/vertical_layout.h"
#include "window/window_session_controller.h"
#include "ui/painter.h"
#include "ui/rp_widget.h"
#include "styles/palette.h"
#include "styles/style_basic.h"
#include "styles/style_dialogs.h"
#include "styles/style_layers.h"
#include "styles/style_menu_icons.h"
#include "styles/style_settings.h"
#include <QtGui/QMouseEvent>
#include <QtGui/QPainter>
#include <QtWidgets/QApplication>
namespace Settings {
namespace {
struct RowData {
QString title;
QString subtitle;
QString icon;
QColor color;
int depth = 0;
};
// The trash button at the right edge of a row.
class TrashButton final : public Ui::AbstractButton {
public:
explicit TrashButton(QWidget *parent) : AbstractButton(parent) {
setCursor(style::cur_pointer);
}
protected:
void paintEvent(QPaintEvent *e) override {
Painter p(this);
st::menuIconDelete.paintInCenter(p, rect());
}
};
// One folder in the list: a round icon of the folder's own color, the title,
// the line below it, the trash button. Shaped like Telegram's Folders page.
class LocalFolderRow final : public Ui::RippleButton {
public:
LocalFolderRow(QWidget *parent, RowData data)
: RippleButton(parent, st::defaultRippleAnimation)
, _data(std::move(data))
, _trash(base::make_unique_q<TrashButton>(this)) {
}
[[nodiscard]] not_null<Ui::AbstractButton*> trash() const {
return _trash.get();
}
protected:
int resizeGetHeight(int newWidth) override {
return st::defaultDialogRow.height;
}
void resizeEvent(QResizeEvent *e) override {
_trash->setGeometry(width() - height(), 0, height(), height());
}
void paintEvent(QPaintEvent *e) override {
Painter p(this);
paintRipple(p, QPoint());
p.setRenderHint(QPainter::Antialiasing);
const auto h = height();
const auto indent = _data.depth * (h * 6 / 10);
const auto left = st::boxRowPadding.left() + indent;
const auto diameter = h * 6 / 10;
const auto circle = QRect(left, (h - diameter) / 2, diameter, diameter);
p.setPen(Qt::NoPen);
p.setBrush(_data.color.isValid() ? _data.color : st::windowBgActive->c);
p.drawEllipse(circle);
auto font = st::normalFont->f;
font.setPixelSize(diameter * 55 / 100);
p.setFont(font);
p.setPen(QColor(255, 255, 255));
p.drawText(
circle,
Qt::AlignCenter,
_data.icon.isEmpty() ? QString::fromUtf8("📁") : _data.icon);
const auto textLeft = circle.right() + st::boxRowPadding.left();
const auto available = std::max(
1,
width() - textLeft - h);
const auto titleHeight = st::semiboldFont->height;
const auto lineHeight = st::normalFont->height;
const auto top = (h - titleHeight - lineHeight) / 2;
p.setPen(st::windowFg);
p.setFont(st::semiboldFont);
p.drawText(
QRect(textLeft, top, available, titleHeight),
Qt::AlignLeft | Qt::AlignVCenter,
st::semiboldFont->elided(_data.title, available));
p.setPen(st::windowSubTextFg);
p.setFont(st::normalFont);
p.drawText(
QRect(textLeft, top + titleHeight, available, lineHeight),
Qt::AlignLeft | Qt::AlignVCenter,
st::normalFont->elided(_data.subtitle, available));
}
private:
RowData _data;
const base::unique_qptr<TrashButton> _trash;
};
struct DragRow {
FilterId id = 0;
Ui::RpWidget *widget = nullptr;
};
struct ListState {
std::vector<DragRow> rows;
FilterId pressed = 0;
QPoint pressPoint;
bool dragging = false;
bool justDragged = false;
FilterId dragId = 0;
bool targetValid = false;
FilterId targetParent = 0;
FilterId targetBefore = 0;
Ui::RpWidget *indicator = nullptr;
base::unique_qptr<Ui::PopupMenu> menu;
};
} // namespace
void SetupLocalFolders(
not_null<Window::SessionController*> controller,
not_null<Ui::VerticalLayout*> container) {
const auto session = &controller->session();
Ui::AddSkip(container);
Ui::AddSubsectionTitle(container, tr::lng_zsg_folders_title());
const auto list = container->add(
object_ptr<Ui::VerticalLayout>(container));
const auto state = list->lifetime().make_state<ListState>();
// The line that shows where the dragged folder will land.
state->indicator = Ui::CreateChild<Ui::RpWidget>(list);
state->indicator->hide();
state->indicator->setAttribute(Qt::WA_TransparentForMouseEvents);
state->indicator->paintRequest(
) | rpl::on_next([=] {
auto p = QPainter(state->indicator);
p.fillRect(state->indicator->rect(), st::windowBgActive);
}, state->indicator->lifetime());
// Where would the folder land if it is dropped at this point: on the
// middle of a row it goes inside that folder, near the top or the bottom
// edge of a row it goes next to that folder.
const auto updateTarget = [=](QPoint global) {
const auto &local = session->data().chatsFilters().local();
const auto &rows = state->rows;
state->targetValid = false;
if (rows.empty()) {
state->indicator->hide();
return;
}
const auto pos = list->mapFromGlobal(global);
const auto first = rows.front().widget->geometry();
const auto last = rows.back().widget->geometry();
auto parent = FilterId(0);
auto before = FilterId(0);
auto indicatorY = 0;
auto indicatorDepth = 0;
if (pos.y() < first.top()) {
const auto top = local.childrenOf(0);
before = top.empty() ? FilterId(0) : top.front();
indicatorY = first.top();
} else if (pos.y() >= last.bottom()) {
indicatorY = last.bottom();
} else {
auto index = 0;
for (auto i = 0, count = int(rows.size()); i != count; ++i) {
const auto geometry = rows[i].widget->geometry();
if (pos.y() >= geometry.top() && pos.y() < geometry.bottom()) {
index = i;
break;
}
}
const auto target = rows[index].id;
const auto geometry = rows[index].widget->geometry();
const auto fraction = float64(pos.y() - geometry.top())
/ std::max(1, geometry.height());
if (fraction < 0.28) {
parent = local.parentOf(target);
before = target;
indicatorY = geometry.top();
indicatorDepth = local.depth(target);
} else if (fraction > 0.72) {
parent = local.parentOf(target);
const auto siblings = local.childrenOf(parent);
const auto i = ranges::find(siblings, target);
before = (i != siblings.end() && (i + 1) != siblings.end())
? *(i + 1)
: FilterId(0);
indicatorY = geometry.bottom();
indicatorDepth = local.depth(target);
} else {
parent = target;
indicatorY = geometry.bottom();
indicatorDepth = local.depth(target) + 1;
}
}
// A folder cannot go inside itself or inside its own subfolders.
auto inside = false;
auto up = parent;
for (auto guard = 0; up && guard < 1000; ++guard) {
if (up == state->dragId) {
inside = true;
break;
}
up = local.parentOf(up);
}
if (inside || before == state->dragId) {
state->indicator->hide();
return;
}
state->targetValid = true;
state->targetParent = parent;
state->targetBefore = before;
const auto indent = indicatorDepth * st::defaultVerticalListSkip * 4;
const auto thickness = st::lineWidth * 3;
state->indicator->setGeometry(
indent,
indicatorY - thickness / 2,
std::max(1, list->width() - indent),
thickness);
state->indicator->show();
state->indicator->raise();
};
const auto drop = [=] {
if (!state->dragging) {
return;
}
const auto id = state->dragId;
const auto valid = state->targetValid;
const auto parent = state->targetParent;
const auto before = state->targetBefore;
state->dragging = false;
state->indicator->hide();
if (!valid) {
return;
}
// The rows are rebuilt by the change, so not inside their own event.
crl::on_main(list, [=] {
session->data().chatsFilters().local().place(id, parent, before);
});
};
const auto rebuild = [=] {
state->indicator->hide();
state->rows.clear();
state->pressed = 0;
state->dragging = false;
list->clear();
const auto &local = session->data().chatsFilters().local();
for (const auto id : local.ids()) {
const auto folder = local.find(id);
if (!folder) {
continue;
}
const auto depth = local.depth(id);
const auto count = int(local.peersOf(id).size());
const auto subfolders = int(local.childrenOf(id).size());
auto subtitle = tr::lng_zsg_folders_chats_count(
tr::now,
lt_count,
count);
if (subfolders) {
subtitle += u" · "_q + tr::lng_zsg_folders_subfolders_count(
tr::now,
lt_count,
subfolders);
}
if (!folder->sync) {
subtitle += u" · "_q + tr::lng_zsg_folders_local_only(tr::now);
}
const auto button = list->add(object_ptr<LocalFolderRow>(
list,
RowData{
.title = folder->title,
.subtitle = subtitle,
.icon = folder->iconEmoji,
.color = local.colorOf(id),
.depth = depth,
}));
state->rows.push_back({ id, button });
button->trash()->setClickedCallback([=] {
DeleteLocalFolder(controller, id);
});
button->addClickHandler([=] {
if (state->justDragged) {
state->justDragged = false;
return;
}
controller->show(
Box(EditLocalFolderBox, controller, id, FilterId(0)));
});
base::install_event_filter(button, [=](not_null<QEvent*> e) {
const auto type = e->type();
if (type == QEvent::MouseButtonPress) {
const auto mouse = static_cast<QMouseEvent*>(e.get());
if (mouse->button() == Qt::RightButton) {
state->menu = base::make_unique_q<Ui::PopupMenu>(
list,
st::popupMenuWithIcons);
FillLocalFolderMenu(
Ui::Menu::CreateAddActionCallback(state->menu),
controller,
id);
if (!state->menu->empty()) {
state->menu->popup(QCursor::pos());
}
return base::EventFilterResult::Cancel;
} else if (mouse->button() == Qt::LeftButton) {
state->pressed = id;
state->pressPoint = mouse->globalPosition().toPoint();
state->dragging = false;
state->justDragged = false;
}
} else if (type == QEvent::MouseMove) {
const auto mouse = static_cast<QMouseEvent*>(e.get());
if ((mouse->buttons() & Qt::LeftButton)
&& state->pressed == id) {
const auto global = mouse->globalPosition().toPoint();
if (!state->dragging
&& (global - state->pressPoint).manhattanLength()
>= QApplication::startDragDistance()) {
state->dragging = true;
state->dragId = id;
}
if (state->dragging) {
updateTarget(global);
}
}
} else if (type == QEvent::MouseButtonRelease) {
if (state->pressed == id) {
state->pressed = 0;
if (state->dragging) {
// The click that follows is not a click.
state->justDragged = true;
drop();
}
}
}
return base::EventFilterResult::Continue;
});
}
list->resizeToWidth(list->width());
};
session->data().chatsFilters().local().changed(
) | rpl::on_next(rebuild, list->lifetime());
rebuild();
const auto create = AddButtonWithIcon(
container,
tr::lng_zsg_folders_create(),
st::settingsButtonActive,
{ &st::settingsIconAdd, IconType::Round, &st::windowBgActive });
create->setClickedCallback([=] {
controller->show(
Box(EditLocalFolderBox, controller, FilterId(0), FilterId(0)));
});
Ui::AddSkip(container);
Ui::AddDividerText(container, tr::lng_zsg_folders_about());
}
} // namespace Settings