Skip to content

Commit

Permalink
Table row styles should be in the same module as the theme.
Browse files Browse the repository at this point in the history
  • Loading branch information
flomang committed Nov 26, 2022
1 parent 91b1570 commit 02e3216
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 148 deletions.
2 changes: 2 additions & 0 deletions crates/core/src/theme/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub mod container;
pub mod modal;
pub mod picklist;
pub mod scrollable;
pub mod table_row;
pub mod text;
pub mod text_input;

Expand All @@ -20,6 +21,7 @@ pub use container::ContainerStyle;
pub use modal::ModalStyle;
pub use picklist::PickListStyle;
pub use scrollable::ScrollableStyle;
pub use table_row::TableRowStyle;
pub use text_input::TextInputStyle;

pub async fn load_user_themes() -> Vec<Theme> {
Expand Down
112 changes: 112 additions & 0 deletions crates/core/src/theme/table_row.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
use super::Theme;
use crate::widgets::table_row::Appearance;
use crate::widgets::style::table_row::StyleSheetAssociated;
use iced_native::{Background, Color};


#[derive(Debug, Clone, Copy, Default)]
pub enum TableRowStyle {
#[default]
Default,
TableRowAlternate,
TableRowHighlife,
TableRowLowlife,
TableRowSelected,
}

impl StyleSheetAssociated for Theme {
type Style = TableRowStyle;

fn appearance(&self, style: Self::Style) -> Appearance {
match style {
TableRowStyle::Default => Appearance {
text_color: Some(self.palette.normal.primary),
background: Some(Background::Color(self.palette.base.foreground)),
border_radius: 0.0,
border_width: 0.0,
border_color: Color::TRANSPARENT,
offset_left: 10.0,
offset_right: 25.0,
},
TableRowStyle::TableRowAlternate => Appearance {
background: Some(Background::Color(Color {
a: 0.50,
..self.palette.base.foreground
})),
..Appearance::default()
},
TableRowStyle::TableRowHighlife => Appearance {
text_color: Some(self.palette.normal.primary),
background: Some(Background::Color(Color {
a: 0.30,
..self.palette.base.foreground
})),
border_radius: 0.0,
border_width: 0.0,
border_color: Color::TRANSPARENT,
offset_left: 0.0,
offset_right: 0.0,
},
TableRowStyle::TableRowLowlife => Appearance {
text_color: Some(self.palette.normal.primary),
background: Some(Background::Color(Color::TRANSPARENT)),
border_radius: 0.0,
border_width: 0.0,
border_color: Color::TRANSPARENT,
offset_left: 0.0,
offset_right: 0.0,
},
TableRowStyle::TableRowSelected => Appearance {
text_color: Some(self.palette.normal.primary),
background: Some(Background::Color(self.palette.normal.primary)),
border_radius: 0.0,
border_width: 0.0,
border_color: Color::TRANSPARENT,
offset_left: 0.0,
offset_right: 0.0,
},
}
}

fn hovered(&self, style: Self::Style) -> Appearance {
let appearance = self.appearance(style);

match style {
TableRowStyle::Default => Appearance {
background: Some(Background::Color(Color {
a: 0.60,
..self.palette.normal.primary
})),
..appearance
},
TableRowStyle::TableRowAlternate => Appearance {
background: Some(Background::Color(Color {
a: 0.25,
..self.palette.normal.primary
})),
..appearance
},
TableRowStyle::TableRowHighlife => Appearance {
background: Some(Background::Color(Color {
a: 0.60,
..self.palette.normal.primary
})),
..appearance
},
TableRowStyle::TableRowLowlife => Appearance {
background: Some(Background::Color(Color {
a: 0.60,
..self.palette.normal.primary
})),
..appearance
},
TableRowStyle::TableRowSelected => Appearance {
background: Some(Background::Color(Color {
a: 0.60,
..self.palette.normal.primary
})),
..appearance
},
}
}
}
4 changes: 1 addition & 3 deletions crates/core/src/widgets/renderer/table_row.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{theme::Theme, widgets::style::table_row::TableRowStyle};
use crate::{theme::Theme};
use super::super::widget::table_row;
use iced_graphics::{Backend, Renderer};
use iced_native::{
Expand All @@ -11,8 +11,6 @@ impl<B> table_row::Renderer for Renderer<B, Theme>
where
B: Backend,
{
//type Style = Box<dyn StyleSheet<Style = TableRowStyle>>;

fn mouse_interaction(
&self,
layout: Layout<'_>,
Expand Down
136 changes: 11 additions & 125 deletions crates/core/src/widgets/style/table_row.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Decorate content and apply alignment.
use iced_core::{Background, Color};
use crate::theme::Theme;

/// The appearance of a table row.
#[derive(Debug, Clone, Copy, Default)]
Expand All @@ -22,27 +23,18 @@ pub trait StyleSheet {
fn hovered(&self) -> Appearance;
}

impl StyleSheet for crate::theme::Theme {

impl StyleSheet for Theme {
fn style(&self) -> Appearance {
Appearance {
text_color: Some(Color::WHITE),
background: None,
border_radius: 0.0,
border_width: 0.0,
border_color: Color::TRANSPARENT,
offset_right: 0.0,
offset_left: 0.0,
}
Appearance::default()
}

fn hovered(&self) -> Appearance {
Appearance {
background: None,
..self.style()
}
Appearance::default()
}
}


pub struct Default;
impl StyleSheet for Default {
fn style(&self) -> Appearance {
Expand All @@ -61,6 +53,7 @@ impl<'a> std::default::Default for Box<dyn StyleSheet + 'a> {
}
}


impl<'a, T> From<T> for Box<dyn StyleSheet + 'a>
where
T: 'a + StyleSheet,
Expand All @@ -70,120 +63,13 @@ where
}
}

/// A set of rules that dictate the style of a table row.
pub trait StyleSheetWithStyle {
type Style: std::default::Default + Copy;

/// A set of rules that dictate the style of a table row with assoicated style type.
pub trait StyleSheetAssociated {
type Style: std::default::Default + Clone;

fn appearance(&self, style: Self::Style) -> Appearance;

/// Produces the style of a hovered table row.
fn hovered(&self, style: Self::Style) -> Appearance;
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum TableRowStyle {
#[default]
Default,
TableRowAlternate,
TableRowHighlife,
TableRowLowlife,
TableRowSelected,
}

// add default impl for Stylesheet
impl StyleSheetWithStyle for crate::theme::Theme {
type Style = TableRowStyle;

fn appearance(&self, style: Self::Style) -> Appearance {
match style {
TableRowStyle::Default => Appearance {
text_color: Some(self.palette.normal.primary),
background: Some(Background::Color(self.palette.base.foreground)),
border_radius: 0.0,
border_width: 0.0,
border_color: Color::TRANSPARENT,
offset_left: 10.0,
offset_right: 25.0,
},
TableRowStyle::TableRowAlternate => Appearance {
background: Some(Background::Color(Color {
a: 0.50,
..self.palette.base.foreground
})),
..Appearance::default()
},
TableRowStyle::TableRowHighlife => Appearance {
text_color: Some(self.palette.normal.primary),
background: Some(Background::Color(Color {
a: 0.30,
..self.palette.base.foreground
})),
border_radius: 0.0,
border_width: 0.0,
border_color: Color::TRANSPARENT,
offset_left: 0.0,
offset_right: 0.0,
},
TableRowStyle::TableRowLowlife => Appearance {
text_color: Some(self.palette.normal.primary),
background: Some(Background::Color(Color::TRANSPARENT)),
border_radius: 0.0,
border_width: 0.0,
border_color: Color::TRANSPARENT,
offset_left: 0.0,
offset_right: 0.0,
},
TableRowStyle::TableRowSelected => Appearance {
text_color: Some(self.palette.normal.primary),
background: Some(Background::Color(self.palette.normal.primary)),
border_radius: 0.0,
border_width: 0.0,
border_color: Color::TRANSPARENT,
offset_left: 0.0,
offset_right: 0.0,
},
}
}

fn hovered(&self, style: Self::Style) -> Appearance {
let appearance = self.appearance(style);

match style {
TableRowStyle::Default => Appearance {
background: Some(Background::Color(Color {
a: 0.60,
..self.palette.normal.primary
})),
..appearance
},
TableRowStyle::TableRowAlternate => Appearance {
background: Some(Background::Color(Color {
a: 0.25,
..self.palette.normal.primary
})),
..appearance
},
TableRowStyle::TableRowHighlife => Appearance {
background: Some(Background::Color(Color {
a: 0.60,
..self.palette.normal.primary
})),
..appearance
},
TableRowStyle::TableRowLowlife => Appearance {
background: Some(Background::Color(Color {
a: 0.60,
..self.palette.normal.primary
})),
..appearance
},
TableRowStyle::TableRowSelected => Appearance {
background: Some(Background::Color(Color {
a: 0.60,
..self.palette.normal.primary
})),
..appearance
},
}
}
}
21 changes: 6 additions & 15 deletions crates/core/src/widgets/widget/table_row.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
#![allow(clippy::type_complexity)]

use crate::widgets::style::table_row::TableRowStyle;

pub use super::super::style::table_row::{Appearance, StyleSheet, StyleSheetWithStyle};
pub use super::super::style::table_row::{Appearance, StyleSheet, StyleSheetAssociated};
use iced_native::{
event, layout, mouse, overlay, renderer, widget, widget::Tree, Alignment, Clipboard, Element,
Event, Layout, Length, Padding, Point, Rectangle, Shell, Widget,
Expand All @@ -23,7 +20,7 @@ where
horizontal_alignment: Alignment,
vertical_alignment: Alignment,
style_sheet: Box<dyn StyleSheet + 'a>,
style: <Renderer::Theme as StyleSheetWithStyle>::Style,
style: <Renderer::Theme as StyleSheetAssociated>::Style,
content: Element<'a, Message, Renderer>,
on_press: Option<Box<dyn Fn(Event) -> Message + 'a>>,
}
Expand All @@ -48,16 +45,14 @@ where
horizontal_alignment: Alignment::Start,
vertical_alignment: Alignment::Start,
style_sheet: Default::default(),
style: <Renderer::Theme as StyleSheetWithStyle>::Style::default(),
style: <Renderer::Theme as StyleSheetAssociated>::Style::default(),
content: content.into(),
on_press: None,
}
}
// pub fn style(mut self, style: <Renderer::Theme as StyleSheet>::Style) -> Self {
// self.style_sheet = style.into();
// self
// }
pub fn style(mut self, style: <Renderer::Theme as StyleSheetWithStyle>::Style) -> Self {

/// Sets the style of the [`TableRow`].
pub fn style(mut self, style: <Renderer::Theme as StyleSheetAssociated>::Style) -> Self {
self.style = style;
self
}
Expand Down Expand Up @@ -279,10 +274,7 @@ where
}
}

//use grin_gui_core::theme::Theme as Custom;
//pub trait Renderer: iced_native::Renderer<Theme = iced_native::Theme> {
pub trait Renderer: iced_native::Renderer<Theme = crate::theme::Theme> {
//type Style: Default;

#[allow(clippy::too_many_arguments)]
fn draw<Message>(
Expand All @@ -291,7 +283,6 @@ pub trait Renderer: iced_native::Renderer<Theme = crate::theme::Theme> {
layout: Layout<'_>,
theme: &crate::theme::Theme,
cursor_position: Point,
//style: TableRowStyle,
style: &renderer::Style,
style_sheet: &dyn StyleSheet,
content: &Element<'_, Message, Self>,
Expand Down
4 changes: 2 additions & 2 deletions src/gui/element/wallet/operation/tx_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1754,9 +1754,9 @@ pub fn data_row_container<'a, 'b>(
});

if is_odd == Some(true) {
table_row = table_row.style(grin_gui_core::widgets::style::table_row::TableRowStyle::TableRowAlternate)
table_row = table_row.style(grin_gui_core::theme::TableRowStyle::TableRowAlternate)
} else {
table_row = table_row.style(grin_gui_core::widgets::style::table_row::TableRowStyle::Default)
table_row = table_row.style(grin_gui_core::theme::TableRowStyle::Default)
}

table_row
Expand Down
Loading

0 comments on commit 02e3216

Please sign in to comment.