neovim-gtk/src/settings.rs

88 lines
1.9 KiB
Rust
Raw Normal View History

2017-03-13 21:02:31 +00:00
#[cfg(unix)]
use ui::{UI, SET};
2017-03-16 12:03:00 +00:00
2017-03-17 22:18:41 +00:00
#[cfg(unix)]
use nvim::RepaintMode;
2017-03-13 21:02:31 +00:00
#[cfg(unix)]
use nvim::RedrawEvents;
2017-03-16 12:03:00 +00:00
use shell::Shell;
2017-03-13 21:02:31 +00:00
#[cfg(unix)]
2017-03-13 15:03:32 +00:00
use gio;
#[derive(PartialEq)]
pub enum FontSource {
Rpc,
2017-03-14 07:48:04 +00:00
#[cfg(unix)]
2017-03-13 15:03:32 +00:00
Gnome,
Default,
}
pub struct Settings {
font_source: FontSource,
2017-03-13 21:02:31 +00:00
#[cfg(unix)]
2017-03-13 15:03:32 +00:00
gnome_interface_settings: gio::Settings,
}
impl Settings {
2017-03-13 21:02:31 +00:00
#[cfg(unix)]
2017-03-13 15:03:32 +00:00
pub fn new() -> Settings {
2017-03-14 07:48:04 +00:00
Settings {
2017-03-13 15:03:32 +00:00
font_source: FontSource::Default,
gnome_interface_settings: gio::Settings::new("org.gnome.desktop.interface"),
}
}
#[cfg(target_os = "windows")]
pub fn new() -> Settings {
2017-03-14 07:48:04 +00:00
Settings { font_source: FontSource::Default }
2017-03-13 15:03:32 +00:00
}
2017-03-13 21:02:31 +00:00
#[cfg(unix)]
2017-03-16 12:03:00 +00:00
pub fn init(&mut self, shell: &mut Shell) {
2017-03-13 15:03:32 +00:00
self.gnome_interface_settings.connect_changed(|_, _| monospace_font_changed());
2017-03-16 12:03:00 +00:00
self.update_font(shell);
2017-03-13 15:03:32 +00:00
}
#[cfg(target_os = "windows")]
2017-03-16 12:03:00 +00:00
pub fn init(&mut self, _: &mut Shell) {}
2017-03-13 15:03:32 +00:00
2017-03-13 21:02:31 +00:00
#[cfg(unix)]
2017-03-16 12:03:00 +00:00
fn update_font(&mut self, shell: &mut Shell) {
2017-03-13 15:03:32 +00:00
// rpc is priority for font
if self.font_source == FontSource::Rpc {
return;
}
2017-03-14 19:31:56 +00:00
if let Some(ref font_name) =
self.gnome_interface_settings
.get_string("monospace-font-name") {
2017-03-16 12:03:00 +00:00
shell.set_font_desc(font_name);
2017-03-14 07:48:04 +00:00
self.font_source = FontSource::Gnome;
}
2017-03-13 15:03:32 +00:00
}
pub fn set_font_source(&mut self, src: FontSource) {
self.font_source = src;
}
}
2017-03-13 21:02:31 +00:00
#[cfg(unix)]
2017-03-13 15:03:32 +00:00
fn monospace_font_changed() {
UI.with(|ui_cell| {
let mut ui = ui_cell.borrow_mut();
2017-03-13 21:02:31 +00:00
SET.with(|set_cell| {
let mut set = set_cell.borrow_mut();
// rpc is priority for font
if set.font_source != FontSource::Rpc {
2017-03-16 12:03:00 +00:00
set.update_font(&mut ui.shell);
2017-03-17 22:18:41 +00:00
ui.shell.on_redraw(&RepaintMode::All);
2017-03-13 21:02:31 +00:00
}
});
2017-03-13 15:03:32 +00:00
});
}