Skip to content

Commit 4453297

Browse files
authored
Avoid looping behavior with color selection (#305)
1 parent 9d0e194 commit 4453297

File tree

2 files changed

+45
-23
lines changed

2 files changed

+45
-23
lines changed

client/web/src/components/widgets/inputs/SwatchPairInput.vue

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
<div class="secondary swatch">
44
<button @click="clickSecondarySwatch" ref="secondaryButton" data-hover-menu-spawner></button>
55
<FloatingMenu :type="MenuType.Popover" :direction="MenuDirection.Right" horizontal ref="secondarySwatchFloatingMenu">
6-
<ColorPicker v-model:color="secondaryColor" />
6+
<ColorPicker @update:color="secondaryColorChanged" :color="secondaryColor" />
77
</FloatingMenu>
88
</div>
99
<div class="primary swatch">
1010
<button @click="clickPrimarySwatch" ref="primaryButton" data-hover-menu-spawner></button>
1111
<FloatingMenu :type="MenuType.Popover" :direction="MenuDirection.Right" horizontal ref="primarySwatchFloatingMenu">
12-
<ColorPicker v-model:color="primaryColor" />
12+
<ColorPicker @update:color="primaryColorChanged" :color="primaryColor" />
1313
</FloatingMenu>
1414
</div>
1515
</div>
@@ -66,7 +66,7 @@
6666
</style>
6767

6868
<script lang="ts">
69-
import { rgbToDecimalRgb } from "@/utilities/color";
69+
import { rgbToDecimalRgb, RGB } from "@/utilities/color";
7070
import { defineComponent } from "vue";
7171
import ColorPicker from "@/components/widgets/floating-menus/ColorPicker.vue";
7272
import FloatingMenu, { MenuDirection, MenuType } from "@/components/widgets/floating-menus/FloatingMenu.vue";
@@ -95,6 +95,16 @@ export default defineComponent({
9595
return this.$refs[name] as T;
9696
},
9797
98+
primaryColorChanged(color: RGB) {
99+
this.primaryColor = color;
100+
this.updatePrimaryColor();
101+
},
102+
103+
secondaryColorChanged(color: RGB) {
104+
this.secondaryColor = color;
105+
this.updateSecondaryColor();
106+
},
107+
98108
async updatePrimaryColor() {
99109
const { update_primary_color, Color } = await wasm;
100110
@@ -126,16 +136,24 @@ export default defineComponent({
126136
};
127137
},
128138
mounted() {
129-
this.$watch("primaryColor", this.updatePrimaryColor, { immediate: true });
130-
this.$watch("secondaryColor", this.updateSecondaryColor, { immediate: true });
131-
132139
registerResponseHandler(ResponseType.UpdateWorkingColors, (responseData: Response) => {
133140
const colorData = responseData as UpdateWorkingColors;
134141
if (!colorData) return;
135142
const { primary, secondary } = colorData;
143+
136144
this.primaryColor = { r: primary.red, g: primary.green, b: primary.blue, a: primary.alpha };
145+
let color = this.primaryColor;
146+
let button = this.getRef<HTMLButtonElement>("primaryButton");
147+
button.style.setProperty("--swatch-color", `rgba(${color.r}, ${color.g}, ${color.b}, ${color.a})`);
148+
137149
this.secondaryColor = { r: secondary.red, g: secondary.green, b: secondary.blue, a: secondary.alpha };
150+
color = this.secondaryColor;
151+
button = this.getRef<HTMLButtonElement>("secondaryButton");
152+
button.style.setProperty("--swatch-color", `rgba(${color.r}, ${color.g}, ${color.b}, ${color.a})`);
138153
});
154+
155+
this.updatePrimaryColor();
156+
this.updateSecondaryColor();
139157
},
140158
});
141159
</script>

core/editor/src/tool/tool_message_handler.rs

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use document_core::color::Color;
44
use crate::input::InputPreprocessor;
55
use crate::{
66
document::Document,
7-
tool::{tool_options::ToolOptions, ToolFsmState, ToolType},
7+
tool::{tool_options::ToolOptions, DocumentToolData, ToolFsmState, ToolType},
88
};
99
use std::collections::VecDeque;
1010

@@ -50,8 +50,14 @@ impl MessageHandler<ToolMessage, (&Document, &InputPreprocessor)> for ToolMessag
5050
let (document, input) = data;
5151
use ToolMessage::*;
5252
match message {
53-
SelectPrimaryColor(c) => self.tool_state.document_tool_data.primary_color = c,
54-
SelectSecondaryColor(c) => self.tool_state.document_tool_data.secondary_color = c,
53+
SelectPrimaryColor(c) => {
54+
self.tool_state.document_tool_data.primary_color = c;
55+
update_working_colors(&self.tool_state.document_tool_data, responses);
56+
}
57+
SelectSecondaryColor(c) => {
58+
self.tool_state.document_tool_data.secondary_color = c;
59+
update_working_colors(&self.tool_state.document_tool_data, responses);
60+
}
5561
SelectTool(tool) => {
5662
let mut reset = |tool| match tool {
5763
ToolType::Ellipse => responses.push_back(EllipseMessage::Abort.into()),
@@ -70,25 +76,13 @@ impl MessageHandler<ToolMessage, (&Document, &InputPreprocessor)> for ToolMessag
7076
SwapColors => {
7177
let doc_data = &mut self.tool_state.document_tool_data;
7278
std::mem::swap(&mut doc_data.primary_color, &mut doc_data.secondary_color);
73-
responses.push_back(
74-
FrontendMessage::UpdateWorkingColors {
75-
primary: doc_data.primary_color,
76-
secondary: doc_data.secondary_color,
77-
}
78-
.into(),
79-
)
79+
update_working_colors(doc_data, responses);
8080
}
8181
ResetColors => {
8282
let doc_data = &mut self.tool_state.document_tool_data;
8383
doc_data.primary_color = Color::BLACK;
8484
doc_data.secondary_color = Color::WHITE;
85-
responses.push_back(
86-
FrontendMessage::UpdateWorkingColors {
87-
primary: doc_data.primary_color,
88-
secondary: doc_data.secondary_color,
89-
}
90-
.into(),
91-
)
85+
update_working_colors(doc_data, responses);
9286
}
9387
SetToolOptions(tool_type, tool_options) => {
9488
self.tool_state.document_tool_data.tool_options.insert(tool_type, tool_options);
@@ -120,3 +114,13 @@ impl MessageHandler<ToolMessage, (&Document, &InputPreprocessor)> for ToolMessag
120114
list
121115
}
122116
}
117+
118+
fn update_working_colors(doc_data: &DocumentToolData, responses: &mut VecDeque<Message>) {
119+
responses.push_back(
120+
FrontendMessage::UpdateWorkingColors {
121+
primary: doc_data.primary_color,
122+
secondary: doc_data.secondary_color,
123+
}
124+
.into(),
125+
);
126+
}

0 commit comments

Comments
 (0)