neovim-gtk/src/ui.rs

510 lines
16 KiB
Rust
Raw Normal View History

2018-01-23 20:22:19 +00:00
use std::cell::{Ref, RefCell, RefMut};
use std::{env, thread};
2018-02-25 14:54:15 +00:00
use std::path::Path;
use std::rc::Rc;
use std::sync::Arc;
2016-03-31 16:19:08 +00:00
2018-01-23 20:22:19 +00:00
use gdk;
2016-03-16 15:25:25 +00:00
use gtk;
use gtk::prelude::*;
2018-03-11 12:49:13 +00:00
use gtk::{AboutDialog, ApplicationWindow, Button, HeaderBar, Orientation, Paned, SettingsExt};
use gio::prelude::*;
use gio::{Menu, MenuExt, MenuItem, SimpleAction};
2018-03-30 20:54:37 +00:00
use glib::variant::FromVariant;
2018-01-23 20:22:19 +00:00
use toml;
2018-03-30 20:54:37 +00:00
use neovim_lib::Value;
2018-01-23 20:22:19 +00:00
use settings::{Settings, SettingsLoader};
use shell::{self, Shell, ShellOptions};
2017-04-01 10:00:14 +00:00
use shell_dlg;
2017-05-13 14:31:19 +00:00
use project::Projects;
use plug_manager;
2018-03-11 12:49:13 +00:00
use file_browser::FileBrowserWidget;
2018-02-25 14:54:15 +00:00
use subscriptions::SubscriptionHandle;
2017-03-13 15:03:32 +00:00
2017-11-05 18:07:02 +00:00
macro_rules! clone {
(@param _) => ( _ );
(@param $x:ident) => ( $x );
($($n:ident),+ => move || $body:expr) => (
{
$( let $n = $n.clone(); )+
move || $body
}
);
($($n:ident),+ => move |$($p:tt),+| $body:expr) => (
{
$( let $n = $n.clone(); )+
move |$(clone!(@param $p),)+| $body
}
);
}
2018-02-16 09:57:26 +00:00
const DEFAULT_WIDTH: i32 = 800;
const DEFAULT_HEIGHT: i32 = 600;
2018-03-11 12:49:13 +00:00
const DEFAULT_SIDEBAR_WIDTH: i32 = 200;
2018-02-16 09:57:26 +00:00
pub struct Ui {
initialized: bool,
comps: Arc<UiMutex<Components>>,
settings: Rc<RefCell<Settings>>,
shell: Rc<RefCell<Shell>>,
2017-05-13 14:31:19 +00:00
projects: Rc<RefCell<Projects>>,
2017-10-15 19:50:59 +00:00
plug_manager: Arc<UiMutex<plug_manager::Manager>>,
2018-03-11 12:49:13 +00:00
file_browser: Arc<UiMutex<FileBrowserWidget>>,
}
pub struct Components {
window: Option<ApplicationWindow>,
2018-01-23 20:22:19 +00:00
window_state: WindowState,
2018-02-25 14:54:15 +00:00
open_btn: Button,
2016-03-19 10:27:39 +00:00
}
2016-03-16 15:25:25 +00:00
impl Components {
fn new() -> Components {
2018-02-25 14:54:15 +00:00
let open_btn = Button::new();
let open_btn_box = gtk::Box::new(gtk::Orientation::Horizontal, 3);
open_btn_box.pack_start(&gtk::Label::new("Open"), false, false, 3);
open_btn_box.pack_start(
&gtk::Image::new_from_icon_name("pan-down-symbolic", gtk::IconSize::Menu.into()),
2018-03-04 12:22:56 +00:00
false,
false,
3,
2018-02-25 14:54:15 +00:00
);
open_btn.add(&open_btn_box);
open_btn.set_can_focus(false);
Components {
2018-02-25 14:54:15 +00:00
open_btn,
2017-03-06 13:58:10 +00:00
window: None,
2018-01-23 20:22:19 +00:00
window_state: WindowState::load(),
2016-03-19 10:27:39 +00:00
}
2016-03-16 15:25:25 +00:00
}
2017-03-16 10:18:13 +00:00
pub fn close_window(&self) {
2017-03-06 13:58:10 +00:00
self.window.as_ref().unwrap().destroy();
}
pub fn window(&self) -> &ApplicationWindow {
self.window.as_ref().unwrap()
}
}
impl Ui {
pub fn new(options: ShellOptions) -> Ui {
let plug_manager = plug_manager::Manager::new();
let plug_manager = Arc::new(UiMutex::new(plug_manager));
2018-03-11 12:49:13 +00:00
let file_browser = Arc::new(UiMutex::new(FileBrowserWidget::new()));
let comps = Arc::new(UiMutex::new(Components::new()));
let settings = Rc::new(RefCell::new(Settings::new()));
let shell = Rc::new(RefCell::new(Shell::new(settings.clone(), options)));
settings.borrow_mut().set_shell(Rc::downgrade(&shell));
2017-05-13 14:31:19 +00:00
let projects = Projects::new(&comps.borrow().open_btn, shell.clone());
Ui {
initialized: false,
2017-05-13 14:31:19 +00:00
comps,
shell,
settings,
projects,
2017-10-15 19:50:59 +00:00
plug_manager,
2018-03-11 12:49:13 +00:00
file_browser,
}
2017-03-16 10:18:13 +00:00
}
2018-02-16 09:57:26 +00:00
pub fn init(&mut self, app: &gtk::Application, restore_win_state: bool) {
if self.initialized {
return;
2017-03-14 20:12:31 +00:00
}
self.initialized = true;
let mut settings = self.settings.borrow_mut();
settings.init();
2018-01-23 20:22:19 +00:00
let window = ApplicationWindow::new(app);
2018-03-11 12:49:13 +00:00
let main = Paned::new(Orientation::Horizontal);
2018-01-23 20:22:19 +00:00
{
// initialize window from comps
// borrowing of comps must be leaved
// for event processing
let mut comps = self.comps.borrow_mut();
self.shell.borrow_mut().init();
comps.window = Some(window.clone());
let prefer_dark_theme = env::var("NVIM_GTK_PREFER_DARK_THEME")
.map(|opt| opt.trim() == "1")
.unwrap_or(false);
if prefer_dark_theme {
if let Some(settings) = window.get_settings() {
settings.set_property_gtk_application_prefer_dark_theme(true);
}
}
2017-05-13 14:31:19 +00:00
2018-02-16 09:57:26 +00:00
if restore_win_state {
if comps.window_state.is_maximized {
window.maximize();
}
window.set_default_size(
comps.window_state.current_width,
comps.window_state.current_height,
);
2018-03-11 12:49:13 +00:00
main.set_position(comps.window_state.sidebar_width);
2018-02-16 09:57:26 +00:00
} else {
window.set_default_size(DEFAULT_WIDTH, DEFAULT_HEIGHT);
2018-03-11 12:49:13 +00:00
main.set_position(DEFAULT_SIDEBAR_WIDTH);
2018-01-23 20:22:19 +00:00
}
}
2018-02-25 14:54:15 +00:00
// Client side decorations including the toolbar are disabled via NVIM_GTK_NO_HEADERBAR=1
let use_header_bar = env::var("NVIM_GTK_NO_HEADERBAR")
.map(|opt| opt.trim() != "1")
.unwrap_or(true);
if app.prefers_app_menu() || use_header_bar {
self.create_main_menu(app, &window);
}
let update_subtitle = if use_header_bar {
Some(self.create_header_bar())
} else {
None
};
2018-03-11 12:49:13 +00:00
let show_sidebar_action =
SimpleAction::new_stateful("show-sidebar", None, &false.to_variant());
let file_browser_ref = self.file_browser.clone();
2018-01-23 20:22:19 +00:00
let comps_ref = self.comps.clone();
2018-03-11 12:49:13 +00:00
show_sidebar_action.connect_change_state(move |action, value| {
if let Some(ref value) = *value {
action.set_state(value);
let is_active = value.get::<bool>().unwrap();
file_browser_ref.borrow().set_visible(is_active);
comps_ref.borrow_mut().window_state.show_sidebar = is_active;
}
2018-01-23 20:22:19 +00:00
});
2018-03-11 12:49:13 +00:00
app.add_action(&show_sidebar_action);
let comps_ref = self.comps.clone();
window.connect_size_allocate(clone!(main => move |window, _| {
gtk_window_size_allocate(
window,
&mut *comps_ref.borrow_mut(),
&main,
);
}));
2018-01-23 20:22:19 +00:00
let comps_ref = self.comps.clone();
window.connect_window_state_event(move |_, event| {
gtk_window_state_event(event, &mut *comps_ref.borrow_mut());
Inhibit(false)
});
2017-03-06 13:58:10 +00:00
2018-01-23 20:22:19 +00:00
let comps_ref = self.comps.clone();
window.connect_destroy(move |_| {
comps_ref.borrow().window_state.save();
});
2017-03-06 21:05:48 +00:00
let shell = self.shell.borrow();
2018-03-11 12:49:13 +00:00
let file_browser = self.file_browser.borrow();
main.pack1(&**file_browser, false, false);
main.pack2(&**shell, true, false);
window.add(&main);
window.show_all();
2018-02-25 14:54:15 +00:00
2018-03-11 12:49:13 +00:00
if restore_win_state {
// Hide sidebar, if it wasn't shown last time.
// Has to be done after show_all(), so it won't be shown again.
let show_sidebar = self.comps.borrow().window_state.show_sidebar;
show_sidebar_action.change_state(&show_sidebar.to_variant());
}
2018-02-25 14:54:15 +00:00
let comps_ref = self.comps.clone();
2018-03-04 12:22:56 +00:00
let update_title = shell.state.borrow().subscribe(
"BufEnter,DirChanged",
&["expand('%:p')", "getcwd()"],
move |args| {
2018-02-25 14:54:15 +00:00
let comps = comps_ref.borrow();
let window = comps.window.as_ref().unwrap();
let file_path = &args[0];
let dir = Path::new(&args[1]);
let filename = if file_path.is_empty() {
"[No Name]"
} else if let Some(rel_path) = Path::new(&file_path)
.strip_prefix(&dir)
.ok()
.and_then(|p| p.to_str())
{
rel_path
} else {
&file_path
};
window.set_title(filename);
2018-03-04 12:22:56 +00:00
},
);
let comps_ref = self.comps.clone();
let shell_ref = self.shell.clone();
window.connect_delete_event(move |_, _| gtk_delete(&*comps_ref, &*shell_ref));
2017-05-27 16:50:25 +00:00
shell.grab_focus();
let comps_ref = self.comps.clone();
shell.set_detach_cb(Some(move || {
2017-07-09 11:05:55 +00:00
let comps_ref = comps_ref.clone();
gtk::idle_add(move || {
comps_ref.borrow().close_window();
Continue(false)
});
}));
2017-10-15 19:50:59 +00:00
let state_ref = self.shell.borrow().state.clone();
2018-03-11 12:49:13 +00:00
let file_browser_ref = self.file_browser.clone();
2017-10-15 19:50:59 +00:00
let plug_manager_ref = self.plug_manager.clone();
shell.set_nvim_started_cb(Some(move || {
2018-02-25 14:54:15 +00:00
let state = state_ref.borrow();
2018-01-23 20:22:19 +00:00
plug_manager_ref
.borrow_mut()
.init_nvim_client(state_ref.borrow().nvim_clone());
2018-03-11 12:49:13 +00:00
file_browser_ref.borrow_mut().init(&state);
2018-02-25 14:54:15 +00:00
state.set_autocmds();
state.run_now(&update_title);
if let Some(ref update_subtitle) = update_subtitle {
state.run_now(&update_subtitle);
}
2017-10-15 19:50:59 +00:00
}));
2018-03-30 20:54:37 +00:00
let sidebar_action = UiMutex::new(show_sidebar_action);
shell.set_nvim_command_cb(Some(move |args: Vec<Value>| {
if let Some(cmd) = args[0].as_str() {
match cmd {
"ToggleSidebar" => {
let action = sidebar_action.borrow();
let state = !bool::from_variant(&action.get_state().unwrap()).unwrap();
action.change_state(&state.to_variant());
}
_ => {}
}
}
}));
2017-03-06 21:05:48 +00:00
}
2017-03-31 20:19:50 +00:00
2018-02-25 14:54:15 +00:00
fn create_header_bar(&self) -> SubscriptionHandle {
let header_bar = HeaderBar::new();
let comps = self.comps.borrow();
let window = comps.window.as_ref().unwrap();
let projects = self.projects.clone();
header_bar.pack_start(&comps.open_btn);
comps
.open_btn
.connect_clicked(move |_| projects.borrow_mut().show());
2018-03-04 12:22:56 +00:00
let new_tab_btn =
Button::new_from_icon_name("tab-new-symbolic", gtk::IconSize::SmallToolbar.into());
2018-02-25 14:54:15 +00:00
let shell_ref = Rc::clone(&self.shell);
new_tab_btn.connect_clicked(move |_| shell_ref.borrow_mut().new_tab());
new_tab_btn.set_can_focus(false);
new_tab_btn.set_tooltip_text("Open a new tab");
2018-02-25 14:54:15 +00:00
header_bar.pack_start(&new_tab_btn);
2018-03-04 12:22:56 +00:00
let paste_btn =
2018-03-04 12:42:44 +00:00
Button::new_from_icon_name("edit-paste-symbolic", gtk::IconSize::SmallToolbar.into());
2018-02-25 14:54:15 +00:00
let shell = self.shell.clone();
paste_btn.connect_clicked(move |_| shell.borrow_mut().edit_paste());
paste_btn.set_can_focus(false);
paste_btn.set_tooltip_text("Paste from clipboard");
2018-02-25 14:54:15 +00:00
header_bar.pack_end(&paste_btn);
let save_btn = Button::new_with_label("Save All");
let shell = self.shell.clone();
save_btn.connect_clicked(move |_| shell.borrow_mut().edit_save_all());
save_btn.set_can_focus(false);
header_bar.pack_end(&save_btn);
header_bar.set_show_close_button(true);
window.set_titlebar(Some(&header_bar));
let shell = self.shell.borrow();
2018-03-04 12:22:56 +00:00
let update_subtitle = shell.state.borrow().subscribe(
"DirChanged",
&["getcwd()"],
move |args| {
2018-02-25 14:54:15 +00:00
header_bar.set_subtitle(&*args[0]);
2018-03-04 12:22:56 +00:00
},
);
2018-02-25 14:54:15 +00:00
update_subtitle
}
2018-02-02 09:17:19 +00:00
fn create_main_menu(&self, app: &gtk::Application, window: &gtk::ApplicationWindow) {
2017-11-05 18:07:02 +00:00
let plug_manager = self.plug_manager.clone();
2017-03-31 20:19:50 +00:00
let menu = Menu::new();
let section = Menu::new();
section.append_item(&MenuItem::new("New Window", "app.new-window"));
menu.append_section(None, &section);
2017-11-05 18:07:02 +00:00
2018-03-11 12:49:13 +00:00
let section = Menu::new();
section.append_item(&MenuItem::new("Sidebar", "app.show-sidebar"));
menu.append_section(None, &section);
let section = Menu::new();
section.append_item(&MenuItem::new("Plugins", "app.Plugins"));
section.append_item(&MenuItem::new("About", "app.HelpAbout"));
menu.append_section(None, &section);
2017-03-31 20:19:50 +00:00
menu.freeze();
2017-05-31 14:14:58 +00:00
app.set_app_menu(Some(&menu));
2017-03-31 20:19:50 +00:00
2017-11-05 18:07:02 +00:00
let plugs_action = SimpleAction::new("Plugins", None);
plugs_action.connect_activate(
2018-02-02 09:17:19 +00:00
clone!(window => move |_, _| plug_manager::Ui::new(&plug_manager).show(&window)),
2017-11-05 18:07:02 +00:00
);
2017-03-31 20:19:50 +00:00
let about_action = SimpleAction::new("HelpAbout", None);
2018-02-02 09:17:19 +00:00
about_action.connect_activate(clone!(window => move |_, _| on_help_about(&window)));
2017-03-31 20:19:50 +00:00
about_action.set_enabled(true);
2017-11-05 18:07:02 +00:00
2017-03-31 20:19:50 +00:00
app.add_action(&about_action);
2017-11-05 18:07:02 +00:00
app.add_action(&plugs_action);
2017-03-31 20:19:50 +00:00
}
}
2018-02-02 09:17:19 +00:00
fn on_help_about(window: &gtk::ApplicationWindow) {
let about = AboutDialog::new();
2018-02-02 09:17:19 +00:00
about.set_transient_for(window);
about.set_program_name("NeovimGtk");
about.set_version(env!("CARGO_PKG_VERSION"));
about.set_logo_icon_name("org.daa.NeovimGtk");
about.set_authors(&[env!("CARGO_PKG_AUTHORS")]);
about.set_comments(
format!(
"Build on top of neovim\n\
2018-01-23 20:22:19 +00:00
Minimum supported neovim version: {}",
shell::MINIMUM_SUPPORTED_NVIM_VERSION
).as_str(),
);
2017-03-31 20:19:50 +00:00
about.connect_response(|about, _| about.destroy());
about.show();
}
fn gtk_delete(comps: &UiMutex<Components>, shell: &RefCell<Shell>) -> Inhibit {
if !shell.borrow().is_nvim_initialized() {
return Inhibit(false);
}
Inhibit(if shell_dlg::can_close_window(comps, shell) {
let comps = comps.borrow();
comps.close_window();
shell.borrow_mut().detach_ui();
false
} else {
true
})
2016-05-04 08:52:57 +00:00
}
2018-03-11 12:49:13 +00:00
fn gtk_window_size_allocate(
app_window: &gtk::ApplicationWindow,
comps: &mut Components,
main: &Paned,
) {
2018-01-23 20:22:19 +00:00
if !app_window.is_maximized() {
let (current_width, current_height) = app_window.get_size();
comps.window_state.current_width = current_width;
comps.window_state.current_height = current_height;
}
2018-03-11 12:49:13 +00:00
if comps.window_state.show_sidebar {
comps.window_state.sidebar_width = main.get_position();
}
2018-01-23 20:22:19 +00:00
}
fn gtk_window_state_event(event: &gdk::EventWindowState, comps: &mut Components) {
comps.window_state.is_maximized = event
.get_new_window_state()
.contains(gdk::WindowState::MAXIMIZED);
}
#[derive(Serialize, Deserialize)]
struct WindowState {
current_width: i32,
current_height: i32,
is_maximized: bool,
2018-03-11 12:49:13 +00:00
show_sidebar: bool,
sidebar_width: i32,
2018-01-23 20:22:19 +00:00
}
2018-05-17 13:44:11 +00:00
impl Default for WindowState {
fn default() -> Self {
2018-01-23 20:22:19 +00:00
WindowState {
2018-02-16 09:57:26 +00:00
current_width: DEFAULT_WIDTH,
current_height: DEFAULT_HEIGHT,
2018-01-23 20:22:19 +00:00
is_maximized: false,
2018-03-11 12:49:13 +00:00
show_sidebar: false,
sidebar_width: DEFAULT_SIDEBAR_WIDTH,
2018-01-23 20:22:19 +00:00
}
}
}
impl SettingsLoader for WindowState {
const SETTINGS_FILE: &'static str = "window.toml";
fn from_str(s: &str) -> Result<Self, String> {
toml::from_str(&s).map_err(|e| format!("{}", e))
}
}
2017-03-26 11:34:38 +00:00
pub struct UiMutex<T: ?Sized> {
2017-09-14 13:29:03 +00:00
thread: thread::ThreadId,
2017-03-26 11:34:38 +00:00
data: RefCell<T>,
}
unsafe impl<T: ?Sized> Send for UiMutex<T> {}
unsafe impl<T: ?Sized> Sync for UiMutex<T> {}
2017-03-26 11:34:38 +00:00
impl<T> UiMutex<T> {
pub fn new(t: T) -> UiMutex<T> {
UiMutex {
2017-09-14 13:29:03 +00:00
thread: thread::current().id(),
data: RefCell::new(t),
}
2017-03-26 11:34:38 +00:00
}
}
impl <T> UiMutex<T> {
pub fn replace(&self, t: T) -> T {
self.assert_ui_thread();
self.data.replace(t)
}
}
2017-03-26 11:34:38 +00:00
impl<T: ?Sized> UiMutex<T> {
pub fn borrow(&self) -> Ref<T> {
self.assert_ui_thread();
2017-03-26 11:34:38 +00:00
self.data.borrow()
}
pub fn borrow_mut(&self) -> RefMut<T> {
self.assert_ui_thread();
2017-03-26 11:34:38 +00:00
self.data.borrow_mut()
}
#[inline]
fn assert_ui_thread(&self) {
2017-09-14 13:29:03 +00:00
if thread::current().id() != self.thread {
panic!("Can access to UI only from main thread");
2017-03-26 11:34:38 +00:00
}
}
}