Skip to content

Commit

Permalink
Make editor font size changeable.
Browse files Browse the repository at this point in the history
  • Loading branch information
Bzero committed Aug 11, 2024
1 parent 7bf1e41 commit c3207f6
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 9 deletions.
2 changes: 2 additions & 0 deletions docs/example_config.ini
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ name = typst
mode = live

[Editor]
# Set the font size
font_size = 12
# Save all open tabs before compiling
save_at_run = True
# The code highlighter style to use. See https://pygments.org/styles/ for available options
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ indent-width = 4

[tool.ruff.lint]
select = ["E", "F", "W", "N", "D", "Q", "PL", "R", "C", "B", "C4", "G", "PT", "RET", "RUF", "SIM", "UP"]
ignore = ["D212", "D203", "D400", "RET505", "D100", "PLR0915", "RET504", "PLE1205", "UP015", "PLC2401"]
ignore = ["D212", "D203", "D400", "RET505", "D100", "PLR0915", "RET504", "PLE1205", "UP015", "PLC2401", "PLR0913"]

[tool.ruff.format]
quote-style = "double"
Expand Down
12 changes: 12 additions & 0 deletions typstwriter/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,18 @@ def __init__(self, parent):
self.search.setShortcut(QtGui.QKeySequence.Find)
self.search.setText("Search")

self.font_size_up = QtWidgets.QAction(self)
self.font_size_up.setIcon(QtGui.QIcon.fromTheme(QtGui.QIcon.ZoomIn, QtGui.QIcon(util.icon_path("plus.svg"))))
self.font_size_up.setText("Increase Font Size")

self.font_size_dn = QtWidgets.QAction(self)
self.font_size_dn.setIcon(QtGui.QIcon.fromTheme(QtGui.QIcon.ZoomOut, QtGui.QIcon(util.icon_path("minus.svg"))))
self.font_size_dn.setText("Decrease Font Size")

self.font_size_reset = QtWidgets.QAction(self)
self.font_size_reset.setIcon(QtGui.QIcon.fromTheme(QtGui.QIcon.ZoomFitBest))
self.font_size_reset.setText("Reset Font Size")

self.layout_typewriter = QtWidgets.QAction(self)
self.layout_typewriter.setIcon(QtGui.QIcon.fromTheme("view-split-top-bottom-symbolic"))
self.layout_typewriter.setText("Layout: Typewriter")
Expand Down
3 changes: 2 additions & 1 deletion typstwriter/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"default_layout": "typewriter"},
"Compiler": {"name": "typst",
"mode": "on_demand"},
"Editor": {"save_at_run": False,
"Editor": {"font_size": 10,
"save_at_run": False,
"highlighter_style": "sas", # Can be any style from https://pygments.org/styles/
"highlight_syntax": True,
"show_line_numbers": True,
Expand Down
49 changes: 44 additions & 5 deletions typstwriter/editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ def __init__(self):

self.recentFiles = util.RecentFiles()

self.font_size = config.get("Editor", "font_size", typ="int")

self.welcome()

def tab_bar_rightclicked(self, event):
Expand All @@ -68,7 +70,7 @@ def tab_bar_rightclicked(self, event):

def new_file(self):
"""Open a new, empty file."""
editorpage = EditorPage()
editorpage = EditorPage(font_size=self.font_size)
name = "*new*"
icon = QtGui.QIcon.fromTheme(QtGui.QIcon.DocumentNew, QtGui.QIcon(util.icon_path("newFile.svg")))
self.TabWidget.addTab(editorpage, icon, name)
Expand All @@ -82,7 +84,7 @@ def new_file(self):
def open_file(self, path):
"""Open an existing file or switch to it if it is already open."""
if path not in self.openfiles_list():
editorpage = EditorPage(path)
editorpage = EditorPage(path, self.font_size)
name = os.path.relpath(path, start=state.working_directory.Value)
icon = util.FileIconProvider().icon(QtCore.QFileInfo(path))
self.TabWidget.addTab(editorpage, icon, name)
Expand Down Expand Up @@ -251,6 +253,30 @@ def clear_errors(self):
if isinstance(t, EditorPage):
t.edit.clear_errors()

@QtCore.Slot()
def increase_font_size(self):
"""Increase the font size."""
self.font_size = min(40, self.font_size * 1.25)
self.set_font_size()

@QtCore.Slot()
def decrease_font_size(self):
"""Decrease the font size."""
self.font_size = max(4, self.font_size * 0.8)
self.set_font_size()

@QtCore.Slot()
def reset_font_size(self):
"""Reset the font size to the config default."""
self.font_size = config.get("Editor", "font_size", typ="int")
self.set_font_size()

def set_font_size(self):
"""Apply font size to all editor tabs."""
for t in self.tabs_list():
if isinstance(t, EditorPage):
t.edit.set_font_size(self.font_size)


class EditorPageBarContextMenu(QtWidgets.QMenu):
"""ContextMenu for EditorPage tabbar."""
Expand Down Expand Up @@ -307,7 +333,7 @@ class EditorPage(QtWidgets.QFrame):
savestatechanged = QtCore.Signal(bool)
pathchanged = QtCore.Signal(str)

def __init__(self, path=None):
def __init__(self, path=None, font_size=None):
"""Set up and load file if path is given."""
QtWidgets.QFrame.__init__(self)
self.setFrameStyle(QtWidgets.QFrame.Shape.StyledPanel)
Expand All @@ -322,7 +348,11 @@ def __init__(self, path=None):
use_spaces = config.get("Editor", "use_spaces", "bool")

self.edit = CodeEdit(
highlight_synatx=syntax_conf, show_line_numbers=line_numbers_conf, highlight_line=line_conf, use_spaces=use_spaces
font_size=font_size,
highlight_synatx=syntax_conf,
show_line_numbers=line_numbers_conf,
highlight_line=line_conf,
use_spaces=use_spaces,
)
self.edit.textChanged.connect(self.modified)
self.verticalLayout.addWidget(self.edit)
Expand Down Expand Up @@ -720,7 +750,7 @@ def tryclose(self):
class CodeEdit(QtWidgets.QPlainTextEdit):
"""A code editor widget."""

def __init__(self, highlight_synatx=True, show_line_numbers=True, highlight_line=True, use_spaces=True):
def __init__(self, font_size=None, highlight_synatx=True, show_line_numbers=True, highlight_line=True, use_spaces=True):
"""Init and set options."""
super().__init__()

Expand All @@ -734,6 +764,9 @@ def __init__(self, highlight_synatx=True, show_line_numbers=True, highlight_line
self.error_highlight = []
self.search_highlights = []

if font_size:
self.set_font_size(font_size)

if not highlight_synatx:
self.highlighter.setDocument(None)
palette.setColor(QtGui.QPalette.Text, QtGui.QColor(self.highlighter.formatter.style.styles[pygments.token.Token]))
Expand Down Expand Up @@ -787,6 +820,12 @@ def keyPressEvent(self, e): # noqa: N802 This is an overriding function

super().keyPressEvent(e)

def set_font_size(self, font_size):
"""Set the font size."""
font = self.font()
font.setPointSize(font_size)
self.setFont(font)

def apply_extra_selections(self):
"""Apply line, error and search highlight extra selections."""
self.setExtraSelections(self.line_highlight + self.error_highlight + self.search_highlights)
Expand Down
3 changes: 3 additions & 0 deletions typstwriter/mainwindow.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ def __init__(self):
self.actions.layout_typewriter.triggered.connect(self.set_layout_typewriter)
self.actions.layout_editorL.triggered.connect(self.set_layout_editorL)
self.actions.layout_editorR.triggered.connect(self.set_layout_editorR)
self.actions.font_size_up.triggered.connect(self.editor.increase_font_size)
self.actions.font_size_dn.triggered.connect(self.editor.decrease_font_size)
self.actions.font_size_reset.triggered.connect(self.editor.reset_font_size)
self.actions.show_fs_explorer.triggered.connect(self.set_fs_explorer_visibility)
self.actions.show_compiler_options.triggered.connect(self.set_compiler_options_visibility)
self.actions.show_compiler_output.triggered.connect(self.set_compiler_output_visibility)
Expand Down
8 changes: 8 additions & 0 deletions typstwriter/menubar.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,19 @@ def __init__(self, actions):
self.layout_menu.setTitle("Layout")
self.layout_menu.addActions(actions.layout.actions())

self.editor_zoom_menu = QtWidgets.QMenu(self)
self.editor_zoom_menu.setTitle("Editor Zoom")
self.editor_zoom_menu.addAction(actions.font_size_up)
self.editor_zoom_menu.addAction(actions.font_size_dn)
self.editor_zoom_menu.addAction(actions.font_size_reset)

self.menuView.addMenu(self.layout_menu)
self.menuView.addSeparator()
self.menuView.addAction(actions.show_fs_explorer)
self.menuView.addAction(actions.show_compiler_options)
self.menuView.addAction(actions.show_compiler_output)
self.menuView.addSeparator()
self.menuView.addMenu(self.editor_zoom_menu)

self.menuSettings.addAction(actions.open_config)

Expand Down
4 changes: 2 additions & 2 deletions typstwriter/pdf_viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,13 @@ def __init__(self):
self.actionZoom_In = QtGui.QAction(self)
self.actionZoom_In.setIcon(QtGui.QIcon.fromTheme(QtGui.QIcon.ZoomIn, QtGui.QIcon(util.icon_path("plus.svg"))))
self.actionZoom_In.setText("Zoom In")
self.actionZoom_In.setShortcut("Ctrl++")
self.actionZoom_In.setShortcut(QtGui.QKeySequence.ZoomIn)

# Action Zoom Out
self.actionZoom_Out = QtGui.QAction(self)
self.actionZoom_Out.setIcon(QtGui.QIcon.fromTheme(QtGui.QIcon.ZoomOut, QtGui.QIcon(util.icon_path("minus.svg"))))
self.actionZoom_Out.setText("Zoom Out")
self.actionZoom_Out.setShortcut("Ctrl+-")
self.actionZoom_Out.setShortcut(QtGui.QKeySequence.ZoomOut)

# Action Previous Page
self.actionPrevious_Page = QtGui.QAction(self)
Expand Down

0 comments on commit c3207f6

Please sign in to comment.