From 302c541f08013f4fcb0d9525f409fc5a710c2e25 Mon Sep 17 00:00:00 2001 From: Philip Daniels Date: Wed, 6 Apr 2022 20:48:58 +0100 Subject: [PATCH 1/4] Slight char_width refactor. This requires an extra function call but removes duplication of "W", so it's more safe for future editing. --- lapce-data/src/config.rs | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/lapce-data/src/config.rs b/lapce-data/src/config.rs index 8910a2d5..e0fa0bdc 100644 --- a/lapce-data/src/config.rs +++ b/lapce-data/src/config.rs @@ -553,13 +553,7 @@ pub fn char_width(&self, text: &mut PietText, font_size: f64) -> f64 { /// Calculate the width of the character "W" (being the widest character) /// in the editor's current font family and current font size. pub fn editor_char_width(&self, text: &mut PietText) -> f64 { - Self::editor_text_size_internal( - self.editor.font_family(), - self.editor.font_size as f64, - text, - "W", - ) - .width + self.char_width(text, self.editor.font_size as f64) } /// Calculate the width of `text_to_measure` in the editor's current font family and font size. From c95fff30f56690f9897d133686eb51e7a02108c5 Mon Sep 17 00:00:00 2001 From: Philip Daniels Date: Wed, 6 Apr 2022 20:50:54 +0100 Subject: [PATCH 2/4] Extend the comment of editor_text_size_internal. Adds a further note about the unsafetiness. --- lapce-data/src/config.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lapce-data/src/config.rs b/lapce-data/src/config.rs index e0fa0bdc..af7bc362 100644 --- a/lapce-data/src/config.rs +++ b/lapce-data/src/config.rs @@ -608,6 +608,10 @@ fn editor_text_size_internal( // always dropped inside this function, and hence its lifetime is always stricly less // than the lifetime of `text_to_measure`, irrespective of whether `text_to_measure` // is actually static or not. + // + // Note that this technique also assumes that `new_text_layout` does not stash + // its parameter away somewhere, such as a global cache. If it did, this would + // break and we would have to go back to calling `to_string` on the parameter. let static_str: &'static str = unsafe { std::mem::transmute(text_to_measure) }; From 4e61787b5f36dadd3ef2e9bdd14ce4aa6dc2b4ac Mon Sep 17 00:00:00 2001 From: Philip Daniels Date: Wed, 6 Apr 2022 21:07:08 +0100 Subject: [PATCH 3/4] Remove to_string on calls to new_text_layout Only on the calls which are passing a static string, this is always safe. --- lapce-ui/src/keymap.rs | 12 ++++++------ lapce-ui/src/palette.rs | 4 +--- lapce-ui/src/settings.rs | 8 +++++--- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/lapce-ui/src/keymap.rs b/lapce-ui/src/keymap.rs index 0e036c9c..6e08c8d8 100644 --- a/lapce-ui/src/keymap.rs +++ b/lapce-ui/src/keymap.rs @@ -488,7 +488,7 @@ fn paint(&mut self, ctx: &mut PaintCtx, data: &LapceTabData, _env: &Env) { ); let text = ctx .text() - .new_text_layout("Save".to_string()) + .new_text_layout("Save") .font(FontFamily::SYSTEM_UI, 13.0) .text_color( data.config @@ -524,7 +524,7 @@ fn paint(&mut self, ctx: &mut PaintCtx, data: &LapceTabData, _env: &Env) { ); let text = ctx .text() - .new_text_layout("Cancel".to_string()) + .new_text_layout("Cancel") .font(FontFamily::SYSTEM_UI, 13.0) .text_color( data.config @@ -612,7 +612,7 @@ fn paint(&mut self, ctx: &mut PaintCtx, data: &LapceTabData, _env: &Env) { let text_layout = ctx .text() - .new_text_layout("Command".to_string()) + .new_text_layout("Command") .font(FontFamily::SYSTEM_UI, 14.0) .default_attribute(TextAttribute::Weight(FontWeight::BOLD)) .text_color( @@ -630,7 +630,7 @@ fn paint(&mut self, ctx: &mut PaintCtx, data: &LapceTabData, _env: &Env) { let text_layout = ctx .text() - .new_text_layout("Keybinding".to_string()) + .new_text_layout("Keybinding") .font(FontFamily::SYSTEM_UI, 14.0) .default_attribute(TextAttribute::Weight(FontWeight::BOLD)) .text_color( @@ -651,7 +651,7 @@ fn paint(&mut self, ctx: &mut PaintCtx, data: &LapceTabData, _env: &Env) { let text_layout = ctx .text() - .new_text_layout("When".to_string()) + .new_text_layout("When") .font(FontFamily::SYSTEM_UI, 14.0) .default_attribute(TextAttribute::Weight(FontWeight::BOLD)) .text_color( @@ -679,7 +679,7 @@ fn paint(&mut self, ctx: &mut PaintCtx, data: &LapceTabData, _env: &Env) { if data.config.lapce.modal { let text_layout = ctx .text() - .new_text_layout("Modes".to_string()) + .new_text_layout("Modes") .font(FontFamily::SYSTEM_UI, 14.0) .default_attribute(TextAttribute::Weight(FontWeight::BOLD)) .text_color( diff --git a/lapce-ui/src/palette.rs b/lapce-ui/src/palette.rs index abf9a0af..8701cdf8 100644 --- a/lapce-ui/src/palette.rs +++ b/lapce-ui/src/palette.rs @@ -493,9 +493,7 @@ fn paint(&mut self, ctx: &mut PaintCtx, data: &PaletteViewData, _env: &Env) { && data.palette.palette_type == PaletteType::SshHost { ctx.text() - .new_text_layout( - "Enter your SSH details, like user@host".to_string(), - ) + .new_text_layout("Enter your SSH details, like user@host") .font(FontFamily::SYSTEM_UI, 14.0) .text_color( data.config diff --git a/lapce-ui/src/settings.rs b/lapce-ui/src/settings.rs index 1ed21f36..0093e483 100644 --- a/lapce-ui/src/settings.rs +++ b/lapce-ui/src/settings.rs @@ -357,13 +357,15 @@ fn paint(&mut self, ctx: &mut PaintCtx, data: &LapceTabData, env: &Env) { .get_color_unchecked(LapceTheme::EDITOR_BACKGROUND), ); - for (i, text) in ["Core Settings", "Editor Settings", "Keybindings"] + static SETTINGS_SECTIONS: [&str; 3] = ["Core Settings", "Editor Settings", "Keybindings"]; + + for (i, text) in SETTINGS_SECTIONS .iter() .enumerate() { let text_layout = ctx .text() - .new_text_layout(text.to_string()) + .new_text_layout(*text) .font(FontFamily::SYSTEM_UI, 14.0) .text_color( data.config @@ -393,7 +395,7 @@ fn paint(&mut self, ctx: &mut PaintCtx, data: &LapceTabData, env: &Env) { ); let text_layout = ctx .text() - .new_text_layout("Settings".to_string()) + .new_text_layout("Settings") .font(FontFamily::SYSTEM_UI, 16.0) .text_color( data.config From e26c0caaf266baa608cb0b15c37f87c4386011fb Mon Sep 17 00:00:00 2001 From: Philip Daniels Date: Wed, 6 Apr 2022 21:30:58 +0100 Subject: [PATCH 4/4] Review comment: make array const. --- lapce-ui/src/settings.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lapce-ui/src/settings.rs b/lapce-ui/src/settings.rs index 0093e483..480e7fd7 100644 --- a/lapce-ui/src/settings.rs +++ b/lapce-ui/src/settings.rs @@ -357,15 +357,15 @@ fn paint(&mut self, ctx: &mut PaintCtx, data: &LapceTabData, env: &Env) { .get_color_unchecked(LapceTheme::EDITOR_BACKGROUND), ); - static SETTINGS_SECTIONS: [&str; 3] = ["Core Settings", "Editor Settings", "Keybindings"]; + const SETTINGS_SECTIONS: [&str; 3] = ["Core Settings", "Editor Settings", "Keybindings"]; for (i, text) in SETTINGS_SECTIONS - .iter() + .into_iter() .enumerate() { let text_layout = ctx .text() - .new_text_layout(*text) + .new_text_layout(text) .font(FontFamily::SYSTEM_UI, 14.0) .text_color( data.config