Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into splitter
Browse files Browse the repository at this point in the history
  • Loading branch information
dparnell committed Dec 22, 2023
2 parents d3d5456 + ac36545 commit 998a82a
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 37 deletions.
8 changes: 8 additions & 0 deletions examples/dyn-container/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "dyn-container"
version = "0.1.0"
edition = "2021"

[dependencies]
im = "15.1.0"
floem = { path = "../.." }
64 changes: 64 additions & 0 deletions examples/dyn-container/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
use floem::{
reactive::{create_rw_signal, RwSignal},
view::View,
views::{dyn_container, h_stack, label, v_stack, Decorators},
widgets::button,
};

#[derive(Clone, PartialEq)]
enum ViewSwitcher {
One,
Two,
}

fn view_one() -> impl View {
label(|| "A view")
}

fn view_two(view: RwSignal<ViewSwitcher>) -> impl View {
v_stack((
label(|| "Another view"),
button(|| "Switch back").on_click_stop(move |_| {
view.set(ViewSwitcher::One);
}),
))
.style(|s| s.gap(0.0, 10.0))
}

fn app_view() -> impl View {
let view = create_rw_signal(ViewSwitcher::One);

v_stack((
h_stack((
label(|| "Swap views:").style(|s| s.padding(5)),
button(|| "Switch views")
.on_click_stop(move |_| {
if view.get() == ViewSwitcher::One {
view.set(ViewSwitcher::Two);
} else {
view.set(ViewSwitcher::One);
}
})
.style(|s| s.margin_bottom(20)),
)),
dyn_container(
move || view.get(),
move |value| match value {
ViewSwitcher::One => Box::new(view_one()),
ViewSwitcher::Two => Box::new(view_two(view)),
},
)
.style(|s| s.padding(10).border(1)),
))
.style(|s| {
s.width_full()
.height_full()
.items_center()
.justify_center()
.gap(10, 0)
})
}

fn main() {
floem::launch(app_view);
}
63 changes: 31 additions & 32 deletions src/views/dyn_container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,52 +19,51 @@ pub struct DynamicContainer<T: 'static> {
///
/// ## Example
/// ```ignore
/// #[derive(Debug, Clone)]
/// use floem::{
/// reactive::create_rw_signal,
/// view::View,
/// views::{dyn_container, label, v_stack, Decorators},
/// widgets::toggle_button,
/// };
///
/// #[derive(Clone)]
/// enum ViewSwitcher {
/// One,
/// Two,
/// }
///
/// fn app() -> impl View {
///
/// fn app_view() -> impl View {
/// let view = create_rw_signal(ViewSwitcher::One);
///
/// let button = || {
/// // imaginary toggle button and state
/// toggle_button(
/// // on toggle function
/// move |state| match state {
/// State::On => view.update(|val| *val = Views::One),
/// State::Off => view.update(|val| *val = Views::Two),
/// },
/// )
/// };
///
/// stack(|| (
/// button(),
/// v_stack((
/// toggle_button(|| true)
/// .on_toggle(move |is_on| {
/// if is_on {
/// view.update(|val| *val = ViewSwitcher::One);
/// } else {
/// view.update(|val| *val = ViewSwitcher::Two);
/// }
/// })
/// .style(|s| s.margin_bottom(20)),
/// dyn_container(
/// move || view.get(),
/// move |val: ViewSwitcher| match val {
/// ViewSwitcher::One => Box::new(label(|| "one".into())),
/// ViewSwitcher::Two => {
/// Box::new(
/// stack(|| (
/// label(|| "stacked".into()),
/// label(|| "two".into())
/// ))
/// ),
/// }
/// move |value| match value {
/// ViewSwitcher::One => Box::new(label(|| "One")),
/// ViewSwitcher::Two => Box::new(v_stack((label(|| "Stacked"), label(|| "Two")))),
/// },
/// )
/// ),
/// ))
/// .style(|| {
/// Style::new()
/// .size(100.pct(), 100.pct())
/// .style(|s| {
/// s.width_full()
/// .height_full()
/// .items_center()
/// .justify_center()
/// .gap(points(10.))
/// .gap(10, 0)
/// })
/// }
///
/// fn main() {
/// floem::launch(app_view);
/// }
/// ```
///
/// See [container_box](crate::views::container_box()) for more documentation on a general container
Expand Down
16 changes: 12 additions & 4 deletions src/views/text_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,14 @@ impl From<(&KeyEvent, &SmolStr)> for TextCommand {
}
}

fn is_word_based_action(event: &KeyEvent) -> bool {
#[cfg(not(target_os = "macos"))]
return event.modifiers.contains(ModifiersState::CONTROL);

#[cfg(target_os = "macos")]
return event.modifiers.contains(ModifiersState::SUPER);
}

const DEFAULT_FONT_SIZE: f32 = 14.0;
const CURSOR_BLINK_INTERVAL_MS: u64 = 500;
/// Specifies approximately how many characters wide the input field should be
Expand Down Expand Up @@ -622,7 +630,7 @@ impl TextInput {
} else {
let prev_cursor_idx = self.cursor_glyph_idx;

if event.modifiers.contains(ModifiersState::CONTROL) {
if is_word_based_action(event) {
self.move_cursor(Movement::Word, Direction::Left);
} else {
self.move_cursor(Movement::Glyph, Direction::Left);
Expand All @@ -649,7 +657,7 @@ impl TextInput {

let prev_cursor_idx = self.cursor_glyph_idx;

if event.modifiers.contains(ModifiersState::CONTROL) {
if is_word_based_action(event) {
self.move_cursor(Movement::Word, Direction::Right);
} else {
self.move_cursor(Movement::Glyph, Direction::Right);
Expand All @@ -675,7 +683,7 @@ impl TextInput {
Key::Named(NamedKey::ArrowLeft) => {
let old_glyph_idx = self.cursor_glyph_idx;

let cursor_moved = if event.modifiers.contains(ModifiersState::CONTROL) {
let cursor_moved = if is_word_based_action(event) {
self.move_cursor(Movement::Word, Direction::Left)
} else {
self.move_cursor(Movement::Glyph, Direction::Left)
Expand All @@ -699,7 +707,7 @@ impl TextInput {
Key::Named(NamedKey::ArrowRight) => {
let old_glyph_idx = self.cursor_glyph_idx;

let cursor_moved = if event.modifiers.contains(ModifiersState::CONTROL) {
let cursor_moved = if is_word_based_action(event) {
self.move_cursor(Movement::Word, Direction::Right)
} else {
self.move_cursor(Movement::Glyph, Direction::Right)
Expand Down
2 changes: 1 addition & 1 deletion vger/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ futures = "0.3.26"
anyhow = "1.0.69"
peniko = { git = "https://github.com/linebender/peniko", rev = "cafdac9a211a0fb2fec5656bd663d1ac770bcc81" }
swash = "0.1.8"
vger = { git = "https://github.com/lapce/vger-rs", rev = "ba0df7cd8e23b9fa692c827b56491d2034f3a810" }
vger = { git = "https://github.com/lapce/vger-rs", rev = "1820c59e09fa731d0867a908d0ef094d27e1b3fb" }
# vger = { path = "../../vger-rs" }
image = { version = "0.24", features = ["jpeg", "png"] }
floem_renderer = { path = "../renderer" }

0 comments on commit 998a82a

Please sign in to comment.