Skip to content

Commit

Permalink
most views don't need closure
Browse files Browse the repository at this point in the history
  • Loading branch information
dzhou121 committed Sep 6, 2023
1 parent c02c55f commit f1c9842
Show file tree
Hide file tree
Showing 42 changed files with 455 additions and 626 deletions.
34 changes: 15 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,21 @@ fn app_view() -> impl View {
let (counter, set_counter) = create_signal(0);

// create user interface with Floem view functions
stack(|| {
(
label(move || format!("Value: {}", counter.get())),
stack(|| {
(
label(|| "Increment")
.on_click(move |_| {
set_counter.update(|value| *value += 1);
true
}),
label(|| "Decrement")
.on_click(move |_| {
set_counter.update(|value| *value -= 1);
true
}),
)
}),
)
})
stack((
label(move || format!("Value: {}", counter.get())),
stack((
text("Increment")
.on_click(move |_| {
set_counter.update(|value| *value += 1);
true
}),
text("Decrement")
.on_click(move |_| {
set_counter.update(|value| *value -= 1);
true
}),
)),
))
}

fn main() {
Expand Down
2 changes: 1 addition & 1 deletion examples/animations/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fn app_view() -> impl View {
let (counter, set_counter) = create_signal(0.0);
let (is_hovered, set_is_hovered) = create_signal(false);

stack(|| {
stack({
(label(|| "Hover or click me!")
.on_click(move |_| {
set_counter.update(|value| *value += 1.0);
Expand Down
124 changes: 60 additions & 64 deletions examples/counter/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,74 +2,70 @@ use floem::{
peniko::Color,
reactive::create_signal,
view::View,
views::{label, stack, Decorators},
views::{label, stack, text, Decorators},
};

fn app_view() -> impl View {
let (counter, set_counter) = create_signal(0);
stack(|| {
(
label(move || format!("Value: {}", counter.get())).style(|s| s.padding_px(10.0)),
stack(|| {
(
label(|| "Increment")
.style(|s| {
s.border_radius(10.0)
.padding_px(10.0)
.background(Color::WHITE)
.box_shadow_blur(5.0)
})
.on_click({
move |_| {
set_counter.update(|value| *value += 1);
true
}
})
.hover_style(|s| s.background(Color::LIGHT_GREEN))
.active_style(|s| s.color(Color::WHITE).background(Color::DARK_GREEN))
.keyboard_navigatable()
.focus_visible_style(|s| s.border_color(Color::BLUE).border(2.)),
label(|| "Decrement")
.on_click({
move |_| {
set_counter.update(|value| *value -= 1);
true
}
})
.style(|s| {
s.box_shadow_blur(5.0)
.background(Color::WHITE)
.border_radius(10.0)
.padding_px(10.0)
.margin_left_px(10.0)
})
.hover_style(|s| s.background(Color::rgb8(244, 67, 54)))
.active_style(|s| s.color(Color::WHITE).background(Color::RED))
.keyboard_navigatable()
.focus_visible_style(|s| s.border_color(Color::BLUE).border(2.)),
label(|| "Reset to 0")
.on_click(move |_| {
println!("Reset counter pressed"); // will not fire if button is disabled
set_counter.update(|value| *value = 0);
true
})
.disabled(move || counter.get() == 0)
.style(|s| {
s.box_shadow_blur(5.0)
.border_radius(10.0)
.padding_px(10.0)
.margin_left_px(10.0)
.background(Color::LIGHT_BLUE)
})
.disabled_style(|s| s.background(Color::LIGHT_GRAY))
.hover_style(|s| s.background(Color::LIGHT_YELLOW))
.active_style(|s| s.color(Color::WHITE).background(Color::YELLOW_GREEN))
.keyboard_navigatable()
.focus_visible_style(|s| s.border_color(Color::BLUE).border(2.)),
)
}),
)
})
stack((
label(move || format!("Value: {}", counter.get())).style(|s| s.padding_px(10.0)),
stack((
text("Increment")
.style(|s| {
s.border_radius(10.0)
.padding_px(10.0)
.background(Color::WHITE)
.box_shadow_blur(5.0)
})
.on_click({
move |_| {
set_counter.update(|value| *value += 1);
true
}
})
.hover_style(|s| s.background(Color::LIGHT_GREEN))
.active_style(|s| s.color(Color::WHITE).background(Color::DARK_GREEN))
.keyboard_navigatable()
.focus_visible_style(|s| s.border_color(Color::BLUE).border(2.)),
text("Decrement")
.on_click({
move |_| {
set_counter.update(|value| *value -= 1);
true
}
})
.style(|s| {
s.box_shadow_blur(5.0)
.background(Color::WHITE)
.border_radius(10.0)
.padding_px(10.0)
.margin_left_px(10.0)
})
.hover_style(|s| s.background(Color::rgb8(244, 67, 54)))
.active_style(|s| s.color(Color::WHITE).background(Color::RED))
.keyboard_navigatable()
.focus_visible_style(|s| s.border_color(Color::BLUE).border(2.)),
text("Reset to 0")
.on_click(move |_| {
println!("Reset counter pressed"); // will not fire if button is disabled
set_counter.update(|value| *value = 0);
true
})
.disabled(move || counter.get() == 0)
.style(|s| {
s.box_shadow_blur(5.0)
.border_radius(10.0)
.padding_px(10.0)
.margin_left_px(10.0)
.background(Color::LIGHT_BLUE)
})
.disabled_style(|s| s.background(Color::LIGHT_GRAY))
.hover_style(|s| s.background(Color::LIGHT_YELLOW))
.active_style(|s| s.color(Color::WHITE).background(Color::YELLOW_GREEN))
.keyboard_navigatable()
.focus_visible_style(|s| s.border_color(Color::BLUE).border(2.)),
)),
))
.style(|s| {
s.size_pct(100.0, 100.0)
.flex_col()
Expand Down
2 changes: 1 addition & 1 deletion examples/responsive/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use floem::{
};

fn app_view() -> impl View {
stack(|| {
stack({
(label(|| "Resize the window to see the magic")
.style(|s| {
s.border(1.0)
Expand Down
12 changes: 6 additions & 6 deletions examples/virtual_list/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@ fn app_view() -> impl View {
let long_list: im::Vector<i32> = (0..1000000).collect();
let (long_list, _set_long_list) = create_signal(long_list);

container(move || {
scroll(move || {
container(
scroll(
virtual_list(
VirtualListDirection::Vertical,
VirtualListItemSize::Fixed(Box::new(|| 20.0)),
move || long_list.get(),
move |item| *item,
move |item| label(move || item.to_string()).style(|s| s.height_px(20.0)),
)
.style(|s| s.flex_col())
})
.style(|s| s.width_px(100.0).height_pct(100.0).border(1.0))
})
.style(|s| s.flex_col()),
)
.style(|s| s.width_px(100.0).height_pct(100.0).border(1.0)),
)
.style(|s| {
s.size_pct(100.0, 100.0)
.padding_vert_px(20.0)
Expand Down
2 changes: 1 addition & 1 deletion examples/widget-gallery/src/buttons.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use floem::{
use crate::form::{form, form_item};

pub fn button_view() -> impl View {
form(|| {
form({
(
form_item("Basic Button:".to_string(), 120.0, || {
label(|| "Click me")
Expand Down
6 changes: 3 additions & 3 deletions examples/widget-gallery/src/checkbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::form::{form, form_item};

pub fn checkbox_view() -> impl View {
let (is_checked, set_is_checked) = create_signal(true);
form(move || {
form({
(
form_item("Basic Checkbox:".to_string(), 120.0, move || {
checkbox(is_checked)
Expand All @@ -20,7 +20,7 @@ pub fn checkbox_view() -> impl View {
})
}),
form_item("Labelled Checkbox:".to_string(), 120.0, move || {
stack(|| {
stack({
(
checkbox(is_checked)
.focus_visible_style(|s| s.border_color(Color::BLUE).border(2.)),
Expand All @@ -33,7 +33,7 @@ pub fn checkbox_view() -> impl View {
})
}),
form_item("Disabled Checkbox:".to_string(), 120.0, move || {
stack(|| {
stack({
(
checkbox(is_checked)
.focus_visible_style(|s| s.border_color(Color::BLUE).border(2.)),
Expand Down
2 changes: 1 addition & 1 deletion examples/widget-gallery/src/context_menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use floem::{
};

pub fn menu_view() -> impl View {
stack(move || {
stack({
(
label(|| "Click me (Popout menu)")
.base_style(|s| s.padding_px(10.0).margin_bottom_px(10.0).border(1.0))
Expand Down
20 changes: 8 additions & 12 deletions examples/widget-gallery/src/form.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use floem::{
views::{container, label, stack, Decorators},
};

pub fn form<VT: ViewTuple + 'static>(children: impl FnOnce() -> VT) -> impl View {
pub fn form<VT: ViewTuple + 'static>(children: VT) -> impl View {
stack(children).style(|s| {
s.flex_col()
.items_start()
Expand All @@ -20,18 +20,14 @@ pub fn form_item<V: View + 'static>(
label_width: f32,
view_fn: impl Fn() -> V,
) -> impl View {
container(|| {
stack(|| {
(
container(|| {
label(move || item_label.clone()).style(|s| s.font_weight(Weight::BOLD))
})
container(
stack((
container(label(move || item_label.clone()).style(|s| s.font_weight(Weight::BOLD)))
.style(move |s| s.width_px(label_width).justify_end().margin_right_px(10.0)),
view_fn(),
)
})
.style(|s| s.flex_row().items_start())
})
view_fn(),
))
.style(|s| s.flex_row().items_start()),
)
.style(|s| {
s.flex_row()
.items_center()
Expand Down
2 changes: 1 addition & 1 deletion examples/widget-gallery/src/inputs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::form::{form, form_item};
pub fn text_input_view() -> impl View {
let text = create_rw_signal("".to_string());

form(move || {
form({
(
form_item("Simple Input:".to_string(), 120.0, move || {
text_input(text)
Expand Down
2 changes: 1 addition & 1 deletion examples/widget-gallery/src/labels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use floem::{
use crate::form::{form, form_item};

pub fn label_view() -> impl View {
form(|| {
form({
(
form_item("Simple Label:".to_string(), 120.0, || {
label(move || "This is a simple label".to_owned())
Expand Down
24 changes: 12 additions & 12 deletions examples/widget-gallery/src/lists.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,27 @@ use floem::{
use crate::form::{form, form_item};

pub fn virt_list_view() -> impl View {
stack(|| {
stack({
(
form(|| (form_item("Simple List".to_string(), 120.0, simple_list),)),
form(|| (form_item("Enhanced List".to_string(), 120.0, enhanced_list),)),
form((form_item("Simple List".to_string(), 120.0, simple_list),)),
form((form_item("Enhanced List".to_string(), 120.0, enhanced_list),)),
)
})
}

fn simple_list() -> impl View {
let long_list: im::Vector<i32> = (0..100).collect();
let (long_list, _set_long_list) = create_signal(long_list);
scroll(move || {
scroll(
virtual_list(
VirtualListDirection::Vertical,
VirtualListItemSize::Fixed(Box::new(|| 20.0)),
move || long_list.get(),
move |item| *item,
move |item| label(move || item.to_string()).style(|s| s.height_px(24.0)),
)
.style(|s| s.flex_col())
})
.style(|s| s.flex_col()),
)
.style(|s| s.width_px(100.0).height_px(300.0).border(1.0))
}

Expand All @@ -46,7 +46,7 @@ fn enhanced_list() -> impl View {
let (selected, set_selected) = create_signal(0);
let list_width = 180.0;
let item_height = 32.0;
scroll(move || {
scroll(
virtual_list(
VirtualListDirection::Vertical,
VirtualListItemSize::Fixed(Box::new(|| 32.0)),
Expand All @@ -59,16 +59,16 @@ fn enhanced_list() -> impl View {
.position(|it| *it == item)
.unwrap();
let (is_checked, set_is_checked) = create_signal(true);
container(move || {
stack(move || {
container({
stack({
(
checkbox(is_checked).on_click(move |_| {
set_is_checked.update(|checked: &mut bool| *checked = !*checked);
true
}),
label(move || item.to_string())
.style(|s| s.height_px(32.0).font_size(32.0)),
container(move || {
container({
label(move || " X ")
.on_click(move |_| {
print!("Item Removed");
Expand Down Expand Up @@ -139,7 +139,7 @@ fn enhanced_list() -> impl View {
.hover_style(|s| s.background(Color::LIGHT_GRAY).cursor(CursorStyle::Pointer))
},
)
.style(move |s| s.flex_col().width_px(list_width))
})
.style(move |s| s.flex_col().width_px(list_width)),
)
.style(move |s| s.width_px(list_width).height_px(300.0).border(1.0))
}
Loading

0 comments on commit f1c9842

Please sign in to comment.