Commit 461e1ca7 authored by Bilal Elmoussaoui's avatar Bilal Elmoussaoui
Browse files

Add levels markers to the new chartb bar

parent 71bb5bbf
Pipeline #99196 failed with stages
in 5 minutes and 51 seconds
......@@ -42,46 +42,44 @@
.main-window .main-container {
background: @background_color;
}
.main-window .level-box ,
.main-window .preview {
padding: 12px;
}
/* Preview */
.preview-head{
font-size: 100px;
font-weight: bolder;
color: @foreground_color;
}
/* Contrast Level */
.level-label {
font-size: 16px;
color: @foreground_color;
padding: 12px;
}
.levelbar trough{
/* ChartBarWidget */
.chart-bar{
border: 0;
padding: 4px;
background-color: darker(@background_color);
}
.levelbar trough block.filled {
.chart-bar .chart-block.filled {
margin-left:0;
margin-right:0;
border:0;
background-color: @foreground_color;
padding: 4px;
}
.levelbar trough block.empty {
border:0;
.chart-bar-marker label{
color: @foreground_color;
font-weight: 600;
font-size: 14px;
}
.levelbar trough block.filled:nth-child(3),
.levelbar trough block.filled:nth-child(7),
.levelbar trough block.filled:nth-child(22) {
border-left:2px solid @background_color;
.chart-bar-marker separator {
background-color: @background_color;
}
/* ColorPopover */
.colour-popover {
padding: 10px;
background: lighter(@background_color);
......
use crate::colour;
use crate::colour_popover::ColourPopover;
use crate::gtk::{BoxExt, ButtonExt, ColorChooserExt, DialogExt, EntryExt, GtkWindowExt, StyleContextExt, WidgetExt};
use colorsys::ColorAlpha;
use std::borrow::Borrow;
use crate::gtk::{BoxExt, ButtonExt, ColorChooserExt, EntryExt, PopoverExt, StyleContextExt, WidgetExt};
use std::str::FromStr;
#[derive(Clone)]
......@@ -12,18 +10,17 @@ pub struct ColourEntry {
}
impl ColourEntry {
pub fn new(label: String, parent: &gtk::ApplicationWindow) -> ColourEntry {
pub fn new(label: String) -> ColourEntry {
let entry = gtk::Entry::new();
let widget = gtk::Box::new(gtk::Orientation::Horizontal, 0);
let colour_entry = ColourEntry { widget, entry };
let window = parent.borrow();
colour_entry.init(label, window);
colour_entry.init(label);
colour_entry
}
fn init(&self, label: String, parent: &gtk::ApplicationWindow) {
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("linked");
......@@ -44,15 +41,13 @@ impl ColourEntry {
colour_selector.set_image(Some(&colour_selector_img));
colour_selector.show();
let window = parent.clone();
let title = label.clone();
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 entry = self.entry.clone();
colour_selector.connect_clicked(move |_| {
colour_selector_pop.widget.show();
colour_selector_pop.widget.popup();
let color_str = entry.get_text().unwrap();
match gdk::RGBA::from_str(&color_str) {
Ok(rgba) => colour_selector_pop.color_chooser.set_rgba(&rgba),
......@@ -64,7 +59,7 @@ impl ColourEntry {
colour_selector_popover.color_chooser.connect_color_activated(move |_, rgba| {
let colour = colour::parse(&rgba.to_string()).unwrap();
entry.set_text(&colour.to_hex_string());
colour_selector_pop.widget.hide();
colour_selector_pop.widget.popdown();
});
self.widget.show();
......
use crate::gtk::{BoxExt, LabelExt, LevelBarExt, StyleContextExt, WidgetExt};
use crate::glib::Cast;
use crate::gtk::{BoxExt, ContainerExt, LabelExt, OverlayExt, StyleContextExt, WidgetExt};
use libhandy::{Column, ColumnExt};
use std::cell::RefCell;
const CHART_BLOCK_WIDTH: i32 = 30;
const CHART_BLOCK_HEIGHT: i32 = 10;
const CHART_PADDING_X: i32 = 20;
const CHART_PADDING_Y: i32 = 60;
struct ChartBarMarker {
pub widget: gtk::Box,
}
impl ChartBarMarker {
pub fn new(marker_label: &str) -> ChartBarMarker {
let widget = gtk::Box::new(gtk::Orientation::Vertical, 0);
widget.get_style_context().add_class("chart-bar-marker");
let marker = ChartBarMarker { widget };
marker.setup_widgets(marker_label);
marker
}
fn setup_widgets(&self, marker_label: &str) {
self.widget.set_halign(gtk::Align::Start);
let label = gtk::Label::new(Some(marker_label));
label.show();
self.widget.pack_start(&label, false, false, 0);
let separator = gtk::Separator::new(gtk::Orientation::Vertical);
separator.set_halign(gtk::Align::Center);
separator.set_property_height_request(CHART_BLOCK_HEIGHT * 2 + 10);
separator.show();
self.widget.pack_start(&separator, false, false, 0);
self.widget.show();
}
}
#[derive(PartialEq)]
enum ChartBarState {
Empty,
Filled,
}
struct ChartBarBlock {
pub widget: gtk::Box,
state: ChartBarState,
}
impl ChartBarBlock {
pub fn new(width_request: i32, height_request: i32) -> ChartBarBlock {
let widget = gtk::Box::new(gtk::Orientation::Horizontal, 0);
let state = ChartBarState::Empty;
widget.get_style_context().add_class("chart-block");
widget.show();
widget.set_property_width_request(width_request);
widget.set_property_height_request(height_request);
ChartBarBlock { widget, state }
}
pub fn set_state(&mut self, state: ChartBarState) {
if state == ChartBarState::Empty {
self.widget.get_style_context().remove_class("empty");
self.widget.get_style_context().add_class("filled");
} else {
self.widget.get_style_context().remove_class("filled");
self.widget.get_style_context().add_class("empty");
}
self.state = state;
}
}
struct ChartBarWidget {
pub widget: Column,
overlay: gtk::Overlay,
min: f64,
max: f64,
value: f64,
blocks: Vec<RefCell<ChartBarBlock>>,
markers: Vec<RefCell<ChartBarMarker>>,
}
impl ChartBarWidget {
pub fn new(min: f64, max: f64) -> ChartBarWidget {
let widget = Column::new();;
let blocks = Vec::new();
let overlay = gtk::Overlay::new();
let markers = Vec::new();
let value = 0.0;
let mut chart_bar = ChartBarWidget {
widget,
overlay,
min,
max,
value,
blocks,
markers,
};
chart_bar.setup_widgets();
chart_bar
}
fn setup_widgets(&mut self) {
self.overlay.set_property_width_request((self.max * (CHART_BLOCK_WIDTH as f64)) as i32 + CHART_PADDING_X);
self.overlay.set_property_height_request(CHART_BLOCK_HEIGHT + CHART_PADDING_Y);
let chart_bar = gtk::Box::new(gtk::Orientation::Horizontal, 0);
chart_bar.set_valign(gtk::Align::Center);
chart_bar.get_style_context().add_class("chart-bar");
for _ in (self.min as i32)..(self.max as i32) {
let chart_block = ChartBarBlock::new(CHART_BLOCK_WIDTH, CHART_BLOCK_HEIGHT);
chart_bar.pack_start(&chart_block.widget, false, false, 0);
self.blocks.push(RefCell::new(chart_block));
}
chart_bar.show();
self.overlay.add(&chart_bar);
self.overlay.show();
self.widget.set_maximum_width(900);
// we can't call add as libhandy::Column is not seen as a gtkContainer
// Thanks to Jordan (Podcasts) for the tip :P
let widget = self.widget.clone();
let column = widget.upcast::<gtk::Widget>();
let column = column.downcast::<gtk::Container>().unwrap();
column.add(&self.overlay);
column.set_margin_bottom(24);
column.show();
}
pub fn set_value(&mut self, value: f64) {
self.value = value;
for (i, block) in self.blocks.iter().enumerate() {
if (value as usize) <= i {
block.borrow_mut().set_state(ChartBarState::Filled);
} else {
block.borrow_mut().set_state(ChartBarState::Empty);
}
}
}
pub fn add_marker_value(&mut self, label: &str, after_idx: i32) {
let marker = ChartBarMarker::new(label);
marker.widget.set_property_width_request((after_idx + 1) * CHART_BLOCK_WIDTH + CHART_PADDING_X);
self.overlay.add_overlay(&marker.widget);
self.markers.push(RefCell::new(marker));
}
}
pub struct ContrastLevelBar {
pub widget: gtk::Box,
levelbar: gtk::LevelBar,
chartbar: ChartBarWidget,
level_label: gtk::Label,
}
impl ContrastLevelBar {
pub fn new() -> ContrastLevelBar {
let widget = gtk::Box::new(gtk::Orientation::Vertical, 0);
let levelbar = gtk::LevelBar::new();
let chartbar = ChartBarWidget::new(0.0, 21.0);
let level_label = gtk::Label::new(Some(""));
let contrast_levelbar = ContrastLevelBar { widget, levelbar, level_label };
let mut contrast_levelbar = ContrastLevelBar { widget, chartbar, level_label };
contrast_levelbar.init();
contrast_levelbar
}
fn init(&self) {
fn init(&mut self) {
self.widget.set_halign(gtk::Align::Fill);
self.widget.set_vexpand(true);
self.widget.get_style_context().add_class("level-box");
self.levelbar.get_style_context().add_class("levelbar");
self.levelbar.set_min_value(0.0);
self.levelbar.set_max_value(21.0);
self.levelbar.add_offset_value("AA-LARGE", 4.5);
self.levelbar.add_offset_value("AA", 7.0);
self.levelbar.add_offset_value("AAA", 21.0);
self.levelbar.set_property_width_request(400);
self.levelbar.set_property_height_request(10);
self.chartbar.add_marker_value("A", 4);
self.chartbar.add_marker_value("AA", 7);
self.chartbar.add_marker_value("AAA", 21);
self.levelbar.set_halign(gtk::Align::Center);
self.levelbar.show();
self.widget.pack_start(&self.levelbar, false, false, 0);
self.widget.pack_start(&self.chartbar.widget, false, false, 0);
self.level_label.get_style_context().add_class("level-label");
self.level_label.show();
......@@ -44,7 +188,7 @@ impl ContrastLevelBar {
}
pub fn set_contrast_level(&mut self, contrast_level: f64) {
self.levelbar.set_value(contrast_level);
self.chartbar.set_value(contrast_level);
let level_label = format!("{:.1} out of 21", contrast_level);
self.level_label.set_text(&level_label);
}
......
......@@ -28,9 +28,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(), &widget);
let bg_entry = ColourEntry::new("Background Colour".to_string());
bg_entry.entry.set_name("bg");
let fg_entry = ColourEntry::new("Foreground Colour".to_string(), &widget);
let fg_entry = ColourEntry::new("Foreground Colour".to_string());
fg_entry.entry.set_name("fg");
let state = Rc::new(RefCell::new(WindowState::init(settings)));
......
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