Skip to content

Commit

Permalink
Add To-Do app example
Browse files Browse the repository at this point in the history
  • Loading branch information
jrmoulton committed Nov 2, 2024
1 parent 6f0bc39 commit 410bf3f
Show file tree
Hide file tree
Showing 6 changed files with 981 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
.DS_Store
rustc-ice*
*.orig

*.db
12 changes: 12 additions & 0 deletions examples/todo-complex/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "todo-complex"
version = "0.1.0"
edition = "2021"

[dependencies]
confy = "0.6.1"
floem = { path = "../..", features = ["serde", "vello"], default-features = false }
im.workspace = true
rusqlite = { version = "0.32.1", features = ["bundled"] }
serde = {workspace = true}
# serde = { version = "1.0.210", features = ["derive"] }
128 changes: 128 additions & 0 deletions examples/todo-complex/src/app_config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
use floem::action::set_window_scale;
use floem::keyboard::Key;
use floem::reactive::provide_context;
use floem::views::Decorators;
use floem::{
event::{Event, EventListener},
kurbo::{Point, Size},
reactive::{create_updater, RwSignal, SignalGet, SignalUpdate, SignalWith},
window::WindowConfig,
Application, IntoView,
};
use serde::{Deserialize, Serialize};

use crate::OS_MOD;

#[derive(Deserialize, Serialize, Clone, Copy, PartialEq, Eq, Debug)]
pub enum AppTheme {
FollowSystem,
DarkMode,
LightMode,
}

#[derive(Deserialize, Serialize, Clone, Copy, PartialEq, Eq, Debug)]
pub struct AppThemeState {
pub system: floem::window::Theme,
pub theme: AppTheme,
}

#[derive(Deserialize, Serialize, Clone, Debug)]
pub struct AppConfig {
pub position: Point,
pub size: Size,
pub app_theme: AppThemeState,
pub window_scale: f64,
}

impl std::default::Default for AppConfig {
fn default() -> Self {
Self {
position: Point { x: 500.0, y: 500.0 },
size: Size {
width: 350.0,
height: 650.0,
},
app_theme: AppThemeState {
system: floem::window::Theme::Dark,
theme: AppTheme::FollowSystem,
},
window_scale: 1.,
}
}
}

pub fn launch_with_track<V: IntoView + 'static>(app_view: impl FnOnce() -> V + 'static) {
let config: AppConfig = confy::load("my_app", "floem-defaults").unwrap_or_default();

let app = Application::new();

// modifying this will rewrite app config to disk
let app_config = RwSignal::new(config);

provide_context(app_config);

// todo: debounce this
create_updater(
move || app_config.get(),
|config| {
let _ = confy::store("my_app", "floem-defaults", config);
},
);

let window_config = WindowConfig::default()
.size(app_config.with(|ac| ac.size))
.position(app_config.with(|ac| ac.position));

app.window(
move |_| {
set_window_scale(app_config.with(|c| c.window_scale));
app_view()
.on_key_down(
Key::Character("=".into()),
|m| m == OS_MOD,
move |_| {
app_config.update(|ac| {
ac.window_scale *= 1.1;
floem::action::set_window_scale(ac.window_scale);
});
},
)
.on_key_down(
Key::Character("-".into()),
|m| m == OS_MOD,
move |_| {
app_config.update(|ac| {
ac.window_scale /= 1.1;
floem::action::set_window_scale(ac.window_scale);
});
},
)
.on_key_down(
Key::Character("0".into()),
|m| m == OS_MOD,
move |_| {
app_config.update(|ac| {
ac.window_scale = 1.;
floem::action::set_window_scale(ac.window_scale);
});
},
)
.on_event_stop(EventListener::WindowMoved, move |event| {
if let Event::WindowMoved(position) = event {
app_config.update(|val| {
val.position = *position;
})
}
})
.on_event_stop(EventListener::WindowResized, move |event| {
if let Event::WindowResized(size) = event {
app_config.update(|val| {
val.size = *size;
})
}
})
},
Some(window_config),
)
.run();
}
Loading

0 comments on commit 410bf3f

Please sign in to comment.