Skip to content

Commit 8fad295

Browse files
Make checkboxes not use interior mutability (#2976)
* Make checkboxes not use interior mutability * Use copy instead of cloning * Fix --------- Co-authored-by: Keavon Chambers <[email protected]>
1 parent 7fcdad1 commit 8fad295

File tree

9 files changed

+76
-111
lines changed

9 files changed

+76
-111
lines changed

editor/src/messages/dialog/export_dialog/export_dialog_message_handler.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,14 +145,14 @@ impl LayoutHolder for ExportDialogMessageHandler {
145145
DropdownInput::new(entries).selected_index(Some(index as u32)).widget_holder(),
146146
];
147147

148-
let mut checkbox_id = CheckboxId::default();
148+
let checkbox_id = CheckboxId::new();
149149
let transparent_background = vec![
150-
TextLabel::new("Transparency").table_align(true).min_width(100).for_checkbox(&mut checkbox_id).widget_holder(),
150+
TextLabel::new("Transparency").table_align(true).min_width(100).for_checkbox(checkbox_id).widget_holder(),
151151
Separator::new(SeparatorType::Unrelated).widget_holder(),
152152
CheckboxInput::new(self.transparent_background)
153153
.disabled(self.file_type == FileType::Jpg)
154154
.on_update(move |value: &CheckboxInput| ExportDialogMessage::TransparentBackground(value.checked).into())
155-
.for_label(checkbox_id.clone())
155+
.for_label(checkbox_id)
156156
.widget_holder(),
157157
];
158158

editor/src/messages/dialog/new_document_dialog/new_document_dialog_message_handler.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,13 @@ impl LayoutHolder for NewDocumentDialogMessageHandler {
8080
.widget_holder(),
8181
];
8282

83-
let mut checkbox_id = CheckboxId::default();
83+
let checkbox_id = CheckboxId::new();
8484
let infinite = vec![
85-
TextLabel::new("Infinite Canvas").table_align(true).min_width(90).for_checkbox(&mut checkbox_id).widget_holder(),
85+
TextLabel::new("Infinite Canvas").table_align(true).min_width(90).for_checkbox(checkbox_id).widget_holder(),
8686
Separator::new(SeparatorType::Unrelated).widget_holder(),
8787
CheckboxInput::new(self.infinite)
8888
.on_update(|checkbox_input: &CheckboxInput| NewDocumentDialogMessage::Infinite(checkbox_input.checked).into())
89-
.for_label(checkbox_id.clone())
89+
.for_label(checkbox_id)
9090
.widget_holder(),
9191
];
9292

editor/src/messages/dialog/preferences_dialog/preferences_dialog_message_handler.rs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ impl PreferencesDialogMessageHandler {
6868
.widget_holder(),
6969
];
7070

71-
let mut checkbox_id = CheckboxId::default();
71+
let checkbox_id = CheckboxId::new();
7272
let zoom_with_scroll_tooltip = "Use the scroll wheel for zooming instead of vertically panning (not recommended for trackpads)";
7373
let zoom_with_scroll = vec![
7474
Separator::new(SeparatorType::Unrelated).widget_holder(),
@@ -81,12 +81,12 @@ impl PreferencesDialogMessageHandler {
8181
}
8282
.into()
8383
})
84-
.for_label(checkbox_id.clone())
84+
.for_label(checkbox_id)
8585
.widget_holder(),
8686
TextLabel::new("Zoom with Scroll")
8787
.table_align(true)
8888
.tooltip(zoom_with_scroll_tooltip)
89-
.for_checkbox(&mut checkbox_id)
89+
.for_checkbox(checkbox_id)
9090
.widget_holder(),
9191
];
9292

@@ -169,7 +169,7 @@ impl PreferencesDialogMessageHandler {
169169
graph_wire_style,
170170
];
171171

172-
let mut checkbox_id = CheckboxId::default();
172+
let checkbox_id = CheckboxId::new();
173173
let vello_tooltip = "Use the experimental Vello renderer (your browser must support WebGPU)";
174174
let use_vello = vec![
175175
Separator::new(SeparatorType::Unrelated).widget_holder(),
@@ -178,17 +178,17 @@ impl PreferencesDialogMessageHandler {
178178
.tooltip(vello_tooltip)
179179
.disabled(!preferences.supports_wgpu())
180180
.on_update(|checkbox_input: &CheckboxInput| PreferencesMessage::UseVello { use_vello: checkbox_input.checked }.into())
181-
.for_label(checkbox_id.clone())
181+
.for_label(checkbox_id)
182182
.widget_holder(),
183183
TextLabel::new("Vello Renderer")
184184
.table_align(true)
185185
.tooltip(vello_tooltip)
186186
.disabled(!preferences.supports_wgpu())
187-
.for_checkbox(&mut checkbox_id)
187+
.for_checkbox(checkbox_id)
188188
.widget_holder(),
189189
];
190190

191-
let mut checkbox_id = CheckboxId::default();
191+
let checkbox_id = CheckboxId::new();
192192
let vector_mesh_tooltip =
193193
"Allow tools to produce vector meshes, where more than two segments can connect to an anchor point.\n\nCurrently this does not properly handle stroke joins and fills.";
194194
let vector_meshes = vec![
@@ -197,13 +197,9 @@ impl PreferencesDialogMessageHandler {
197197
CheckboxInput::new(preferences.vector_meshes)
198198
.tooltip(vector_mesh_tooltip)
199199
.on_update(|checkbox_input: &CheckboxInput| PreferencesMessage::VectorMeshes { enabled: checkbox_input.checked }.into())
200-
.for_label(checkbox_id.clone())
201-
.widget_holder(),
202-
TextLabel::new("Vector Meshes")
203-
.table_align(true)
204-
.tooltip(vector_mesh_tooltip)
205-
.for_checkbox(&mut checkbox_id)
200+
.for_label(checkbox_id)
206201
.widget_holder(),
202+
TextLabel::new("Vector Meshes").table_align(true).tooltip(vector_mesh_tooltip).for_checkbox(checkbox_id).widget_holder(),
207203
];
208204

209205
Layout::WidgetLayout(WidgetLayout::new(vec![

editor/src/messages/layout/utility_types/widgets/input_widgets.rs

Lines changed: 11 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ use graphene_std::Color;
55
use graphene_std::raster::curve::Curve;
66
use graphene_std::transform::ReferencePoint;
77
use graphite_proc_macros::WidgetBuilder;
8-
use once_cell::sync::OnceCell;
9-
use std::sync::Arc;
108

119
#[derive(Clone, Derivative, serde::Serialize, serde::Deserialize, WidgetBuilder, specta::Type)]
1210
#[derivative(Debug, PartialEq)]
@@ -20,7 +18,7 @@ pub struct CheckboxInput {
2018

2119
pub tooltip: String,
2220

23-
#[serde(rename = "forLabel", skip_serializing_if = "checkbox_id_is_empty")]
21+
#[serde(rename = "forLabel")]
2422
pub for_label: CheckboxId,
2523

2624
#[serde(skip)]
@@ -44,19 +42,24 @@ impl Default for CheckboxInput {
4442
icon: "Checkmark".into(),
4543
tooltip: Default::default(),
4644
tooltip_shortcut: Default::default(),
47-
for_label: CheckboxId::default(),
45+
for_label: CheckboxId::new(),
4846
on_update: Default::default(),
4947
on_commit: Default::default(),
5048
}
5149
}
5250
}
5351

54-
#[derive(Clone, Default, Debug, Eq, PartialEq)]
55-
pub struct CheckboxId(Arc<OnceCell<u64>>);
52+
#[derive(Copy, Clone, Debug, Eq, PartialEq, serde::Serialize, serde::Deserialize)]
53+
pub struct CheckboxId(u64);
5654

5755
impl CheckboxId {
58-
pub fn fill(&mut self) {
59-
let _ = self.0.set(graphene_std::uuid::generate_uuid());
56+
pub fn new() -> Self {
57+
Self(graphene_std::uuid::generate_uuid())
58+
}
59+
}
60+
impl Default for CheckboxId {
61+
fn default() -> Self {
62+
Self::new()
6063
}
6164
}
6265
impl specta::Type for CheckboxId {
@@ -65,31 +68,6 @@ impl specta::Type for CheckboxId {
6568
specta::datatype::DataType::Primitive(specta::datatype::PrimitiveType::u64)
6669
}
6770
}
68-
impl serde::Serialize for CheckboxId {
69-
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
70-
where
71-
S: serde::Serializer,
72-
{
73-
self.0.get().copied().serialize(serializer)
74-
}
75-
}
76-
impl<'a> serde::Deserialize<'a> for CheckboxId {
77-
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
78-
where
79-
D: serde::Deserializer<'a>,
80-
{
81-
let optional_id: Option<u64> = Option::deserialize(deserializer)?;
82-
// TODO: This is potentially weird because after deserialization the two labels will be decoupled if the value not existent
83-
let id = optional_id.unwrap_or(0);
84-
let checkbox_id = CheckboxId(OnceCell::new().into());
85-
checkbox_id.0.set(id).map_err(serde::de::Error::custom)?;
86-
Ok(checkbox_id)
87-
}
88-
}
89-
90-
fn checkbox_id_is_empty(id: &CheckboxId) -> bool {
91-
id.0.get().is_none()
92-
}
9371

9472
#[derive(Clone, serde::Serialize, serde::Deserialize, Derivative, WidgetBuilder, specta::Type)]
9573
#[derivative(Debug, PartialEq, Default)]

editor/src/messages/layout/utility_types/widgets/label_widgets.rs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -57,21 +57,12 @@ pub struct TextLabel {
5757

5858
pub tooltip: String,
5959

60-
#[serde(rename = "checkboxId")]
61-
#[widget_builder(skip)]
62-
pub checkbox_id: CheckboxId,
60+
#[serde(rename = "forCheckbox")]
61+
pub for_checkbox: CheckboxId,
6362

6463
// Body
6564
#[widget_builder(constructor)]
6665
pub value: String,
6766
}
6867

69-
impl TextLabel {
70-
pub fn for_checkbox(mut self, id: &mut CheckboxId) -> Self {
71-
id.fill();
72-
self.checkbox_id = id.clone();
73-
self
74-
}
75-
}
76-
7768
// TODO: Add UserInputLabel

0 commit comments

Comments
 (0)