wgui: interactable components, rename TextLabel -> WidgetLabel

This commit is contained in:
Aleksander
2025-08-10 11:46:01 +02:00
parent 91e584383f
commit 93a3fee349
26 changed files with 294 additions and 174 deletions

View File

@@ -1,14 +1,14 @@
use super::{WidgetObj, WidgetState};
pub struct Div {}
pub struct WidgetDiv {}
impl Div {
impl WidgetDiv {
pub fn create() -> anyhow::Result<WidgetState> {
WidgetState::new(Box::new(Self {}))
}
}
impl WidgetObj for Div {
impl WidgetObj for WidgetDiv {
fn draw(&mut self, _state: &mut super::DrawState, _params: &super::DrawParams) {
// no-op
}

View File

@@ -12,19 +12,19 @@ use crate::{
use super::{WidgetObj, WidgetState};
#[derive(Default)]
pub struct TextParams {
pub struct WidgetLabelParams {
pub content: Translation,
pub style: TextStyle,
}
pub struct TextLabel {
params: TextParams,
pub struct WidgetLabel {
params: WidgetLabelParams,
buffer: Rc<RefCell<Buffer>>,
last_boundary: Boundary,
}
impl TextLabel {
pub fn create(i18n: &mut I18n, params: TextParams) -> anyhow::Result<WidgetState> {
impl WidgetLabel {
pub fn create(i18n: &mut I18n, params: WidgetLabelParams) -> anyhow::Result<WidgetState> {
let metrics = Metrics::from(&params.style);
let attrs = Attrs::from(&params.style);
let wrap = Wrap::from(&params.style);
@@ -70,7 +70,7 @@ impl TextLabel {
}
}
impl WidgetObj for TextLabel {
impl WidgetObj for WidgetLabel {
fn draw(&mut self, state: &mut super::DrawState, _params: &super::DrawParams) {
let boundary = drawing::Boundary::construct(state.transform_stack);

View File

@@ -14,9 +14,9 @@ use crate::{
};
pub mod div;
pub mod label;
pub mod rectangle;
pub mod sprite;
pub mod text;
pub mod util;
pub struct WidgetData {
@@ -173,11 +173,15 @@ pub fn get_scrollbar_info(l: &taffy::Layout) -> Option<ScrollbarInfo> {
}
impl dyn WidgetObj {
// panics on failure
// TODO: panic-less alternative
pub fn get_as<T: 'static>(&self) -> &T {
let any = self.as_any();
any.downcast_ref::<T>().unwrap()
}
// panics on failure
// TODO: panic-less alternative
pub fn get_as_mut<T: 'static>(&mut self) -> &mut T {
let any = self.as_any_mut();
any.downcast_mut::<T>().unwrap()

View File

@@ -6,7 +6,7 @@ use crate::{
use super::{WidgetObj, WidgetState};
#[derive(Default)]
pub struct RectangleParams {
pub struct WidgetRectangleParams {
pub color: drawing::Color,
pub color2: drawing::Color,
pub gradient: GradientMode,
@@ -17,17 +17,17 @@ pub struct RectangleParams {
pub round: WLength,
}
pub struct Rectangle {
pub params: RectangleParams,
pub struct WidgetRectangle {
pub params: WidgetRectangleParams,
}
impl Rectangle {
pub fn create(params: RectangleParams) -> anyhow::Result<WidgetState> {
WidgetState::new(Box::new(Rectangle { params }))
impl WidgetRectangle {
pub fn create(params: WidgetRectangleParams) -> anyhow::Result<WidgetState> {
WidgetState::new(Box::new(WidgetRectangle { params }))
}
}
impl WidgetObj for Rectangle {
impl WidgetObj for WidgetRectangle {
fn draw(&mut self, state: &mut super::DrawState, _params: &super::DrawParams) {
let boundary = drawing::Boundary::construct(state.transform_stack);

View File

@@ -12,24 +12,24 @@ use crate::{
use super::{WidgetObj, WidgetState};
#[derive(Default)]
pub struct SpriteBoxParams {
#[derive(Debug, Default)]
pub struct WidgetSpriteParams {
pub glyph_data: Option<CustomGlyphData>,
pub color: Option<drawing::Color>,
}
#[derive(Default)]
pub struct SpriteBox {
params: SpriteBoxParams,
#[derive(Debug, Default)]
pub struct WidgetSprite {
params: WidgetSpriteParams,
}
impl SpriteBox {
pub fn create(params: SpriteBoxParams) -> anyhow::Result<WidgetState> {
impl WidgetSprite {
pub fn create(params: WidgetSpriteParams) -> anyhow::Result<WidgetState> {
WidgetState::new(Box::new(Self { params }))
}
}
impl WidgetObj for SpriteBox {
impl WidgetObj for WidgetSprite {
fn draw(&mut self, state: &mut super::DrawState, _params: &super::DrawParams) {
let boundary = drawing::Boundary::construct(state.transform_stack);