1
0
mirror of https://github.com/google/comprehensive-rust.git synced 2025-04-13 04:30:31 +02:00

Remove unused callback from GUI exercise (#1293)

Here's a possible update to the GUI/TUI exercise, taking out the
callback and explaining (slightly).

See what you think.

---------

Co-authored-by: Martin Geisler <martin@geisler.net>
This commit is contained in:
Nick Radcliffe 2023-10-03 15:07:33 +01:00 committed by GitHub
parent 0f768df367
commit a883a569d0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 8 additions and 10 deletions

View File

@ -1,13 +1,14 @@
# A Simple GUI Library
# Drawing A Simple GUI
Let us design a classical GUI library using our new knowledge of traits and
trait objects.
trait objects. We'll only implement the drawing of it (as text) for simplicity.
We will have a number of widgets in our library:
* `Window`: has a `title` and contains other widgets.
* `Button`: has a `label` and a callback function which is invoked when the
button is pressed.
* `Button`: has a `label`. In reality, it would also take a callback
function to allow the program to do something when the button is clicked
but we won't include that since we're only drawing the GUI.
* `Label`: has a `label`.
The widgets will implement a `Widget` trait, see below.

View File

@ -43,14 +43,12 @@ impl Label {
pub struct Button {
label: Label,
callback: Box<dyn FnMut()>,
}
impl Button {
fn new(label: &str, callback: Box<dyn FnMut()>) -> Button {
fn new(label: &str) -> Button {
Button {
label: Label::new(label),
callback,
}
}
}
@ -158,8 +156,7 @@ fn main() {
let mut window = Window::new("Rust GUI Demo 1.23");
window.add_widget(Box::new(Label::new("This is a small text GUI demo.")));
window.add_widget(Box::new(Button::new(
"Click me!",
Box::new(|| println!("You clicked the button!")),
"Click me!"
)));
window.draw();
}

View File

@ -1,6 +1,6 @@
# Day 3 Morning Exercise
## A Simple GUI Library
## Drawing A Simple GUI
([back to exercise](simple-gui.md))