use super::{EXPAND_OTHERS, EXPAND_RESPONSES, UiState};
use serde_json::Value;
const ZOOM: &str = "zoom";
const NOTIFY_UNFOCUSED: &str = "notify_unfocused";
const ZOOM_MIN: f32 = 0.2;
const ZOOM_MAX: f32 = 5.0;
impl UiState {
fn flag(&self, key: &str, default: bool) -> bool {
self.pane
.root
.get(key)
.and_then(Value::as_bool)
.unwrap_or(default)
}
fn set_flag(&mut self, key: &str, value: bool) {
self.pane.root.insert(key.to_string(), Value::Bool(value));
self.pane.save();
}
pub fn transcript_expand_responses(&self) -> bool {
self.flag(EXPAND_RESPONSES, true)
}
pub fn set_transcript_expand_responses(&mut self, expand: bool) {
self.set_flag(EXPAND_RESPONSES, expand);
}
pub fn transcript_expand_others(&self) -> bool {
self.flag(EXPAND_OTHERS, false)
}
pub fn set_transcript_expand_others(&mut self, expand: bool) {
self.set_flag(EXPAND_OTHERS, expand);
}
pub fn notify_unfocused(&self) -> bool {
self.flag(NOTIFY_UNFOCUSED, true)
}
pub fn zoom(&self) -> f32 {
self.pane
.root
.get(ZOOM)
.and_then(Value::as_f64)
.map_or(1.0, |z| (z as f32).clamp(ZOOM_MIN, ZOOM_MAX))
}
pub fn set_zoom(&mut self, zoom: f32) {
let snapped = (f64::from(zoom.clamp(ZOOM_MIN, ZOOM_MAX)) * 100.0).round() / 100.0;
self.pane
.root
.insert(ZOOM.to_string(), Value::from(snapped));
self.pane.save();
}
}