Skip to content

Commit aa4b366

Browse files
Implement RadioGroup::{row,column,for_axis} to allow layout customization (#2157)
1 parent 737849a commit aa4b366

File tree

7 files changed

+36
-15
lines changed

7 files changed

+36
-15
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ You can find its changes [documented below](#070---2021-01-01).
9999
- Dont warn about unhandled `Notification`s which have `known_target` set to false ([#2141] by [@xarvic])
100100
- `ClipBox`, `Flex`, `List` and `Split` only call layout on children which need it ([#2145] by [@xarvic])
101101
- `SizedBox` now supports using `Key<f64>` for specifying size ([#2151] by [@GoldsteinE])
102+
- `RadioGroup` widgets are now constructed with new `row()`, `column()`, and `for_axis()` methods ([#2157] by [@twitchyliquid64])
102103

103104
### Deprecated
104105

@@ -551,6 +552,7 @@ Last release without a changelog :(
551552
[@maurerdietmar]: https://github.com/maurerdietmar
552553
[@superfell]: https://github.com/superfell
553554
[@GoldsteinE]: https://github.com/GoldsteinE
555+
[@twitchyliquid64]: https://github.com/twitchyliquid64
554556

555557
[#599]: https://github.com/linebender/druid/pull/599
556558
[#611]: https://github.com/linebender/druid/pull/611
@@ -841,6 +843,7 @@ Last release without a changelog :(
841843
[#2145]: https://github.com/linebender/druid/pull/2145
842844
[#2148]: https://github.com/linebender/druid/pull/2148
843845
[#2151]: https://github.com/linebender/druid/pull/2151
846+
[#2157]: https://github.com/linebender/druid/pull/2157
844847
[#2158]: https://github.com/linebender/druid/pull/2158
845848

846849
[Unreleased]: https://github.com/linebender/druid/compare/v0.7.0...master

druid/examples/flex.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ fn make_control_row() -> impl Widget<AppState> {
158158
.cross_axis_alignment(CrossAxisAlignment::Start)
159159
.with_child(Label::new("Type:"))
160160
.with_default_spacer()
161-
.with_child(RadioGroup::new(FLEX_TYPE_OPTIONS.to_vec()).lens(Params::axis)),
161+
.with_child(RadioGroup::column(FLEX_TYPE_OPTIONS.to_vec()).lens(Params::axis)),
162162
)
163163
.with_default_spacer()
164164
.with_child(
@@ -167,7 +167,7 @@ fn make_control_row() -> impl Widget<AppState> {
167167
.with_child(Label::new("CrossAxis:"))
168168
.with_default_spacer()
169169
.with_child(
170-
RadioGroup::new(CROSS_AXIS_ALIGNMENT_OPTIONS.to_vec())
170+
RadioGroup::column(CROSS_AXIS_ALIGNMENT_OPTIONS.to_vec())
171171
.lens(Params::cross_alignment),
172172
),
173173
)
@@ -178,7 +178,7 @@ fn make_control_row() -> impl Widget<AppState> {
178178
.with_child(Label::new("MainAxis:"))
179179
.with_default_spacer()
180180
.with_child(
181-
RadioGroup::new(MAIN_AXIS_ALIGNMENT_OPTIONS.to_vec())
181+
RadioGroup::column(MAIN_AXIS_ALIGNMENT_OPTIONS.to_vec())
182182
.lens(Params::main_alignment),
183183
),
184184
)
@@ -209,7 +209,7 @@ fn make_spacer_select() -> impl Widget<Params> {
209209
.cross_axis_alignment(CrossAxisAlignment::Start)
210210
.with_child(Label::new("Insert Spacers:"))
211211
.with_default_spacer()
212-
.with_child(RadioGroup::new(SPACER_OPTIONS.to_vec()).lens(Params::spacers))
212+
.with_child(RadioGroup::column(SPACER_OPTIONS.to_vec()).lens(Params::spacers))
213213
.with_default_spacer()
214214
.with_child(
215215
Flex::row()

druid/examples/image.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ fn make_control_row() -> impl Widget<AppState> {
116116
.with_child(Label::new("FillStrat:"))
117117
.with_default_spacer()
118118
.with_child(
119-
RadioGroup::new(FILL_STRAT_OPTIONS.to_vec()).lens(AppState::fill_strat),
119+
RadioGroup::column(FILL_STRAT_OPTIONS.to_vec()).lens(AppState::fill_strat),
120120
),
121121
)
122122
.with_default_spacer()
@@ -126,7 +126,7 @@ fn make_control_row() -> impl Widget<AppState> {
126126
.with_child(Label::new("interpolation mode:"))
127127
.with_default_spacer()
128128
.with_child(
129-
RadioGroup::new(INTERPOLATION_MODE_OPTIONS.to_vec())
129+
RadioGroup::column(INTERPOLATION_MODE_OPTIONS.to_vec())
130130
.lens(AppState::interpolation_mode),
131131
),
132132
)

druid/examples/tabs.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ fn build_root_widget() -> impl Widget<AppState> {
114114

115115
let axis_picker = group(
116116
"Tab bar axis",
117-
RadioGroup::new(vec![
117+
RadioGroup::column(vec![
118118
("Horizontal", Axis::Horizontal),
119119
("Vertical", Axis::Vertical),
120120
])
@@ -123,7 +123,7 @@ fn build_root_widget() -> impl Widget<AppState> {
123123

124124
let cross_picker = group(
125125
"Tab bar edge",
126-
RadioGroup::new(vec![
126+
RadioGroup::column(vec![
127127
("Leading", TabsEdge::Leading),
128128
("Trailing", TabsEdge::Trailing),
129129
])
@@ -132,7 +132,7 @@ fn build_root_widget() -> impl Widget<AppState> {
132132

133133
let transit_picker = group(
134134
"Transition",
135-
RadioGroup::new(vec![
135+
RadioGroup::column(vec![
136136
("Instant", TabsTransition::Instant),
137137
(
138138
"Slide",

druid/examples/text.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ fn build_root_widget() -> impl Widget<AppState> {
123123
let line_break_chooser = Flex::column()
124124
.with_child(Label::new("Line break mode"))
125125
.with_spacer(SPACER_SIZE)
126-
.with_child(RadioGroup::new(vec![
126+
.with_child(RadioGroup::column(vec![
127127
("Clip", LineBreaking::Clip),
128128
("Wrap", LineBreaking::WordWrap),
129129
("Overflow", LineBreaking::Overflow),
@@ -133,7 +133,7 @@ fn build_root_widget() -> impl Widget<AppState> {
133133
let alignment_picker = Flex::column()
134134
.with_child(Label::new("Justification"))
135135
.with_spacer(SPACER_SIZE)
136-
.with_child(RadioGroup::new(vec![
136+
.with_child(RadioGroup::column(vec![
137137
("Start", TextAlignment::Start),
138138
("End", TextAlignment::End),
139139
("Center", TextAlignment::Center),

druid/examples/widget_gallery.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ fn ui_builder() -> impl Widget<AppData> {
145145
"Painter",
146146
))
147147
.with_child(label_widget(
148-
RadioGroup::new(vec![
148+
RadioGroup::column(vec![
149149
("radio gaga", MyRadio::GaGa),
150150
("radio gugu", MyRadio::GuGu),
151151
("radio baabaa", MyRadio::BaaBaa),

druid/src/widget/radio.rs

+21-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
use crate::debug_state::DebugState;
1818
use crate::kurbo::Circle;
1919
use crate::widget::prelude::*;
20-
use crate::widget::{CrossAxisAlignment, Flex, Label, LabelText};
20+
use crate::widget::{Axis, CrossAxisAlignment, Flex, Label, LabelText};
2121
use crate::{theme, Data, LinearGradient, UnitPoint};
2222
use tracing::{instrument, trace};
2323

@@ -29,10 +29,28 @@ pub struct RadioGroup;
2929

3030
impl RadioGroup {
3131
/// Given a vector of `(label_text, enum_variant)` tuples, create a group of Radio buttons
32-
pub fn new<T: Data + PartialEq>(
32+
/// along the vertical axis.
33+
pub fn column<T: Data + PartialEq>(
3334
variants: impl IntoIterator<Item = (impl Into<LabelText<T>> + 'static, T)>,
3435
) -> impl Widget<T> {
35-
let mut col = Flex::column().cross_axis_alignment(CrossAxisAlignment::Start);
36+
RadioGroup::for_axis(Axis::Vertical, variants)
37+
}
38+
39+
/// Given a vector of `(label_text, enum_variant)` tuples, create a group of Radio buttons
40+
/// along the horizontal axis.
41+
pub fn row<T: Data + PartialEq>(
42+
variants: impl IntoIterator<Item = (impl Into<LabelText<T>> + 'static, T)>,
43+
) -> impl Widget<T> {
44+
RadioGroup::for_axis(Axis::Horizontal, variants)
45+
}
46+
47+
/// Given a vector of `(label_text, enum_variant)` tuples, create a group of Radio buttons
48+
/// along the specified axis.
49+
pub fn for_axis<T: Data + PartialEq>(
50+
axis: Axis,
51+
variants: impl IntoIterator<Item = (impl Into<LabelText<T>> + 'static, T)>,
52+
) -> impl Widget<T> {
53+
let mut col = Flex::for_axis(axis).cross_axis_alignment(CrossAxisAlignment::Start);
3654
let mut is_first = true;
3755
for (label, variant) in variants.into_iter() {
3856
if !is_first {

0 commit comments

Comments
 (0)