Repaint + some basic input

This commit is contained in:
daa84 2016-03-31 16:52:22 +03:00
parent 62eeeec201
commit 8d2c22e571
5 changed files with 40 additions and 4 deletions

1
Cargo.lock generated
View File

@ -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",

View File

@ -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"

View File

@ -1,4 +1,5 @@
extern crate gtk;
extern crate gdk;
extern crate glib;
extern crate cairo;
extern crate neovim_lib;

View File

@ -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<Value>) {
println!("Unsupported event type {:?}", ev);
}
}
safe_call(move |ui| {
ui.on_redraw();
Ok(())
});
} else {
println!("Notification {}", method);
}

View File

@ -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();
}
}