Skip to content

Commit

Permalink
Make font weight configurable for buffer and terminal
Browse files Browse the repository at this point in the history
  • Loading branch information
nathansobo committed May 27, 2024
1 parent 1e5fcb7 commit c4c2728
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 24 deletions.
2 changes: 2 additions & 0 deletions assets/settings/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
},
// The default font size for text in the editor
"buffer_font_size": 15,
// The weight of the editor font in standard CSS units from 100 to 900.
"buffer_font_weight": 400,
// Set the buffer's line height.
// May take 3 values:
// 1. Use a line height that's comfortable for reading (1.618)
Expand Down
2 changes: 1 addition & 1 deletion crates/assistant2/src/ui/composer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ impl RenderOnce for Composer {
font_family: settings.buffer_font.family.clone(),
font_features: settings.buffer_font.features.clone(),
font_size: font_size.into(),
font_weight: FontWeight::NORMAL,
font_weight: settings.buffer_font.weight,
font_style: FontStyle::Normal,
line_height: line_height.into(),
background_color: None,
Expand Down
2 changes: 1 addition & 1 deletion crates/editor/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11479,7 +11479,7 @@ impl Render for Editor {
font_family: settings.buffer_font.family.clone(),
font_features: settings.buffer_font.features.clone(),
font_size: settings.buffer_font_size(cx).into(),
font_weight: FontWeight::NORMAL,
font_weight: settings.buffer_font.weight,
font_style: FontStyle::Normal,
line_height: relative(settings.buffer_line_height.value()),
background_color: None,
Expand Down
4 changes: 3 additions & 1 deletion crates/gpui/src/text_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ pub use font_features::*;
pub use line::*;
pub use line_layout::*;
pub use line_wrapper::*;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

use crate::{
px, Bounds, DevicePixels, Hsla, Pixels, PlatformTextSystem, Point, Result, SharedString, Size,
Expand Down Expand Up @@ -554,7 +556,7 @@ impl DerefMut for LineWrapperHandle {

/// The degree of blackness or stroke thickness of a font. This value ranges from 100.0 to 900.0,
/// with 400.0 as normal.
#[derive(Clone, Copy, Debug, PartialEq, PartialOrd)]
#[derive(Clone, Copy, Debug, PartialEq, PartialOrd, Deserialize, Serialize, JsonSchema)]
pub struct FontWeight(pub f32);

impl Default for FontWeight {
Expand Down
10 changes: 5 additions & 5 deletions crates/search/src/project_search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ use editor::{
};
use gpui::{
actions, div, Action, AnyElement, AnyView, AppContext, Context as _, Element, EntityId,
EventEmitter, FocusHandle, FocusableView, FontStyle, FontWeight, Global, Hsla,
InteractiveElement, IntoElement, Model, ModelContext, ParentElement, Point, Render,
SharedString, Styled, Subscription, Task, TextStyle, UpdateGlobal, View, ViewContext,
VisualContext, WeakModel, WeakView, WhiteSpace, WindowContext,
EventEmitter, FocusHandle, FocusableView, FontStyle, Global, Hsla, InteractiveElement,
IntoElement, Model, ModelContext, ParentElement, Point, Render, SharedString, Styled,
Subscription, Task, TextStyle, UpdateGlobal, View, ViewContext, VisualContext, WeakModel,
WeakView, WhiteSpace, WindowContext,
};
use menu::Confirm;
use project::{search::SearchQuery, search_history::SearchHistoryCursor, Project, ProjectPath};
Expand Down Expand Up @@ -1311,7 +1311,7 @@ impl ProjectSearchBar {
font_family: settings.buffer_font.family.clone(),
font_features: settings.buffer_font.features.clone(),
font_size: rems(0.875).into(),
font_weight: FontWeight::NORMAL,
font_weight: settings.buffer_font.weight,
font_style: FontStyle::Normal,
line_height: relative(1.3),
background_color: None,
Expand Down
4 changes: 2 additions & 2 deletions crates/semantic_index/src/project_index_debug_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ impl ProjectIndexDebugView {
}

fn render_chunk(&mut self, ix: usize, cx: &mut ViewContext<Self>) -> AnyElement {
let buffer_font = ThemeSettings::get_global(cx).buffer_font.family.clone();
let buffer_font = ThemeSettings::get_global(cx).buffer_font.clone();
let Some(state) = &self.selected_path else {
return div().into_any();
};
Expand All @@ -142,7 +142,7 @@ impl ProjectIndexDebugView {
div()
.text_ui(cx)
.w_full()
.font_family(buffer_font)
.font(buffer_font)
.child(
h_flex()
.justify_between()
Expand Down
5 changes: 4 additions & 1 deletion crates/terminal/src/terminal_settings.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use collections::HashMap;
use gpui::{px, AbsoluteLength, AppContext, FontFeatures, Pixels};
use gpui::{px, AbsoluteLength, AppContext, FontFeatures, FontWeight, Pixels};
use schemars::{
gen::SchemaGenerator,
schema::{InstanceType, RootSchema, Schema, SchemaObject},
Expand Down Expand Up @@ -31,6 +31,7 @@ pub struct TerminalSettings {
pub font_family: Option<String>,
pub line_height: TerminalLineHeight,
pub font_features: Option<FontFeatures>,
pub font_weight: Option<FontWeight>,
pub env: HashMap<String, String>,
pub blinking: TerminalBlink,
pub alternate_scroll: AlternateScroll,
Expand Down Expand Up @@ -114,6 +115,8 @@ pub struct TerminalSettingsContent {
/// Default: comfortable
pub line_height: Option<TerminalLineHeight>,
pub font_features: Option<FontFeatures>,
/// Sets the terminal's font weight in CSS weight units 0-900.
pub font_weight: Option<f32>,
/// Any key-value pairs added to this list will be added to the terminal's
/// environment. Use `:` to separate multiple values.
///
Expand Down
4 changes: 3 additions & 1 deletion crates/terminal_view/src/terminal_element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,8 @@ impl Element for TerminalElement {
.clone()
.unwrap_or(settings.buffer_font.features.clone());

let font_weight = terminal_settings.font_weight.unwrap_or_default();

let line_height = terminal_settings.line_height.value();
let font_size = terminal_settings.font_size;

Expand All @@ -617,6 +619,7 @@ impl Element for TerminalElement {
let text_style = TextStyle {
font_family,
font_features,
font_weight,
font_size: font_size.into(),
font_style: FontStyle::Normal,
line_height: line_height.into(),
Expand All @@ -626,7 +629,6 @@ impl Element for TerminalElement {
underline: None,
strikethrough: None,
color: theme.colors().text,
font_weight: FontWeight::NORMAL,
};

let text_system = cx.text_system();
Expand Down
20 changes: 9 additions & 11 deletions crates/theme/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,16 +227,7 @@ pub struct ThemeSettingsContent {
/// The OpenType features to enable for text in the UI.
#[serde(default)]
pub ui_font_features: Option<FontFeatures>,
/// The weight of the font from 100 to 900.
/// - **100**: Thin (Hairline)
/// - **200**: Extra Light (Ultra Light)
/// - **300**: Light
/// - **400**: Normal (Regular or Roman)
/// - **500**: Medium
/// - **600**: Semi Bold (Demi Bold)
/// - **700**: Bold
/// - **800**: Extra Bold (Ultra Bold)
/// - **900**: Black (Heavy)
/// The weight of the UI font in CSS units from 100 to 900.
#[serde(default)]
pub ui_font_weight: Option<f32>,
/// The name of a font to use for rendering in text buffers.
Expand All @@ -245,6 +236,9 @@ pub struct ThemeSettingsContent {
/// The default font size for rendering in text buffers.
#[serde(default)]
pub buffer_font_size: Option<f32>,
/// The weight of the editor font in CSS units from 100 to 900.
#[serde(default)]
pub buffer_font_weight: Option<f32>,
/// The buffer's line height.
#[serde(default)]
pub buffer_line_height: Option<BufferLineHeight>,
Expand Down Expand Up @@ -404,7 +398,7 @@ impl settings::Settings for ThemeSettings {
buffer_font: Font {
family: defaults.buffer_font_family.clone().unwrap().into(),
features: defaults.buffer_font_features.clone().unwrap(),
weight: FontWeight::default(),
weight: defaults.buffer_font_weight.map(FontWeight).unwrap(),
style: FontStyle::default(),
},
buffer_font_size: defaults.buffer_font_size.unwrap().into(),
Expand All @@ -430,6 +424,10 @@ impl settings::Settings for ThemeSettings {
this.buffer_font.features = value;
}

if let Some(value) = value.buffer_font_weight {
this.buffer_font.weight = FontWeight(value);
}

if let Some(value) = value.ui_font_family.clone() {
this.ui_font.family = value.into();
}
Expand Down
2 changes: 1 addition & 1 deletion crates/ui_text_field/src/ui_text_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ impl Render for TextField {
font_family: settings.buffer_font.family.clone(),
font_features: settings.buffer_font.features.clone(),
font_size: rems(0.875).into(),
font_weight: FontWeight::NORMAL,
font_weight: settings.buffer_font.weight,
font_style: FontStyle::Normal,
line_height: relative(1.2),
color: style.text_color,
Expand Down

0 comments on commit c4c2728

Please sign in to comment.