-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5bd7150
commit d51084a
Showing
5 changed files
with
229 additions
and
5 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
mod config; | ||
use zbus::{dbus_interface, fdo, SignalContext}; | ||
|
||
use zbus::zvariant::{Array, DeserializeDict, OwnedValue, SerializeDict, Signature, Type}; | ||
|
||
const DEFAULT_COLOR: u32 = 0; | ||
const DARK_COLOR: u32 = 1; | ||
const LIGHT_COLOR: u32 = 2; | ||
|
||
const APPEARANCE: &str = "org.freedesktop.appearance"; | ||
const COLOR_SCHEME: &str = "color-scheme"; | ||
const ACCENT_COLOR: &str = "accent-color"; | ||
|
||
#[derive(DeserializeDict, SerializeDict, Clone, Copy, PartialEq, Type)] | ||
#[zvariant(signature = "dict")] | ||
struct Color { | ||
color: [f64; 3], | ||
} | ||
|
||
impl Into<OwnedValue> for Color { | ||
fn into(self) -> OwnedValue { | ||
let arraysignature = Signature::try_from("d").unwrap(); | ||
let mut array = Array::new(arraysignature); | ||
for col in self.color { | ||
array.append(col.into()).unwrap(); | ||
} | ||
OwnedValue::from(array) | ||
} | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct SettingsBackend { | ||
config: config::SettingsConfig, | ||
} | ||
|
||
impl SettingsBackend { | ||
pub fn init() -> Self { | ||
Self { | ||
config: config::SettingsConfig::config_from_file(), | ||
} | ||
} | ||
} | ||
#[dbus_interface(name = "org.freedesktop.impl.portal.Settings")] | ||
impl SettingsBackend { | ||
#[dbus_interface(property, name = "version")] | ||
fn version(&self) -> u32 { | ||
1 | ||
} | ||
|
||
fn read(&self, namespace: String, key: String) -> fdo::Result<OwnedValue> { | ||
if namespace != APPEARANCE { | ||
return Err(zbus::fdo::Error::Failed("No such namespace".to_string())); | ||
} | ||
if key == COLOR_SCHEME { | ||
return Ok(OwnedValue::from(self.config.get_color_scheme())); | ||
} | ||
if key == ACCENT_COLOR { | ||
return Ok(Color { | ||
color: self.config.get_accent_color(), | ||
} | ||
.into()); | ||
} | ||
Err(zbus::fdo::Error::Failed("No such namespace".to_string())) | ||
} | ||
|
||
#[dbus_interface(signal)] | ||
async fn setting_changed( | ||
ctxt: &SignalContext<'_>, | ||
namespace: String, | ||
key: String, | ||
value: u32, | ||
) -> zbus::Result<()>; | ||
// add code here | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
use serde::Deserialize; | ||
use std::io::Read; | ||
const DEFAULT_COLOR_NAME: &str = "default"; | ||
const DARK_COLOR_NAME: &str = "dark"; | ||
const LIGHT_COLOR_NAME: &str = "light"; | ||
|
||
const DEFAULT_ACCENT_COLLOR: &str = "#ffffff"; | ||
|
||
#[derive(Deserialize, PartialEq, Eq, Debug)] | ||
pub struct SettingsConfig { | ||
pub color_scheme: String, | ||
pub accent_color: String, | ||
} | ||
|
||
impl SettingsConfig { | ||
pub fn get_color_scheme(&self) -> u32 { | ||
match self.color_scheme.as_str() { | ||
DEFAULT_COLOR_NAME => super::DEFAULT_COLOR, | ||
DARK_COLOR_NAME => super::DARK_COLOR, | ||
LIGHT_COLOR_NAME => super::LIGHT_COLOR, | ||
_ => unreachable!(), | ||
} | ||
} | ||
pub fn get_accent_color(&self) -> [f64; 3] { | ||
let color = csscolorparser::parse(&self.accent_color) | ||
.map(|color| color.to_rgba8()) | ||
.unwrap_or( | ||
csscolorparser::parse(DEFAULT_ACCENT_COLLOR) | ||
.unwrap() | ||
.to_rgba8(), | ||
); | ||
[ | ||
color[0] as f64 / 256.0, | ||
color[1] as f64 / 256.0, | ||
color[2] as f64 / 256.0, | ||
] | ||
} | ||
} | ||
|
||
impl Default for SettingsConfig { | ||
fn default() -> Self { | ||
SettingsConfig { | ||
color_scheme: DEFAULT_COLOR_NAME.to_string(), | ||
accent_color: DEFAULT_ACCENT_COLLOR.to_string(), | ||
} | ||
} | ||
} | ||
|
||
impl SettingsConfig { | ||
pub fn config_from_file() -> Self { | ||
let Ok(home) = std::env::var("HOME") else { | ||
return Self::default(); | ||
}; | ||
let config_path = std::path::Path::new(home.as_str()) | ||
.join(".config") | ||
.join("xdg-desktop-portal-luminous") | ||
.join("config.toml"); | ||
let Ok(mut file) = std::fs::OpenOptions::new().read(true).open(config_path) else { | ||
return Self::default(); | ||
}; | ||
let mut buf = String::new(); | ||
if file.read_to_string(&mut buf).is_err() { | ||
return Self::default(); | ||
}; | ||
toml::from_str(&buf).unwrap_or(Self::default()) | ||
} | ||
} |