new padding widget

This commit is contained in:
TheOddGarlic 2022-05-08 21:05:46 +03:00
parent b3aab25989
commit 9b3969c6ad
4 changed files with 105 additions and 2 deletions

View file

@ -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;

View file

@ -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!")
}))

View file

@ -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;

98
src/widget/padding.rs Executable file
View file

@ -0,0 +1,98 @@
/*
* Copyright (C) 2022 Umut İnan Erdoğan <umutinanerdogan@pm.me>
*
* 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<W: Widget> {
widget: W,
left: u32,
right: u32,
top: u32,
bottom: u32,
}
impl<W: Widget> Widget for Padding<W> {
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<W: Widget> Padding<W> {
pub fn new(
left: u32,
right: u32,
top: u32,
bottom: u32,
widget: W,
) -> Self {
Self {
widget,
left,
right,
top,
bottom,
}
}
}
pub trait IntoPadded<W: Widget> {
fn padding(self, left: u32, right: u32, top: u32, bottom: u32) -> Padding<W>;
fn padding_x(self, left: u32, right: u32) -> Padding<W>;
fn padding_y(self, top: u32, bottom: u32) -> Padding<W>;
fn padding_left(self, left: u32) -> Padding<W>;
fn padding_right(self, right: u32) -> Padding<W>;
fn padding_top(self, top: u32) -> Padding<W>;
fn padding_bottom(self, bottom: u32) -> Padding<W>;
fn padding_all(self, padding: u32) -> Padding<W>;
}
impl<W: Widget> IntoPadded<W> for W {
fn padding(self, left: u32, right: u32, top: u32, bottom: u32) -> Padding<W> {
Padding::new(left, right, top, bottom, self)
}
fn padding_x(self, left: u32, right: u32) -> Padding<W> {
Padding::new(left, right, 0, 0, self)
}
fn padding_y(self, top: u32, bottom: u32) -> Padding<W> {
Padding::new(0, 0, top, bottom, self)
}
fn padding_left(self, left: u32) -> Padding<W> {
Padding::new(left, 0, 0, 0, self)
}
fn padding_right(self, right: u32) -> Padding<W> {
Padding::new(0, right, 0, 0, self)
}
fn padding_top(self, top: u32) -> Padding<W> {
Padding::new(0, 0, top, 0, self)
}
fn padding_bottom(self, bottom: u32) -> Padding<W> {
Padding::new(0, 0, 0, bottom, self)
}
fn padding_all(self, padding: u32) -> Padding<W> {
Padding::new(padding, padding, padding, padding, self)
}
}