Commit 5183fcb7 authored by Florian Blasius's avatar Florian Blasius 🤘

[update] font property handling

parent 894aab6b
......@@ -10,8 +10,8 @@ use std::{
use dces::prelude::{Entity, World};
use orbclient::Color;
use orbgl_api::prelude::Canvas;
use orbfont::Font;
use orbgl_api::prelude::Canvas;
use crate::{
application::Tree, event::EventQueue, properties::Bounds, structs::Point, theme::Theme,
......
......@@ -5,7 +5,7 @@ use std::rc::Rc;
use orbclient::{self, Color, Mode, Renderer as OrbRenderer, Window as OrbWindow};
use dces::prelude::{Entity, World};
use orbgl::prelude::{FramebufferSurface, CairoRenderEngine};
use orbgl::prelude::{CairoRenderEngine, FramebufferSurface};
use orbgl_api::Canvas;
use crate::application::Tree;
......@@ -35,7 +35,7 @@ pub struct OrbitalBackend {
impl OrbitalBackend {
pub fn new(theme: Theme, inner: OrbWindow) -> OrbitalBackend {
let mut inner = inner;
let surface = FramebufferSurface::new(
inner.width(),
inner.height(),
......
......@@ -4,13 +4,19 @@ pub enum Alignment {
Start,
Center,
End,
Stretch
Stretch,
}
impl Alignment {
/// Calculates the position (x or y) of the widget depending on the available measure, the goal measure
/// margin and alignment.
pub fn align_position(&self, available_measure: f64, measure: f64, margin_start: f64, margin_end: f64) -> f64 {
pub fn align_position(
&self,
available_measure: f64,
measure: f64,
margin_start: f64,
margin_end: f64,
) -> f64 {
match self {
Alignment::End => available_measure - measure - margin_end,
Alignment::Center => (available_measure - measure) / 2.0,
......@@ -20,7 +26,13 @@ impl Alignment {
/// Calculates the measure (measure or height) of the widget depending on the available measure, the goal measure
/// margin and horizontal alignment.
pub fn align_measure(&self, available_measure: f64, measure: f64, margin_start: f64, margin_end: f64) -> f64 {
pub fn align_measure(
&self,
available_measure: f64,
measure: f64,
margin_start: f64,
margin_end: f64,
) -> f64 {
match self {
Alignment::Stretch => available_measure - margin_start - margin_end,
_ => measure,
......@@ -37,4 +49,4 @@ impl From<&str> for Alignment {
_ => Alignment::Stretch,
}
}
}
\ No newline at end of file
}
......@@ -8,4 +8,4 @@ mod error;
mod parent_type;
#[cfg(test)]
mod tests;
\ No newline at end of file
mod tests;
use super::*;
mod alignment;
\ No newline at end of file
mod alignment;
......@@ -65,7 +65,7 @@ impl Layout for FixedSizeLayout {
let size = {
if widget.has_property::<Image>() {
if let Ok(image) = widget.borrow_property::<Image>() {
Some((image.0.width(), image.0.height()))
Some((image.0.width(), image.0.height()))
} else {
None
}
......
......@@ -67,8 +67,8 @@ macro_rules! template {
macro_rules! property {
($type:ident, $property:ident, $method:ident, $shared_method:ident) => {
use dces::prelude::{Entity, EntityComponentManager};
use crate::widget::{SharedProperty, Template, get_property};
use crate::widget::{get_property, SharedProperty, Template};
pub trait $property: Sized + From<Template> + Into<Template> {
/// Transforms the property into a template.
......@@ -93,4 +93,4 @@ macro_rules! property {
}
}
};
}
\ No newline at end of file
}
......@@ -98,4 +98,4 @@ impl From<(f64, f64, f64, f64)> for Bounds {
fn from(t: (f64, f64, f64, f64)) -> Self {
Bounds::new(t.0, t.1, t.2, t.3)
}
}
\ No newline at end of file
}
......@@ -22,13 +22,15 @@ impl HorizontalAlignment {
/// Calculates the x position of the widget depending on the available width, the goal width
/// margin and horizontal alignment.
pub fn align_x(&self, available_width: f64, width: f64, margin: Margin) -> f64 {
self.0.align_position(available_width, width, margin.left(), margin.right())
self.0
.align_position(available_width, width, margin.left(), margin.right())
}
/// Calculates the width of the widget depending on the available width, the goal width
/// margin and horizontal alignment.
pub fn align_width(&self, available_width: f64, width: f64, margin: Margin) -> f64 {
self.0.align_measure(available_width, width,margin.left(), margin.right())
self.0
.align_measure(available_width, width, margin.left(), margin.right())
}
}
......
use super::*;
use crate::structs::{Size, Position};
use crate::structs::{Position, Size};
#[test]
fn test_contains() {
......
......@@ -2,56 +2,56 @@ use super::*;
#[test]
fn test_builder_width() {
let width = 12.0;
let width = 12.0;
let constraint = Constraint::create().width(width).build();
let constraint = Constraint::create().width(width).build();
assert_eq!(constraint.width(), width);
assert_eq!(constraint.width(), width);
}
#[test]
fn test_builder_height() {
let height = 12.0;
let height = 12.0;
let constraint = Constraint::create().height(height).build();
let constraint = Constraint::create().height(height).build();
assert_eq!(constraint.height(), height);
assert_eq!(constraint.height(), height);
}
#[test]
fn test_builder_min_width() {
let width = 12.0;
let width = 12.0;
let constraint = Constraint::create().min_width(width).build();
let constraint = Constraint::create().min_width(width).build();
assert_eq!(constraint.min_width(), width);
assert_eq!(constraint.min_width(), width);
}
#[test]
fn test_builder_min_height() {
let height = 12.0;
let height = 12.0;
let constraint = Constraint::create().min_height(height).build();
let constraint = Constraint::create().min_height(height).build();
assert_eq!(constraint.min_height(), height);
assert_eq!(constraint.min_height(), height);
}
#[test]
fn test_builder_max_width() {
let width = 12.0;
let width = 12.0;
let constraint = Constraint::create().max_width(width).build();
let constraint = Constraint::create().max_width(width).build();
assert_eq!(constraint.max_width(), width);
assert_eq!(constraint.max_width(), width);
}
#[test]
fn test_builder_max_height() {
let height = 12.0;
let height = 12.0;
let constraint = Constraint::create().max_height(height).build();
let constraint = Constraint::create().max_height(height).build();
assert_eq!(constraint.max_height(), height);
assert_eq!(constraint.max_height(), height);
}
#[test]
......
......@@ -6,7 +6,7 @@ fn test_into() {
assert_eq!(offset.0, 14.0);
assert_eq!(offset.1, 14.0);
let offset: Offset = (14.0, 16.0).into();
let offset: Offset = (14.0, 16.0).into();
assert_eq!(offset.0, 14.0);
assert_eq!(offset.1, 16.0);
}
\ No newline at end of file
}
......@@ -22,13 +22,15 @@ impl VerticalAlignment {
/// Calculates the y position of the widget depending on the available height, the goal height
/// margin and Vertical alignment.
pub fn align_y(&self, available_height: f64, height: f64, margin: Margin) -> f64 {
self.0.align_position(available_height, height, margin.top(), margin.bottom())
self.0
.align_position(available_height, height, margin.top(), margin.bottom())
}
/// Calculates the height of the widget depending on the available height, the goal height
/// margin and Vertical alignment.
pub fn align_height(&self, available_height: f64, height: f64, margin: Margin) -> f64 {
self.0.align_measure(available_height, height,margin.top(), margin.bottom())
self.0
.align_measure(available_height, height, margin.top(), margin.bottom())
}
}
......
......@@ -19,7 +19,7 @@ impl From<BorderBrush> for Color {
impl Default for BorderBrush {
fn default() -> BorderBrush {
"#000000".into()
"#000000".into()
}
}
......@@ -27,4 +27,4 @@ impl From<&str> for BorderBrush {
fn from(s: &str) -> BorderBrush {
BorderBrush(s.into())
}
}
\ No newline at end of file
}
......@@ -9,7 +9,6 @@ property!(
shared_border_radius
);
impl From<f64> for BorderRadius {
fn from(t: f64) -> Self {
BorderRadius(t)
......
......@@ -11,7 +11,6 @@ property!(
shared_border_thickness
);
impl Spacer for BorderThickness {
fn left(&self) -> f64 {
self.0.left
......
......@@ -6,12 +6,7 @@ use crate::styling::fonts;
#[derive(Clone)]
pub struct Font(pub OrbFont);
property!(
Font,
FontProperty,
font,
shared_font
);
property!(Font, FontProperty, font, shared_font);
impl Default for Font {
fn default() -> Self {
......@@ -19,7 +14,6 @@ impl Default for Font {
}
}
impl From<&str> for Font {
fn from(s: &str) -> Font {
Font(OrbFont::from_path(s).unwrap())
......
......@@ -2,12 +2,7 @@
#[derive(Default, Clone, Copy)]
pub struct FontSize(pub f64);
property!(
FontSize,
FontSizeProperty,
font_size,
shared_font_size
);
property!(FontSize, FontSizeProperty, font_size, shared_font_size);
impl From<f64> for FontSize {
fn from(t: f64) -> Self {
......
......@@ -19,7 +19,7 @@ impl From<Foreground> for Color {
impl Default for Foreground {
fn default() -> Foreground {
"#000000".into()
"#000000".into()
}
}
......@@ -27,4 +27,4 @@ impl From<&str> for Foreground {
fn from(s: &str) -> Foreground {
Foreground(s.into())
}
}
\ No newline at end of file
}
......@@ -4,12 +4,7 @@ use crate::structs::{Brush, Color};
#[derive(Clone)]
pub struct IconBrush(pub Brush);
property!(
IconBrush,
IconBrushProperty,
icon_brush,
shared_icon_brush
);
property!(IconBrush, IconBrushProperty, icon_brush, shared_icon_brush);
impl From<IconBrush> for Color {
fn from(b: IconBrush) -> Color {
......@@ -19,7 +14,7 @@ impl From<IconBrush> for Color {
impl Default for IconBrush {
fn default() -> IconBrush {
"#000000".into()
"#000000".into()
}
}
......@@ -27,4 +22,4 @@ impl From<&str> for IconBrush {
fn from(s: &str) -> IconBrush {
IconBrush(s.into())
}
}
\ No newline at end of file
}
......@@ -6,12 +6,7 @@ use crate::styling::fonts;
#[derive(Clone)]
pub struct IconFont(pub OrbFont);
property!(
IconFont,
IconFontProperty,
icon_font,
shared_icon_font
);
property!(IconFont, IconFontProperty, icon_font, shared_icon_font);
impl Default for IconFont {
fn default() -> Self {
......@@ -19,7 +14,6 @@ impl Default for IconFont {
}
}
impl From<&str> for IconFont {
fn from(s: &str) -> IconFont {
IconFont(OrbFont::from_path(s).unwrap())
......
......@@ -2,12 +2,7 @@
#[derive(Default, Clone, Copy)]
pub struct IconSize(pub f64);
property!(
IconSize,
IconSizeProperty,
icon_size,
shared_icon_size
);
property!(IconSize, IconSizeProperty, icon_size, shared_icon_size);
impl From<f64> for IconSize {
fn from(t: f64) -> Self {
......
......@@ -4,8 +4,8 @@ pub use self::background::*;
pub use self::border_brush::*;
pub use self::border_radius::*;
pub use self::border_thickness::*;
pub use self::font_size::*;
pub use self::font::*;
pub use self::font_size::*;
pub use self::foreground::*;
pub use self::icon_brush::*;
pub use self::icon_font::*;
......@@ -16,8 +16,8 @@ mod background;
mod border_brush;
mod border_radius;
mod border_thickness;
mod font_size;
mod font;
mod font_size;
mod foreground;
mod icon_brush;
mod icon_font;
......@@ -25,4 +25,4 @@ mod icon_size;
mod opacity;
#[cfg(test)]
mod tests;
\ No newline at end of file
mod tests;
......@@ -2,13 +2,7 @@
#[derive(Default, Clone, Copy)]
pub struct Opacity(pub f64);
property!(
Opacity,
OpacityProperty,
opacity,
shared_opacity
);
property!(Opacity, OpacityProperty, opacity, shared_opacity);
impl From<f64> for Opacity {
fn from(t: f64) -> Self {
......
......@@ -9,4 +9,4 @@ fn test_into() {
let background: Background = "#ffffff".into();
assert_eq!(background.0, Brush::SolidColor(Color::rgb(255, 255, 255)));
}
\ No newline at end of file
}
......@@ -4,4 +4,4 @@ use super::*;
fn test_into() {
let border_radius: BorderRadius = 20.0.into();
assert_eq!(border_radius.0, 20.0);
}
\ No newline at end of file
}
......@@ -4,4 +4,4 @@ use super::*;
fn test_into() {
let border_radius: BorderRadius = 20.0.into();
assert_eq!(border_radius.0, 20.0);
}
\ No newline at end of file
}
......@@ -9,4 +9,4 @@ fn test_into() {
let foreground: Foreground = "#ffffff".into();
assert_eq!(foreground.0, Brush::SolidColor(Color::rgb(255, 255, 255)));
}
\ No newline at end of file
}
......@@ -9,4 +9,4 @@ fn test_into() {
let icon_brush: IconBrush = "#ffffff".into();
assert_eq!(icon_brush.0, Brush::SolidColor(Color::rgb(255, 255, 255)));
}
\ No newline at end of file
}
......@@ -4,4 +4,4 @@ use super::*;
fn test_into() {
let icon_size: IconSize = 20.0.into();
assert_eq!(icon_size.0, 20.0);
}
\ No newline at end of file
}
......@@ -4,10 +4,10 @@ mod background;
mod border_brush;
mod border_radius;
mod border_thickness;
mod font_size;
mod font;
mod font_size;
mod foreground;
mod icon_brush;
mod icon_font;
mod icon_size;
mod opacity;
\ No newline at end of file
mod opacity;
......@@ -4,4 +4,4 @@ use super::*;
fn test_into() {
let opacity: Opacity = 20.0.into();
assert_eq!(opacity.0, 20.0);
}
\ No newline at end of file
}
use super::*;
mod text_selection;
mod water_mark;
\ No newline at end of file
mod water_mark;
......@@ -5,4 +5,4 @@ fn test_into() {
let offset: TextSelection = (14, 16).into();
assert_eq!(offset.start_index, 14);
assert_eq!(offset.length, 16);
}
\ No newline at end of file
}
......@@ -5,4 +5,4 @@ fn test_into() {
let water_mark: WaterMark = "test".into();
assert_eq!(water_mark.0, "test");
assert_eq!(water_mark.0, "test");
}
\ No newline at end of file
}
use super::{Brush, Thickness};
/// Used to build a border, specifying additional details.
#[derive(Default)]
pub struct BorderBuilder {
brush: Brush,
thickness: Thickness,
radius: f64,
}
impl BorderBuilder {
/// Creates a new border builder with default values.
pub fn new() -> BorderBuilder {
BorderBuilder::default()
}
/// Inserts a border brush.
pub fn brush<B: Into<Brush>>(mut self, brush: B) -> Self {
self.brush = brush.into();
self
}
/// Inserts a border thickness.
pub fn thickness(mut self, thickness: Thickness) -> Self {
self.thickness = thickness;
self
}
/// Inserts a border radius.
pub fn radius(mut self, radius: f64) -> Self {
self.radius = radius;
self
}
/// Builds the border.
pub fn build(self) -> Border {
Border {
brush: self.brush,
thickness: self.thickness,
radius: self.radius,
}
}
}
/// Describes a border of a shape with border `brush`, `thickness` and `radius`.
#[derive(Clone, Default, Debug, PartialEq)]
pub struct Border {
brush: Brush,
thickness: Thickness,
radius: f64,
}
impl Border {
/// Creates a new `BorderBuilder` with default values.
pub fn create() -> BorderBuilder {
BorderBuilder::new()
}
/// Gets the border brush.
pub fn brush(&self) -> &Brush {
&self.brush
}
/// Sets the border brush.
pub fn set_brush<B: Into<Brush>>(&mut self, brush: B) {
self.brush = brush.into();
}
/// Gets the thickness.
pub fn thickness(&self) -> Thickness {
self.thickness