Commit 9cc3f137 authored by Bilal Elmoussaoui's avatar Bilal Elmoussaoui
Browse files

use the portal for picking the color

fixes #6
parent 2ded8a63
This diff is collapsed.
......@@ -16,7 +16,6 @@ libhandy = "1.0.0-alpha"
pretty_env_logger = "0.4"
pango = "0.9"
sass-rs = "0.2"
zbus = "1.0"
zvariant = "2.0"
rand = "0.7"
gtk-macros = "0.2"
ashpd = { version = "0.1", features = ["gdk_color"] }
......@@ -14,8 +14,7 @@
"--share=ipc",
"--socket=fallback-x11",
"--socket=wayland",
"--device=dri",
"--talk-name=org.gnome.Shell.Screenshot"
"--device=dri"
],
"build-options": {
"append-path": "/usr/lib/sdk/rust-stable/bin",
......
use crate::application::Action;
use sass_rs::{compile_string, Options};
use std::collections::HashMap;
use std::thread;
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);
......@@ -39,33 +37,6 @@ fn to_hex(n: f64) -> String {
}
}
#[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>>;
}
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")
.unwrap()
.downcast_ref::<zvariant::Structure>()
.unwrap()
.fields()
.iter()
.map(|c| *c.downcast_ref::<f64>().unwrap())
.collect::<Vec<f64>>();
Ok(gdk::RGBA {
red: color[0],
green: color[1],
blue: color[2],
alpha: 1.0,
})
}
pub fn compile_styles(sender: &glib::Sender<Action>, fg: gdk::RGBA, bg: gdk::RGBA) {
thread::spawn(clone!(@strong sender => move || {
let colours = format!(include_str!("Adwaita/_colors.scss"), fg = fg.to_string(), bg = bg.to_string());
......
......@@ -4,6 +4,9 @@ use gtk::prelude::*;
use gtk::Inhibit;
use std::str::FromStr;
use ashpd::desktop::screenshot::{Color, PickColorOptions, ScreenshotProxy};
use ashpd::{zbus, RequestProxy, Response, WindowIdentifier};
use glib::subclass;
use glib::subclass::prelude::*;
use glib::translate::*;
......@@ -83,9 +86,25 @@ impl ObjectImpl for ColourEntryPrivate {
self_.pack_start(&colour_selector, false, false, 0);
colour_selector.connect_clicked(clone!(@weak self.entry as entry => move |_| {
match colour::pick_color() {
Ok(rgba) => entry.set_text(&colour::rgba_to_hex(&rgba)),
Err(why) => error!("Failed to pick a color {}", why),
let pick_color = || -> zbus::fdo::Result<()> {
let connection = zbus::Connection::new_session()?;
let proxy = ScreenshotProxy::new(&connection)?;
let request_handle = proxy.pick_color(
WindowIdentifier::default(),
PickColorOptions::default()
)?;
let request = RequestProxy::new(&connection, &request_handle)?;
request.on_response(|response: Response<Color>| {
if let Ok(color) = response {
let rgba : gdk::RGBA = (&color).into();
entry.set_text(&colour::rgba_to_hex(&rgba))
}
})?;
Ok(())
};
if let Err(e) = pick_color() {
error!("Failed to pick the color {}", e);
}
}));
self_.show();
......
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