Skip to content

Commit de366f9

Browse files
committed
Add Brush tool warning; move font list loading to document creation time
1 parent 4576197 commit de366f9

File tree

13 files changed

+99
-82
lines changed

13 files changed

+99
-82
lines changed

editor/src/dispatcher.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ use crate::messages::dialog::DialogMessageData;
33
use crate::messages::portfolio::document::node_graph::document_node_definitions;
44
use crate::messages::prelude::*;
55

6-
use graphene_core::text::Font;
7-
86
#[derive(Debug, Default)]
97
pub struct Dispatcher {
108
buffered_queue: Option<Vec<VecDeque<Message>>>,
@@ -135,10 +133,6 @@ impl Dispatcher {
135133
// Display the menu bar at the top of the window
136134
queue.add(MenuBarMessage::SendLayout);
137135

138-
// Load the default font
139-
let font = Font::new(graphene_core::consts::DEFAULT_FONT_FAMILY.into(), graphene_core::consts::DEFAULT_FONT_STYLE.into());
140-
queue.add(FrontendMessage::TriggerFontLoad { font, is_default: true });
141-
142136
// Send the information for tooltips and categories for each node/input.
143137
queue.add(FrontendMessage::SendUIMetadata {
144138
input_type_descriptions: Vec::new(),

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

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -61,34 +61,34 @@ impl PreferencesDialogMessageHandler {
6161
.widget_holder(),
6262
];
6363

64-
let imaginate_server_hostname = vec![
65-
TextLabel::new("Imaginate").min_width(60).italic(true).widget_holder(),
66-
TextLabel::new("Server Hostname").table_align(true).widget_holder(),
67-
Separator::new(SeparatorType::Unrelated).widget_holder(),
68-
TextInput::new(&preferences.imaginate_server_hostname)
69-
.min_width(200)
70-
.on_update(|text_input: &TextInput| PreferencesMessage::ImaginateServerHostname { hostname: text_input.value.clone() }.into())
71-
.widget_holder(),
72-
];
73-
74-
let imaginate_refresh_frequency = vec![
75-
TextLabel::new("").min_width(60).widget_holder(),
76-
TextLabel::new("Refresh Frequency").table_align(true).widget_holder(),
77-
Separator::new(SeparatorType::Unrelated).widget_holder(),
78-
NumberInput::new(Some(preferences.imaginate_refresh_frequency))
79-
.unit(" seconds")
80-
.min(0.)
81-
.max((1_u64 << f64::MANTISSA_DIGITS) as f64)
82-
.min_width(200)
83-
.on_update(|number_input: &NumberInput| PreferencesMessage::ImaginateRefreshFrequency { seconds: number_input.value.unwrap() }.into())
84-
.widget_holder(),
85-
];
64+
// TODO: Reenable when Imaginate is restored
65+
// let imaginate_server_hostname = vec![
66+
// TextLabel::new("Imaginate").min_width(60).italic(true).widget_holder(),
67+
// TextLabel::new("Server Hostname").table_align(true).widget_holder(),
68+
// Separator::new(SeparatorType::Unrelated).widget_holder(),
69+
// TextInput::new(&preferences.imaginate_server_hostname)
70+
// .min_width(200)
71+
// .on_update(|text_input: &TextInput| PreferencesMessage::ImaginateServerHostname { hostname: text_input.value.clone() }.into())
72+
// .widget_holder(),
73+
// ];
74+
// let imaginate_refresh_frequency = vec![
75+
// TextLabel::new("").min_width(60).widget_holder(),
76+
// TextLabel::new("Refresh Frequency").table_align(true).widget_holder(),
77+
// Separator::new(SeparatorType::Unrelated).widget_holder(),
78+
// NumberInput::new(Some(preferences.imaginate_refresh_frequency))
79+
// .unit(" seconds")
80+
// .min(0.)
81+
// .max((1_u64 << f64::MANTISSA_DIGITS) as f64)
82+
// .min_width(200)
83+
// .on_update(|number_input: &NumberInput| PreferencesMessage::ImaginateRefreshFrequency { seconds: number_input.value.unwrap() }.into())
84+
// .widget_holder(),
85+
// ];
8686

8787
Layout::WidgetLayout(WidgetLayout::new(vec![
8888
LayoutGroup::Row { widgets: zoom_with_scroll },
8989
LayoutGroup::Row { widgets: use_vello },
90-
LayoutGroup::Row { widgets: imaginate_server_hostname },
91-
LayoutGroup::Row { widgets: imaginate_refresh_frequency },
90+
// LayoutGroup::Row { widgets: imaginate_server_hostname },
91+
// LayoutGroup::Row { widgets: imaginate_refresh_frequency },
9292
]))
9393
}
9494

editor/src/messages/frontend/frontend_message.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,6 @@ pub enum FrontendMessage {
8181
},
8282
TriggerFontLoad {
8383
font: Font,
84-
#[serde(rename = "isDefault")]
85-
is_default: bool,
8684
},
8785
TriggerImport,
8886
TriggerIndexedDbRemoveDocument {

editor/src/messages/layout/layout_message_handler.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,6 @@ impl LayoutMessageHandler {
189189

190190
responses.add(PortfolioMessage::LoadFont {
191191
font: Font::new(font_family.into(), font_style.into()),
192-
is_default: false,
193192
});
194193
(font_input.on_update.callback)(font_input)
195194
}

editor/src/messages/portfolio/document/document_message_handler.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1676,7 +1676,7 @@ impl DocumentMessageHandler {
16761676
}
16771677
}
16781678
for font in fonts {
1679-
responses.add_front(FrontendMessage::TriggerFontLoad { font, is_default: false });
1679+
responses.add_front(FrontendMessage::TriggerFontLoad { font });
16801680
}
16811681
}
16821682

editor/src/messages/portfolio/portfolio_message.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ pub enum PortfolioMessage {
5151
font_style: String,
5252
preview_url: String,
5353
data: Vec<u8>,
54-
is_default: bool,
5554
},
5655
ImaginateCheckServerStatus,
5756
ImaginatePollServerStatus,
@@ -63,7 +62,6 @@ pub enum PortfolioMessage {
6362
},
6463
LoadFont {
6564
font: Font,
66-
is_default: bool,
6765
},
6866
NewDocumentWithName {
6967
name: String,

editor/src/messages/portfolio/portfolio_message_handler.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -278,11 +278,10 @@ impl MessageHandler<PortfolioMessage, PortfolioMessageData<'_>> for PortfolioMes
278278
font_style,
279279
preview_url,
280280
data,
281-
is_default,
282281
} => {
283282
let font = Font::new(font_family, font_style);
284283

285-
self.persistent_data.font_cache.insert(font, preview_url, data, is_default);
284+
self.persistent_data.font_cache.insert(font, preview_url, data);
286285
self.executor.update_font_cache(self.persistent_data.font_cache.clone());
287286
for document_id in self.document_ids.iter() {
288287
let _ = self.executor.submit_node_graph_evaluation(
@@ -334,9 +333,9 @@ impl MessageHandler<PortfolioMessage, PortfolioMessageData<'_>> for PortfolioMes
334333
document.load_layer_resources(responses);
335334
}
336335
}
337-
PortfolioMessage::LoadFont { font, is_default } => {
336+
PortfolioMessage::LoadFont { font } => {
338337
if !self.persistent_data.font_cache.loaded_font(&font) {
339-
responses.add_front(FrontendMessage::TriggerFontLoad { font, is_default });
338+
responses.add_front(FrontendMessage::TriggerFontLoad { font });
340339
}
341340
}
342341
PortfolioMessage::NewDocumentWithName { name } => {
@@ -939,6 +938,10 @@ impl PortfolioMessageHandler {
939938
if self.active_document().is_some() {
940939
responses.add(BroadcastEvent::ToolAbort);
941940
responses.add(ToolMessage::DeactivateTools);
941+
} else {
942+
// Load the default font upon creating the first document
943+
let font = Font::new(graphene_core::consts::DEFAULT_FONT_FAMILY.into(), graphene_core::consts::DEFAULT_FONT_STYLE.into());
944+
responses.add(FrontendMessage::TriggerFontLoad { font });
942945
}
943946

944947
// TODO: Remove this and find a way to fix the issue where creating a new document when the node graph is open causes the transform in the new document to be incorrect

editor/src/messages/preferences/preferences_message_handler.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,10 @@ impl MessageHandler<PreferencesMessage, ()> for PreferencesMessageHandler {
4545
if let Ok(deserialized_preferences) = serde_json::from_str::<PreferencesMessageHandler>(&preferences) {
4646
*self = deserialized_preferences;
4747

48-
responses.add(PortfolioMessage::ImaginateServerHostname);
49-
responses.add(PortfolioMessage::ImaginateCheckServerStatus);
48+
// TODO: Reenable when Imaginate is restored
49+
// responses.add(PortfolioMessage::ImaginateServerHostname);
50+
// responses.add(PortfolioMessage::ImaginateCheckServerStatus);
51+
5052
responses.add(PortfolioMessage::EditorPreferences);
5153
responses.add(PortfolioMessage::UpdateVelloPreference);
5254
responses.add(PreferencesMessage::ModifyLayout {

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ pub struct BrushTool {
2929
}
3030

3131
pub struct BrushOptions {
32+
legacy_warning_was_shown: bool,
3233
diameter: f64,
3334
hardness: f64,
3435
flow: f64,
@@ -41,6 +42,7 @@ pub struct BrushOptions {
4142
impl Default for BrushOptions {
4243
fn default() -> Self {
4344
Self {
45+
legacy_warning_was_shown: false,
4446
diameter: DEFAULT_BRUSH_SIZE,
4547
hardness: 0.,
4648
flow: 100.,
@@ -78,6 +80,7 @@ pub enum BrushToolMessageOptionsUpdate {
7880
Hardness(f64),
7981
Spacing(f64),
8082
WorkingColors(Option<Color>, Option<Color>),
83+
NoDisplayLegacyWarning,
8184
}
8285

8386
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
@@ -217,6 +220,7 @@ impl<'a> MessageHandler<ToolMessage, &mut ToolActionHandlerData<'a>> for BrushTo
217220
self.options.color.primary_working_color = primary;
218221
self.options.color.secondary_working_color = secondary;
219222
}
223+
BrushToolMessageOptionsUpdate::NoDisplayLegacyWarning => self.options.legacy_warning_was_shown = true,
220224
}
221225

222226
self.send_layout(responses, LayoutTarget::ToolOptions);
@@ -308,6 +312,20 @@ impl Fsm for BrushToolFsmState {
308312
document, global_tool_data, input, ..
309313
} = tool_action_data;
310314

315+
if !tool_options.legacy_warning_was_shown {
316+
responses.add(DialogMessage::DisplayDialogError {
317+
title: "Unsupported tool".into(),
318+
description: "
319+
The current Brush tool is a legacy feature with\n\
320+
significant quality and performance limitations.\n\
321+
It will be replaced soon by a new implementation.\n\
322+
"
323+
.trim()
324+
.into(),
325+
});
326+
responses.add(BrushToolMessage::UpdateOptions(BrushToolMessageOptionsUpdate::NoDisplayLegacyWarning));
327+
}
328+
311329
let ToolMessage::Brush(event) = event else {
312330
return self;
313331
};

frontend/src/state-providers/fonts.ts

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,33 @@ export function createFontsState(editor: Editor) {
88
// TODO: Do some code cleanup to remove the need for this empty store
99
const { subscribe } = writable({});
1010

11-
function createURL(font: string): URL {
11+
function createURL(font: string, weight: string): URL {
1212
const url = new URL("https://fonts.googleapis.com/css2");
1313
url.searchParams.set("display", "swap");
14-
url.searchParams.set("family", font);
14+
url.searchParams.set("family", `${font}:wght@${weight}`);
1515
url.searchParams.set("text", font);
16+
1617
return url;
1718
}
1819

1920
async function fontNames(): Promise<{ name: string; url: URL | undefined }[]> {
20-
return (await fontList).map((font) => ({ name: font.family, url: createURL(font.family) }));
21+
const pickPreviewWeight = (variants: string[]) => {
22+
const weights = variants.map((variant) => Number(variant.match(/.* \((\d+)\)/)?.[1] || "NaN"));
23+
const weightGoal = 400;
24+
const sorted = weights.map((weight) => [weight, Math.abs(weightGoal - weight - 1)]);
25+
sorted.sort(([_, a], [__, b]) => a - b);
26+
return sorted[0][0].toString();
27+
};
28+
return (await loadFontList()).map((font) => ({ name: font.family, url: createURL(font.family, pickPreviewWeight(font.variants)) }));
2129
}
2230

2331
async function getFontStyles(fontFamily: string): Promise<{ name: string; url: URL | undefined }[]> {
24-
const font = (await fontList).find((value) => value.family === fontFamily);
32+
const font = (await loadFontList()).find((value) => value.family === fontFamily);
2533
return font?.variants.map((variant) => ({ name: variant, url: undefined })) || [];
2634
}
2735

2836
async function getFontFileUrl(fontFamily: string, fontStyle: string): Promise<string | undefined> {
29-
const font = (await fontList).find((value) => value.family === fontFamily);
37+
const font = (await loadFontList()).find((value) => value.family === fontFamily);
3038
const fontFileUrl = font?.files.get(fontStyle);
3139
return fontFileUrl?.replace("http://", "https://");
3240
}
@@ -47,33 +55,41 @@ export function createFontsState(editor: Editor) {
4755
return `${weightName}${isItalic ? " Italic" : ""} (${weight})`;
4856
}
4957

58+
let fontList: Promise<{ family: string; variants: string[]; files: Map<string, string> }[]> | undefined;
59+
60+
async function loadFontList(): Promise<{ family: string; variants: string[]; files: Map<string, string> }[]> {
61+
if (fontList) return fontList;
62+
63+
fontList = new Promise<{ family: string; variants: string[]; files: Map<string, string> }[]>((resolve) => {
64+
fetch(fontListAPI)
65+
.then((response) => response.json())
66+
.then((fontListResponse) => {
67+
const fontListData = fontListResponse.items as { family: string; variants: string[]; files: Record<string, string> }[];
68+
const result = fontListData.map((font) => {
69+
const { family } = font;
70+
const variants = font.variants.map(formatFontStyleName);
71+
const files = new Map(font.variants.map((x) => [formatFontStyleName(x), font.files[x]]));
72+
return { family, variants, files };
73+
});
74+
75+
resolve(result);
76+
});
77+
});
78+
79+
return fontList;
80+
}
81+
5082
// Subscribe to process backend events
5183
editor.subscriptions.subscribeJsMessage(TriggerFontLoad, async (triggerFontLoad) => {
5284
const url = await getFontFileUrl(triggerFontLoad.font.fontFamily, triggerFontLoad.font.fontStyle);
5385
if (url) {
5486
const response = await (await fetch(url)).arrayBuffer();
55-
editor.handle.onFontLoad(triggerFontLoad.font.fontFamily, triggerFontLoad.font.fontStyle, url, new Uint8Array(response), triggerFontLoad.isDefault);
87+
editor.handle.onFontLoad(triggerFontLoad.font.fontFamily, triggerFontLoad.font.fontStyle, url, new Uint8Array(response));
5688
} else {
5789
editor.handle.errorDialog("Failed to load font", `The font ${triggerFontLoad.font.fontFamily} with style ${triggerFontLoad.font.fontStyle} does not exist`);
5890
}
5991
});
6092

61-
const fontList = new Promise<{ family: string; variants: string[]; files: Map<string, string> }[]>((resolve) => {
62-
fetch(fontListAPI)
63-
.then((response) => response.json())
64-
.then((fontListResponse) => {
65-
const fontListData = fontListResponse.items as { family: string; variants: string[]; files: Record<string, string> }[];
66-
const result = fontListData.map((font) => {
67-
const { family } = font;
68-
const variants = font.variants.map(formatFontStyleName);
69-
const files = new Map(font.variants.map((x) => [formatFontStyleName(x), font.files[x]]));
70-
return { family, variants, files };
71-
});
72-
73-
resolve(result);
74-
});
75-
});
76-
7793
return {
7894
subscribe,
7995
fontNames,

0 commit comments

Comments
 (0)