Commit 0139d9ef authored by Bilal Elmoussaoui's avatar Bilal Elmoussaoui
Browse files

subclass contrast preview

parent 8bb01b80
......@@ -28,7 +28,7 @@ entry selection{
color: @foreground_color;
padding-bottom: 6px;
}
.preview-comment {
.preview-body {
padding: 6px;
}
/* ChartBarWidget */
......
use gettextrs::gettext;
use glib::subclass;
use glib::subclass::prelude::*;
use glib::translate::*;
use gtk::prelude::*;
use gtk::subclass::prelude::{BoxImpl, ContainerImpl, WidgetImpl};
pub struct ContrastPreview {
pub widget: gtk::Box,
pub struct ContrastPreviewPrivate {
head_label: gtk::Label,
comment_label: gtk::Label,
body_label: gtk::Label,
}
impl ContrastPreview {
pub fn new() -> ContrastPreview {
let widget = gtk::Box::new(gtk::Orientation::Vertical, 0);
impl ObjectSubclass for ContrastPreviewPrivate {
const NAME: &'static str = "ContrastPreview";
type ParentType = gtk::Box;
type Instance = subclass::simple::InstanceStruct<Self>;
type Class = subclass::simple::ClassStruct<Self>;
let head_label = gtk::Label::new(Some(""));
let comment_label = gtk::Label::new(Some(""));
let preview = ContrastPreview { widget, head_label, comment_label };
glib_object_subclass!();
preview.setup_widget();
preview
fn new() -> Self {
let head_label = gtk::Label::new(None);
let body_label = gtk::Label::new(None);
Self { head_label, body_label }
}
}
impl ObjectImpl for ContrastPreviewPrivate {
glib_object_impl!();
fn constructed(&self, obj: &glib::Object) {
self.parent_constructed(obj);
let self_ = obj.downcast_ref::<gtk::Box>().unwrap();
fn setup_widget(&self) {
self.widget.set_vexpand(true);
self.widget.set_valign(gtk::Align::Center);
self.widget.get_style_context().add_class("preview");
self_.set_orientation(gtk::Orientation::Vertical);
self_.set_vexpand(true);
self_.set_valign(gtk::Align::Center);
self_.get_style_context().add_class("preview");
self.head_label.get_style_context().add_class("preview-head");
self.head_label.set_halign(gtk::Align::Center);
self.head_label.set_valign(gtk::Align::Start);
self.head_label.show();
self.comment_label.get_style_context().add_class("preview-comment");
self.comment_label.set_halign(gtk::Align::Center);
self.comment_label.set_valign(gtk::Align::Start);
self.comment_label.show();
self.body_label.get_style_context().add_class("preview-body");
self.body_label.set_halign(gtk::Align::Center);
self.body_label.set_valign(gtk::Align::Start);
self.body_label.show();
self_.pack_start(&self.head_label, false, false, 0);
self_.pack_start(&self.body_label, false, false, 0);
self_.show();
}
}
impl WidgetImpl for ContrastPreviewPrivate {}
impl ContainerImpl for ContrastPreviewPrivate {}
impl BoxImpl for ContrastPreviewPrivate {}
glib_wrapper! {
pub struct ContrastPreview(
Object<subclass::simple::InstanceStruct<ContrastPreviewPrivate>,
subclass::simple::ClassStruct<ContrastPreviewPrivate>,
BoxClass>)
@extends gtk::Widget, gtk::Container, gtk::Box;
self.widget.pack_start(&self.head_label, false, false, 0);
self.widget.pack_start(&self.comment_label, false, false, 0);
match fn {
get_type => || ContrastPreviewPrivate::get_type().to_glib(),
}
}
self.widget.show();
impl ContrastPreview {
pub fn new() -> Self {
glib::Object::new(ContrastPreview::static_type(), &[]).unwrap().downcast::<ContrastPreview>().unwrap()
}
pub fn set_contrast_level(&self, contrast_level: f64) {
let self_ = ContrastPreviewPrivate::from_instance(self);
if 0.0 <= contrast_level && contrast_level < 3.0 {
self.head_label.set_text(&gettext("Nope"));
self.comment_label.set_text(&gettext("This color combination doesn’t have enough contrast to be legible."));
self_.head_label.set_text(&gettext("Nope"));
self_.body_label.set_text(&gettext("This color combination doesn’t have enough contrast to be legible."));
} else if 3.0 <= contrast_level && contrast_level < 4.5 {
self.head_label.set_text(&gettext("Not bad"));
self.comment_label.set_text(&gettext("This color combination can work, but only at large text sizes."));
self_.head_label.set_text(&gettext("Not bad"));
self_.body_label.set_text(&gettext("This color combination can work, but only at large text sizes."));
} else if 4.5 <= contrast_level && contrast_level < 7.0 {
self.head_label.set_text(&gettext("Pretty good"));
self.comment_label.set_text(&gettext("This color combination should work OK in most cases."));
self_.head_label.set_text(&gettext("Pretty good"));
self_.body_label.set_text(&gettext("This color combination should work OK in most cases."));
} else if 7.0 <= contrast_level && contrast_level <= 21.0 {
self.head_label.set_text(&gettext("Awesome"));
self.comment_label.set_text(&gettext("This color combination has great contrast."));
self_.head_label.set_text(&gettext("Awesome"));
self_.body_label.set_text(&gettext("This color combination has great contrast."));
}
}
}
......@@ -21,7 +21,7 @@ pub struct Window {
reverse_btn: gtk::Button,
pub state: Rc<RefCell<WindowState>>,
pub levelbar: RefCell<ContrastLevelBar>,
pub preview: RefCell<ContrastPreview>,
pub preview: ContrastPreview,
sender: glib::Sender<Action>,
}
......@@ -39,7 +39,7 @@ impl Window {
let state = Rc::new(RefCell::new(WindowState::init(settings)));
let levelbar = RefCell::new(ContrastLevelBar::new());
let preview = RefCell::new(ContrastPreview::new());
let preview = ContrastPreview::new();
let window = Rc::new(Window {
widget: window_widget,
......@@ -64,7 +64,7 @@ impl Window {
let contrast_level = colour::calc_contrast_level(&bg_rgba, &fg_rgba);
self.levelbar.borrow_mut().set_contrast_level(contrast_level);
self.preview.borrow_mut().set_contrast_level(contrast_level);
self.preview.set_contrast_level(contrast_level);
colour::compile_styles(&self.sender, fg_rgba, bg_rgba);
}
......@@ -140,7 +140,7 @@ impl Window {
get_widget!(self.builder, libhandy::HeaderBar, headerbar);
headerbar.set_custom_title(Some(&entries_container));
container.pack_start(&self.preview.borrow().widget, true, true, 12);
container.pack_start(&self.preview, true, true, 12);
container.pack_start(&self.levelbar.borrow().widget, true, true, 12);
self.widget.add(&container);
......
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