From 8d2c22e5714ae4230e89f82bf408403dcd8faca9 Mon Sep 17 00:00:00 2001 From: daa84 Date: Thu, 31 Mar 2016 16:52:22 +0300 Subject: [PATCH] Repaint + some basic input --- Cargo.lock | 1 + Cargo.toml | 1 + src/main.rs | 1 + src/nvim.rs | 7 +++++++ src/ui.rs | 34 ++++++++++++++++++++++++++++++---- 5 files changed, 40 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 38fd74d..9f85172 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3,6 +3,7 @@ name = "neovim-gtk" version = "0.1.0" dependencies = [ "cairo-rs 0.0.8 (registry+https://github.com/rust-lang/crates.io-index)", + "gdk 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "glib 0.0.8 (registry+https://github.com/rust-lang/crates.io-index)", "gtk 0.0.7 (registry+https://github.com/rust-lang/crates.io-index)", "neovim-lib 0.1.0", diff --git a/Cargo.toml b/Cargo.toml index 2183d13..ae44453 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,6 +7,7 @@ build = "build.rs" [dependencies] cairo-rs = "0.0.8" glib = "0.0.8" +gdk = "0.3.0" #neovim-lib = "0.1" rmp = "0.7" diff --git a/src/main.rs b/src/main.rs index 78b0059..9a630ef 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,5 @@ extern crate gtk; +extern crate gdk; extern crate glib; extern crate cairo; extern crate neovim_lib; diff --git a/src/nvim.rs b/src/nvim.rs index 5b3f68f..add19aa 100644 --- a/src/nvim.rs +++ b/src/nvim.rs @@ -16,6 +16,8 @@ pub trait RedrawEvents { fn on_clear(&mut self); fn on_resize(&mut self, columns: u64, rows: u64); + + fn on_redraw(&self); } macro_rules! try_str { @@ -72,6 +74,11 @@ fn nvim_cb(method: &str, params: Vec) { println!("Unsupported event type {:?}", ev); } } + + safe_call(move |ui| { + ui.on_redraw(); + Ok(()) + }); } else { println!("Notification {}", method); } diff --git a/src/ui.rs b/src/ui.rs index 526eb21..eb71e98 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -5,7 +5,9 @@ use cairo; use gtk; use gtk::prelude::*; use gtk::{Window, WindowType, DrawingArea, Grid, ToolButton, ButtonBox, Orientation, Image}; -use neovim_lib::Neovim; +use gdk; +use gdk::EventKey; +use neovim_lib::{Neovim, NeovimApi}; use ui_model::UiModel; use nvim::RedrawEvents; @@ -74,6 +76,7 @@ impl Ui { window.add(&grid); window.show_all(); + window.connect_key_press_event(gtk_key_press); window.connect_delete_event(|_, _| { gtk::main_quit(); Inhibit(false) @@ -81,6 +84,22 @@ impl Ui { } } +fn gtk_key_press(_: &Window, ev: &EventKey) -> Inhibit { + let keyval = ev.get_keyval(); + if let Some(keyval_name) = gdk::keyval_name(keyval) { + 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 + }; + ui.nvim().input(&input).expect("Error run input command to nvim"); + }); + } + Inhibit(true) +} + fn gtk_draw(drawing_area: &DrawingArea, ctx: &cairo::Context) -> Inhibit { let width = drawing_area.get_allocated_width() as f64; let height = drawing_area.get_allocated_height() as f64; @@ -89,14 +108,17 @@ fn gtk_draw(drawing_area: &DrawingArea, ctx: &cairo::Context) -> Inhibit { cairo::enums::FontSlant::Normal, cairo::enums::FontWeight::Normal); ctx.set_font_face(font_face); + + let font_extents = ctx.font_extents(); UI.with(|ui_cell| { - ctx.set_source_rgb(0.0, 0.0, 0.0); let ui = ui_cell.borrow(); - let mut line_y = 30.0; + + ctx.set_source_rgb(0.0, 0.0, 0.0); + let mut line_y = font_extents.height; for line in ui.model.lines() { ctx.move_to(0.0, line_y); ctx.show_text(&line); - line_y += 30.0; + line_y += font_extents.height; } }); @@ -119,4 +141,8 @@ impl RedrawEvents for Ui { fn on_resize(&mut self, columns: u64, rows: u64) { self.model = UiModel::new(rows, columns); } + + fn on_redraw(&self) { + self.drawing_area.queue_draw(); + } }