Commit 9054bb3c authored by Bilal Elmoussaoui's avatar Bilal Elmoussaoui
Browse files

Clean up channels code

parent d1e04dd2
Pipeline #113647 passed with stages
in 7 minutes and 50 seconds
......@@ -7,7 +7,7 @@ variables:
BUNDLE: "contrast-nightly.flatpak"
flatpak:
image: "registry.gitlab.gnome.org/gnome/gnome-runtime-images/rust_bundle:master"
image: "registry.gitlab.gnome.org/gnome/gnome-runtime-images/rust_bundle:3.32"
stage: "test"
variables:
MANIFEST_PATH: "build-aux/org.gnome.design.ContrastDevel.json"
......
{
"app-id": "org.gnome.design.Contrast",
"runtime": "org.gnome.Platform",
"runtime-version": "3.32",
"sdk": "org.gnome.Sdk",
"sdk-extensions": [
"org.freedesktop.Sdk.Extension.rust-stable"
],
"command": "contrast",
"finish-args": [
"--share=ipc",
"--socket=x11",
"--socket=wayland",
"--device=dri",
"--filesystem=xdg-run/dconf",
"--filesystem=~/.config/dconf:ro",
"--talk-name=ca.desrt.dconf",
"--env=DCONF_USER_CONFIG_DIR=.config/dconf",
"--talk-name=org.gnome.Shell.Screenshot"
],
"build-options": {
"append-path": "/usr/lib/sdk/rust-stable/bin",
"env": {
"CARGO_HOME": "/run/build/contrast/cargo"
}
},
"modules": [{
"name": "libhandy",
"buildsystem": "meson",
"config-opts": [
"-Dintrospection=disabled",
"-Dgtk_doc=false",
"-Dtests=false",
"-Dexamples=false",
"-Dvapi=false",
"-Dglade_catalog=disabled"
],
"cleanup": [
"/include",
"/lib/pkgconfig",
"/bin"
],
"sources": [{
"type": "git",
"url": "https://source.puri.sm/Librem5/libhandy.git",
"tag": "v0.0.10",
"commit": "2d777677352d037b6f5cc24d9c1c8d9a74ac0ded"
}]
},
{
"name": "contrast",
"buildsystem": "meson",
"sources": [{
"type": "archive",
"url": "https://gitlab.gnome.org/World/design/contrast/uploads/c69c4bac5fab625dfe214042406bd7ea/contrast-0.0.1.tar.xz",
"sha256": "14cab145a579e7f424471559ae5c0988232f2c4bbb8d06348fa765e70a305dfb"
}]
}
]
}
{
"app-id": "org.gnome.design.ContrastDevel",
"runtime": "org.gnome.Platform",
"runtime-version": "master",
"runtime-version": "3.32",
"sdk": "org.gnome.Sdk",
"sdk-extensions": [
"org.freedesktop.Sdk.Extension.rust-stable"
......
......@@ -14,7 +14,7 @@ dependency('libhandy-0.0', version: '>= 0.0.10')
find_program('cargo', required: false)
find_program('glib-compile-resources', required: true)
find_program('cargo-vendor', required: false)
glib_compile_schemas = find_program('glib-compile-schemas', required: true)
desktop_file_validate = find_program('desktop-file-validate', required: false)
appstream_util = find_program('appstream-util', required: false)
......
......@@ -3,11 +3,13 @@ use super::config;
use super::styles::update_styles;
use super::window::Window;
use gio::prelude::*;
use glib::{Receiver, Sender};
use gtk::prelude::*;
use gtk::SettingsExt;
use std::env;
use std::{cell::RefCell, rc::Rc};
pub enum Message {
pub enum Action {
ApplyStylesheet(String),
ColorChanged,
}
......@@ -16,15 +18,16 @@ pub struct Application {
app: gtk::Application,
window: Window,
provider: gtk::CssProvider,
sender: glib::Sender<Message>,
sender: Sender<Action>,
receiver: RefCell<Option<Receiver<Action>>>,
}
impl Application {
pub fn new() -> Self {
pub fn new() -> Rc<Self> {
let app = gtk::Application::new(Some(config::APP_ID), gio::ApplicationFlags::FLAGS_NONE).unwrap();
let (sender, receiver) = glib::MainContext::channel(glib::PRIORITY_DEFAULT);
let (sender, r) = glib::MainContext::channel(glib::PRIORITY_DEFAULT);
let receiver = RefCell::new(Some(r));
let window = Window::new(sender.clone());
glib::set_application_name(&format!("{}Contrast", config::NAME_PREFIX));
......@@ -33,43 +36,23 @@ impl Application {
let builder = gtk::Builder::new_from_resource("/org/gnome/design/Contrast/shortcuts.ui");
let dialog: gtk::ShortcutsWindow = builder.get_object("shortcuts").unwrap();
window.widget.set_help_overlay(Some(&dialog));
let provider = gtk::CssProvider::new();
let sender_rec = sender.clone();
let provider_rec = provider.clone();
let levelbar = window.levelbar.clone();
let preview = window.preview.clone();
let window_state = window.state.clone();
receiver.attach(None, move |msg| {
match msg {
Message::ApplyStylesheet(text) => {
match provider_rec.load_from_data(text.as_bytes()) {
Ok(_) => (),
Err(err) => error!("Stylesheet couldn't be updated {}", err),
};
}
Message::ColorChanged => {
let fg_colour = window_state.borrow().fg_colour;
let bg_colour = window_state.borrow().bg_colour;
let contrast_level = colour::calc_contrast_level(&bg_colour, &fg_colour);
levelbar.borrow_mut().set_contrast_level(contrast_level);
preview.borrow_mut().set_contrast_level(contrast_level);
update_styles(&sender_rec, fg_colour, bg_colour);
}
}
glib::Continue(true)
let application = Rc::new(Self {
app,
window,
provider,
sender,
receiver,
});
let application = Self { app, window, provider, sender };
application.setup_gactions();
application.setup_signals();
application.setup_css();
application
}
pub fn setup_gactions(&self) {
fn setup_gactions(&self) {
// Quit
let app = self.app.clone();
let simple_action = gio::SimpleAction::new("quit", None);
......@@ -93,7 +76,7 @@ impl Application {
self.app.add_action(&simple_action);
}
pub fn setup_signals(&self) {
fn setup_signals(&self) {
let window = self.window.widget.clone();
self.app.connect_activate(move |app| {
window.set_application(Some(app));
......@@ -102,7 +85,7 @@ impl Application {
});
}
pub fn setup_css(&self) {
fn setup_css(&self) {
if let Some(gtk_settings) = gtk::Settings::get_default() {
gtk_settings.set_property_gtk_theme_name(Some("Adwaita"));
}
......@@ -116,14 +99,40 @@ impl Application {
gtk::CssProvider::load_from_resource(&p, "/org/gnome/design/Contrast/style.css");
gtk::StyleContext::add_provider_for_screen(&gdk::Screen::get_default().unwrap(), &p, 500);
self.sender.send(Message::ColorChanged).ok(); // never fails
self.sender.send(Action::ColorChanged).ok(); // never fails
}
pub fn run(&self) {
fn do_action(&self, action: Action) -> glib::Continue {
match action {
Action::ApplyStylesheet(text) => {
match self.provider.load_from_data(text.as_bytes()) {
Ok(_) => (),
Err(err) => error!("Stylesheet couldn't be updated {}", err),
};
}
Action::ColorChanged => {
let fg_colour = self.window.state.borrow().fg_colour;
let bg_colour = self.window.state.borrow().bg_colour;
let contrast_level = colour::calc_contrast_level(&bg_colour, &fg_colour);
self.window.levelbar.borrow_mut().set_contrast_level(contrast_level);
self.window.preview.borrow_mut().set_contrast_level(contrast_level);
update_styles(&self.sender, fg_colour, bg_colour);
}
};
glib::Continue(true)
}
pub fn run(&self, app: Rc<Self>) {
info!("{}Contrast ({})", config::NAME_PREFIX, config::APP_ID);
info!("Version: {} ({})", config::VERSION, config::PROFILE);
info!("Datadir: {}", config::PKGDATADIR);
let app = app.clone();
let receiver = self.receiver.borrow_mut().take().unwrap();
receiver.attach(None, move |action| app.do_action(action));
let args: Vec<String> = env::args().collect();
self.app.run(&args);
}
......
......@@ -32,5 +32,5 @@ fn main() {
static_resources::init().expect("Failed to initialize the resource file.");
let app = Application::new();
app.run();
app.run(app.clone());
}
......@@ -7,9 +7,9 @@ global_conf.set_quoted('VERSION', version + version_suffix)
global_conf.set_quoted('GETTEXT_PACKAGE', gettext_package)
global_conf.set_quoted('LOCALEDIR', localedir)
config_output_file = configure_file(
input: 'config.rs.in',
output: 'config.rs',
configuration: global_conf
input: 'config.rs.in',
output: 'config.rs',
configuration: global_conf
)
# Copy the config.rs output to the source directory.
run_command(
......@@ -36,18 +36,18 @@ run_command(
)
sources = files(
'application.rs',
'colour.rs',
'colour_entry.rs',
'colour_popover.rs',
'config.rs',
'contrast_level.rs',
'contrast_preview.rs',
'main.rs',
'static_resources.rs',
'styles.rs',
'window.rs',
'window_state.rs',
'application.rs',
'colour.rs',
'colour_entry.rs',
'colour_popover.rs',
'config.rs',
'contrast_level.rs',
'contrast_preview.rs',
'main.rs',
'static_resources.rs',
'styles.rs',
'window.rs',
'window_state.rs',
)
cargo_release = custom_target(
......
use crate::application::Message;
use crate::application::Action;
use crate::colour;
use sass_rs::{compile_string, Options};
use std::thread;
pub fn update_styles(sender: &glib::Sender<Message>, fg: gdk::RGBA, bg: gdk::RGBA) {
pub fn update_styles(sender: &glib::Sender<Action>, fg: gdk::RGBA, bg: gdk::RGBA) {
let sender = sender.clone();
let res = thread::Builder::new().name("style worker".to_string()).spawn(move || {
......@@ -22,7 +22,7 @@ pub fn update_styles(sender: &glib::Sender<Message>, fg: gdk::RGBA, bg: gdk::RGB
scss.push_str(&format!("@define-color foreground_color {};", fg.to_string()));
match compile_string(&scss, Options::default()) {
Ok(styles) => {
if let Err(err) = sender.send(Message::ApplyStylesheet(styles)) {
if let Err(err) = sender.send(Action::ApplyStylesheet(styles)) {
error!("Can't update styles! {}", err);
}
}
......
use super::application::Message;
use super::application::Action;
use super::colour_entry::ColourEntry;
use super::config::{APP_ID, PROFILE};
use super::contrast_level::ContrastLevelBar;
......@@ -22,11 +22,11 @@ pub struct Window {
pub state: Rc<RefCell<WindowState>>,
pub levelbar: Rc<RefCell<ContrastLevelBar>>,
pub preview: Rc<RefCell<ContrastPreview>>,
sender: glib::Sender<Message>,
sender: glib::Sender<Action>,
}
impl Window {
pub fn new(sender: glib::Sender<Message>) -> Self {
pub fn new(sender: glib::Sender<Action>) -> Self {
let settings = gio::Settings::new(APP_ID);
let builder = gtk::Builder::new_from_resource("/org/gnome/design/Contrast/window.ui");
let widget: gtk::ApplicationWindow = builder.get_object("window").unwrap();
......@@ -66,7 +66,7 @@ impl Window {
window
}
pub fn init(&self) {
fn init(&self) {
self.widget.get_style_context().add_class("main-window");
// setup app menu
let menu_builder = gtk::Builder::new_from_resource("/org/gnome/design/Contrast/menu.ui");
......@@ -94,7 +94,7 @@ impl Window {
state.borrow_mut().set_bg_colour(Some(&bg_colour));
state.borrow_mut().set_fg_colour(Some(&fg_colour));
sender.send(Message::ColorChanged).ok(); // never fails
sender.send(Action::ColorChanged).ok(); // never fails
};
let bg_handle = self.bg_entry.entry.connect_changed(on_entry_changed.clone());
......@@ -120,7 +120,7 @@ impl Window {
state.borrow_mut().set_bg_colour(Some(&fg_colour));
state.borrow_mut().set_fg_colour(Some(&bg_colour));
sender.send(Message::ColorChanged).ok(); // never fails
sender.send(Action::ColorChanged).ok(); // never fails
});
actions.add_action(&reverse_colors);
......
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