| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
pub mod chrome; |
| 37 |
mod node; |
| 38 |
mod shell; |
| 39 |
|
| 40 |
#[cfg(test)] |
| 41 |
mod tests; |
| 42 |
|
| 43 |
pub use crate::shell::{Parts, Shell}; |
| 44 |
pub use makeover_webview::Emit; |
| 45 |
|
| 46 |
use std::collections::HashMap; |
| 47 |
|
| 48 |
use makeover_layout::Arrangement; |
| 49 |
use quasi_http::Serves; |
| 50 |
use quasi_router::{Node, Screen}; |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
#[derive(Debug, Clone, Default)] |
| 67 |
pub struct Webview { |
| 68 |
|
| 69 |
pub shell: Shell, |
| 70 |
|
| 71 |
|
| 72 |
pub emit: Emit, |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
pub fills: HashMap<String, String>, |
| 89 |
} |
| 90 |
|
| 91 |
impl Webview { |
| 92 |
|
| 93 |
#[must_use] |
| 94 |
pub fn new() -> Self { |
| 95 |
Self::default() |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
#[must_use] |
| 100 |
pub fn under(prefix: &str) -> Self { |
| 101 |
Self { |
| 102 |
shell: Shell::under(prefix), |
| 103 |
..Self::default() |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
#[must_use] |
| 109 |
pub fn with_shell(mut self, shell: Shell) -> Self { |
| 110 |
self.shell = shell; |
| 111 |
self |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
#[must_use] |
| 116 |
pub fn with_emit(mut self, emit: Emit) -> Self { |
| 117 |
self.emit = emit; |
| 118 |
self |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
#[must_use] |
| 127 |
pub fn with_fill(mut self, slot_id: impl Into<String>, markup: impl Into<String>) -> Self { |
| 128 |
self.fills.insert(slot_id.into(), markup.into()); |
| 129 |
self |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
fn arrangement_class(arrangement: Arrangement) -> &'static str { |
| 138 |
match arrangement { |
| 139 |
Arrangement::ListDetail { tabbed: false, .. } => "list-detail", |
| 140 |
Arrangement::ListDetail { tabbed: true, .. } => "list-detail-tabbed", |
| 141 |
Arrangement::SidebarContent { .. } => "sidebar-content", |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
fn share_style(arrangement: Arrangement) -> String { |
| 157 |
let first = u16::from(arrangement.share().as_percent()); |
| 158 |
format!( |
| 159 |
" style=\"--region-share:{first}fr;--region-rest:{}fr\"", |
| 160 |
100 - first |
| 161 |
) |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
impl Serves for Webview { |
| 166 |
fn screen(&self, screen: &Screen) -> String { |
| 167 |
let mut out = String::with_capacity(1024); |
| 168 |
self.shell |
| 169 |
.open(&screen.title, Some(&screen.discovery), &mut out); |
| 170 |
|
| 171 |
out.push_str("<main class=\""); |
| 172 |
out.push_str(&makeover_webview::class( |
| 173 |
Self::arrangement_class(screen.arrangement), |
| 174 |
&self.emit, |
| 175 |
)); |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
out.push(' '); |
| 181 |
out.push_str(&makeover_webview::class( |
| 182 |
&format!("measure-{}", screen.measure.as_str()), |
| 183 |
&self.emit, |
| 184 |
)); |
| 185 |
out.push('"'); |
| 186 |
out.push_str(&Self::share_style(screen.arrangement)); |
| 187 |
out.push('>'); |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
if !screen.notices.is_empty() { |
| 194 |
out.push_str("<div class=\""); |
| 195 |
out.push_str(&makeover_webview::class("notices", &self.emit)); |
| 196 |
out.push_str("\">"); |
| 197 |
for notice in &screen.notices { |
| 198 |
node::node_html( |
| 199 |
notice, |
| 200 |
self.shell.morphs(), |
| 201 |
&self.emit, |
| 202 |
&self.fills, |
| 203 |
&mut out, |
| 204 |
); |
| 205 |
} |
| 206 |
out.push_str("</div>"); |
| 207 |
} |
| 208 |
|
| 209 |
for slot in &screen.slots { |
| 210 |
node::slot_html(slot, self.shell.morphs(), &self.emit, &self.fills, &mut out); |
| 211 |
} |
| 212 |
|
| 213 |
out.push_str("</main>"); |
| 214 |
|
| 215 |
|
| 216 |
crate::chrome::chrome_html(&self.shell.chrome, self.shell.morphs(), &mut out); |
| 217 |
Shell::close(&mut out); |
| 218 |
out |
| 219 |
} |
| 220 |
|
| 221 |
fn overlay(&self, screen: &Screen) -> String { |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
let mut out = String::with_capacity(512); |
| 227 |
for notice in &screen.notices { |
| 228 |
node::node_html( |
| 229 |
notice, |
| 230 |
self.shell.morphs(), |
| 231 |
&self.emit, |
| 232 |
&self.fills, |
| 233 |
&mut out, |
| 234 |
); |
| 235 |
} |
| 236 |
for slot in &screen.slots { |
| 237 |
node::slot_html(slot, self.shell.morphs(), &self.emit, &self.fills, &mut out); |
| 238 |
} |
| 239 |
out |
| 240 |
} |
| 241 |
|
| 242 |
fn overlay_target(&self) -> Option<&str> { |
| 243 |
Some(crate::chrome::OVERLAY_ID) |
| 244 |
} |
| 245 |
|
| 246 |
fn fragment(&self, node: &Node) -> String { |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
let mut out = String::with_capacity(256); |
| 251 |
node::node_html(node, self.shell.morphs(), &self.emit, &self.fills, &mut out); |
| 252 |
out |
| 253 |
} |
| 254 |
|
| 255 |
fn invalidated(&self, region: &str, node: &Node) -> String { |
| 256 |
|
| 257 |
|
| 258 |
let mut out = String::with_capacity(256); |
| 259 |
node::oob_html( |
| 260 |
region, |
| 261 |
node, |
| 262 |
self.shell.morphs(), |
| 263 |
&self.emit, |
| 264 |
&self.fills, |
| 265 |
&mut out, |
| 266 |
); |
| 267 |
out |
| 268 |
} |
| 269 |
} |
| 270 |
|