Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement set_ui_options method and add gui_set_font option #29

Merged
merged 1 commit into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/drawoptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,6 @@ void DrawOptions::setFont(const QString &font_name, int font_size)
QRect boundingBoxSingleChar = font_metrics.boundingRect("A");
m_cell_size =
QSize(boundingBoxSingleChar.width(), boundingBoxSingleChar.height()); // FIXME only works for monospaced fonts

emit updated();
}
8 changes: 7 additions & 1 deletion src/drawoptions.hpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
#ifndef DRAWOPTIONS_HPP
#define DRAWOPTIONS_HPP

#include <QObject>
#include <QPainter>
#include <QSize>

class DrawOptions
class DrawOptions : public QObject
{
Q_OBJECT

public:
DrawOptions();
~DrawOptions();
Expand All @@ -15,6 +18,9 @@ class DrawOptions

void setFont(const QString &font_name, int font_size);

signals:
void updated();

private:
QFont m_font;
QSize m_cell_size;
Expand Down
5 changes: 5 additions & 0 deletions src/kakouneclient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ void KakouneClient::handleRequest(QJsonObject request)
{
emit hideInfoBox();
}
else if (method == "set_ui_options")
{
RPC::SetUIOptionsRequest request = RPC::deserializeSetUIOptionsRequest(request_params);
emit setUIOptions(request.options);
}
else
{
qDebug() << "Unkown method: " << method;
Expand Down
2 changes: 2 additions & 0 deletions src/kakouneclient.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ class KakouneClient : public QObject
void showInfoBox();
void hideInfoBox();

void setUIOptions(QMap<QString, QString> options);

private:
void sendRequest(const QString &method_name, QJsonArray params);
void handleRequest(QJsonObject request);
Expand Down
29 changes: 29 additions & 0 deletions src/kakounewidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ KakouneWidget::KakouneWidget(const QString &session_id, DrawOptions *draw_option
m_client = new KakouneClient(session_id, client_arguments, {{"KAKQT_WINDOW_ID", m_id.toString()}});
connect(m_client, &KakouneClient::refresh, this, &KakouneWidget::clientRefreshed);
connect(m_client, &KakouneClient::finished, this, &KakouneWidget::finished);
connect(m_client, &KakouneClient::setUIOptions, this, &KakouneWidget::setUIOptions);

m_textedit = new KakouneTextEdit(m_client, draw_options, this);
m_menu = new KakouneMenu(m_client, draw_options, m_textedit);
Expand Down Expand Up @@ -65,3 +66,31 @@ void KakouneWidget::installEventFilter(QObject *filter)
{
m_textedit->installEventFilter(filter);
}

void KakouneWidget::setUIOptions(QMap<QString, QString> options)
{
for (auto option = options.begin(); option != options.end(); option++)
{
if (option.key() == "gui_set_font")
{
int lastSpaceIndex = option.value().lastIndexOf(' ');

QString font_name = option.value().left(lastSpaceIndex);
QString font_size_str = option.value().mid(lastSpaceIndex + 1);

bool ok;
int font_size = font_size_str.toInt(&ok);
if (!ok)
{
qDebug() << "Error converting font size to integer.";
continue;
}

m_draw_options->setFont(font_name, font_size);
}
else
{
qDebug() << "Unknown ui option: " << option.key();
}
}
}
1 change: 1 addition & 0 deletions src/kakounewidget.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class KakouneWidget : public QWidget

private slots:
void clientRefreshed();
void setUIOptions(QMap<QString, QString> options);
signals:
void finished();
void refresh();
Expand Down
12 changes: 12 additions & 0 deletions src/rpc/rpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,18 @@ RefreshRequest deserializeRefreshRequest(QJsonArray request_params)
return RefreshRequest{request_params.at(0).toBool()};
}

SetUIOptionsRequest deserializeSetUIOptionsRequest(QJsonArray request_params)
{
QMap<QString, QString> options;
QJsonObject root = request_params.at(0).toObject();
for (auto it = root.begin(); it != root.end(); ++it)
{
options.insert(it.key(), it.value().toString());
}

return SetUIOptionsRequest{options};
}

QByteArray serializeRequest(const QString &method_name, QJsonArray params)
{
QJsonObject req{{{"jsonrpc", "2.0"}, {"method", method_name}, {"params", params}}};
Expand Down
6 changes: 6 additions & 0 deletions src/rpc/rpc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ struct RefreshRequest
bool force;
};

struct SetUIOptionsRequest
{
QMap<QString, QString> options;
};

Coord deserializeCoord(QJsonObject coord_serialized);
Face deserializeFace(QJsonObject face_serialized);
Atom deserializeAtom(QJsonObject atom_serialized);
Expand All @@ -90,6 +95,7 @@ MenuShowRequest deserializeMenuShowRequest(QJsonArray request_params);
MenuSelectRequest deserializeMenuSelectRequest(QJsonArray request_params);
InfoShowRequest deserializeInfoShowRequest(QJsonArray request_params);
RefreshRequest deserializeRefreshRequest(QJsonArray request_params);
SetUIOptionsRequest deserializeSetUIOptionsRequest(QJsonArray request_params);

QByteArray serializeRequest(const QString &method_name, QJsonArray params);
} // namespace RPC
Expand Down
8 changes: 7 additions & 1 deletion src/statusbar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
StatusBar::StatusBar(DrawOptions *draw_options, QWidget *parent)
{
m_draw_options = draw_options;
setFixedHeight(m_draw_options->getCellSize().height());
connect(draw_options, &DrawOptions::updated, this, &StatusBar::drawOptionsUpdated);
drawOptionsUpdated();
}

StatusBar::~StatusBar()
Expand Down Expand Up @@ -36,3 +37,8 @@ void StatusBar::paintEvent(QPaintEvent *ev)

m_client->getStatusLine().draw(context, QPoint(), status_default_face);
}

void StatusBar::drawOptionsUpdated()
{
setFixedHeight(m_draw_options->getCellSize().height());
}
3 changes: 3 additions & 0 deletions src/statusbar.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ class StatusBar : public QWidget
protected:
void paintEvent(QPaintEvent *ev) override;

protected slots:
void drawOptionsUpdated();

private:
KakouneClient *m_client;
DrawOptions *m_draw_options;
Expand Down
Loading