Skip to content

Commit

Permalink
Todo example: add a context menu
Browse files Browse the repository at this point in the history
  • Loading branch information
ogoffart committed Feb 20, 2025
1 parent c3732e6 commit 705f810
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 8 deletions.
2 changes: 2 additions & 0 deletions examples/todo/cpp/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ AppState create_ui()
}
});

demo->on_remove([todo_model](int index) { todo_model->erase(index); });

demo->on_popup_confirmed(
[demo = slint::ComponentWeakHandle(demo)] { (*demo.lock())->window().hide(); });

Expand Down
6 changes: 6 additions & 0 deletions examples/todo/rust/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ fn init() -> State {
let todo_model = todo_model.clone();
move |text| todo_model.push(TodoItem { checked: false, title: text })
});
main_window.on_remove({
let todo_model = todo_model.clone();
move |index| {
todo_model.remove(index as usize);
}
});
main_window.on_remove_done({
let todo_model = todo_model.clone();
move || {
Expand Down
34 changes: 26 additions & 8 deletions examples/todo/ui/todo.slint
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-License-Identifier: MIT

import { SpinBox, Button, CheckBox, Slider, LineEdit, ScrollView, ListView,
HorizontalBox, VerticalBox, GridBox, StandardButton, Palette } from "std-widgets.slint";
HorizontalBox, VerticalBox, GridBox, StandardButton, Palette, StyleMetrics } from "std-widgets.slint";

@rust-attr(derive(serde::Serialize, serde::Deserialize))
export struct TodoItem {
Expand All @@ -28,6 +28,7 @@ export component MainWindow inherits Window {

callback todo-added(string);
callback remove-done();
callback remove(index: int);
callback popup_confirmed;
callback show_confirm_popup;
callback apply_sorting_and_filtering();
Expand Down Expand Up @@ -108,14 +109,31 @@ export component MainWindow inherits Window {
}

list-view := ListView {
for todo in root.todo-model: HorizontalLayout {
CheckBox {
toggled => {
todo.checked = self.checked;
for todo[index] in root.todo-model: cm := ContextMenu {
MenuItem {
title: @tr("Remove");
activated => { root.remove(index); }
}
MenuItem {
title: todo.checked ? @tr("Mark as not done") : @tr("Mark as done");
activated => {
todo.checked = !todo.checked;
}
}
HorizontalLayout {
padding: StyleMetrics.layout-spacing / 2;
spacing: StyleMetrics.layout-spacing;
CheckBox {
toggled => {
todo.checked = self.checked;
}
checked: todo.checked;
horizontal-stretch: 0;
}
Text {
horizontal-stretch: 1;
text: todo.title;
}

text: todo.title;
checked: todo.checked;
}
}
}
Expand Down

0 comments on commit 705f810

Please sign in to comment.