neovim-gtk/src/settings.rs

179 lines
4.1 KiB
Rust
Raw Normal View History

use std::rc::{Rc, Weak};
use std::cell::RefCell;
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;
2017-07-22 20:32:13 +00:00
#[cfg(unix)]
use gio::SettingsExt;
2017-03-13 15:03:32 +00:00
#[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,
}
struct State {
2017-03-13 15:03:32 +00:00
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 State {
2017-03-13 21:02:31 +00:00
#[cfg(unix)]
pub fn new() -> State {
State {
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() -> State {
State { 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
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) =
2017-09-13 15:34:18 +00:00
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 struct Settings {
shell: Option<Weak<RefCell<Shell>>>,
state: Rc<RefCell<State>>,
}
impl Settings {
pub fn new() -> Settings {
Settings {
shell: None,
state: Rc::new(RefCell::new(State::new())),
}
}
pub fn set_shell(&mut self, shell: Weak<RefCell<Shell>>) {
self.shell = Some(shell);
}
#[cfg(unix)]
pub fn init(&mut self) {
let shell = Weak::upgrade(self.shell.as_ref().unwrap()).unwrap();
let state = self.state.clone();
2017-09-13 15:34:18 +00:00
self.state.borrow_mut().update_font(
&mut *shell.borrow_mut(),
);
self.state
.borrow()
.gnome_interface_settings
2017-09-13 15:34:18 +00:00
.connect_changed(move |_, _| {
monospace_font_changed(&mut *shell.borrow_mut(), &mut *state.borrow_mut())
});
}
#[cfg(target_os = "windows")]
pub fn init(&mut self) {}
2017-03-13 15:03:32 +00:00
pub fn set_font_source(&mut self, src: FontSource) {
self.state.borrow_mut().font_source = src;
2017-03-13 15:03:32 +00:00
}
}
2017-03-13 21:02:31 +00:00
#[cfg(unix)]
fn monospace_font_changed(mut shell: &mut Shell, state: &mut State) {
// rpc is priority for font
if state.font_source != FontSource::Rpc {
state.update_font(&mut shell);
}
2017-03-13 15:03:32 +00:00
}
2017-10-19 14:04:58 +00:00
use std::path::Path;
use std::fs::File;
use std::io::prelude::*;
use toml;
use serde;
use dirs;
pub trait SettingsLoader: Sized + serde::Serialize {
const SETTINGS_FILE: &'static str;
fn empty() -> Self;
fn from_str(s: &str) -> Result<Self, String>;
fn load() -> Self {
match load_err() {
Ok(settings) => settings,
Err(e) => {
error!("{}", e);
2017-10-19 14:04:58 +00:00
Self::empty()
}
}
}
2017-10-24 15:03:34 +00:00
fn is_file_exists() -> bool {
if let Ok(mut toml_path) = dirs::get_app_config_dir() {
toml_path.push(Self::SETTINGS_FILE);
toml_path.is_file()
} else {
false
}
}
2017-10-19 14:04:58 +00:00
fn save(&self) {
match save_err(self) {
Ok(()) => (),
2017-10-24 15:03:34 +00:00
Err(e) => error!("{}", e),
2017-10-19 14:04:58 +00:00
}
}
}
fn load_from_file<T: SettingsLoader>(path: &Path) -> Result<T, String> {
if path.exists() {
let mut file = File::open(path).map_err(|e| format!("{}", e))?;
let mut contents = String::new();
file.read_to_string(&mut contents).map_err(
|e| format!("{}", e),
)?;
T::from_str(&contents)
} else {
Ok(T::empty())
}
}
fn load_err<T: SettingsLoader>() -> Result<T, String> {
let mut toml_path = dirs::get_app_config_dir_create()?;
toml_path.push(T::SETTINGS_FILE);
load_from_file(&toml_path)
}
fn save_err<T: SettingsLoader>(sl: &T) -> Result<(), String> {
let mut toml_path = dirs::get_app_config_dir_create()?;
toml_path.push(T::SETTINGS_FILE);
let mut file = File::create(toml_path).map_err(|e| format!("{}", e))?;
let contents = toml::to_vec::<T>(sl).map_err(|e| format!("{}", e))?;
file.write_all(&contents).map_err(|e| format!("{}", e))?;
Ok(())
}