Make shell global variable due to borrow check conflict

This commit is contained in:
daa84 2017-03-31 13:22:05 +03:00
parent 0bd7356cd6
commit 5f523b8896
5 changed files with 89 additions and 83 deletions

View File

@ -1,6 +1,6 @@
use cairo; use cairo;
use ui_model::Color; use ui_model::Color;
use ui::{UI, UiMutex}; use ui::{SH, UiMutex};
use shell::{Shell, NvimMode}; use shell::{Shell, NvimMode};
use nvim::{RepaintMode, RedrawEvents}; use nvim::{RepaintMode, RedrawEvents};
use std::sync::Arc; use std::sync::Arc;

View File

@ -10,10 +10,10 @@ extern crate pangocairo;
extern crate neovim_lib; extern crate neovim_lib;
extern crate phf; extern crate phf;
mod nvim;
mod ui_model; mod ui_model;
#[macro_use] #[macro_use]
mod ui; mod ui;
mod nvim;
mod shell; mod shell;
mod input; mod input;
mod settings; mod settings;
@ -24,6 +24,7 @@ use std::env;
use gio::ApplicationExt; use gio::ApplicationExt;
use shell::Shell; use shell::Shell;
use ui::SH;
const BIN_PATH_ARG: &'static str = "--nvim-bin-path"; const BIN_PATH_ARG: &'static str = "--nvim-bin-path";
@ -45,17 +46,21 @@ fn main() {
fn activate(app: &gtk::Application) { fn activate(app: &gtk::Application) {
ui::UI.with(|ui_cell| { ui::UI.with(|ui_cell| {
let mut ui = ui_cell.borrow_mut(); let mut ui = ui_cell.borrow_mut();
if !ui.initialized { if !ui.initialized {
ui.init(app); ui.init(app);
let path = nvim_bin_path(std::env::args()); let path = nvim_bin_path(std::env::args());
nvim::initialize(&mut ui.shell, path.as_ref()) SHELL!(shell = {
.expect("Can't start nvim instance"); nvim::initialize(&mut shell, path.as_ref())
.expect("Can't start nvim instance");
guard_dispatch_thread(&mut ui.shell); guard_dispatch_thread(&mut shell);
} });
}
nvim::open_file(ui.shell.nvim(), open_arg().as_ref()); SHELL!(shell = {
nvim::open_file(shell.nvim(), open_arg().as_ref());
});
}); });
} }

View File

@ -2,7 +2,7 @@ use neovim_lib::{Neovim, NeovimApi, Session, Value, Integer, UiAttachOptions, Ca
use std::io::{Result, Error, ErrorKind}; use std::io::{Result, Error, ErrorKind};
use std::result; use std::result;
use ui_model::{UiModel, ModelRect}; use ui_model::{UiModel, ModelRect};
use ui; use ui::SH;
use shell::Shell; use shell::Shell;
use glib; use glib;
@ -191,9 +191,9 @@ fn safe_call<F>(cb: F)
where F: Fn(&mut Shell) -> result::Result<(), String> + 'static + Send where F: Fn(&mut Shell) -> result::Result<(), String> + 'static + Send
{ {
glib::idle_add(move || { glib::idle_add(move || {
ui::UI.with(|ui_cell| if let Err(msg) = cb(&mut ui_cell.borrow_mut().shell) { SHELL!(shell = { if let Err(msg) = cb(&mut shell) {
println!("Error call function: {}", msg); println!("Error call function: {}", msg);
}); }});
glib::Continue(false) glib::Continue(false)
}); });
} }

View File

@ -17,7 +17,7 @@ use settings;
use ui_model::{UiModel, Cell, Attrs, Color, ModelRect, COLOR_BLACK, COLOR_WHITE, COLOR_RED}; use ui_model::{UiModel, Cell, Attrs, Color, ModelRect, COLOR_BLACK, COLOR_WHITE, COLOR_RED};
use nvim::{RedrawEvents, GuiApi, RepaintMode}; use nvim::{RedrawEvents, GuiApi, RepaintMode};
use input::{convert_key, keyval_to_input_string}; use input::{convert_key, keyval_to_input_string};
use ui::{UI, Ui, SET}; use ui::{UI, SH, SET};
use cursor::Cursor; use cursor::Cursor;
const DEFAULT_FONT_NAME: &'static str = "DejaVu Sans Mono 12"; const DEFAULT_FONT_NAME: &'static str = "DejaVu Sans Mono 12";
@ -256,20 +256,18 @@ fn gtk_key_press(_: &DrawingArea, ev: &EventKey) -> Inhibit {
} }
fn gtk_draw(_: &DrawingArea, ctx: &cairo::Context) -> Inhibit { fn gtk_draw(_: &DrawingArea, ctx: &cairo::Context) -> Inhibit {
UI.with(|ui_cell| { SHELL!(shell = {
let mut ui = ui_cell.borrow_mut(); if shell.line_height.is_none() {
let (width, height) = calc_char_bounds(&shell, ctx);
if ui.shell.line_height.is_none() { shell.line_height = Some(height as f64);
let (width, height) = calc_char_bounds(&ui.shell, ctx); shell.char_width = Some(width as f64);
ui.shell.line_height = Some(height as f64);
ui.shell.char_width = Some(width as f64);
} }
draw(&ui.shell, ctx); draw(&shell, ctx);
request_width(&mut shell);
request_width(&mut ui);
}); });
Inhibit(false) Inhibit(false)
} }
@ -442,27 +440,31 @@ fn calc_char_bounds(shell: &Shell, ctx: &cairo::Context) -> (i32, i32) {
layout.get_pixel_size() layout.get_pixel_size()
} }
fn request_width(ui: &mut Ui) { fn request_width(shell: &mut Shell) {
if !ui.shell.request_width { if !shell.request_width {
return; return;
} }
if ui.shell.resize_timer.is_some() { if shell.resize_timer.is_some() {
return; return;
} }
ui.shell.request_width = false; shell.request_width = false;
let width = ui.shell.drawing_area.get_allocated_width(); let width = shell.drawing_area.get_allocated_width();
let height = ui.shell.drawing_area.get_allocated_height(); let height = shell.drawing_area.get_allocated_height();
let request_height = (ui.shell.model.rows as f64 * ui.shell.line_height.unwrap()) as i32; let request_height = (shell.model.rows as f64 * shell.line_height.unwrap()) as i32;
let request_width = (ui.shell.model.columns as f64 * ui.shell.char_width.unwrap()) as i32; let request_width = (shell.model.columns as f64 * shell.char_width.unwrap()) as i32;
if width != request_width || height != request_height { if width != request_width || height != request_height {
let window = ui.window.as_ref().unwrap(); UI.with(|ui_cell| {
let (win_width, win_height) = window.get_size(); let ui = ui_cell.borrow();
let h_border = win_width - width;
let v_border = win_height - height; let window = ui.window.as_ref().unwrap();
window.resize(request_width + h_border, request_height + v_border); let (win_width, win_height) = window.get_size();
let h_border = win_width - width;
let v_border = win_height - height;
window.resize(request_width + h_border, request_height + v_border);
});
} }
} }

View File

@ -21,20 +21,21 @@ macro_rules! ui_thread_var {
} }
ui_thread_var!(UI, Ui, Ui::new()); ui_thread_var!(UI, Ui, Ui::new());
ui_thread_var!(SH, Shell, Shell::new());
ui_thread_var!(SET, settings::Settings, settings::Settings::new()); ui_thread_var!(SET, settings::Settings, settings::Settings::new());
#[macro_export] #[macro_export]
macro_rules! SHELL { macro_rules! SHELL {
(&$id:ident = $expr:expr) => ( (&$id:ident = $expr:expr) => (
UI.with(|ui_cell| { SH.with(|shell_cell| {
let $id = &ui_cell.borrow().shell; let $id = &shell_cell.borrow();
$expr $expr
}); });
); );
($id:ident = $expr:expr) => ( ($id:ident = $expr:expr) => (
UI.with(|ui_cell| { SH.with(|shell_cell| {
let mut $id = &mut ui_cell.borrow_mut().shell; let mut $id = &mut shell_cell.borrow_mut();
$expr $expr
}); });
); );
@ -44,7 +45,6 @@ pub struct Ui {
pub initialized: bool, pub initialized: bool,
pub window: Option<ApplicationWindow>, pub window: Option<ApplicationWindow>,
header_bar: HeaderBar, header_bar: HeaderBar,
pub shell: Shell,
} }
impl Ui { impl Ui {
@ -53,7 +53,6 @@ impl Ui {
window: None, window: None,
header_bar: HeaderBar::new(), header_bar: HeaderBar::new(),
initialized: false, initialized: false,
shell: Shell::new(),
} }
} }
@ -63,7 +62,9 @@ impl Ui {
pub fn destroy(&mut self) { pub fn destroy(&mut self) {
self.close_window(); self.close_window();
self.shell.nvim().ui_detach().expect("Error in ui_detach"); SHELL!(shell = {
shell.nvim().ui_detach().expect("Error in ui_detach");
});
} }
pub fn init(&mut self, app: &gtk::Application) { pub fn init(&mut self, app: &gtk::Application) {
@ -72,60 +73,58 @@ impl Ui {
} }
self.initialized = true; self.initialized = true;
SET.with(|settings| { SHELL!(shell = {
let mut settings = settings.borrow_mut(); SET.with(|settings| {
settings.init(&mut self.shell); let mut settings = settings.borrow_mut();
settings.init(&mut shell);
});
self.header_bar.set_show_close_button(true);
let save_image = Image::new_from_icon_name("document-save",
gtk_sys::GTK_ICON_SIZE_SMALL_TOOLBAR as i32);
let save_btn = ToolButton::new(Some(&save_image), None);
save_btn.connect_clicked(|_| edit_save_all());
self.header_bar.pack_start(&save_btn);
let paste_image = Image::new_from_icon_name("edit-paste",
gtk_sys::GTK_ICON_SIZE_SMALL_TOOLBAR as i32);
let paste_btn = ToolButton::new(Some(&paste_image), None);
paste_btn.connect_clicked(|_| edit_paste());
self.header_bar.pack_start(&paste_btn);
shell.init();
self.window = Some(ApplicationWindow::new(app));
let window = self.window.as_ref().unwrap();
window.set_titlebar(Some(&self.header_bar));
window.add(&shell.drawing_area);
window.show_all();
window.connect_delete_event(gtk_delete);
window.set_title("Neovim-gtk");
shell.add_configure_event();
}); });
self.header_bar.set_show_close_button(true);
let save_image = Image::new_from_icon_name("document-save",
gtk_sys::GTK_ICON_SIZE_SMALL_TOOLBAR as i32);
let save_btn = ToolButton::new(Some(&save_image), None);
save_btn.connect_clicked(|_| edit_save_all());
self.header_bar.pack_start(&save_btn);
let paste_image = Image::new_from_icon_name("edit-paste",
gtk_sys::GTK_ICON_SIZE_SMALL_TOOLBAR as i32);
let paste_btn = ToolButton::new(Some(&paste_image), None);
paste_btn.connect_clicked(|_| edit_paste());
self.header_bar.pack_start(&paste_btn);
self.shell.init();
self.window = Some(ApplicationWindow::new(app));
let window = self.window.as_ref().unwrap();
window.set_titlebar(Some(&self.header_bar));
window.add(&self.shell.drawing_area);
window.show_all();
window.connect_delete_event(gtk_delete);
window.set_title("Neovim-gtk");
self.shell.add_configure_event();
} }
} }
fn edit_paste() { fn edit_paste() {
UI.with(|ui_cell| { SHELL!(shell = {
let mut ui = ui_cell.borrow_mut(); let paste_command = if shell.mode == NvimMode::Normal {
let paste_command = if ui.shell.mode == NvimMode::Normal {
"\"*p" "\"*p"
} else { } else {
"<Esc>\"*pa" "<Esc>\"*pa"
}; };
let mut nvim = ui.shell.nvim(); let mut nvim = shell.nvim();
nvim.input(paste_command).report_err(nvim); nvim.input(paste_command).report_err(nvim);
}); });
} }
fn edit_save_all() { fn edit_save_all() {
UI.with(|ui_cell| { SHELL!(shell = {
let mut ui = ui_cell.borrow_mut(); let mut nvim = shell.nvim();
let mut nvim = ui.shell.nvim();
nvim.command(":wa").report_err(nvim); nvim.command(":wa").report_err(nvim);
}); });
} }