Skip to content

Commit

Permalink
Merge pull request #1437 from JAicewizard/rework_flex
Browse files Browse the repository at this point in the history
slight cleanup of the flex showcase
  • Loading branch information
richard-uk1 authored Nov 30, 2020
2 parents 8b0e5c9 + c0f195c commit 60ea802
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 44 deletions.
83 changes: 40 additions & 43 deletions druid/examples/flex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,40 @@
// limitations under the License.

//! Demonstrates alignment of children in the flex container.
//! This example showcases the full set of functionality of flex, giving you
//! knobs to change all the parameters. 99% of the time you will want to
//! hard-code these parameters, which will simplify your code considerably.
use druid::widget::prelude::*;
use druid::widget::{
Button, Checkbox, CrossAxisAlignment, Flex, Label, MainAxisAlignment, ProgressBar, RadioGroup,
SizedBox, Slider, Stepper, Switch, TextBox, WidgetExt,
};
use druid::{
AppLauncher, Color, Data, Lens, LensExt, LocalizedString, PlatformError, WidgetId, WindowDesc,
};
use druid::{AppLauncher, Color, Data, Lens, LensExt, WindowDesc};

const DEFAULT_SPACER_SIZE: f64 = 8.;
const SPACER_OPTIONS: [(&str, Spacers); 4] = [
("None", Spacers::None),
("Default", Spacers::Default),
("Flex", Spacers::Flex),
("Fixed:", Spacers::Fixed),
];
const MAIN_AXIS_ALIGNMENT_OPTIONS: [(&str, MainAxisAlignment); 6] = [
("Start", MainAxisAlignment::Start),
("Center", MainAxisAlignment::Center),
("End", MainAxisAlignment::End),
("Between", MainAxisAlignment::SpaceBetween),
("Evenly", MainAxisAlignment::SpaceEvenly),
("Around", MainAxisAlignment::SpaceAround),
];
const CROSS_AXIS_ALIGNMENT_OPTIONS: [(&str, CrossAxisAlignment); 4] = [
("Start", CrossAxisAlignment::Start),
("Center", CrossAxisAlignment::Center),
("End", CrossAxisAlignment::End),
("Baseline", CrossAxisAlignment::Baseline),
];
const FLEX_TYPE_OPTIONS: [(&str, FlexType); 2] =
[("Row", FlexType::Row), ("Column", FlexType::Column)];

#[derive(Clone, Data, Lens)]
struct AppState {
Expand Down Expand Up @@ -130,10 +153,7 @@ fn make_control_row() -> impl Widget<AppState> {
.cross_axis_alignment(CrossAxisAlignment::Start)
.with_child(Label::new("Type:"))
.with_default_spacer()
.with_child(
RadioGroup::new(vec![("Row", FlexType::Row), ("Column", FlexType::Column)])
.lens(Params::axis),
),
.with_child(RadioGroup::new(FLEX_TYPE_OPTIONS.to_vec()).lens(Params::axis)),
)
.with_default_spacer()
.with_child(
Expand All @@ -142,13 +162,8 @@ fn make_control_row() -> impl Widget<AppState> {
.with_child(Label::new("CrossAxis:"))
.with_default_spacer()
.with_child(
RadioGroup::new(vec![
("Start", CrossAxisAlignment::Start),
("Center", CrossAxisAlignment::Center),
("End", CrossAxisAlignment::End),
("Baseline", CrossAxisAlignment::Baseline),
])
.lens(Params::cross_alignment),
RadioGroup::new(CROSS_AXIS_ALIGNMENT_OPTIONS.to_vec())
.lens(Params::cross_alignment),
),
)
.with_default_spacer()
Expand All @@ -158,15 +173,8 @@ fn make_control_row() -> impl Widget<AppState> {
.with_child(Label::new("MainAxis:"))
.with_default_spacer()
.with_child(
RadioGroup::new(vec![
("Start", MainAxisAlignment::Start),
("Center", MainAxisAlignment::Center),
("End", MainAxisAlignment::End),
("Between", MainAxisAlignment::SpaceBetween),
("Evenly", MainAxisAlignment::SpaceEvenly),
("Around", MainAxisAlignment::SpaceAround),
])
.lens(Params::main_alignment),
RadioGroup::new(MAIN_AXIS_ALIGNMENT_OPTIONS.to_vec())
.lens(Params::main_alignment),
),
)
.with_default_spacer()
Expand Down Expand Up @@ -196,15 +204,7 @@ fn make_spacer_select() -> impl Widget<Params> {
.cross_axis_alignment(CrossAxisAlignment::Start)
.with_child(Label::new("Insert Spacers:"))
.with_default_spacer()
.with_child(
RadioGroup::new(vec![
("None", Spacers::None),
("Default", Spacers::Default),
("Flex", Spacers::Flex),
("Fixed:", Spacers::Fixed),
])
.lens(Params::spacers),
)
.with_child(RadioGroup::new(SPACER_OPTIONS.to_vec()).lens(Params::spacers))
.with_default_spacer()
.with_child(
Flex::row()
Expand Down Expand Up @@ -237,15 +237,15 @@ fn space_if_needed<T: Data>(flex: &mut Flex<T>, params: &Params) {
}

fn build_widget(state: &Params) -> Box<dyn Widget<AppState>> {
let flex = match state.axis {
let mut flex = match state.axis {
FlexType::Column => Flex::column(),
FlexType::Row => Flex::row(),
}
.cross_axis_alignment(state.cross_alignment)
.main_axis_alignment(state.main_alignment)
.must_fill_main_axis(state.fill_major_axis);

let mut flex = flex.with_child(
flex.add_child(
TextBox::new()
.with_placeholder("Sample text")
.lens(DemoState::input_text),
Expand Down Expand Up @@ -289,7 +289,6 @@ fn build_widget(state: &Params) -> Box<dyn Widget<AppState>> {
FlexType::Column => flex = flex.width(200.),
}
}

if state.fix_major_axis {
match state.axis {
FlexType::Row => flex = flex.width(600.),
Expand Down Expand Up @@ -319,11 +318,11 @@ fn make_ui() -> impl Widget<AppState> {
.padding(10.0)
}

pub fn main() -> Result<(), PlatformError> {
pub fn main() {
let main_window = WindowDesc::new(make_ui)
.window_size((720., 600.00))
.with_min_size((620., 265.00))
.title(LocalizedString::new("Flex Container Options"));
.window_size((720., 600.))
.with_min_size((620., 300.))
.title("Flex Container Options");

let demo_state = DemoState {
input_text: "hello".into(),
Expand All @@ -343,10 +342,8 @@ pub fn main() -> Result<(), PlatformError> {
fill_major_axis: false,
};

let data = AppState { demo_state, params };

AppLauncher::with_window(main_window)
.use_simple_logger()
.launch(data)?;
Ok(())
.launch(AppState { demo_state, params })
.expect("Failed to launch application");
}
2 changes: 1 addition & 1 deletion druid/examples/web/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ impl_example!(cursor);
impl_example!(custom_widget);
impl_example!(either);
impl_example!(event_viewer);
impl_example!(flex.unwrap());
impl_example!(flex);
impl_example!(game_of_life);
impl_example!(hello);
impl_example!(identity);
Expand Down

0 comments on commit 60ea802

Please sign in to comment.