From 9b3969c6ad1d2da17b5dba38ae917118d103ea92 Mon Sep 17 00:00:00 2001 From: TheOddGarlic Date: Sun, 8 May 2022 21:05:46 +0300 Subject: [PATCH] new padding widget --- src/lib.rs | 3 ++ src/main.rs | 4 +- src/widget/mod.rs | 2 + src/widget/padding.rs | 98 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 105 insertions(+), 2 deletions(-) create mode 100755 src/widget/padding.rs diff --git a/src/lib.rs b/src/lib.rs index 19baba7..8cd04b2 100755 --- a/src/lib.rs +++ b/src/lib.rs @@ -23,8 +23,11 @@ pub mod prelude { pub use crate::event::application::Event as ApplicationEvent; pub use crate::event::window::Event as WindowEvent; pub use crate::platform::Platform; + pub use crate::widget::Column; + pub use crate::widget::IntoPadded; pub use crate::widget::Label; pub use crate::widget::Row; + pub use crate::widget::Padding; pub use crate::widget::Widget; pub use crate::window::*; pub use crate::rgb; diff --git a/src/main.rs b/src/main.rs index 14338c4..6d854da 100755 --- a/src/main.rs +++ b/src/main.rs @@ -8,7 +8,6 @@ use abletk::plugin::QuitPlugin; use abletk::prelude::*; -use abletk::widget::Column; #[launch] fn launch() -> _ { @@ -22,7 +21,8 @@ fn launch() -> _ { .add(Label::new("World!")) .add(Label::new("AbleTK!"))) .add(Label::new("this is a label!") - .bg_color(rgb!(0xFF0000FF)))) + .bg_color(rgb!(0xFF0000FF))) + .padding_left(10)) .on_event(WindowEvent::Closed, |_, window| { window.set_title("CLOSING!") })) diff --git a/src/widget/mod.rs b/src/widget/mod.rs index 05f0ff8..06a4534 100755 --- a/src/widget/mod.rs +++ b/src/widget/mod.rs @@ -8,11 +8,13 @@ mod column; mod label; +mod padding; mod row; use abletk_common::Renderer; pub use column::*; pub use label::*; +pub use padding::*; pub use row::*; use crate::layout::position::Position; diff --git a/src/widget/padding.rs b/src/widget/padding.rs new file mode 100755 index 0000000..168d788 --- /dev/null +++ b/src/widget/padding.rs @@ -0,0 +1,98 @@ +/* + * 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 Padding { + widget: W, + left: u32, + right: u32, + top: u32, + bottom: u32, +} + +impl Widget for Padding { + fn draw(&self, renderer: &mut Renderer) { + self.widget.draw(renderer) + } + + fn position(&self, renderer: &mut Renderer) -> Position { + let pos = self.widget.position(renderer); + + Position::new( + self.left, + self.top, + pos.width() + self.right, + pos.height() + self.bottom, + ) + } +} + +impl Padding { + pub fn new( + left: u32, + right: u32, + top: u32, + bottom: u32, + widget: W, + ) -> Self { + Self { + widget, + left, + right, + top, + bottom, + } + } +} + +pub trait IntoPadded { + fn padding(self, left: u32, right: u32, top: u32, bottom: u32) -> Padding; + fn padding_x(self, left: u32, right: u32) -> Padding; + fn padding_y(self, top: u32, bottom: u32) -> Padding; + fn padding_left(self, left: u32) -> Padding; + fn padding_right(self, right: u32) -> Padding; + fn padding_top(self, top: u32) -> Padding; + fn padding_bottom(self, bottom: u32) -> Padding; + fn padding_all(self, padding: u32) -> Padding; +} + +impl IntoPadded for W { + fn padding(self, left: u32, right: u32, top: u32, bottom: u32) -> Padding { + Padding::new(left, right, top, bottom, self) + } + + fn padding_x(self, left: u32, right: u32) -> Padding { + Padding::new(left, right, 0, 0, self) + } + + fn padding_y(self, top: u32, bottom: u32) -> Padding { + Padding::new(0, 0, top, bottom, self) + } + + fn padding_left(self, left: u32) -> Padding { + Padding::new(left, 0, 0, 0, self) + } + + fn padding_right(self, right: u32) -> Padding { + Padding::new(0, right, 0, 0, self) + } + + fn padding_top(self, top: u32) -> Padding { + Padding::new(0, 0, top, 0, self) + } + + fn padding_bottom(self, bottom: u32) -> Padding { + Padding::new(0, 0, 0, bottom, self) + } + + fn padding_all(self, padding: u32) -> Padding { + Padding::new(padding, padding, padding, padding, self) + } +}