Commit 042f56d6 authored by Florian Blasius's avatar Florian Blasius 🤘

[api update] implement all pieces for grid

working the new api.
parent ac9c36cb
// use orbtk::*;
use orbtk::*;
// widget!(MainView);
widget!(MainView);
// impl Widget for MainView {
// fn create() -> Self {
// MainView::new()
// .child(
// Grid::create()
// .columns(
// Columns::create()
// .column("*")
// .column("Auto")
// .column(50.0)
// .build(),
// )
// .rows(Rows::create().row("*").row("*").build())
// .child(
// Grid::create()
// .selector("lynch")
// .margin((10.0, 0.0, 0.0, 4.0))
// .attach(GridColumn(0))
// .child(
// TextBlock::create()
// .text("(0,0)")
// .horizontal_alignment("Center")
// .vertical_alignment("Center"),
// ),
// )
// .child(
// Grid::create()
// .selector("bluebayoux")
// .margin(10.0)
// .constraint(Constraint::create().width(150.0).build())
// .attach(GridColumn(1))
// .child(
// TextBlock::create()
// .text("(1,0)")
// .horizontal_alignment("Center")
// .vertical_alignment("Center"),
// ),
// )
// .child(
// Grid::create()
// .selector("linkwater")
// .attach(GridColumn(2))
// .child(
// TextBlock::create()
// .text("(2,0)")
// .selector("linkwater")
// .horizontal_alignment("Center")
// .vertical_alignment("Center"),
// ),
// )
// .child(
// Grid::create()
// .selector("goldendream")
// .attach(GridColumn(0))
// .attach(GridRow(1))
// .attach(ColumnSpan(3))
// .child(
// TextBlock::create()
// .text("(0,1) - ColumnSpan 3")
// .selector("goldendream")
// .horizontal_alignment(HorizontalAlignment(Alignment::Center))
// .vertical_alignment(VerticalAlignment(Alignment::Center)),
// ),
// ),
// )
// .debug_name("MainView")
// }
// }
// fn main() {
// let mut application = Application::default();
// application
// .create_window()
// .bounds((100.0, 100.0, 420.0, 730.0))
// .title("OrbTk - grid example")
// .root(MainView::create())
// .theme(
// Theme::create()
// .extension_path("examples/res/grid.css")
// .build(),
// )
// .resizable(true)
// .build();
// application.run();
// }
impl Template for MainView {
fn template(self, _: Entity, context: &mut BuildContext) -> Self {
self.name("MainView").child(
Grid::create()
.columns(
Columns::create()
.column("*")
.column("Auto")
.column(50.0)
.build(),
)
.rows(Rows::create().row("*").row("*").build())
.child(
Grid::create()
.selector("lynch")
.margin((10.0, 0.0, 0.0, 4.0))
.attach(GridColumn(0))
.child(
TextBlock::create()
.text("(0,0)")
.horizontal_alignment("Center")
.vertical_alignment("Center")
.build(context),
)
.build(context),
)
.child(
Grid::create()
.selector("bluebayoux")
.margin(10.0)
.constraint(Constraint::create().width(150.0).build())
.attach(GridColumn(1))
.child(
TextBlock::create()
.text("(1,0)")
.horizontal_alignment("Center")
.vertical_alignment("Center")
.build(context),
)
.build(context),
)
.child(
Grid::create()
.selector("linkwater")
.attach(GridColumn(2))
.child(
TextBlock::create()
.text("(2,0)")
.selector("linkwater")
.horizontal_alignment("Center")
.vertical_alignment("Center")
.build(context),
)
.build(context),
)
.child(
Grid::create()
.selector("goldendream")
.attach(GridColumn(0))
.attach(GridRow(1))
.attach(ColumnSpan(3))
.child(
TextBlock::create()
.text("(0,1) - ColumnSpan 3")
.selector("goldendream")
.horizontal_alignment(HorizontalAlignment(Alignment::Center))
.vertical_alignment(VerticalAlignment(Alignment::Center))
.build(context),
)
.build(context),
)
.build(context),
)
}
}
fn main() {
}
\ No newline at end of file
let mut application = Application::default();
application
.create_window()
.bounds((100.0, 100.0, 420.0, 730.0))
.title("OrbTk - grid example")
.theme(
Theme::create()
.extension_path("examples/res/grid.css")
.build(),
)
.resizable(true)
.build(MainView::create());
application.run();
}
This diff is collapsed.
......@@ -16,6 +16,12 @@ macro_rules! property {
}
}
impl Into<PropertySource<$property>> for $property {
fn into(self) -> PropertySource<$property> {
PropertySource::Value(self)
}
}
impl From<$property> for $type {
fn from(property: $property) -> $type {
property.0.into()
......@@ -87,6 +93,11 @@ macro_rules! widget {
self
}
/// Sets or shares the constraint property.
pub fn constraint<P: Into<PropertySource<Constraint>>>(self, constraint: P) -> Self {
self.attach(constraint)
}
/// Sets or shares the vertical alignment property.
pub fn vertical_alignment<P: Into<PropertySource<VerticalAlignment>>>(self, vertical_alignment: P) -> Self {
self.attach(vertical_alignment)
......
// use std::slice::{Iter, IterMut};
// /// Used to build a column, specifying additional details.
// #[derive(Default)]
// pub struct ColumnBuilder {
// width: ColumnWidth,
// min_width: f64,
// max_width: f64,
// }
// impl ColumnBuilder {
// /// Creates a new `ColumnBuilder` with default values.
// pub fn new() -> Self {
// ColumnBuilder::default()
// }
// /// Inserts a new width.
// pub fn width(mut self, width: ColumnWidth) -> Self {
// self.width = width;
// self
// }
// /// Inserts a new min width.
// pub fn min_width(mut self, min_width: f64) -> Self {
// self.min_width = min_width;
// self
// }
// /// Inserts a new max width.
// pub fn max_width(mut self, max_width: f64) -> Self {
// self.max_width = max_width;
// self
// }
// /// Builds the column.
// pub fn build(self) -> Column {
// Column {
// width: self.width,
// min_width: self.min_width,
// max_width: self.max_width,
// current_width: 0.0,
// }
// }
// }
// /// Used to define the column of the `Grid`.
// #[derive(Copy, Clone, PartialEq)]
// pub struct Column {
// pub width: ColumnWidth,
// pub min_width: f64,
// pub max_width: f64,
// current_width: f64,
// }
// impl Column {
// /// Creates a new `ColumnBuilder` object with default values.
// pub fn create() -> ColumnBuilder {
// ColumnBuilder::new()
// }
// /// Gets the column width.
// pub fn width(&self) -> ColumnWidth {
// self.width
// }
// /// Gets the current width.
// pub fn current_width(&self) -> f64 {
// self.current_width
// }
// /// Sets the current width.
// pub fn set_current_width(&mut self, width: f64) {
// self.current_width = if self.min_width == 0.0 && self.max_width == 0.0 && width > 0.0 {
// width
// } else if width < self.min_width && self.min_width > 0.0 {
// self.min_width
// } else if width > self.max_width && self.max_width > 0.0 {
// self.max_width
// } else {
// width
// };
// }
// }
// /// Used to define the width of a grid column.
// #[derive(Copy, Clone, Debug, PartialEq)]
// pub enum ColumnWidth {
// /// Column is measured by the largest child.
// Auto,
// /// Column expands to the rest available width.
// Stretch,
// /// Defines a fixed size for the column.
// Width(f64),
// }
// impl Default for ColumnWidth {
// fn default() -> Self {
// ColumnWidth::Stretch
// }
// }
// /// Used to build a columns, specifying additional details.
// #[derive(Default)]
// pub struct ColumnsBuilder {
// columns: Vec<Column>,
// }
// /// Used to build a columns, specifying additional details.
// impl ColumnsBuilder {
// /// Creates a new `ColumnsBuilder` with default values.
// pub fn new() -> Self {
// ColumnsBuilder::default()
// }
// /// Inserts a new column.
// pub fn column<C: Into<Column>>(mut self, column: C) -> Self {
// self.columns.push(column.into());
// self
// }
// /// Builds the columns.
// pub fn build(self) -> Columns {
// Columns {
// value: self.columns,
// }
// }
// }
// /// Represents a list of grid columns.
// #[derive(Default, Clone, PartialEq)]
// pub struct Columns {
// value: Vec<Column>,
// }
// property!(Columns, ColumnsProperty, columns, shared_columns);
// impl Columns {
// /// Creates a new `ColumnsBuilder` object with default values.
// pub fn create() -> ColumnsBuilder {
// ColumnsBuilder::new()
// }
// /// Returns the number of elements in the columns list, also referred to as its 'length'.
// pub fn len(&self) -> usize {
// self.value.len()
// }
// /// Returns a reference to an column.
// pub fn get(&self, column: usize) -> Option<&Column> {
// self.value.get(column)
// }
// /// Returns a mutable reference to an column.
// pub fn get_mut(&mut self, column: usize) -> Option<&mut Column> {
// self.value.get_mut(column)
// }
// /// Returns an iterator over the slice.
// pub fn iter(&self) -> Iter<Column> {
// self.value.iter()
// }
// /// Returns a mutable iterator over the slice.
// pub fn iter_mut(&mut self) -> IterMut<Column> {
// self.value.iter_mut()
// }
// }
// impl From<&str> for Column {
// fn from(t: &str) -> Self {
// match t {
// "Auto" | "auto" => Column::create().width(ColumnWidth::Auto).build(),
// _ => Column::create().width(ColumnWidth::Stretch).build(),
// }
// }
// }
// impl From<f64> for Column {
// fn from(t: f64) -> Self {
// Column::create().width(ColumnWidth::Width(t)).build()
// }
// }
use std::slice::{Iter, IterMut};
/// Used to build a column, specifying additional details.
#[derive(Default)]
pub struct ColumnBuilder {
width: ColumnWidth,
min_width: f64,
max_width: f64,
}
impl ColumnBuilder {
/// Creates a new `ColumnBuilder` with default values.
pub fn new() -> Self {
ColumnBuilder::default()
}
/// Inserts a new width.
pub fn width(mut self, width: ColumnWidth) -> Self {
self.width = width;
self
}
/// Inserts a new min width.
pub fn min_width(mut self, min_width: f64) -> Self {
self.min_width = min_width;
self
}
/// Inserts a new max width.
pub fn max_width(mut self, max_width: f64) -> Self {
self.max_width = max_width;
self
}
/// Builds the column.
pub fn build(self) -> Column {
Column {
width: self.width,
min_width: self.min_width,
max_width: self.max_width,
current_width: 0.0,
}
}
}
/// Used to define the column of the `Grid`.
#[derive(Copy, Clone, Debug, Default, PartialEq)]
pub struct Column {
pub width: ColumnWidth,
pub min_width: f64,
pub max_width: f64,
current_width: f64,
}
impl Column {
/// Creates a new `ColumnBuilder` object with default values.
pub fn create() -> ColumnBuilder {
ColumnBuilder::new()
}
/// Gets the column width.
pub fn width(&self) -> ColumnWidth {
self.width
}
/// Gets the current width.
pub fn current_width(&self) -> f64 {
self.current_width
}
/// Sets the current width.
pub fn set_current_width(&mut self, width: f64) {
self.current_width = if self.min_width == 0.0 && self.max_width == 0.0 && width > 0.0 {
width
} else if width < self.min_width && self.min_width > 0.0 {
self.min_width
} else if width > self.max_width && self.max_width > 0.0 {
self.max_width
} else {
width
};
}
}
impl From<&str> for Column {
fn from(t: &str) -> Self {
match t {
"Auto" | "auto" => Column::create().width(ColumnWidth::Auto).build(),
_ => Column::create().width(ColumnWidth::Stretch).build(),
}
}
}
impl From<f64> for Column {
fn from(t: f64) -> Self {
Column::create().width(ColumnWidth::Width(t)).build()
}
}
/// Used to define the width of a grid column.
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum ColumnWidth {
/// Column is measured by the largest child.
Auto,
/// Column expands to the rest available width.
Stretch,
/// Defines a fixed size for the column.
Width(f64),
}
impl Default for ColumnWidth {
fn default() -> Self {
ColumnWidth::Stretch
}
}
/// Used to build a columns, specifying additional details.
#[derive(Default)]
pub struct ColumnsBuilder {
columns: Vec<Column>,
}
/// Used to build a columns, specifying additional details.
impl ColumnsBuilder {
/// Creates a new `ColumnsBuilder` with default values.
pub fn new() -> Self {
ColumnsBuilder::default()
}
/// Inserts a new column.
pub fn column<C: Into<Column>>(mut self, column: C) -> Self {
self.columns.push(column.into());
self
}
/// Builds the columns.
pub fn build(self) -> Columns {
Columns(ColumnsContainer(self.columns))
}
}
/// Helper struct used inside of the columns Property.
#[derive(Debug, Clone, PartialEq, Default)]
pub struct ColumnsContainer(Vec<Column>);
property!(
/// `Columns` describes a list of grid columns.
Columns(ColumnsContainer)
);
// --- Trait implementations ---
/// Provides additional operations on grid columns.
pub trait ColumnExtension {
fn create() -> ColumnsBuilder;
/// Returns the number of elements in the columns list, also referred to as its 'length'.
fn len(&self) -> usize;
/// Returns a reference to an column.
fn get(&self, column: usize) -> Option<&Column>;
/// Returns a mutable reference to an column.
fn get_mut(&mut self, column: usize) -> Option<&mut Column>;
/// Returns an iterator over the slice.
fn iter(&self) -> Iter<Column>;
/// Returns a mutable iterator over the slice.
fn iter_mut(&mut self) -> IterMut<Column>;
}
impl ColumnExtension for Columns {
/// Creates a new `ColumnsBuilder` object with default values.
fn create() -> ColumnsBuilder {
ColumnsBuilder::new()
}
/// Returns the number of elements in the columns list, also referred to as its 'length'.
fn len(&self) -> usize {
(self.0).0.len()
}
/// Returns a reference to an column.
fn get(&self, column: usize) -> Option<&Column> {
(self.0).0.get(column)
}
/// Returns a mutable reference to an column.
fn get_mut(&mut self, column: usize) -> Option<&mut Column> {
(self.0).0.get_mut(column)
}
/// Returns an iterator over the slice.
fn iter(&self) -> Iter<Column> {
(self.0).0.iter()
}
/// Returns a mutable iterator over the slice.
fn iter_mut(&mut self) -> IterMut<Column> {
(self.0).0.iter_mut()
}
}
// --- Conversions ---
// /// Used to define the column span of a widget on the `Grid`.
// #[derive(Default, Copy, Clone, PartialEq)]
// pub struct ColumnSpan(pub usize);
// property!(ColumnSpan, ColumnSpanProperty, column_span, shared_column_span);
\ No newline at end of file
property!(
/// `ColumnSpan` describes the column span of a widget on the `Grid`.
ColumnSpan(usize)
);
\ No newline at end of file
use std::f64;
/// Used to build a constraint, specifying additional details.
#[derive(Default)]
pub struct ConstraintBuilder {
width: f64,
height: f64,
min_width: f64,
min_height: f64,
max_width: f64,
max_height: f64,
}