mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2025-12-06 11:46:42 +01:00
91 lines
2.3 KiB
Rust
91 lines
2.3 KiB
Rust
use crate::*;
|
|
use std::sync::{Arc, atomic::{AtomicUsize, Ordering::Relaxed}};
|
|
|
|
pub trait HasSize<E: Output> {
|
|
fn size (&self) -> &Measure<E>;
|
|
fn width (&self) -> usize {
|
|
self.size().w()
|
|
}
|
|
fn height (&self) -> usize {
|
|
self.size().h()
|
|
}
|
|
}
|
|
|
|
impl<E: Output, T: Has<Measure<E>>> HasSize<E> for T {
|
|
fn size (&self) -> &Measure<E> {
|
|
self.get()
|
|
}
|
|
}
|
|
|
|
/// A widget that tracks its render width and height
|
|
#[derive(Default)]
|
|
pub struct Measure<E: Output> {
|
|
_engine: PhantomData<E>,
|
|
pub x: Arc<AtomicUsize>,
|
|
pub y: Arc<AtomicUsize>,
|
|
}
|
|
|
|
// TODO: 🡘 🡙 ←🡙→ indicator to expand window when too small
|
|
impl<E: Output> Content<E> for Measure<E> {
|
|
fn render (&self, to: &mut E) {
|
|
self.x.store(to.area().w().into(), Relaxed);
|
|
self.y.store(to.area().h().into(), Relaxed);
|
|
}
|
|
}
|
|
|
|
impl<E: Output> Clone for Measure<E> {
|
|
fn clone (&self) -> Self {
|
|
Self {
|
|
_engine: Default::default(),
|
|
x: self.x.clone(),
|
|
y: self.y.clone(),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl<E: Output> std::fmt::Debug for Measure<E> {
|
|
fn fmt (&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
|
|
f.debug_struct("Measure")
|
|
.field("width", &self.x)
|
|
.field("height", &self.y)
|
|
.finish()
|
|
}
|
|
}
|
|
|
|
impl<E: Output> Measure<E> {
|
|
pub fn new () -> Self {
|
|
Self {
|
|
_engine: PhantomData::default(),
|
|
x: Arc::new(0.into()),
|
|
y: Arc::new(0.into()),
|
|
}
|
|
}
|
|
pub fn set_w (&self, w: impl Into<usize>) -> &Self {
|
|
self.x.store(w.into(), Relaxed);
|
|
self
|
|
}
|
|
pub fn set_h (&self, h: impl Into<usize>) -> &Self {
|
|
self.y.store(h.into(), Relaxed);
|
|
self
|
|
}
|
|
pub fn set_wh (&self, w: impl Into<usize>, h: impl Into<usize>) -> &Self {
|
|
self.set_w(w);
|
|
self.set_h(h);
|
|
self
|
|
}
|
|
pub fn w (&self) -> usize {
|
|
self.x.load(Relaxed)
|
|
}
|
|
pub fn h (&self) -> usize {
|
|
self.y.load(Relaxed)
|
|
}
|
|
pub fn wh (&self) -> [usize;2] {
|
|
[self.w(), self.h()]
|
|
}
|
|
pub fn format (&self) -> Arc<str> {
|
|
format!("{}x{}", self.w(), self.h()).into()
|
|
}
|
|
pub fn of <T: Render<E>> (&self, item: T) -> Bsp<Fill<&Self>, T> {
|
|
Bsp::b(Fill::xy(self), item)
|
|
}
|
|
}
|