refactor: pedantic cargo clippy, do not use Results for always-succeeding functions
This commit is contained in:
@@ -17,18 +17,18 @@ pub enum AnimationEasing {
|
||||
impl AnimationEasing {
|
||||
fn interpolate(&self, x: f32) -> f32 {
|
||||
match self {
|
||||
AnimationEasing::Linear => x,
|
||||
AnimationEasing::InQuad => x * x,
|
||||
AnimationEasing::OutQuad => 1.0 - (1.0 - x) * (1.0 - x),
|
||||
AnimationEasing::OutBack => {
|
||||
Self::Linear => x,
|
||||
Self::InQuad => x * x,
|
||||
Self::OutQuad => 1.0 - (1.0 - x) * (1.0 - x),
|
||||
Self::OutBack => {
|
||||
let a = 1.7;
|
||||
let b = a + 1.0;
|
||||
1.0 + b * (x - 1.0).powf(3.0) + a * (x - 1.0).powf(2.0)
|
||||
1.0 + b * (x - 1.0).powi(3) + a * (x - 1.0).powi(2)
|
||||
}
|
||||
AnimationEasing::InBack => {
|
||||
Self::InBack => {
|
||||
let a = 1.7;
|
||||
let b = a + 1.0;
|
||||
b * x.powf(3.0) - a * x.powf(2.0)
|
||||
b * x.powi(3) - a * x.powi(2)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -46,7 +46,7 @@ pub type AnimationCallback = Box<dyn Fn(&mut CallbackDataCommon, &mut CallbackDa
|
||||
pub struct Animation {
|
||||
target_widget: WidgetID,
|
||||
|
||||
animation_id: u32,
|
||||
id: u32,
|
||||
ticks_remaining: u32,
|
||||
ticks_duration: u32,
|
||||
|
||||
@@ -66,7 +66,7 @@ impl Animation {
|
||||
easing: AnimationEasing,
|
||||
callback: AnimationCallback,
|
||||
) -> Self {
|
||||
Animation::new_ex(target_widget, 0, ticks, easing, callback)
|
||||
Self::new_ex(target_widget, 0, ticks, easing, callback)
|
||||
}
|
||||
|
||||
pub fn new_ex(
|
||||
@@ -78,7 +78,7 @@ impl Animation {
|
||||
) -> Self {
|
||||
Self {
|
||||
target_widget,
|
||||
animation_id,
|
||||
id: animation_id,
|
||||
callback,
|
||||
easing,
|
||||
ticks_duration: ticks,
|
||||
@@ -155,15 +155,15 @@ impl Animations {
|
||||
|
||||
pub fn add(&mut self, anim: Animation) {
|
||||
// prevent running two animations at once
|
||||
self.stop_by_widget(anim.target_widget, Some(anim.animation_id));
|
||||
self.stop_by_widget(anim.target_widget, Some(anim.id));
|
||||
self.running_animations.push(anim);
|
||||
}
|
||||
|
||||
pub fn stop_by_widget(&mut self, widget_id: WidgetID, animation_id: Option<u32>) {
|
||||
pub fn stop_by_widget(&mut self, widget_id: WidgetID, opt_animation_id: Option<u32>) {
|
||||
self.running_animations.retain(|anim| {
|
||||
if let Some(animation_id) = &animation_id {
|
||||
if let Some(animation_id) = &opt_animation_id {
|
||||
if anim.target_widget == widget_id {
|
||||
anim.animation_id != *animation_id
|
||||
anim.id != *animation_id
|
||||
} else {
|
||||
true
|
||||
}
|
||||
|
||||
@@ -265,7 +265,7 @@ pub fn construct<U1, U2>(
|
||||
round: params.round,
|
||||
border_color: params.border_color,
|
||||
border: 2.0,
|
||||
})?,
|
||||
}),
|
||||
style,
|
||||
)?;
|
||||
|
||||
@@ -287,10 +287,8 @@ pub fn construct<U1, U2>(
|
||||
..params.text_style
|
||||
},
|
||||
},
|
||||
)?,
|
||||
taffy::Style {
|
||||
..Default::default()
|
||||
},
|
||||
),
|
||||
Default::default(),
|
||||
)?;
|
||||
|
||||
let data = Rc::new(Data {
|
||||
|
||||
@@ -150,7 +150,7 @@ fn anim_hover_out(state: Rc<RefCell<State>>, widget_id: WidgetID) -> Animation {
|
||||
}
|
||||
|
||||
fn register_event_mouse_enter<U1, U2>(
|
||||
data: Rc<Data>,
|
||||
data: &Rc<Data>,
|
||||
state: Rc<RefCell<State>>,
|
||||
listeners: &mut EventListenerCollection<U1, U2>,
|
||||
listener_handles: &mut ListenerHandleVec,
|
||||
@@ -171,7 +171,7 @@ fn register_event_mouse_enter<U1, U2>(
|
||||
}
|
||||
|
||||
fn register_event_mouse_leave<U1, U2>(
|
||||
data: Rc<Data>,
|
||||
data: &Rc<Data>,
|
||||
state: Rc<RefCell<State>>,
|
||||
listeners: &mut EventListenerCollection<U1, U2>,
|
||||
listener_handles: &mut ListenerHandleVec,
|
||||
@@ -192,7 +192,7 @@ fn register_event_mouse_leave<U1, U2>(
|
||||
}
|
||||
|
||||
fn register_event_mouse_press<U1, U2>(
|
||||
data: Rc<Data>,
|
||||
data: &Rc<Data>,
|
||||
state: Rc<RefCell<State>>,
|
||||
listeners: &mut EventListenerCollection<U1, U2>,
|
||||
listener_handles: &mut ListenerHandleVec,
|
||||
@@ -289,7 +289,7 @@ pub fn construct<U1, U2>(
|
||||
border_color: Color::new(1.0, 1.0, 1.0, 0.0),
|
||||
round: WLength::Units(5.0),
|
||||
..Default::default()
|
||||
})?,
|
||||
}),
|
||||
style,
|
||||
)?;
|
||||
|
||||
@@ -306,7 +306,7 @@ pub fn construct<U1, U2>(
|
||||
round: WLength::Units(8.0),
|
||||
color: Color::new(1.0, 1.0, 1.0, 0.0),
|
||||
..Default::default()
|
||||
})?,
|
||||
}),
|
||||
taffy::Style {
|
||||
size: box_size,
|
||||
padding: taffy::Rect::length(4.0),
|
||||
@@ -326,7 +326,7 @@ pub fn construct<U1, U2>(
|
||||
COLOR_UNCHECKED
|
||||
},
|
||||
..Default::default()
|
||||
})?,
|
||||
}),
|
||||
taffy::Style {
|
||||
size: taffy::Size {
|
||||
width: percent(1.0),
|
||||
@@ -347,17 +347,15 @@ pub fn construct<U1, U2>(
|
||||
..Default::default()
|
||||
},
|
||||
},
|
||||
)?,
|
||||
taffy::Style {
|
||||
..Default::default()
|
||||
},
|
||||
),
|
||||
Default::default(),
|
||||
)?;
|
||||
|
||||
let data = Rc::new(Data {
|
||||
node_label,
|
||||
id_container,
|
||||
id_label,
|
||||
id_inner_box,
|
||||
id_label,
|
||||
node_label,
|
||||
});
|
||||
|
||||
let state = Rc::new(RefCell::new(State {
|
||||
@@ -369,9 +367,9 @@ pub fn construct<U1, U2>(
|
||||
|
||||
let mut base = ComponentBase::default();
|
||||
|
||||
register_event_mouse_enter(data.clone(), state.clone(), listeners, &mut base.lhandles);
|
||||
register_event_mouse_leave(data.clone(), state.clone(), listeners, &mut base.lhandles);
|
||||
register_event_mouse_press(data.clone(), state.clone(), listeners, &mut base.lhandles);
|
||||
register_event_mouse_enter(&data, state.clone(), listeners, &mut base.lhandles);
|
||||
register_event_mouse_leave(&data, state.clone(), listeners, &mut base.lhandles);
|
||||
register_event_mouse_press(&data, state.clone(), listeners, &mut base.lhandles);
|
||||
register_event_mouse_release(data.clone(), state.clone(), listeners, &mut base.lhandles);
|
||||
|
||||
let checkbox = Rc::new(ComponentCheckbox { base, data, state });
|
||||
|
||||
@@ -32,6 +32,7 @@ pub struct Component(pub Rc<dyn ComponentTrait>);
|
||||
pub type ComponentWeak = std::rc::Weak<dyn ComponentTrait>;
|
||||
|
||||
impl Component {
|
||||
|
||||
pub fn weak(&self) -> ComponentWeak {
|
||||
Rc::downgrade(&self.0)
|
||||
}
|
||||
@@ -42,6 +43,6 @@ impl Component {
|
||||
}
|
||||
|
||||
// safety: we already checked it above, should be safe to directly cast it
|
||||
unsafe { Ok(Rc::from_raw(Rc::into_raw(self.0.clone()) as _)) }
|
||||
unsafe { Ok(Rc::from_raw(Rc::into_raw(self.0.clone()).cast())) }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -131,7 +131,7 @@ impl State {
|
||||
self.set_value(common.state, data, common.alterables, val);
|
||||
}
|
||||
|
||||
fn update_text(&self, i18n: &mut I18n, text: &mut WidgetLabel, value: f32) {
|
||||
fn update_text(i18n: &mut I18n, text: &mut WidgetLabel, value: f32) {
|
||||
// round displayed value, should be sufficient for now
|
||||
text.set_text(
|
||||
i18n,
|
||||
@@ -156,7 +156,7 @@ impl State {
|
||||
state
|
||||
.widgets
|
||||
.call(data.slider_text_id, |label: &mut WidgetLabel| {
|
||||
self.update_text(&mut state.globals.i18n(), label, value);
|
||||
Self::update_text(&mut state.globals.i18n(), label, value);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -286,7 +286,7 @@ fn register_event_mouse_press<U1, U2>(
|
||||
|
||||
if state.hovered {
|
||||
state.dragging = true;
|
||||
state.update_value_to_mouse(event_data, &data, common)
|
||||
state.update_value_to_mouse(event_data, &data, common);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
@@ -295,7 +295,7 @@ fn register_event_mouse_press<U1, U2>(
|
||||
}
|
||||
|
||||
fn register_event_mouse_release<U1, U2>(
|
||||
data: Rc<Data>,
|
||||
data: &Rc<Data>,
|
||||
state: Rc<RefCell<State>>,
|
||||
listeners: &mut EventListenerCollection<U1, U2>,
|
||||
listener_handles: &mut ListenerHandleVec,
|
||||
@@ -328,7 +328,7 @@ pub fn construct<U1, U2>(
|
||||
style.min_size = style.size;
|
||||
style.max_size = style.size;
|
||||
|
||||
let (body_id, slider_body_node) = layout.add_child(parent, WidgetDiv::create()?, style)?;
|
||||
let (body_id, slider_body_node) = layout.add_child(parent, WidgetDiv::create(), style)?;
|
||||
|
||||
let (_background_id, _) = layout.add_child(
|
||||
body_id,
|
||||
@@ -338,7 +338,7 @@ pub fn construct<U1, U2>(
|
||||
border_color: BODY_BORDER_COLOR,
|
||||
border: 2.0,
|
||||
..Default::default()
|
||||
})?,
|
||||
}),
|
||||
taffy::Style {
|
||||
size: taffy::Size {
|
||||
width: percent(1.0),
|
||||
@@ -364,7 +364,7 @@ pub fn construct<U1, U2>(
|
||||
|
||||
// invisible outer handle body
|
||||
let (slider_handle_id, slider_handle_node) =
|
||||
layout.add_child(body_id, WidgetDiv::create()?, slider_handle_style)?;
|
||||
layout.add_child(body_id, WidgetDiv::create(), slider_handle_style)?;
|
||||
|
||||
let (slider_handle_rect_id, _) = layout.add_child(
|
||||
slider_handle_id,
|
||||
@@ -374,7 +374,7 @@ pub fn construct<U1, U2>(
|
||||
border: 2.0,
|
||||
round: WLength::Percent(1.0),
|
||||
..Default::default()
|
||||
})?,
|
||||
}),
|
||||
taffy::Style {
|
||||
position: taffy::Position::Absolute,
|
||||
size: taffy::Size {
|
||||
@@ -406,7 +406,7 @@ pub fn construct<U1, U2>(
|
||||
..Default::default()
|
||||
},
|
||||
},
|
||||
)?,
|
||||
),
|
||||
Default::default(),
|
||||
)?;
|
||||
|
||||
@@ -427,7 +427,7 @@ pub fn construct<U1, U2>(
|
||||
register_event_mouse_motion(data.clone(), state.clone(), listeners, &mut base.lhandles);
|
||||
register_event_mouse_press(data.clone(), state.clone(), listeners, &mut base.lhandles);
|
||||
register_event_mouse_leave(data.clone(), state.clone(), listeners, &mut base.lhandles);
|
||||
register_event_mouse_release(data.clone(), state.clone(), listeners, &mut base.lhandles);
|
||||
register_event_mouse_release(&data, state.clone(), listeners, &mut base.lhandles);
|
||||
|
||||
let slider = Rc::new(ComponentSlider { base, data, state });
|
||||
|
||||
|
||||
@@ -24,11 +24,11 @@ pub struct Boundary {
|
||||
}
|
||||
|
||||
impl Boundary {
|
||||
pub fn from_pos_size(pos: Vec2, size: Vec2) -> Self {
|
||||
pub const fn from_pos_size(pos: Vec2, size: Vec2) -> Self {
|
||||
Self { pos, size }
|
||||
}
|
||||
|
||||
pub fn construct(transform_stack: &TransformStack) -> Self {
|
||||
pub const fn construct(transform_stack: &TransformStack) -> Self {
|
||||
let transform = transform_stack.get();
|
||||
|
||||
Self {
|
||||
@@ -51,6 +51,7 @@ impl Color {
|
||||
Self { r, g, b, a }
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn add_rgb(&self, n: f32) -> Self {
|
||||
Self {
|
||||
r: self.r + n,
|
||||
@@ -60,8 +61,9 @@ impl Color {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn lerp(&self, other: &Color, n: f32) -> Color {
|
||||
Color {
|
||||
#[must_use]
|
||||
pub fn lerp(&self, other: &Self, n: f32) -> Self {
|
||||
Self {
|
||||
r: self.r * (1.0 - n) + other.r * n,
|
||||
g: self.g * (1.0 - n) + other.g * n,
|
||||
b: self.b * (1.0 - n) + other.b * n,
|
||||
@@ -164,7 +166,7 @@ fn draw_children(
|
||||
model: &glam::Mat4,
|
||||
) {
|
||||
for node_id in layout.state.tree.child_ids(parent_node_id) {
|
||||
let Some(widget_id) = layout.state.tree.get_node_context(node_id).cloned() else {
|
||||
let Some(widget_id) = layout.state.tree.get_node_context(node_id).copied() else {
|
||||
debug_assert!(false);
|
||||
continue;
|
||||
};
|
||||
|
||||
@@ -73,19 +73,20 @@ pub enum Event {
|
||||
}
|
||||
|
||||
impl Event {
|
||||
fn test_transform_pos(&self, transform: &Transform, pos: &Vec2) -> bool {
|
||||
fn test_transform_pos(transform: &Transform, pos: Vec2) -> bool {
|
||||
pos.x >= transform.pos.x
|
||||
&& pos.x < transform.pos.x + transform.dim.x
|
||||
&& pos.y >= transform.pos.y
|
||||
&& pos.y < transform.pos.y + transform.dim.y
|
||||
}
|
||||
|
||||
|
||||
pub fn test_mouse_within_transform(&self, transform: &Transform) -> bool {
|
||||
match self {
|
||||
Event::MouseDown(evt) => self.test_transform_pos(transform, &evt.pos),
|
||||
Event::MouseMotion(evt) => self.test_transform_pos(transform, &evt.pos),
|
||||
Event::MouseUp(evt) => self.test_transform_pos(transform, &evt.pos),
|
||||
Event::MouseWheel(evt) => self.test_transform_pos(transform, &evt.pos),
|
||||
Self::MouseDown(evt) => Self::test_transform_pos(transform, evt.pos),
|
||||
Self::MouseMotion(evt) => Self::test_transform_pos(transform, evt.pos),
|
||||
Self::MouseUp(evt) => Self::test_transform_pos(transform, evt.pos),
|
||||
Self::MouseWheel(evt) => Self::test_transform_pos(transform, evt.pos),
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
@@ -102,7 +103,7 @@ pub struct EventAlterables {
|
||||
}
|
||||
|
||||
impl EventAlterables {
|
||||
pub fn mark_redraw(&mut self) {
|
||||
pub const fn mark_redraw(&mut self) {
|
||||
self.needs_redraw = true;
|
||||
}
|
||||
|
||||
@@ -114,7 +115,7 @@ impl EventAlterables {
|
||||
self.dirty_nodes.push(node_id);
|
||||
}
|
||||
|
||||
pub fn trigger_haptics(&mut self) {
|
||||
pub const fn trigger_haptics(&mut self) {
|
||||
self.trigger_haptics = true;
|
||||
}
|
||||
|
||||
@@ -128,7 +129,8 @@ pub struct CallbackDataCommon<'a> {
|
||||
pub alterables: &'a mut EventAlterables,
|
||||
}
|
||||
|
||||
impl<'a> CallbackDataCommon<'a> {
|
||||
impl CallbackDataCommon<'_> {
|
||||
|
||||
pub fn i18n(&self) -> RefMut<I18n> {
|
||||
self.state.globals.i18n()
|
||||
}
|
||||
@@ -151,15 +153,16 @@ pub enum CallbackMetadata {
|
||||
|
||||
impl CallbackMetadata {
|
||||
// helper function
|
||||
pub fn get_mouse_pos_absolute(&self) -> Option<Vec2> {
|
||||
|
||||
pub const fn get_mouse_pos_absolute(&self) -> Option<Vec2> {
|
||||
match *self {
|
||||
CallbackMetadata::None => None,
|
||||
CallbackMetadata::MouseButton(b) => Some(b.pos),
|
||||
CallbackMetadata::MousePosition(b) => Some(b.pos),
|
||||
CallbackMetadata::Custom(_) => None,
|
||||
Self::MouseButton(b) => Some(b.pos),
|
||||
Self::MousePosition(b) => Some(b.pos),
|
||||
Self::Custom(_) | Self::None => None,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
pub fn get_mouse_pos_relative(&self, transform_stack: &TransformStack) -> Option<Vec2> {
|
||||
let mouse_pos_abs = self.get_mouse_pos_absolute()?;
|
||||
Some(mouse_pos_abs - transform_stack.get_pos())
|
||||
@@ -206,6 +209,7 @@ pub struct EventListener<U1, U2> {
|
||||
}
|
||||
|
||||
impl<U1, U2> EventListener<U1, U2> {
|
||||
|
||||
pub fn callback_for_kind(
|
||||
&self,
|
||||
kind: EventListenerKind,
|
||||
@@ -291,7 +295,7 @@ impl<U1, U2> EventListenerCollection<U1, U2> {
|
||||
|
||||
let mut count = 0;
|
||||
|
||||
for (_id, vec) in self.map.iter_mut() {
|
||||
for (_id, vec) in &mut self.map {
|
||||
vec.0.retain(|listener| {
|
||||
if listener.handle.strong_count() != 0 {
|
||||
true
|
||||
@@ -307,6 +311,7 @@ impl<U1, U2> EventListenerCollection<U1, U2> {
|
||||
log::debug!("EventListenerCollection: cleaned-up {count} expired events");
|
||||
}
|
||||
|
||||
|
||||
pub fn get(&self, widget_id: WidgetID) -> Option<&EventListenerVec<U1, U2>> {
|
||||
self.map.get(widget_id)
|
||||
}
|
||||
|
||||
@@ -124,7 +124,7 @@ impl WCommandBuffer<CmdBufXfer> {
|
||||
|
||||
pub fn update_image(
|
||||
&mut self,
|
||||
image: Arc<Image>,
|
||||
image: &Arc<Image>,
|
||||
data: &[u8],
|
||||
offset: [u32; 3],
|
||||
extent: Option<[u32; 3]>,
|
||||
@@ -156,9 +156,9 @@ impl WCommandBuffer<CmdBufXfer> {
|
||||
|
||||
pub fn copy_image(
|
||||
&mut self,
|
||||
src: Arc<Image>,
|
||||
src: &Arc<Image>,
|
||||
src_offset: [u32; 3],
|
||||
dst: Arc<Image>,
|
||||
dst: &Arc<Image>,
|
||||
dst_offset: [u32; 3],
|
||||
extent: Option<[u32; 3]>,
|
||||
) -> anyhow::Result<()> {
|
||||
|
||||
@@ -181,8 +181,8 @@ impl WGfx {
|
||||
|
||||
pub fn create_pipeline<V>(
|
||||
self: &Arc<Self>,
|
||||
vert: Arc<ShaderModule>,
|
||||
frag: Arc<ShaderModule>,
|
||||
vert: &Arc<ShaderModule>,
|
||||
frag: &Arc<ShaderModule>,
|
||||
format: Format,
|
||||
blend: Option<AttachmentBlend>,
|
||||
topology: PrimitiveTopology,
|
||||
|
||||
@@ -32,7 +32,7 @@ where
|
||||
V: BufferContents + Vertex,
|
||||
{
|
||||
pub(super) fn new(
|
||||
pipeline: Arc<WGfxPipeline<V>>,
|
||||
pipeline: &Arc<WGfxPipeline<V>>,
|
||||
dimensions: [f32; 2],
|
||||
vertex_buffer: Subbuffer<[V]>,
|
||||
vertices: Range<u32>,
|
||||
|
||||
@@ -105,6 +105,7 @@ where
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
pub fn inner(&self) -> Arc<GraphicsPipeline> {
|
||||
self.pipeline.clone()
|
||||
}
|
||||
@@ -149,6 +150,7 @@ where
|
||||
)?)
|
||||
}
|
||||
|
||||
#[allow(clippy::needless_pass_by_value)]
|
||||
pub fn uniform_buffer_upload<T>(
|
||||
&self,
|
||||
set: usize,
|
||||
@@ -183,8 +185,8 @@ where
|
||||
{
|
||||
pub(super) fn new_with_vert_input(
|
||||
graphics: Arc<WGfx>,
|
||||
vert: Arc<ShaderModule>,
|
||||
frag: Arc<ShaderModule>,
|
||||
vert: &Arc<ShaderModule>,
|
||||
frag: &Arc<ShaderModule>,
|
||||
format: Format,
|
||||
blend: Option<AttachmentBlend>,
|
||||
topology: PrimitiveTopology,
|
||||
@@ -199,7 +201,7 @@ where
|
||||
V::per_vertex().definition(&vert_entry_point)?
|
||||
});
|
||||
|
||||
WGfxPipeline::new_from_stages(
|
||||
Self::new_from_stages(
|
||||
graphics,
|
||||
format,
|
||||
blend,
|
||||
@@ -219,7 +221,7 @@ where
|
||||
descriptor_sets: Vec<Arc<DescriptorSet>>,
|
||||
) -> anyhow::Result<WGfxPass<V>> {
|
||||
WGfxPass::new(
|
||||
self.clone(),
|
||||
&self.clone(),
|
||||
dimensions,
|
||||
vertex_buffer,
|
||||
vertices,
|
||||
|
||||
@@ -93,8 +93,8 @@ impl I18n {
|
||||
let json_root_translated = serde_json::from_str(str::from_utf8(&data_translated)?)?;
|
||||
|
||||
Ok(Self {
|
||||
json_root_fallback,
|
||||
json_root_translated,
|
||||
json_root_fallback,
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -186,7 +186,7 @@ impl Layout {
|
||||
user_data: &mut (&mut U1, &mut U2),
|
||||
) -> anyhow::Result<()> {
|
||||
let l = self.state.tree.layout(node_id)?;
|
||||
let Some(widget_id) = self.state.tree.get_node_context(node_id).cloned() else {
|
||||
let Some(widget_id) = self.state.tree.get_node_context(node_id).copied() else {
|
||||
anyhow::bail!("invalid widget ID");
|
||||
};
|
||||
|
||||
@@ -230,13 +230,9 @@ impl Layout {
|
||||
widget::EventResult::Pass => {
|
||||
// go on
|
||||
}
|
||||
widget::EventResult::Consumed => {
|
||||
iter_children = false;
|
||||
}
|
||||
widget::EventResult::Outside => {
|
||||
iter_children = false;
|
||||
}
|
||||
widget::EventResult::Unused => {
|
||||
widget::EventResult::Consumed
|
||||
| widget::EventResult::Outside
|
||||
| widget::EventResult::Unused => {
|
||||
iter_children = false;
|
||||
}
|
||||
}
|
||||
@@ -252,7 +248,7 @@ impl Layout {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn check_toggle_needs_redraw(&mut self) -> bool {
|
||||
pub const fn check_toggle_needs_redraw(&mut self) -> bool {
|
||||
if self.needs_redraw {
|
||||
self.needs_redraw = false;
|
||||
true
|
||||
@@ -261,7 +257,7 @@ impl Layout {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn check_toggle_haptics_triggered(&mut self) -> bool {
|
||||
pub const fn check_toggle_haptics_triggered(&mut self) -> bool {
|
||||
if self.haptics_triggered {
|
||||
self.haptics_triggered = false;
|
||||
true
|
||||
@@ -306,7 +302,7 @@ impl Layout {
|
||||
&mut state.widgets,
|
||||
&mut state.nodes,
|
||||
None, // no parent
|
||||
WidgetDiv::create()?,
|
||||
WidgetDiv::create(),
|
||||
taffy::Style {
|
||||
size: taffy::Size::auto(),
|
||||
..Default::default()
|
||||
|
||||
@@ -1,3 +1,22 @@
|
||||
#![warn(clippy::all, clippy::pedantic, clippy::nursery)]
|
||||
#![allow(
|
||||
clippy::suboptimal_flops,
|
||||
clippy::cast_precision_loss,
|
||||
clippy::missing_errors_doc,
|
||||
clippy::default_trait_access,
|
||||
clippy::missing_panics_doc,
|
||||
clippy::cast_possible_wrap,
|
||||
clippy::cast_possible_truncation,
|
||||
clippy::cast_sign_loss,
|
||||
clippy::items_after_statements,
|
||||
clippy::future_not_send,
|
||||
clippy::must_use_candidate,
|
||||
clippy::implicit_hasher,
|
||||
clippy::option_if_let_else,
|
||||
clippy::significant_drop_tightening,
|
||||
clippy::float_cmp
|
||||
)]
|
||||
|
||||
pub mod animation;
|
||||
pub mod any;
|
||||
pub mod assets;
|
||||
|
||||
@@ -69,7 +69,7 @@ pub fn parse_component_button<'a, U1, U2>(
|
||||
},
|
||||
)?;
|
||||
|
||||
process_component(file, ctx, node, Component(component))?;
|
||||
process_component(file, ctx, node, Component(component));
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -51,7 +51,7 @@ pub fn parse_component_checkbox<'a, U1, U2>(
|
||||
},
|
||||
)?;
|
||||
|
||||
process_component(file, ctx, node, Component(component))?;
|
||||
process_component(file, ctx, node, Component(component));
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -48,7 +48,7 @@ pub fn parse_component_slider<'a, U1, U2>(
|
||||
},
|
||||
)?;
|
||||
|
||||
process_component(file, ctx, node, Component(component))?;
|
||||
process_component(file, ctx, node, Component(component));
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -84,7 +84,7 @@ impl ParserState {
|
||||
}
|
||||
|
||||
// safety: we already checked it above, should be safe to directly cast it
|
||||
unsafe { Ok(Rc::from_raw(Rc::into_raw(component.0) as _)) }
|
||||
unsafe { Ok(Rc::from_raw(Rc::into_raw(component.0).cast())) }
|
||||
}
|
||||
|
||||
pub fn get_widget_id(&self, id: &str) -> anyhow::Result<WidgetID> {
|
||||
@@ -135,7 +135,7 @@ impl ParserState {
|
||||
};
|
||||
|
||||
parse_widget_other_internal(
|
||||
template.clone(),
|
||||
&template.clone(),
|
||||
template_parameters,
|
||||
&file,
|
||||
&mut ctx,
|
||||
@@ -239,11 +239,11 @@ fn parse_val(value: &Rc<str>) -> Option<f32> {
|
||||
}
|
||||
|
||||
fn is_percent(value: &str) -> bool {
|
||||
value.ends_with("%")
|
||||
value.ends_with('%')
|
||||
}
|
||||
|
||||
fn parse_percent(value: &str) -> Option<f32> {
|
||||
let Some(val_str) = value.split("%").next() else {
|
||||
let Some(val_str) = value.split('%').next() else {
|
||||
print_invalid_value(value);
|
||||
return None;
|
||||
};
|
||||
@@ -295,7 +295,7 @@ where
|
||||
}
|
||||
|
||||
fn parse_widget_other_internal<U1, U2>(
|
||||
template: Rc<Template>,
|
||||
template: &Rc<Template>,
|
||||
template_parameters: HashMap<Rc<str>, Rc<str>>,
|
||||
file: &ParserFile,
|
||||
ctx: &mut ParserContext<U1, U2>,
|
||||
@@ -312,7 +312,7 @@ fn parse_widget_other_internal<U1, U2>(
|
||||
let template_node = doc
|
||||
.borrow_doc()
|
||||
.get_node(template.node)
|
||||
.ok_or(anyhow::anyhow!("template node invalid"))?;
|
||||
.ok_or_else(|| anyhow::anyhow!("template node invalid"))?;
|
||||
|
||||
parse_children(&template_file, ctx, template_node, parent_id)?;
|
||||
|
||||
@@ -334,7 +334,7 @@ fn parse_widget_other<'a, U1, U2>(
|
||||
let template_parameters: HashMap<Rc<str>, Rc<str>> =
|
||||
iter_attribs(file, ctx, &node, false).collect();
|
||||
|
||||
parse_widget_other_internal(template.clone(), template_parameters, file, ctx, parent_id)
|
||||
parse_widget_other_internal(&template.clone(), template_parameters, file, ctx, parent_id)
|
||||
}
|
||||
|
||||
fn parse_tag_include<'a, U1, U2>(
|
||||
@@ -349,11 +349,15 @@ fn parse_tag_include<'a, U1, U2>(
|
||||
#[allow(clippy::single_match)]
|
||||
match key {
|
||||
"src" => {
|
||||
let mut new_path = file.path.parent().unwrap_or(Path::new("/")).to_path_buf();
|
||||
let mut new_path = file
|
||||
.path
|
||||
.parent()
|
||||
.unwrap_or_else(|| Path::new("/"))
|
||||
.to_path_buf();
|
||||
new_path.push(value);
|
||||
|
||||
let (new_file, node_layout) = get_doc_from_path(ctx, &new_path)?;
|
||||
parse_document_root(new_file, ctx, parent_id, node_layout)?;
|
||||
parse_document_root(&new_file, ctx, parent_id, node_layout)?;
|
||||
|
||||
return Ok(());
|
||||
}
|
||||
@@ -366,10 +370,7 @@ fn parse_tag_include<'a, U1, U2>(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn parse_tag_var<'a, U1, U2>(
|
||||
ctx: &mut ParserContext<U1, U2>,
|
||||
node: roxmltree::Node<'a, 'a>,
|
||||
) -> anyhow::Result<()> {
|
||||
fn parse_tag_var<'a, U1, U2>(ctx: &mut ParserContext<U1, U2>, node: roxmltree::Node<'a, 'a>) {
|
||||
let mut out_key: Option<&str> = None;
|
||||
let mut out_value: Option<&str> = None;
|
||||
|
||||
@@ -391,17 +392,15 @@ fn parse_tag_var<'a, U1, U2>(
|
||||
|
||||
let Some(key) = out_key else {
|
||||
print_missing_attrib("var", "key");
|
||||
return Ok(());
|
||||
return;
|
||||
};
|
||||
|
||||
let Some(value) = out_value else {
|
||||
print_missing_attrib("var", "value");
|
||||
return Ok(());
|
||||
return;
|
||||
};
|
||||
|
||||
ctx.var_map.insert(Rc::from(key), Rc::from(value));
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn replace_vars(input: &str, vars: &HashMap<Rc<str>, Rc<str>>) -> Rc<str> {
|
||||
@@ -414,12 +413,11 @@ pub fn replace_vars(input: &str, vars: &HashMap<Rc<str>, Rc<str>>) -> Rc<str> {
|
||||
let out = re.replace_all(input, |captures: ®ex::Captures| {
|
||||
let input_var = &captures[1];
|
||||
|
||||
match vars.get(input_var) {
|
||||
Some(replacement) => replacement.clone(),
|
||||
None => {
|
||||
log::warn!("failed to replace var named \"{input_var}\" (not found)");
|
||||
Rc::from("")
|
||||
}
|
||||
if let Some(replacement) = vars.get(input_var) {
|
||||
replacement.clone()
|
||||
} else {
|
||||
log::warn!("failed to replace var named \"{input_var}\" (not found)");
|
||||
Rc::from("")
|
||||
}
|
||||
});
|
||||
|
||||
@@ -433,7 +431,7 @@ fn process_attrib<'a, U1, U2>(
|
||||
key: &str,
|
||||
value: &str,
|
||||
) -> (Rc<str>, Rc<str>) {
|
||||
if value.starts_with("~") {
|
||||
if value.starts_with('~') {
|
||||
let name = &value[1..];
|
||||
|
||||
(
|
||||
@@ -473,7 +471,7 @@ fn iter_attribs<'a, U1, U2>(
|
||||
|
||||
if key == "macro" {
|
||||
if let Some(macro_attrib) = ctx.macro_attribs.get(value) {
|
||||
for (macro_key, macro_value) in macro_attrib.attribs.iter() {
|
||||
for (macro_key, macro_value) in ¯o_attrib.attribs {
|
||||
res.push(process_attrib(file, ctx, macro_key, macro_value));
|
||||
}
|
||||
} else {
|
||||
@@ -487,15 +485,12 @@ fn iter_attribs<'a, U1, U2>(
|
||||
res.into_iter()
|
||||
}
|
||||
|
||||
fn parse_tag_theme<'a, U1, U2>(
|
||||
ctx: &mut ParserContext<U1, U2>,
|
||||
node: roxmltree::Node<'a, 'a>,
|
||||
) -> anyhow::Result<()> {
|
||||
fn parse_tag_theme<'a, U1, U2>(ctx: &mut ParserContext<U1, U2>, node: roxmltree::Node<'a, 'a>) {
|
||||
for child_node in node.children() {
|
||||
let child_name = child_node.tag_name().name();
|
||||
match child_name {
|
||||
"var" => {
|
||||
parse_tag_var(ctx, child_node)?;
|
||||
parse_tag_var(ctx, child_node);
|
||||
}
|
||||
"" => { /* ignore */ }
|
||||
_ => {
|
||||
@@ -503,15 +498,13 @@ fn parse_tag_theme<'a, U1, U2>(
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn parse_tag_template<U1, U2>(
|
||||
file: &ParserFile,
|
||||
ctx: &mut ParserContext<U1, U2>,
|
||||
node: roxmltree::Node<'_, '_>,
|
||||
) -> anyhow::Result<()> {
|
||||
) {
|
||||
let mut template_name: Option<Rc<str>> = None;
|
||||
|
||||
let attribs: Vec<_> = iter_attribs(file, ctx, &node, false).collect();
|
||||
@@ -529,7 +522,7 @@ fn parse_tag_template<U1, U2>(
|
||||
|
||||
let Some(name) = template_name else {
|
||||
log::error!("Template name not specified, ignoring");
|
||||
return Ok(());
|
||||
return;
|
||||
};
|
||||
|
||||
ctx.templates.insert(
|
||||
@@ -539,15 +532,13 @@ fn parse_tag_template<U1, U2>(
|
||||
node_document: file.document.clone(),
|
||||
}),
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn parse_tag_macro<U1, U2>(
|
||||
file: &ParserFile,
|
||||
ctx: &mut ParserContext<U1, U2>,
|
||||
node: roxmltree::Node<'_, '_>,
|
||||
) -> anyhow::Result<()> {
|
||||
) {
|
||||
let mut macro_name: Option<Rc<str>> = None;
|
||||
|
||||
let attribs: Vec<_> = iter_attribs(file, ctx, &node, true).collect();
|
||||
@@ -568,17 +559,15 @@ fn parse_tag_macro<U1, U2>(
|
||||
|
||||
let Some(name) = macro_name else {
|
||||
log::error!("Template name not specified, ignoring");
|
||||
return Ok(());
|
||||
return;
|
||||
};
|
||||
|
||||
ctx.macro_attribs.insert(
|
||||
name.clone(),
|
||||
name,
|
||||
MacroAttribs {
|
||||
attribs: macro_attribs,
|
||||
},
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn process_component<'a, U1, U2>(
|
||||
@@ -586,7 +575,7 @@ fn process_component<'a, U1, U2>(
|
||||
ctx: &mut ParserContext<U1, U2>,
|
||||
node: roxmltree::Node<'a, 'a>,
|
||||
component: Component,
|
||||
) -> anyhow::Result<()> {
|
||||
) {
|
||||
let attribs: Vec<_> = iter_attribs(file, ctx, &node, false).collect();
|
||||
|
||||
for (key, value) in attribs {
|
||||
@@ -606,7 +595,6 @@ fn process_component<'a, U1, U2>(
|
||||
}
|
||||
|
||||
ctx.components.push(component);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn parse_widget_universal<'a, U1, U2>(
|
||||
@@ -614,7 +602,7 @@ fn parse_widget_universal<'a, U1, U2>(
|
||||
ctx: &mut ParserContext<U1, U2>,
|
||||
node: roxmltree::Node<'a, 'a>,
|
||||
widget_id: WidgetID,
|
||||
) -> anyhow::Result<()> {
|
||||
) {
|
||||
let attribs: Vec<_> = iter_attribs(file, ctx, &node, false).collect();
|
||||
|
||||
for (key, value) in attribs {
|
||||
@@ -629,7 +617,6 @@ fn parse_widget_universal<'a, U1, U2>(
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn parse_children<'a, U1, U2>(
|
||||
@@ -652,7 +639,7 @@ fn parse_children<'a, U1, U2>(
|
||||
}
|
||||
Some(s) => print_invalid_attrib("ignore_in_mode", s),
|
||||
_ => {}
|
||||
};
|
||||
}
|
||||
|
||||
match child_node.tag_name().name() {
|
||||
"include" => {
|
||||
@@ -731,8 +718,8 @@ pub fn parse_from_assets<U1, U2>(
|
||||
|
||||
let mut ctx = create_default_context(doc_params, layout, listeners);
|
||||
|
||||
let (file, node_layout) = get_doc_from_path(&mut ctx, &path)?;
|
||||
parse_document_root(file, &mut ctx, parent_id, node_layout)?;
|
||||
let (file, node_layout) = get_doc_from_path(&ctx, &path)?;
|
||||
parse_document_root(&file, &mut ctx, parent_id, node_layout)?;
|
||||
|
||||
// move everything essential to the result
|
||||
let result = ParserState {
|
||||
@@ -766,7 +753,7 @@ fn assets_path_to_xml(assets: &mut Box<dyn AssetProvider>, path: &Path) -> anyho
|
||||
}
|
||||
|
||||
fn get_doc_from_path<U1, U2>(
|
||||
ctx: &mut ParserContext<U1, U2>,
|
||||
ctx: &ParserContext<U1, U2>,
|
||||
path: &Path,
|
||||
) -> anyhow::Result<(ParserFile, roxmltree::NodeId)> {
|
||||
let xml = assets_path_to_xml(&mut ctx.layout.state.globals.assets(), path)?;
|
||||
@@ -791,7 +778,7 @@ fn get_doc_from_path<U1, U2>(
|
||||
}
|
||||
|
||||
fn parse_document_root<U1, U2>(
|
||||
file: ParserFile,
|
||||
file: &ParserFile,
|
||||
ctx: &mut ParserContext<U1, U2>,
|
||||
parent_id: WidgetID,
|
||||
node_layout: roxmltree::NodeId,
|
||||
@@ -800,22 +787,22 @@ fn parse_document_root<U1, U2>(
|
||||
.document
|
||||
.borrow_doc()
|
||||
.get_node(node_layout)
|
||||
.ok_or(anyhow::anyhow!("layout node not found"))?;
|
||||
.ok_or_else(|| anyhow::anyhow!("layout node not found"))?;
|
||||
|
||||
for child_node in node_layout.children() {
|
||||
#[allow(clippy::single_match)]
|
||||
match child_node.tag_name().name() {
|
||||
/* topmost include directly in <layout> */
|
||||
"include" => parse_tag_include(&file, ctx, child_node, parent_id)?,
|
||||
"theme" => parse_tag_theme(ctx, child_node)?,
|
||||
"template" => parse_tag_template(&file, ctx, child_node)?,
|
||||
"macro" => parse_tag_macro(&file, ctx, child_node)?,
|
||||
"include" => parse_tag_include(file, ctx, child_node, parent_id)?,
|
||||
"theme" => parse_tag_theme(ctx, child_node),
|
||||
"template" => parse_tag_template(file, ctx, child_node),
|
||||
"macro" => parse_tag_macro(file, ctx, child_node),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(tag_elements) = get_tag_by_name(&node_layout, "elements") {
|
||||
parse_children(&file, ctx, tag_elements, parent_id)?;
|
||||
parse_children(file, ctx, tag_elements, parent_id)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
||||
@@ -86,10 +86,10 @@ pub fn parse_text_style(attribs: &[(Rc<str>, Rc<str>)]) -> TextStyle {
|
||||
style
|
||||
}
|
||||
|
||||
#[allow(clippy::too_many_lines)]
|
||||
#[allow(clippy::cognitive_complexity)]
|
||||
pub fn parse_style(attribs: &[(Rc<str>, Rc<str>)]) -> taffy::Style {
|
||||
let mut style = taffy::Style {
|
||||
..Default::default()
|
||||
};
|
||||
let mut style = taffy::Style::default();
|
||||
|
||||
for (key, value) in attribs {
|
||||
match key.as_ref() {
|
||||
|
||||
@@ -4,7 +4,7 @@ use crate::{
|
||||
ParserContext, ParserFile, iter_attribs, parse_children, parse_widget_universal,
|
||||
style::parse_style,
|
||||
},
|
||||
widget,
|
||||
widget::div::WidgetDiv,
|
||||
};
|
||||
|
||||
pub fn parse_widget_div<'a, U1, U2>(
|
||||
@@ -18,9 +18,9 @@ pub fn parse_widget_div<'a, U1, U2>(
|
||||
|
||||
let (new_id, _) = ctx
|
||||
.layout
|
||||
.add_child(parent_id, widget::div::WidgetDiv::create()?, style)?;
|
||||
.add_child(parent_id, WidgetDiv::create(), style)?;
|
||||
|
||||
parse_widget_universal(file, ctx, node, new_id)?;
|
||||
parse_widget_universal(file, ctx, node, new_id);
|
||||
parse_children(file, ctx, node, new_id)?;
|
||||
|
||||
Ok(())
|
||||
|
||||
@@ -5,7 +5,7 @@ use crate::{
|
||||
ParserContext, ParserFile, iter_attribs, parse_children, parse_widget_universal,
|
||||
style::{parse_style, parse_text_style},
|
||||
},
|
||||
widget::label::{WidgetLabelParams, WidgetLabel},
|
||||
widget::label::{WidgetLabel, WidgetLabelParams},
|
||||
};
|
||||
|
||||
pub fn parse_widget_label<'a, U1, U2>(
|
||||
@@ -36,9 +36,9 @@ pub fn parse_widget_label<'a, U1, U2>(
|
||||
let (new_id, _) =
|
||||
ctx
|
||||
.layout
|
||||
.add_child(parent_id, WidgetLabel::create(&mut i18n, params)?, style)?;
|
||||
.add_child(parent_id, WidgetLabel::create(&mut i18n, params), style)?;
|
||||
|
||||
parse_widget_universal(file, ctx, node, new_id)?;
|
||||
parse_widget_universal(file, ctx, node, new_id);
|
||||
parse_children(file, ctx, node, new_id)?;
|
||||
|
||||
Ok(())
|
||||
|
||||
@@ -6,7 +6,7 @@ use crate::{
|
||||
print_invalid_attrib,
|
||||
style::{parse_color, parse_round, parse_style},
|
||||
},
|
||||
widget::{self, rectangle::WidgetRectangleParams},
|
||||
widget::rectangle::{WidgetRectangle, WidgetRectangleParams},
|
||||
};
|
||||
|
||||
pub fn parse_widget_rectangle<'a, U1, U2>(
|
||||
@@ -55,13 +55,11 @@ pub fn parse_widget_rectangle<'a, U1, U2>(
|
||||
}
|
||||
}
|
||||
|
||||
let (new_id, _) = ctx.layout.add_child(
|
||||
parent_id,
|
||||
widget::rectangle::WidgetRectangle::create(params)?,
|
||||
style,
|
||||
)?;
|
||||
let (new_id, _) = ctx
|
||||
.layout
|
||||
.add_child(parent_id, WidgetRectangle::create(params), style)?;
|
||||
|
||||
parse_widget_universal(file, ctx, node, new_id)?;
|
||||
parse_widget_universal(file, ctx, node, new_id);
|
||||
parse_children(file, ctx, node, new_id)?;
|
||||
|
||||
Ok(())
|
||||
|
||||
@@ -53,13 +53,13 @@ pub fn parse_widget_sprite<'a, U1, U2>(
|
||||
params.glyph_data = Some(CustomGlyphData::new(glyph));
|
||||
} else {
|
||||
log::warn!("No source for sprite node!");
|
||||
};
|
||||
}
|
||||
|
||||
let (new_id, _) = ctx
|
||||
.layout
|
||||
.add_child(parent_id, WidgetSprite::create(params)?, style)?;
|
||||
.add_child(parent_id, WidgetSprite::create(params), style)?;
|
||||
|
||||
parse_widget_universal(file, ctx, node, new_id)?;
|
||||
parse_widget_universal(file, ctx, node, new_id);
|
||||
parse_children(file, ctx, node, new_id)?;
|
||||
|
||||
Ok(())
|
||||
|
||||
@@ -96,7 +96,7 @@ impl SharedContext {
|
||||
}
|
||||
|
||||
fn atlas_for_pixel_scale(&mut self, pixel_scale: f32) -> anyhow::Result<SharedContextKey> {
|
||||
for (key, atlas) in self.atlas_map.iter() {
|
||||
for (key, atlas) in &self.atlas_map {
|
||||
if (atlas.pixel_scale - pixel_scale).abs() < f32::EPSILON {
|
||||
return Ok(key);
|
||||
}
|
||||
@@ -125,7 +125,7 @@ pub struct Context {
|
||||
|
||||
impl Context {
|
||||
pub fn new(shared: &mut SharedContext, pixel_scale: f32) -> anyhow::Result<Self> {
|
||||
let viewport = Viewport::new(shared.gfx.clone())?;
|
||||
let viewport = Viewport::new(&shared.gfx)?;
|
||||
let shared_ctx_key = shared.atlas_for_pixel_scale(pixel_scale)?;
|
||||
|
||||
Ok(Self {
|
||||
@@ -159,7 +159,7 @@ impl Context {
|
||||
|
||||
let fov = 0.4;
|
||||
let aspect_ratio = size.x / size.y;
|
||||
let projection = Mat4::perspective_rh(fov, aspect_ratio, 1.0, 100000.0);
|
||||
let projection = Mat4::perspective_rh(fov, aspect_ratio, 1.0, 100_000.0);
|
||||
|
||||
let b = size.y / 2.0;
|
||||
let angle_half = fov / 2.0;
|
||||
@@ -192,7 +192,7 @@ impl Context {
|
||||
shared.rect_pipeline.clone(),
|
||||
)?];
|
||||
|
||||
for primitive in primitives.iter() {
|
||||
for primitive in primitives {
|
||||
let pass = passes.last_mut().unwrap(); // always safe
|
||||
|
||||
match &primitive.payload {
|
||||
|
||||
@@ -28,7 +28,7 @@ impl ModelBuffer {
|
||||
|
||||
let buffer = gfx.empty_buffer::<f32>(
|
||||
BufferUsage::STORAGE_BUFFER | BufferUsage::TRANSFER_DST,
|
||||
INITIAL_CAPACITY_F32 as _,
|
||||
INITIAL_CAPACITY_F32.into(),
|
||||
)?;
|
||||
|
||||
let mut models = Vec::<glam::Mat4>::new();
|
||||
@@ -56,7 +56,7 @@ impl ModelBuffer {
|
||||
self.buffer_capacity_f32 = required_capacity_f32;
|
||||
self.buffer = gfx.empty_buffer::<f32>(
|
||||
BufferUsage::STORAGE_BUFFER | BufferUsage::TRANSFER_DST,
|
||||
required_capacity_f32 as _,
|
||||
required_capacity_f32.into(),
|
||||
)?;
|
||||
//log::info!("resized to {}", required_capacity_f32);
|
||||
}
|
||||
@@ -64,7 +64,7 @@ impl ModelBuffer {
|
||||
//safe
|
||||
let floats = unsafe {
|
||||
std::slice::from_raw_parts(
|
||||
self.models.as_slice().as_ptr() as *const f32,
|
||||
self.models.as_slice().as_ptr().cast::<f32>(),
|
||||
required_capacity_f32 as usize,
|
||||
)
|
||||
};
|
||||
|
||||
@@ -34,7 +34,7 @@ pub struct RectVertex {
|
||||
pub depth: f32,
|
||||
}
|
||||
|
||||
/// Cloneable pipeline & shaders to be shared between RectRenderer instances.
|
||||
/// Cloneable pipeline & shaders to be shared between `RectRenderer` instances.
|
||||
#[derive(Clone)]
|
||||
pub struct RectPipeline {
|
||||
gfx: Arc<WGfx>,
|
||||
@@ -47,8 +47,8 @@ impl RectPipeline {
|
||||
let frag = frag_rect::load(gfx.device.clone())?;
|
||||
|
||||
let color_rect = gfx.create_pipeline::<RectVertex>(
|
||||
vert,
|
||||
frag,
|
||||
&vert,
|
||||
&frag,
|
||||
format,
|
||||
Some(BLEND_ALPHA),
|
||||
PrimitiveTopology::TriangleStrip,
|
||||
|
||||
@@ -23,29 +23,31 @@ pub enum CustomGlyphContent {
|
||||
impl CustomGlyphContent {
|
||||
pub fn from_bin_svg(data: &[u8]) -> anyhow::Result<Self> {
|
||||
let tree = Tree::from_data(data, &Options::default())?;
|
||||
Ok(CustomGlyphContent::Svg(Box::new(tree)))
|
||||
Ok(Self::Svg(Box::new(tree)))
|
||||
}
|
||||
|
||||
pub fn from_bin_raster(data: &[u8]) -> anyhow::Result<Self> {
|
||||
let image = image::load_from_memory(data)?.into_rgba8();
|
||||
Ok(CustomGlyphContent::Image(image))
|
||||
Ok(Self::Image(image))
|
||||
}
|
||||
|
||||
#[allow(clippy::case_sensitive_file_extension_comparisons)]
|
||||
pub fn from_assets(provider: &mut Box<dyn AssetProvider>, path: &str) -> anyhow::Result<Self> {
|
||||
let data = provider.load_from_path(path)?;
|
||||
if path.ends_with(".svg") || path.ends_with(".svgz") {
|
||||
Ok(CustomGlyphContent::from_bin_svg(&data)?)
|
||||
Ok(Self::from_bin_svg(&data)?)
|
||||
} else {
|
||||
Ok(CustomGlyphContent::from_bin_raster(&data)?)
|
||||
Ok(Self::from_bin_raster(&data)?)
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(clippy::case_sensitive_file_extension_comparisons)]
|
||||
pub fn from_file(path: &str) -> anyhow::Result<Self> {
|
||||
let data = std::fs::read(path)?;
|
||||
if path.ends_with(".svg") || path.ends_with(".svgz") {
|
||||
Ok(CustomGlyphContent::from_bin_svg(&data)?)
|
||||
Ok(Self::from_bin_svg(&data)?)
|
||||
} else {
|
||||
Ok(CustomGlyphContent::from_bin_raster(&data)?)
|
||||
Ok(Self::from_bin_raster(&data)?)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -107,7 +109,7 @@ pub struct CustomGlyph {
|
||||
}
|
||||
|
||||
impl CustomGlyph {
|
||||
pub fn new(data: CustomGlyphData) -> Self {
|
||||
pub const fn new(data: CustomGlyphData) -> Self {
|
||||
Self {
|
||||
data,
|
||||
left: 0.0,
|
||||
@@ -156,10 +158,10 @@ pub struct RasterizedCustomGlyph {
|
||||
}
|
||||
|
||||
impl RasterizedCustomGlyph {
|
||||
pub(super) fn try_from(input: &RasterizeCustomGlyphRequest) -> Option<RasterizedCustomGlyph> {
|
||||
pub(super) fn try_from(input: &RasterizeCustomGlyphRequest) -> Option<Self> {
|
||||
match input.data.content.as_ref() {
|
||||
CustomGlyphContent::Svg(tree) => rasterize_svg(tree, input),
|
||||
CustomGlyphContent::Image(data) => rasterize_image(data),
|
||||
CustomGlyphContent::Image(data) => Some(rasterize_image(data)),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -212,7 +214,7 @@ pub enum ContentType {
|
||||
|
||||
impl ContentType {
|
||||
/// The number of bytes per pixel for this content type
|
||||
pub fn bytes_per_pixel(&self) -> usize {
|
||||
pub const fn bytes_per_pixel(&self) -> usize {
|
||||
match self {
|
||||
Self::Color => 4,
|
||||
Self::Mask => 1,
|
||||
@@ -226,10 +228,10 @@ fn rasterize_svg(
|
||||
) -> Option<RasterizedCustomGlyph> {
|
||||
// Calculate the scale based on the "glyph size".
|
||||
let svg_size = tree.size();
|
||||
let scale_x = input.width as f32 / svg_size.width();
|
||||
let scale_y = input.height as f32 / svg_size.height();
|
||||
let scale_x = f32::from(input.width) / svg_size.width();
|
||||
let scale_y = f32::from(input.height) / svg_size.height();
|
||||
|
||||
let mut pixmap = resvg::tiny_skia::Pixmap::new(input.width as u32, input.height as u32)?;
|
||||
let mut pixmap = resvg::tiny_skia::Pixmap::new(u32::from(input.width), u32::from(input.height))?;
|
||||
let mut transform = resvg::usvg::Transform::from_scale(scale_x, scale_y);
|
||||
|
||||
// Offset the glyph by the subpixel amount.
|
||||
@@ -249,11 +251,11 @@ fn rasterize_svg(
|
||||
})
|
||||
}
|
||||
|
||||
fn rasterize_image(image: &RgbaImage) -> Option<RasterizedCustomGlyph> {
|
||||
Some(RasterizedCustomGlyph {
|
||||
fn rasterize_image(image: &RgbaImage) -> RasterizedCustomGlyph {
|
||||
RasterizedCustomGlyph {
|
||||
data: image.to_vec(),
|
||||
content_type: ContentType::Color,
|
||||
width: image.width() as _,
|
||||
height: image.height() as _,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,10 +20,10 @@ pub static FONT_SYSTEM: LazyLock<Mutex<FontSystem>> =
|
||||
pub static SWASH_CACHE: LazyLock<Mutex<SwashCache>> =
|
||||
LazyLock::new(|| Mutex::new(SwashCache::new()));
|
||||
|
||||
/// Used in case no font_size is defined
|
||||
/// Used in case no `font_size` is defined
|
||||
const DEFAULT_FONT_SIZE: f32 = 14.;
|
||||
|
||||
/// In case no line_height is defined, use font_size * DEFAULT_LINE_HEIGHT_RATIO
|
||||
/// In case no `line_height` is defined, use `font_size` * `DEFAULT_LINE_HEIGHT_RATIO`
|
||||
const DEFAULT_LINE_HEIGHT_RATIO: f32 = 1.43;
|
||||
|
||||
pub(crate) const DEFAULT_METRICS: Metrics = Metrics::new(
|
||||
@@ -55,7 +55,7 @@ impl From<&TextStyle> for Metrics {
|
||||
fn from(style: &TextStyle) -> Self {
|
||||
let font_size = style.size.unwrap_or(DEFAULT_FONT_SIZE);
|
||||
|
||||
Metrics {
|
||||
Self {
|
||||
font_size,
|
||||
line_height: style
|
||||
.size
|
||||
@@ -67,9 +67,9 @@ impl From<&TextStyle> for Metrics {
|
||||
impl From<&TextStyle> for Wrap {
|
||||
fn from(value: &TextStyle) -> Self {
|
||||
if value.wrap {
|
||||
Wrap::WordOrGlyph
|
||||
Self::WordOrGlyph
|
||||
} else {
|
||||
Wrap::None
|
||||
Self::None
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -84,10 +84,10 @@ pub enum FontStyle {
|
||||
}
|
||||
|
||||
impl From<FontStyle> for Style {
|
||||
fn from(value: FontStyle) -> Style {
|
||||
fn from(value: FontStyle) -> Self {
|
||||
match value {
|
||||
FontStyle::Normal => Style::Normal,
|
||||
FontStyle::Italic => Style::Italic,
|
||||
FontStyle::Normal => Self::Normal,
|
||||
FontStyle::Italic => Self::Italic,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -100,10 +100,10 @@ pub enum FontWeight {
|
||||
}
|
||||
|
||||
impl From<FontWeight> for Weight {
|
||||
fn from(value: FontWeight) -> Weight {
|
||||
fn from(value: FontWeight) -> Self {
|
||||
match value {
|
||||
FontWeight::Normal => Weight::NORMAL,
|
||||
FontWeight::Bold => Weight::BOLD,
|
||||
FontWeight::Normal => Self::NORMAL,
|
||||
FontWeight::Bold => Self::BOLD,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -119,20 +119,20 @@ pub enum HorizontalAlign {
|
||||
}
|
||||
|
||||
impl From<HorizontalAlign> for Align {
|
||||
fn from(value: HorizontalAlign) -> Align {
|
||||
fn from(value: HorizontalAlign) -> Self {
|
||||
match value {
|
||||
HorizontalAlign::Left => Align::Left,
|
||||
HorizontalAlign::Right => Align::Right,
|
||||
HorizontalAlign::Center => Align::Center,
|
||||
HorizontalAlign::Justified => Align::Justified,
|
||||
HorizontalAlign::End => Align::End,
|
||||
HorizontalAlign::Left => Self::Left,
|
||||
HorizontalAlign::Right => Self::Right,
|
||||
HorizontalAlign::Center => Self::Center,
|
||||
HorizontalAlign::Justified => Self::Justified,
|
||||
HorizontalAlign::End => Self::End,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<drawing::Color> for cosmic_text::Color {
|
||||
fn from(value: drawing::Color) -> cosmic_text::Color {
|
||||
cosmic_text::Color::rgba(
|
||||
fn from(value: drawing::Color) -> Self {
|
||||
Self::rgba(
|
||||
(value.r * 255.999) as _,
|
||||
(value.g * 255.999) as _,
|
||||
(value.b * 255.999) as _,
|
||||
@@ -142,12 +142,12 @@ impl From<drawing::Color> for cosmic_text::Color {
|
||||
}
|
||||
|
||||
impl From<cosmic_text::Color> for drawing::Color {
|
||||
fn from(value: cosmic_text::Color) -> drawing::Color {
|
||||
drawing::Color::new(
|
||||
value.r() as f32 / 255.999,
|
||||
value.g() as f32 / 255.999,
|
||||
value.b() as f32 / 255.999,
|
||||
value.a() as f32 / 255.999,
|
||||
fn from(value: cosmic_text::Color) -> Self {
|
||||
Self::new(
|
||||
f32::from(value.r()) / 255.999,
|
||||
f32::from(value.g()) / 255.999,
|
||||
f32::from(value.b()) / 255.999,
|
||||
f32::from(value.a()) / 255.999,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,7 +21,7 @@ use super::{
|
||||
};
|
||||
use crate::gfx::{BLEND_ALPHA, WGfx, pipeline::WGfxPipeline};
|
||||
|
||||
/// Pipeline & shaders to be reused between TextRenderer instances
|
||||
/// Pipeline & shaders to be reused between `TextRenderer` instances
|
||||
#[derive(Clone)]
|
||||
pub struct TextPipeline {
|
||||
pub(super) gfx: Arc<WGfx>,
|
||||
@@ -34,8 +34,8 @@ impl TextPipeline {
|
||||
let frag = frag_atlas::load(gfx.device.clone())?;
|
||||
|
||||
let pipeline = gfx.create_pipeline::<GlyphVertex>(
|
||||
vert,
|
||||
frag,
|
||||
&vert,
|
||||
&frag,
|
||||
format,
|
||||
Some(BLEND_ALPHA),
|
||||
PrimitiveTopology::TriangleStrip,
|
||||
@@ -133,7 +133,7 @@ impl InnerAtlas {
|
||||
})
|
||||
}
|
||||
|
||||
fn descriptor_set(kind: Kind) -> usize {
|
||||
const fn descriptor_set(kind: Kind) -> usize {
|
||||
match kind {
|
||||
Kind::Color => 0,
|
||||
Kind::Mask => 1,
|
||||
@@ -176,7 +176,7 @@ impl InnerAtlas {
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn num_channels(&self) -> usize {
|
||||
pub const fn num_channels(&self) -> usize {
|
||||
self.kind.num_channels()
|
||||
}
|
||||
|
||||
@@ -185,13 +185,14 @@ impl InnerAtlas {
|
||||
font_system: &mut FontSystem,
|
||||
cache: &mut SwashCache,
|
||||
) -> anyhow::Result<bool> {
|
||||
const GROWTH_FACTOR: u32 = 2;
|
||||
|
||||
if self.size >= self.max_texture_dimension_2d {
|
||||
return Ok(false);
|
||||
}
|
||||
|
||||
// Grow each dimension by a factor of 2. The growth factor was chosen to match the growth
|
||||
// factor of `Vec`.`
|
||||
const GROWTH_FACTOR: u32 = 2;
|
||||
let new_size = (self.size * GROWTH_FACTOR).min(self.max_texture_dimension_2d);
|
||||
log::info!("Grow {:?} atlas {} → {new_size}", self.kind, self.size);
|
||||
|
||||
@@ -230,11 +231,11 @@ impl InnerAtlas {
|
||||
GlyphonCacheKey::Custom(cache_key) => (cache_key.width as usize, cache_key.height as usize),
|
||||
};
|
||||
|
||||
let offset = [x as _, y as _, 0];
|
||||
let offset = [x.into(), y.into(), 0];
|
||||
cmd_buf.copy_image(
|
||||
old_image.clone(),
|
||||
&old_image.clone(),
|
||||
offset,
|
||||
image.clone(),
|
||||
&image.clone(),
|
||||
offset,
|
||||
Some([width as _, height as _, 1]),
|
||||
)?;
|
||||
@@ -267,17 +268,17 @@ pub(super) enum Kind {
|
||||
}
|
||||
|
||||
impl Kind {
|
||||
fn num_channels(self) -> usize {
|
||||
const fn num_channels(self) -> usize {
|
||||
match self {
|
||||
Kind::Mask => 1,
|
||||
Kind::Color => 4,
|
||||
Self::Mask => 1,
|
||||
Self::Color => 4,
|
||||
}
|
||||
}
|
||||
|
||||
fn texture_format(self) -> Format {
|
||||
const fn texture_format(self) -> Format {
|
||||
match self {
|
||||
Kind::Mask => Format::R8_UNORM,
|
||||
Kind::Color => Format::R8G8B8A8_UNORM,
|
||||
Self::Mask => Format::R8_UNORM,
|
||||
Self::Color => Format::R8G8B8A8_UNORM,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -326,7 +327,10 @@ impl TextAtlas {
|
||||
Ok(did_grow)
|
||||
}
|
||||
|
||||
pub(super) fn inner_for_content_mut(&mut self, content_type: ContentType) -> &mut InnerAtlas {
|
||||
pub(super) const fn inner_for_content_mut(
|
||||
&mut self,
|
||||
content_type: ContentType,
|
||||
) -> &mut InnerAtlas {
|
||||
match content_type {
|
||||
ContentType::Color => &mut self.color_atlas,
|
||||
ContentType::Mask => &mut self.mask_atlas,
|
||||
|
||||
@@ -52,6 +52,7 @@ impl TextRenderer {
|
||||
}
|
||||
|
||||
/// Prepares all of the provided text areas for rendering.
|
||||
#[allow(clippy::too_many_lines)]
|
||||
pub fn prepare<'a>(
|
||||
&mut self,
|
||||
font_system: &mut FontSystem,
|
||||
@@ -70,7 +71,7 @@ impl TextRenderer {
|
||||
let bounds_max_x = text_area.bounds.right.min(resolution[0] as i32);
|
||||
let bounds_max_y = text_area.bounds.bottom.min(resolution[1] as i32);
|
||||
|
||||
for glyph in text_area.custom_glyphs.iter() {
|
||||
for glyph in text_area.custom_glyphs {
|
||||
let x = text_area.left + (glyph.left * text_area.scale);
|
||||
let y = text_area.top + (glyph.top * text_area.scale);
|
||||
let width = (glyph.width * text_area.scale).round() as u16;
|
||||
@@ -102,7 +103,7 @@ impl TextRenderer {
|
||||
let color = glyph.color.unwrap_or(text_area.default_color);
|
||||
|
||||
if let Some(glyph_to_render) = prepare_glyph(
|
||||
PrepareGlyphParams {
|
||||
&mut PrepareGlyphParams {
|
||||
label_pos: Vec2::new(text_area.left, text_area.top),
|
||||
x,
|
||||
y,
|
||||
@@ -114,7 +115,7 @@ impl TextRenderer {
|
||||
font_system,
|
||||
model_buffer: &mut self.model_buffer,
|
||||
scale_factor: text_area.scale,
|
||||
glyph_scale: width as f32 / cached_width as f32,
|
||||
glyph_scale: f32::from(width) / f32::from(cached_width),
|
||||
bounds_min_x,
|
||||
bounds_min_y,
|
||||
bounds_max_x,
|
||||
@@ -169,7 +170,7 @@ impl TextRenderer {
|
||||
.take_while(is_run_visible);
|
||||
|
||||
for run in layout_runs {
|
||||
for glyph in run.glyphs.iter() {
|
||||
for glyph in run.glyphs {
|
||||
let physical_glyph = glyph.physical((text_area.left, text_area.top), text_area.scale);
|
||||
|
||||
let color = match glyph.color_opt {
|
||||
@@ -178,7 +179,7 @@ impl TextRenderer {
|
||||
};
|
||||
|
||||
if let Some(glyph_to_render) = prepare_glyph(
|
||||
PrepareGlyphParams {
|
||||
&mut PrepareGlyphParams {
|
||||
label_pos: Vec2::new(text_area.left, text_area.top),
|
||||
x: physical_glyph.x,
|
||||
y: physical_glyph.y,
|
||||
@@ -323,9 +324,9 @@ struct PrepareGlyphParams<'a> {
|
||||
depth: f32,
|
||||
}
|
||||
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
#[allow(clippy::too_many_lines)]
|
||||
fn prepare_glyph(
|
||||
par: PrepareGlyphParams,
|
||||
par: &mut PrepareGlyphParams,
|
||||
get_glyph_image: impl FnOnce(&mut SwashCache, &mut FontSystem) -> Option<GetGlyphImageResult>,
|
||||
) -> anyhow::Result<Option<GlyphVertex>> {
|
||||
let gfx = par.atlas.common.gfx.clone();
|
||||
@@ -347,33 +348,31 @@ fn prepare_glyph(
|
||||
|
||||
// Find a position in the packer
|
||||
let allocation = loop {
|
||||
match inner.try_allocate(image.width as usize, image.height as usize) {
|
||||
Some(a) => break a,
|
||||
None => {
|
||||
if !par
|
||||
.atlas
|
||||
.grow(par.font_system, par.cache, image.content_type)?
|
||||
{
|
||||
anyhow::bail!(
|
||||
"Atlas full. atlas: {:?} cache_key: {:?}",
|
||||
image.content_type,
|
||||
par.cache_key
|
||||
);
|
||||
}
|
||||
|
||||
inner = par.atlas.inner_for_content_mut(image.content_type);
|
||||
}
|
||||
if let Some(a) = inner.try_allocate(image.width as usize, image.height as usize) {
|
||||
break a;
|
||||
}
|
||||
if !par
|
||||
.atlas
|
||||
.grow(par.font_system, par.cache, image.content_type)?
|
||||
{
|
||||
anyhow::bail!(
|
||||
"Atlas full. atlas: {:?} cache_key: {:?}",
|
||||
image.content_type,
|
||||
par.cache_key
|
||||
);
|
||||
}
|
||||
|
||||
inner = par.atlas.inner_for_content_mut(image.content_type);
|
||||
};
|
||||
let atlas_min = allocation.rectangle.min;
|
||||
|
||||
let mut cmd_buf = gfx.create_xfer_command_buffer(CommandBufferUsage::OneTimeSubmit)?;
|
||||
|
||||
cmd_buf.update_image(
|
||||
inner.image_view.image().clone(),
|
||||
inner.image_view.image(),
|
||||
&image.data,
|
||||
[atlas_min.x as _, atlas_min.y as _, 0],
|
||||
Some([image.width as _, image.height as _, 1]),
|
||||
Some([image.width.into(), image.height.into(), 1]),
|
||||
)?;
|
||||
|
||||
cmd_buf.build_and_execute_now()?; //TODO: do not wait for fence here
|
||||
@@ -406,16 +405,16 @@ fn prepare_glyph(
|
||||
})
|
||||
};
|
||||
|
||||
let mut x = par.x + details.left as i32;
|
||||
let mut y = (par.line_y * par.scale_factor).round() as i32 + par.y - details.top as i32;
|
||||
let mut x = par.x + i32::from(details.left);
|
||||
let mut y = (par.line_y * par.scale_factor).round() as i32 + par.y - i32::from(details.top);
|
||||
|
||||
let (mut atlas_x, mut atlas_y, content_type) = match details.gpu_cache {
|
||||
GpuCacheStatus::InAtlas { x, y, content_type } => (x, y, content_type),
|
||||
GpuCacheStatus::SkipRasterization => return Ok(None),
|
||||
};
|
||||
|
||||
let mut glyph_width = details.width as i32;
|
||||
let mut glyph_height = details.height as i32;
|
||||
let mut glyph_width = i32::from(details.width);
|
||||
let mut glyph_height = i32::from(details.height);
|
||||
|
||||
// Starts beyond right edge or ends beyond left edge
|
||||
let max_x = x + glyph_width;
|
||||
@@ -429,7 +428,7 @@ fn prepare_glyph(
|
||||
return Ok(None);
|
||||
}
|
||||
|
||||
// Clip left ege
|
||||
// Clip left edge
|
||||
if x < par.bounds_min_x {
|
||||
let right_shift = par.bounds_min_x - x;
|
||||
|
||||
|
||||
@@ -7,8 +7,8 @@ use vulkano::buffer::BufferContents;
|
||||
pub struct WMat4(pub [f32; 16]);
|
||||
|
||||
impl WMat4 {
|
||||
pub fn from_glam(mat: &Mat4) -> WMat4 {
|
||||
WMat4(*mat.as_ref())
|
||||
pub fn from_glam(mat: &Mat4) -> Self {
|
||||
Self(*mat.as_ref())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -20,7 +20,8 @@ pub struct Viewport {
|
||||
|
||||
impl Viewport {
|
||||
/// Creates a new `Viewport` with the given `device` and `cache`.
|
||||
pub fn new(gfx: Arc<WGfx>) -> anyhow::Result<Self> {
|
||||
#[allow(clippy::iter_on_single_items)]
|
||||
pub fn new(gfx: &Arc<WGfx>) -> anyhow::Result<Self> {
|
||||
let params = Params {
|
||||
screen_resolution: [0, 0],
|
||||
pixel_scale: 1.0,
|
||||
@@ -87,7 +88,7 @@ impl Viewport {
|
||||
}
|
||||
|
||||
/// Returns the current resolution of the `Viewport`.
|
||||
pub fn resolution(&self) -> [u32; 2] {
|
||||
pub const fn resolution(&self) -> [u32; 2] {
|
||||
self.params.screen_resolution
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,11 +35,11 @@ impl TransformStack {
|
||||
self.top -= 1;
|
||||
}
|
||||
|
||||
pub fn get(&self) -> &Transform {
|
||||
pub const fn get(&self) -> &Transform {
|
||||
&self.stack[(self.top - 1) as usize]
|
||||
}
|
||||
|
||||
pub fn get_pos(&self) -> Vec2 {
|
||||
pub const fn get_pos(&self) -> Vec2 {
|
||||
self.stack[(self.top - 1) as usize].pos
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ use super::{WidgetObj, WidgetState};
|
||||
pub struct WidgetDiv {}
|
||||
|
||||
impl WidgetDiv {
|
||||
pub fn create() -> anyhow::Result<WidgetState> {
|
||||
pub fn create() -> WidgetState {
|
||||
WidgetState::new(Box::new(Self {}))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,7 +24,7 @@ pub struct WidgetLabel {
|
||||
}
|
||||
|
||||
impl WidgetLabel {
|
||||
pub fn create(i18n: &mut I18n, params: WidgetLabelParams) -> anyhow::Result<WidgetState> {
|
||||
pub fn create(i18n: &mut I18n, params: WidgetLabelParams) -> WidgetState {
|
||||
let metrics = Metrics::from(¶ms.style);
|
||||
let attrs = Attrs::from(¶ms.style);
|
||||
let wrap = Wrap::from(¶ms.style);
|
||||
@@ -39,7 +39,7 @@ impl WidgetLabel {
|
||||
[(params.content.generate(i18n).as_ref(), attrs)],
|
||||
&Attrs::new(),
|
||||
Shaping::Advanced,
|
||||
params.style.align.map(|a| a.into()),
|
||||
params.style.align.map(Into::into),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -65,7 +65,7 @@ impl WidgetLabel {
|
||||
[(self.params.content.generate(i18n).as_ref(), attrs)],
|
||||
&Attrs::new(),
|
||||
Shaping::Advanced,
|
||||
self.params.style.align.map(|a| a.into()),
|
||||
self.params.style.align.map(Into::into),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,7 +27,7 @@ pub struct WidgetData {
|
||||
}
|
||||
|
||||
impl WidgetData {
|
||||
pub fn set_device_pressed(&mut self, device: usize, pressed: bool) -> bool {
|
||||
pub const fn set_device_pressed(&mut self, device: usize, pressed: bool) -> bool {
|
||||
let bit = 1 << device;
|
||||
let state_changed;
|
||||
if pressed {
|
||||
@@ -40,7 +40,7 @@ impl WidgetData {
|
||||
state_changed
|
||||
}
|
||||
|
||||
pub fn set_device_hovered(&mut self, device: usize, hovered: bool) -> bool {
|
||||
pub const fn set_device_hovered(&mut self, device: usize, hovered: bool) -> bool {
|
||||
let bit = 1 << device;
|
||||
let state_changed;
|
||||
if hovered {
|
||||
@@ -53,19 +53,19 @@ impl WidgetData {
|
||||
state_changed
|
||||
}
|
||||
|
||||
pub fn get_pressed(&self, device: usize) -> bool {
|
||||
pub const fn get_pressed(&self, device: usize) -> bool {
|
||||
self.pressed & (1 << device) != 0
|
||||
}
|
||||
|
||||
pub fn get_hovered(&self, device: usize) -> bool {
|
||||
pub const fn get_hovered(&self, device: usize) -> bool {
|
||||
self.hovered & (1 << device) != 0
|
||||
}
|
||||
|
||||
pub fn is_pressed(&self) -> bool {
|
||||
pub const fn is_pressed(&self) -> bool {
|
||||
self.pressed != 0
|
||||
}
|
||||
|
||||
pub fn is_hovered(&self) -> bool {
|
||||
pub const fn is_hovered(&self) -> bool {
|
||||
self.hovered != 0
|
||||
}
|
||||
}
|
||||
@@ -82,8 +82,8 @@ impl WidgetState {
|
||||
(data, obj)
|
||||
}
|
||||
|
||||
fn new(obj: Box<dyn WidgetObj>) -> anyhow::Result<Self> {
|
||||
Ok(Self {
|
||||
fn new(obj: Box<dyn WidgetObj>) -> Self {
|
||||
Self {
|
||||
data: WidgetData {
|
||||
hovered: 0,
|
||||
pressed: 0,
|
||||
@@ -91,7 +91,7 @@ impl WidgetState {
|
||||
transform: glam::Mat4::IDENTITY,
|
||||
},
|
||||
obj,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -130,7 +130,7 @@ pub struct EventParams<'a> {
|
||||
}
|
||||
|
||||
impl EventParams<'_> {
|
||||
pub fn mark_redraw(&mut self) {
|
||||
pub const fn mark_redraw(&mut self) {
|
||||
self.alterables.needs_redraw = true;
|
||||
}
|
||||
}
|
||||
@@ -321,6 +321,8 @@ impl WidgetState {
|
||||
true
|
||||
}
|
||||
|
||||
#[allow(clippy::too_many_lines)]
|
||||
#[allow(clippy::cognitive_complexity)]
|
||||
pub fn process_event<'a, U1, U2>(
|
||||
&mut self,
|
||||
widget_id: WidgetID,
|
||||
|
||||
@@ -22,8 +22,8 @@ pub struct WidgetRectangle {
|
||||
}
|
||||
|
||||
impl WidgetRectangle {
|
||||
pub fn create(params: WidgetRectangleParams) -> anyhow::Result<WidgetState> {
|
||||
WidgetState::new(Box::new(WidgetRectangle { params }))
|
||||
pub fn create(params: WidgetRectangleParams) -> WidgetState {
|
||||
WidgetState::new(Box::new(Self { params }))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -24,7 +24,7 @@ pub struct WidgetSprite {
|
||||
}
|
||||
|
||||
impl WidgetSprite {
|
||||
pub fn create(params: WidgetSpriteParams) -> anyhow::Result<WidgetState> {
|
||||
pub fn create(params: WidgetSpriteParams) -> WidgetState {
|
||||
WidgetState::new(Box::new(Self { params }))
|
||||
}
|
||||
}
|
||||
@@ -44,8 +44,7 @@ impl WidgetObj for WidgetSprite {
|
||||
self
|
||||
.params
|
||||
.color
|
||||
.map(|c| c.into())
|
||||
.unwrap_or(cosmic_text::Color::rgb(255, 255, 255)),
|
||||
.map_or(cosmic_text::Color::rgb(255, 255, 255), Into::into),
|
||||
),
|
||||
snap_to_physical_pixel: true,
|
||||
};
|
||||
@@ -76,7 +75,7 @@ impl WidgetObj for WidgetSprite {
|
||||
payload: drawing::PrimitivePayload::Text(Rc::new(RefCell::new(buffer))),
|
||||
transform: state.transform_stack.get().transform,
|
||||
});
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
fn measure(
|
||||
|
||||
@@ -51,8 +51,8 @@ pub(super) struct LinePool {
|
||||
impl LinePool {
|
||||
pub(super) fn new(app: &AppState) -> anyhow::Result<Self> {
|
||||
let pipeline = app.gfx.create_pipeline(
|
||||
app.gfx_extras.shaders.get("vert_quad").unwrap().clone(), // want panic
|
||||
app.gfx_extras.shaders.get("frag_color").unwrap().clone(), // want panic
|
||||
app.gfx_extras.shaders.get("vert_quad").unwrap(), // want panic
|
||||
app.gfx_extras.shaders.get("frag_color").unwrap(), // want panic
|
||||
app.gfx.surface_format,
|
||||
None,
|
||||
PrimitiveTopology::TriangleStrip,
|
||||
|
||||
@@ -15,13 +15,13 @@ use vulkano::{
|
||||
use crate::{
|
||||
backend::openxr::{helpers::translation_rotation_to_posef, swapchain::SwapchainOpts},
|
||||
config_io,
|
||||
graphics::{dds::WlxCommandBufferDds, CommandBuffers, ExtentExt},
|
||||
graphics::{CommandBuffers, ExtentExt, dds::WlxCommandBufferDds},
|
||||
state::AppState,
|
||||
};
|
||||
|
||||
use super::{
|
||||
swapchain::{create_swapchain, WlxSwapchain},
|
||||
CompositionLayer, XrState,
|
||||
swapchain::{WlxSwapchain, create_swapchain},
|
||||
};
|
||||
|
||||
pub(super) struct Skybox {
|
||||
@@ -96,8 +96,8 @@ impl Skybox {
|
||||
let mut swapchain = create_swapchain(xr, app.gfx.clone(), extent, opts)?;
|
||||
let tgt = swapchain.acquire_wait_image()?;
|
||||
let pipeline = app.gfx.create_pipeline(
|
||||
app.gfx_extras.shaders.get("vert_quad").unwrap().clone(), // want panic
|
||||
app.gfx_extras.shaders.get("frag_srgb").unwrap().clone(), // want panic
|
||||
app.gfx_extras.shaders.get("vert_quad").unwrap(), // want panic
|
||||
app.gfx_extras.shaders.get("frag_srgb").unwrap(), // want panic
|
||||
app.gfx.surface_format,
|
||||
None,
|
||||
PrimitiveTopology::TriangleStrip,
|
||||
@@ -145,8 +145,8 @@ impl Skybox {
|
||||
SwapchainOpts::new().immutable(),
|
||||
)?;
|
||||
let pipeline = app.gfx.create_pipeline(
|
||||
app.gfx_extras.shaders.get("vert_quad").unwrap().clone(), // want panic
|
||||
app.gfx_extras.shaders.get("frag_grid").unwrap().clone(), // want panic
|
||||
app.gfx_extras.shaders.get("vert_quad").unwrap(), // want panic
|
||||
app.gfx_extras.shaders.get("frag_grid").unwrap(), // want panic
|
||||
app.gfx.surface_format,
|
||||
Some(AttachmentBlend::alpha()),
|
||||
PrimitiveTopology::TriangleStrip,
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#![warn(clippy::all, clippy::pedantic, clippy::nursery, clippy::cargo)]
|
||||
#![allow(
|
||||
dead_code,
|
||||
clippy::default_trait_access,
|
||||
clippy::cast_precision_loss,
|
||||
clippy::cast_possible_truncation,
|
||||
clippy::cast_sign_loss,
|
||||
|
||||
@@ -60,8 +60,7 @@ where
|
||||
color: wgui::drawing::Color::new(0., 0., 0., 0.6),
|
||||
round: WLength::Units(4.0),
|
||||
..Default::default()
|
||||
})
|
||||
.unwrap(),
|
||||
}),
|
||||
taffy::Style {
|
||||
flex_direction: taffy::FlexDirection::Column,
|
||||
padding: length(BACKGROUND_PADDING),
|
||||
@@ -87,7 +86,7 @@ where
|
||||
for row in 0..layout.key_sizes.len() {
|
||||
let (div, _) = panel.layout.add_child(
|
||||
background,
|
||||
WidgetDiv::create().unwrap(),
|
||||
WidgetDiv::create(),
|
||||
taffy::Style {
|
||||
flex_direction: taffy::FlexDirection::Row,
|
||||
..Default::default()
|
||||
@@ -108,7 +107,7 @@ where
|
||||
let Some(key) = layout.get_key_data(keymap.as_ref(), has_altgr, col, row) else {
|
||||
let _ = panel.layout.add_child(
|
||||
div,
|
||||
WidgetDiv::create()?,
|
||||
WidgetDiv::create(),
|
||||
taffy::Style {
|
||||
size: taffy_size,
|
||||
min_size: taffy_size,
|
||||
|
||||
@@ -46,8 +46,8 @@ impl ScreenPipeline {
|
||||
let extentf = [meta.extent[0] as f32, meta.extent[1] as f32];
|
||||
|
||||
let pipeline = app.gfx.create_pipeline(
|
||||
app.gfx_extras.shaders.get("vert_quad").unwrap().clone(), // want panic
|
||||
app.gfx_extras.shaders.get("frag_screen").unwrap().clone(), // want panic
|
||||
app.gfx_extras.shaders.get("vert_quad").unwrap(), // want panic
|
||||
app.gfx_extras.shaders.get("frag_screen").unwrap(), // want panic
|
||||
app.gfx.surface_format,
|
||||
Some(AttachmentBlend::default()),
|
||||
PrimitiveTopology::TriangleStrip,
|
||||
|
||||
@@ -17,7 +17,7 @@ use wgui::{
|
||||
prelude::{auto, length, percent},
|
||||
},
|
||||
widget::{
|
||||
label::{WidgetLabelParams, WidgetLabel},
|
||||
label::{WidgetLabel, WidgetLabelParams},
|
||||
rectangle::{WidgetRectangle, WidgetRectangleParams},
|
||||
util::WLength,
|
||||
},
|
||||
@@ -182,8 +182,7 @@ fn new_toast(toast: Toast, app: &mut AppState) -> Option<(OverlayState, Box<dyn
|
||||
border: 1.0,
|
||||
round: WLength::Units(4.0),
|
||||
..Default::default()
|
||||
})
|
||||
.unwrap(),
|
||||
}),
|
||||
taffy::Style {
|
||||
align_items: Some(taffy::AlignItems::Center),
|
||||
justify_content: Some(taffy::JustifyContent::Center),
|
||||
@@ -205,8 +204,7 @@ fn new_toast(toast: Toast, app: &mut AppState) -> Option<(OverlayState, Box<dyn
|
||||
..Default::default()
|
||||
},
|
||||
},
|
||||
)
|
||||
.unwrap(),
|
||||
),
|
||||
taffy::Style {
|
||||
size: taffy::Size {
|
||||
width: percent(1.0),
|
||||
@@ -229,8 +227,7 @@ fn new_toast(toast: Toast, app: &mut AppState) -> Option<(OverlayState, Box<dyn
|
||||
..Default::default()
|
||||
},
|
||||
},
|
||||
)
|
||||
.unwrap(),
|
||||
),
|
||||
taffy::Style {
|
||||
size: taffy::Size {
|
||||
width: percent(1.0),
|
||||
|
||||
@@ -123,8 +123,8 @@ impl WayVRBackend {
|
||||
resolution: [u16; 2],
|
||||
) -> anyhow::Result<Self> {
|
||||
let pipeline = app.gfx.create_pipeline(
|
||||
app.gfx_extras.shaders.get("vert_quad").unwrap().clone(), // want panic
|
||||
app.gfx_extras.shaders.get("frag_srgb").unwrap().clone(), // want panic
|
||||
app.gfx_extras.shaders.get("vert_quad").unwrap(), // want panic
|
||||
app.gfx_extras.shaders.get("frag_srgb").unwrap(), // want panic
|
||||
app.gfx.surface_format,
|
||||
None,
|
||||
PrimitiveTopology::TriangleStrip,
|
||||
|
||||
Reference in New Issue
Block a user