fractal/session/model/
session_settings.rsuse std::collections::BTreeSet;
use gtk::{glib, prelude::*, subclass::prelude::*};
use serde::{Deserialize, Serialize};
use super::SidebarSectionName;
use crate::Application;
#[derive(Debug, Clone, Serialize, Deserialize, glib::Boxed)]
#[boxed_type(name = "StoredSessionSettings")]
pub struct StoredSessionSettings {
#[serde(default, skip_serializing_if = "Vec::is_empty")]
explore_custom_servers: Vec<String>,
#[serde(
default = "ruma::serde::default_true",
skip_serializing_if = "ruma::serde::is_true"
)]
notifications_enabled: bool,
#[serde(
default = "ruma::serde::default_true",
skip_serializing_if = "ruma::serde::is_true"
)]
public_read_receipts_enabled: bool,
#[serde(
default = "ruma::serde::default_true",
skip_serializing_if = "ruma::serde::is_true"
)]
typing_enabled: bool,
#[serde(default)]
sections_expanded: SectionsExpanded,
}
impl Default for StoredSessionSettings {
fn default() -> Self {
Self {
explore_custom_servers: Default::default(),
notifications_enabled: true,
public_read_receipts_enabled: true,
typing_enabled: true,
sections_expanded: Default::default(),
}
}
}
mod imp {
use std::{
cell::{OnceCell, RefCell},
marker::PhantomData,
};
use super::*;
#[derive(Debug, Default, glib::Properties)]
#[properties(wrapper_type = super::SessionSettings)]
pub struct SessionSettings {
#[property(get, construct_only)]
pub session_id: OnceCell<String>,
#[property(get, construct_only)]
pub stored_settings: RefCell<StoredSessionSettings>,
#[property(get = Self::notifications_enabled, set = Self::set_notifications_enabled, explicit_notify, default = true)]
pub notifications_enabled: PhantomData<bool>,
#[property(get = Self::public_read_receipts_enabled, set = Self::set_public_read_receipts_enabled, explicit_notify, default = true)]
pub public_read_receipts_enabled: PhantomData<bool>,
#[property(get = Self::typing_enabled, set = Self::set_typing_enabled, explicit_notify, default = true)]
pub typing_enabled: PhantomData<bool>,
}
#[glib::object_subclass]
impl ObjectSubclass for SessionSettings {
const NAME: &'static str = "SessionSettings";
type Type = super::SessionSettings;
}
#[glib::derived_properties]
impl ObjectImpl for SessionSettings {}
impl SessionSettings {
fn notifications_enabled(&self) -> bool {
self.stored_settings.borrow().notifications_enabled
}
fn set_notifications_enabled(&self, enabled: bool) {
if self.notifications_enabled() == enabled {
return;
}
let obj = self.obj();
self.stored_settings.borrow_mut().notifications_enabled = enabled;
obj.save();
obj.notify_notifications_enabled();
}
fn public_read_receipts_enabled(&self) -> bool {
self.stored_settings.borrow().public_read_receipts_enabled
}
fn set_public_read_receipts_enabled(&self, enabled: bool) {
if self.public_read_receipts_enabled() == enabled {
return;
}
let obj = self.obj();
self.stored_settings
.borrow_mut()
.public_read_receipts_enabled = enabled;
obj.save();
obj.notify_public_read_receipts_enabled();
}
fn typing_enabled(&self) -> bool {
self.stored_settings.borrow().typing_enabled
}
fn set_typing_enabled(&self, enabled: bool) {
if self.typing_enabled() == enabled {
return;
}
let obj = self.obj();
self.stored_settings.borrow_mut().typing_enabled = enabled;
obj.save();
obj.notify_typing_enabled();
}
}
}
glib::wrapper! {
pub struct SessionSettings(ObjectSubclass<imp::SessionSettings>);
}
impl SessionSettings {
pub fn new(session_id: &str) -> Self {
glib::Object::builder()
.property("session-id", session_id)
.property("stored-settings", StoredSessionSettings::default())
.build()
}
pub fn restore(session_id: &str, stored_settings: StoredSessionSettings) -> Self {
glib::Object::builder()
.property("session-id", session_id)
.property("stored-settings", &stored_settings)
.build()
}
fn save(&self) {
Application::default().session_list().settings().save();
}
pub fn delete(&self) {
Application::default()
.session_list()
.settings()
.remove(&self.session_id());
}
pub fn explore_custom_servers(&self) -> Vec<String> {
self.imp()
.stored_settings
.borrow()
.explore_custom_servers
.clone()
}
pub fn set_explore_custom_servers(&self, servers: Vec<String>) {
if self.explore_custom_servers() == servers {
return;
}
self.imp()
.stored_settings
.borrow_mut()
.explore_custom_servers = servers;
self.save();
}
pub fn is_section_expanded(&self, section_name: SidebarSectionName) -> bool {
self.imp()
.stored_settings
.borrow()
.sections_expanded
.is_section_expanded(section_name)
}
pub fn set_section_expanded(&self, section_name: SidebarSectionName, expanded: bool) {
self.imp()
.stored_settings
.borrow_mut()
.sections_expanded
.set_section_expanded(section_name, expanded);
self.save();
}
}
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
pub struct SectionsExpanded(BTreeSet<SidebarSectionName>);
impl SectionsExpanded {
pub fn is_section_expanded(&self, section_name: SidebarSectionName) -> bool {
self.0.contains(§ion_name)
}
pub fn set_section_expanded(&mut self, section_name: SidebarSectionName, expanded: bool) {
if expanded {
self.0.insert(section_name);
} else {
self.0.remove(§ion_name);
}
}
}
impl Default for SectionsExpanded {
fn default() -> Self {
Self(BTreeSet::from([
SidebarSectionName::VerificationRequest,
SidebarSectionName::Invited,
SidebarSectionName::Favorite,
SidebarSectionName::Normal,
SidebarSectionName::LowPriority,
]))
}
}