Commit fa059b3b authored by Bilal Elmoussaoui's avatar Bilal Elmoussaoui
Browse files

Use a color chooser dialog instead of popover

parent 7b63fb00
......@@ -53,12 +53,4 @@
.chart-bar-marker separator {
background-color: @theme_fg_color;
}
/* ColorPopover */
.colour-popover {
padding: 10px;
}
/*Hackish way to remove extra left spacing */
.colour-popover colorchooser overlay grid{
margin-left:-30px;
}
......@@ -15,6 +15,9 @@ pub fn calc_contrast_level(bg_color: &Rgb, fg_color: &Rgb) -> f64 {
}
pub fn parse(colour: &str) -> Result<Rgb, ColorError> {
// TODO: We only need this to convert GBA to HEX.
// I should replace that ASAP
// This could allow me to ditch colorsys dependency.
if colour.starts_with("#") {
match Rgb::from_hex_str(colour) {
Ok(rgb) => Ok(rgb),
......
use crate::colour;
use crate::colour_popover::ColourPopover;
use crate::gtk::Inhibit;
use crate::gtk::{BoxExt, ButtonExt, ColorChooserExt, EntryExt, PopoverExt, StyleContextExt, WidgetExt};
use crate::gtk::{BoxExt, ButtonExt, ColorChooserExt, DialogExt, EntryExt, GtkWindowExt, StyleContextExt, WidgetExt};
use std::str::FromStr;
#[derive(Clone)]
......@@ -11,17 +9,17 @@ pub struct ColourEntry {
}
impl ColourEntry {
pub fn new(label: String) -> ColourEntry {
pub fn new(label: String, parent: &gtk::ApplicationWindow) -> ColourEntry {
let entry = gtk::Entry::new();
let widget = gtk::Box::new(gtk::Orientation::Horizontal, 0);
let colour_entry = ColourEntry { widget, entry };
colour_entry.init(label);
colour_entry.init(label, parent);
colour_entry
}
fn init(&self, label: String) {
fn init(&self, label: String, parent: &gtk::ApplicationWindow) {
self.widget.set_halign(gtk::Align::Center);
self.widget.set_valign(gtk::Align::Center);
self.widget.get_style_context().add_class("linked");
......@@ -43,29 +41,27 @@ impl ColourEntry {
colour_selector.show();
self.widget.pack_start(&colour_selector, false, false, 0);
let colour_selector_popover = ColourPopover::new(colour_selector.clone());
let colour_selector_pop = colour_selector_popover.clone();
let parent_window = parent.clone();
let entry = self.entry.clone();
colour_selector.connect_clicked(move |_| {
colour_selector_pop.widget.popup();
let colour_chooser = gtk::ColorChooserDialog::new(Some(&label), Some(&parent_window));
colour_chooser.set_modal(true);
let colour_str = entry.get_text().unwrap();
match gdk::RGBA::from_str(&colour_str) {
Ok(rgba) => colour_selector_pop.color_chooser.set_rgba(&rgba),
Ok(rgba) => colour_chooser.set_rgba(&rgba),
Err(_) => error!("Failed to parse color {}", colour_str),
}
});
let colour_selector_pop = colour_selector_popover.clone();
let entry = self.entry.clone();
colour_selector_popover.color_chooser.connect_button_press_event(move |_, _| {
let colour = colour_selector_pop.color_chooser.get_rgba();
let colour_str = colour.to_string();
match colour::parse(&colour_str) {
Ok(rgb) => entry.set_text(&rgb.to_hex_string()),
Err(_) => error!("Failed to parse the colour {}", colour_str),
};
colour_selector_pop.widget.popdown();
Inhibit(false)
let entry = entry.clone();
colour_chooser.connect_response(move |dialog, res_type| {
if res_type == gtk::ResponseType::Ok {
let colour = colour::parse(&dialog.get_rgba().to_string()).unwrap();
entry.set_text(&colour.to_hex_string());
}
dialog.destroy();
});
colour_chooser.show();
});
self.widget.show();
}
......
use gtk::{BoxExt, ButtonExt, ColorChooserWidgetExt, ContainerExt, IsA, StyleContextExt, WidgetExt};
#[derive(Clone)]
pub struct ColourPopover {
pub widget: gtk::Popover,
pub color_chooser: gtk::ColorChooserWidget,
}
impl ColourPopover {
pub fn new<P: IsA<gtk::Widget>>(relative_to: P) -> ColourPopover {
let widget = gtk::Popover::new(Some(&relative_to));
widget.get_style_context().add_class("colour-popover");
let color_chooser = gtk::ColorChooserWidget::new();
let colour_popover = ColourPopover { widget, color_chooser };
colour_popover.setup_widget();
colour_popover
}
fn setup_widget(&self) {
let container = gtk::Box::new(gtk::Orientation::Vertical, 0);
let back_btn = gtk::Button::new();
back_btn.set_halign(gtk::Align::Start);
back_btn.set_valign(gtk::Align::Center);
let back_img = gtk::Image::new_from_icon_name(Some("go-previous-symbolic"), gtk::IconSize::Button);
back_img.show();
back_btn.set_image(Some(&back_img));
container.pack_start(&back_btn, false, false, 6);
container.pack_start(&self.color_chooser, false, false, 0);
let color_chooser = self.color_chooser.clone();
back_btn.connect_clicked(move |_| {
color_chooser.set_property_show_editor(false);
});
self.color_chooser.set_halign(gtk::Align::Start);
self.color_chooser.set_valign(gtk::Align::Center);
self.color_chooser.connect_property_show_editor_notify(move |color_chooser| {
back_btn.set_visible(color_chooser.get_property_show_editor());
});
self.color_chooser.show();
container.show();
self.widget.add(&container);
}
}
......@@ -12,7 +12,6 @@ use gettextrs::*;
mod application;
mod colour;
mod colour_entry;
mod colour_popover;
mod config;
mod contrast_level;
mod contrast_preview;
......
......@@ -39,7 +39,6 @@ sources = files(
'application.rs',
'colour.rs',
'colour_entry.rs',
'colour_popover.rs',
'config.rs',
'contrast_level.rs',
'contrast_preview.rs',
......
......@@ -33,9 +33,9 @@ impl Window {
let headerbar: libhandy::HeaderBar = builder.get_object("headerbar").unwrap();
let appmenu_button: gtk::MenuButton = builder.get_object("appmenu_button").unwrap();
let bg_entry = ColourEntry::new("Background Colour".to_string());
let bg_entry = ColourEntry::new("Background Colour".to_string(), &widget);
bg_entry.entry.set_name("bg");
let fg_entry = ColourEntry::new("Foreground Colour".to_string());
let fg_entry = ColourEntry::new("Foreground Colour".to_string(), &widget);
fg_entry.entry.set_name("fg");
let reverse_btn = gtk::Button::new();
......
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