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

feat: make layout-independent shortcuts #147

Merged
merged 1 commit into from
Jan 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 59 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ glib-macros = "0.20.7"
glib = "0.20.7"
resource = "0.5.0" # font emedding
fontconfig = "0.9.0" # font loading
keycode = "0.4.0"

[dependencies.relm4-icons]
version = "0.9.0"
Expand Down
32 changes: 27 additions & 5 deletions src/sketch_board.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use femtovg::imgref::Img;
use femtovg::rgb::{ComponentBytes, RGBA};
use gdk_pixbuf::glib::Bytes;
use gdk_pixbuf::Pixbuf;
use keycode::{KeyMap, KeyMappingId};
use std::cell::RefCell;
use std::fs;
use std::io::Write;
Expand Down Expand Up @@ -426,16 +427,26 @@ impl Component for SketchBoard {
let result = match msg {
SketchBoardInput::InputEvent(mut ie) => {
if let InputEvent::Key(ke) = ie {
if ke.key == Key::z && ke.modifier == ModifierType::CONTROL_MASK {
if ke.is_one_of(Key::z, KeyMappingId::UsZ)
&& ke.modifier == ModifierType::CONTROL_MASK
{
self.handle_undo()
} else if ke.key == Key::y && ke.modifier == ModifierType::CONTROL_MASK {
} else if ke.is_one_of(Key::y, KeyMappingId::UsY)
&& ke.modifier == ModifierType::CONTROL_MASK
{
self.handle_redo()
} else if ke.key == Key::t && ke.modifier == ModifierType::CONTROL_MASK {
} else if ke.is_one_of(Key::t, KeyMappingId::UsT)
&& ke.modifier == ModifierType::CONTROL_MASK
{
self.handle_toggle_toolbars_display(sender)
} else if ke.key == Key::s && ke.modifier == ModifierType::CONTROL_MASK {
} else if ke.is_one_of(Key::s, KeyMappingId::UsS)
&& ke.modifier == ModifierType::CONTROL_MASK
{
self.renderer.request_render(Action::SaveToFile);
ToolUpdateResult::Unmodified
} else if ke.key == Key::c && ke.modifier == ModifierType::CONTROL_MASK {
} else if ke.is_one_of(Key::c, KeyMappingId::UsC)
&& ke.modifier == ModifierType::CONTROL_MASK
{
self.renderer.request_render(Action::SaveToClipboard);
ToolUpdateResult::Unmodified
} else if ke.key == Key::Escape {
Expand Down Expand Up @@ -523,4 +534,15 @@ impl KeyEventMsg {
modifier,
}
}

/// Matches one of providen keys. The modifier is not considered.
/// And the key has more priority over keycode.
fn is_one_of(&self, key: Key, code: KeyMappingId) -> bool {
// INFO: on linux the keycode from gtk4 is evdev keycode, so need to match by him if need
// to use layout-independent shortcuts. And notice that there is substraction by 8, it's
// because of x11 compatibility in which the keycodes are in range [8,255]. So need shift
// them to get correct evdev keycode.
let keymap = KeyMap::from(code);
self.key == key || self.code as u16 - 8 == keymap.evdev
}
}
Loading