#![allow(deprecated)]
use std::time::Duration;
use waft_animation::{
AnimationBehavior, AnimationController, AnimationLocalListenersMixin,
AnimationLocalStatusListenersMixin, AnimationRef, AnimationStatus, AnimationStatusListener,
Drive, IntTween,
};
use waft_embedder::{Canvas, Color, Matrix4, Offset, Paint, Rect, TextDirection};
use waft_foundation::{App, Handle, Listener, OptionRetained, Ref};
use waft_painting::{BorderRadius, BoxShape, ShapeBorderRef, draw_rrect, get_as_translation};
use waft_rendering::AnyRenderBox;
use crate::ink_well::{
InteractiveInkFeature, InteractiveInkFeatureData, InteractiveInkFeatureLeaf,
InteractiveInkFeatureLeafData, rrect_from_rect_and_corners,
};
use crate::material::{
AnyMaterialInkController, InkFeature, InkFeatureData, InkFeatureLeaf, InkFeatureLeafData,
Material, RectCallback,
};
const K_DEFAULT_HIGHLIGHT_FADE_DURATION: Duration = Duration::from_millis(200);
pub struct InkHighlight {
ink_feature: InkFeatureData,
interactive_ink_feature: InteractiveInkFeatureData,
shape: BoxShape,
radius: Option<f64>,
border_radius: BorderRadius,
rect_callback: Option<RectCallback>,
text_direction: TextDirection,
alpha: Option<AnimationRef<i64>>,
alpha_controller: Option<Ref<AnimationController>>,
active: bool,
}
impl InkHighlight {
pub const DEFAULT_FADE_DURATION: Duration = K_DEFAULT_HIGHLIGHT_FADE_DURATION;
#[allow(clippy::too_many_arguments)]
pub fn new(
app: &mut App,
controller: AnyMaterialInkController,
reference_box: AnyRenderBox,
color: Color,
text_direction: TextDirection,
shape: BoxShape,
radius: Option<f64>,
border_radius: Option<BorderRadius>,
custom_border: Option<ShapeBorderRef>,
rect_callback: Option<RectCallback>,
on_removed: Option<Listener>,
fade_duration: Duration,
) -> Ref<InkHighlight> {
let ink_feature = InkFeatureData::new(app, controller, reference_box, on_removed);
let this = app.create(InkHighlight {
ink_feature,
interactive_ink_feature: InteractiveInkFeatureData::new(color, custom_border),
shape,
radius,
border_radius: border_radius.unwrap_or(BorderRadius::ZERO),
rect_callback,
text_direction,
alpha: None,
alpha_controller: None,
active: true,
});
let handle = this.handle();
let vsync = controller.vsync(app);
let alpha_controller = AnimationController::new(
app,
None,
Some(fade_duration),
None,
0.0,
1.0,
AnimationBehavior::Normal,
vsync,
);
alpha_controller.add_listener(app, controller.mark_needs_paint_listener());
alpha_controller.add_status_listener(
app,
AnimationStatusListener::new(move |status, app| {
handle.handle_alpha_status_changed(app, status)
}),
);
alpha_controller.forward(app, None);
let alpha_tween = IntTween::new(app, Some(0), Some(i64::from(color.alpha())));
let alpha = alpha_controller.drive(app, alpha_tween);
let highlight = app.get_mut(handle);
highlight.alpha_controller = Some(alpha_controller);
highlight.alpha = Some(alpha);
controller.add_ink_feature(app, this.erase());
this
}
fn alpha_controller(self: Handle<Self>, app: &App) -> Handle<AnimationController> {
app.get(self)
.alpha_controller
.handle()
.expect("created in new")
}
pub fn active(self: Handle<Self>, app: &App) -> bool {
app.get(self).active
}
pub fn activate(self: Handle<Self>, app: &mut App) {
app.get_mut(self).active = true;
self.alpha_controller(app).forward(app, None);
}
pub fn deactivate(self: Handle<Self>, app: &mut App) {
app.get_mut(self).active = false;
self.alpha_controller(app).reverse(app, None);
}
#[cfg(test)]
pub(crate) fn debug_paint_color(self: Handle<Self>, app: &App) -> Color {
let alpha = app
.get(self)
.alpha
.as_ref()
.expect("created in new")
.value(app);
InteractiveInkFeature::color(self, app).with_alpha(alpha as i32)
}
fn handle_alpha_status_changed(self: Handle<Self>, app: &mut App, status: AnimationStatus) {
if status.is_dismissed() && !app.get(self).active {
InkFeatureLeaf::dispose(self, app);
}
}
fn paint_highlight(
self: Handle<Self>,
app: &App,
canvas: &mut Canvas,
rect: Rect,
paint: &Paint,
) {
let highlight = app.get(self);
canvas.save();
if let Some(custom_border) = highlight.interactive_ink_feature().custom_border_ref() {
let path = custom_border.get_outer_path(rect, Some(highlight.text_direction));
canvas.clip_path(
path.as_valo(),
path.fill_type,
waft_embedder::ClipOp::Intersect,
true,
);
}
match highlight.shape {
BoxShape::Circle => {
canvas.draw_circle(
rect.center(),
highlight.radius.unwrap_or(Material::DEFAULT_SPLASH_RADIUS) as f32,
paint,
);
}
BoxShape::Rectangle => {
if highlight.border_radius != BorderRadius::ZERO {
let clip_rrect = rrect_from_rect_and_corners(rect, highlight.border_radius);
draw_rrect(canvas, clip_rrect, paint);
} else {
canvas.draw_rect(rect, paint);
}
}
}
canvas.restore();
}
}
impl InkFeatureLeafData for InkHighlight {
fn ink_feature(&self) -> &InkFeatureData {
&self.ink_feature
}
fn ink_feature_mut(&mut self) -> &mut InkFeatureData {
&mut self.ink_feature
}
}
impl InteractiveInkFeatureLeafData for InkHighlight {
fn interactive_ink_feature(&self) -> &InteractiveInkFeatureData {
&self.interactive_ink_feature
}
fn interactive_ink_feature_mut(&mut self) -> &mut InteractiveInkFeatureData {
&mut self.interactive_ink_feature
}
}
impl InkFeatureLeaf for InkHighlight {
fn dispose(self: Handle<Self>, app: &mut App) {
let alpha_controller = self.alpha_controller(app);
alpha_controller.dispose(app);
InkFeature::dispose(self, app);
}
fn paint_feature(self: Handle<Self>, app: &mut App, canvas: &mut Canvas, transform: &Matrix4) {
let alpha = app
.get(self)
.alpha
.as_ref()
.expect("created in new")
.value(app);
let color = InteractiveInkFeature::color(self, app);
let paint = Paint::from_color(color.with_alpha(alpha as i32).into());
let origin_offset = get_as_translation(*transform);
let rect = match &app.get(self).rect_callback {
Some(rect_callback) => rect_callback(app),
None => Offset::ZERO & InkFeature::reference_box(self, app).size(app),
};
match origin_offset {
None => {
canvas.save();
canvas.concat(transform);
self.paint_highlight(app, canvas, rect, &paint);
canvas.restore();
}
Some(origin_offset) => {
self.paint_highlight(app, canvas, rect.shift(origin_offset), &paint);
}
}
}
}
impl InteractiveInkFeatureLeaf for InkHighlight {}