Skip to content

Commit

Permalink
Support left/right tap key labels
Browse files Browse the repository at this point in the history
- Still contains FIXME to be discussed on PR review
  • Loading branch information
magicDGS authored and caksoylar committed Nov 23, 2024
1 parent 5465ba7 commit d406471
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 7 deletions.
15 changes: 14 additions & 1 deletion keymap_drawer/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ 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 @@ -160,10 +163,20 @@ class KeySidePars(BaseModel):
}
/* styling for combo tap, and key hold/shifted label text */
text.combo, text.hold, text.shifted {
text.combo, text.hold, text.shifted, text.left, text.right {
font-size: 11px;
}
text.left {
text-anchor: start;
dominant-baseline: auto;
}
text.right {
text-anchor: start;
dominant-baseline: auto;
}
text.hold {
text-anchor: middle;
dominant-baseline: auto;
Expand Down
13 changes: 13 additions & 0 deletions keymap_drawer/draw/combo.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,19 @@ 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 Down
12 changes: 12 additions & 0 deletions keymap_drawer/draw/draw.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,18 @@ 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 Down
2 changes: 1 addition & 1 deletion keymap_drawer/draw/glyph.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ 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) 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
2 changes: 1 addition & 1 deletion keymap_drawer/draw/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from keymap_drawer.draw.glyph import GlyphMixin
from keymap_drawer.physical_layout import Point

LegendType = Literal["tap", "hold", "shifted"]
LegendType = Literal["tap", "hold", "shifted", "left", "right"]


class UtilsMixin(GlyphMixin):
Expand Down
15 changes: 11 additions & 4 deletions keymap_drawer/keymap.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,15 @@
class LayoutKey(BaseModel, populate_by_name=True, coerce_numbers_to_str=True, extra="forbid"):
"""
Represents a binding in the keymap, which has a tap property by default and
can optionally have hold or shifted properties, or be "held" or be a "ghost" key.
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="")
left: str = ""
right: str = ""

type: str = "" # pre-defined types: "held" | "ghost"

@classmethod
Expand All @@ -42,13 +45,13 @@ def from_key_spec(cls, key_spec: dict | str | int | None) -> "LayoutKey":
@model_serializer
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.type:
return {k: v for k, v in (("t", self.tap), ("h", self.hold), ("s", self.shifted), ("type", self.type)) if v}
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 self.tap

def full_serializer(self) -> dict[str, str]:
"""Custom serializer that always outputs a dict."""
return {k: v for k in ("tap", "hold", "shifted", "type") if (v := getattr(self, k))}
return {k: v for k in ("tap", "hold", "shifted", "left", "right", "type") if (v := getattr(self, k))}

def apply_formatter(self, formatter: Callable[[str], str]) -> None:
"""Add a formatter function (str -> str) to all non-empty fields."""
Expand All @@ -58,6 +61,10 @@ def apply_formatter(self, formatter: Callable[[str], str]) -> None:
self.hold = formatter(self.hold)
if self.shifted:
self.shifted = formatter(self.shifted)
if self.left:
self.left = formatter(self.left)
if self.right:
self.right = formatter(self.right)


class ComboSpec(BaseModel, populate_by_name=True, extra="forbid"):
Expand Down

0 comments on commit d406471

Please sign in to comment.