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

Macros: split derive mode into a new module and make it more useful #460

Merged
merged 6 commits into from
Dec 14, 2024
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
70 changes: 2 additions & 68 deletions crates/kas-core/src/hidden.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

use crate::classes::HasStr;
use crate::event::{ConfigCx, Event, EventCx, IsUsed};
use crate::geom::{Coord, Offset, Rect};
use crate::geom::Rect;
use crate::layout::{Align, AlignHints, AxisInfo, SizeRules};
use crate::theme::{DrawCx, SizeCx, Text, TextClass};
use crate::{Events, Id, Layout, NavAdvance, Node, Widget};
Expand Down Expand Up @@ -78,6 +78,7 @@ impl_scope! {
#[autoimpl(Deref, DerefMut using self.inner)]
#[autoimpl(class_traits using self.inner where W: trait)]
#[derive(Clone, Default)]
#[widget{ derive = self.inner; }]
pub struct MapAny<A, W: Widget<Data = ()>> {
_a: std::marker::PhantomData<A>,
pub inner: W,
Expand All @@ -93,73 +94,6 @@ impl_scope! {
}
}

// We don't use #[widget] here. This is not supported outside of Kas!
impl Layout for Self {
#[inline]
fn as_layout(&self) -> &dyn Layout {
self
}

#[inline]
fn id_ref(&self) -> &Id {
self.inner.id_ref()
}

#[inline]
fn rect(&self) -> Rect {
self.inner.rect()
}

#[inline]
fn widget_name(&self) -> &'static str {
"MapAny"
}

#[inline]
fn num_children(&self) -> usize {
self.inner.num_children()
}
#[inline]
fn get_child(&self, index: usize) -> Option<&dyn Layout> {
self.inner.get_child(index)
}

#[inline]
fn find_child_index(&self, id: &Id) -> Option<usize> {
self.inner.find_child_index(id)
}

#[inline]
fn size_rules(&mut self, sizer: SizeCx, axis: AxisInfo) -> SizeRules {
self.inner.size_rules(sizer, axis)
}

#[inline]
fn set_rect(&mut self, cx: &mut ConfigCx, rect: Rect, hints: AlignHints) {
self.inner.set_rect(cx, rect, hints);
}

#[inline]
fn nav_next(&self, reverse: bool, from: Option<usize>) -> Option<usize> {
self.inner.nav_next(reverse, from)
}

#[inline]
fn translation(&self) -> Offset {
self.inner.translation()
}

#[inline]
fn find_id(&mut self, coord: Coord) -> Option<Id> {
self.inner.find_id(coord)
}

#[inline]
fn draw(&mut self, draw: DrawCx) {
self.inner.draw(draw);
}
}

impl Widget for Self {
type Data = A;

Expand Down
1 change: 1 addition & 0 deletions crates/kas-macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,6 @@ features = ["extra-traits", "full", "visit", "visit-mut"]
version_check = "0.9"

[lints.clippy]
collapsible_if = "allow"
collapsible_else_if = "allow"
unit_arg = "allow"
7 changes: 4 additions & 3 deletions crates/kas-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ mod collection;
mod extends;
mod make_layout;
mod widget;
mod widget_args;
mod widget_derive;
mod widget_index;

/// Implement `Default`
Expand Down Expand Up @@ -112,7 +114,7 @@ pub fn autoimpl(attr: TokenStream, item: TokenStream) -> TokenStream {
}

const IMPL_SCOPE_RULES: [&dyn scope::ScopeAttr; 2] =
[&scope::AttrImplDefault, &widget::AttrImplWidget];
[&scope::AttrImplDefault, &widget_args::AttrImplWidget];

fn find_attr(path: &syn::Path) -> Option<&'static dyn scope::ScopeAttr> {
IMPL_SCOPE_RULES
Expand Down Expand Up @@ -314,8 +316,7 @@ pub fn impl_scope(input: TokenStream) -> TokenStream {
/// ```
///
/// This is a special mode where most features of `#[widget]` are not
/// available. Only [`Layout`] methods may be specified (overriding those from
/// the derived widget); everything else is derived.
/// available.
///
/// ## Debugging
///
Expand Down
3 changes: 2 additions & 1 deletion crates/kas-macros/src/make_layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
// https://www.apache.org/licenses/LICENSE-2.0

use crate::collection::{CellInfo, GridDimensions, NameGenerator, StorIdent};
use crate::widget::{self, Child, ChildIdent};
use crate::widget;
use crate::widget_args::{Child, ChildIdent};
use proc_macro2::{Span, TokenStream as Toks};
use proc_macro_error2::{emit_error, emit_warning};
use quote::{quote, quote_spanned, ToTokens, TokenStreamExt};
Expand Down
Loading
Loading