mirror of
https://github.com/telegramdesktop/tdesktop
synced 2025-03-03 21:07:53 +00:00
Moved box with vertical drum picker for time to separated module.
This commit is contained in:
parent
6a7cbc545f
commit
209e09ab66
@ -7,13 +7,12 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
||||
*/
|
||||
#include "menu/menu_ttl.h"
|
||||
|
||||
#include "base/event_filter.h"
|
||||
#include "lang/lang_keys.h"
|
||||
#include "ui/boxes/time_picker_box.h"
|
||||
#include "ui/layers/generic_box.h"
|
||||
#include "ui/text/format_values.h"
|
||||
#include "ui/ui_utility.h"
|
||||
#include "ui/widgets/labels.h"
|
||||
#include "ui/widgets/vertical_drum_picker.h"
|
||||
#if 0
|
||||
#include "ui/boxes/choose_time.h"
|
||||
#include "ui/widgets/menu/menu_action.h"
|
||||
@ -21,7 +20,6 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
||||
#include "styles/style_dialogs.h" // dialogsScamFont
|
||||
#include "styles/style_menu_icons.h"
|
||||
#endif
|
||||
#include "styles/style_chat.h"
|
||||
#include "styles/style_layers.h"
|
||||
|
||||
namespace TTLMenu {
|
||||
@ -37,118 +35,7 @@ constexpr auto kTTLDurHours3 = crl::time(24 * 7);
|
||||
constexpr auto kTTLDurSeconds3 = kTTLDurHours3 * 3600;
|
||||
constexpr auto kTTLDurHours4 = crl::time(24 * 30);
|
||||
constexpr auto kTTLDurSeconds4 = kTTLDurHours4 * 3600;
|
||||
#endif
|
||||
|
||||
void SetupPickerAndConfirm(
|
||||
not_null<Ui::GenericBox*> box,
|
||||
Fn<void(TimeId)> callback,
|
||||
TimeId startTtlPeriod) {
|
||||
const auto ttls = std::vector<int>{
|
||||
(86400 * 1),
|
||||
(86400 * 2),
|
||||
(86400 * 3),
|
||||
(86400 * 4),
|
||||
(86400 * 5),
|
||||
(86400 * 6),
|
||||
(86400 * 7 * 1),
|
||||
(86400 * 7 * 2),
|
||||
(86400 * 7 * 3),
|
||||
(86400 * 30 * 1),
|
||||
(86400 * 30 * 2),
|
||||
(86400 * 30 * 3),
|
||||
(86400 * 30 * 4),
|
||||
(86400 * 30 * 5),
|
||||
(86400 * 30 * 6),
|
||||
(86400 * 30 * 12),
|
||||
};
|
||||
const auto phrases = ranges::views::all(
|
||||
ttls
|
||||
) | ranges::views::transform(Ui::FormatTTL) | ranges::to_vector;
|
||||
|
||||
const auto startIndex = [&] {
|
||||
const auto it = ranges::find(ttls, startTtlPeriod);
|
||||
return (it == end(ttls)) ? 0 : std::distance(begin(ttls), it);
|
||||
}();
|
||||
|
||||
const auto content = box->addRow(object_ptr<Ui::FixedHeightWidget>(
|
||||
box,
|
||||
st::historyMessagesTTLPickerHeight));
|
||||
|
||||
const auto font = st::boxTextFont;
|
||||
const auto maxPhraseWidth = [&] {
|
||||
const auto maxPhrase = ranges::max_element(
|
||||
phrases,
|
||||
std::less<>(),
|
||||
[&](const QString &s) { return font->width(s); });
|
||||
return font->width(*maxPhrase);
|
||||
}();
|
||||
const auto itemHeight = st::historyMessagesTTLPickerItemHeight;
|
||||
auto paintCallback = [=](
|
||||
Painter &p,
|
||||
int index,
|
||||
float64 y,
|
||||
float64 distanceFromCenter,
|
||||
int outerWidth) {
|
||||
const auto r = QRectF(0, y, outerWidth, itemHeight);
|
||||
const auto progress = std::abs(distanceFromCenter);
|
||||
p.setOpacity(1. - progress);
|
||||
p.setFont(font);
|
||||
p.setPen(st::defaultFlatLabel.textFg);
|
||||
p.drawText(r, phrases[index], style::al_center);
|
||||
};
|
||||
|
||||
const auto picker = Ui::CreateChild<Ui::VerticalDrumPicker>(
|
||||
content,
|
||||
std::move(paintCallback),
|
||||
phrases.size(),
|
||||
itemHeight,
|
||||
startIndex);
|
||||
|
||||
content->sizeValue(
|
||||
) | rpl::start_with_next([=](const QSize &s) {
|
||||
picker->resize(maxPhraseWidth, s.height());
|
||||
picker->moveToLeft((s.width() - picker->width()) / 2, 0);
|
||||
}, content->lifetime());
|
||||
|
||||
content->paintRequest(
|
||||
) | rpl::start_with_next([=](const QRect &r) {
|
||||
Painter p(content);
|
||||
|
||||
p.fillRect(r, Qt::transparent);
|
||||
|
||||
const auto lineRect = QRect(
|
||||
0,
|
||||
content->height() / 2,
|
||||
content->width(),
|
||||
st::defaultInputField.borderActive);
|
||||
p.fillRect(lineRect.translated(0, itemHeight / 2), st::activeLineFg);
|
||||
p.fillRect(lineRect.translated(0, -itemHeight / 2), st::activeLineFg);
|
||||
}, content->lifetime());
|
||||
|
||||
base::install_event_filter(content, [=](not_null<QEvent*> e) {
|
||||
if ((e->type() == QEvent::MouseButtonPress)
|
||||
|| (e->type() == QEvent::MouseButtonRelease)
|
||||
|| (e->type() == QEvent::MouseMove)) {
|
||||
picker->handleMouseEvent(static_cast<QMouseEvent*>(e.get()));
|
||||
} else if (e->type() == QEvent::Wheel) {
|
||||
picker->handleWheelEvent(static_cast<QWheelEvent*>(e.get()));
|
||||
}
|
||||
return base::EventFilterResult::Continue;
|
||||
});
|
||||
base::install_event_filter(box, [=](not_null<QEvent*> e) {
|
||||
if (e->type() == QEvent::KeyPress) {
|
||||
picker->handleKeyEvent(static_cast<QKeyEvent*>(e.get()));
|
||||
}
|
||||
return base::EventFilterResult::Continue;
|
||||
});
|
||||
|
||||
box->addButton(tr::lng_settings_save(), [=] {
|
||||
callback(ttls[picker->index()]);
|
||||
box->getDelegate()->hideLayer();
|
||||
});
|
||||
}
|
||||
|
||||
#if 0
|
||||
class IconWithText final : public Ui::Menu::Action {
|
||||
public:
|
||||
using Ui::Menu::Action::Action;
|
||||
@ -273,7 +160,36 @@ void TTLBox(not_null<Ui::GenericBox*> box, Args args) {
|
||||
box,
|
||||
std::move(args.about),
|
||||
st::boxLabel));
|
||||
SetupPickerAndConfirm(box, args.callback, args.startTtl);
|
||||
|
||||
const auto ttls = std::vector<TimeId>{
|
||||
(86400 * 1),
|
||||
(86400 * 2),
|
||||
(86400 * 3),
|
||||
(86400 * 4),
|
||||
(86400 * 5),
|
||||
(86400 * 6),
|
||||
(86400 * 7 * 1),
|
||||
(86400 * 7 * 2),
|
||||
(86400 * 7 * 3),
|
||||
(86400 * 30 * 1),
|
||||
(86400 * 30 * 2),
|
||||
(86400 * 30 * 3),
|
||||
(86400 * 30 * 4),
|
||||
(86400 * 30 * 5),
|
||||
(86400 * 30 * 6),
|
||||
(86400 * 30 * 12),
|
||||
};
|
||||
const auto phrases = ranges::views::all(
|
||||
ttls
|
||||
) | ranges::views::transform(Ui::FormatTTL) | ranges::to_vector;
|
||||
|
||||
const auto pickerTtl = TimePickerBox(box, ttls, phrases, args.startTtl);
|
||||
|
||||
box->addButton(tr::lng_settings_save(), [=] {
|
||||
args.callback(pickerTtl());
|
||||
box->getDelegate()->hideLayer();
|
||||
});
|
||||
|
||||
box->setTitle(tr::lng_manage_messages_ttl_title());
|
||||
|
||||
box->addButton(tr::lng_cancel(), [=] { box->closeBox(); });
|
||||
|
107
Telegram/SourceFiles/ui/boxes/time_picker_box.cpp
Normal file
107
Telegram/SourceFiles/ui/boxes/time_picker_box.cpp
Normal file
@ -0,0 +1,107 @@
|
||||
/*
|
||||
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 "ui/boxes/time_picker_box.h"
|
||||
|
||||
#include "base/event_filter.h"
|
||||
#include "lang/lang_keys.h"
|
||||
#include "ui/layers/generic_box.h"
|
||||
#include "ui/ui_utility.h"
|
||||
#include "ui/widgets/vertical_drum_picker.h"
|
||||
#include "styles/style_chat.h"
|
||||
#include "styles/style_layers.h"
|
||||
|
||||
namespace Ui {
|
||||
|
||||
Fn<TimeId()> TimePickerBox(
|
||||
not_null<GenericBox*> box,
|
||||
std::vector<TimeId> values,
|
||||
std::vector<QString> phrases,
|
||||
TimeId startValue) {
|
||||
Expects(phrases.size() == values.size());
|
||||
|
||||
const auto startIndex = [&] {
|
||||
const auto it = ranges::find(values, startValue);
|
||||
return (it == end(values)) ? 0 : std::distance(begin(values), it);
|
||||
}();
|
||||
|
||||
const auto content = box->addRow(object_ptr<Ui::FixedHeightWidget>(
|
||||
box,
|
||||
st::historyMessagesTTLPickerHeight));
|
||||
|
||||
const auto font = st::boxTextFont;
|
||||
const auto maxPhraseWidth = [&] {
|
||||
const auto maxPhrase = ranges::max_element(
|
||||
phrases,
|
||||
std::less<>(),
|
||||
[&](const QString &s) { return font->width(s); });
|
||||
return font->width(*maxPhrase);
|
||||
}();
|
||||
const auto itemHeight = st::historyMessagesTTLPickerItemHeight;
|
||||
auto paintCallback = [=](
|
||||
Painter &p,
|
||||
int index,
|
||||
float64 y,
|
||||
float64 distanceFromCenter,
|
||||
int outerWidth) {
|
||||
const auto r = QRectF(0, y, outerWidth, itemHeight);
|
||||
const auto progress = std::abs(distanceFromCenter);
|
||||
p.setOpacity(1. - progress);
|
||||
p.setFont(font);
|
||||
p.setPen(st::defaultFlatLabel.textFg);
|
||||
p.drawText(r, phrases[index], style::al_center);
|
||||
};
|
||||
|
||||
const auto picker = Ui::CreateChild<Ui::VerticalDrumPicker>(
|
||||
content,
|
||||
std::move(paintCallback),
|
||||
phrases.size(),
|
||||
itemHeight,
|
||||
startIndex);
|
||||
|
||||
content->sizeValue(
|
||||
) | rpl::start_with_next([=](const QSize &s) {
|
||||
picker->resize(maxPhraseWidth, s.height());
|
||||
picker->moveToLeft((s.width() - picker->width()) / 2, 0);
|
||||
}, content->lifetime());
|
||||
|
||||
content->paintRequest(
|
||||
) | rpl::start_with_next([=](const QRect &r) {
|
||||
Painter p(content);
|
||||
|
||||
p.fillRect(r, Qt::transparent);
|
||||
|
||||
const auto lineRect = QRect(
|
||||
0,
|
||||
content->height() / 2,
|
||||
content->width(),
|
||||
st::defaultInputField.borderActive);
|
||||
p.fillRect(lineRect.translated(0, itemHeight / 2), st::activeLineFg);
|
||||
p.fillRect(lineRect.translated(0, -itemHeight / 2), st::activeLineFg);
|
||||
}, content->lifetime());
|
||||
|
||||
base::install_event_filter(content, [=](not_null<QEvent*> e) {
|
||||
if ((e->type() == QEvent::MouseButtonPress)
|
||||
|| (e->type() == QEvent::MouseButtonRelease)
|
||||
|| (e->type() == QEvent::MouseMove)) {
|
||||
picker->handleMouseEvent(static_cast<QMouseEvent*>(e.get()));
|
||||
} else if (e->type() == QEvent::Wheel) {
|
||||
picker->handleWheelEvent(static_cast<QWheelEvent*>(e.get()));
|
||||
}
|
||||
return base::EventFilterResult::Continue;
|
||||
});
|
||||
base::install_event_filter(box, [=](not_null<QEvent*> e) {
|
||||
if (e->type() == QEvent::KeyPress) {
|
||||
picker->handleKeyEvent(static_cast<QKeyEvent*>(e.get()));
|
||||
}
|
||||
return base::EventFilterResult::Continue;
|
||||
});
|
||||
|
||||
return [=] { return values[picker->index()]; };
|
||||
}
|
||||
|
||||
} // namespace Ui
|
20
Telegram/SourceFiles/ui/boxes/time_picker_box.h
Normal file
20
Telegram/SourceFiles/ui/boxes/time_picker_box.h
Normal file
@ -0,0 +1,20 @@
|
||||
/*
|
||||
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
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
namespace Ui {
|
||||
|
||||
class GenericBox;
|
||||
|
||||
Fn<TimeId()> TimePickerBox(
|
||||
not_null<GenericBox*> box,
|
||||
std::vector<TimeId> values,
|
||||
std::vector<QString> phrases,
|
||||
TimeId startValue);
|
||||
|
||||
} // namespace Ui
|
@ -155,6 +155,8 @@ PRIVATE
|
||||
ui/boxes/report_box.h
|
||||
ui/boxes/single_choice_box.cpp
|
||||
ui/boxes/single_choice_box.h
|
||||
ui/boxes/time_picker_box.cpp
|
||||
ui/boxes/time_picker_box.h
|
||||
|
||||
ui/chat/attach/attach_abstract_single_file_preview.cpp
|
||||
ui/chat/attach/attach_abstract_single_file_preview.h
|
||||
|
Loading…
Reference in New Issue
Block a user