neovim-gtk/src/settings.rs

107 lines
2.4 KiB
Rust
Raw Normal View History

use std::rc::{Rc, Weak};
use std::cell::RefCell;
2017-03-16 12:03:00 +00:00
2017-03-17 22:18:41 +00:00
#[cfg(unix)]
use nvim::RepaintMode;
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
}