Commit 33b28cc6 authored by Bilal Elmoussaoui's avatar Bilal Elmoussaoui
Browse files

replace dbus-rs with zbus <3

parent 316b5bd6
This diff is collapsed.
......@@ -16,6 +16,7 @@ libhandy = { git = "https://gitlab.gnome.org/World/Rust/libhandy-rs.git" }
pretty_env_logger = "0.4"
pango = "0.9"
sass-rs = "0.2"
dbus = "0.8"
zbus = "1.0"
zvariant = "2.0"
rand = "0.7"
gtk-macros = "0.2"
use crate::application::Action;
use dbus::{
blocking::{BlockingSender, Connection},
message::Message,
};
use sass_rs::{compile_string, Options};
use std::collections::HashMap;
use std::thread;
use std::time::Duration;
use zbus::dbus_proxy;
pub fn calc_contrast_level(bg_color: &gdk::RGBA, fg_color: &gdk::RGBA) -> f64 {
let bg_luminance = get_luminance(bg_color);
......@@ -43,53 +39,31 @@ fn to_hex(n: f64) -> String {
}
}
#[derive(Debug)]
pub enum ColorPickerError {
DBus(dbus::Error),
NoResponse,
#[dbus_proxy(interface = "org.gnome.Shell.Screenshot", default_path = "/org/gnome/Shell/Screenshot")]
trait Screenshot {
fn pick_color(&self) -> zbus::Result<HashMap<String, zvariant::OwnedValue>>;
}
impl From<dbus::Error> for ColorPickerError {
fn from(s: dbus::Error) -> ColorPickerError {
ColorPickerError::DBus(s)
}
}
impl From<String> for ColorPickerError {
fn from(_s: String) -> ColorPickerError {
ColorPickerError::NoResponse
}
}
impl std::fmt::Display for ColorPickerError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "failed to pick a color")
}
}
impl std::error::Error for ColorPickerError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
// Generic error, underlying cause isn't tracked.
None
}
}
pub fn pick_color() -> Result<gdk::RGBA, ColorPickerError> {
let connection = Connection::new_session()?;
let message = Message::new_method_call("org.gnome.Shell.Screenshot", "/org/gnome/Shell/Screenshot", "org.gnome.Shell.Screenshot", "PickColor")?;
let response = connection.send_with_reply_and_block(message, Duration::from_secs(60))?;
let result: HashMap<String, dbus::arg::Variant<Box<dyn dbus::arg::RefArg>>> = response.get1().ok_or_else(|| ColorPickerError::NoResponse)?;
let mut color = result
pub fn pick_color() -> zbus::Result<gdk::RGBA> {
let connection = zbus::Connection::new_session()?;
let proxy = ScreenshotProxy::new(&connection)?;
let reply = proxy.pick_color()?;
let color = reply
.get("color")
.ok_or_else(|| ColorPickerError::NoResponse)?
.0
.as_iter()
.ok_or_else(|| ColorPickerError::NoResponse)?;
let red = color.next().ok_or_else(|| ColorPickerError::NoResponse)?.as_f64().ok_or_else(|| ColorPickerError::NoResponse)?;
let green = color.next().ok_or_else(|| ColorPickerError::NoResponse)?.as_f64().ok_or_else(|| ColorPickerError::NoResponse)?;
let blue = color.next().ok_or_else(|| ColorPickerError::NoResponse)?.as_f64().ok_or_else(|| ColorPickerError::NoResponse)?;
let rgba = gdk::RGBA { red, green, blue, alpha: 1.0 };
.unwrap()
.downcast_ref::<zvariant::Structure>()
.unwrap()
.fields()
.into_iter()
.map(|c| *c.downcast_ref::<f64>().unwrap())
.collect::<Vec<f64>>();
let rgba = gdk::RGBA {
red: color[0],
green: color[1],
blue: color[2],
alpha: 1.0,
};
Ok(rgba)
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment