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 ui_model::Color;
use ui::{UI, UiMutex};
use ui::{SH, UiMutex};
use shell::{Shell, NvimMode};
use nvim::{RepaintMode, RedrawEvents};
use std::sync::Arc;

View File

@ -10,10 +10,10 @@ extern crate pangocairo;
extern crate neovim_lib;
extern crate phf;
mod nvim;
mod ui_model;
#[macro_use]
mod ui;
mod nvim;
mod shell;
mod input;
mod settings;
@ -24,6 +24,7 @@ use std::env;
use gio::ApplicationExt;
use shell::Shell;
use ui::SH;
const BIN_PATH_ARG: &'static str = "--nvim-bin-path";
@ -45,17 +46,21 @@ fn main() {
fn activate(app: &gtk::Application) {
ui::UI.with(|ui_cell| {
let mut ui = ui_cell.borrow_mut();
if !ui.initialized {
ui.init(app);
if !ui.initialized {
ui.init(app);
let path = nvim_bin_path(std::env::args());
nvim::initialize(&mut ui.shell, path.as_ref())
.expect("Can't start nvim instance");
let path = nvim_bin_path(std::env::args());
SHELL!(shell = {
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::result;
use ui_model::{UiModel, ModelRect};
use ui;
use ui::SH;
use shell::Shell;
use glib;
@ -191,9 +191,9 @@ fn safe_call<F>(cb: F)
where F: Fn(&mut Shell) -> result::Result<(), String> + 'static + Send
{
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);
});
}});
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 nvim::{RedrawEvents, GuiApi, RepaintMode};
use input::{convert_key, keyval_to_input_string};
use ui::{UI, Ui, SET};
use ui::{UI, SH, SET};
use cursor::Cursor;
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 {
UI.with(|ui_cell| {
let mut ui = ui_cell.borrow_mut();
if ui.shell.line_height.is_none() {
let (width, height) = calc_char_bounds(&ui.shell, ctx);
ui.shell.line_height = Some(height as f64);
ui.shell.char_width = Some(width as f64);
SHELL!(shell = {
if shell.line_height.is_none() {
let (width, height) = calc_char_bounds(&shell, ctx);
shell.line_height = Some(height as f64);
shell.char_width = Some(width as f64);
}
draw(&ui.shell, ctx);
request_width(&mut ui);
draw(&shell, ctx);
request_width(&mut shell);
});
Inhibit(false)
}
@ -442,27 +440,31 @@ fn calc_char_bounds(shell: &Shell, ctx: &cairo::Context) -> (i32, i32) {
layout.get_pixel_size()
}
fn request_width(ui: &mut Ui) {
if !ui.shell.request_width {
fn request_width(shell: &mut Shell) {
if !shell.request_width {
return;
}
if ui.shell.resize_timer.is_some() {
if shell.resize_timer.is_some() {
return;
}
ui.shell.request_width = false;
shell.request_width = false;
let width = ui.shell.drawing_area.get_allocated_width();
let height = ui.shell.drawing_area.get_allocated_height();
let request_height = (ui.shell.model.rows as f64 * ui.shell.line_height.unwrap()) as i32;
let request_width = (ui.shell.model.columns as f64 * ui.shell.char_width.unwrap()) as i32;
let width = shell.drawing_area.get_allocated_width();
let height = shell.drawing_area.get_allocated_height();
let request_height = (shell.model.rows as f64 * shell.line_height.unwrap()) as i32;
let request_width = (shell.model.columns as f64 * shell.char_width.unwrap()) as i32;
if width != request_width || height != request_height {
let window = ui.window.as_ref().unwrap();
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);
UI.with(|ui_cell| {
let ui = ui_cell.borrow();
let window = ui.window.as_ref().unwrap();
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!(SH, Shell, Shell::new());
ui_thread_var!(SET, settings::Settings, settings::Settings::new());
#[macro_export]
macro_rules! SHELL {
(&$id:ident = $expr:expr) => (
UI.with(|ui_cell| {
let $id = &ui_cell.borrow().shell;
SH.with(|shell_cell| {
let $id = &shell_cell.borrow();
$expr
});
);
($id:ident = $expr:expr) => (
UI.with(|ui_cell| {
let mut $id = &mut ui_cell.borrow_mut().shell;
SH.with(|shell_cell| {
let mut $id = &mut shell_cell.borrow_mut();
$expr
});
);
@ -44,7 +45,6 @@ pub struct Ui {
pub initialized: bool,
pub window: Option<ApplicationWindow>,
header_bar: HeaderBar,
pub shell: Shell,
}
impl Ui {
@ -53,7 +53,6 @@ impl Ui {
window: None,
header_bar: HeaderBar::new(),
initialized: false,
shell: Shell::new(),
}
}
@ -63,7 +62,9 @@ impl Ui {
pub fn destroy(&mut self) {
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) {
@ -72,60 +73,58 @@ impl Ui {
}
self.initialized = true;
SET.with(|settings| {
let mut settings = settings.borrow_mut();
settings.init(&mut self.shell);
SHELL!(shell = {
SET.with(|settings| {
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() {
UI.with(|ui_cell| {
let mut ui = ui_cell.borrow_mut();
let paste_command = if ui.shell.mode == NvimMode::Normal {
SHELL!(shell = {
let paste_command = if shell.mode == NvimMode::Normal {
"\"*p"
} else {
"<Esc>\"*pa"
};
let mut nvim = ui.shell.nvim();
let mut nvim = shell.nvim();
nvim.input(paste_command).report_err(nvim);
});
}
fn edit_save_all() {
UI.with(|ui_cell| {
let mut ui = ui_cell.borrow_mut();
let mut nvim = ui.shell.nvim();
SHELL!(shell = {
let mut nvim = shell.nvim();
nvim.command(":wa").report_err(nvim);
});
}