-
-
Notifications
You must be signed in to change notification settings - Fork 536
feat: block shadows #1893
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
base: main
Are you sure you want to change the base?
feat: block shadows #1893
Conversation
Codecov Report❌ Patch coverage is
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. 🚀 New features to boost your workflow:
|
|
I've pushed a very initial, dirty PoC. Works on blocks only - this will change a lot. |
|
I like the shade options, looks like magic :D |
j-g00da
left a comment
There was a problem hiding this 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)
| /// 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)) | ||
| }; | ||
| } |
There was a problem hiding this comment.
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.
|
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 |
orhun
left a comment
There was a problem hiding this 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)] |
There was a problem hiding this comment.
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);
}
}
Resolves #1892