2
0
mirror of https://github.com/kotatogram/kotatogram-desktop synced 2025-08-30 22:25:12 +00:00

[Option][GUI] Font options

This commit is contained in:
RadRussianRus
2022-08-26 15:24:01 +03:00
committed by Eric Kotato
parent f09d824616
commit ed9b395ff1
13 changed files with 424 additions and 4 deletions

View File

@@ -987,6 +987,8 @@ PRIVATE
intro/intro_step.h
intro/intro_widget.cpp
intro/intro_widget.h
kotato/boxes/kotato_fonts_box.cpp
kotato/boxes/kotato_fonts_box.h
kotato/boxes/kotato_radio_box.cpp
kotato/boxes/kotato_radio_box.h
kotato/kotato_lang.cpp

View File

@@ -28,6 +28,17 @@
"ktg_mac_menu_show": "Show Kotatogram",
"ktg_settings_kotato": "Kotatogram Settings",
"ktg_settings_chats": "Chats",
"ktg_fonts_title": "Fonts",
"ktg_settings_fonts": "Change application fonts",
"ktg_fonts_reset": "Reset",
"ktg_fonts_about": "You will need to restart app to apply and see changes.",
"ktg_fonts_main": "Main font",
"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",
"ktg_settings_system": "System",
"ktg_settings_other": "Other",

View File

@@ -1017,3 +1017,7 @@ shortInfoBox: ShortInfoBox {
labeled: infoLabeled;
labeledOneLine: infoLabeledOneLine;
}
fontsBoxTextStyle: TextStyle(defaultTextStyle) {
font: font(13px);
}

View File

@@ -27,12 +27,14 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
namespace {
constexpr auto kDefaultProxyPort = 80;
Core::UiIntegration UiIntegrationInstance;
} // namespace
PreLaunchWindow *PreLaunchWindowInstance = nullptr;
PreLaunchWindow::PreLaunchWindow(QString title) {
Ui::Integration::Set(&UiIntegrationInstance);
style::internal::StartFonts();
setWindowIcon(Window::CreateIcon());

View File

@@ -8,6 +8,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "core/ui_integration.h"
#include "api/api_text_entities.h"
#include "kotato/kotato_settings.h"
#include "core/local_url_handlers.h"
#include "core/file_utilities.h"
#include "core/application.h"
@@ -19,6 +20,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "ui/text/text_custom_emoji.h"
#include "ui/basic_click_handlers.h"
#include "ui/emoji_config.h"
#include "ui/style/style_core_custom_font.h"
#include "lang/lang_keys.h"
#include "platform/platform_specific.h"
#include "boxes/url_auth_box.h"
@@ -136,6 +138,18 @@ void UiIntegration::activationFromTopPanel() {
Platform::IgnoreApplicationActivationRightNow();
}
style::CustomFontSettings UiIntegration::fontSettings() {
return {
::Kotato::JsonSettings::GetString("fonts/main"),
::Kotato::JsonSettings::GetString("fonts/semibold"),
::Kotato::JsonSettings::GetString("fonts/monospaced"),
::Kotato::JsonSettings::GetInt("fonts/size"),
::Kotato::JsonSettings::GetBool("fonts/semibold_is_bold"),
::Kotato::JsonSettings::GetBool("fonts/use_system_font"),
::Kotato::JsonSettings::GetBool("fonts/use_original_metrics"),
};
}
bool UiIntegration::screenIsLocked() {
return Core::App().screenIsLocked();
}

View File

@@ -46,6 +46,7 @@ public:
void activationFromTopPanel() override;
bool screenIsLocked() override;
style::CustomFontSettings fontSettings() override;
std::shared_ptr<ClickHandler> createLinkHandler(
const EntityLinkData &data,

View File

@@ -8264,7 +8264,7 @@ void HistoryWidget::paintEvent(QPaintEvent *e) {
HistoryView::ServiceMessagePainter::PaintBubble(p, st, tr);
p.setPen(st->msgServiceFg());
p.setFont(st::msgServiceFont->f);
p.setFont(st::msgServiceFont);
p.drawTextLeft(tr.left() + st::msgPadding.left(), tr.top() + st::msgServicePadding.top(), width(), tr::lng_willbe_history(tr::now));
//AssertIsDebug();

View File

@@ -0,0 +1,303 @@
/*
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
*/
#include "kotato/boxes/kotato_fonts_box.h"
#include "kotato/kotato_lang.h"
#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"
#include "ui/widgets/checkbox.h"
#include "ui/widgets/buttons.h"
#include "ui/widgets/fields/input_field.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_calls.h"
#include "styles/style_settings.h"
#include "ui/boxes/confirm_box.h"
#include "lang/lang_keys.h"
#include "core/application.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 &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,
const QStringList &fontList) {
_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());
field->changes(
) | rpl::start_with_next([=] {
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());
})));
}, field->lifetime());
const auto defaultValue = field->getLastText().trimmed();
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(::Kotato::JsonSettings::GetIntWithPending("fonts/size"))
{
}
void FontsBox::prepare() {
setTitle(rktr("ktg_fonts_title"));
addButton(tr::lng_settings_save(), [=] { save(); });
addButton(tr::lng_cancel(), [=] { closeBox(); });
addLeftButton(rktr("ktg_fonts_reset"), [=] { resetToDefault(); });
_useSystemFont = _content->add(
object_ptr<Ui::Checkbox>(_content,
ktr("ktg_fonts_use_system_font"),
::Kotato::JsonSettings::GetBoolWithPending("fonts/use_system_font")),
QMargins(
st::boxPadding.left(),
0,
st::boxPadding.right(),
st::boxPadding.bottom()));
_useOriginalMetrics = _content->add(
object_ptr<Ui::Checkbox>(_content,
ktr("ktg_fonts_use_original_metrics"),
::Kotato::JsonSettings::GetBoolWithPending("fonts/use_original_metrics")),
QMargins(
st::boxPadding.left(),
st::boxPadding.bottom(),
st::boxPadding.right(),
st::boxPadding.bottom()));
_mainFontName = _content->add(
object_ptr<Ui::InputField>(_content, st::defaultInputField, rktr("ktg_fonts_main")),
QMargins(
st::boxPadding.left(),
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(
st::boxPadding.left(),
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"),
::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()));
_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::ktgSettingsSliderLabel),
st::groupCallDelayLabelMargin);
_fontSizeSlider = _content->add(
object_ptr<Ui::MediaSlider>(
_content,
st::defaultContinuousSlider),
st::localStorageLimitMargin);
const auto updateFontSizeLabel = [=](int value) {
const auto prefix = (value >= 0) ? qsl("+") : QString();
const auto pixels = prefix + QString::number(value);
_fontSizeLabel->setText(
ktr("ktg_fonts_size", { "pixels", pixels }));
};
const auto updateFontSize = [=](int value) {
updateFontSizeLabel(value);
_fontSize = value;
};
_fontSizeSlider->resize(st::defaultContinuousSlider.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(
st::boxPadding.left(),
0,
st::boxPadding.right(),
st::boxPadding.bottom()));
_mainFontName->setText(::Kotato::JsonSettings::GetStringWithPending("fonts/main"));
_semiboldFontName->setText(::Kotato::JsonSettings::GetStringWithPending("fonts/semibold"));
_monospacedFontName->setText(::Kotato::JsonSettings::GetStringWithPending("fonts/monospaced"));
const auto fontNames = QFontDatabase().families();
_mainFontList->prepare(_mainFontName, fontNames);
_semiboldFontList->prepare(_semiboldFontName, fontNames);
_monospacedFontList->prepare(_monospacedFontName, fontNames);
auto wrap = object_ptr<Ui::OverrideMargins>(this, std::move(_owned));
setDimensionsToContent(st::boxWidth, wrap.data());
setInnerWidget(std::move(wrap));
}
void FontsBox::setInnerFocus() {
_mainFontName->setFocusFast();
}
void FontsBox::save() {
::Kotato::JsonSettings::SetAfterRestart("fonts/main", _mainFontName->getLastText().trimmed());
::Kotato::JsonSettings::SetAfterRestart("fonts/semibold", _semiboldFontName->getLastText().trimmed());
::Kotato::JsonSettings::SetAfterRestart("fonts/monospaced", _monospacedFontName->getLastText().trimmed());
::Kotato::JsonSettings::SetAfterRestart("fonts/semibold_is_bold", _semiboldIsBold->checked());
::Kotato::JsonSettings::SetAfterRestart("fonts/use_system_font", _useSystemFont->checked());
::Kotato::JsonSettings::SetAfterRestart("fonts/use_original_metrics", _useOriginalMetrics->checked());
::Kotato::JsonSettings::SetAfterRestart("fonts/size", _fontSize);
::Kotato::JsonSettings::Write();
const auto box = std::make_shared<QPointer<BoxContent>>();
*box = getDelegate()->show(
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(),
}));
}
void FontsBox::resetToDefault() {
::Kotato::JsonSettings::ResetAfterRestart("fonts/main");
::Kotato::JsonSettings::ResetAfterRestart("fonts/semibold");
::Kotato::JsonSettings::ResetAfterRestart("fonts/monospaced");
::Kotato::JsonSettings::ResetAfterRestart("fonts/semibold_is_bold");
::Kotato::JsonSettings::ResetAfterRestart("fonts/size");
::Kotato::JsonSettings::ResetAfterRestart("fonts/use_system_font");
::Kotato::JsonSettings::ResetAfterRestart("fonts/use_original_metrics");
::Kotato::JsonSettings::Write();
const auto box = std::make_shared<QPointer<BoxContent>>();
*box = getDelegate()->show(
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(),
}));
}

View File

@@ -0,0 +1,50 @@
/*
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
*/
#pragma once
#include "ui/layers/box_content.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);
protected:
void prepare() override;
void setInnerFocus() override;
private:
void save();
void resetToDefault();
object_ptr<Ui::VerticalLayout> _owned;
not_null<Ui::VerticalLayout*> _content;
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;
};

View File

@@ -217,6 +217,32 @@ const std::map<QString, Definition, std::greater<QString>> DefinitionMap {
.defaultValue = false, }},
// Stored settings
{ "fonts/main", {
.type = SettingType::QStringSetting,
.fillerValue = qsl("Open Sans"), }},
{ "fonts/semibold", {
.type = SettingType::QStringSetting,
.fillerValue = qsl("Open Sans Semibold"), }},
{ "fonts/semibold_is_bold", {
.type = SettingType::BoolSetting,
.defaultValue = false, }},
{ "fonts/monospaced", {
.type = SettingType::QStringSetting,
.fillerValue = qsl("Consolas"), }},
{ "fonts/size", {
.type = SettingType::IntSetting,
.defaultValue = 0, }},
{ "fonts/use_system_font", {
.type = SettingType::BoolSetting,
#ifdef DESKTOP_APP_USE_PACKAGED_FONTS
.defaultValue = true,
#else
.defaultValue = Platform::IsLinux(),
#endif
}},
{ "fonts/use_original_metrics", {
.type = SettingType::BoolSetting,
.defaultValue = false, }},
};
using OldOptionKey = QString;

View File

@@ -71,6 +71,14 @@ void SetupKotatoChats(
Ui::AddSkip(container);
Ui::AddSubsectionTitle(container, rktr("ktg_settings_chats"));
container->add(object_ptr<Button>(
container,
rktr("ktg_settings_fonts"),
st::settingsButtonNoIcon
))->addClickHandler([=] {
Ui::show(Box<FontsBox>());
});
Ui::AddSkip(container);
Ui::AddDivider(container);

View File

@@ -77,7 +77,7 @@ void PreviewWindowTitle(Painter &p, const style::palette &palette, QRect body, i
}
p.setPen(st::titleFgActive[palette]);
p.setFont(font);
p.setFont(style::font(font.pixelSize(), 0, font.family()));
p.drawText(titleRect, u"Kotatogram"_q, style::al_center);

View File

@@ -282,8 +282,7 @@ void EmptyUserpic::paint(
x = style::RightToLeft() ? (outerWidth - x - size) : x;
const auto fontsize = (size * 13) / 33;
auto font = st::historyPeerUserpicFont->f;
font.setPixelSize(fontsize);
auto font = style::font(fontsize, st::historyPeerUserpicFont->flags(), st::historyPeerUserpicFont->family());
PainterHighQualityEnabler hq(p);
{