2
0
mirror of https://github.com/kotatogram/kotatogram-desktop synced 2025-08-27 12:57:09 +00:00

246 lines
7.1 KiB
C++
Raw Permalink Normal View History

2019-10-14 02:17:53 +03:00
/*
This file is part of Kotatogram Desktop,
the unofficial app based on Telegram Desktop.
For license and copyright information please follow this link:
https://github.com/kotatogram/kotatogram-desktop/blob/dev/LEGAL
*/
2021-08-25 01:09:37 +03:00
#include "kotato/boxes/kotato_fonts_box.h"
2019-10-14 02:17:53 +03:00
2021-08-24 14:01:27 +03:00
#include "kotato/kotato_lang.h"
2022-01-10 03:16:41 +03:00
#include "kotato/kotato_settings.h"
#include "base/platform/base_platform_info.h"
#include "ui/wrap/vertical_layout.h"
#include "ui/wrap/padding_wrap.h"
#include "ui/wrap/wrap.h"
2019-10-14 02:17:53 +03:00
#include "ui/widgets/checkbox.h"
#include "ui/widgets/buttons.h"
2024-07-01 01:16:47 +04:00
#include "ui/widgets/fields/input_field.h"
2021-12-31 17:18:24 +04:00
#include "ui/widgets/labels.h"
#include "ui/widgets/continuous_sliders.h"
2019-11-06 16:22:25 +03:00
#include "styles/style_layers.h"
2019-10-14 02:17:53 +03:00
#include "styles/style_boxes.h"
2024-07-01 01:16:47 +04:00
#include "styles/style_calls.h"
2021-12-31 17:18:24 +04:00
#include "styles/style_settings.h"
2021-12-07 09:25:44 +03:00
#include "ui/boxes/confirm_box.h"
2019-10-14 02:17:53 +03:00
#include "lang/lang_keys.h"
#include "core/application.h"
2019-10-14 02:17:53 +03:00
2021-12-31 17:18:24 +04:00
#include <QFontDatabase>
#include <QListView>
#include <QStringListModel>
#include <QVBoxLayout>
class FontListView : public QListView {
public:
FontListView(QWidget *parent)
: QListView(parent) {
setModel(new QStringListModel(parent));
setEditTriggers(NoEditTriggers);
setFont(st::normalFont);
}
inline QStringListModel *model() const {
return static_cast<QStringListModel *>(QListView::model());
}
inline void setCurrentItem(int item) {
QListView::setCurrentIndex(static_cast<QAbstractListModel*>(model())->index(item));
}
inline int currentItem() const {
return QListView::currentIndex().row();
}
inline int count() const {
return model()->rowCount();
}
inline QString currentText() const {
int row = QListView::currentIndex().row();
return row < 0 ? QString() : model()->stringList().at(row);
}
void currentChanged(const QModelIndex &current, const QModelIndex &previous) override {
QListView::currentChanged(current, previous);
if (current.isValid())
_highlighted.fire_copy(model()->stringList().at(current.row()));
}
QString text(int i) const {
return model()->stringList().at(i);
}
rpl::producer<QString> highlighted() {
return _highlighted.events();
}
rpl::lifetime &lifetime() {
return _lifetime;
}
private:
rpl::event_stream<QString> _highlighted;
rpl::lifetime _lifetime;
};
class RpFontListView : public Ui::RpWidget {
public:
RpFontListView(QWidget *parent)
: Ui::RpWidget(parent)
, _layout(this)
, _view(this) {
_layout->addWidget(_view);
}
void prepare(
Ui::InputField *field,
2022-01-10 03:16:41 +03:00
const QStringList &fontList) {
2021-12-31 17:18:24 +04:00
_view->model()->setStringList(fontList);
resize(0, _view->sizeHintForRow(0) * 10);
_view->highlighted(
) | rpl::start_with_next([=](QString fontName) {
if (!field->hasFocus()) {
field->setText(fontName);
}
}, _view->lifetime());
2024-07-01 01:16:47 +04:00
field->changes(
) | rpl::start_with_next([=] {
2021-12-31 17:18:24 +04:00
if (field->getLastText().isEmpty()) {
_view->setCurrentItem(-1);
return;
}
_view->setCurrentItem(
std::distance(fontList.begin(), ranges::find_if(
fontList,
[&](const auto &fontName) {
return fontName.startsWith(field->getLastText());
})));
2024-07-01 01:16:47 +04:00
}, field->lifetime());
2022-01-10 03:16:41 +03:00
const auto defaultValue = field->getLastText().trimmed();
2021-12-31 17:18:24 +04:00
if (!defaultValue.isEmpty()) {
_view->setCurrentItem(fontList.indexOf(defaultValue));
}
}
private:
object_ptr<QVBoxLayout> _layout;
object_ptr<FontListView> _view;
};
2019-10-14 02:17:53 +03:00
FontsBox::FontsBox(QWidget* parent)
: _owned(this)
, _content(_owned.data())
2022-01-10 03:16:41 +03:00
, _fontSize(::Kotato::JsonSettings::GetIntWithPending("fonts/size"))
2019-10-14 02:17:53 +03:00
{
}
void FontsBox::prepare() {
2021-08-24 14:01:27 +03:00
setTitle(rktr("ktg_fonts_title"));
2019-10-14 02:17:53 +03:00
addButton(tr::lng_settings_save(), [=] { save(); });
addButton(tr::lng_cancel(), [=] { closeBox(); });
2021-08-24 14:01:27 +03:00
addLeftButton(rktr("ktg_fonts_reset"), [=] { resetToDefault(); });
2019-10-14 02:17:53 +03:00
_semiboldIsBold = _content->add(
2022-01-10 03:16:41 +03:00
object_ptr<Ui::Checkbox>(_content,
ktr("ktg_fonts_semibold_is_bold"),
::Kotato::JsonSettings::GetBoolWithPending("fonts/semibold_is_bold")),
QMargins(
st::boxPadding.left(),
0,
st::boxPadding.right(),
st::boxPadding.bottom()));
_monospacedFontName = _content->add(
object_ptr<Ui::InputField>(_content, st::defaultInputField, rktr("ktg_fonts_monospaced")),
QMargins(
st::boxPadding.left(),
0,
st::boxPadding.right(),
st::boxPadding.bottom()));
2021-12-31 17:18:24 +04:00
_monospacedFontList = _content->add(
object_ptr<RpFontListView>(_content),
QMargins(
st::boxPadding.left(),
st::boxPadding.bottom(),
st::boxPadding.right(),
st::boxPadding.bottom()));
_fontSizeLabel = _content->add(
object_ptr<Ui::LabelSimple>(
_content,
2024-07-01 01:16:47 +04:00
st::ktgSettingsSliderLabel),
st::groupCallDelayLabelMargin);
2021-12-31 17:18:24 +04:00
_fontSizeSlider = _content->add(
object_ptr<Ui::MediaSlider>(
_content,
2024-07-01 01:16:47 +04:00
st::defaultContinuousSlider),
st::localStorageLimitMargin);
2021-12-31 17:18:24 +04:00
const auto updateFontSizeLabel = [=](int value) {
2021-12-31 16:52:28 +03:00
const auto prefix = (value >= 0) ? qsl("+") : QString();
const auto pixels = prefix + QString::number(value);
2021-12-31 17:18:24 +04:00
_fontSizeLabel->setText(
ktr("ktg_fonts_size", { "pixels", pixels }));
};
const auto updateFontSize = [=](int value) {
updateFontSizeLabel(value);
_fontSize = value;
};
2024-07-01 01:16:47 +04:00
_fontSizeSlider->resize(st::defaultContinuousSlider.seekSize);
2021-12-31 17:18:24 +04:00
_fontSizeSlider->setPseudoDiscrete(
21,
[](int val) { return val - 10; },
_fontSize,
updateFontSize);
updateFontSizeLabel(_fontSize);
_content->add(
object_ptr<Ui::FlatLabel>(_content, rktr("ktg_fonts_about"), st::boxDividerLabel),
QMargins(
st::boxPadding.left(),
0,
st::boxPadding.right(),
st::boxPadding.bottom()));
2022-01-10 03:16:41 +03:00
_monospacedFontName->setText(::Kotato::JsonSettings::GetStringWithPending("fonts/monospaced"));
2019-10-14 02:17:53 +03:00
2021-12-31 17:18:24 +04:00
const auto fontNames = QFontDatabase().families();
2022-01-10 03:16:41 +03:00
_monospacedFontList->prepare(_monospacedFontName, fontNames);
2021-12-31 17:18:24 +04:00
auto wrap = object_ptr<Ui::OverrideMargins>(this, std::move(_owned));
setDimensionsToContent(st::boxWidth, wrap.data());
setInnerWidget(std::move(wrap));
2019-10-14 02:17:53 +03:00
}
void FontsBox::setInnerFocus() {
_monospacedFontName->setFocusFast();
2019-10-14 02:17:53 +03:00
}
void FontsBox::save() {
::Kotato::JsonSettings::SetAfterRestart("fonts/monospaced", _monospacedFontName->getLastText().trimmed());
::Kotato::JsonSettings::SetAfterRestart("fonts/semibold_is_bold", _semiboldIsBold->checked());
::Kotato::JsonSettings::SetAfterRestart("fonts/size", _fontSize);
::Kotato::JsonSettings::Write();
2019-10-14 07:27:00 +03:00
const auto box = std::make_shared<QPointer<BoxContent>>();
*box = getDelegate()->show(
2024-07-01 01:16:47 +04:00
Ui::MakeConfirmBox({
.text = tr::lng_settings_need_restart(),
.confirmed = [] { Core::Restart(); },
.cancelled = crl::guard(this, [=] { closeBox(); box->data()->closeBox(); }),
.confirmText = tr::lng_settings_restart_now(),
.cancelText = tr::lng_settings_restart_later(),
}));
2019-10-14 02:17:53 +03:00
}
void FontsBox::resetToDefault() {
::Kotato::JsonSettings::ResetAfterRestart("fonts/monospaced");
::Kotato::JsonSettings::ResetAfterRestart("fonts/semibold_is_bold");
::Kotato::JsonSettings::ResetAfterRestart("fonts/size");
::Kotato::JsonSettings::Write();
2019-10-14 07:27:00 +03:00
const auto box = std::make_shared<QPointer<BoxContent>>();
*box = getDelegate()->show(
2024-07-01 01:16:47 +04:00
Ui::MakeConfirmBox({
.text = tr::lng_settings_need_restart(),
.confirmed = [] { Core::Restart(); },
.cancelled = crl::guard(this, [=] { closeBox(); box->data()->closeBox(); }),
.confirmText = tr::lng_settings_restart_now(),
.cancelText = tr::lng_settings_restart_later(),
}));
}