Skip to content

Commit

Permalink
attempt to fix a rendering bug, introduce a different rendering bug
Browse files Browse the repository at this point in the history
  • Loading branch information
edwloef committed Jan 4, 2025
1 parent 4990559 commit f094d60
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 36 deletions.
40 changes: 19 additions & 21 deletions src/dir.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::file::File;
use crate::{file::File, LINE_HEIGHT};
use iced::{
advanced::{
layout::{self, flex::Axis, Limits, Node},
Expand Down Expand Up @@ -26,7 +26,6 @@ const DIR_OPEN: &[u8] = include_bytes!("../assets/system-uicons--chevron-down.sv

struct State<Message> {
open: bool,
line_height: OnceCell<f32>,
dirs: OnceCell<Rc<[Dir<Message>]>>,
files: OnceCell<Rc<[File<Message>]>>,
}
Expand All @@ -35,7 +34,6 @@ impl<Message> Default for State<Message> {
fn default() -> Self {
Self {
open: false,
line_height: OnceCell::new(),
dirs: OnceCell::new(),
files: OnceCell::new(),
}
Expand Down Expand Up @@ -301,25 +299,21 @@ where
if !state.open {
return Node::new(Size::new(
limits.max().width,
*state
.line_height
.get_or_init(|| (renderer.default_size().0 * 1.3).ceil()),
renderer.default_size().0 * LINE_HEIGHT,
));
}

self.diff(tree);

let state = tree.state.downcast_ref::<State<Message>>();

let layout = layout::flex::resolve(
Axis::Vertical,
renderer,
limits,
Length::Fill,
Length::Shrink,
Padding::ZERO
.top(*state.line_height.get().unwrap())
.left(*state.line_height.get().unwrap()),
.top(renderer.default_size().0 * LINE_HEIGHT)
.left(renderer.default_size().0 * LINE_HEIGHT),
0.0,
Alignment::Start,
&self.get_children(),
Expand All @@ -345,8 +339,8 @@ where
if let Some(pos) = cursor.position() {
if event == Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left))
&& layout.bounds().contains(pos)
&& &cursor.position_in(layout.bounds()).unwrap().y
<= state.line_height.get().unwrap()
&& cursor.position_in(layout.bounds()).unwrap().y
<= renderer.default_size().0 * LINE_HEIGHT
{
state.open ^= true;
shell.invalidate_layout();
Expand Down Expand Up @@ -388,19 +382,21 @@ where
viewport: &Rectangle,
) {
let state = tree.state.downcast_ref::<State<Message>>();
let bounds = layout.bounds();
let Some(bounds) = layout.bounds().intersection(viewport) else {
return;
};

let background = Quad {
bounds: Rectangle::new(
bounds.position(),
Size::new(bounds.width, *state.line_height.get().unwrap()),
Size::new(bounds.width, renderer.default_size().0 * LINE_HEIGHT),
),
..Quad::default()
};
let background_color = cursor.position_in(bounds).map_or_else(
|| theme.extended_palette().primary.weak.color,
|pos| {
if &pos.y <= state.line_height.get().unwrap() {
if pos.y <= renderer.default_size().0 * LINE_HEIGHT {
theme.extended_palette().secondary.weak.color
} else {
theme.extended_palette().primary.weak.color
Expand All @@ -417,14 +413,16 @@ where
}))
.color(theme.extended_palette().secondary.base.text);

let offset = (state.line_height.get().unwrap() * 0.1).round();
let offset = (renderer.default_size().0 * LINE_HEIGHT * 0.1).round();
renderer.draw_svg(
icon,
Rectangle::new(
bounds.position() + Vector::new(-offset, -offset),
Size::new(
state.line_height.get().unwrap() + 2.0 * offset,
state.line_height.get().unwrap() + 2.0 * offset,
bounds
.width
.min(renderer.default_size().0.mul_add(LINE_HEIGHT, 2.0 * offset)),
renderer.default_size().0.mul_add(LINE_HEIGHT, 2.0 * offset),
),
),
);
Expand All @@ -443,7 +441,7 @@ where

renderer.fill_text(
name,
bounds.position() + Vector::new(*state.line_height.get().unwrap(), 0.0),
bounds.position() + Vector::new(renderer.default_size().0 * LINE_HEIGHT, 0.0),
theme.extended_palette().secondary.base.text,
bounds,
);
Expand All @@ -461,8 +459,8 @@ where
});

let offset = Vector::new(
state.line_height.get().unwrap() * 0.5 - 1.0,
state.line_height.get().unwrap() * 1.5 - 1.0,
(renderer.default_size().0 * LINE_HEIGHT).mul_add(0.5, -1.0),
(renderer.default_size().0 * LINE_HEIGHT).mul_add(1.5, -1.0),
);
let size = Size::new(2.0, bounds.size().height - offset.x - offset.y);
let line = Quad {
Expand Down
28 changes: 13 additions & 15 deletions src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,19 @@ use iced::{
Element, Event, Length, Rectangle, Renderer, Size, Theme, Vector,
};
use std::{
cell::{OnceCell, RefCell},
cell::RefCell,
fmt::{Debug, Formatter},
path::PathBuf,
rc::Rc,
};

use crate::LINE_HEIGHT;

const FILE: &[u8] = include_bytes!("../assets/system-uicons--document.svg");

#[derive(Default)]
struct State {
last_click: Option<Click>,
line_height: OnceCell<f32>,
}

#[expect(clippy::type_complexity)]
Expand Down Expand Up @@ -86,29 +87,26 @@ impl<Message> Widget<Message, Theme, Renderer> for File<Message> {
Size::new(Length::Fill, Length::Shrink)
}

fn layout(&self, tree: &mut Tree, renderer: &Renderer, limits: &Limits) -> Node {
let state = tree.state.downcast_ref::<State>();

fn layout(&self, _tree: &mut Tree, renderer: &Renderer, limits: &Limits) -> Node {
Node::new(Size::new(
limits.max().width,
*state
.line_height
.get_or_init(|| (renderer.default_size().0 * 1.3).ceil()),
renderer.default_size().0 * LINE_HEIGHT,
))
}

fn draw(
&self,
tree: &Tree,
_tree: &Tree,
renderer: &mut Renderer,
theme: &Theme,
_style: &Style,
layout: Layout<'_>,
cursor: Cursor,
_viewport: &Rectangle,
viewport: &Rectangle,
) {
let state = tree.state.downcast_ref::<State>();
let bounds = layout.bounds();
let Some(bounds) = layout.bounds().intersection(viewport) else {
return;
};

let background = Quad {
bounds,
Expand All @@ -129,8 +127,8 @@ impl<Message> Widget<Message, Theme, Renderer> for File<Message> {
Rectangle::new(
bounds.position(),
Size::new(
*state.line_height.get().unwrap(),
*state.line_height.get().unwrap(),
bounds.width.min(renderer.default_size().0 * LINE_HEIGHT),
renderer.default_size().0 * LINE_HEIGHT,
),
),
);
Expand All @@ -149,7 +147,7 @@ impl<Message> Widget<Message, Theme, Renderer> for File<Message> {

renderer.fill_text(
name,
bounds.position() + Vector::new(*state.line_height.get().unwrap(), 0.0),
bounds.position() + Vector::new(renderer.default_size().0 * LINE_HEIGHT, 0.0),
theme.extended_palette().secondary.base.text,
bounds,
);
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
mod dir;
mod file;

const LINE_HEIGHT: f32 = 1.3;

#[doc(inline)]
pub use dir::Dir as FileTree;

Expand Down

0 comments on commit f094d60

Please sign in to comment.