Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/app/extras.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,11 @@ fn now_ms() -> u64 {
fn encode_art(picker: &Picker, bytes: &[u8], size: (u16, u16)) -> Result<(Protocol, Option<[u8; 3]>), String> {
let img = image::load_from_memory(bytes).map_err(|e| e.to_string())?;
let tint = Some(theme::cover_tint(&img));
// Shrink with a cheap filter first: the cover is 640 px, the target is ~a few hundred.
// Fill the cells exactly (cropping the few pixels the cell grid isn't square by), so the
// cover meets its frame on every side instead of leaving a gap on one.
let fs = picker.font_size();
let (w, h) = (size.0 as u32 * fs.width as u32, size.1 as u32 * fs.height as u32);
let img = if img.width() > w * 2 || img.height() > h * 2 { img.thumbnail(w * 2, h * 2) } else { img };
let img = img.resize_to_fill(w.max(1), h.max(1), image::imageops::FilterType::Triangle);
picker
.new_protocol(img, Size::new(size.0, size.1), Resize::Fit(Some(FilterType::Triangle)))
.map(|p| (p, tint))
Expand Down Expand Up @@ -549,7 +550,10 @@ impl App {
np.set_protocol_type(p.protocol_type());
self.img_picker = Some(np);
if let Some(a) = self.art.as_mut() {
a.size = (0, 0); // no longer matches art_want, so refresh_media re-encodes (old cover stays up meanwhile)
a.size = (0, 0); // no longer matches art_want, so refresh_media re-encodes
// The old picture is sized in old pixels: it would spill over the borders (zoom out) or
// sit small in the corner (zoom in) and leave bits behind, so show none until the new one.
a.proto = None;
}
}

Expand Down
24 changes: 15 additions & 9 deletions src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,17 @@ pub fn draw(f: &mut Frame, app: &mut App) {
let side_w = if f.area().width < 70 && app.focus != Focus::Side { 0 } else { (f.area().width / 4).clamp(18, 32) };
let [side, main] = Layout::horizontal([Constraint::Length(side_w), Constraint::Min(20)]).areas(body);

// sidebar, with the cover under it when there's room
// sidebar, with the cover under it when there's room, framed like the panels so its
// edges line up with theirs (a picture can't start mid-cell the way a border line does)
let art_rows = art_rows(app, side);
app.art_want = art_rows.map(|r| (side.width, r));
app.art_want = art_rows.map(|r| (side.width - 2, r));
let (side, art_area) = match art_rows {
Some(r) => {
let [s, a] = Layout::vertical([Constraint::Min(6), Constraint::Length(r)]).areas(side);
(s, Some(a))
let [s, a] = Layout::vertical([Constraint::Min(6), Constraint::Length(r + 2)]).areas(side);
let frame = Block::bordered().border_type(BorderType::Rounded).border_style(Style::new().dim());
let inner = frame.inner(a);
f.render_widget(frame, a);
(s, Some(inner))
}
None => (side, None),
};
Expand Down Expand Up @@ -186,16 +190,18 @@ pub fn draw_splash(f: &mut Frame, playlists: &[String]) {
f.render_widget(Paragraph::new(" Connecting…").dim(), bottom);
}

/// Height (in rows) for a square cover the width of the sidebar, if the sidebar is tall
/// enough to still show a useful list above it.
/// Rows for a square cover inside a frame the width of the sidebar (rounded, so the cover
/// fills it with the least cropped), if the sidebar is tall enough to still show a useful
/// list above it.
fn art_rows(app: &App, side: Rect) -> Option<u16> {
let picker = app.img_picker.as_ref()?;
if !app.art_on || app.now.as_ref()?.art.is_none() {
if !app.art_on || app.now.as_ref()?.art.is_none() || side.width < 3 {
return None;
}
let fs = picker.font_size();
let rows = (side.width as u32 * fs.width as u32 / fs.height.max(1) as u32) as u16;
(rows >= 4 && side.height >= rows + 10).then_some(rows)
let (w, fh) = ((side.width - 2) as u32 * fs.width as u32, fs.height.max(1) as u32);
let rows = ((w + fh / 2) / fh) as u16;
(rows >= 4 && side.height >= rows + 12).then_some(rows)
}

fn draw_table(f: &mut Frame, app: &mut App, main: Rect) {
Expand Down
Loading