rust_widgets 2.5.2

Pure Rust cross-platform native GUI library with hardware-adaptive rendering, 180 widgets, touch/gesture support, i18n, and SVG-pipeline-accurate output
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
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
// SPDX-FileCopyrightText: Copyright (c) 2026 Mike Li/Mikewolfli/Wei Li(mikewolfli@163.com)
// SPDX-License-Identifier: MIT

//! Frame widget.
use crate::compat::{Rc, RefCell, ToString};
use crate::core::{Color, ObjectId, Point, Rect, Size};
use crate::event::{Event, EventHandler};
use crate::render::RenderContext;

use crate::widget::capability::coercion::{expect_f32, expect_string};
use crate::widget::capability::properties_trait::{base_property_get, base_property_set};
use crate::widget::capability::types::{CapabilityAccessError, CapabilityValue};
use crate::widget::capability::WidgetProperties;
use crate::widget::{BaseWidget, Draw, SimpleRegistry, Widget, WidgetKind};
use crate::{impl_widget_property_hooks, property_names_of};
/// Frame widget.
pub struct Frame {
    base: BaseWidget,
    frame_shape: FrameShape,
    frame_shadow: FrameShadow,
    line_width: f32,
    mid_line_width: f32,
    widget: Option<ObjectId>,
    /// Optional shared registry for child widget forwarding.
    registry: Option<Rc<RefCell<SimpleRegistry>>>,
}
/// Frame shape.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum FrameShape {
    /// No frame
    NoFrame,
    /// Box frame
    #[default]
    Box,
    /// Panel frame
    Panel,
    /// Styled panel frame
    StyledPanel,
    /// HLine frame
    HLine,
    /// VLine frame
    VLine,
    /// WinPanel frame
    WinPanel,
}

impl FrameShape {
    /// The factory spelling of this shape, matching `FRAME_PROPERTIES`.
    pub fn as_str(self) -> &'static str {
        match self {
            FrameShape::NoFrame => "no_frame",
            FrameShape::Box => "box",
            FrameShape::Panel => "panel",
            FrameShape::StyledPanel => "styled_panel",
            FrameShape::HLine => "hline",
            FrameShape::VLine => "vline",
            FrameShape::WinPanel => "win_panel",
        }
    }

    /// Parses a factory spelling into a shape.
    pub fn from_name(name: &str) -> Option<Self> {
        Some(match name {
            "no_frame" => FrameShape::NoFrame,
            "box" => FrameShape::Box,
            "panel" => FrameShape::Panel,
            "styled_panel" => FrameShape::StyledPanel,
            "hline" => FrameShape::HLine,
            "vline" => FrameShape::VLine,
            "win_panel" => FrameShape::WinPanel,
            _ => return None,
        })
    }
}
/// Frame shadow.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum FrameShadow {
    /// Plain shadow
    #[default]
    Plain,
    /// Raised shadow
    Raised,
    /// Sunken shadow
    Sunken,
}

impl FrameShadow {
    /// The factory spelling of this shadow, matching `FRAME_PROPERTIES`.
    pub fn as_str(self) -> &'static str {
        match self {
            FrameShadow::Plain => "plain",
            FrameShadow::Raised => "raised",
            FrameShadow::Sunken => "sunken",
        }
    }

    /// Parses a factory spelling into a shadow.
    pub fn from_name(name: &str) -> Option<Self> {
        Some(match name {
            "plain" => FrameShadow::Plain,
            "raised" => FrameShadow::Raised,
            "sunken" => FrameShadow::Sunken,
            _ => return None,
        })
    }
}
impl Frame {
    /// Creates a frame.
    pub fn new(geometry: Rect) -> Self {
        Self {
            base: BaseWidget::new(WidgetKind::Frame, geometry, "Frame"),
            frame_shape: FrameShape::Box,
            frame_shadow: FrameShadow::Plain,
            line_width: 1.0,
            mid_line_width: 0.0,
            widget: None,
            registry: None,
        }
    }
    /// Sets the shared widget registry for child forwarding.
    pub fn set_registry(&mut self, registry: Rc<RefCell<SimpleRegistry>>) {
        self.registry = Some(registry);
        self.base.request_redraw();
    }
    /// Returns the shared widget registry, if set.
    pub fn registry(&self) -> Option<&Rc<RefCell<SimpleRegistry>>> {
        self.registry.as_ref()
    }
    /// Returns frame shape.
    pub fn frame_shape(&self) -> FrameShape {
        self.frame_shape
    }
    /// Sets frame shape.
    pub fn set_frame_shape(&mut self, shape: FrameShape) {
        self.frame_shape = shape;
        self.base.request_redraw();
    }
    /// Returns frame shadow.
    pub fn frame_shadow(&self) -> FrameShadow {
        self.frame_shadow
    }
    /// Sets frame shadow.
    pub fn set_frame_shadow(&mut self, shadow: FrameShadow) {
        self.frame_shadow = shadow;
        self.base.request_redraw();
    }
    /// Returns line width.
    pub fn line_width(&self) -> f32 {
        self.line_width
    }
    /// Sets line width.
    pub fn set_line_width(&mut self, width: f32) {
        self.line_width = width.max(0.0);
        self.base.request_redraw();
    }
    /// Returns mid line width.
    pub fn mid_line_width(&self) -> f32 {
        self.mid_line_width
    }
    /// Sets mid line width.
    pub fn set_mid_line_width(&mut self, width: f32) {
        self.mid_line_width = width.max(0.0);
        self.base.request_redraw();
    }
    /// Sets widget.
    pub fn set_widget(&mut self, widget: Option<ObjectId>) {
        // Remove old child from widget tree before replacing
        if let Some(old) = self.widget {
            self.base.request_redraw();
            self.base.remove_child(old);
        }
        self.widget = widget;
        if let Some(widget_id) = widget {
            self.base.add_child(widget_id);
        }
        self.base.request_redraw();
    }
    /// Returns widget.
    pub fn widget(&self) -> Option<ObjectId> {
        self.widget
    }
    /// Draws frame border.
    fn draw_frame(&self, context: &mut RenderContext) {
        let rect = self.geometry();
        match self.frame_shape {
            FrameShape::NoFrame => {}
            FrameShape::Box => self.draw_box_frame(context, rect),
            FrameShape::Panel => self.draw_panel_frame(context, rect),
            FrameShape::StyledPanel => self.draw_styled_panel_frame(context, rect),
            FrameShape::HLine => self.draw_hline_frame(context, rect),
            FrameShape::VLine => self.draw_vline_frame(context, rect),
            FrameShape::WinPanel => self.draw_win_panel_frame(context, rect),
        }
    }
    /// Draws box frame.
    fn draw_box_frame(&self, context: &mut RenderContext, rect: Rect) {
        // A frame's whole appearance is its outline, so every literal below is the
        // fallback for `border_color`. Two of them are *derived* rather than dropped:
        //
        //  * The raised/sunken bevel needs two tones that read as "lit" and "shaded".
        //    They are made by lightening/darkening the border colour, which is what
        //    keeps the 3D effect readable on a dark surface instead of hardcoding a
        //    white highlight that would glow.
        //  * Explicit black/white border colours are recognised and the bevel is
        //    suppressed, because that is the only nonzero `border_width` a theme can
        //    express today; inventing a white highlight on top of a theme that asked
        //    for a plain black rule would be inventing chrome the caller did not ask
        //    for. An unset or grey border keeps the historical light/dark bevel.
        let style = self.style();
        let border = style.border_color.unwrap_or(Color::rgb(0, 0, 0));
        let themed = style.border_color.is_some();
        let plain =
            themed && (border == Color::rgb(0, 0, 0) || border == Color::rgb(255, 255, 255));
        match self.frame_shadow {
            FrameShadow::Plain => {
                // Draw single border
                context.draw_rect(rect, border);
            }
            FrameShadow::Raised => {
                // Draw raised border
                let light_color = if plain {
                    border
                } else if themed {
                    border.blend(&Color::rgb(255, 255, 255), 0.5)
                } else {
                    Color::rgb(255, 255, 255)
                };
                let dark_color = if plain {
                    border
                } else if themed {
                    border.blend(&Color::rgb(0, 0, 0), 0.5)
                } else {
                    Color::rgb(128, 128, 128)
                };
                // Top and left (light)
                context.draw_line(
                    Point::from_f32(rect.x as f32, rect.y as f32),
                    Point::from_f32(rect.x as f32 + rect.width as f32, rect.y as f32),
                    light_color,
                );
                context.draw_line(
                    Point::from_f32(rect.x as f32, rect.y as f32),
                    Point::from_f32(rect.x as f32, rect.y as f32 + rect.height as f32),
                    light_color,
                );
                // Bottom and right (dark)
                context.draw_line(
                    Point::from_f32(rect.x as f32, rect.y as f32 + rect.height as f32),
                    Point::from_f32(
                        rect.x as f32 + rect.width as f32,
                        rect.y as f32 + rect.height as f32,
                    ),
                    dark_color,
                );
                context.draw_line(
                    Point::from_f32(rect.x as f32 + rect.width as f32, rect.y as f32),
                    Point::from_f32(
                        rect.x as f32 + rect.width as f32,
                        rect.y as f32 + rect.height as f32,
                    ),
                    dark_color,
                );
                // Draw mid line if needed
                let mid_light = if themed {
                    border.blend(&Color::rgb(255, 255, 255), 0.25)
                } else {
                    Color::rgb(192, 192, 192)
                };
                let mid_dark = if themed {
                    border.blend(&Color::rgb(0, 0, 0), 0.75)
                } else {
                    Color::rgb(64, 64, 64)
                };
                self.draw_mid_lines(context, rect, mid_light, mid_dark);
            }
            FrameShadow::Sunken => {
                // Draw sunken border
                let light_color = if plain {
                    border
                } else if themed {
                    border.blend(&Color::rgb(0, 0, 0), 0.5)
                } else {
                    Color::rgb(128, 128, 128)
                };
                let dark_color = if plain {
                    border
                } else if themed {
                    border.blend(&Color::rgb(255, 255, 255), 0.5)
                } else {
                    Color::rgb(255, 255, 255)
                };
                // Top and left (dark)
                context.draw_line(
                    Point::from_f32(rect.x as f32, rect.y as f32),
                    Point::from_f32(rect.x as f32 + rect.width as f32, rect.y as f32),
                    light_color,
                );
                context.draw_line(
                    Point::from_f32(rect.x as f32, rect.y as f32),
                    Point::from_f32(rect.x as f32, rect.y as f32 + rect.height as f32),
                    light_color,
                );
                // Bottom and right (light)
                context.draw_line(
                    Point::from_f32(rect.x as f32, rect.y as f32 + rect.height as f32),
                    Point::from_f32(
                        rect.x as f32 + rect.width as f32,
                        rect.y as f32 + rect.height as f32,
                    ),
                    dark_color,
                );
                context.draw_line(
                    Point::from_f32(rect.x as f32 + rect.width as f32, rect.y as f32),
                    Point::from_f32(
                        rect.x as f32 + rect.width as f32,
                        rect.y as f32 + rect.height as f32,
                    ),
                    dark_color,
                );
                // Draw mid line if needed
                let mid_light = if themed {
                    border.blend(&Color::rgb(0, 0, 0), 0.75)
                } else {
                    Color::rgb(64, 64, 64)
                };
                let mid_dark = if themed {
                    border.blend(&Color::rgb(255, 255, 255), 0.25)
                } else {
                    Color::rgb(192, 192, 192)
                };
                self.draw_mid_lines(context, rect, mid_light, mid_dark);
            }
        }
    }
    /// Draws the mid-line section shared by Raised/Sunken box frames.
    fn draw_mid_lines(
        &self,
        context: &mut RenderContext,
        rect: Rect,
        mid_light: Color,
        mid_dark: Color,
    ) {
        let line_width = self.line_width;
        let mid_line_width = self.mid_line_width;
        if mid_line_width <= 0.0 {
            return;
        }
        context.draw_line(
            Point::from_f32(rect.x as f32 + line_width, rect.y as f32 + line_width),
            Point::from_f32(
                rect.x as f32 + rect.width as f32 - line_width,
                rect.y as f32 + line_width,
            ),
            mid_light,
        );
        context.draw_line(
            Point::from_f32(rect.x as f32 + line_width, rect.y as f32 + line_width),
            Point::from_f32(
                rect.x as f32 + line_width,
                rect.y as f32 + rect.height as f32 - line_width,
            ),
            mid_light,
        );
        context.draw_line(
            Point::from_f32(
                rect.x as f32 + line_width,
                rect.y as f32 + rect.height as f32 - line_width,
            ),
            Point::from_f32(
                rect.x as f32 + rect.width as f32 - line_width,
                rect.y as f32 + rect.height as f32 - line_width,
            ),
            mid_dark,
        );
        context.draw_line(
            Point::from_f32(
                rect.x as f32 + rect.width as f32 - line_width,
                rect.y as f32 + line_width,
            ),
            Point::from_f32(
                rect.x as f32 + rect.width as f32 - line_width,
                rect.y as f32 + rect.height as f32 - line_width,
            ),
            mid_dark,
        );
    }
    /// Draws panel frame with a subtle background fill and simple border.
    fn draw_panel_frame(&self, context: &mut RenderContext, rect: Rect) {
        let style = self.style();
        let bg_color = style.background_color.unwrap_or(Color::rgb(236, 233, 216));
        context.fill_rect(rect, bg_color);
        context.draw_rect(rect, style.border_color.unwrap_or(Color::rgb(64, 64, 64)));
    }
    /// Draws styled panel frame.
    fn draw_styled_panel_frame(&self, context: &mut RenderContext, rect: Rect) {
        // More sophisticated panel with gradient
        let bg_color = self.style().background_color.unwrap_or(Color::rgb(240, 240, 240));
        context.fill_rect(rect, bg_color);
        self.draw_box_frame(context, rect);
    }
    /// Draws horizontal line frame.
    fn draw_hline_frame(&self, context: &mut RenderContext, rect: Rect) {
        let y = rect.y + (rect.height as i32) / 2;
        context.draw_line_stroke(
            Point::new(rect.x, y),
            Point::new(rect.x + rect.width as i32, y),
            self.style().border_color.unwrap_or(Color::rgb(0, 0, 0)),
            self.line_width as u32,
        );
    }
    /// Draws vertical line frame.
    fn draw_vline_frame(&self, context: &mut RenderContext, rect: Rect) {
        let x = rect.x + (rect.width as i32) / 2;
        context.draw_line_stroke(
            Point::new(x, rect.y),
            Point::new(x, rect.y + rect.height as i32),
            self.style().border_color.unwrap_or(Color::rgb(0, 0, 0)),
            self.line_width as u32,
        );
    }
    /// Draws Windows panel frame.
    fn draw_win_panel_frame(&self, context: &mut RenderContext, rect: Rect) {
        // Windows-style panel
        //
        // The fill is themed, but the white/grey bevel is not: it is the illusion of
        // a raised edge, so unlike `draw_box_frame` it is left alone rather than
        // derived from the border colour. A panel that has asked for a dark background
        // still wants its highlight to read as a highlight.
        let style = self.style();
        let bg_color = style.background_color.unwrap_or(Color::rgb(240, 240, 240));
        context.fill_rect(rect, bg_color);
        // Draw 3D border
        let light_color = Color::rgb(255, 255, 255);
        let dark_color = Color::rgb(128, 128, 128);
        // Outer border (sunken)
        context.draw_line(
            Point::from_f32(rect.x as f32, rect.y as f32),
            Point::from_f32(rect.x as f32 + rect.width as f32, rect.y as f32),
            dark_color,
        );
        context.draw_line(
            Point::from_f32(rect.x as f32, rect.y as f32),
            Point::from_f32(rect.x as f32, rect.y as f32 + rect.height as f32),
            dark_color,
        );
        context.draw_line(
            Point::from_f32(rect.x as f32, rect.y as f32 + rect.height as f32),
            Point::from_f32(rect.x as f32 + rect.width as f32, rect.y as f32 + rect.height as f32),
            light_color,
        );
        context.draw_line(
            Point::from_f32(rect.x as f32 + rect.width as f32, rect.y as f32),
            Point::from_f32(rect.x as f32 + rect.width as f32, rect.y as f32 + rect.height as f32),
            light_color,
        );
        // Inner border (raised)
        let inner_rect = Rect::new(
            rect.x + 1,
            rect.y + 1,
            rect.width.saturating_sub(2),
            rect.height.saturating_sub(2),
        );
        context.draw_line(
            Point::new(inner_rect.x, inner_rect.y),
            Point::new(inner_rect.x + inner_rect.width as i32, inner_rect.y),
            light_color,
        );
        context.draw_line(
            Point::new(inner_rect.x, inner_rect.y),
            Point::new(inner_rect.x, inner_rect.y + inner_rect.height as i32),
            light_color,
        );
        context.draw_line(
            Point::new(inner_rect.x, inner_rect.y + inner_rect.height as i32),
            Point::new(
                inner_rect.x + inner_rect.width as i32,
                inner_rect.y + inner_rect.height as i32,
            ),
            dark_color,
        );
        context.draw_line(
            Point::new(inner_rect.x + inner_rect.width as i32, inner_rect.y),
            Point::new(
                inner_rect.x + inner_rect.width as i32,
                inner_rect.y + inner_rect.height as i32,
            ),
            dark_color,
        );
    }
}
// Implement Widget trait
impl Widget for Frame {
    fn base(&self) -> &BaseWidget {
        &self.base
    }

    fn base_mut(&mut self) -> &mut BaseWidget {
        &mut self.base
    }

    fn size_hint(&self) -> Size {
        Size::new(200, 200)
    }
    impl_draw_bridge!();
    impl_widget_property_hooks!();
}

/// `Frame`'s property contract.
///
/// # Why this is not empty
///
/// The old dispatch grouped `WidgetKind::Panel | WidgetKind::Frame` into the
/// breadcrumb arm, which only ever matched `Breadcrumb` instances and answered
/// `UnsupportedOnWidget` for a `Frame`. Declaring nothing was honest about *that*
/// bug but wrong about the control: `Frame` has four real, independently settable
/// fields (`frame_shape`, `frame_shadow`, `line_width`, `mid_line_width`), and back
/// when it had no capability the factory could not construct one at all —
/// `factory_name_for_kind(WidgetKind::Frame)` returned `""`, so
/// `create_frame(..)` silently produced id `0`.
///
/// The names match `FRAME_PROPERTIES` one for one; a token that appears in the
/// schema but not in the `set` match below is a property that answers
/// `UnknownProperty` on write, which
/// `published_enum_tokens_are_accepted_by_their_control` rejects.
impl WidgetProperties for Frame {
    fn get(&self, name: &str) -> Result<CapabilityValue, CapabilityAccessError> {
        match name {
            "frame_shape" => Ok(CapabilityValue::String(self.frame_shape.as_str().to_string())),
            "frame_shadow" => Ok(CapabilityValue::String(self.frame_shadow.as_str().to_string())),
            "line_width" => Ok(CapabilityValue::Float(self.line_width as f64)),
            "mid_line_width" => Ok(CapabilityValue::Float(self.mid_line_width as f64)),
            _ => base_property_get(self, name),
        }
    }

    fn set(&mut self, name: &str, value: CapabilityValue) -> Result<(), CapabilityAccessError> {
        match name {
            "frame_shape" => {
                let text = expect_string(value)?;
                let Some(shape) = FrameShape::from_name(&text) else {
                    return Err(CapabilityAccessError::TypeMismatch);
                };
                self.set_frame_shape(shape);
                Ok(())
            }
            "frame_shadow" => {
                let text = expect_string(value)?;
                let Some(shadow) = FrameShadow::from_name(&text) else {
                    return Err(CapabilityAccessError::TypeMismatch);
                };
                self.set_frame_shadow(shadow);
                Ok(())
            }
            "line_width" => {
                self.set_line_width(expect_f32(value)?);
                Ok(())
            }
            "mid_line_width" => {
                self.set_mid_line_width(expect_f32(value)?);
                Ok(())
            }
            _ => base_property_set(self, name, value),
        }
    }

    fn property_names(&self) -> &'static [&'static str] {
        // Mirrors `FRAME_PROPERTIES`.
        property_names_of![
            "frame_shape",
            "frame_shadow",
            "line_width",
            "mid_line_width",
            BASE_PROPERTY_NAMES
        ]
    }
}

impl EventHandler for Frame {
    fn handle_event(&mut self, event: &Event) {
        self.base.handle_event(event);
        // Skip event forwarding when disabled
        if !self.base.is_enabled() {
            return;
        }
        // Forward events to widget via registry
        if let Some(widget_id) = self.widget {
            if let Some(ref reg) = self.registry {
                reg.borrow_mut().forward_event(widget_id, event);
            }
        }
    }
}
impl Draw for Frame {
    fn draw(&mut self, context: &mut RenderContext) {
        // Draw frame
        self.draw_frame(context);
        // Draw widget via registry
        if let Some(widget_id) = self.widget {
            if let Some(ref reg) = self.registry {
                reg.borrow_mut().draw_widget(widget_id, context);
            }
        }
        // Dim content when disabled
        if !self.base.is_enabled() {
            let rect = self.geometry();
            context.fill_rect(rect, Color::rgba(128, 128, 128, 80));
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::compat::{MiniToString, String};
    use crate::core::{Color, ObjectId, Rect};
    use crate::style::WidgetStyle;

    #[test]
    fn frame_creation_defaults() {
        let f = Frame::new(Rect::new(0, 0, 200, 100));
        assert_eq!(f.frame_shape(), FrameShape::Box);
        assert_eq!(f.frame_shadow(), FrameShadow::Plain);
        assert!((f.line_width() - 1.0).abs() < f32::EPSILON);
        assert!((f.mid_line_width() - 0.0).abs() < f32::EPSILON);
        assert!(f.widget().is_none());
        assert!(f.registry().is_none());
    }

    #[test]
    fn frame_shape_roundtrip() {
        let mut f = Frame::new(Rect::new(0, 0, 200, 100));
        f.set_frame_shape(FrameShape::Panel);
        assert_eq!(f.frame_shape(), FrameShape::Panel);
        f.set_frame_shape(FrameShape::NoFrame);
        assert_eq!(f.frame_shape(), FrameShape::NoFrame);
        f.set_frame_shape(FrameShape::StyledPanel);
        assert_eq!(f.frame_shape(), FrameShape::StyledPanel);
        f.set_frame_shape(FrameShape::HLine);
        assert_eq!(f.frame_shape(), FrameShape::HLine);
        f.set_frame_shape(FrameShape::VLine);
        assert_eq!(f.frame_shape(), FrameShape::VLine);
        f.set_frame_shape(FrameShape::WinPanel);
        assert_eq!(f.frame_shape(), FrameShape::WinPanel);
        f.set_frame_shape(FrameShape::Box);
        assert_eq!(f.frame_shape(), FrameShape::Box);
    }

    #[test]
    fn frame_shadow_roundtrip() {
        let mut f = Frame::new(Rect::new(0, 0, 200, 100));
        f.set_frame_shadow(FrameShadow::Raised);
        assert_eq!(f.frame_shadow(), FrameShadow::Raised);
        f.set_frame_shadow(FrameShadow::Sunken);
        assert_eq!(f.frame_shadow(), FrameShadow::Sunken);
        f.set_frame_shadow(FrameShadow::Plain);
        assert_eq!(f.frame_shadow(), FrameShadow::Plain);
    }

    #[test]
    fn frame_line_width_roundtrip() {
        let mut f = Frame::new(Rect::new(0, 0, 200, 100));
        f.set_line_width(3.0);
        assert!((f.line_width() - 3.0).abs() < f32::EPSILON);
        f.set_line_width(0.5);
        assert!((f.line_width() - 0.5).abs() < f32::EPSILON);
    }

    #[test]
    fn frame_mid_line_width_roundtrip() {
        let mut f = Frame::new(Rect::new(0, 0, 200, 100));
        f.set_mid_line_width(2.0);
        assert!((f.mid_line_width() - 2.0).abs() < f32::EPSILON);
        f.set_mid_line_width(0.0);
        assert!((f.mid_line_width() - 0.0).abs() < f32::EPSILON);
    }

    #[test]
    fn frame_widget_roundtrip() {
        let mut f = Frame::new(Rect::new(0, 0, 200, 100));
        assert!(f.widget().is_none());
        let wid: ObjectId = 42;
        f.set_widget(Some(wid));
        assert_eq!(f.widget(), Some(wid));
        f.set_widget(None);
        assert!(f.widget().is_none());
    }

    #[test]
    fn frame_geometry_delegation() {
        let mut f = Frame::new(Rect::new(0, 0, 200, 100));
        f.set_geometry(Rect::new(10, 20, 300, 150));
        assert_eq!(f.geometry(), Rect::new(10, 20, 300, 150));
    }

    #[test]
    fn frame_visibility() {
        let mut f = Frame::new(Rect::new(0, 0, 200, 100));
        assert!(f.is_visible());
        f.hide();
        assert!(!f.is_visible());
        f.show();
        assert!(f.is_visible());
    }

    #[test]
    fn frame_enabled() {
        let mut f = Frame::new(Rect::new(0, 0, 200, 100));
        assert!(f.is_enabled());
        f.set_enabled(false);
        assert!(!f.is_enabled());
        f.set_enabled(true);
        assert!(f.is_enabled());
    }

    #[test]
    fn frame_parent_children() {
        let mut f = Frame::new(Rect::new(0, 0, 200, 100));
        assert!(f.parent().is_none());
        let pid: ObjectId = 10;
        f.set_parent(Some(pid));
        assert_eq!(f.parent(), Some(pid));
        f.set_parent(None);
        assert!(f.parent().is_none());

        let cid: ObjectId = 20;
        f.add_child(cid);
        assert_eq!(f.children().len(), 1);
        assert_eq!(f.children()[0], cid);
        f.remove_child(cid);
        assert!(f.children().is_empty());
    }

    #[test]
    fn frame_tooltip_roundtrip() {
        let mut f = Frame::new(Rect::new(0, 0, 200, 100));
        assert!(f.tooltip().is_empty());
        f.set_tooltip("Frame info".to_string());
        assert_eq!(f.tooltip(), "Frame info");
        f.set_tooltip(String::new());
        assert!(f.tooltip().is_empty());
    }

    #[test]
    fn frame_style_roundtrip() {
        let mut f = Frame::new(Rect::new(0, 0, 200, 100));
        assert_eq!(*f.style(), WidgetStyle::default());
        let custom = WidgetStyle::default().with_background(Color::rgb(240, 240, 240));
        f.set_style(custom.clone());
        assert_eq!(*f.style(), custom);
    }

    #[test]
    fn frame_id_kind() {
        let f_a = Frame::new(Rect::new(0, 0, 100, 50));
        let f_b = Frame::new(Rect::new(0, 0, 100, 50));
        assert_ne!(f_a.id(), f_b.id());
        assert_eq!(f_a.kind(), WidgetKind::Frame);
        assert_eq!(f_b.kind(), WidgetKind::Frame);
    }

    #[test]
    fn frame_signal_accessors() {
        let f = Frame::new(Rect::new(0, 0, 100, 50));
        let _hover = f.base().hover_signal();
        let _mouse_down = f.base().mouse_down_signal();
        let _mouse_up = f.base().mouse_up_signal();
    }
}