fix keypress
This commit is contained in:
parent
a19c28473e
commit
6c80fd3e8c
26
build.rs
26
build.rs
@ -15,33 +15,7 @@ fn main() {
|
||||
|
||||
write!(&mut file, "static KEYVAL_MAP: phf::Map<&'static str, &'static str> = ").unwrap();
|
||||
phf_codegen::Map::new()
|
||||
.entry("slash", "\"/\"")
|
||||
.entry("backslash", "\"\\\\\"")
|
||||
.entry("dead_circumflex", "\"^\"")
|
||||
.entry("at", "\"@\"")
|
||||
.entry("numbersign", "\"#\"")
|
||||
.entry("dollar", "\"$\"")
|
||||
.entry("percent", "\"%\"")
|
||||
.entry("ampersand", "\"&\"")
|
||||
.entry("asterisk", "\"*\"")
|
||||
.entry("parenleft", "\"(\"")
|
||||
.entry("parenright", "\")\"")
|
||||
.entry("underscore", "\"_\"")
|
||||
.entry("plus", "\"+\"")
|
||||
.entry("minus", "\"-\"")
|
||||
.entry("bracketleft", "\"[\"")
|
||||
.entry("bracketright", "\"]\"")
|
||||
.entry("braceleft", "\"{\"")
|
||||
.entry("braceright", "\"}\"")
|
||||
.entry("dead_diaeresis", "\"\\\"\"")
|
||||
.entry("dead_acute", "\"'\"")
|
||||
.entry("less", "\"<\"")
|
||||
.entry("greater", "\">\"")
|
||||
.entry("comma", "\",\"")
|
||||
.entry("colon", "\":\"")
|
||||
.entry("period", "\".\"")
|
||||
.entry("BackSpace", "\"BS\"")
|
||||
.entry("space", "\"space\"")
|
||||
.entry("Return", "\"CR\"")
|
||||
.entry("Escape", "\"Esc\"")
|
||||
.entry("Delete", "\"Del\"")
|
||||
|
42
src/input.rs
Normal file
42
src/input.rs
Normal file
@ -0,0 +1,42 @@
|
||||
|
||||
use gdk;
|
||||
use gdk::EventKey;
|
||||
use phf;
|
||||
|
||||
include!(concat!(env!("OUT_DIR"), "/key_map_table.rs"));
|
||||
|
||||
|
||||
fn keyval_to_input_string(val: &str, state: gdk::ModifierType) -> String {
|
||||
let mut input = String::from("<");
|
||||
if state.contains(gdk::enums::modifier_type::ShiftMask) {
|
||||
input.push_str("S-");
|
||||
}
|
||||
if state.contains(gdk::enums::modifier_type::ControlMask) {
|
||||
input.push_str("C-");
|
||||
}
|
||||
if state.contains(gdk::enums::modifier_type::Mod1Mask) {
|
||||
input.push_str("A-");
|
||||
}
|
||||
input.push_str(val);
|
||||
input.push_str(">");
|
||||
input
|
||||
}
|
||||
|
||||
pub fn convert_key(ev: &EventKey) -> Option<String> {
|
||||
let keyval = ev.get_keyval();
|
||||
let state = ev.get_state();
|
||||
if let Some(ref keyval_name) = gdk::keyval_name(keyval) {
|
||||
if let Some(cnvt) = KEYVAL_MAP.get(keyval_name as &str).cloned() {
|
||||
return Some(keyval_to_input_string(cnvt, state));
|
||||
}
|
||||
}
|
||||
if let Some(ch) = gdk::keyval_to_unicode(keyval) {
|
||||
return Some(if !state.is_empty() {
|
||||
keyval_to_input_string(&ch.to_string(), state)
|
||||
} else {
|
||||
ch.to_string()
|
||||
});
|
||||
}
|
||||
|
||||
None
|
||||
}
|
@ -9,6 +9,7 @@ extern crate phf;
|
||||
mod nvim;
|
||||
mod ui_model;
|
||||
mod ui;
|
||||
mod input;
|
||||
|
||||
fn main() {
|
||||
gtk::init().expect("Failed to initialize GTK");
|
||||
|
68
src/ui.rs
68
src/ui.rs
@ -13,14 +13,18 @@ use cairo::enums::{FontWeight, FontSlant};
|
||||
use gtk;
|
||||
use gtk::prelude::*;
|
||||
use gtk::{Window, WindowType, DrawingArea, Grid, ToolButton, ButtonBox, Orientation, Image};
|
||||
use gdk;
|
||||
use gdk::EventKey;
|
||||
use neovim_lib::{Neovim, NeovimApi};
|
||||
|
||||
use ui_model::{UiModel, Attrs, Color};
|
||||
use nvim::RedrawEvents;
|
||||
|
||||
use input::convert_key;
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
const FONT_NAME: &'static str = "Droid Sans Mono for Powerline";
|
||||
#[cfg(target_os = "windows")]
|
||||
const FONT_NAME: &'static str = "Liberation_Mono:h12:cANSI";
|
||||
const FONT_SIZE: f64 = 16.0;
|
||||
|
||||
thread_local!(pub static UI: RefCell<Ui> = {
|
||||
@ -98,60 +102,12 @@ impl Ui {
|
||||
}
|
||||
}
|
||||
|
||||
use phf;
|
||||
include!(concat!(env!("OUT_DIR"), "/key_map_table.rs"));
|
||||
|
||||
|
||||
fn keyval_to_input_string(val: &str, state: gdk::ModifierType) -> String {
|
||||
let mut input = String::from("<");
|
||||
if state.contains(gdk::enums::modifier_type::ShiftMask) {
|
||||
input.push_str("S-");
|
||||
}
|
||||
if state.contains(gdk::enums::modifier_type::ControlMask) {
|
||||
input.push_str("C-");
|
||||
}
|
||||
if state.contains(gdk::enums::modifier_type::Mod1Mask) {
|
||||
input.push_str("A-");
|
||||
}
|
||||
input.push_str(val);
|
||||
input.push_str(">");
|
||||
input
|
||||
}
|
||||
|
||||
fn convert_keyval(input: &str, state: gdk::ModifierType) -> String {
|
||||
if let Some(cnvt) = KEYVAL_MAP.get(input).cloned() {
|
||||
keyval_to_input_string(cnvt, state)
|
||||
} else {
|
||||
if state.is_empty() {
|
||||
input.to_string()
|
||||
} else {
|
||||
keyval_to_input_string(input, state)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn is_keyval_ignore(keyval_name: &String) -> bool {
|
||||
keyval_name.contains("Shift") || keyval_name.contains("Alt") || keyval_name.contains("Control")
|
||||
}
|
||||
|
||||
fn gtk_key_press(_: &Window, ev: &EventKey) -> Inhibit {
|
||||
let keyval = ev.get_keyval();
|
||||
let state = ev.get_state();
|
||||
if let Some(keyval_name) = gdk::keyval_name(keyval) {
|
||||
if !is_keyval_ignore(&keyval_name) {
|
||||
UI.with(|ui_cell| {
|
||||
let mut ui = ui_cell.borrow_mut();
|
||||
let input = if keyval_name.starts_with("KP_") {
|
||||
keyval_name.chars().skip(3).collect()
|
||||
} else {
|
||||
keyval_name
|
||||
};
|
||||
|
||||
let converted_input = convert_keyval(&input, state);
|
||||
println!("{}", converted_input);
|
||||
ui.nvim().input(&converted_input).expect("Error run input command to nvim");
|
||||
});
|
||||
}
|
||||
if let Some(input) = convert_key(ev) {
|
||||
UI.with(|ui_cell| {
|
||||
let mut ui = ui_cell.borrow_mut();
|
||||
ui.nvim().input(&input).expect("Error run input command to nvim");
|
||||
});
|
||||
}
|
||||
Inhibit(true)
|
||||
}
|
||||
@ -213,11 +169,11 @@ fn gtk_draw(drawing_area: &DrawingArea, ctx: &cairo::Context) -> Inhibit {
|
||||
}
|
||||
|
||||
request_width(&drawing_area, &ui, font_extents.height, char_bounds.width);
|
||||
|
||||
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
Inhibit(true)
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user