diff options
author | 2021-10-20 19:07:09 +0700 | |
---|---|---|
committer | 2021-10-20 19:19:13 +0700 | |
commit | 47c8f6ceee801bd2896cd075097dcb075ff070d7 (patch) | |
tree | 4d4aa3baaef8062460cfb5f84232d2893f2f6863 /native/src/widget | |
parent | d39ad717ed0ab85acbe935d7ab883166b36e7bc7 (diff) | |
download | iced-47c8f6ceee801bd2896cd075097dcb075ff070d7.tar.gz iced-47c8f6ceee801bd2896cd075097dcb075ff070d7.tar.bz2 iced-47c8f6ceee801bd2896cd075097dcb075ff070d7.zip |
Implement `Widget::draw` for `Radio`
Diffstat (limited to 'native/src/widget')
-rw-r--r-- | native/src/widget/radio.rs | 63 |
1 files changed, 61 insertions, 2 deletions
diff --git a/native/src/widget/radio.rs b/native/src/widget/radio.rs index 0e1023b5..ebd14517 100644 --- a/native/src/widget/radio.rs +++ b/native/src/widget/radio.rs @@ -1,14 +1,16 @@ //! Create choices using radio buttons. use std::hash::Hash; +use crate::alignment; use crate::event::{self, Event}; use crate::layout; use crate::mouse; use crate::renderer; +use crate::text; use crate::touch; use crate::{ - Alignment, Clipboard, Color, Element, Hasher, Layout, Length, Point, - Rectangle, Row, Text, Widget, + Alignment, Background, Clipboard, Color, Element, Hasher, Layout, Length, + Point, Rectangle, Row, Text, Widget, }; pub use iced_style::radio::{Style, StyleSheet}; @@ -206,6 +208,63 @@ where cursor_position: Point, _viewport: &Rectangle, ) { + let bounds = layout.bounds(); + let is_mouse_over = bounds.contains(cursor_position); + + let mut children = layout.children(); + + { + let layout = children.next().unwrap(); + let bounds = layout.bounds(); + + let size = bounds.width; + let dot_size = size / 2.0; + + let style = if is_mouse_over { + self.style_sheet.hovered() + } else { + self.style_sheet.active() + }; + + renderer.fill_rectangle(renderer::Quad { + bounds, + background: style.background, + border_radius: size / 2.0, + border_width: style.border_width, + border_color: style.border_color, + }); + + if self.is_selected { + renderer.fill_rectangle(renderer::Quad { + bounds: Rectangle { + x: bounds.x + dot_size / 2.0, + y: bounds.y + dot_size / 2.0, + width: bounds.width - dot_size, + height: bounds.height - dot_size, + }, + background: Background::Color(style.dot_color), + border_radius: dot_size / 2.0, + border_width: 0.0, + border_color: Color::TRANSPARENT, + }); + } + } + + { + let label_layout = children.next().unwrap(); + + text::draw( + renderer, + style, + label_layout, + &self.label, + self.font, + self.text_size, + self.text_color, + alignment::Horizontal::Left, + alignment::Vertical::Center, + ); + } } fn hash_layout(&self, state: &mut Hasher) { |