neovim-gtk/src/settings.rs

83 lines
1.8 KiB
Rust
Raw Normal View History

2017-03-13 21:02:31 +00:00
#[cfg(unix)]
use ui::{UI, SET};
#[cfg(unix)]
use nvim::RedrawEvents;
2017-03-13 15:03:32 +00:00
use ui::Ui;
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-13 15:03:32 +00:00
pub fn init(&mut self, ui: &mut Ui) {
self.gnome_interface_settings.connect_changed(|_, _| monospace_font_changed());
self.update_font(ui);
}
#[cfg(target_os = "windows")]
2017-03-14 07:48:04 +00:00
pub fn init(&mut self, _: &mut Ui) {}
2017-03-13 15:03:32 +00:00
2017-03-13 21:02:31 +00:00
#[cfg(unix)]
2017-03-13 15:03:32 +00:00
fn update_font(&mut self, ui: &mut Ui) {
// rpc is priority for font
if self.font_source == FontSource::Rpc {
return;
}
2017-03-14 07:48:04 +00:00
if let Some(ref font_name) = self.gnome_interface_settings
.get_string("monospace-font-name") {
ui.set_font_desc(font_name);
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 {
set.update_font(&mut *ui);
ui.on_redraw();
}
});
2017-03-13 15:03:32 +00:00
});
}