feat: initial version
This commit is contained in:
@@ -0,0 +1 @@
|
||||
/target
|
||||
Generated
+6262
File diff suppressed because it is too large
Load Diff
+13
@@ -0,0 +1,13 @@
|
||||
[package]
|
||||
name = "bropicker"
|
||||
version = "0.0.1"
|
||||
edition = "2024"
|
||||
build = "build.rs"
|
||||
|
||||
[dependencies]
|
||||
serde = { version = "1.0.228", features = ["derive"] }
|
||||
serde_json = "1.0.149"
|
||||
slint = { version = "1.16.1", features = ["serde"] }
|
||||
|
||||
[build-dependencies]
|
||||
slint-build = "1.16.1"
|
||||
@@ -0,0 +1,6 @@
|
||||
// Copyright © SixtyFPS GmbH <info@slint.dev>
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
fn main() {
|
||||
slint_build::compile("ui/todo.slint").unwrap();
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
# Copyright © SixtyFPS GmbH <info@slint.dev>
|
||||
# SPDX-License-Identifier: MIT
|
||||
|
||||
[package]
|
||||
name = "todo"
|
||||
version = "1.17.0"
|
||||
authors = ["Slint Developers <info@slint.dev>"]
|
||||
edition.workspace = true
|
||||
build = "build.rs"
|
||||
publish = false
|
||||
license = "MIT"
|
||||
|
||||
[lib]
|
||||
crate-type = ["lib", "cdylib"]
|
||||
path = "lib.rs"
|
||||
name = "todo_lib"
|
||||
|
||||
[[bin]]
|
||||
path = "main.rs"
|
||||
name = "todo"
|
||||
|
||||
[dependencies]
|
||||
slint = { path = "../../../api/rs/slint", features = ["serde", "backend-android-activity-06"] }
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
serde_json = "1.0"
|
||||
|
||||
[target.'cfg(target_arch = "wasm32")'.dependencies]
|
||||
wasm-bindgen = { version = "0.2" }
|
||||
console_error_panic_hook = "0.1.5"
|
||||
|
||||
[build-dependencies]
|
||||
slint-build = { path = "../../../api/rs/build" }
|
||||
|
||||
[dev-dependencies]
|
||||
i-slint-backend-testing = { workspace = true }
|
||||
@@ -0,0 +1,41 @@
|
||||
<!DOCTYPE html>
|
||||
|
||||
<!-- Copyright © SixtyFPS GmbH <info@slint.dev> -->
|
||||
<!-- SPDX-License-Identifier: MIT -->
|
||||
|
||||
<html>
|
||||
<!--
|
||||
This is a static html file used to display the wasm build.
|
||||
In order to generate the build
|
||||
- Run `wasm-pack build --release --target web` in this directory.
|
||||
-->
|
||||
|
||||
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Slint Todo Demo (Web Assembly version)</title>
|
||||
<link rel="stylesheet" href="https://slint.dev/css/demos-v1.css">
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<p>This is the <a href="https://slint.dev">Slint</a> Todo Demo compiled to WebAssembly.</p>
|
||||
<div id="spinner" style="position: relative;">
|
||||
<div class="spinner">Loading...</div>
|
||||
</div>
|
||||
<canvas id="canvas" unselectable="on" data-slint-auto-resize-to-preferred="true"></canvas>
|
||||
<p class="links">
|
||||
<a href="https://github.com/slint-ui/slint/blob/master/examples/todo/">
|
||||
View Source Code on GitHub</a> -
|
||||
<a href="https://slint.dev/editor?load_demo=examples/todo/ui/todo.slint">
|
||||
Open in SlintPad
|
||||
</a>
|
||||
</p>
|
||||
<script type="module">
|
||||
import init from './pkg/todo_lib.js';
|
||||
init().finally(() => {
|
||||
document.getElementById("spinner").remove();
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
+204
@@ -0,0 +1,204 @@
|
||||
// Copyright © SixtyFPS GmbH <info@slint.dev>
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
use slint::{FilterModel, Model, SortModel};
|
||||
use std::rc::Rc;
|
||||
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
use wasm_bindgen::prelude::*;
|
||||
|
||||
slint::include_modules!();
|
||||
|
||||
#[cfg_attr(target_arch = "wasm32", wasm_bindgen(start))]
|
||||
pub fn main() {
|
||||
let state = init();
|
||||
let main_window = state.main_window.clone_strong();
|
||||
#[cfg(target_os = "android")]
|
||||
STATE.with(|ui| *ui.borrow_mut() = Some(state));
|
||||
main_window.run().unwrap();
|
||||
}
|
||||
|
||||
fn init() -> State {
|
||||
// This provides better error messages in debug mode.
|
||||
// It's disabled in release mode so it doesn't bloat up the file size.
|
||||
#[cfg(all(debug_assertions, target_arch = "wasm32"))]
|
||||
console_error_panic_hook::set_once();
|
||||
|
||||
let todo_model = Rc::new(slint::VecModel::<TodoItem>::from(vec![
|
||||
TodoItem { checked: true, title: "Implement the .slint file".into() },
|
||||
TodoItem { checked: true, title: "Do the Rust part".into() },
|
||||
TodoItem { checked: false, title: "Make the C++ code".into() },
|
||||
TodoItem { checked: false, title: "Write some JavaScript code".into() },
|
||||
TodoItem { checked: false, title: "Test the application".into() },
|
||||
TodoItem { checked: false, title: "Ship to customer".into() },
|
||||
TodoItem { checked: false, title: "???".into() },
|
||||
TodoItem { checked: false, title: "Profit".into() },
|
||||
]));
|
||||
|
||||
let main_window = MainWindow::new().unwrap();
|
||||
|
||||
main_window.on_todo_added({
|
||||
let todo_model = todo_model.clone();
|
||||
move |text| todo_model.push(TodoItem { checked: false, title: text })
|
||||
});
|
||||
main_window.on_remove_done({
|
||||
let todo_model = todo_model.clone();
|
||||
move || {
|
||||
let mut offset = 0;
|
||||
for i in 0..todo_model.row_count() {
|
||||
if todo_model.row_data(i - offset).unwrap().checked {
|
||||
todo_model.remove(i - offset);
|
||||
offset += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
let weak_window = main_window.as_weak();
|
||||
main_window.on_popup_confirmed(move || {
|
||||
let window = weak_window.unwrap();
|
||||
window.hide().unwrap();
|
||||
});
|
||||
|
||||
{
|
||||
let weak_window = main_window.as_weak();
|
||||
let todo_model = todo_model.clone();
|
||||
main_window.window().on_close_requested(move || {
|
||||
let window = weak_window.unwrap();
|
||||
|
||||
if todo_model.iter().any(|t| !t.checked) {
|
||||
window.invoke_show_confirm_popup();
|
||||
slint::CloseRequestResponse::KeepWindowShown
|
||||
} else {
|
||||
slint::CloseRequestResponse::HideWindow
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
main_window.on_apply_sorting_and_filtering({
|
||||
let weak_window = main_window.as_weak();
|
||||
let todo_model = todo_model.clone();
|
||||
|
||||
move || {
|
||||
let window = weak_window.unwrap();
|
||||
window.set_todo_model(todo_model.clone().into());
|
||||
|
||||
if window.get_hide_done_items() {
|
||||
window.set_todo_model(
|
||||
Rc::new(FilterModel::new(window.get_todo_model(), |e| !e.checked)).into(),
|
||||
);
|
||||
}
|
||||
|
||||
if window.get_is_sort_by_name() {
|
||||
window.set_todo_model(
|
||||
Rc::new(SortModel::new(window.get_todo_model(), |lhs, rhs| {
|
||||
lhs.title.to_lowercase().cmp(&rhs.title.to_lowercase())
|
||||
}))
|
||||
.into(),
|
||||
);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
main_window.set_show_header(true);
|
||||
main_window.set_todo_model(todo_model.clone().into());
|
||||
State { main_window, todo_model }
|
||||
}
|
||||
|
||||
#[cfg(target_os = "android")]
|
||||
#[unsafe(no_mangle)]
|
||||
fn android_main(app: slint::android::AndroidApp) {
|
||||
use slint::android::android_activity::{MainEvent, PollEvent};
|
||||
slint::android::init_with_event_listener(app, |event| {
|
||||
match event {
|
||||
PollEvent::Main(MainEvent::SaveState { saver, .. }) => {
|
||||
STATE.with(|state| -> Option<()> {
|
||||
let todo_state = SerializedState::save(state.borrow().as_ref()?);
|
||||
saver.store(&serde_json::to_vec(&todo_state).ok()?);
|
||||
Some(())
|
||||
});
|
||||
}
|
||||
PollEvent::Main(MainEvent::Resume { loader, .. }) => {
|
||||
STATE.with(|state| -> Option<()> {
|
||||
let bytes: Vec<u8> = loader.load()?;
|
||||
let todo_state: SerializedState = serde_json::from_slice(&bytes).ok()?;
|
||||
todo_state.restore(state.borrow().as_ref()?);
|
||||
Some(())
|
||||
});
|
||||
}
|
||||
_ => {}
|
||||
};
|
||||
})
|
||||
.unwrap();
|
||||
main();
|
||||
}
|
||||
|
||||
pub struct State {
|
||||
pub main_window: MainWindow,
|
||||
pub todo_model: Rc<slint::VecModel<TodoItem>>,
|
||||
}
|
||||
|
||||
#[cfg(target_os = "android")]
|
||||
thread_local! {
|
||||
static STATE : core::cell::RefCell<Option<State>> = Default::default();
|
||||
}
|
||||
|
||||
#[cfg(target_os = "android")]
|
||||
#[derive(serde::Serialize, serde::Deserialize)]
|
||||
struct SerializedState {
|
||||
items: Vec<TodoItem>,
|
||||
sort: bool,
|
||||
hide_done: bool,
|
||||
}
|
||||
|
||||
#[cfg(target_os = "android")]
|
||||
impl SerializedState {
|
||||
fn restore(self, state: &State) {
|
||||
state.todo_model.set_vec(self.items);
|
||||
state.main_window.set_hide_done_items(self.hide_done);
|
||||
state.main_window.set_is_sort_by_name(self.sort);
|
||||
state.main_window.invoke_apply_sorting_and_filtering();
|
||||
}
|
||||
fn save(state: &State) -> Self {
|
||||
Self {
|
||||
items: state.todo_model.iter().collect(),
|
||||
sort: state.main_window.get_is_sort_by_name(),
|
||||
hide_done: state.main_window.get_hide_done_items(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn press_add_adds_one_todo() {
|
||||
if option_env!("SLINT_EMIT_DEBUG_INFO").unwrap_or_default() != "1" {
|
||||
println!("This test needs to be build with `SLINT_EMIT_DEBUG_INFO=1` in the environment");
|
||||
return;
|
||||
}
|
||||
i_slint_backend_testing::init_no_event_loop();
|
||||
use i_slint_backend_testing::{ElementHandle, ElementQuery};
|
||||
let state = init();
|
||||
state.todo_model.set_vec(vec![TodoItem { checked: false, title: "first".into() }]);
|
||||
let line_edit = ElementQuery::from_root(&state.main_window)
|
||||
.match_id("MainWindow::text-edit")
|
||||
.find_first()
|
||||
.unwrap();
|
||||
assert_eq!(line_edit.accessible_value().unwrap(), "");
|
||||
line_edit.set_accessible_value("second");
|
||||
|
||||
let button = ElementHandle::find_by_accessible_label(&state.main_window, "Add New Entry")
|
||||
.next()
|
||||
.unwrap();
|
||||
button.invoke_accessible_default_action();
|
||||
|
||||
assert_eq!(state.todo_model.row_count(), 2);
|
||||
assert_eq!(
|
||||
state.todo_model.row_data(0).unwrap(),
|
||||
TodoItem { checked: false, title: "first".into() }
|
||||
);
|
||||
assert_eq!(
|
||||
state.todo_model.row_data(1).unwrap(),
|
||||
TodoItem { checked: false, title: "second".into() }
|
||||
);
|
||||
|
||||
assert_eq!(line_edit.accessible_value().unwrap(), "");
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
// Copyright © SixtyFPS GmbH <info@slint.dev>
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
// In order to be compatible with both desktop, wasm, and android, the example is both a binary and a library.
|
||||
// Just forward to the library in main
|
||||
|
||||
fn main() {
|
||||
todo_lib::main();
|
||||
}
|
||||
@@ -0,0 +1,161 @@
|
||||
// Copyright © SixtyFPS GmbH <info@slint.dev>
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import {
|
||||
SpinBox,
|
||||
Button,
|
||||
CheckBox,
|
||||
Slider,
|
||||
LineEdit,
|
||||
ScrollView,
|
||||
ListView,
|
||||
HorizontalBox,
|
||||
VerticalBox,
|
||||
GridBox,
|
||||
StandardButton,
|
||||
Palette,
|
||||
} from "std-widgets.slint";
|
||||
|
||||
@rust-attr(derive(serde::Serialize, serde::Deserialize))
|
||||
export struct TodoItem {
|
||||
title: string,
|
||||
checked: bool,
|
||||
}
|
||||
|
||||
export component MainWindow inherits Window {
|
||||
in property <[TodoItem]> todo-model: [
|
||||
{ title: "Implement the .slint file", checked: true },
|
||||
{ title: "Do the Rust part", checked: false },
|
||||
{ title: "Make the C++ code", checked: false },
|
||||
{ title: "Write some JavaScript code", checked: false },
|
||||
{ title: "Test the application", checked: false },
|
||||
{ title: "Ship to customer", checked: false },
|
||||
{ title: "???", checked: false },
|
||||
{ title: "Profit", checked: false },
|
||||
];
|
||||
|
||||
in property <bool> show-header: false;
|
||||
in-out property <bool> is-sort-by-name: false;
|
||||
in-out property <bool> hide-done-items: false;
|
||||
|
||||
callback todo-added(string);
|
||||
callback remove-done();
|
||||
callback popup_confirmed;
|
||||
callback show_confirm_popup;
|
||||
callback apply_sorting_and_filtering();
|
||||
|
||||
show_confirm_popup => {
|
||||
confirm_popup.show();
|
||||
}
|
||||
|
||||
preferred-width: 400px;
|
||||
preferred-height: 600px;
|
||||
|
||||
confirm_popup := PopupWindow {
|
||||
x: 40px;
|
||||
y: 100px;
|
||||
width: min(confirm_popup_layout.preferred-width, root.width - 80px);
|
||||
|
||||
Rectangle {
|
||||
background: Palette.background;
|
||||
border-color: Palette.border;
|
||||
border-width: 1px;
|
||||
}
|
||||
|
||||
confirm_popup_layout := Dialog {
|
||||
height: 100%;
|
||||
width: 100%;
|
||||
background: transparent;
|
||||
|
||||
confirm_popup_text := Text {
|
||||
text: "Some items are not done, are you sure you wish to quit?";
|
||||
wrap: word-wrap;
|
||||
}
|
||||
|
||||
StandardButton {
|
||||
kind: yes;
|
||||
clicked => {
|
||||
root.popup_confirmed();
|
||||
}
|
||||
}
|
||||
|
||||
StandardButton {
|
||||
kind: no;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
VerticalBox {
|
||||
x: root.safe-area-insets.left;
|
||||
y: root.safe-area-insets.top;
|
||||
width: root.width - root.safe-area-insets.right - root.safe-area-insets.left;
|
||||
height: root.height - root.safe-area-insets.bottom - root.safe-area-insets.top;
|
||||
|
||||
HorizontalBox {
|
||||
padding: 0px;
|
||||
text-edit := LineEdit {
|
||||
accepted(text) => {
|
||||
root.todo-added(self.text);
|
||||
self.text = "";
|
||||
}
|
||||
|
||||
placeholder-text: "What needs to be done?";
|
||||
}
|
||||
|
||||
btn := Button {
|
||||
clicked => {
|
||||
root.todo-added(text-edit.text);
|
||||
text-edit.text = "";
|
||||
}
|
||||
|
||||
text: "Add New Entry";
|
||||
enabled: text-edit.text != "";
|
||||
}
|
||||
}
|
||||
|
||||
if (root.show-header): HorizontalBox {
|
||||
padding: 0px;
|
||||
alignment: start;
|
||||
|
||||
CheckBox {
|
||||
toggled => {
|
||||
root.apply_sorting_and_filtering();
|
||||
}
|
||||
|
||||
text: "Sort by name";
|
||||
checked <=> root.is-sort-by-name;
|
||||
}
|
||||
|
||||
CheckBox {
|
||||
toggled => {
|
||||
root.apply_sorting_and_filtering();
|
||||
}
|
||||
|
||||
text: "Hide done items";
|
||||
checked <=> root.hide-done-items;
|
||||
}
|
||||
}
|
||||
|
||||
list-view := ListView {
|
||||
for todo in root.todo-model: HorizontalLayout {
|
||||
CheckBox {
|
||||
text: todo.title;
|
||||
// checked <=> todo.checked;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
HorizontalBox {
|
||||
padding: 0px;
|
||||
alignment: end;
|
||||
|
||||
Button {
|
||||
clicked => {
|
||||
root.remove-done();
|
||||
}
|
||||
|
||||
text: "Remove Done Items";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+51
@@ -0,0 +1,51 @@
|
||||
use std::rc::Rc;
|
||||
|
||||
slint::include_modules!();
|
||||
|
||||
fn init() -> State {
|
||||
let browser_model = Rc::new(slint::VecModel::<BrowserConfig>::from(vec![
|
||||
BrowserConfig {
|
||||
path: "chrome.exe".into(),
|
||||
flags: "profile 1".into(),
|
||||
},
|
||||
BrowserConfig {
|
||||
path: "chrome.exe".into(),
|
||||
flags: "profile 2".into(),
|
||||
},
|
||||
BrowserConfig {
|
||||
path: "zen.exe".into(),
|
||||
flags: "".into(),
|
||||
},
|
||||
BrowserConfig {
|
||||
path: "edge.exe".into(),
|
||||
flags: "".into(),
|
||||
},
|
||||
]));
|
||||
|
||||
let main_window = MainWindow::new().unwrap();
|
||||
|
||||
{
|
||||
main_window.window().on_close_requested(move || {
|
||||
todo!();
|
||||
});
|
||||
}
|
||||
|
||||
main_window.set_browser_model(browser_model.clone().into());
|
||||
State {
|
||||
main_window,
|
||||
browser_model: browser_model,
|
||||
}
|
||||
}
|
||||
|
||||
pub struct State {
|
||||
pub main_window: MainWindow,
|
||||
pub browser_model: Rc<slint::VecModel<BrowserConfig>>,
|
||||
}
|
||||
|
||||
pub fn main() {
|
||||
let state = init();
|
||||
let main_window = state.main_window.clone_strong();
|
||||
#[cfg(target_os = "android")]
|
||||
STATE.with(|ui| *ui.borrow_mut() = Some(state));
|
||||
main_window.run().unwrap();
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
import {
|
||||
SpinBox,
|
||||
Button,
|
||||
CheckBox,
|
||||
Slider,
|
||||
LineEdit,
|
||||
ScrollView,
|
||||
ListView,
|
||||
HorizontalBox,
|
||||
VerticalBox,
|
||||
GridBox,
|
||||
StandardButton,
|
||||
Palette,
|
||||
} from "std-widgets.slint";
|
||||
|
||||
@rust-attr(derive(serde::Serialize, serde::Deserialize))
|
||||
export struct BrowserConfig {
|
||||
path: string,
|
||||
flags: string,
|
||||
}
|
||||
|
||||
export component MainWindow inherits Window {
|
||||
in property <[BrowserConfig]> browser-model: [
|
||||
{ path: "chrome.exe", flags: "profile 1" },
|
||||
{ path: "chrome.exe", flags: "profile 2" },
|
||||
{ path: "zen.exe", flags: "" },
|
||||
{ path: "edge.exe", flags: "" },
|
||||
];
|
||||
|
||||
callback todo-added(string);
|
||||
callback remove-done();
|
||||
callback update_todo(BrowserConfig);
|
||||
|
||||
preferred-width: 400px;
|
||||
preferred-height: 600px;
|
||||
|
||||
VerticalBox {
|
||||
x: root.safe-area-insets.left;
|
||||
y: root.safe-area-insets.top;
|
||||
width: root.width - root.safe-area-insets.right - root.safe-area-insets.left;
|
||||
height: root.height - root.safe-area-insets.bottom - root.safe-area-insets.top;
|
||||
|
||||
list-view := ListView {
|
||||
for browser in root.browser-model: HorizontalLayout {
|
||||
HorizontalBox {
|
||||
padding: 8px;
|
||||
alignment: center;
|
||||
|
||||
Text {
|
||||
text: browser.path;
|
||||
}
|
||||
|
||||
Text {
|
||||
text: browser.flags;
|
||||
}
|
||||
|
||||
Button {
|
||||
text: "use";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user