Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Todo example: add a context menu #7677

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading