Better font choosing (#286)
This commit is contained in:
parent
9c0d3e79ad
commit
852b3fe036
4 changed files with 165 additions and 1 deletions
|
|
@ -51,6 +51,7 @@
|
|||
"ktg_fonts_semibold": "Semibold font",
|
||||
"ktg_fonts_semibold_is_bold": "Bold font face",
|
||||
"ktg_fonts_monospaced": "Monospaced font",
|
||||
"ktg_fonts_size": "Font size: {pixels}px",
|
||||
"ktg_fonts_use_system_font": "Use system font",
|
||||
"ktg_fonts_use_original_metrics": "Use Open Sans height",
|
||||
"ktg_settings_network": "Network",
|
||||
|
|
|
|||
|
|
@ -51,6 +51,7 @@
|
|||
"ktg_fonts_semibold": "Полужирный шрифт",
|
||||
"ktg_fonts_semibold_is_bold": "Жирное начертание",
|
||||
"ktg_fonts_monospaced": "Моноширинный шрифт",
|
||||
"ktg_fonts_size": "Размер шрифтв: {pixels} пикс.",
|
||||
"ktg_fonts_use_system_font": "Использовать системный шрифт",
|
||||
"ktg_fonts_use_original_metrics": "Высота Open Sans",
|
||||
"ktg_settings_network": "Сеть",
|
||||
|
|
|
|||
|
|
@ -15,16 +15,113 @@ https://github.com/kotatogram/kotatogram-desktop/blob/dev/LEGAL
|
|||
#include "ui/widgets/checkbox.h"
|
||||
#include "ui/widgets/buttons.h"
|
||||
#include "ui/widgets/input_fields.h"
|
||||
#include "ui/widgets/labels.h"
|
||||
#include "ui/widgets/continuous_sliders.h"
|
||||
#include "styles/style_layers.h"
|
||||
#include "styles/style_boxes.h"
|
||||
#include "styles/style_settings.h"
|
||||
#include "ui/boxes/confirm_box.h"
|
||||
#include "kotato/json_settings.h"
|
||||
#include "lang/lang_keys.h"
|
||||
#include "app.h"
|
||||
|
||||
#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 ¤t, 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,
|
||||
const QStringList &fontList,
|
||||
const QString &defaultValue) {
|
||||
_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());
|
||||
QObject::connect(field, &Ui::InputField::changed, [=] {
|
||||
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());
|
||||
})));
|
||||
});
|
||||
if (!defaultValue.isEmpty()) {
|
||||
_view->setCurrentItem(fontList.indexOf(defaultValue));
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
object_ptr<QVBoxLayout> _layout;
|
||||
object_ptr<FontListView> _view;
|
||||
};
|
||||
|
||||
FontsBox::FontsBox(QWidget* parent)
|
||||
: _owned(this)
|
||||
, _content(_owned.data())
|
||||
, _fontSize(cFontSize())
|
||||
{
|
||||
}
|
||||
|
||||
|
|
@ -57,6 +154,13 @@ void FontsBox::prepare() {
|
|||
0,
|
||||
st::boxPadding.right(),
|
||||
st::boxPadding.bottom()));
|
||||
_mainFontList = _content->add(
|
||||
object_ptr<RpFontListView>(_content),
|
||||
QMargins(
|
||||
st::boxPadding.left(),
|
||||
st::boxPadding.bottom(),
|
||||
st::boxPadding.right(),
|
||||
st::boxPadding.bottom()));
|
||||
_semiboldFontName = _content->add(
|
||||
object_ptr<Ui::InputField>(_content, st::defaultInputField, rktr("ktg_fonts_semibold")),
|
||||
QMargins(
|
||||
|
|
@ -64,6 +168,13 @@ void FontsBox::prepare() {
|
|||
0,
|
||||
st::boxPadding.right(),
|
||||
st::boxPadding.bottom()));
|
||||
_semiboldFontList = _content->add(
|
||||
object_ptr<RpFontListView>(_content),
|
||||
QMargins(
|
||||
st::boxPadding.left(),
|
||||
st::boxPadding.bottom(),
|
||||
st::boxPadding.right(),
|
||||
st::boxPadding.bottom()));
|
||||
_semiboldIsBold = _content->add(
|
||||
object_ptr<Ui::Checkbox>(_content, ktr("ktg_fonts_semibold_is_bold"), cSemiboldFontIsBold()),
|
||||
QMargins(
|
||||
|
|
@ -78,6 +189,39 @@ void FontsBox::prepare() {
|
|||
0,
|
||||
st::boxPadding.right(),
|
||||
st::boxPadding.bottom()));
|
||||
_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,
|
||||
st::settingsAudioVolumeLabel),
|
||||
st::settingsAudioVolumeLabelPadding);
|
||||
_fontSizeSlider = _content->add(
|
||||
object_ptr<Ui::MediaSlider>(
|
||||
_content,
|
||||
st::settingsAudioVolumeSlider),
|
||||
st::settingsAudioVolumeSliderPadding);
|
||||
const auto updateFontSizeLabel = [=](int value) {
|
||||
const auto pixels = QString::number(value);
|
||||
_fontSizeLabel->setText(
|
||||
ktr("ktg_fonts_size", { "pixels", pixels }));
|
||||
};
|
||||
const auto updateFontSize = [=](int value) {
|
||||
updateFontSizeLabel(value);
|
||||
_fontSize = value;
|
||||
};
|
||||
_fontSizeSlider->resize(st::settingsAudioVolumeSlider.seekSize);
|
||||
_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(
|
||||
|
|
@ -98,6 +242,11 @@ void FontsBox::prepare() {
|
|||
_monospacedFontName->setText(cMonospaceFont());
|
||||
}
|
||||
|
||||
const auto fontNames = QFontDatabase().families();
|
||||
_mainFontList->prepare(_mainFontName, fontNames, cMainFont());
|
||||
_semiboldFontList->prepare(_semiboldFontName, fontNames, cSemiboldFont());
|
||||
_monospacedFontList->prepare(_monospacedFontName, fontNames, cMonospaceFont());
|
||||
|
||||
auto wrap = object_ptr<Ui::OverrideMargins>(this, std::move(_owned));
|
||||
setDimensionsToContent(st::boxWidth, wrap.data());
|
||||
setInnerWidget(std::move(wrap));
|
||||
|
|
@ -123,6 +272,7 @@ void FontsBox::save() {
|
|||
cSetSemiboldFont(semiboldFont);
|
||||
cSetSemiboldFontIsBold(semiboldIsBold);
|
||||
cSetMonospaceFont(monospacedFont);
|
||||
cSetFontSize(_fontSize);
|
||||
Kotato::JsonSettings::Write();
|
||||
App::restart();
|
||||
};
|
||||
|
|
@ -143,6 +293,7 @@ void FontsBox::resetToDefault() {
|
|||
cSetSemiboldFont(QString());
|
||||
cSetSemiboldFontIsBold(false);
|
||||
cSetMonospaceFont(QString());
|
||||
cSetFontSize(0);
|
||||
#ifdef DESKTOP_APP_USE_PACKAGED_FONTS
|
||||
cSetUseSystemFont(true);
|
||||
#else
|
||||
|
|
|
|||
|
|
@ -8,13 +8,17 @@ https://github.com/kotatogram/kotatogram-desktop/blob/dev/LEGAL
|
|||
#pragma once
|
||||
|
||||
#include "ui/layers/box_content.h"
|
||||
#include "ui/wrap/vertical_layout.h"
|
||||
|
||||
namespace Ui {
|
||||
class VerticalLayout;
|
||||
class Checkbox;
|
||||
class InputField;
|
||||
class LabelSimple;
|
||||
class MediaSlider;
|
||||
} // namespace Ui
|
||||
|
||||
class RpFontListView;
|
||||
|
||||
class FontsBox : public Ui::BoxContent {
|
||||
public:
|
||||
FontsBox(QWidget* parent);
|
||||
|
|
@ -33,7 +37,14 @@ private:
|
|||
QPointer<Ui::Checkbox> _useSystemFont;
|
||||
QPointer<Ui::Checkbox> _useOriginalMetrics;
|
||||
QPointer<Ui::InputField> _mainFontName;
|
||||
QPointer<RpFontListView> _mainFontList;
|
||||
QPointer<Ui::InputField> _semiboldFontName;
|
||||
QPointer<RpFontListView> _semiboldFontList;
|
||||
QPointer<Ui::Checkbox> _semiboldIsBold;
|
||||
QPointer<Ui::InputField> _monospacedFontName;
|
||||
QPointer<RpFontListView> _monospacedFontList;
|
||||
QPointer<Ui::LabelSimple> _fontSizeLabel;
|
||||
QPointer<Ui::MediaSlider> _fontSizeSlider;
|
||||
|
||||
int _fontSize;
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue