Skip to content

Conversation

@j-g00da
Copy link
Member

@j-g00da j-g00da commented Jun 3, 2025

Resolves #1892

Image Image Image Image

@codecov
Copy link

codecov bot commented Jun 3, 2025

Codecov Report

❌ Patch coverage is 10.93750% with 57 lines in your changes missing coverage. Please review.
✅ Project coverage is 92.6%. Comparing base (cba5cca) to head (cc4c0de).
⚠️ Report is 85 commits behind head on main.

Files with missing lines Patch % Lines
ratatui-widgets/src/block/shadow.rs 0.0% 52 Missing ⚠️
ratatui-widgets/src/block.rs 58.3% 5 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##            main   #1893     +/-   ##
=======================================
- Coverage   92.9%   92.6%   -0.4%     
=======================================
  Files         79      80      +1     
  Lines      16605   16669     +64     
=======================================
+ Hits       15437   15444      +7     
- Misses      1168    1225     +57     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@j-g00da
Copy link
Member Author

j-g00da commented Jun 5, 2025

I've pushed a very initial, dirty PoC. Works on blocks only - this will change a lot.

@orhun
Copy link
Member

orhun commented Jun 5, 2025

I like the shade options, looks like magic :D

Copy link
Member Author

@j-g00da j-g00da left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left some comments, WDYT? @joshka @orhun
Related: #1892 (comment)

Comment on lines +96 to +132
/// TODO: docs
#[macro_export]
macro_rules! cell_filter {
// base case
($x:ident, $y:ident, $shadow:ident, $base:ident, $buf:ident, $body:block) => {
for $y in $shadow.top()..$shadow.bottom() {
for $x in $shadow.left()..$shadow.right() {
if $base.contains(Position { $x, $y }) {
continue;
}
$body
}
}
};

// function
($(#[$attr:meta])* $vis:vis fn $name:ident($x:ident: u16, $y:ident: u16, $buf:ident: &mut Buffer) $body:block) => {
$(#[$attr])* $vis fn $name (shadow_area: Rect, base_area: Rect, $buf: &mut Buffer) {
$crate::cell_filter!($x, $y, shadow_area, base_area, $buf, $body)
}
};

// closure
(|$x:ident: u16, $y:ident: u16, $buf:ident: &mut Buffer| $body:block) => {
|shadow_area: Rect, base_area: Rect, $buf: &mut Buffer| {
$crate::cell_filter!($x, $y, shadow_area, base_area, $buf, $body)
}
};
(|$x:ident: u16, $y:ident: u16, $buf:ident: &mut Buffer| $body:expr) => {
$crate::cell_filter!(|$x: u16, $y: u16, $buf: &mut Buffer| { $body; })
};

// fill
($s:expr) => {
$crate::cell_filter!(|x: u16, y: u16, buf: &mut Buffer| buf[(x, y)].set_symbol($s))
};
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is WIP and can be improved, but works. Just wanted to show this PoC.

@j-g00da j-g00da added this to the v0.31.0 milestone Jun 28, 2025
@orhun
Copy link
Member

orhun commented Aug 2, 2025

This is looking pretty good tbh, should we finalize it and include it in the release?

@j-g00da
Copy link
Member Author

j-g00da commented Aug 3, 2025

This is looking pretty good tbh, should we finalize it and include it in the release?

Right now it's just a PoC, I would have to clean it up, move stuff around and add some tests and docs but can be done in 1 week probably.

cc: @joshka

Copy link
Member

@orhun orhun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just had a look at this PR while trying out RustRover and did a small thing

use ratatui_core::widgets::Widget;

/// TODO: docs
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implementing PartialEq gives a lint warning:

--> ratatui-widgets/src/block/shadow.rs:31:5
|
29 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
| --------- in this derive macro expansion
30 | pub struct Shadow {
31 | filter: fn(Rect, Rect, &mut Buffer),
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: the address of the same function can vary between different codegen units
= note: furthermore, different functions could have the same address after being merged together
= note: for more information visit https://doc.rust-lang.org/nightly/core/ptr/fn.fn_addr_eq.html

I think we need to implement it separately without the filter function:

// Define equality without comparing the function pointer.
impl PartialEq for Shadow {
    fn eq(&self, other: &Self) -> bool {
        self.style == other.style && self.offset == other.offset
    }
}

// Hash only stable fields, without the function pointer.
impl Hash for Shadow {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.style.hash(state);
        self.offset.hash(state);
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Block shadows

3 participants