126 lines
3.4 KiB
Rust
Executable File
126 lines
3.4 KiB
Rust
Executable File
/*
|
|
* 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, window::Window};
|
|
use abletk_common::Renderer;
|
|
use winit::event::MouseButton;
|
|
|
|
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,
|
|
)
|
|
}
|
|
|
|
fn mouse_pressed(&mut self,
|
|
renderer: &mut Renderer,
|
|
window: &mut Window,
|
|
button: MouseButton,
|
|
x: u32,
|
|
y: u32,
|
|
) {
|
|
let pos = self.position(renderer);
|
|
if pos.contains(x, y) {
|
|
self.widget.mouse_pressed(renderer, window, button, x - pos.x(), y - pos.y());
|
|
}
|
|
}
|
|
|
|
fn mouse_released(&mut self,
|
|
renderer: &mut Renderer,
|
|
window: &mut Window,
|
|
button: MouseButton,
|
|
x: u32,
|
|
y: u32,
|
|
) {
|
|
let pos = self.position(renderer);
|
|
if pos.contains(x, y) {
|
|
self.widget.mouse_released(renderer, window, button, x - pos.x(), y - pos.y());
|
|
}
|
|
}
|
|
}
|
|
|
|
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 Padded<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> Padded<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)
|
|
}
|
|
}
|