neovim-gtk/src/ui.rs

99 lines
2.6 KiB
Rust
Raw Normal View History

2016-03-16 15:25:25 +00:00
use cairo;
use gtk;
use gtk::prelude::*;
2016-03-17 13:58:21 +00:00
use gtk::{Window, WindowType, DrawingArea, Grid, ToolButton, ButtonBox, Orientation, Image};
use neovim_lib::Neovim;
2016-03-16 15:25:25 +00:00
2016-03-19 08:47:23 +00:00
use ui_model::UiModel;
2016-03-23 11:59:18 +00:00
use nvim::RedrawEvents;
2016-03-19 08:47:23 +00:00
2016-03-19 10:27:39 +00:00
pub struct Ui {
2016-03-28 15:05:10 +00:00
pub model: UiModel,
nvim: Option<Neovim>,
2016-03-19 10:27:39 +00:00
}
2016-03-16 15:25:25 +00:00
impl Ui {
pub fn new() -> Ui {
2016-03-19 10:27:39 +00:00
Ui {
model: UiModel::empty(),
nvim: None,
2016-03-19 10:27:39 +00:00
}
2016-03-16 15:25:25 +00:00
}
pub fn set_nvim(&mut self, nvim: Neovim) {
self.nvim = Some(nvim);
}
pub fn nvim(&mut self) -> &mut Neovim {
self.nvim.as_mut().unwrap()
}
2016-03-19 10:27:39 +00:00
pub fn show(&self) {
2016-03-16 15:25:25 +00:00
gtk::init().expect("Failed to initialize GTK");
let window = Window::new(WindowType::Toplevel);
let grid = Grid::new();
let button_bar = ButtonBox::new(Orientation::Horizontal);
2016-03-17 13:58:21 +00:00
button_bar.set_hexpand(true);
button_bar.set_layout(gtk::ButtonBoxStyle::Start);
let open_image = Image::new_from_icon_name("document-open", 50);
let open_btn = ToolButton::new(Some(&open_image), None);
button_bar.add(&open_btn);
let save_image = Image::new_from_icon_name("document-save", 50);
let save_btn = ToolButton::new(Some(&save_image), None);
button_bar.add(&save_btn);
let exit_image = Image::new_from_icon_name("application-exit", 50);
let exit_btn = ToolButton::new(Some(&exit_image), None);
button_bar.add(&exit_btn);
2016-03-16 15:25:25 +00:00
grid.attach(&button_bar, 0, 0, 1, 1);
let drawing_area = DrawingArea::new();
drawing_area.set_size_request(500, 500);
drawing_area.connect_draw(Self::gtk_draw);
grid.attach(&drawing_area, 0, 1, 1, 1);
window.add(&grid);
window.show_all();
window.connect_delete_event(|_,_| {
gtk::main_quit();
Inhibit(false)
});
}
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;
ctx.set_source_rgb(1.0, 0.0, 0.0);
ctx.arc(width / 2.0, height / 2.0,
width / 2.0,
0.0, 2.0 * 3.14);
ctx.fill();
Inhibit(true)
}
}
2016-03-23 11:59:18 +00:00
impl RedrawEvents for Ui {
2016-03-28 14:03:21 +00:00
fn on_cursor_goto(&mut self, row: u64, col: u64) {
self.model.set_cursor(row, col);
}
2016-03-28 15:05:10 +00:00
fn on_put(&mut self, text: &str) {
self.model.put(text);
}
2016-03-29 09:22:16 +00:00
fn on_clear(&mut self) {
self.model.clear();
}
2016-03-29 09:43:01 +00:00
fn on_resize(&mut self, columns: u64, rows: u64) {
self.model = UiModel::new(rows, columns);
}
2016-03-23 11:59:18 +00:00
}