Move font size adjustment code to the theme crate

This commit is contained in:
Max Brunsfeld 2023-05-17 15:56:32 -07:00
parent 258723566f
commit 42eca3048f
10 changed files with 69 additions and 56 deletions

View file

@ -682,9 +682,7 @@ fn diagnostic_header_renderer(diagnostic: Diagnostic) -> RenderBlock {
let settings = settings::get::<ThemeSettings>(cx); let settings = settings::get::<ThemeSettings>(cx);
let theme = &settings.theme.editor; let theme = &settings.theme.editor;
let style = theme.diagnostic_header.clone(); let style = theme.diagnostic_header.clone();
let font_size = (style.text_scale_factor let font_size = (style.text_scale_factor * settings.buffer_font_size(cx)).round();
* settings::font_size_for_setting(settings.buffer_font_size, cx))
.round();
let icon_width = cx.em_width * style.icon_width_factor; let icon_width = cx.em_width * style.icon_width_factor;
let icon = if diagnostic.severity == DiagnosticSeverity::ERROR { let icon = if diagnostic.severity == DiagnosticSeverity::ERROR {
Svg::new("icons/circle_x_mark_12.svg") Svg::new("icons/circle_x_mark_12.svg")

View file

@ -7446,7 +7446,7 @@ fn build_style(
let font_id = font_cache let font_id = font_cache
.select_font(font_family_id, &font_properties) .select_font(font_family_id, &font_properties)
.unwrap(); .unwrap();
let font_size = settings::font_size_for_setting(settings.buffer_font_size, cx); let font_size = settings.buffer_font_size(cx);
EditorStyle { EditorStyle {
text: TextStyle { text: TextStyle {
color: settings.theme.editor.text_color, color: settings.theme.editor.text_color,
@ -7619,9 +7619,7 @@ pub fn diagnostic_block_renderer(diagnostic: Diagnostic, is_valid: bool) -> Rend
let settings = settings::get::<ThemeSettings>(cx); let settings = settings::get::<ThemeSettings>(cx);
let theme = &settings.theme.editor; let theme = &settings.theme.editor;
let style = diagnostic_style(diagnostic.severity, is_valid, theme); let style = diagnostic_style(diagnostic.severity, is_valid, theme);
let font_size = (style.text_scale_factor let font_size = (style.text_scale_factor * settings.buffer_font_size(cx)).round();
* settings::font_size_for_setting(settings.buffer_font_size, cx))
.round();
Flex::column() Flex::column()
.with_children(highlighted_lines.iter().map(|(line, highlights)| { .with_children(highlighted_lines.iter().map(|(line, highlights)| {
Label::new( Label::new(

View file

@ -1,19 +0,0 @@
use gpui::AppContext;
#[derive(Default)]
pub struct FontSizeDelta(pub f32);
pub fn adjust_font_size_delta(cx: &mut AppContext, f: fn(&mut f32, cx: &mut AppContext)) {
cx.update_default_global::<FontSizeDelta, _, _>(|size, cx| {
f(&mut size.0, cx);
});
cx.refresh_windows();
}
pub fn font_size_for_setting(size: f32, cx: &AppContext) -> f32 {
if cx.has_global::<FontSizeDelta>() {
size + cx.global::<FontSizeDelta>().0
} else {
size
}
}

View file

@ -1,11 +1,9 @@
mod font_size;
mod keymap_file; mod keymap_file;
mod settings_file; mod settings_file;
mod settings_store; mod settings_store;
use std::{borrow::Cow, str}; use std::{borrow::Cow, str};
pub use font_size::{adjust_font_size_delta, font_size_for_setting};
use gpui::AssetSource; use gpui::AssetSource;
pub use keymap_file::{keymap_file_json_schema, KeymapFileContent}; pub use keymap_file::{keymap_file_json_schema, KeymapFileContent};
pub use settings_file::*; pub use settings_file::*;

View file

@ -123,7 +123,7 @@ pub fn init(cx: &mut AppContext) {
pub struct TerminalSettings { pub struct TerminalSettings {
pub shell: Shell, pub shell: Shell,
pub working_directory: WorkingDirectory, pub working_directory: WorkingDirectory,
pub font_size: Option<f32>, font_size: Option<f32>,
pub font_family: Option<String>, pub font_family: Option<String>,
pub line_height: TerminalLineHeight, pub line_height: TerminalLineHeight,
pub font_features: Option<fonts::Features>, pub font_features: Option<fonts::Features>,
@ -149,6 +149,13 @@ pub struct TerminalSettingsContent {
pub copy_on_select: Option<bool>, pub copy_on_select: Option<bool>,
} }
impl TerminalSettings {
pub fn font_size(&self, cx: &AppContext) -> Option<f32> {
self.font_size
.map(|size| theme::adjusted_font_size(size, cx))
}
}
impl settings::Setting for TerminalSettings { impl settings::Setting for TerminalSettings {
const KEY: Option<&'static str> = Some("terminal"); const KEY: Option<&'static str> = Some("terminal");

View file

@ -16,7 +16,6 @@ use gpui::{
use itertools::Itertools; use itertools::Itertools;
use language::CursorShape; use language::CursorShape;
use ordered_float::OrderedFloat; use ordered_float::OrderedFloat;
use settings::font_size_for_setting;
use terminal::{ use terminal::{
alacritty_terminal::{ alacritty_terminal::{
ansi::{Color as AnsiColor, Color::Named, CursorShape as AlacCursorShape, NamedColor}, ansi::{Color as AnsiColor, Color::Named, CursorShape as AlacCursorShape, NamedColor},
@ -531,12 +530,9 @@ impl Element<TerminalView> for TerminalElement {
let tooltip_style = settings.theme.tooltip.clone(); let tooltip_style = settings.theme.tooltip.clone();
let font_cache = cx.font_cache(); let font_cache = cx.font_cache();
let font_size = font_size_for_setting( let font_size = terminal_settings
terminal_settings .font_size(cx)
.font_size .unwrap_or(settings.buffer_font_size(cx));
.unwrap_or(settings.buffer_font_size),
cx,
);
let font_family_name = terminal_settings let font_family_name = terminal_settings
.font_family .font_family
.as_ref() .as_ref()

View file

@ -13,8 +13,8 @@ use serde_json::Value;
use std::{collections::HashMap, sync::Arc}; use std::{collections::HashMap, sync::Arc};
use ui::{ButtonStyle, CheckboxStyle, IconStyle, ModalStyle, SvgStyle}; use ui::{ButtonStyle, CheckboxStyle, IconStyle, ModalStyle, SvgStyle};
pub use theme_registry::ThemeRegistry; pub use theme_registry::*;
pub use theme_settings::ThemeSettings; pub use theme_settings::*;
pub fn current(cx: &AppContext) -> Arc<Theme> { pub fn current(cx: &AppContext) -> Arc<Theme> {
settings::get::<ThemeSettings>(cx).theme.clone() settings::get::<ThemeSettings>(cx).theme.clone()

View file

@ -12,15 +12,19 @@ use settings::SettingsJsonSchemaParams;
use std::sync::Arc; use std::sync::Arc;
use util::ResultExt as _; use util::ResultExt as _;
const MIN_FONT_SIZE: f32 = 6.0;
#[derive(Clone)] #[derive(Clone)]
pub struct ThemeSettings { pub struct ThemeSettings {
pub buffer_font_family_name: String, pub buffer_font_family_name: String,
pub buffer_font_features: fonts::Features, pub buffer_font_features: fonts::Features,
pub buffer_font_family: FamilyId, pub buffer_font_family: FamilyId,
pub buffer_font_size: f32, buffer_font_size: f32,
pub theme: Arc<Theme>, pub theme: Arc<Theme>,
} }
pub struct AdjustedBufferFontSize(pub f32);
#[derive(Clone, Debug, Default, Serialize, Deserialize, JsonSchema)] #[derive(Clone, Debug, Default, Serialize, Deserialize, JsonSchema)]
pub struct ThemeSettingsContent { pub struct ThemeSettingsContent {
#[serde(default)] #[serde(default)]
@ -33,6 +37,48 @@ pub struct ThemeSettingsContent {
pub theme: Option<String>, pub theme: Option<String>,
} }
impl ThemeSettings {
pub fn buffer_font_size(&self, cx: &AppContext) -> f32 {
if cx.has_global::<AdjustedBufferFontSize>() {
cx.global::<AdjustedBufferFontSize>().0
} else {
self.buffer_font_size
}
.max(MIN_FONT_SIZE)
}
}
pub fn adjusted_font_size(size: f32, cx: &AppContext) -> f32 {
if cx.has_global::<AdjustedBufferFontSize>() {
let buffer_font_size = settings::get::<ThemeSettings>(cx).buffer_font_size;
let delta = cx.global::<AdjustedBufferFontSize>().0 - buffer_font_size;
size + delta
} else {
size
}
.max(MIN_FONT_SIZE)
}
pub fn adjust_font_size(cx: &mut AppContext, f: fn(&mut f32)) {
if !cx.has_global::<AdjustedBufferFontSize>() {
let buffer_font_size = settings::get::<ThemeSettings>(cx).buffer_font_size;
cx.set_global(AdjustedBufferFontSize(buffer_font_size));
}
cx.update_global::<AdjustedBufferFontSize, _, _>(|delta, cx| {
f(&mut delta.0);
delta.0 = delta
.0
.max(MIN_FONT_SIZE - settings::get::<ThemeSettings>(cx).buffer_font_size);
});
cx.refresh_windows();
}
pub fn reset_font_size(cx: &mut AppContext) {
cx.remove_global::<AdjustedBufferFontSize>();
cx.refresh_windows();
}
impl settings::Setting for ThemeSettings { impl settings::Setting for ThemeSettings {
const KEY: Option<&'static str> = None; const KEY: Option<&'static str> = None;

View file

@ -222,7 +222,7 @@ impl ThemeTestbench {
let settings = settings::get::<ThemeSettings>(cx); let settings = settings::get::<ThemeSettings>(cx);
let font_cache = cx.font_cache(); let font_cache = cx.font_cache();
let family_id = settings.buffer_font_family; let family_id = settings.buffer_font_family;
let font_size = settings::font_size_for_setting(settings.buffer_font_size, cx); let font_size = settings.buffer_font_size(cx);
let font_id = font_cache let font_id = font_cache
.select_font(family_id, &Default::default()) .select_font(family_id, &Default::default())
.unwrap(); .unwrap();

View file

@ -29,12 +29,9 @@ use project_panel::ProjectPanel;
use search::{BufferSearchBar, ProjectSearchBar}; use search::{BufferSearchBar, ProjectSearchBar};
use serde::Deserialize; use serde::Deserialize;
use serde_json::to_string_pretty; use serde_json::to_string_pretty;
use settings::{ use settings::{KeymapFileContent, SettingsStore, DEFAULT_SETTINGS_ASSET_PATH};
adjust_font_size_delta, KeymapFileContent, SettingsStore, DEFAULT_SETTINGS_ASSET_PATH,
};
use std::{borrow::Cow, str, sync::Arc}; use std::{borrow::Cow, str, sync::Arc};
use terminal_view::terminal_button::TerminalButton; use terminal_view::terminal_button::TerminalButton;
use theme::ThemeSettings;
use util::{channel::ReleaseChannel, paths, ResultExt}; use util::{channel::ReleaseChannel, paths, ResultExt};
use uuid::Uuid; use uuid::Uuid;
use welcome::BaseKeymap; use welcome::BaseKeymap;
@ -76,8 +73,6 @@ actions!(
] ]
); );
const MIN_FONT_SIZE: f32 = 6.0;
pub fn init(app_state: &Arc<AppState>, cx: &mut gpui::AppContext) { pub fn init(app_state: &Arc<AppState>, cx: &mut gpui::AppContext) {
cx.add_action(about); cx.add_action(about);
cx.add_global_action(|_: &Hide, cx: &mut gpui::AppContext| { cx.add_global_action(|_: &Hide, cx: &mut gpui::AppContext| {
@ -121,18 +116,12 @@ pub fn init(app_state: &Arc<AppState>, cx: &mut gpui::AppContext) {
cx.add_global_action(quit); cx.add_global_action(quit);
cx.add_global_action(move |action: &OpenBrowser, cx| cx.platform().open_url(&action.url)); cx.add_global_action(move |action: &OpenBrowser, cx| cx.platform().open_url(&action.url));
cx.add_global_action(move |_: &IncreaseBufferFontSize, cx| { cx.add_global_action(move |_: &IncreaseBufferFontSize, cx| {
adjust_font_size_delta(cx, |size, _| *size += 1.0) theme::adjust_font_size(cx, |size| *size += 1.0)
}); });
cx.add_global_action(move |_: &DecreaseBufferFontSize, cx| { cx.add_global_action(move |_: &DecreaseBufferFontSize, cx| {
adjust_font_size_delta(cx, |size, cx| { theme::adjust_font_size(cx, |size| *size -= 1.0)
if cx.global::<ThemeSettings>().buffer_font_size + *size > MIN_FONT_SIZE {
*size -= 1.0;
}
})
});
cx.add_global_action(move |_: &ResetBufferFontSize, cx| {
adjust_font_size_delta(cx, |size, _| *size = 0.0)
}); });
cx.add_global_action(move |_: &ResetBufferFontSize, cx| theme::reset_font_size(cx));
cx.add_global_action(move |_: &install_cli::Install, cx| { cx.add_global_action(move |_: &install_cli::Install, cx| {
cx.spawn(|cx| async move { cx.spawn(|cx| async move {
install_cli::install_cli(&cx) install_cli::install_cli(&cx)
@ -639,7 +628,7 @@ mod tests {
collections::HashSet, collections::HashSet,
path::{Path, PathBuf}, path::{Path, PathBuf},
}; };
use theme::ThemeRegistry; use theme::{ThemeRegistry, ThemeSettings};
use util::http::FakeHttpClient; use util::http::FakeHttpClient;
use workspace::{ use workspace::{
item::{Item, ItemHandle}, item::{Item, ItemHandle},