dash-frontend: clock, wgui: refactoring, non-panicking casts

This commit is contained in:
Aleksander
2025-09-16 20:09:13 +02:00
parent 129785daa7
commit 0fdc0e3828
20 changed files with 287 additions and 165 deletions

View File

@@ -1,10 +1,16 @@
use slotmap::Key;
use crate::layout::WidgetID;
use super::{WidgetObj, WidgetState};
pub struct WidgetDiv {}
pub struct WidgetDiv {
id: WidgetID,
}
impl WidgetDiv {
pub fn create() -> WidgetState {
WidgetState::new(Box::new(Self {}))
WidgetState::new(Box::new(Self { id: WidgetID::null() }))
}
}
@@ -12,4 +18,12 @@ impl WidgetObj for WidgetDiv {
fn draw(&mut self, _state: &mut super::DrawState, _params: &super::DrawParams) {
// no-op
}
fn get_id(&self) -> WidgetID {
self.id
}
fn set_id(&mut self, id: WidgetID) {
self.id = id;
}
}

View File

@@ -1,12 +1,15 @@
use std::{cell::RefCell, rc::Rc};
use cosmic_text::{Attrs, Buffer, Metrics, Shaping, Wrap};
use slotmap::Key;
use taffy::AvailableSpace;
use crate::{
drawing::{self, Boundary},
event::CallbackDataCommon,
globals::Globals,
i18n::{I18n, Translation},
layout::WidgetID,
renderer_vk::text::{FONT_SYSTEM, TextStyle},
};
@@ -19,6 +22,8 @@ pub struct WidgetLabelParams {
}
pub struct WidgetLabel {
id: WidgetID,
params: WidgetLabelParams,
buffer: Rc<RefCell<Buffer>>,
last_boundary: Boundary,
@@ -52,12 +57,15 @@ impl WidgetLabel {
params,
buffer: Rc::new(RefCell::new(buffer)),
last_boundary: Boundary::default(),
id: WidgetID::null(),
}))
}
pub fn set_text(&mut self, i18n: &mut I18n, translation: Translation) {
// set text without layout/re-render update.
// Not recommended unless the widget wasn't rendered yet (first init).
pub fn set_text_simple(&mut self, i18n: &mut I18n, translation: Translation) -> bool {
if self.params.content == translation {
return;
return false;
}
self.params.content = translation;
@@ -72,6 +80,15 @@ impl WidgetLabel {
Shaping::Advanced,
self.params.style.align.map(Into::into),
);
true
}
// set text and check if it needs to be re-rendered/re-layouted
pub fn set_text(&mut self, common: &mut CallbackDataCommon, translation: Translation) {
if self.set_text_simple(&mut common.i18n(), translation) {
common.mark_widget_dirty(self.id);
}
}
}
@@ -118,4 +135,12 @@ impl WidgetObj for WidgetLabel {
let height = total_lines as f32 * buffer.metrics().line_height;
taffy::Size { width, height }
}
fn get_id(&self) -> WidgetID {
self.id
}
fn set_id(&mut self, id: WidgetID) {
self.id = id;
}
}

View File

@@ -111,6 +111,10 @@ pub struct DrawParams<'a> {
}
pub trait WidgetObj: AnyTrait {
// every widget stores their of id for convenience reasons
fn get_id(&self) -> WidgetID;
fn set_id(&mut self, id: WidgetID); // always set at insertion
fn draw(&mut self, state: &mut DrawState, params: &DrawParams);
fn measure(
&mut self,
@@ -173,18 +177,14 @@ 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 {
pub fn get_as<T: 'static>(&self) -> Option<&T> {
let any = self.as_any();
any.downcast_ref::<T>().unwrap()
any.downcast_ref::<T>()
}
// panics on failure
// TODO: panic-less alternative
pub fn get_as_mut<T: 'static>(&mut self) -> &mut T {
pub fn get_as_mut<T: 'static>(&mut self) -> Option<&mut T> {
let any = self.as_any_mut();
any.downcast_mut::<T>().unwrap()
any.downcast_mut::<T>()
}
}

View File

@@ -1,5 +1,8 @@
use slotmap::Key;
use crate::{
drawing::{self, GradientMode},
layout::WidgetID,
widget::util::WLength,
};
@@ -19,11 +22,15 @@ pub struct WidgetRectangleParams {
pub struct WidgetRectangle {
pub params: WidgetRectangleParams,
id: WidgetID,
}
impl WidgetRectangle {
pub fn create(params: WidgetRectangleParams) -> WidgetState {
WidgetState::new(Box::new(Self { params }))
WidgetState::new(Box::new(Self {
params,
id: WidgetID::null(),
}))
}
}
@@ -33,9 +40,7 @@ impl WidgetObj for WidgetRectangle {
let round_units = match self.params.round {
WLength::Units(units) => units as u8,
WLength::Percent(percent) => {
(f32::min(boundary.size.x, boundary.size.y) * percent / 2.0) as u8
}
WLength::Percent(percent) => (f32::min(boundary.size.x, boundary.size.y) * percent / 2.0) as u8,
};
state.primitives.push(drawing::RenderPrimitive {
@@ -52,4 +57,12 @@ impl WidgetObj for WidgetRectangle {
}),
});
}
fn get_id(&self) -> WidgetID {
self.id
}
fn set_id(&mut self, id: WidgetID) {
self.id = id;
}
}

View File

@@ -1,9 +1,11 @@
use std::{cell::RefCell, rc::Rc};
use cosmic_text::{Attrs, Buffer, Color, Shaping, Weight};
use slotmap::Key;
use crate::{
drawing::{self},
layout::WidgetID,
renderer_vk::text::{
DEFAULT_METRICS, FONT_SYSTEM,
custom_glyph::{CustomGlyph, CustomGlyphData},
@@ -21,11 +23,15 @@ pub struct WidgetSpriteParams {
#[derive(Debug, Default)]
pub struct WidgetSprite {
params: WidgetSpriteParams,
id: WidgetID,
}
impl WidgetSprite {
pub fn create(params: WidgetSpriteParams) -> WidgetState {
WidgetState::new(Box::new(Self { params }))
WidgetState::new(Box::new(Self {
params,
id: WidgetID::null(),
}))
}
}
@@ -62,9 +68,7 @@ impl WidgetObj for WidgetSprite {
{
let mut font_system = FONT_SYSTEM.lock();
let mut buffer = buffer.borrow_with(&mut font_system);
let attrs = Attrs::new()
.color(Color::rgb(255, 0, 255))
.weight(Weight::BOLD);
let attrs = Attrs::new().color(Color::rgb(255, 0, 255)).weight(Weight::BOLD);
// set text last in order to avoid expensive re-shaping
buffer.set_text("Error", &attrs, Shaping::Basic);
@@ -85,4 +89,12 @@ impl WidgetObj for WidgetSprite {
) -> taffy::Size<f32> {
taffy::Size::ZERO
}
fn get_id(&self) -> WidgetID {
self.id
}
fn set_id(&mut self, id: WidgetID) {
self.id = id;
}
}