Skip to content

Commit

Permalink
fix alignment, combos and add new aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
caksoylar committed Nov 23, 2024
1 parent d406471 commit 237a664
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 38 deletions.
9 changes: 2 additions & 7 deletions keymap_drawer/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,6 @@ class KeySidePars(BaseModel):
legend_rel_x: float = 0
legend_rel_y: float = 0

# FIXME: better naming of the configuration and explanation to indicate that this refers to the fraction of the width where the right label will be set with respect to the center (defined by the tap)
legend_right_rel_w: float = 4

# draw key sides
draw_key_sides: bool = False

Expand Down Expand Up @@ -169,12 +166,10 @@ class KeySidePars(BaseModel):
text.left {
text-anchor: start;
dominant-baseline: auto;
}
text.right {
text-anchor: start;
dominant-baseline: auto;
text-anchor: end;
}
text.hold {
Expand All @@ -192,7 +187,7 @@ class KeySidePars(BaseModel):
}
/* styling for hold/shifted label text in combo box */
text.combo.hold, text.combo.shifted {
text.combo.hold, text.combo.shifted, text.combo.left, text.combo.right {
font-size: 8px;
}
Expand Down
25 changes: 12 additions & 13 deletions keymap_drawer/draw/combo.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,19 +141,6 @@ def print_combo(self, combo: ComboSpec, combo_ind: int) -> tuple[Point, Point]:
classes=["combo", combo.type, combo.key.type],
legend_type="tap",
)
# FIXME: combos were not test
self._draw_legend(
p - Point(self.cfg.combo_w / 2 - self.cfg.small_pad, 0),
[combo.key.left],
classes=["combo", combo.type, combo.key.type],
legend_type="left",
)
self._draw_legend(
p + Point(self.cfg.combo_w / self.cfg.legend_right_rel_w - self.cfg.inner_pad_w - self.cfg.small_pad, 0),
[combo.key.right],
classes=["combo", combo.type, combo.key.type],
legend_type="right",
)
self._draw_legend(
p + Point(0, self.cfg.combo_h / 2 - self.cfg.small_pad),
[combo.key.hold],
Expand All @@ -166,6 +153,18 @@ def print_combo(self, combo: ComboSpec, combo_ind: int) -> tuple[Point, Point]:
classes=["combo", combo.type, combo.key.type],
legend_type="shifted",
)
self._draw_legend(
p - Point(self.cfg.combo_w / 2 - self.cfg.small_pad, 0),
[combo.key.left],
classes=["combo", combo.type, combo.key.type],
legend_type="left",
)
self._draw_legend(
p + Point(self.cfg.combo_w / 2 - self.cfg.small_pad, 0),
[combo.key.right],
classes=["combo", combo.type, combo.key.type],
legend_type="right",
)
if combo.rotation != 0.0:
self.out.write("</g>\n")

Expand Down
24 changes: 12 additions & 12 deletions keymap_drawer/draw/draw.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,18 +94,6 @@ def print_key(self, p_key: PhysicalKey, l_key: LayoutKey, key_ind: int) -> None:
legend_type="tap",
shift=shift,
)
self._draw_legend(
tap_shift - Point(w / 2 - self.cfg.inner_pad_w - self.cfg.small_pad, 0),
[l_key.left],
classes=["key", l_key.type],
legend_type="left"
)
self._draw_legend(
tap_shift + Point(w / self.cfg.legend_right_rel_w - self.cfg.inner_pad_w - self.cfg.small_pad, 0),
[l_key.right],
classes=["key", l_key.type],
legend_type="right",
)
self._draw_legend(
Point(0, h / 2 - self.cfg.inner_pad_h - self.cfg.small_pad),
[l_key.hold],
Expand All @@ -118,6 +106,18 @@ def print_key(self, p_key: PhysicalKey, l_key: LayoutKey, key_ind: int) -> None:
classes=["key", l_key.type],
legend_type="shifted",
)
self._draw_legend(
tap_shift - Point(w / 2 - self.cfg.inner_pad_w - self.cfg.small_pad, 0),
[l_key.left],
classes=["key", l_key.type],
legend_type="left",
)
self._draw_legend(
tap_shift + Point(w / 2 - self.cfg.inner_pad_w - self.cfg.small_pad, 0),
[l_key.right],
classes=["key", l_key.type],
legend_type="right",
)

self.out.write("</g>\n")

Expand Down
6 changes: 5 additions & 1 deletion keymap_drawer/draw/glyph.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ def init_glyphs(self) -> None:
"""Preprocess all glyphs in the keymap to get their name to SVG mapping."""

def find_key_glyph_names(key: LayoutKey) -> set[str]:
return {glyph for field in (key.tap, key.hold, key.shifted, key.left, key.right) if (glyph := self._legend_to_name(field))}
return {
glyph
for field in (key.tap, key.hold, key.shifted, key.left, key.right)
if (glyph := self._legend_to_name(field))
}

# find all named glyphs in the keymap
names = set()
Expand Down
21 changes: 16 additions & 5 deletions keymap_drawer/keymap.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from itertools import chain
from typing import Callable, Iterable, Literal

from pydantic import BaseModel, Field, field_validator, model_serializer, model_validator
from pydantic import AliasChoices, BaseModel, Field, field_validator, model_serializer, model_validator

from keymap_drawer.config import Config
from keymap_drawer.physical_layout import PhysicalLayout, layout_factory
Expand All @@ -20,9 +20,9 @@ class LayoutKey(BaseModel, populate_by_name=True, coerce_numbers_to_str=True, ex
can optionally have hold or shifted properties, left or right labels, or be "held" or be a "ghost" key.
"""

tap: str = Field(alias="t", default="")
hold: str = Field(alias="h", default="")
shifted: str = Field(alias="s", default="")
tap: str = Field(validation_alias=AliasChoices("center", "t"), serialization_alias="t", default="")
hold: str = Field(validation_alias=AliasChoices("bottom", "h"), serialization_alias="h", default="")
shifted: str = Field(validation_alias=AliasChoices("shifted", "s"), serialization_alias="s", default="")
left: str = ""
right: str = ""

Expand All @@ -46,7 +46,18 @@ def from_key_spec(cls, key_spec: dict | str | int | None) -> "LayoutKey":
def serialize_model(self) -> str | dict[str, str]:
"""Custom serializer to output string-only for simple legends."""
if self.hold or self.shifted or self.left or self.right or self.type:
return {k: v for k, v in (("t", self.tap), ("h", self.hold), ("s", self.shifted), ("left", self.left), ("right", self.right), ("type", self.type)) if v}
return {
k: v
for k, v in (
("t", self.tap),
("h", self.hold),
("s", self.shifted),
("left", self.left),
("right", self.right),
("type", self.type),
)
if v
}
return self.tap

def full_serializer(self) -> dict[str, str]:
Expand Down

0 comments on commit 237a664

Please sign in to comment.