A Simple GUI Library

Let us design a classical GUI library using our new knowledge of traits and trait objects.

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.
  • Label: has a label.

The widgets will implement a Widget trait, see below.

Copy the code below to https://play.rust-lang.org/, fill in the missing draw_into methods so that you implement the Widget trait:

  1. // TODO: remove this when you're done with your implementation.
  2. #![allow(unused_imports, unused_variables, dead_code)]
  3. pub trait Widget {
  4. /// Natural width of `self`.
  5. fn width(&self) -> usize;
  6. /// Draw the widget into a buffer.
  7. fn draw_into(&self, buffer: &mut dyn std::fmt::Write);
  8. /// Draw the widget on standard output.
  9. fn draw(&self) {
  10. let mut buffer = String::new();
  11. self.draw_into(&mut buffer);
  12. println!("{buffer}");
  13. }
  14. }
  15. pub struct Label {
  16. label: String,
  17. }
  18. impl Label {
  19. fn new(label: &str) -> Label {
  20. Label {
  21. label: label.to_owned(),
  22. }
  23. }
  24. }
  25. pub struct Button {
  26. label: Label,
  27. callback: Box<dyn FnMut()>,
  28. }
  29. impl Button {
  30. fn new(label: &str, callback: Box<dyn FnMut()>) -> Button {
  31. Button {
  32. label: Label::new(label),
  33. callback,
  34. }
  35. }
  36. }
  37. pub struct Window {
  38. title: String,
  39. widgets: Vec<Box<dyn Widget>>,
  40. }
  41. impl Window {
  42. fn new(title: &str) -> Window {
  43. Window {
  44. title: title.to_owned(),
  45. widgets: Vec::new(),
  46. }
  47. }
  48. fn add_widget(&mut self, widget: Box<dyn Widget>) {
  49. self.widgets.push(widget);
  50. }
  51. }
  52. impl Widget for Label {
  53. fn width(&self) -> usize {
  54. unimplemented!()
  55. }
  56. fn draw_into(&self, buffer: &mut dyn std::fmt::Write) {
  57. unimplemented!()
  58. }
  59. }
  60. impl Widget for Button {
  61. fn width(&self) -> usize {
  62. unimplemented!()
  63. }
  64. fn draw_into(&self, buffer: &mut dyn std::fmt::Write) {
  65. unimplemented!()
  66. }
  67. }
  68. impl Widget for Window {
  69. fn width(&self) -> usize {
  70. unimplemented!()
  71. }
  72. fn draw_into(&self, buffer: &mut dyn std::fmt::Write) {
  73. unimplemented!()
  74. }
  75. }
  76. fn main() {
  77. let mut window = Window::new("Rust GUI Demo 1.23");
  78. window.add_widget(Box::new(Label::new("This is a small text GUI demo.")));
  79. window.add_widget(Box::new(Button::new(
  80. "Click me!",
  81. Box::new(|| println!("You clicked the button!")),
  82. )));
  83. window.draw();
  84. }

The output of the above program can be something simple like this:

======== Rust GUI Demo 1.23 ======== This is a small text GUI demo. | Click me! |

If you want to draw aligned text, you can use the fill/alignment formatting operators. In particular, notice how you can pad with different characters (here a '/') and how you can control alignment:

  1. fn main() {
  2. let width = 10;
  3. println!("left aligned: |{:/<width$}|", "foo");
  4. println!("centered: |{:/^width$}|", "foo");
  5. println!("right aligned: |{:/>width$}|", "foo");
  6. }

Using such alignment tricks, you can for example produce output like this:

+--------------------------------+ | Rust GUI Demo 1.23 | +================================+ | This is a small text GUI demo. | | +-----------+ | | | Click me! | | | +-----------+ | +--------------------------------+