Skip to content

Commit

Permalink
feat: support settings interface
Browse files Browse the repository at this point in the history
  • Loading branch information
Decodetalkers committed Sep 28, 2023
1 parent 5bd7150 commit d51084a
Show file tree
Hide file tree
Showing 5 changed files with 229 additions and 5 deletions.
88 changes: 83 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ wayland-protocols-misc = { version = "0.2.0", features = ["client"] }
xkbcommon = "0.6.0"
tempfile = "3.8.0"
thiserror = "1.0.48"
toml = "0.8.0"
csscolorparser = "0.6.2"


[build-dependencies]
Expand Down
3 changes: 3 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ mod screencast;
mod screenshot;
mod session;
mod slintbackend;
mod settings;

use remotedesktop::RemoteDesktopBackend;
use screencast::ScreenCastBackend;
use screenshot::ScreenShotBackend;
use settings::SettingsBackend;

use std::collections::HashMap;
use std::future::pending;
Expand Down Expand Up @@ -56,6 +58,7 @@ async fn main() -> anyhow::Result<()> {
.serve_at("/org/freedesktop/portal/desktop", ScreenShotBackend)?
.serve_at("/org/freedesktop/portal/desktop", ScreenCastBackend)?
.serve_at("/org/freedesktop/portal/desktop", RemoteDesktopBackend)?
.serve_at("/org/freedesktop/portal/desktop", SettingsBackend::init())?
.build()
.await?;

Expand Down
74 changes: 74 additions & 0 deletions src/settings.rs
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
}
67 changes: 67 additions & 0 deletions src/settings/config.rs
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())
}
}

0 comments on commit d51084a

Please sign in to comment.