|
| 1 | +use crate::color::{BLACK, WHITE}; |
| 2 | +use crate::context::Context; |
| 3 | +use crate::device::CURRENT_DEVICE; |
| 4 | +use crate::font::{font_from_style, Fonts, NORMAL_STYLE}; |
| 5 | +use crate::framebuffer::Framebuffer; |
| 6 | +use crate::geom::{BorderSpec, CornerSpec, Rectangle}; |
| 7 | +use crate::gesture::GestureEvent; |
| 8 | +use crate::unit::scale_by_dpi; |
| 9 | +use crate::view::button::Button; |
| 10 | +use crate::view::icon::Icon; |
| 11 | +use crate::view::label::Label; |
| 12 | +use crate::view::{Align, Bus, Event, Hub, Id, RenderQueue, View, ViewId, ID_FEEDER}; |
| 13 | +use crate::view::{BORDER_RADIUS_MEDIUM, SMALL_BAR_HEIGHT, THICKNESS_LARGE}; |
| 14 | + |
| 15 | +const LABEL_QUEUE: &str = "Queue"; |
| 16 | +const LABEL_ADD_ARTICLE: &str = "Add article"; |
| 17 | + |
| 18 | +pub struct ExternalLink { |
| 19 | + id: Id, |
| 20 | + rect: Rectangle, |
| 21 | + children: Vec<Box<dyn View>>, |
| 22 | +} |
| 23 | + |
| 24 | +impl ExternalLink { |
| 25 | + pub fn new(context: &mut Context, link: String) -> ExternalLink { |
| 26 | + let id = ID_FEEDER.next(); |
| 27 | + let fonts = &mut context.fonts; |
| 28 | + let mut children = Vec::new(); |
| 29 | + let dpi = CURRENT_DEVICE.dpi; |
| 30 | + let (width, height) = context.display.dims; |
| 31 | + let small_height = scale_by_dpi(SMALL_BAR_HEIGHT, dpi) as i32; |
| 32 | + let thickness = scale_by_dpi(THICKNESS_LARGE, dpi) as i32; |
| 33 | + let border_radius = scale_by_dpi(BORDER_RADIUS_MEDIUM, dpi) as i32; |
| 34 | + |
| 35 | + let (x_height, padding) = { |
| 36 | + let font = font_from_style(fonts, &NORMAL_STYLE, dpi); |
| 37 | + (font.x_heights.0 as i32, font.em() as i32) |
| 38 | + }; |
| 39 | + |
| 40 | + let window_width = width as i32 - 2 * padding; |
| 41 | + let window_height = small_height * 4 + 2 * padding; |
| 42 | + |
| 43 | + let dx = (width as i32 - window_width) / 2; |
| 44 | + let dy = (height as i32 - window_height) / 4; |
| 45 | + |
| 46 | + let rect = rect![dx, dy, dx + window_width, dy + window_height]; |
| 47 | + |
| 48 | + let close_icon = Icon::new( |
| 49 | + "close", |
| 50 | + rect![ |
| 51 | + rect.max.x - small_height, |
| 52 | + rect.min.y + thickness, |
| 53 | + rect.max.x - thickness, |
| 54 | + rect.min.y + small_height |
| 55 | + ], |
| 56 | + Event::Close(ViewId::ExternalLink), |
| 57 | + ) |
| 58 | + .corners(Some(CornerSpec::Detailed { |
| 59 | + north_west: 0, |
| 60 | + north_east: border_radius - thickness, |
| 61 | + south_east: 0, |
| 62 | + south_west: 0, |
| 63 | + })); |
| 64 | + children.push(Box::new(close_icon) as Box<dyn View>); |
| 65 | + |
| 66 | + let label = Label::new( |
| 67 | + rect![ |
| 68 | + rect.min.x + small_height, |
| 69 | + rect.min.y + thickness, |
| 70 | + rect.max.x - small_height, |
| 71 | + rect.min.y + small_height |
| 72 | + ], |
| 73 | + "External link".to_string(), |
| 74 | + Align::Center, |
| 75 | + ); |
| 76 | + children.push(Box::new(label) as Box<dyn View>); |
| 77 | + |
| 78 | + // TODO: wrap the URL if needed. |
| 79 | + let link_label = Label::new( |
| 80 | + rect![ |
| 81 | + rect.min.x + small_height, |
| 82 | + rect.min.y + thickness + small_height, |
| 83 | + rect.max.x - small_height, |
| 84 | + rect.min.y + 2 * small_height |
| 85 | + ], |
| 86 | + link.clone(), |
| 87 | + Align::Left(0), |
| 88 | + ); |
| 89 | + children.push(Box::new(link_label) as Box<dyn View>); |
| 90 | + |
| 91 | + let max_button_label_width = { |
| 92 | + let font = font_from_style(fonts, &NORMAL_STYLE, dpi); |
| 93 | + [LABEL_QUEUE, LABEL_ADD_ARTICLE] |
| 94 | + .iter() |
| 95 | + .map(|t| font.plan(t, None, None).width) |
| 96 | + .max() |
| 97 | + .unwrap() as i32 |
| 98 | + }; |
| 99 | + |
| 100 | + let button_y = rect.min.y + small_height * 3; |
| 101 | + let button_height = 4 * x_height; |
| 102 | + |
| 103 | + let button_queue = Button::new( |
| 104 | + rect![ |
| 105 | + rect.min.x + 3 * padding, |
| 106 | + button_y + small_height - button_height, |
| 107 | + rect.min.x + 5 * padding + max_button_label_width, |
| 108 | + button_y + small_height |
| 109 | + ], |
| 110 | + Event::QueueLink(link.clone()), |
| 111 | + LABEL_QUEUE.to_string(), |
| 112 | + ) |
| 113 | + .disabled(context.settings.external_urls_queue.is_none()); |
| 114 | + children.push(Box::new(button_queue) as Box<dyn View>); |
| 115 | + |
| 116 | + let button_add_article = Button::new( |
| 117 | + rect![ |
| 118 | + rect.max.x - 5 * padding - max_button_label_width, |
| 119 | + button_y + small_height - button_height, |
| 120 | + rect.max.x - 3 * padding, |
| 121 | + button_y + small_height |
| 122 | + ], |
| 123 | + Event::AddArticleLink(link), |
| 124 | + LABEL_ADD_ARTICLE.to_string(), |
| 125 | + ) |
| 126 | + .disabled(context.settings.article_auth.api == ""); |
| 127 | + children.push(Box::new(button_add_article) as Box<dyn View>); |
| 128 | + |
| 129 | + ExternalLink { id, rect, children } |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +impl View for ExternalLink { |
| 134 | + fn handle_event( |
| 135 | + &mut self, |
| 136 | + evt: &Event, |
| 137 | + _hub: &Hub, |
| 138 | + bus: &mut Bus, |
| 139 | + _rq: &mut RenderQueue, |
| 140 | + _context: &mut Context, |
| 141 | + ) -> bool { |
| 142 | + match *evt { |
| 143 | + Event::Gesture(GestureEvent::Tap(center)) if !self.rect.includes(center) => { |
| 144 | + bus.push_back(Event::Close(ViewId::ExternalLink)); |
| 145 | + true |
| 146 | + } |
| 147 | + Event::Gesture(..) => true, |
| 148 | + _ => false, |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + fn render(&self, fb: &mut dyn Framebuffer, _rect: Rectangle, _fonts: &mut Fonts) { |
| 153 | + let dpi = CURRENT_DEVICE.dpi; |
| 154 | + |
| 155 | + let border_radius = scale_by_dpi(BORDER_RADIUS_MEDIUM, dpi) as i32; |
| 156 | + let border_thickness = scale_by_dpi(THICKNESS_LARGE, dpi) as u16; |
| 157 | + |
| 158 | + fb.draw_rounded_rectangle_with_border( |
| 159 | + &self.rect, |
| 160 | + &CornerSpec::Uniform(border_radius), |
| 161 | + &BorderSpec { |
| 162 | + thickness: border_thickness, |
| 163 | + color: BLACK, |
| 164 | + }, |
| 165 | + &WHITE, |
| 166 | + ); |
| 167 | + } |
| 168 | + |
| 169 | + fn is_background(&self) -> bool { |
| 170 | + true |
| 171 | + } |
| 172 | + |
| 173 | + fn rect(&self) -> &Rectangle { |
| 174 | + &self.rect |
| 175 | + } |
| 176 | + |
| 177 | + fn rect_mut(&mut self) -> &mut Rectangle { |
| 178 | + &mut self.rect |
| 179 | + } |
| 180 | + |
| 181 | + fn children(&self) -> &Vec<Box<dyn View>> { |
| 182 | + &self.children |
| 183 | + } |
| 184 | + |
| 185 | + fn children_mut(&mut self) -> &mut Vec<Box<dyn View>> { |
| 186 | + &mut self.children |
| 187 | + } |
| 188 | + |
| 189 | + fn id(&self) -> Id { |
| 190 | + self.id |
| 191 | + } |
| 192 | +} |
0 commit comments