cushy/
graphics.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
use std::fmt::Debug;
use std::ops::{Deref, DerefMut};

use figures::units::{Px, UPx};
use figures::{
    self, Fraction, IntoSigned, IntoUnsigned, Point, Rect, Round, ScreenScale, ScreenUnit, Size, Zero
};
use kempt::{map, Map};
use kludgine::cosmic_text::{fontdb, FamilyOwned, FontSystem};
use kludgine::drawing::Renderer;
use kludgine::shapes::Shape;
use kludgine::text::{MeasuredText, Text, TextOrigin};
use kludgine::{
    cosmic_text, ClipGuard, Color, Drawable, Kludgine, RenderingGraphics, ShaderScalable,
    ShapeSource, TextureSource,
};

use crate::animation::ZeroToOne;
use crate::fonts::{FontCollection, LoadedFontFace, LoadedFontId};
use crate::styles::FontFamilyList;
use crate::value::{DynamicRead, Generation, Source};

/// A 2d graphics context
pub struct Graphics<'clip, 'gfx, 'pass> {
    renderer: RenderContext<'clip, 'gfx, 'pass>,
    region: Rect<Px>,
    pub(crate) opacity: ZeroToOne,
}

enum RenderContext<'clip, 'gfx, 'pass> {
    Renderer(Renderer<'gfx, 'pass>),
    Clipped(ClipGuard<'clip, Renderer<'gfx, 'pass>>),
}

impl<'gfx, 'pass> Graphics<'_, 'gfx, 'pass> {
    /// Returns a new graphics context for the given [`Renderer`].
    #[must_use]
    pub fn new(renderer: Renderer<'gfx, 'pass>) -> Self {
        Self {
            region: renderer.clip_rect().into_signed(),
            renderer: RenderContext::Renderer(renderer),
            opacity: ZeroToOne::ONE,
        }
    }

    /// Returns the offset relative to the clipping rect that the graphics
    /// context renders at.
    ///
    /// This is used when rendering controls that are partially offscreen to the
    /// left or top of the window's origin.
    ///
    /// In general, this is handled automatically. This function should only be
    /// needed when using [`inner_graphics()`](Self::inner_graphics).
    #[must_use]
    pub fn translation(&self) -> Point<Px> {
        let clip_origin = self.renderer.clip_rect().origin.into_signed();
        self.region.origin - clip_origin
    }

    /// Returns the underlying renderer.
    ///
    /// Note: Kludgine graphics contexts only support clipping. This type adds
    /// [`self.translation()`](Self::translation) to the offset of each drawing
    /// call. When using the underlying renderer, any drawing calls will need
    /// this offset as well, otherwise the widget that is being rendered will
    /// not render correctly when placed in a [`Scroll`](crate::widgets::Scroll)
    /// widget.
    pub fn inner_graphics(&mut self) -> &mut Renderer<'gfx, 'pass> {
        &mut self.renderer
    }

    /// Returns a context that has been clipped to `clip`.
    ///
    /// The new clipping rectangle is interpreted relative to the current
    /// clipping rectangle. As a side effect, this function can never expand the
    /// clipping rect beyond the current clipping rect.
    ///
    /// The returned context will report the clipped size, and all drawing
    /// operations will be relative to the origin of `clip`.
    pub fn clipped_to(&mut self, clip: Rect<Px>) -> Graphics<'_, 'gfx, 'pass> {
        let region = clip + self.region.origin;

        // If the current region has a negative component, we need to adjust the
        // clipped rect before we perform an intersection in unsigned space.
        let mut effective_region = region;
        if region.origin.x < 0 {
            effective_region.size.width += region.origin.x;
            effective_region.origin.x = Px::ZERO;
        }
        if region.origin.y < 0 {
            effective_region.size.height += region.origin.y;
            effective_region.origin.y = Px::ZERO;
        }
        let new_clip = self
            .renderer
            .clip_rect()
            .intersection(&effective_region.into_unsigned())
            .map(|intersection| intersection - self.renderer.clip_rect().origin)
            .unwrap_or_default();

        Graphics {
            renderer: RenderContext::Clipped(self.renderer.clipped_to(new_clip)),
            region,
            opacity: self.opacity,
        }
    }

    /// Returns the current clipping rectangle.
    ///
    /// The clipping rectangle is represented in unsigned pixels in the window's
    /// coordinate system.
    #[must_use]
    pub fn clip_rect(&self) -> Rect<UPx> {
        self.renderer.clip_rect()
    }

    /// Returns the current region being rendered to.
    ///
    /// The rendering region utilizes signed pixels, which allows it to
    /// represent regions that are out of bounds of the window's visible region.
    #[must_use]
    pub fn region(&self) -> Rect<Px> {
        self.region
    }

    /// Returns the visible region of the graphics context.
    ///
    /// This is the intersection of [`Self::region()`] and
    /// [`Self::clip_rect()`].
    #[must_use]
    pub fn visible_rect(&self) -> Option<Rect<UPx>> {
        self.clip_rect().intersection(&self.region.into_unsigned())
    }

    /// Returns the size of the current region.
    ///
    /// This is `self.region().size` converted to unsigned pixels.
    #[must_use]
    pub fn size(&self) -> Size<UPx> {
        self.region.size.into_unsigned()
    }

    /// Returns the current DPI scaling factor applied to the window this
    /// context is attached to.
    #[must_use]
    pub fn scale(&self) -> Fraction {
        self.renderer.scale()
    }

    /// Fills the entire context with `color`.
    ///
    /// If the alpha channel of `color` is 0, this function does nothing.
    pub fn fill(&mut self, color: Color) {
        if color.alpha() > 0 {
            let rect = Rect::from(self.region.size);
            self.draw_shape(&Shape::filled_rect(rect, color));
        }
    }

    /// Draws a shape at the origin, rotating and scaling as needed.
    pub fn draw_shape<'a, Unit>(&mut self, shape: impl Into<Drawable<&'a Shape<Unit, false>, Unit>>)
    where
        Unit: Zero + ShaderScalable + figures::ScreenUnit + Copy,
    {
        let mut shape = shape.into();
        shape.opacity = Some(
            shape
                .opacity
                .map_or(*self.opacity, |opacity| opacity * *self.opacity),
        );
        shape.translation += Point::<Unit>::from_px(self.translation(), self.scale());
        self.renderer.draw_shape(shape);
    }

    /// Draws `texture` at `destination`, scaling as necessary.
    pub fn draw_texture<Unit>(
        &mut self,
        texture: &impl TextureSource,
        destination: Rect<Unit>,
        opacity: ZeroToOne,
    ) where
        Unit: figures::ScreenUnit + ShaderScalable,
        i32: From<<Unit as IntoSigned>::Signed>,
    {
        let translate = Point::<Unit>::from_px(self.translation(), self.scale());
        self.renderer
            .draw_texture(texture, destination + translate, *(self.opacity * opacity));
    }

    /// Draws a shape that was created with texture coordinates, applying the
    /// provided texture.
    pub fn draw_textured_shape<'shape, Unit, Shape>(
        &mut self,
        shape: impl Into<Drawable<&'shape Shape, Unit>>,
        texture: &impl TextureSource,
        opacity: ZeroToOne,
    ) where
        Unit: Zero + ShaderScalable + figures::ScreenUnit + Copy,
        i32: From<<Unit as IntoSigned>::Signed>,
        Shape: ShapeSource<Unit, true> + 'shape,
    {
        let mut shape = shape.into();
        let effective_opacity = self.opacity * opacity;
        shape.opacity = Some(
            shape
                .opacity
                .map_or(*effective_opacity, |opacity| opacity * *effective_opacity),
        );
        shape.translation += Point::<Unit>::from_px(self.translation(), self.scale());
        self.renderer.draw_textured_shape(shape, texture);
    }

    /// Measures `text` using the current text settings.
    ///
    /// `default_color` does not affect the
    pub fn measure_text<'a, Unit>(&mut self, text: impl Into<Text<'a, Unit>>) -> MeasuredText<Unit>
    where
        Unit: figures::ScreenUnit,
    {
        self.renderer.measure_text(text)
    }

    /// Draws `text` using the current text settings.
    pub fn draw_text<'a, Unit>(&mut self, text: impl Into<Drawable<Text<'a, Unit>, Unit>>)
    where
        Unit: ScreenUnit,
    {
        let mut text = text.into();
        text.opacity = Some(
            text.opacity
                .map_or(*self.opacity, |opacity| opacity * *self.opacity),
        );
        text.translation += Point::<Unit>::from_px(self.translation(), self.scale());
        self.renderer.draw_text(text);
    }

    /// Prepares the text layout contained in `buffer` to be rendered.
    ///
    /// When the text in `buffer` has no color defined, `default_color` will be
    /// used.
    ///
    /// `origin` allows controlling how the text will be drawn relative to the
    /// coordinate provided in [`render()`](kludgine::PreparedGraphic::render).
    pub fn draw_text_buffer<'a, Unit>(
        &mut self,
        buffer: impl Into<Drawable<&'a cosmic_text::Buffer, Unit>>,
        default_color: Color,
        origin: TextOrigin<Px>,
    ) where
        Unit: ScreenUnit,
    {
        let mut buffer = buffer.into();
        buffer.opacity = Some(
            buffer
                .opacity
                .map_or(*self.opacity, |opacity| opacity * *self.opacity),
        );
        buffer.translation += Point::<Unit>::from_px(self.translation(), self.scale());
        self.renderer
            .draw_text_buffer(buffer, default_color, origin);
    }

    /// Measures `buffer` and caches the results using `default_color` when
    /// the buffer has no color associated with text.
    pub fn measure_text_buffer<Unit>(
        &mut self,
        buffer: &cosmic_text::Buffer,
        default_color: Color,
    ) -> MeasuredText<Unit>
    where
        Unit: figures::ScreenUnit,
    {
        self.renderer.measure_text_buffer(buffer, default_color)
    }

    /// Prepares the text layout contained in `buffer` to be rendered.
    ///
    /// When the text in `buffer` has no color defined, `default_color` will be
    /// used.
    ///
    /// `origin` allows controlling how the text will be drawn relative to the
    /// coordinate provided in [`render()`](kludgine::PreparedGraphic::render).
    pub fn draw_measured_text<'a, Unit>(
        &mut self,
        text: impl Into<Drawable<&'a MeasuredText<Unit>, Unit>>,
        origin: TextOrigin<Unit>,
    ) where
        Unit: ScreenUnit + Round,
    {
        let mut text = text.into();
        text.opacity = Some(
            text.opacity
                .map_or(*self.opacity, |opacity| opacity * *self.opacity),
        );
        text.translation += Point::<Unit>::from_px(self.translation(), self.scale());
        self.renderer.draw_measured_text(text, origin);
    }

    /// Draws the custom rendering operation when this graphics is presented to
    /// the screen.
    ///
    /// The rendering operation will be clipped automatically, but the rendering
    /// operation will need to position and size itself accordingly.
    pub fn draw<Op: RenderOperation>(&mut self)
    where
        Op::DrawInfo: Default,
    {
        let region = self.region;
        self.renderer
            .draw::<CushyRenderOp<Op>>((<Op::DrawInfo>::default(), region));
    }

    /// Draws `Op` with the given context.
    pub fn draw_with<Op: RenderOperation>(&mut self, context: Op::DrawInfo) {
        let region = self.region;
        self.renderer.draw::<CushyRenderOp<Op>>((context, region));
    }

    /// Returns a reference to the font system used to render.
    pub fn font_system(&mut self) -> &mut FontSystem {
        self.renderer.font_system()
    }

    /// Returns this renderer as a
    /// [`DrawingArea`](plotters::drawing::DrawingArea) compatible with the
    /// [plotters](https://github.com/plotters-rs/plotters) crate.
    #[cfg(feature = "plotters")]
    pub fn as_plot_area(
        &mut self,
    ) -> plotters::drawing::DrawingArea<
        kludgine::drawing::PlotterBackend<'_, 'gfx, 'pass>,
        plotters::coord::Shift,
    > {
        self.renderer.as_plot_area()
    }
}

impl Deref for Graphics<'_, '_, '_> {
    type Target = Kludgine;

    fn deref(&self) -> &Self::Target {
        &self.renderer
    }
}

impl DerefMut for Graphics<'_, '_, '_> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.renderer
    }
}

impl<'gfx, 'pass> Deref for RenderContext<'_, 'gfx, 'pass> {
    type Target = Renderer<'gfx, 'pass>;

    fn deref(&self) -> &Self::Target {
        match self {
            RenderContext::Renderer(renderer) => renderer,
            RenderContext::Clipped(clipped) => clipped,
        }
    }
}

impl DerefMut for RenderContext<'_, '_, '_> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        match self {
            RenderContext::Renderer(renderer) => renderer,
            RenderContext::Clipped(clipped) => &mut *clipped,
        }
    }
}

#[derive(Debug)]
struct Prepared<T> {
    region: Rect<Px>,
    data: T,
}

struct CushyRenderOp<Op>(Op);

impl<Op> kludgine::drawing::RenderOperation for CushyRenderOp<Op>
where
    Op: RenderOperation,
{
    type DrawInfo = (Op::DrawInfo, Rect<Px>);
    type Prepared = Prepared<Op::Prepared>;

    fn new(graphics: &mut kludgine::Graphics<'_>) -> Self {
        Self(Op::new(graphics))
    }

    fn prepare(
        &mut self,
        (context, region): Self::DrawInfo,
        graphics: &mut kludgine::Graphics<'_>,
    ) -> Self::Prepared {
        Prepared {
            region,
            data: self.0.prepare(context, region, graphics),
        }
    }

    fn render<'pass>(
        &'pass self,
        prepared: &Self::Prepared,
        opacity: f32,
        graphics: &mut RenderingGraphics<'_, 'pass>,
    ) {
        self.0
            .render(&prepared.data, prepared.region, opacity, graphics);
    }
}

pub(crate) struct LoadedFontIds {
    generation: usize,
    pub(crate) faces: Vec<LoadedFontFace>,
}

pub struct FontState {
    app_fonts: FontCollection,
    app_font_generation: Generation,
    window_fonts: FontCollection,
    window_font_generation: Option<Generation>,
    pub(crate) loaded_fonts: Map<LoadedFontId, LoadedFontIds>,
    font_generation: usize,
    fonts: Map<String, usize>,
    pub(crate) current_font_family: Option<FontFamilyList>,
}

impl FontState {
    pub fn new(
        db: &mut cosmic_text::fontdb::Database,
        window_fonts: FontCollection,
        app_fonts: FontCollection,
    ) -> Self {
        let mut fonts = Map::new();
        Self::gather_available_family_names(&mut fonts, 0, db);
        let mut state = Self {
            fonts,
            current_font_family: None,
            window_font_generation: None,
            window_fonts,
            app_font_generation: app_fonts.0.generation(),
            app_fonts,
            font_generation: 0,
            loaded_fonts: Map::new(),
        };

        state.update_fonts(db);

        state
    }

    fn gather_available_family_names(
        families: &mut Map<String, usize>,
        generation: usize,
        db: &cosmic_text::fontdb::Database,
    ) {
        for (family, _) in db.faces().filter_map(|f| f.families.first()) {
            families
                .entry(family)
                .and_modify(|gen| *gen = generation)
                .or_insert(generation);
        }

        let mut i = 0;
        while i < families.len() {
            if families.field(i).expect("length checked").value == generation {
                i += 1;
            } else {
                families.remove_by_index(i);
            }
        }
    }

    pub fn update_fonts(&mut self, db: &mut cosmic_text::fontdb::Database) -> bool {
        let new_app_generation = self.app_fonts.0.generation();
        let app_fonts_changed = if self.app_font_generation == new_app_generation {
            false
        } else {
            self.app_font_generation = new_app_generation;
            true
        };
        let new_window_generation = self.window_fonts.0.generation();
        let window_fonts_changed = if self.window_font_generation == Some(new_window_generation) {
            false
        } else {
            self.window_font_generation = Some(new_window_generation);
            true
        };

        let changed = app_fonts_changed || window_fonts_changed;
        if changed {
            self.font_generation += 1;

            if app_fonts_changed {
                Self::synchronize_font_list(
                    &mut self.loaded_fonts,
                    self.font_generation,
                    &self.app_fonts,
                    db,
                );
            }
            if window_fonts_changed {
                Self::synchronize_font_list(
                    &mut self.loaded_fonts,
                    self.font_generation,
                    &self.window_fonts,
                    db,
                );
            }

            // Remove all fonts that didn't have their generation touched.
            let mut i = 0;
            while i < self.loaded_fonts.len() {
                let field = self.loaded_fonts.field(i).expect("length checked");
                let check_if_changed = (app_fonts_changed
                    && self.app_fonts.0.as_ptr() == field.key().collection)
                    || (window_fonts_changed
                        && self.window_fonts.0.as_ptr() == field.key().collection);
                if !check_if_changed || field.value.generation == self.font_generation {
                    i += 1;
                } else {
                    for face in self.loaded_fonts.remove_by_index(i).value.faces {
                        db.remove_face(face.id);
                    }
                }
            }

            Self::gather_available_family_names(&mut self.fonts, self.font_generation, db);
        }

        changed
    }

    fn synchronize_font_list(
        loaded_fonts: &mut Map<LoadedFontId, LoadedFontIds>,
        generation: usize,
        collection: &FontCollection,
        db: &mut cosmic_text::fontdb::Database,
    ) {
        for (font_id, data) in collection.0.read().fonts(collection) {
            match loaded_fonts.entry(font_id) {
                map::Entry::Occupied(mut entry) => {
                    entry.generation = generation;
                }
                map::Entry::Vacant(entry) => {
                    let faces = db
                        .load_font_source(fontdb::Source::Binary(data.clone()))
                        .into_iter()
                        .filter_map(|id| {
                            db.face(id).map(|face| LoadedFontFace {
                                id,
                                families: face.families.clone(),
                                weight: face.weight,
                                style: face.style,
                                stretch: face.stretch,
                            })
                        })
                        .collect();
                    entry.insert(LoadedFontIds { generation, faces });
                }
            }
        }
    }

    #[must_use]
    pub fn next_frame(&mut self, db: &mut cosmic_text::fontdb::Database) -> bool {
        self.current_font_family = None;
        self.update_fonts(db)
    }

    pub fn find_available_font_family(&self, list: &FontFamilyList) -> Option<FamilyOwned> {
        list.iter()
            .find(|family| match family {
                FamilyOwned::Name(name) => self.fonts.contains(name),
                _ => true,
            })
            .cloned()
    }

    pub fn apply_font_family_list(
        &self,
        family: &FontFamilyList,
        fallback: impl FnOnce() -> Option<FamilyOwned>,
        apply: impl FnOnce(String),
    ) {
        if let Some(FamilyOwned::Name(name)) =
            self.find_available_font_family(family).or_else(fallback)
        {
            apply(name);
        }
    }
}

/// A custom wgpu-powered rendering operation.
///
/// # How custom rendering ops work
///
/// When [`Graphics::draw`]/[`Graphics::draw_with`] are invoked for the first
/// time for a given `RenderOperation` implementor, [`new()`](Self::new) is
/// called to create a shared instance of this rendering operation. The new
/// function is a good location to put any initialization that can be shared
/// amongst all drawing calls, as it is invoked only once for each surface.
///
/// After an existing or newly-created operation is located, `prepare()` is
/// invoked passing through the `DrawInfo` provided to the draw call. The result
/// of this function is stored.
///
/// When the graphics is presented, `render()` is invoked providing the data
/// returned from the `prepare()` function.
pub trait RenderOperation: Send + Sync + 'static {
    /// Data that is provided to the `prepare()` function. This is passed
    /// through from the `draw/draw_with` invocation.
    type DrawInfo;
    /// Data that is created in the `prepare()` function, and provided to the
    /// `render()` function.
    type Prepared: Debug + Send + Sync + 'static;

    /// Returns a new instance of this render operation.
    fn new(graphics: &mut kludgine::Graphics<'_>) -> Self;

    /// Prepares this operation to be rendered in `region` in `graphics`.
    ///
    /// This operation's  will automatically be clipped to the available space
    /// for the context it is being drawn to. The render operation should
    /// project itself into `region` and only use the clip rect as an
    /// optimization. To test that this is handled correctly, try placing
    /// whatever is being rendered in a `Scroll` widget and ensure that as the
    /// contents are clipped, the visible area shows the correct contents.
    fn prepare(
        &mut self,
        context: Self::DrawInfo,
        region: Rect<Px>,
        graphics: &mut kludgine::Graphics<'_>,
    ) -> Self::Prepared;

    /// Render `preprared` to `graphics` at `region` with `opacity`.
    ///
    /// This operation's  will automatically be clipped to the available space
    /// for the context it is being drawn to. The render operation should
    /// project itself into `region` and only use the clip rect as an
    /// optimization. To test that this is handled correctly, try placing
    /// whatever is being rendered in a `Scroll` widget and ensure that as the
    /// contents are clipped, the visible area shows the correct contents.
    fn render(
        &self,
        prepared: &Self::Prepared,
        region: Rect<Px>,
        opacity: f32,
        graphics: &mut RenderingGraphics<'_, '_>,
    );
}

/// A [`RenderOperation`] with no per-drawing-call state.
pub trait SimpleRenderOperation: Send + Sync + 'static {
    /// Returns a new instance of this render operation.
    fn new(graphics: &mut kludgine::Graphics<'_>) -> Self;

    /// Render to `graphics` at `rect` with `opacity`.
    ///
    /// This operation's  will automatically be clipped to the available space
    /// for the context it is being drawn to. The render operation should
    /// project itself into `region` and only use the clip rect as an
    /// optimization. To test that this is handled correctly, try placing
    /// whatever is being rendered in a `Scroll` widget and ensure that as the
    /// contents are clipped, the visible area shows the correct contents.
    fn render(&self, region: Rect<Px>, opacity: f32, graphics: &mut RenderingGraphics<'_, '_>);
}

impl<T> RenderOperation for T
where
    T: SimpleRenderOperation,
{
    type DrawInfo = ();
    type Prepared = ();

    fn new(graphics: &mut kludgine::Graphics<'_>) -> Self {
        Self::new(graphics)
    }

    fn prepare(
        &mut self,
        _context: Self::DrawInfo,
        _origin: Rect<Px>,
        _graphics: &mut kludgine::Graphics<'_>,
    ) -> Self::Prepared {
    }

    fn render(
        &self,
        _prepared: &Self::Prepared,
        region: Rect<Px>,
        opacity: f32,
        graphics: &mut RenderingGraphics<'_, '_>,
    ) {
        self.render(region, opacity, graphics);
    }
}