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

Add a procedural macro for defining layouts #54

Merged
merged 4 commits into from
Apr 28, 2021
Merged

Conversation

Skelebot
Copy link
Contributor

@Skelebot Skelebot commented Apr 21, 2021

Add a procedural macro to generate keyberon::layout::Layers

Possible future extensions

  • Compiler warnings when rows have inconsistent number of keycodes / layers have inconsistent number of rows
  • Additional layout setup like automatic impl_heterogeneous_map (solving Clean impl_heterogenous_map #16)

Issues

  • Does not differentiate between Action::MultipleKeyCodes and Action::MultipleActions, so the macro always uses MultipleActions which can increase binary size slightly (possible improvement in the future)
  • Commas can accidentaly be used and parsed as a key (possibly solved by compiler warnings on inconsistent number of keycodes in rows)

Syntax

Items inside the macro are converted to Actions as such:

  • Action::KeyCode: Idents are automatically understood as keycodes: A, RCtrl, Space
    • Punctuation, numbers and other literals that aren't special to the rust parser are converted
      to KeyCodes as well: , becomes KeyCode::Commma, 2 becomes KeyCode::Kb2, / becomes KeyCode::Slash
    • Characters which require shifted keys are converted to Action::MultipleKeyCodes(&[LShift, <character>]):
      ! becomes Action::MultipleKeyCodes(&[LShift, Kb1]) etc
    • Characters special to the rust parser (parentheses, brackets, braces, quotes, apostrophes, underscores, backslashes and backticks)
      left alone cause parsing errors and as such have to be enclosed by apostrophes: '[' becomes KeyCode::LBracket,
      '\'' becomes KeyCode::Quote, '\\' becomes KeyCode::BSlash
  • Action::NoOp: Lowercase n
  • Action::Trans: Lowercase t
  • Action::Layer: A number in parentheses: (1), (4 - 2), (0x4u8 as usize)
  • Action::MultipleActions: Actions in brackets: [LCtrl S], [LAlt LCtrl C], [(2) B {Action::NoOp}]
  • Other Actions: anything in braces ({}) is copied unchanged to the final layout - { Action::Custom(42) }
    simply becomes Action::Custom(42)

Important note: comma (,) is a keycode on its own, and can't be used to separate keycodes as one would have
to do when not using a macro.

Usage example:

Example layout for a 4x12 split keyboard:

use keyberon::action::Action;
static DLAYER: Action = Action::DefaultLayer(5);

pub static LAYERS: keyberon::layout::Layers = keyberon::layout::layout! {
    {
        [ Tab    Q W E R T   Y U I O P BSpace ]
        [ LCtrl  A S D F G   H J K L ; Quote  ]
        [ LShift Z X C V B   N M , . / Escape ]
        [ n n LGui {DLAYER} Space Escape   BSpace Enter (1) RAlt n n ]
    }
    {
        [ Tab    1 2 3 4 5   6 7 8 9 0 BSpace  ]
        [ LCtrl  ! @ # $ %   ^ & * '(' ')' - = ]
        [ LShift n n n n n   n n n n n [LAlt A]]
        [ n n LGui (2) t t   t t t RAlt n n    ]
    }
    // ...
};

@theotherjimmy
Copy link

Looks, and works, great!

Copy link
Owner

@TeXitoi TeXitoi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

You also need to add an entry in the changelog

layout-macro/src/lib.rs Outdated Show resolved Hide resolved
Cargo.toml Outdated Show resolved Hide resolved
layout-macro/Cargo.toml Outdated Show resolved Hide resolved
@TeXitoi TeXitoi merged commit 3361917 into TeXitoi:master Apr 28, 2021
@TeXitoi
Copy link
Owner

TeXitoi commented Apr 28, 2021

Thanks

@gilescope
Copy link

slick!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants