mirror of
https://github.com/google/comprehensive-rust.git
synced 2025-04-15 21:28:43 +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:
parent
0f768df367
commit
a883a569d0
@ -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
|
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:
|
We will have a number of widgets in our library:
|
||||||
|
|
||||||
* `Window`: has a `title` and contains other widgets.
|
* `Window`: has a `title` and contains other widgets.
|
||||||
* `Button`: has a `label` and a callback function which is invoked when the
|
* `Button`: has a `label`. In reality, it would also take a callback
|
||||||
button is pressed.
|
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`.
|
* `Label`: has a `label`.
|
||||||
|
|
||||||
The widgets will implement a `Widget` trait, see below.
|
The widgets will implement a `Widget` trait, see below.
|
||||||
|
@ -43,14 +43,12 @@ impl Label {
|
|||||||
|
|
||||||
pub struct Button {
|
pub struct Button {
|
||||||
label: Label,
|
label: Label,
|
||||||
callback: Box<dyn FnMut()>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Button {
|
impl Button {
|
||||||
fn new(label: &str, callback: Box<dyn FnMut()>) -> Button {
|
fn new(label: &str) -> Button {
|
||||||
Button {
|
Button {
|
||||||
label: Label::new(label),
|
label: Label::new(label),
|
||||||
callback,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -158,8 +156,7 @@ fn main() {
|
|||||||
let mut window = Window::new("Rust GUI Demo 1.23");
|
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(Label::new("This is a small text GUI demo.")));
|
||||||
window.add_widget(Box::new(Button::new(
|
window.add_widget(Box::new(Button::new(
|
||||||
"Click me!",
|
"Click me!"
|
||||||
Box::new(|| println!("You clicked the button!")),
|
|
||||||
)));
|
)));
|
||||||
window.draw();
|
window.draw();
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
# Day 3 Morning Exercise
|
# Day 3 Morning Exercise
|
||||||
|
|
||||||
## A Simple GUI Library
|
## Drawing A Simple GUI
|
||||||
|
|
||||||
([back to exercise](simple-gui.md))
|
([back to exercise](simple-gui.md))
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user