Add missing_trait_methods lints to test helper widgets (#679)

This commit is contained in:
Olivier FAURE 2024-10-17 14:01:39 +02:00 committed by GitHub
parent bad934b21a
commit f4848ce937
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 121 additions and 27 deletions

View File

@ -18,7 +18,10 @@ use std::rc::Rc;
use accesskit::{NodeBuilder, Role};
use accesskit_winit::Event;
use smallvec::SmallVec;
use tracing::trace_span;
use vello::Scene;
use widget::widget::get_child_at_pos;
use widget::WidgetRef;
use crate::event::{PointerEvent, TextEvent};
use crate::widget::SizedBox;
@ -252,8 +255,7 @@ impl<S> ModularWidget<S> {
}
}
// TODO
// #[warn(clippy::missing_trait_methods)]
#[warn(clippy::missing_trait_methods)]
impl<S: 'static> Widget for ModularWidget<S> {
fn on_pointer_event(&mut self, ctx: &mut EventCtx, event: &event::PointerEvent) {
if let Some(f) = self.on_pointer_event.as_mut() {
@ -348,6 +350,42 @@ impl<S: 'static> Widget for ModularWidget<S> {
fn accepts_text_input(&self) -> bool {
self.accepts_text_input
}
fn make_trace_span(&self) -> tracing::Span {
trace_span!("ModularWidget")
}
fn get_debug_text(&self) -> Option<String> {
None
}
fn get_cursor(&self) -> CursorIcon {
CursorIcon::Default
}
fn get_child_at_pos<'c>(
&self,
ctx: QueryCtx<'c>,
pos: Point,
) -> Option<WidgetRef<'c, dyn Widget>> {
get_child_at_pos(self, ctx, pos)
}
fn type_name(&self) -> &'static str {
std::any::type_name::<Self>()
}
fn short_type_name(&self) -> &'static str {
"ModularWidget"
}
fn as_any(&self) -> &dyn std::any::Any {
self.as_dyn_any()
}
fn as_mut_any(&mut self) -> &mut dyn std::any::Any {
self.as_mut_dyn_any()
}
}
impl ReplaceChild {
@ -437,6 +475,7 @@ impl Recording {
}
}
#[warn(clippy::missing_trait_methods)]
impl<W: Widget> Widget for Recorder<W> {
fn on_pointer_event(&mut self, ctx: &mut EventCtx, event: &event::PointerEvent) {
self.recording.push(Record::PE(event.clone()));
@ -496,4 +535,52 @@ impl<W: Widget> Widget for Recorder<W> {
fn children_ids(&self) -> SmallVec<[WidgetId; 16]> {
self.child.children_ids()
}
fn accepts_pointer_interaction(&self) -> bool {
self.child.accepts_pointer_interaction()
}
fn accepts_focus(&self) -> bool {
self.child.accepts_focus()
}
fn accepts_text_input(&self) -> bool {
self.child.accepts_text_input()
}
fn make_trace_span(&self) -> tracing::Span {
self.child.make_trace_span()
}
fn get_debug_text(&self) -> Option<String> {
self.child.get_debug_text()
}
fn get_cursor(&self) -> CursorIcon {
self.child.get_cursor()
}
fn get_child_at_pos<'c>(
&self,
ctx: QueryCtx<'c>,
pos: Point,
) -> Option<WidgetRef<'c, dyn Widget>> {
self.child.get_child_at_pos(ctx, pos)
}
fn type_name(&self) -> &'static str {
std::any::type_name::<Self>()
}
fn short_type_name(&self) -> &'static str {
"Recorder"
}
fn as_any(&self) -> &dyn std::any::Any {
self.child.as_any()
}
fn as_mut_any(&mut self) -> &mut dyn std::any::Any {
self.child.as_mut_any()
}
}

View File

@ -225,31 +225,7 @@ pub trait Widget: AsAny {
ctx: QueryCtx<'c>,
pos: Point,
) -> Option<WidgetRef<'c, dyn Widget>> {
let relative_pos = pos - ctx.window_origin().to_vec2();
if !ctx
.clip_path()
.map_or(true, |clip| clip.contains(relative_pos))
{
return None;
}
// Assumes `Self::children_ids` is in increasing "z-order", picking the last child in case
// of overlapping children.
for child_id in self.children_ids().iter().rev() {
let child = ctx.get(*child_id);
let relative_pos = pos - child.ctx().window_origin().to_vec2();
// The position must be inside the child's layout and inside the child's clip path (if
// any).
if !child.ctx().is_stashed()
&& child.ctx().accepts_pointer_interaction()
&& child.ctx().window_layout_rect().contains(pos)
{
return Some(child);
}
}
None
get_child_at_pos(self, ctx, pos)
}
/// Get the (verbose) type name of the widget for debugging purposes.
@ -291,6 +267,37 @@ pub trait Widget: AsAny {
}
}
pub(crate) fn get_child_at_pos<'c>(
widget: &(impl Widget + ?Sized),
ctx: QueryCtx<'c>,
pos: Point,
) -> Option<WidgetRef<'c, dyn Widget>> {
let relative_pos = pos - ctx.window_origin().to_vec2();
if !ctx
.clip_path()
.map_or(true, |clip| clip.contains(relative_pos))
{
return None;
}
// Assumes `Self::children_ids` is in increasing "z-order", picking the last child in case
// of overlapping children.
for child_id in widget.children_ids().iter().rev() {
let child = ctx.get(*child_id);
// The position must be inside the child's layout and inside the child's clip path (if
// any).
if !child.ctx().is_stashed()
&& child.ctx().accepts_pointer_interaction()
&& child.ctx().window_layout_rect().contains(pos)
{
return Some(child);
}
}
None
}
/// Marker trait for Widgets whose parents can get a raw mutable reference to them.
///
/// "Raw mut" means using a mutable reference (eg `&mut MyWidget`) to the data