Commit 24d4921e authored by Bilal Elmoussaoui's avatar Bilal Elmoussaoui
Browse files

subclass colour entry

parent 0139d9ef
......@@ -4,31 +4,44 @@ use gtk::prelude::*;
use gtk::Inhibit;
use std::str::FromStr;
#[derive(Clone)]
pub struct ColourEntry {
pub widget: gtk::Box,
use glib::subclass;
use glib::subclass::prelude::*;
use glib::translate::*;
use gtk::subclass::prelude::{BoxImpl, ContainerImpl, WidgetImpl};
pub struct ColourEntryPrivate {
pub entry: gtk::Entry,
}
impl ColourEntry {
pub fn new(label: String) -> ColourEntry {
let entry = gtk::Entry::new();
let widget = gtk::Box::new(gtk::Orientation::Horizontal, 0);
impl ObjectSubclass for ColourEntryPrivate {
const NAME: &'static str = "ColourEntry";
type ParentType = gtk::Box;
type Instance = subclass::simple::InstanceStruct<Self>;
type Class = subclass::simple::ClassStruct<Self>;
glib_object_subclass!();
let colour_entry = ColourEntry { widget, entry };
fn new() -> Self {
let entry = gtk::Entry::new();
colour_entry.init(label);
colour_entry
Self { entry }
}
}
impl ObjectImpl for ColourEntryPrivate {
glib_object_impl!();
fn constructed(&self, obj: &glib::Object) {
self.parent_constructed(obj);
let self_ = obj.downcast_ref::<gtk::Box>().unwrap();
fn init(&self, label: String) {
self.widget.set_halign(gtk::Align::Center);
self.widget.set_valign(gtk::Align::Center);
self.widget.get_style_context().add_class(&gtk::STYLE_CLASS_LINKED);
self.widget.set_hexpand(true);
self.widget.set_tooltip_text(Some(&label));
self_.set_halign(gtk::Align::Center);
self_.set_valign(gtk::Align::Center);
self_.get_style_context().add_class(&gtk::STYLE_CLASS_LINKED);
self_.set_hexpand(true);
// self_.set_tooltip_text(Some(&label));
self.entry.set_placeholder_text(Some(label.as_str()));
//self.entry.set_placeholder_text(Some(label.as_str()));
self.entry.set_halign(gtk::Align::Center);
self.entry.set_valign(gtk::Align::Center);
self.entry.set_property_primary_icon_name(Some("circle-symbolic"));
......@@ -61,7 +74,7 @@ impl ColourEntry {
colour_popover.widget.popdown();
Inhibit(false)
}));
self.widget.pack_start(&self.entry, false, false, 0);
self_.pack_start(&self.entry, false, false, 0);
let colour_selector = gtk::Button::new();
let colour_selector_img = gtk::Image::new_from_icon_name(Some("color-select-symbolic"), gtk::IconSize::Button);
......@@ -69,23 +82,55 @@ impl ColourEntry {
colour_selector.set_image(Some(&colour_selector_img));
colour_selector.show();
self.widget.pack_start(&colour_selector, false, false, 0);
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(_) => warn!("Failed to pick a color"),
}
}));
self.widget.show();
self_.show();
}
}
impl WidgetImpl for ColourEntryPrivate {}
impl ContainerImpl for ColourEntryPrivate {}
impl BoxImpl for ColourEntryPrivate {}
glib_wrapper! {
pub struct ColourEntry(
Object<subclass::simple::InstanceStruct<ColourEntryPrivate>,
subclass::simple::ClassStruct<ColourEntryPrivate>,
BoxClass>)
@extends gtk::Widget, gtk::Container, gtk::Box;
match fn {
get_type => || ColourEntryPrivate::get_type().to_glib(),
}
}
impl ColourEntry {
pub fn new(placeholder: &str) -> Self {
glib::Object::new(ColourEntry::static_type(), &[("placeholder", &placeholder.to_value())])
.unwrap()
.downcast::<ColourEntry>()
.unwrap()
}
pub fn get_entry(&self) -> gtk::Entry {
let self_ = ColourEntryPrivate::from_instance(self);
self_.entry.clone()
}
pub fn get_text(&self) -> String {
self.entry.get_text().unwrap().to_string()
let self_ = ColourEntryPrivate::from_instance(self);
self_.entry.get_text().unwrap().to_string()
}
pub fn set_text(&self, colour: Option<String>) {
let self_ = ColourEntryPrivate::from_instance(self);
if let Some(colour) = colour {
self.entry.set_text(&colour)
self_.entry.set_text(&colour)
}
}
}
......@@ -31,8 +31,8 @@ impl Window {
let builder = gtk::Builder::new_from_resource("/org/gnome/design/Contrast/window.ui");
get_widget!(builder, gtk::ApplicationWindow, window_widget);
let bg_entry = ColourEntry::new(gettext("Background Colour"));
let fg_entry = ColourEntry::new(gettext("Foreground Colour"));
let bg_entry = ColourEntry::new(&gettext("Background Colour"));
let fg_entry = ColourEntry::new(&gettext("Foreground Colour"));
let reverse_btn = gtk::Button::new();
......@@ -87,23 +87,23 @@ impl Window {
window.colour_changed(bg_colour, fg_colour);
});
let bg_handle = self.bg_entry.entry.connect_changed(on_entry_changed.clone());
let fg_handle = self.fg_entry.entry.connect_changed(on_entry_changed);
let bg_handle = self.bg_entry.get_entry().connect_changed(on_entry_changed.clone());
let fg_handle = self.fg_entry.get_entry().connect_changed(on_entry_changed);
let actions = gio::SimpleActionGroup::new();
action!(
actions,
"reverse-colors",
clone!(@strong self.fg_entry as fg_entry, @strong self.bg_entry as bg_entry => move |_, _| {
fg_entry.entry.block_signal(&fg_handle);
bg_entry.entry.block_signal(&bg_handle);
fg_entry.get_entry().block_signal(&fg_handle);
bg_entry.get_entry().block_signal(&bg_handle);
let fg_colour = fg_entry.get_text();
let bg_colour = bg_entry.get_text();
fg_entry.set_text(Some(bg_colour.clone()));
bg_entry.set_text(Some(fg_colour.clone()));
fg_entry.entry.unblock_signal(&fg_handle);
bg_entry.entry.unblock_signal(&bg_handle);
fg_entry.get_entry().unblock_signal(&fg_handle);
bg_entry.get_entry().unblock_signal(&bg_handle);
window.colour_changed(fg_colour, bg_colour);
})
......@@ -123,8 +123,8 @@ impl Window {
container.get_style_context().add_class("main-container");
container.set_valign(gtk::Align::Center);
self.fg_entry.entry.get_style_context().add_class("fg-entry");
self.bg_entry.entry.get_style_context().add_class("bg-entry");
self.fg_entry.get_entry().get_style_context().add_class("fg-entry");
self.bg_entry.get_entry().get_style_context().add_class("bg-entry");
self.reverse_btn.set_halign(gtk::Align::Center);
self.reverse_btn.set_action_name(Some("window.reverse-colors"));
......@@ -132,9 +132,9 @@ impl Window {
self.reverse_btn.set_image(Some(&reverse_img));
let entries_container = gtk::Box::new(gtk::Orientation::Horizontal, 0);
entries_container.pack_start(&self.bg_entry.widget, false, false, 6);
entries_container.pack_start(&self.bg_entry, false, false, 6);
entries_container.pack_start(&self.reverse_btn, false, false, 6);
entries_container.pack_start(&self.fg_entry.widget, false, false, 6);
entries_container.pack_start(&self.fg_entry, false, false, 6);
entries_container.set_halign(gtk::Align::Center);
get_widget!(self.builder, libhandy::HeaderBar, headerbar);
......
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