Skip to content

Commit

Permalink
Allow zooming the font with keyboard (ctrl+-0) #9
Browse files Browse the repository at this point in the history
  • Loading branch information
sakhnik committed Oct 18, 2021
1 parent 4490fc2 commit 93dee2f
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 24 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Switched to gtk4 to get rid of texture noise. Chunks of texts are rendered as gtk labels.
- Capture and display service output like --help or --version in the GUI window
- TCP session that can be engaged by recompiling currently
- Font zoom using keyboard shortcuts `^+`, `^-`, `^0`

### Fixed

Expand Down
31 changes: 25 additions & 6 deletions src/GGrid.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#include "GGrid.hpp"
#include "Logger.hpp"
#include "Renderer.hpp"
#include "IMenuBarToggler.hpp"
#include "IWindowHandler.hpp"
#include <sstream>

GGrid::GGrid(GtkWidget *grid, Session::PtrT &session, IMenuBarToggler *menu_bar_toggler)
GGrid::GGrid(GtkWidget *grid, Session::PtrT &session, IWindowHandler *window_handler)
: _grid{grid}
, _session{session}
, _menu_bar_toggler{menu_bar_toggler}
, _window_handler{window_handler}
{
gtk_widget_set_focusable(_grid, true);

Expand Down Expand Up @@ -86,7 +86,7 @@ void GGrid::UpdateStyle()

oss << "* {\n";
oss << "font-family: Fira Code;\n";
oss << "font-size: 14pt;\n";
oss << "font-size: " << _font_size_pt << "pt;\n";
mapAttr(renderer->GetDefAttr(), renderer->GetDefAttr());
oss << "}\n";

Expand All @@ -110,6 +110,7 @@ void GGrid::UpdateStyle()
gtk_css_provider_load_from_data(_css_provider.get(), style.data(), -1);

MeasureCell();
_window_handler->CheckSizeAsync();
}

void GGrid::Present(int width, int height)
Expand Down Expand Up @@ -201,11 +202,29 @@ gboolean GGrid::_OnKeyPressed(guint keyval, guint /*keycode*/, GdkModifierType s
return true;
}
_alt_pending = false;
_menu_bar_toggler->MenuBarHide();
_window_handler->MenuBarHide();

if (!_session)
return true;

if (0 != (GDK_CONTROL_MASK & state))
{
double font_size_pt = _font_size_pt;
if (keyval == GDK_KEY_equal)
font_size_pt *= 1.1;
else if (keyval == GDK_KEY_minus)
font_size_pt /= 1.1;
else if (keyval == GDK_KEY_0)
font_size_pt = 14;
if (font_size_pt != _font_size_pt)
{
auto guard = _session->GetRenderer()->Lock();
_font_size_pt = font_size_pt;
UpdateStyle();
return true;
}
}

gunichar uc = gdk_keyval_to_unicode(keyval);
auto input = MkPtr(g_string_append_unichar(g_string_new(nullptr), uc),
[](GString *s) { g_string_free(s, true); });
Expand Down Expand Up @@ -262,7 +281,7 @@ gboolean GGrid::_OnKeyReleased(guint keyval, guint /*keycode*/, GdkModifierType
{
if (_alt_pending && keyval == GDK_KEY_Alt_L)
{
_menu_bar_toggler->MenuBarToggle();
_window_handler->MenuBarToggle();
}
return true;
}
Expand Down
8 changes: 5 additions & 3 deletions src/GGrid.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
#include <functional>
#include <gtk/gtk.h>

struct IMenuBarToggler;
struct IWindowHandler;

class GGrid
{
public:
GGrid(GtkWidget *grid, Session::PtrT &session, IMenuBarToggler *);
GGrid(GtkWidget *grid, Session::PtrT &session, IWindowHandler *);

GtkStyleProvider* GetStyle() const
{
Expand Down Expand Up @@ -48,7 +48,7 @@ class GGrid
private:
GtkWidget *_grid;
Session::PtrT &_session;
IMenuBarToggler *_menu_bar_toggler;
IWindowHandler *_window_handler;
PtrT<GtkCssProvider> _css_provider = NullPtr<GtkCssProvider>([](auto *p) { g_object_unref(p); });

int _cell_width = 0;
Expand All @@ -65,4 +65,6 @@ class GGrid

int _last_rows = 0, _last_cols = 0;
void _CheckSize(int width, int height);

double _font_size_pt = 14;
};
8 changes: 4 additions & 4 deletions src/GWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ GWindow::GWindow(GtkApplication *app, Session::PtrT &session)
}

// Adjust the grid size to the actual window size
_CheckSizeAsync();
CheckSizeAsync();
}

GWindow::~GWindow()
Expand Down Expand Up @@ -83,7 +83,7 @@ void GWindow::_SetupWindowSignals()
{
using SizeChangedT = gboolean (*)(GObject *, GParamSpec *, gpointer data);
SizeChangedT sizeChanged = [](auto *, auto *, gpointer data) {
reinterpret_cast<GWindow *>(data)->_CheckSizeAsync();
reinterpret_cast<GWindow *>(data)->CheckSizeAsync();
return FALSE;
};

Expand All @@ -92,7 +92,7 @@ void GWindow::_SetupWindowSignals()

using OnShowT = void (*)(GtkWidget *, gpointer);
OnShowT onShow = [](auto *, gpointer data) {
reinterpret_cast<GWindow *>(data)->_CheckSizeAsync();
reinterpret_cast<GWindow *>(data)->CheckSizeAsync();
};
g_signal_connect(_window, "show", G_CALLBACK(onShow), this);

Expand Down Expand Up @@ -125,7 +125,7 @@ void GWindow::_SetupStatusLabel()
gtk_widget_set_visible(status_label, false);
}

void GWindow::_CheckSizeAsync()
void GWindow::CheckSizeAsync()
{
g_timeout_add(0, [](gpointer data) {
reinterpret_cast<GWindow *>(data)->_CheckSize();
Expand Down
6 changes: 3 additions & 3 deletions src/GWindow.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
#include "Utils.hpp"
#include "Session.hpp"
#include "GGrid.hpp"
#include "IMenuBarToggler.hpp"
#include "IWindowHandler.hpp"
#include <string>
#include <memory>
#include <gtk/gtk.h>


class GWindow
: public IWindow
, public IMenuBarToggler
, public IWindowHandler
{
public:
GWindow(GtkApplication *, Session::PtrT &session);
Expand All @@ -38,7 +38,7 @@ class GWindow
void _SetupWindowSignals();
void _SetupStatusLabel();

void _CheckSizeAsync();
void CheckSizeAsync() override;
void _CheckSize();

void _Present();
Expand Down
8 changes: 0 additions & 8 deletions src/IMenuBarToggler.hpp

This file was deleted.

9 changes: 9 additions & 0 deletions src/IWindowHandler.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once

struct IWindowHandler
{
virtual ~IWindowHandler() { }
virtual void MenuBarToggle() = 0;
virtual void MenuBarHide() = 0;
virtual void CheckSizeAsync() = 0;
};
1 change: 1 addition & 0 deletions src/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ nvim_ui_sources = [
'GPointer.hpp',
'GWindow.cpp',
'GWindow.hpp',
'IWindowHandler.hpp',
gtk_res,
win_res,
]
Expand Down

0 comments on commit 93dee2f

Please sign in to comment.