Skip to content

Commit 5376550

Browse files
committed
gcore-shaders: move ChoiceType, switch Cow for &str, adjust node macro
1 parent 8edc4eb commit 5376550

8 files changed

Lines changed: 59 additions & 54 deletions

File tree

editor/src/messages/portfolio/document/node_graph/node_properties.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1938,7 +1938,7 @@ pub mod choice {
19381938
use super::ParameterWidgetsInfo;
19391939
use crate::messages::tool::tool_messages::tool_prelude::*;
19401940
use graph_craft::document::value::TaggedValue;
1941-
use graphene_std::registry::{ChoiceTypeStatic, ChoiceWidgetHint};
1941+
use graphene_std::choice_type::{ChoiceTypeStatic, ChoiceWidgetHint};
19421942
use std::marker::PhantomData;
19431943

19441944
pub trait WidgetFactory {
@@ -1998,10 +1998,7 @@ pub mod choice {
19981998
.map(|(item, metadata)| {
19991999
let updater = updater_factory();
20002000
let committer = committer_factory();
2001-
MenuListEntry::new(metadata.name.as_ref())
2002-
.label(metadata.label.as_ref())
2003-
.on_update(move |_| updater(item))
2004-
.on_commit(committer)
2001+
MenuListEntry::new(metadata.name).label(metadata.label).on_update(move |_| updater(item)).on_commit(committer)
20052002
})
20062003
.collect()
20072004
})
@@ -2020,11 +2017,11 @@ pub mod choice {
20202017
.map(|(item, var_meta)| {
20212018
let updater = updater_factory();
20222019
let committer = committer_factory();
2023-
let entry = RadioEntryData::new(var_meta.name.as_ref()).on_update(move |_| updater(item)).on_commit(committer);
2020+
let entry = RadioEntryData::new(var_meta.name).on_update(move |_| updater(item)).on_commit(committer);
20242021
match (var_meta.icon.as_deref(), var_meta.docstring.as_deref()) {
2025-
(None, None) => entry.label(var_meta.label.as_ref()),
2026-
(None, Some(doc)) => entry.label(var_meta.label.as_ref()).tooltip(doc),
2027-
(Some(icon), None) => entry.icon(icon).tooltip(var_meta.label.as_ref()),
2022+
(None, None) => entry.label(var_meta.label),
2023+
(None, Some(doc)) => entry.label(var_meta.label).tooltip(doc),
2024+
(Some(icon), None) => entry.icon(icon).tooltip(var_meta.label),
20282025
(Some(icon), Some(doc)) => entry.icon(icon).tooltip(format!("{}\n\n{}", var_meta.label, doc)),
20292026
}
20302027
})

editor/src/messages/portfolio/menu_bar/menu_bar_message_handler.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ impl LayoutHolder for MenuBarMessageHandler {
419419
action: MenuBarEntry::no_action(),
420420
disabled: no_active_document || !has_selected_layers,
421421
children: MenuBarEntryChildren(vec![{
422-
let list = <BooleanOperation as graphene_std::registry::ChoiceTypeStatic>::list();
422+
let list = <BooleanOperation as graphene_std::choice_type::ChoiceTypeStatic>::list();
423423
list.iter()
424424
.flat_map(|i| i.iter())
425425
.map(move |(operation, info)| MenuBarEntry {

editor/src/messages/tool/tool_messages/select_tool.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ impl SelectTool {
187187
}
188188

189189
fn boolean_widgets(&self, selected_count: usize) -> impl Iterator<Item = WidgetHolder> + use<> {
190-
let list = <BooleanOperation as graphene_std::registry::ChoiceTypeStatic>::list();
190+
let list = <BooleanOperation as graphene_std::choice_type::ChoiceTypeStatic>::list();
191191
list.iter().flat_map(|i| i.iter()).map(move |(operation, info)| {
192192
let mut tooltip = info.label.to_string();
193193
if let Some(doc) = info.docstring.as_deref() {
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
pub trait ChoiceTypeStatic: Sized + Copy + crate::AsU32 + Send + Sync {
2+
const WIDGET_HINT: ChoiceWidgetHint;
3+
const DESCRIPTION: Option<&'static str>;
4+
fn list() -> &'static [&'static [(Self, VariantMetadata)]];
5+
}
6+
7+
pub enum ChoiceWidgetHint {
8+
Dropdown,
9+
RadioButtons,
10+
}
11+
12+
/// Translation struct between macro and definition.
13+
#[derive(Clone, Debug)]
14+
pub struct VariantMetadata {
15+
/// Name as declared in source code.
16+
pub name: &'static str,
17+
18+
/// Name to be displayed in UI.
19+
pub label: &'static str,
20+
21+
/// User-facing documentation text.
22+
pub docstring: Option<&'static str>,
23+
24+
/// Name of icon to display in radio buttons and such.
25+
pub icon: Option<&'static str>,
26+
}

node-graph/gcore-shaders/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
pub use glam;
22

33
pub mod blending;
4+
pub mod choice_type;
45
pub mod color;
56

67
pub trait AsU32 {

node-graph/gcore/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ pub use ctor;
3737
pub use dyn_any::{StaticTypeSized, WasmNotSend, WasmNotSync};
3838
pub use graphene_core_shaders::AsU32;
3939
pub use graphene_core_shaders::blending;
40+
pub use graphene_core_shaders::choice_type;
4041
pub use graphene_core_shaders::color;
4142
pub use graphic_element::{Artboard, ArtboardGroupTable, GraphicElement, GraphicGroupTable};
4243
pub use memo::MemoHash;

node-graph/gcore/src/registry.rs

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use crate::{Node, NodeIO, NodeIOTypes, ProtoNodeIdentifier, Type, WasmNotSend};
22
use dyn_any::{DynAny, StaticType};
3-
use std::borrow::Cow;
43
use std::collections::HashMap;
54
use std::marker::PhantomData;
65
use std::ops::Deref;
@@ -59,33 +58,6 @@ pub struct FieldMetadata {
5958
pub unit: Option<&'static str>,
6059
}
6160

62-
pub trait ChoiceTypeStatic: Sized + Copy + crate::AsU32 + Send + Sync {
63-
const WIDGET_HINT: ChoiceWidgetHint;
64-
const DESCRIPTION: Option<&'static str>;
65-
fn list() -> &'static [&'static [(Self, VariantMetadata)]];
66-
}
67-
68-
pub enum ChoiceWidgetHint {
69-
Dropdown,
70-
RadioButtons,
71-
}
72-
73-
/// Translation struct between macro and definition.
74-
#[derive(Clone, Debug)]
75-
pub struct VariantMetadata {
76-
/// Name as declared in source code.
77-
pub name: Cow<'static, str>,
78-
79-
/// Name to be displayed in UI.
80-
pub label: Cow<'static, str>,
81-
82-
/// User-facing documentation text.
83-
pub docstring: Option<Cow<'static, str>>,
84-
85-
/// Name of icon to display in radio buttons and such.
86-
pub icon: Option<Cow<'static, str>>,
87-
}
88-
8961
#[derive(Clone, Debug)]
9062
pub enum RegistryWidgetOverride {
9163
None,

node-graph/node-macro/src/derive_choice_type.rs

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,21 @@ fn derive_enum(enum_attributes: &[Attribute], name: Ident, input: syn::DataEnum)
111111
})
112112
.collect();
113113

114-
let crate_name = proc_macro_crate::crate_name("graphene-core")
115-
.map_err(|e| syn::Error::new(Span::call_site(), format!("Failed to find location of graphene_core. Make sure it is imported as a dependency: {}", e)))?;
116-
let crate_name = match crate_name {
117-
proc_macro_crate::FoundCrate::Itself => quote!(crate),
118-
proc_macro_crate::FoundCrate::Name(name) => {
119-
let identifier = Ident::new(&name, Span::call_site());
120-
quote! { #identifier }
114+
let crate_name = {
115+
let crate_name = proc_macro_crate::crate_name("graphene-core-shaders")
116+
.or_else(|_e| proc_macro_crate::crate_name("graphene-core"))
117+
.map_err(|e| {
118+
syn::Error::new(
119+
Span::call_site(),
120+
format!("Failed to find location of 'graphene_core' or 'graphene-core-shaders'. Make sure it is imported as a dependency: {}", e),
121+
)
122+
})?;
123+
match crate_name {
124+
proc_macro_crate::FoundCrate::Itself => quote!(crate),
125+
proc_macro_crate::FoundCrate::Name(name) => {
126+
let identifier = Ident::new(&name, Span::call_site());
127+
quote! { #identifier }
128+
}
121129
}
122130
};
123131

@@ -140,19 +148,19 @@ fn derive_enum(enum_attributes: &[Attribute], name: Ident, input: syn::DataEnum)
140148
let docstring = match &variant.basic_item.description {
141149
Some(s) => {
142150
let s = s.trim();
143-
quote! { Some(::std::borrow::Cow::Borrowed(#s)) }
151+
quote! { Some(#s) }
144152
}
145153
None => quote! { None },
146154
};
147155
let icon = match &variant.basic_item.icon {
148-
Some(s) => quote! { Some(::std::borrow::Cow::Borrowed(#s)) },
156+
Some(s) => quote! { Some(#s) },
149157
None => quote! { None },
150158
};
151159
quote! {
152160
(
153-
#name::#vname, #crate_name::registry::VariantMetadata {
154-
name: ::std::borrow::Cow::Borrowed(#vname_str),
155-
label: ::std::borrow::Cow::Borrowed(#label),
161+
#name::#vname, #crate_name::choice_type::VariantMetadata {
162+
name: #vname_str,
163+
label: #label,
156164
docstring: #docstring,
157165
icon: #icon,
158166
}
@@ -174,10 +182,10 @@ fn derive_enum(enum_attributes: &[Attribute], name: Ident, input: syn::DataEnum)
174182
}
175183
}
176184

177-
impl #crate_name::registry::ChoiceTypeStatic for #name {
178-
const WIDGET_HINT: #crate_name::registry::ChoiceWidgetHint = #crate_name::registry::ChoiceWidgetHint::#widget_hint;
185+
impl #crate_name::choice_type::ChoiceTypeStatic for #name {
186+
const WIDGET_HINT: #crate_name::choice_type::ChoiceWidgetHint = #crate_name::choice_type::ChoiceWidgetHint::#widget_hint;
179187
const DESCRIPTION: Option<&'static str> = #enum_description;
180-
fn list() -> &'static [&'static [(Self, #crate_name::registry::VariantMetadata)]] {
188+
fn list() -> &'static [&'static [(Self, #crate_name::choice_type::VariantMetadata)]] {
181189
&[ #(#group)* ]
182190
}
183191
}

0 commit comments

Comments
 (0)