Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RadioGroup size and padding #2400

Closed
wants to merge 20 commits into from
Closed
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
836522f
change
josefc12 May 2, 2024
63378b8
radio accepts Label (which you can style) as opposed to just text
josefc12 May 2, 2024
0f2a3fa
radio accepts Label (which you can style) as opposed to just text
josefc12 May 2, 2024
dc9911d
radio accepts Label (which you can style) as opposed to just text and…
josefc12 May 2, 2024
3e80383
radio accepts Label (which you can style) as opposed to just text and…
josefc12 May 2, 2024
91050fe
radio accepts Label (which you can style) as opposed to just text and…
josefc12 May 2, 2024
1d418ec
radio accepts Label (which you can style) as opposed to just text and…
josefc12 May 2, 2024
a62ab94
radio accepts Label (which you can style) as opposed to just text and…
josefc12 May 2, 2024
b2f83e8
radio accepts Label (which you can style) as opposed to just text and…
josefc12 May 2, 2024
30e7faa
radio accepts Label (which you can style) as opposed to just text and…
josefc12 May 2, 2024
fd729bf
radio accepts Label (which you can style) as opposed to just text and…
josefc12 May 2, 2024
a88404f
radio accepts Label (which you can style) as opposed to just text and…
josefc12 May 2, 2024
3a30c1d
radio accepts Label (which you can style) as opposed to just text and…
josefc12 May 2, 2024
611a80b
radio now accepts size and padding for the individual Radio of the gr…
josefc12 May 2, 2024
c1e4ee4
radio now accepts size and padding for the individual Radio of the gr…
josefc12 May 2, 2024
ebd4f59
radio now accepts size and padding for the individual Radio of the gr…
josefc12 May 2, 2024
b8e350b
radio now accepts size and padding for the individual Radio of the gr…
josefc12 May 2, 2024
314d80e
radio now accepts size and padding for the individual Radio of the gr…
josefc12 May 2, 2024
06aa110
radio now accepts size and padding for the individual Radio of the gr…
josefc12 May 3, 2024
9960b42
radio now accepts size and padding for the individual Radio of the gr…
josefc12 May 3, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 22 additions & 18 deletions druid/src/widget/radio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ use crate::widget::{Axis, CrossAxisAlignment, Flex, Label, LabelText};
use crate::{theme, Data, LinearGradient, UnitPoint};
use tracing::{instrument, trace};

const DEFAULT_RADIO_RADIUS: f64 = 7.0;
const INNER_CIRCLE_RADIUS: f64 = 2.0;
//const DEFAULT_RADIO_RADIUS: f64 = 7.0;
//const INNER_CIRCLE_RADIUS: f64 = 2.0;
/// A group of radio buttons
#[derive(Debug, Clone)]
pub struct RadioGroup;
Expand All @@ -31,15 +31,15 @@ impl RadioGroup {
/// Given a vector of `(label_text, enum_variant)` tuples, create a group of Radio buttons
/// along the vertical axis.
pub fn column<T: Data + PartialEq>(
variants: impl IntoIterator<Item = (impl Into<LabelText<T>> + 'static, T)>,
variants: impl IntoIterator<Item = (impl Into<LabelText<T>> + 'static, T, f64, f64)>,
) -> impl Widget<T> {
RadioGroup::for_axis(Axis::Vertical, variants)
}

/// Given a vector of `(label_text, enum_variant)` tuples, create a group of Radio buttons
/// along the horizontal axis.
pub fn row<T: Data + PartialEq>(
variants: impl IntoIterator<Item = (impl Into<LabelText<T>> + 'static, T)>,
variants: impl IntoIterator<Item = (impl Into<LabelText<T>> + 'static, T, f64, f64)>,
) -> impl Widget<T> {
RadioGroup::for_axis(Axis::Horizontal, variants)
}
Expand All @@ -48,15 +48,15 @@ impl RadioGroup {
/// along the specified axis.
pub fn for_axis<T: Data + PartialEq>(
axis: Axis,
variants: impl IntoIterator<Item = (impl Into<LabelText<T>> + 'static, T)>,
variants: impl IntoIterator<Item = (impl Into<LabelText<T>> + 'static, T, f64, f64)>,
) -> impl Widget<T> {
let mut col = Flex::for_axis(axis).cross_axis_alignment(CrossAxisAlignment::Start);
let mut is_first = true;
for (label, variant) in variants.into_iter() {
for (label, variant, size, padding) in variants.into_iter() {
if !is_first {
col.add_default_spacer();
}
let radio = Radio::new(label, variant);
let radio = Radio::new(label, variant, size, padding);
col.add_child(radio);
is_first = false;
}
Expand All @@ -68,14 +68,18 @@ impl RadioGroup {
pub struct Radio<T> {
variant: T,
child_label: Label<T>,
size: f64,
padding: f64
}

impl<T: Data> Radio<T> {
/// Create a lone Radio button from label text and an enum variant
pub fn new(label: impl Into<LabelText<T>>, variant: T) -> Radio<T> {
pub fn new(label: impl Into<LabelText<T>>, variant: T, size: f64, padding: f64) -> Radio<T> {
Radio {
variant,
child_label: Label::new(label),
child_label: Label::new(label).with_text_size(size),
size: size,
padding: padding
}
}
}
Expand Down Expand Up @@ -126,12 +130,12 @@ impl<T: Data + PartialEq> Widget<T> for Radio<T> {
bc.debug_check("Radio");

let label_size = self.child_label.layout(ctx, bc, data, env);
let radio_diam = env.get(theme::BASIC_WIDGET_HEIGHT);
let x_padding = env.get(theme::WIDGET_CONTROL_COMPONENT_PADDING);
let radio_height = self.size;//env.get(theme::BASIC_WIDGET_HEIGHT);
let x_padding = 3.;//env.get(theme::WIDGET_CONTROL_COMPONENT_PADDING);

let desired_size = Size::new(
label_size.width + radio_diam + x_padding,
radio_diam.max(label_size.height),
label_size.width + radio_height + x_padding,
radio_height//radio_diam.max(label_size.height),
);
let size = bc.constrain(desired_size);
trace!("Computed size: {}", size);
Expand All @@ -140,10 +144,10 @@ impl<T: Data + PartialEq> Widget<T> for Radio<T> {

#[instrument(name = "Radio", level = "trace", skip(self, ctx, data, env))]
fn paint(&mut self, ctx: &mut PaintCtx, data: &T, env: &Env) {
let size = env.get(theme::BASIC_WIDGET_HEIGHT);
let x_padding = env.get(theme::WIDGET_CONTROL_COMPONENT_PADDING);
let size = self.size;//env.get(theme::BASIC_WIDGET_HEIGHT);
let x_padding = self.padding;//env.get(theme::WIDGET_CONTROL_COMPONENT_PADDING);

let circle = Circle::new((size / 2., size / 2.), DEFAULT_RADIO_RADIUS);
let circle = Circle::new((size / 2., size / 2.), size/2.);

// Paint the background
let background_gradient = LinearGradient::new(
Expand All @@ -167,7 +171,7 @@ impl<T: Data + PartialEq> Widget<T> for Radio<T> {

// Check if data enum matches our variant
if *data == self.variant {
let inner_circle = Circle::new((size / 2., size / 2.), INNER_CIRCLE_RADIUS);
let inner_circle = Circle::new((size / 2., size / 2.), (size/4.).ceil());

let fill = if ctx.is_disabled() {
env.get(theme::DISABLED_TEXT_COLOR)
Expand All @@ -179,7 +183,7 @@ impl<T: Data + PartialEq> Widget<T> for Radio<T> {
}

// Paint the text label
self.child_label.draw_at(ctx, (size + x_padding, 0.0));
self.child_label.draw_at(ctx, (size + x_padding, (size/(size/2.0)*-1.0+(size/(size/2.0)/2.0).ceil())));
}

fn debug_state(&self, data: &T) -> DebugState {
Expand Down
Loading