preparation

This commit is contained in:
2025-09-09 21:53:32 -04:00
parent 15cc91d92a
commit 709a2d0e17
14 changed files with 292 additions and 220 deletions

View File

@@ -35,6 +35,10 @@ impl<T: ColorNum> Color<T> {
self.a = a;
self
}
pub fn as_arr(self) -> [T; 4] {
[self.r, self.g, self.b, self.a]
}
}
pub trait ColorNum {

View File

@@ -1,4 +1,12 @@
use std::{any::TypeId, marker::PhantomData, sync::mpsc::Sender};
use std::{
any::TypeId,
marker::PhantomData,
sync::{
Arc,
atomic::{AtomicBool, Ordering},
mpsc::Sender,
},
};
use crate::{
layout::{FnTag, Ui, Widget, WidgetLike, WidgetTag},
@@ -21,7 +29,7 @@ pub struct WidgetId<W = AnyWidget> {
pub(super) id: Id,
counter: RefCounter,
send: Sender<Id>,
is_static: bool,
is_static: Arc<AtomicBool>,
_pd: PhantomData<W>,
}
@@ -52,7 +60,7 @@ impl<W> Clone for WidgetId<W> {
ty: self.ty,
counter: self.counter.clone(),
send: self.send.clone(),
is_static: self.is_static,
is_static: self.is_static.clone(),
_pd: PhantomData,
}
}
@@ -65,7 +73,7 @@ impl<W> WidgetId<W> {
id,
counter: RefCounter::new(),
send,
is_static,
is_static: Arc::new(is_static.into()),
_pd: PhantomData,
}
}
@@ -88,11 +96,11 @@ impl<W> WidgetId<W> {
self.counter.refs()
}
pub(super) fn into_static(self) -> StaticWidgetId<W> {
let s = std::mem::ManuallyDrop::new(self);
pub fn into_static(self) -> StaticWidgetId<W> {
self.is_static.store(true, Ordering::Release);
StaticWidgetId {
ty: s.ty,
id: s.id.copyable(),
ty: self.ty,
id: self.id.copyable(),
_pd: PhantomData,
}
}
@@ -110,7 +118,7 @@ impl WidgetId {
impl<W> Drop for WidgetId<W> {
fn drop(&mut self) {
if self.counter.drop() && !self.is_static {
if self.counter.drop() && !self.is_static.load(Ordering::Acquire) {
let _ = self.send.send(self.id.duplicate());
}
}
@@ -119,21 +127,14 @@ impl<W> Drop for WidgetId<W> {
pub struct IdTag;
pub struct IdFnTag;
// pub trait WidgetIdFn<W, Ctx> = FnOnce(&mut Ui) -> WidgetId<W>;
macro_rules! WidgetIdFn {
($W:ty) => {
impl FnOnce(&mut $crate::layout::Ui) -> $crate::layout::WidgetId<$W>
};
($W:ty, $($use:tt)*) => {
impl FnOnce(&mut $crate::layout::Ui) -> $crate::layout::WidgetId<$W> + use<$($use)*>
};
}
pub(crate) use WidgetIdFn;
pub trait WidgetIdFn<W>: FnOnce(&mut Ui) -> WidgetId<W> {}
impl<W, F: FnOnce(&mut Ui) -> WidgetId<W>> WidgetIdFn<W> for F {}
/// TODO: does this ever make sense to use? it allows for invalid ids
pub trait Idable<Tag> {
type Widget: Widget;
fn set(self, ui: &mut Ui, id: &WidgetId<Self::Widget>);
fn id<'a>(self, id: &WidgetId<Self::Widget>) -> WidgetIdFn!(Self::Widget, 'a, Self, Tag)
fn id(self, id: &WidgetId<Self::Widget>) -> impl WidgetIdFn<Self::Widget>
where
Self: Sized,
{
@@ -143,10 +144,7 @@ pub trait Idable<Tag> {
id
}
}
fn id_static<'a>(
self,
id: StaticWidgetId<Self::Widget>,
) -> WidgetIdFn!(Self::Widget, 'a, Self, Tag)
fn id_static(self, id: StaticWidgetId<Self::Widget>) -> impl WidgetIdFn<Self::Widget>
where
Self: Sized,
{

View File

@@ -2,8 +2,8 @@ use std::ops::Range;
use crate::{
layout::{
Active, SensorMap, SizeCtx, TextAttrs, TextBuffer, TextData, TextOffset, TextureHandle,
Textures, UiRegion, Vec2, WidgetId, Widgets,
Active, Cursor, SensorMap, SizeCtx, TextAttrs, TextBuffer, TextData, TextOffset,
TextureHandle, Textures, UiRegion, Vec2, WidgetId, Widgets,
},
render::{Primitive, PrimitiveHandle, Primitives},
util::{HashSet, Id},
@@ -251,14 +251,15 @@ impl<'a, 'c> Painter<'a, 'c> {
buffer: &mut TextBuffer,
content: &str,
attrs: &TextAttrs,
cursor: &Cursor,
) -> (TextureHandle, TextOffset) {
self.ctx
.text
.draw(buffer, content, attrs, self.ctx.textures)
.draw(buffer, content, attrs, cursor, self.ctx.textures)
}
pub fn region_size(&self) -> Vec2 {
self.region.in_size(self.ctx.screen_size)
pub fn region(&self) -> UiRegion {
self.region
}
pub fn size<W>(&mut self, id: &WidgetId<W>) -> Vec2 {

View File

@@ -181,8 +181,8 @@ impl UiRegion {
self
}
pub fn to_screen(&self, size: Vec2) -> ScreenRect {
ScreenRect {
pub fn to_screen(&self, size: Vec2) -> ScreenRegion {
ScreenRegion {
top_left: self.top_left.anchor * size + self.top_left.offset,
bot_right: self.bot_right.anchor * size + self.bot_right.offset,
}
@@ -213,11 +213,11 @@ impl UiRegion {
}
#[derive(Debug)]
pub struct ScreenRect {
top_left: Vec2,
bot_right: Vec2,
pub struct ScreenRegion {
pub top_left: Vec2,
pub bot_right: Vec2,
}
impl ScreenRect {
impl ScreenRegion {
pub fn contains(&self, pos: Vec2) -> bool {
pos.x >= self.top_left.x
&& pos.x <= self.bot_right.x

View File

@@ -1,3 +1,5 @@
use std::any::Any;
use crate::{
layout::{Ui, UiRegion, Vec2, WidgetId},
util::{HashMap, Id, bitflags},
@@ -42,15 +44,15 @@ pub struct SensorGroup {
pub cursor: ActivationState,
pub sensors: Vec<Sensor>,
}
pub trait SenseFn: FnMut(&mut Ui) + 'static {
fn box_clone(&self) -> Box<dyn SenseFn>;
}
impl<F: FnMut(&mut Ui) + 'static + Clone> SenseFn for F {
fn box_clone(&self) -> Box<dyn SenseFn> {
Box::new(self.clone())
}
pub struct SenseCtx {
pub cursor: Vec2,
pub size: Vec2,
}
pub trait SenseFn: FnMut(&mut Ui, SenseCtx) + 'static {}
impl<F: FnMut(&mut Ui, SenseCtx) + 'static> SenseFn for F {}
impl Ui {
pub fn add_sensor<W>(&mut self, id: &WidgetId<W>, f: Sensor) {
self.sensor_map
@@ -72,7 +74,11 @@ impl Ui {
for sensor in &mut group.sensors {
if should_run(sensor.senses, group.cursor, group.hover) {
(sensor.f.box_clone())(self);
let ctx = SenseCtx {
cursor: cursor.pos - region.top_left,
size: region.bot_right - region.top_left,
};
(sensor.f)(self, ctx);
}
}
}
@@ -132,21 +138,3 @@ impl ActivationState {
}
}
}
impl Clone for SensorGroup {
fn clone(&self) -> Self {
Self {
hover: self.hover,
cursor: self.cursor,
sensors: self.sensors.clone(),
}
}
}
impl Clone for Sensor {
fn clone(&self) -> Self {
Self {
senses: self.senses,
f: self.f.box_clone(),
}
}
}

View File

@@ -28,6 +28,16 @@ pub struct TextAttrs {
pub family: Family<'static>,
}
#[derive(Default)]
pub enum Cursor {
#[default]
None,
Select {
line: usize,
idx: usize,
},
}
pub type TextBuffer = Buffer;
impl Default for TextAttrs {
@@ -48,6 +58,7 @@ impl TextData {
buffer: &mut TextBuffer,
content: &str,
attrs: &TextAttrs,
cursor: &Cursor,
textures: &mut Textures,
) -> (TextureHandle, TextOffset) {
buffer.set_metrics(
@@ -67,7 +78,14 @@ impl TextData {
let mut max_y = 0;
let c = attrs.color;
let mut max_width = 0.0f32;
for run in buffer.layout_runs() {
let mut i = 0;
let mut cursor_x = 0;
for (run_i, run) in buffer.layout_runs().enumerate() {
if let Cursor::Select { line, .. } = cursor
&& *line == run_i
{
cursor_x = run.line_w as i32;
}
for glyph in run.glyphs.iter() {
let physical_glyph = glyph.physical((0., 0.), 1.0);
@@ -76,6 +94,14 @@ impl TextData {
None => cosmic_text::Color::rgba(c.r, c.g, c.b, c.a),
};
if let Cursor::Select { idx, line } = cursor
&& *line == run_i
&& *idx == i
{
cursor_x = physical_glyph.x;
}
i += 1;
self.swash_cache.with_pixels(
&mut self.font_system,
physical_glyph.cache_key,
@@ -93,6 +119,11 @@ impl TextData {
}
max_width = max_width.max(run.line_w);
}
if let &Cursor::Select { line, .. } = cursor {
let y = (attrs.line_height * (line + 1) as f32) as i32 - 1;
max_y = max_y.max(y);
max_x = max_x.max(cursor_x);
}
let width = (max_x - min_x + 1) as u32;
let height = (max_y - min_y + 1) as u32;
let mut image = RgbaImage::new(width, height);
@@ -101,6 +132,15 @@ impl TextData {
let y = (y - min_y) as u32;
image.put_pixel(x, y, color);
}
if let &Cursor::Select { line, .. } = cursor {
for y in 0..attrs.line_height as u32 {
// no clue if this is good or bad for non integer values
// depends on how the layouting is actually done
let x = (cursor_x - min_x) as u32;
let y = (y as f32 + attrs.line_height * line as f32 - min_y as f32) as u32;
image.put_pixel(x, y, Rgba(c.as_arr()));
}
}
let offset = TextOffset {
top_left: Vec2::new(min_x as f32, min_y as f32),
bot_right: Vec2::new(

View File

@@ -15,9 +15,8 @@ use std::{
};
pub struct Ui {
base: Option<WidgetId>,
root: Option<WidgetId>,
widgets: Widgets,
labels: HashMap<Id, String>,
updates: Vec<Id>,
recv: Receiver<Id>,
pub(super) send: Sender<Id>,
@@ -41,6 +40,7 @@ pub struct Widgets {
pub struct WidgetData {
widget: Box<dyn Widget>,
borrowed: bool,
label: String,
}
impl Ui {
@@ -58,7 +58,7 @@ impl Ui {
/// useful for debugging
pub fn set_label<W>(&mut self, id: &WidgetId<W>, label: String) {
self.labels.insert(id.id.duplicate(), label);
*self.widgets.label_mut(id).unwrap() = label;
}
pub fn add_widget<W: Widget>(&mut self, w: W) -> WidgetId<W> {
@@ -67,8 +67,6 @@ impl Ui {
pub fn push<W: Widget>(&mut self, w: W) -> WidgetId<W> {
let id = self.id();
self.labels
.insert(id.id.duplicate(), std::any::type_name::<W>().to_string());
self.widgets.insert(id.id.duplicate(), w);
id
}
@@ -77,8 +75,8 @@ impl Ui {
self.widgets.insert(id.id.duplicate(), w);
}
pub fn set_base<Tag>(&mut self, w: impl WidgetLike<Tag>) {
self.base = Some(w.add(self).erase_type());
pub fn set_root<Tag>(&mut self, w: impl WidgetLike<Tag>) {
self.root = Some(w.add(self).erase_type());
self.full_redraw = true;
}
@@ -129,8 +127,8 @@ impl Ui {
&mut self.text,
self.size,
);
if let Some(base) = &self.base {
ctx.draw(&base.id);
if let Some(root) = &self.root {
ctx.draw(&root.id);
}
}
@@ -162,7 +160,6 @@ impl Ui {
/// free any resources that don't have references anymore
fn free(&mut self) {
for id in self.recv.try_iter() {
self.labels.remove(&id);
self.widgets.delete(id);
}
self.textures.free();
@@ -175,6 +172,10 @@ impl Ui {
pub fn num_widgets(&self) -> usize {
self.widgets.len()
}
pub fn active_widgets(&self) -> usize {
self.active.widgets.len()
}
}
impl<W: Widget> Index<&WidgetId<W>> for Ui {
@@ -254,16 +255,25 @@ impl Widgets {
self.get_dyn_mut(&id.id)?.as_any_mut().downcast_mut()
}
pub fn insert(&mut self, id: Id, widget: impl Widget) {
self.insert_any(id, Box::new(widget));
pub fn insert<W: Widget>(&mut self, id: Id, widget: W) {
self.insert_any(id, Box::new(widget), std::any::type_name::<W>().to_string());
}
pub fn insert_any(&mut self, id: Id, widget: Box<dyn Widget>) {
pub fn label<W>(&self, id: &WidgetId<W>) -> Option<&String> {
self.map.get(&id.id).map(|d| &d.label)
}
pub fn label_mut<W>(&mut self, id: &WidgetId<W>) -> Option<&mut String> {
self.map.get_mut(&id.id).map(|d| &mut d.label)
}
pub fn insert_any(&mut self, id: Id, widget: Box<dyn Widget>, label: String) {
self.map.insert(
id,
WidgetData {
widget,
borrowed: false,
label,
},
);
}
@@ -331,9 +341,8 @@ impl Default for Ui {
fn default() -> Self {
let (send, recv) = channel();
Self {
base: Default::default(),
root: Default::default(),
widgets: Widgets::new(),
labels: Default::default(),
updates: Default::default(),
primitives: Default::default(),
textures: Textures::new(),

View File

@@ -1,4 +1,7 @@
use crate::layout::{Painter, TextData, Textures, Ui, Vec2, WidgetId, WidgetIdFn, Widgets};
use crate::layout::{
Cursor, Painter, StaticWidgetId, TextAttrs, TextBuffer, TextData, TextOffset, TextureHandle,
Textures, Ui, Vec2, WidgetId, WidgetIdFn, Widgets,
};
use std::{any::Any, marker::PhantomData};
@@ -20,6 +23,16 @@ impl SizeCtx<'_> {
pub fn size<W>(&mut self, id: &WidgetId<W>) -> Vec2 {
self.widgets.get_dyn_dynamic(&id.id).get_size(self)
}
pub fn draw_text(
&mut self,
buffer: &mut TextBuffer,
content: &str,
attrs: &TextAttrs,
cursor: &Cursor,
) -> (TextureHandle, TextOffset) {
self.text
.draw(buffer, content, attrs, cursor, self.textures)
}
}
pub struct WidgetTag;
@@ -31,7 +44,7 @@ pub trait WidgetLike<Tag> {
fn with_id<W2>(
self,
f: impl FnOnce(&mut Ui, WidgetId<Self::Widget>) -> WidgetId<W2>,
) -> WidgetIdFn!(W2)
) -> impl WidgetIdFn<W2>
where
Self: Sized,
{
@@ -40,20 +53,19 @@ pub trait WidgetLike<Tag> {
f(ui, id)
}
}
fn add_static(self, ui: &mut Ui) -> StaticWidgetId<Self::Widget>
where
Self: Sized,
{
self.add(ui).into_static()
}
}
// pub trait WidgetFn<W: Widget<Ctx>, Ctx> = FnOnce(&mut Ui<Ctx>) -> W;
/// A function that returns a widget given a UI.
/// Useful for defining trait functions on widgets that create a parent widget so that the children
/// don't need to be IDs yet
/// currently a macro for rust analyzer (doesn't support trait aliases atm)
macro_rules! WidgetFn {
($W:ty) => {
impl FnOnce(&mut $crate::layout::Ui) -> $W
};
}
pub(crate) use WidgetFn;
pub trait WidgetFn<W: Widget>: FnOnce(&mut Ui) -> W {}
impl<W: Widget, F: FnOnce(&mut Ui) -> W> WidgetFn<W> for F {}
impl<W: Widget, F: FnOnce(&mut Ui) -> W> WidgetLike<FnTag> for F {
type Widget = W;