Commit add0fb1a authored by Florian Blasius's avatar Florian Blasius 🤘

[api update] code review

parent a69ee16a
......@@ -22,7 +22,7 @@ impl Template for MainView {
.child(
TextBlock::create()
.text("(0,0)")
.selector("white")
.selector("light-text")
.horizontal_alignment("Center")
.vertical_alignment("Center")
.build(context),
......
......@@ -15,7 +15,7 @@ linkwater {
color: #3b434a;
}
white {
light-text {
color: #dfebf5;
}
......
......@@ -61,51 +61,37 @@ impl Layout for FixedSizeLayout {
// -- todo will be removed after orbgl merge --
let size = {
if widget.has::<Image>() {
if let Some(image) = widget.try_get::<Image>() {
Some((image.width(), image.height()))
} else {
None
}
} else if widget.has::<Text>() {
let text = widget.get::<Text>();
let font = widget.get::<Font>();
let font_size = widget.get::<FontSize>();
if text.0.is_empty() {
if let Some(water_mark) = widget.try_get::<WaterMark>() {
if water_mark.0.is_empty() {
None
} else {
Some(FONT_MEASURE.measure(&water_mark.0, &(font.0).0, font_size.0 as u32))
}
let size = widget.try_get::<Image>()
.map(|image| (image.width(), image.height()))
.or_else(|| {
widget.try_get::<Text>().and_then(|text| {
let font = widget.get::<Font>();
let font_size = widget.get::<FontSize>();
if text.0.is_empty() {
widget.try_get::<WaterMark>()
.filter(|water_mark| !water_mark.0.is_empty())
.map(|water_mark| FONT_MEASURE.measure(&water_mark.0, &(font.0).0, font_size.0 as u32))
} else {
None
}
} else {
let mut size = FONT_MEASURE.measure(&text.0, &(font.0).0, font_size.0 as u32);
let mut size = FONT_MEASURE.measure(&text.0, &(font.0).0, font_size.0 as u32);
if text.0.ends_with(" ") {
size.0 += FONT_MEASURE.measure("a", &(font.0).0, font_size.0 as u32).0 / 2;
if text.0.ends_with(" ") {
size.0 += FONT_MEASURE.measure("a", &(font.0).0, font_size.0 as u32).0 / 2;
}
Some(size)
}
Some(size)
}
} else if widget.has::<FontIcon>() {
let font_icon = widget.get::<FontIcon>();
if font_icon.0.is_empty() {
None
} else {
Some(FONT_MEASURE.measure(
})
})
.or_else(|| {
widget.try_get::<FontIcon>()
.filter(|font_icon| !font_icon.0.is_empty())
.map(|font_icon| FONT_MEASURE.measure(
&font_icon.0,
&(widget.get::<IconFont>().0).0,
widget.get::<IconSize>().0 as u32,
))
}
} else {
None
}
};
});
if let Some(size) = size {
if let Ok(constraint) = ecm.borrow_mut_component::<Constraint>(entity) {
......
......@@ -116,8 +116,7 @@ impl Layout for TextSelectionLayout {
size.1 = vertical_alignment.align_measure(parent_size.1, size.1, margin.top(), margin.bottom());
if widget.has::<Text>() {
let text = widget.get::<Text>();
if let Some(text) = widget.try_get::<Text>() {
let font = widget.get::<Font>();
let font_size = widget.get::<FontSize>();
......@@ -135,9 +134,7 @@ impl Layout for TextSelectionLayout {
}
}
if let Some(off) = widget.try_get::<Offset>() {
pos += (off.0).0;
}
pos += widget.try_get::<Offset>().map_or(0.0, |off| (off.0).0);
if let Some(margin) = widget.try_get_mut::<Margin>() {
margin.set_left(pos);
......
......@@ -135,6 +135,22 @@ impl ColumnsBuilder {
self
}
/// Inserts a list of columns.
pub fn columns<R: Into<Column> + Clone>(mut self, columns: &[R]) -> Self {
for column in columns.to_vec() {
self.columns.push(column.into());
}
self
}
/// Inserts the given column as often as given.
pub fn repeat<R: Into<Column> + Copy>(mut self, column: R, count: usize) -> Self {
for _ in 0..count {
self.columns.push(column.into())
}
self
}
/// Builds the columns.
pub fn build(self) -> Columns {
Columns(ColumnsContainer(self.columns))
......
......@@ -238,7 +238,7 @@ impl ConstraintExtension for BoxConstraint {
self.min_width = min_width;
self.min_height = min_height;
}
fn max_width(&self) -> f64 {
self.max_width
}
......
......@@ -134,6 +134,22 @@ impl RowsBuilder {
self
}
/// Inserts a list of rows.
pub fn rows<R: Into<Row> + Clone>(mut self, rows: &[R]) -> Self {
for row in rows.to_vec() {
self.row_definitions.push(row.into());
}
self
}
/// Inserts the given row as often as given.
pub fn repeat<R: Into<Row> + Copy>(mut self, row: R, count: usize) -> Self {
for _ in 0..count {
self.row_definitions.push(row.into())
}
self
}
/// Builds the rows.
pub fn build(self) -> Rows {
Rows(RowsContainer(self.row_definitions))
......@@ -153,6 +169,7 @@ property!(
/// Provides additional operations on grid rows.
pub trait RowExtension {
/// Returns a new Rows Builder.
fn create() -> RowsBuilder;
/// Returns the number of elements in the rows list, also referred to as its 'length'.
......
......@@ -128,7 +128,7 @@ fn test_from() {
#[test]
fn test_into() {
let bounds: PropertySource<Bounds> = (17.0, 18.0, 19.0, 20.0).into();
let bounds: PropertySource<Bounds> = (17.0, 18.0, 19.0, 20.0).into();
assert_eq!(bounds, PropertySource::Value::<Bounds>(Bounds(Rect::new(17.0, 18.0, 19.0, 20.0))));
assert_eq!(bounds, PropertySource::Value::<Bounds>(Bounds(Rect::new(17.0, 18.0, 19.0, 20.0))));
}
use super::*;
use crate::enums::Alignment;
#[test]
fn test_align_position() {
let available_width = 100.0;
let width = 50.0;
let margin = Margin::default();
let horizontal_alignment = HorizontalAlignment(Alignment::Stretch);
assert_eq!(
horizontal_alignment.align_x(available_width, width, margin.clone()),
0.0
);
let horizontal_alignment = HorizontalAlignment(Alignment::Center);
assert_eq!(
horizontal_alignment.align_x(available_width, width, margin.clone()),
25.0
);
let horizontal_alignment = HorizontalAlignment(Alignment::Start);
assert_eq!(
horizontal_alignment.align_x(available_width, width, margin.clone()),
0.0
);
let horizontal_alignment = HorizontalAlignment(Alignment::End);
assert_eq!(
horizontal_alignment.align_x(available_width, width, margin.clone()),
50.0
);
}
#[test]
fn test_align_width() {
let available_width = 100.0;
let width = 50.0;
let margin = Margin::default();
let horizontal_alignment = HorizontalAlignment(Alignment::Stretch);
assert_eq!(
horizontal_alignment.align_width(available_width, width, margin.clone()),
available_width
);
let horizontal_alignment = HorizontalAlignment(Alignment::Center);
assert_eq!(
horizontal_alignment.align_width(available_width, width, margin.clone()),
width
);
let horizontal_alignment = HorizontalAlignment(Alignment::Start);
assert_eq!(
horizontal_alignment.align_width(available_width, width, margin.clone()),
width
);
let horizontal_alignment = HorizontalAlignment(Alignment::End);
assert_eq!(
horizontal_alignment.align_width(available_width, width, margin.clone()),
width
);
}
#[test]
fn test_into() {
let horizontal_alignment: HorizontalAlignment = "Start".into();
assert_eq!(horizontal_alignment.0, Alignment::Start);
let horizontal_alignment: HorizontalAlignment = "start".into();
assert_eq!(horizontal_alignment.0, Alignment::Start);
let horizontal_alignment: HorizontalAlignment = "Center".into();
assert_eq!(horizontal_alignment.0, Alignment::Center);
let horizontal_alignment: HorizontalAlignment = "center".into();
assert_eq!(horizontal_alignment.0, Alignment::Center);
let horizontal_alignment: HorizontalAlignment = "End".into();
assert_eq!(horizontal_alignment.0, Alignment::End);
let horizontal_alignment: HorizontalAlignment = "end".into();
assert_eq!(horizontal_alignment.0, Alignment::End);
let horizontal_alignment: HorizontalAlignment = "Stretch".into();
assert_eq!(horizontal_alignment.0, Alignment::Stretch);
let horizontal_alignment: HorizontalAlignment = "stretch".into();
assert_eq!(horizontal_alignment.0, Alignment::Stretch);
let horizontal_alignment: HorizontalAlignment = "other".into();
assert_eq!(horizontal_alignment.0, Alignment::Stretch);
}
use super::*;
use crate::enums::Alignment;
#[test]
fn test_align_position() {
let available_width = 100.0;
let width = 50.0;
let margin = Margin::default();
let horizontal_alignment = HorizontalAlignment(Alignment::Stretch);
assert_eq!(
horizontal_alignment.align_x(available_width, width, margin.clone()),
0.0
);
let horizontal_alignment = HorizontalAlignment(Alignment::Center);
assert_eq!(
horizontal_alignment.align_x(available_width, width, margin.clone()),
25.0
);
let horizontal_alignment = HorizontalAlignment(Alignment::Start);
assert_eq!(
horizontal_alignment.align_x(available_width, width, margin.clone()),
0.0
);
let horizontal_alignment = HorizontalAlignment(Alignment::End);
assert_eq!(
horizontal_alignment.align_x(available_width, width, margin.clone()),
50.0
);
}
#[test]
fn test_align_width() {
let available_width = 100.0;
let width = 50.0;
let margin = Margin::default();
let horizontal_alignment = HorizontalAlignment(Alignment::Stretch);
assert_eq!(
horizontal_alignment.align_width(available_width, width, margin.clone()),
available_width
);
let horizontal_alignment = HorizontalAlignment(Alignment::Center);
assert_eq!(
horizontal_alignment.align_width(available_width, width, margin.clone()),
width
);
let horizontal_alignment = HorizontalAlignment(Alignment::Start);
assert_eq!(
horizontal_alignment.align_width(available_width, width, margin.clone()),
width
);
let horizontal_alignment = HorizontalAlignment(Alignment::End);
assert_eq!(
horizontal_alignment.align_width(available_width, width, margin.clone()),
width
);
}
#[test]
fn test_into() {
let horizontal_alignment: HorizontalAlignment = "Start".into();
assert_eq!(horizontal_alignment.0, Alignment::Start);
let horizontal_alignment: HorizontalAlignment = "start".into();
assert_eq!(horizontal_alignment.0, Alignment::Start);
let horizontal_alignment: HorizontalAlignment = "Center".into();
assert_eq!(horizontal_alignment.0, Alignment::Center);
let horizontal_alignment: HorizontalAlignment = "center".into();
assert_eq!(horizontal_alignment.0, Alignment::Center);
let horizontal_alignment: HorizontalAlignment = "End".into();
assert_eq!(horizontal_alignment.0, Alignment::End);
let horizontal_alignment: HorizontalAlignment = "end".into();
assert_eq!(horizontal_alignment.0, Alignment::End);
let horizontal_alignment: HorizontalAlignment = "Stretch".into();
assert_eq!(horizontal_alignment.0, Alignment::Stretch);
let horizontal_alignment: HorizontalAlignment = "stretch".into();
assert_eq!(horizontal_alignment.0, Alignment::Stretch);
let horizontal_alignment: HorizontalAlignment = "other".into();
assert_eq!(horizontal_alignment.0, Alignment::Stretch);
}
......@@ -15,7 +15,7 @@ impl Default for InnerFont {
impl fmt::Debug for InnerFont {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "InnerFont(orbfont::Font)")
f.write_str("InnerFont(orbfont::Font)")
}
}
......@@ -38,10 +38,8 @@ impl From<Box<[u8]>> for InnerFont {
}
}
property!(
/// `Font` describes the text font of a widget.
Font(InnerFont)
);
property!(/// `Font` describes the text font of a widget.
Font(InnerFont));
// --- Conversions ---
......
......@@ -15,7 +15,7 @@ impl Default for InnerImage {
impl fmt::Debug for InnerImage {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "InnerImage(orbimage::Image)")
f.write_str("InnerImage(orbimage::Image)")
}
}
......
......@@ -50,7 +50,7 @@ impl RectangleRenderObject {
x + border_thickness.left,
y + border_thickness.top,
width - border_thickness.left - border_thickness.right,
height - border_thickness.top - border_thickness.right,
height - border_thickness.top - border_thickness.bottom,
brush,
);
}
......
......@@ -35,13 +35,13 @@ impl RenderObject for TextRenderObject {
let text = widget.clone::<Text>();
let txt = {
if !text.0.is_empty() {
text.0.clone()
} else {
widget.clone_or_default::<WaterMark>().0
}
if !text.0.is_empty() {
text.0.clone()
} else {
widget.clone_or_default::<WaterMark>().0
}
};
renderer.render_text(
&txt,
&widget.get::<Bounds>(),
......@@ -50,6 +50,6 @@ impl RenderObject for TextRenderObject {
widget.get::<FontSize>().0 as u32,
widget.clone::<Foreground>().into(),
&(widget.get::<Font>().0).0,
);
);
}
}
......@@ -219,19 +219,19 @@ impl Theme {
}
pub fn brush(&self, property: &str, query: &Selector) -> Option<Brush> {
self.get(property, query).map_or(None, |v| v.brush())
self.get(property, query).and_then(|v| v.brush())
}
pub fn uint(&self, property: &str, query: &Selector) -> Option<u32> {
self.get(property, query).map_or(None, |v| v.uint())
self.get(property, query).and_then(|v| v.uint())
}
pub fn float(&self, property: &str, query: &Selector) -> Option<f32> {
self.get(property, query).map_or(None, |v| v.float())
self.get(property, query).and_then(|v| v.float())
}
pub fn string(&self, property: &str, query: &Selector) -> Option<String> {
self.get(property, query).map_or(None, |v| v.string())
self.get(property, query).and_then(|v| v.string())
}
/// Updates the given widget by theme and selector.
......
......@@ -96,8 +96,8 @@ impl<'a> WidgetContainer<'a> {
///
/// Panics if the widget does not contains the property.
pub fn set<P>(&mut self, value: P)
where P: Component + Default + Clone,
where
P: Component + Default + Clone,
{
if let Ok(property) = self.ecm.borrow_mut_component::<P>(self.current_node) {
*property = value;
......@@ -116,30 +116,18 @@ impl<'a> WidgetContainer<'a> {
where
P: Clone + Component + Default,
{
if let Ok(_) = self.ecm.borrow_component::<P>(self.current_node) {
return true;
}
false
self.ecm.borrow_component::<P>(self.current_node).is_ok()
}
/// Returns a reference of a property of type `P` from the given widget entity. If the entity does
/// not exists or it doesn't have a component of type `P` `None` will be returned.
pub fn try_get<P: Component + Default>(&self) -> Option<&P> {
if let Ok(property) = self.ecm.borrow_component::<P>(self.current_node) {
return Some(property);
}
None
self.ecm.borrow_component::<P>(self.current_node).ok()
}
/// Returns a mutable reference of a property of type `P` from the given widget entity. If the entity does
/// not exists or it doesn't have a component of type `P` `None` will be returned.
pub fn try_get_mut<P: Component + Default>(&mut self) -> Option<&mut P> {
if let Ok(property) = self.ecm.borrow_mut_component::<P>(self.current_node) {
return Some(property);
}
None
self.ecm.borrow_mut_component::<P>(self.current_node).ok()
}
}
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