Advertisement
NLinker

GUI controls in Rust

Jun 24th, 2017
401
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 3.18 KB | None | 0 0
  1. // all controls must implement this
  2. trait Control: Sized {
  3.     fn size_x(&self) -> u32;
  4.  
  5.     fn size_y(&self) -> u32;
  6.  
  7.     fn size(&self) -> (u32, u32) {
  8.         return (self.size_x(), self.size_y());
  9.     }
  10. }
  11.  
  12. // all checkboxes must implement this
  13. trait Checkbox: Control {
  14.     fn checked(&self) -> bool;
  15.     fn set_checked(&mut self, is_checked: bool);
  16. }
  17.  
  18. // our custom checkbox has a few supported shapes
  19. #[derive(PartialEq, Copy, Clone)]
  20. enum Shape {
  21.     Circle,
  22.     Square,
  23.     Octagon,
  24. }
  25.  
  26. // and methods to set and retrieve the current shape
  27. trait CustomCheckbox: Checkbox {
  28.     fn shape(&self) -> Shape;
  29.     fn set_shape(&mut self, shape: Shape);
  30. }
  31.  
  32. // this is our custom checkbox control's in-memory representation
  33. struct CoolCheckbox {
  34.     is_checked: bool,
  35.     checkbox_shape: Shape,
  36.     width: u32,
  37.     height: u32,
  38. }
  39.  
  40. impl CoolCheckbox {
  41.     fn new() -> CoolCheckbox {
  42.         CoolCheckbox {
  43.             is_checked: false,
  44.             checkbox_shape: Shape::Square,
  45.             width: 25,
  46.             height: 30,
  47.         }
  48.     }
  49.  
  50.     fn coolness_factor(&self) -> u32 {
  51.         42
  52.     }
  53. }
  54.  
  55. impl Control for CoolCheckbox {
  56.     fn size_x(&self) -> u32 {
  57.         self.width
  58.     }
  59.     fn size_y(&self) -> u32 {
  60.         self.height
  61.     }
  62. }
  63.  
  64. impl Checkbox for CoolCheckbox {
  65.     fn checked(&self) -> bool {
  66.         self.is_checked
  67.     }
  68.     fn set_checked(&mut self, is_checked: bool) {
  69.         self.is_checked = is_checked
  70.     }
  71. }
  72.  
  73. impl CustomCheckbox for CoolCheckbox {
  74.     fn shape(&self) -> Shape {
  75.         self.checkbox_shape
  76.     }
  77.  
  78.     fn set_shape(&mut self, shape: Shape) {
  79.         self.checkbox_shape = shape;
  80.     }
  81. }
  82.  
  83. // doesn't care what kind of control it is
  84. fn do_general<ControlT>(control: &ControlT) -> u32
  85. where
  86.     ControlT: Control,
  87. {
  88.     control.size_x() * control.size_y()
  89. }
  90.  
  91. // doesn't need to know that it is your custom checkbox
  92. fn do_something<CheckboxT>(checkbox: &CheckboxT) -> bool
  93. where
  94.     CheckboxT: Checkbox,
  95. {
  96.     let size_x = checkbox.size_x();
  97.     let checked = checkbox.checked();
  98.  
  99.     //returns true if checkbox is wide enough and checked... for whatever reason
  100.     size_x > 40 && checked
  101. }
  102.  
  103. // only works on checkboxes that implement CustomCheckbox
  104. fn do_something_specifc<CheckboxT>(checkbox: &CheckboxT) -> &''static str
  105. where
  106.     CheckboxT: CustomCheckbox,
  107. {
  108.     let size_x = checkbox.size_x();
  109.     let checked = checkbox.checked();
  110.     let shape = checkbox.shape();
  111.  
  112.     //returns true if checkbox is wide enough and checked... for whatever reason
  113.     if shape == Shape::Circle && size_x > 40 && checked {
  114.         "this CustomCheckbox rocks"
  115.     } else {
  116.         "this CustomCheckbox is only a little awesome"
  117.     }
  118. }
  119.  
  120. // only works for our concrete implementation of CoolCheckbox
  121. fn do_coolcheckbox_stuff(coolbox: &CoolCheckbox) -> u32 {
  122.     coolbox.coolness_factor() + 5
  123. }
  124.  
  125. fn main() {
  126.     let mycheckbox = CoolCheckbox::new();
  127.  
  128.     println!("general: {}", do_general(&mycheckbox));
  129.     println!("checkbox: {}", do_something(&mycheckbox));
  130.     println!("customcheckbox: {}", do_something_specifc(&mycheckbox));
  131.     println!("coolcheckbox: {}", do_coolcheckbox_stuff(&mycheckbox));
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement