From 69c4cd938684c0fc9e0c03531177ad24bc444e81 Mon Sep 17 00:00:00 2001 From: TheOddGarlic Date: Thu, 28 Apr 2022 11:45:33 +0300 Subject: [PATCH] very primitive column layout --- src/main.rs | 8 ++++--- src/widget/column.rs | 56 ++++++++++++++++++++++++++++++++++++++++++++ src/widget/label.rs | 2 +- src/widget/mod.rs | 2 ++ 4 files changed, 64 insertions(+), 4 deletions(-) create mode 100755 src/widget/column.rs diff --git a/src/main.rs b/src/main.rs index 9c6d0d7..bb2d106 100755 --- a/src/main.rs +++ b/src/main.rs @@ -8,6 +8,7 @@ use abletk::plugin::QuitPlugin; use abletk::prelude::*; +use abletk::widget::Column; #[launch] fn launch() -> _ { @@ -15,8 +16,9 @@ fn launch() -> _ { .apply_plugin(QuitPlugin) .add_window(Window::builder( Row::new() - .add(Label::new("Hello, AbleTK! ") + .add(Label::new("Hello, ") .color(rgb!(0xFF00FFFF))) - .add(Label::new("Hello, World!") - .color(rgb!(0x64CAFEFF))))) + .add(Column::new() + .add(Label::new("World!")) + .add(Label::new("AbleTK!"))))) } diff --git a/src/widget/column.rs b/src/widget/column.rs new file mode 100755 index 0000000..ed025c8 --- /dev/null +++ b/src/widget/column.rs @@ -0,0 +1,56 @@ +/* + * Copyright (C) 2022 Umut İnan Erdoğan + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +use crate::{layout::position::Position, widget::Widget}; +use abletk_common::Renderer; + +pub struct Column { + widgets: Vec>, +} + +impl Widget for Column { + fn draw(&self, renderer: &mut Renderer) { + self.widgets.iter().for_each(|widget| { + let pos = widget.position(renderer); + renderer.position_at( + renderer.position().0 + pos.x(), + renderer.position().1 + pos.y() + ); + widget.draw(renderer); + renderer.position_at( + renderer.position().0, + renderer.position().1 + pos.height() + ); + }); + } + + fn position(&self, renderer: &mut Renderer) -> Position { + let (width, height) = self.widgets.iter() + .map(|widget| widget.position(renderer)) + .map(|pos| (pos.width(), pos.height())) + .fold((0, 0), |accum, item| ( + if item.0 > accum.0 { item.0 } else { accum.0 }, + accum.1 + item.1 + )); + + Position::new(0, 0, width, height) + } +} + +impl Column { + pub fn new() -> Self { + Self { + widgets: Vec::new(), + } + } + + pub fn add(mut self, widget: T) -> Self { + self.widgets.push(Box::new(widget)); + self + } +} diff --git a/src/widget/label.rs b/src/widget/label.rs index 39abbc6..30fb04c 100755 --- a/src/widget/label.rs +++ b/src/widget/label.rs @@ -30,7 +30,7 @@ impl Label { pub fn new>(text: S) -> Self { Self { text: text.into(), - color: rgb!(0xFFFFFFFF), + color: rgb!(0x000000FF), } } diff --git a/src/widget/mod.rs b/src/widget/mod.rs index eff683e..05f0ff8 100755 --- a/src/widget/mod.rs +++ b/src/widget/mod.rs @@ -6,10 +6,12 @@ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ +mod column; mod label; mod row; use abletk_common::Renderer; +pub use column::*; pub use label::*; pub use row::*;