Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use taffy for tables #129

Merged
merged 15 commits into from
May 31, 2023
286 changes: 107 additions & 179 deletions Cargo.lock

Large diffs are not rendered by default.

9 changes: 5 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ html5ever = "0.26.0"
image = "0.24.6"
clap = { version = "4.2.7", features = ["cargo"] }
copypasta = { version = "0.8.2", default-features = false }
resvg = "0.33.0"
usvg = "0.33.0"
tiny-skia = "0.9.0"
resvg = "0.34.1"
usvg = "0.34.1"
tiny-skia = "0.10.0"
anyhow = "1.0.71"
dirs = "5.0.1"
serde = { version = "1.0.162", features = ["derive"] }
Expand All @@ -55,6 +55,7 @@ indexmap = { version = "1.9.3", features = ["serde"] }
html-escape = "0.2.13"
fxhash = "0.2.1"
twox-hash = "1.6.3"
taffy = { git = "https://github.com/DioxusLabs/taffy", rev = "d338f3731da519d182bbc074de46382984ab7c4a" }
syntect = "5.0.0"

[dependencies.glyphon]
Expand All @@ -77,4 +78,4 @@ lto = true

[dev-dependencies]
insta = "1.29.0"
pretty_assertions = "1.3.0"
pretty_assertions = "1.3.0"
16 changes: 8 additions & 8 deletions src/image/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::io;
use std::path::PathBuf;
use std::sync::{Arc, Mutex};
use std::time::Instant;
use usvg::{Size as UsvgSize, TreeParsing, TreeTextToPath};
use usvg::{TreeParsing, TreeTextToPath};
use wgpu::util::DeviceExt;
use wgpu::{BindGroup, Device, TextureFormat};
use winit::event_loop::EventLoopProxy;
Expand Down Expand Up @@ -196,18 +196,18 @@ impl Image {
fontdb.load_system_fonts();
let mut tree = usvg::Tree::from_data(&image_data, &opt).unwrap();
tree.size = tree.size.scale_to(
UsvgSize::new(
tree.size.width() * hidpi_scale as f64,
tree.size.height() * hidpi_scale as f64,
tiny_skia::Size::from_wh(
tree.size.width() * hidpi_scale,
tree.size.height() * hidpi_scale,
)
.unwrap(),
);
tree.convert_text(&fontdb);
let rtree = resvg::Tree::from_usvg(&tree);
let pixmap_size = resvg::IntSize::from_usvg(rtree.size);
let mut pixmap = tiny_skia::Pixmap::new(pixmap_size.width(), pixmap_size.height())
.context("Couldn't create svg pixmap")
.unwrap();
let mut pixmap =
tiny_skia::Pixmap::new(rtree.size.width() as u32, rtree.size.height() as u32)
.context("Couldn't create svg pixmap")
.unwrap();
rtree.render(tiny_skia::Transform::default(), &mut pixmap.as_mut());
ImageData::new(
ImageBuffer::from_raw(pixmap.width(), pixmap.height(), pixmap.data().into())
Expand Down
20 changes: 16 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,16 @@ use positioner::Spacer;
use positioner::DEFAULT_MARGIN;
use positioner::DEFAULT_PADDING;
use renderer::Renderer;
use taffy::Taffy;
use text::TextBox;
use text::TextSystem;
use utils::{ImageCache, Point, Rect, Size};

use anyhow::Context;
#[cfg(feature = "x11")]
use copypasta::{ClipboardContext, ClipboardProvider};
#[cfg(feature = "wayland")]
use copypasta::{nop_clipboard::NopClipboardContext as ClipboardContext, ClipboardProvider};
#[cfg(feature = "x11")]
use copypasta::{ClipboardContext, ClipboardProvider};
use notify::{RecommendedWatcher, RecursiveMode, Watcher};

use winit::event::ModifiersState;
Expand Down Expand Up @@ -405,6 +406,7 @@ impl Inlyne {

let cursor_icon = if let Some(hoverable) = Self::find_hoverable(
&mut self.renderer.text_system,
&mut self.renderer.positioner.taffy,
&self.elements,
loc,
screen_size,
Expand Down Expand Up @@ -493,6 +495,7 @@ impl Inlyne {
let screen_size = self.renderer.screen_size();
if let Some(hoverable) = Self::find_hoverable(
&mut self.renderer.text_system,
&mut self.renderer.positioner.taffy,
&self.elements,
last_loc,
screen_size,
Expand Down Expand Up @@ -709,6 +712,7 @@ impl Inlyne {

fn find_hoverable<'a>(
text_system: &mut TextSystem,
taffy: &mut Taffy,
elements: &'a [Positioned<Element>],
loc: Point,
screen_size: Size,
Expand Down Expand Up @@ -742,6 +746,7 @@ impl Inlyne {
table
.find_hoverable(
text_system,
taffy,
loc,
bounds.pos,
screen_pos(screen_size, bounds.pos.0),
Expand All @@ -752,7 +757,7 @@ impl Inlyne {
Element::Image(image) => Some(Hoverable::Image(image)),
Element::Spacer(_) => unreachable!("Spacers are filtered"),
Element::Row(row) => {
Self::find_hoverable(text_system, &row.elements, loc, screen_size, zoom)
Self::find_hoverable(text_system, taffy, &row.elements, loc, screen_size, zoom)
}
Element::Section(section) => {
if let Some(ref summary) = *section.summary {
Expand All @@ -763,7 +768,14 @@ impl Inlyne {
}
}
if !*section.hidden.borrow() {
Self::find_hoverable(text_system, &section.elements, loc, screen_size, zoom)
Self::find_hoverable(
text_system,
taffy,
&section.elements,
loc,
screen_size,
zoom,
)
} else {
None
}
Expand Down
42 changes: 15 additions & 27 deletions src/positioner.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::{cell::RefCell, collections::HashMap};

use anyhow::Context;
use taffy::Taffy;

use crate::{
table::{TABLE_COL_GAP, TABLE_ROW_GAP},
text::{TextBox, TextSystem},
utils::{Align, Point, Rect, Size},
Element,
Expand Down Expand Up @@ -44,16 +44,20 @@ pub struct Positioner {
pub hidpi_scale: f32,
pub page_width: f32,
pub anchors: HashMap<String, f32>,
pub taffy: Taffy,
}

impl Positioner {
pub fn new(screen_size: Size, hidpi_scale: f32, page_width: f32) -> Self {
let mut taffy = Taffy::new();
taffy.disable_rounding();
Self {
reserved_height: DEFAULT_PADDING * hidpi_scale,
hidpi_scale,
page_width,
screen_size,
anchors: HashMap::new(),
taffy,
}
}

Expand Down Expand Up @@ -107,34 +111,18 @@ impl Positioner {
}
Element::Table(table) => {
let pos = (DEFAULT_MARGIN + centering, self.reserved_height);
let width = table
.column_widths(
text_system,
(
self.screen_size.0 - pos.0 - DEFAULT_MARGIN - centering,
f32::INFINITY,
),
zoom,
)
.iter()
.fold(0., |acc, x| acc + x);
let height = table
.row_heights(
text_system,
(
self.screen_size.0 - pos.0 - DEFAULT_MARGIN - centering,
f32::INFINITY,
),
zoom,
)
.iter()
.fold(0., |acc, x| acc + x);
Rect::new(
pos,
let layout = table.layout(
text_system,
&mut self.taffy,
(
width * (TABLE_COL_GAP * table.headers.len() as f32),
height + (TABLE_ROW_GAP * (table.rows.len() + 1) as f32),
self.screen_size.0 - pos.0 - DEFAULT_MARGIN - centering,
f32::INFINITY,
),
zoom,
)?;
Rect::new(
(DEFAULT_MARGIN + centering, self.reserved_height),
layout.size,
)
}
Element::Row(row) => {
Expand Down
Loading