Skip to content

Commit 422505b

Browse files
committed
reformat
fix clippy suggestions
1 parent 78d4902 commit 422505b

File tree

4 files changed

+58
-43
lines changed

4 files changed

+58
-43
lines changed

druid/examples/menu.rs

Lines changed: 40 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@
2020

2121
use druid::widget::prelude::*;
2222
use druid::widget::{Flex, Label};
23-
use druid::{AppDelegate, AppLauncher, Command, Data, DelegateCtx, Handled, Lens, Menu, MenuItem, Selector, Target, WidgetExt, WindowDesc};
23+
use druid::{
24+
AppDelegate, AppLauncher, Command, Data, DelegateCtx, Handled, Lens, Menu, MenuItem, Selector,
25+
Target, WidgetExt, WindowDesc,
26+
};
2427

2528
const COMMAND: Selector = Selector::new("custom_Selector");
2629

@@ -31,13 +34,11 @@ struct AppState {
3134
}
3235

3336
pub fn main() {
34-
35-
3637
// describe the main window
3738
let main_window = WindowDesc::new(build_root_widget())
3839
.title("Hello World!")
3940
.window_size((400.0, 400.0))
40-
.menu(|_, _, _|build_menu());
41+
.menu(|_, _, _| build_menu());
4142

4243
// create the initial app state
4344
let initial_state: AppState = AppState {
@@ -55,75 +56,86 @@ pub fn main() {
5556

5657
fn build_root_widget() -> impl Widget<AppState> {
5758
Flex::column()
58-
.with_child(Label::new(|data: &AppState, _: &_|format!("Current value: {}", data.value)))
59+
.with_child(Label::new(|data: &AppState, _: &_| {
60+
format!("Current value: {}", data.value)
61+
}))
5962
.with_default_spacer()
60-
.with_child(Label::new(|data: &AppState, _: &_|format!("IS selected: {}", data.option)))
63+
.with_child(Label::new(|data: &AppState, _: &_| {
64+
format!("IS selected: {}", data.option)
65+
}))
6166
.center()
6267
}
6368

6469
fn build_menu() -> Menu<AppState> {
6570
let menu = Menu::new("Druid Menu")
66-
.entry(
67-
MenuItem::new("Send Command")
68-
.command(COMMAND)
69-
)
71+
.entry(MenuItem::new("Send Command").command(COMMAND))
7072
.separator()
7173
.entry(
7274
MenuItem::new("Change value")
73-
.on_activate(|_, data: &mut AppState, _|data.value = (data.value + 1) % 4)
75+
.on_activate(|_, data: &mut AppState, _| data.value = (data.value + 1) % 4),
7476
)
7577
.entry(
7678
MenuItem::new("1 Selected")
7779
.radio_item(1, Some(0))
78-
.lens(AppState::value)
80+
.lens(AppState::value),
7981
)
8082
.entry(
8183
MenuItem::new("2 Selected")
8284
.radio_item(2, Some(0))
83-
.lens(AppState::value)
85+
.lens(AppState::value),
8486
)
8587
.entry(
8688
// Implementing the radio item from hand
8789
MenuItem::new("3 Selected")
88-
.on_activate(|_, data: &mut AppState, _|if data.value == 3 {data.value = 0} else {data.value = 3})
89-
.selected_if(|data: &AppState, _|data.value == 3)
90+
.on_activate(|_, data: &mut AppState, _| {
91+
if data.value == 3 {
92+
data.value = 0
93+
} else {
94+
data.value = 3
95+
}
96+
})
97+
.selected_if(|data: &AppState, _| data.value == 3),
9098
)
9199
.separator()
92100
.entry(
93101
MenuItem::new("CheckBox")
94102
.toggle_data()
95-
.lens(AppState::option)
103+
.lens(AppState::option),
96104
)
97105
.entry(
98106
// Implementing the CheckBox from hand
99107
MenuItem::new("Manual CheckBox")
100-
.on_activate(|_, data: &mut AppState, _|data.option = !data.option)
101-
.selected_if(|data: &AppState, _|data.option)
108+
.on_activate(|_, data: &mut AppState, _| data.option = !data.option)
109+
.selected_if(|data: &AppState, _| data.option),
102110
)
103111
.entry(
104112
MenuItem::new("Disabled")
105-
.on_activate(|_, _, _|panic!("disabled Menu Item was activated!"))
106-
.enabled(false)
107-
113+
.on_activate(|_, _, _| panic!("disabled Menu Item was activated!"))
114+
.enabled(false),
108115
)
109116
.entry(
110117
MenuItem::new("Disabled Selectable")
111-
.on_activate(|_, _, _|panic!("disabled Menu Item was activated!"))
118+
.on_activate(|_, _, _| panic!("disabled Menu Item was activated!"))
112119
.selected(false)
113-
.enabled(false)
120+
.enabled(false),
114121
)
115122
//we dont add new menu items based on data!
116-
.rebuild_on(|_, _, _|false);
117-
118-
Menu::empty()
119-
.entry(menu)
123+
.rebuild_on(|_, _, _| false);
120124

125+
Menu::empty().entry(menu)
121126
}
122127

123128
struct Delegate;
124129

125130
impl AppDelegate<AppState> for Delegate {
126-
fn command(&mut self, _: &mut DelegateCtx, _: Target, cmd: &Command, _: &mut AppState, _: &Env) -> Handled {
131+
fn command(
132+
&mut self,
133+
_: &mut DelegateCtx,
134+
_: Target,
135+
cmd: &Command,
136+
_: &mut AppState,
137+
_: &Env,
138+
) -> Handled {
127139
if cmd.is(COMMAND) {
128140
println!("Clicked \"Send Command\"!");
129141
Handled::Yes

druid/examples/web/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ impl_example!(invalidation);
7070
impl_example!(layout);
7171
impl_example!(lens);
7272
impl_example!(list);
73+
impl_example!(menu);
7374
impl_example!(multiwin);
7475
impl_example!(open_save);
7576
impl_example!(panels.unwrap());
@@ -86,3 +87,4 @@ impl_example!(transparency);
8687
impl_example!(view_switcher);
8788
impl_example!(widget_gallery);
8889
impl_example!(text);
90+
impl_example!(z_stack);

druid/examples/widget_gallery.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ impl<T: Data> Widget<T> for SquaresGrid<T> {
333333
// The space needed to lay all elements out on a single line.
334334
let ideal_width = (self.cell_size.width + self.spacing + 1.0) * count;
335335
// Constrain the width.
336-
let width = ideal_width.min(bc.max().width).max(bc.min().width);
336+
let width = ideal_width.clamp(bc.min().width, bc.max().width);
337337
// Given the width, the space needed to lay out all elements (as many as possible on each
338338
// line).
339339
let cells_in_row =
@@ -345,7 +345,7 @@ impl<T: Data> Widget<T> for SquaresGrid<T> {
345345
let ideal_height = height_from_rows(rows);
346346

347347
// Constrain the height
348-
let height = ideal_height.max(bc.min().height).min(bc.max().height);
348+
let height = ideal_height.clamp(bc.min().height, bc.max().height);
349349
// Now calculate how many rows we can actually fit in
350350
while height_from_rows(rows) > height && rows > 0 {
351351
rows -= 1;

druid/src/menu/mod.rs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -647,17 +647,19 @@ impl<T: Data> MenuItem<T> {
647647
///
648648
/// When selected this MenuItem will set the provided value as data.
649649
/// If data is equal to the provided value the Item is selected otherwise not.
650-
pub fn radio_item(self, value: T, unselect: Option<T>) -> Self where T: PartialEq {
650+
pub fn radio_item(self, value: T, unselect: Option<T>) -> Self
651+
where
652+
T: PartialEq,
653+
{
651654
let value2 = value.clone();
652-
self
653-
.on_activate(move |_, data: &mut T, _|{
654-
if data != value {
655-
*data = value.clone();
656-
} else if let Some(value) = unselect.clone() {
657-
*data = value;
658-
}
659-
})
660-
.selected_if(move |data, _|data == value2)
655+
self.on_activate(move |_, data: &mut T, _| {
656+
if *data != value {
657+
*data = value.clone();
658+
} else if let Some(value) = unselect.clone() {
659+
*data = value;
660+
}
661+
})
662+
.selected_if(move |data, _| *data == value2)
661663
}
662664

663665
/// Provide a hotkey for activating this menu item.
@@ -747,9 +749,8 @@ impl MenuItem<bool> {
747749
/// this is a convenience method which sets the `on_activate` and `selected_if` callbacks
748750
/// to behave like a `CheckBox`.
749751
pub fn toggle_data(self) -> Self {
750-
self
751-
.on_activate(|_, data, _|*data = !*data)
752-
.selected_if(|data, _|*data)
752+
self.on_activate(|_, data, _| *data = !*data)
753+
.selected_if(|data, _| *data)
753754
}
754755
}
755756

0 commit comments

Comments
 (0)