You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

129 lines
3.4KB

  1. /*
  2. * meli - ui crate.
  3. *
  4. * Copyright 2017-2018 Manos Pitsidianakis
  5. *
  6. * This file is part of meli.
  7. *
  8. * meli is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * meli is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with meli. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. /*! Entities that handle Mail specific functions.
  22. */
  23. use super::*;
  24. pub mod index;
  25. pub use self::index::*;
  26. #[derive(Debug)]
  27. struct MenuEntry {
  28. name: String,
  29. subentries: Vec<MenuEntry>,
  30. index: Index,
  31. }
  32. #[derive(Debug)]
  33. pub struct Indexer {
  34. entries: Vec<MenuEntry>,
  35. dirty: bool,
  36. cursor: Vec<usize>,
  37. }
  38. impl fmt::Display for Indexer {
  39. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  40. // TODO display subject/info
  41. write!(f, "index")
  42. }
  43. }
  44. impl Default for Indexer {
  45. fn default() -> Self {
  46. Indexer {
  47. entries: Vec::with_capacity(8),
  48. dirty: true,
  49. cursor: Vec::with_capacity(8),
  50. }
  51. }
  52. }
  53. impl Indexer {
  54. fn draw_menu(&mut self, _grid: &mut CellBuffer, _area: Area, _context: &mut Context) {}
  55. }
  56. impl Component for Indexer {
  57. fn draw(&mut self, grid: &mut CellBuffer, area: Area, context: &mut Context) {
  58. if !self.is_dirty() {
  59. return;
  60. }
  61. clear_area(grid, area);
  62. let upper_left = upper_left!(area);
  63. let bottom_right = bottom_right!(area);
  64. let total_cols = get_x(bottom_right) - get_x(upper_left);
  65. let index_entity_width = (30 * total_cols) / 100;
  66. let mid = get_x(bottom_right) - index_entity_width;
  67. for i in get_y(upper_left)..=get_y(bottom_right) {
  68. set_and_join_box(grid, (mid, i), VERT_BOUNDARY);
  69. }
  70. let left_menu_area = (upper_left, (set_x(bottom_right, mid - 1)));
  71. let right_index_area = (set_x(upper_left, mid + 1), bottom_right);
  72. self.draw_menu(grid, left_menu_area, context);
  73. self.entries[self.cursor[0]]
  74. .index
  75. .draw(grid, right_index_area, context);
  76. self.dirty = false;
  77. context.dirty_areas.push_back(area);
  78. }
  79. fn process_event(&mut self, event: &mut UIEvent, _context: &mut Context) -> bool {
  80. if !self.entries[self.cursor[0]]
  81. .index
  82. .process_event(event, _context)
  83. {
  84. for i in 0..self.entries.len() {
  85. if i == self.cursor[0] {
  86. continue;
  87. }
  88. self.entries[i].index.process_event(event, _context);
  89. }
  90. }
  91. match event.event_type {
  92. UIEventType::RefreshMailbox(_) => {
  93. self.dirty = true;
  94. }
  95. UIEventType::ChangeMode(UIMode::Normal) => {
  96. self.dirty = true;
  97. }
  98. UIEventType::Resize => {
  99. self.dirty = true;
  100. }
  101. _ => {}
  102. }
  103. false
  104. }
  105. fn is_dirty(&self) -> bool {
  106. self.dirty
  107. }
  108. fn set_dirty(&mut self) {
  109. self.dirty = true;
  110. }
  111. }