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.
 
 
 
 

137 lines
4.2 KiB

  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. use super::*;
  22. mod compact;
  23. pub use self::compact::*;
  24. mod thread;
  25. pub use self::thread::*;
  26. mod plain;
  27. pub use self::plain::*;
  28. #[derive(Debug)]
  29. pub enum Listing {
  30. Plain(PlainListing),
  31. Threaded(ThreadListing),
  32. Compact(CompactListing),
  33. }
  34. impl fmt::Display for Listing {
  35. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  36. match self {
  37. Listing::Compact(l) => write!(f, "{}", l),
  38. Listing::Plain(l) => write!(f, "{}", l),
  39. Listing::Threaded(l) => write!(f, "{}", l),
  40. }
  41. }
  42. }
  43. impl Default for Listing {
  44. fn default() -> Self {
  45. Listing::Threaded(Default::default())
  46. }
  47. }
  48. impl Component for Listing {
  49. fn draw(&mut self, grid: &mut CellBuffer, area: Area, context: &mut Context) {
  50. match self {
  51. Listing::Compact(l) => l.draw(grid, area, context),
  52. Listing::Plain(l) => l.draw(grid, area, context),
  53. Listing::Threaded(l) => l.draw(grid, area, context),
  54. }
  55. }
  56. fn process_event(&mut self, event: &mut UIEvent, context: &mut Context) -> bool {
  57. if match self {
  58. Listing::Plain(l) => l.process_event(event, context),
  59. Listing::Compact(l) => l.process_event(event, context),
  60. Listing::Threaded(l) => l.process_event(event, context),
  61. } {
  62. return true;
  63. }
  64. match event.event_type {
  65. UIEventType::Resize => self.set_dirty(),
  66. UIEventType::Action(ref action) => match action {
  67. Action::Listing(ListingAction::SetPlain) => {
  68. if let Listing::Plain(_) = self {
  69. return true;
  70. }
  71. *self = Listing::Plain(PlainListing::default());
  72. return true;
  73. }
  74. Action::Listing(ListingAction::SetThreaded) => {
  75. if let Listing::Threaded(_) = self {
  76. return true;
  77. }
  78. self.set_dirty();
  79. *self = Listing::Threaded(ThreadListing::default());
  80. return true;
  81. }
  82. Action::Listing(ListingAction::SetCompact) => {
  83. if let Listing::Compact(_) = self {
  84. return true;
  85. }
  86. *self = Listing::Compact(CompactListing::default());
  87. return true;
  88. }
  89. _ => {}
  90. },
  91. _ => {}
  92. }
  93. false
  94. }
  95. fn is_dirty(&self) -> bool {
  96. match self {
  97. Listing::Compact(l) => l.is_dirty(),
  98. Listing::Plain(l) => l.is_dirty(),
  99. Listing::Threaded(l) => l.is_dirty(),
  100. }
  101. }
  102. fn set_dirty(&mut self) {
  103. match self {
  104. Listing::Compact(l) => l.set_dirty(),
  105. Listing::Plain(l) => l.set_dirty(),
  106. Listing::Threaded(l) => l.set_dirty(),
  107. }
  108. }
  109. fn get_shortcuts(&self, context: &Context) -> ShortcutMap {
  110. match self {
  111. Listing::Compact(l) => l.get_shortcuts(context),
  112. Listing::Plain(l) => l.get_shortcuts(context),
  113. Listing::Threaded(l) => l.get_shortcuts(context),
  114. }
  115. }
  116. }
  117. impl From<IndexStyle> for Listing {
  118. fn from(index_style: IndexStyle) -> Self {
  119. match index_style {
  120. IndexStyle::Plain => Listing::Plain(Default::default()),
  121. IndexStyle::Threaded => Listing::Threaded(Default::default()),
  122. IndexStyle::Compact => Listing::Compact(Default::default()),
  123. }
  124. }
  125. }