Use custom color wrapper type everywhere in gpui & zed

Co-Authored-By: Nathan Sobo <nathan@zed.dev>
This commit is contained in:
Max Brunsfeld
2021-08-03 12:48:58 -07:00
co-authored by Nathan Sobo
parent fa01273466
commit ef0ffbe819
20 changed files with 319 additions and 233 deletions
+5 -5
View File
@@ -16,7 +16,7 @@ pub use display_map::DisplayPoint;
use display_map::*;
pub use element::*;
use gpui::{
color::ColorU, font_cache::FamilyId, fonts::Properties as FontProperties,
color::Color, font_cache::FamilyId, fonts::Properties as FontProperties,
geometry::vector::Vector2F, keymap::Binding, text_layout, AppContext, ClipboardItem, Element,
ElementBox, Entity, FontCache, ModelHandle, MutableAppContext, RenderContext, Task,
TextLayoutCache, View, ViewContext, WeakViewHandle,
@@ -2349,7 +2349,7 @@ impl Snapshot {
.layout_str(
"1".repeat(digit_count).as_str(),
font_size,
&[(digit_count, font_id, ColorU::black())],
&[(digit_count, font_id, Color::black())],
)
.width())
}
@@ -2374,9 +2374,9 @@ impl Snapshot {
{
let display_row = rows.start + ix as u32;
let color = if active_rows.contains_key(&display_row) {
theme.editor.line_number_active.0
theme.editor.line_number_active
} else {
theme.editor.line_number.0
theme.editor.line_number
};
if soft_wrapped {
layouts.push(None);
@@ -2485,7 +2485,7 @@ impl Snapshot {
&[(
self.display_snapshot.line_len(row) as usize,
font_id,
ColorU::black(),
Color::black(),
)],
))
}
+5 -5
View File
@@ -340,7 +340,7 @@ mod tests {
util::RandomCharIter,
};
use buffer::{History, SelectionGoal};
use gpui::{color::ColorU, MutableAppContext};
use gpui::{color::Color, MutableAppContext};
use rand::{prelude::StdRng, Rng};
use std::{env, sync::Arc};
use Bias::*;
@@ -656,12 +656,12 @@ mod tests {
syntax: vec![
(
"mod.body".to_string(),
ColorU::from_u32(0xff0000ff),
Color::from_u32(0xff0000ff),
Default::default(),
),
(
"fn.name".to_string(),
ColorU::from_u32(0x00ff00ff),
Color::from_u32(0x00ff00ff),
Default::default(),
),
],
@@ -754,12 +754,12 @@ mod tests {
syntax: vec![
(
"mod.body".to_string(),
ColorU::from_u32(0xff0000ff),
Color::from_u32(0xff0000ff),
Default::default(),
),
(
"fn.name".to_string(),
ColorU::from_u32(0x00ff00ff),
Color::from_u32(0x00ff00ff),
Default::default(),
),
],
+11 -11
View File
@@ -1,7 +1,7 @@
use super::{DisplayPoint, Editor, SelectAction, Snapshot};
use crate::time::ReplicaId;
use gpui::{
color::ColorU,
color::Color,
geometry::{
rect::RectF,
vector::{vec2f, Vector2F},
@@ -196,14 +196,14 @@ impl EditorElement {
let theme = &settings.theme;
cx.scene.push_quad(Quad {
bounds: gutter_bounds,
background: Some(theme.editor.gutter_background.0),
border: Border::new(0., ColorU::transparent_black()),
background: Some(theme.editor.gutter_background),
border: Border::new(0., Color::transparent_black()),
corner_radius: 0.,
});
cx.scene.push_quad(Quad {
bounds: text_bounds,
background: Some(theme.editor.background.0),
border: Border::new(0., ColorU::transparent_black()),
background: Some(theme.editor.background),
border: Border::new(0., Color::transparent_black()),
corner_radius: 0.,
});
@@ -229,7 +229,7 @@ impl EditorElement {
);
cx.scene.push_quad(Quad {
bounds: RectF::new(origin, size),
background: Some(theme.editor.active_line_background.0),
background: Some(theme.editor.active_line_background),
border: Border::default(),
corner_radius: 0.,
});
@@ -290,7 +290,7 @@ impl EditorElement {
};
let selection = Selection {
color: replica_theme.selection.0,
color: replica_theme.selection,
line_height: layout.line_height,
start_y: content_origin.y() + row_range.start as f32 * layout.line_height
- scroll_top,
@@ -333,7 +333,7 @@ impl EditorElement {
- scroll_left;
let y = selection.end.row() as f32 * layout.line_height - scroll_top;
cursors.push(Cursor {
color: replica_theme.cursor.0,
color: replica_theme.cursor,
origin: content_origin + vec2f(x, y),
line_height: layout.line_height,
});
@@ -707,7 +707,7 @@ impl PaintState {
struct Cursor {
origin: Vector2F,
line_height: f32,
color: ColorU,
color: Color,
}
impl Cursor {
@@ -715,7 +715,7 @@ impl Cursor {
cx.scene.push_quad(Quad {
bounds: RectF::new(self.origin, vec2f(2.0, self.line_height)),
background: Some(self.color),
border: Border::new(0., ColorU::black()),
border: Border::new(0., Color::black()),
corner_radius: 0.,
});
}
@@ -726,7 +726,7 @@ struct Selection {
start_y: f32,
line_height: f32,
lines: Vec<SelectionLine>,
color: ColorU,
color: Color,
}
#[derive(Debug)]
+8 -8
View File
@@ -6,7 +6,7 @@ use crate::{
worktree::{match_paths, PathMatch, Worktree},
};
use gpui::{
color::ColorF,
color::Color,
elements::*,
fonts::{Properties, Weight},
geometry::vector::vec2f,
@@ -82,7 +82,7 @@ impl View for FileFinder {
.with_uniform_padding(6.0)
.with_corner_radius(6.0)
.with_background_color(settings.theme.ui.modal_background)
.with_shadow(vec2f(0., 4.), 12., ColorF::new(0.0, 0.0, 0.0, 0.5).to_u8())
.with_shadow(vec2f(0., 4.), 12., Color::new(0, 0, 0, 128))
.boxed(),
)
.with_max_width(600.0)
@@ -114,7 +114,7 @@ impl FileFinder {
settings.ui_font_family,
settings.ui_font_size,
)
.with_default_color(settings.theme.editor.default_text.0)
.with_default_color(settings.theme.editor.default_text)
.boxed(),
)
.with_margin_top(6.0)
@@ -155,9 +155,9 @@ impl FileFinder {
let bold = *Properties::new().weight(Weight::BOLD);
let selected_index = self.selected_index();
let label_style = LabelStyle {
default_color: theme.modal_match_text.0,
highlight_color: theme.modal_match_text_highlight.0,
highlight_font_properties: bold,
color: theme.modal_match_text,
highlight_color: Some(theme.modal_match_text_highlight),
highlight_font_properties: Some(bold),
..Default::default()
};
let mut container = Container::new(
@@ -206,9 +206,9 @@ impl FileFinder {
)
.with_uniform_padding(6.0)
.with_background_color(if index == selected_index {
theme.modal_match_background_active.0
theme.modal_match_background_active
} else {
theme.modal_match_background.0
theme.modal_match_background
});
if index == selected_index || index < self.matches.len() - 1 {
+27 -81
View File
@@ -1,6 +1,6 @@
use anyhow::{anyhow, Context, Result};
use gpui::{
color::ColorU,
color::Color,
font_cache::{FamilyId, FontCache},
fonts::{Properties as FontProperties, Style as FontStyle, Weight as FontWeight},
AssetSource,
@@ -9,12 +9,7 @@ use parking_lot::Mutex;
use postage::watch;
use serde::{de::value::MapDeserializer, Deserialize};
use serde_json::Value;
use std::{
collections::HashMap,
fmt,
ops::{Deref, DerefMut},
sync::Arc,
};
use std::{collections::HashMap, sync::Arc};
const DEFAULT_STYLE_ID: StyleId = StyleId(u32::MAX);
@@ -38,7 +33,7 @@ pub struct ThemeRegistry {
pub struct Theme {
pub ui: UiTheme,
pub editor: EditorTheme,
pub syntax: Vec<(String, ColorU, FontProperties)>,
pub syntax: Vec<(String, Color, FontProperties)>,
}
#[derive(Deserialize)]
@@ -93,9 +88,6 @@ pub struct ReplicaTheme {
pub selection: Color,
}
#[derive(Clone, Copy, Default)]
pub struct Color(pub ColorU);
#[derive(Clone, Debug)]
pub struct ThemeMap(Arc<[StyleId]>);
@@ -151,7 +143,7 @@ impl ThemeRegistry {
}
let theme_toml = self.load(name)?;
let mut syntax = Vec::<(String, ColorU, FontProperties)>::new();
let mut syntax = Vec::<(String, Color, FontProperties)>::new();
for (key, style) in theme_toml.syntax.iter() {
let mut color = Color::default();
let mut properties = FontProperties::new();
@@ -171,7 +163,7 @@ impl ThemeRegistry {
}
match syntax.binary_search_by_key(&key, |e| &e.0) {
Ok(i) | Err(i) => {
syntax.insert(i, (key.to_string(), color.0, properties));
syntax.insert(i, (key.to_string(), color, properties));
}
}
}
@@ -234,11 +226,12 @@ impl ThemeRegistry {
}
impl Theme {
pub fn syntax_style(&self, id: StyleId) -> (ColorU, FontProperties) {
self.syntax.get(id.0 as usize).map_or(
(self.editor.default_text.0, FontProperties::new()),
|entry| (entry.1, entry.2),
)
pub fn syntax_style(&self, id: StyleId) -> (Color, FontProperties) {
self.syntax
.get(id.0 as usize)
.map_or((self.editor.default_text, FontProperties::new()), |entry| {
(entry.1, entry.2)
})
}
#[cfg(test)]
@@ -313,53 +306,6 @@ impl Default for StyleId {
}
}
impl Color {
fn from_u32(rgba: u32) -> Self {
Self(ColorU::from_u32(rgba))
}
}
impl<'de> Deserialize<'de> for Color {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let rgb = u32::deserialize(deserializer)?;
Ok(Self::from_u32((rgb << 8) + 0xFF))
}
}
impl Into<ColorU> for Color {
fn into(self) -> ColorU {
self.0
}
}
impl Deref for Color {
type Target = ColorU;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl DerefMut for Color {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl fmt::Debug for Color {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
impl PartialEq<ColorU> for Color {
fn eq(&self, other: &ColorU) -> bool {
self.0.eq(other)
}
}
pub fn channel(
font_cache: &FontCache,
) -> Result<(watch::Sender<Settings>, watch::Receiver<Settings>)> {
@@ -478,25 +424,25 @@ mod tests {
let registry = ThemeRegistry::new(assets);
let theme = registry.get("my-theme").unwrap();
assert_eq!(theme.ui.tab_background_active, ColorU::from_u32(0x100000ff));
assert_eq!(theme.editor.background, ColorU::from_u32(0x00ed00ff));
assert_eq!(theme.editor.line_number, ColorU::from_u32(0xddddddff));
assert_eq!(theme.ui.tab_background_active, Color::from_u32(0x100000ff));
assert_eq!(theme.editor.background, Color::from_u32(0x00ed00ff));
assert_eq!(theme.editor.line_number, Color::from_u32(0xddddddff));
assert_eq!(
theme.syntax,
&[
(
"alpha.one".to_string(),
ColorU::from_u32(0x112233ff),
Color::from_u32(0x112233ff),
*FontProperties::new().weight(FontWeight::BOLD)
),
(
"beta.two".to_string(),
ColorU::from_u32(0xaabbccff),
Color::from_u32(0xaabbccff),
*FontProperties::new().weight(FontWeight::NORMAL)
),
(
"gamma.three".to_string(),
ColorU::from_u32(0x00000000),
Color::from_u32(0x00000000),
*FontProperties::new()
.weight(FontWeight::LIGHT)
.style(FontStyle::Italic),
@@ -553,10 +499,10 @@ mod tests {
let registry = ThemeRegistry::new(assets);
let theme = registry.get("light").unwrap();
assert_eq!(theme.ui.tab_background, ColorU::from_u32(0x555555ff));
assert_eq!(theme.ui.tab_text, ColorU::from_u32(0x333333ff));
assert_eq!(theme.editor.background, ColorU::from_u32(0x666666ff));
assert_eq!(theme.editor.default_text, ColorU::from_u32(0x444444ff));
assert_eq!(theme.ui.tab_background, Color::from_u32(0x555555ff));
assert_eq!(theme.ui.tab_text, Color::from_u32(0x333333ff));
assert_eq!(theme.editor.background, Color::from_u32(0x666666ff));
assert_eq!(theme.editor.default_text, Color::from_u32(0x444444ff));
assert_eq!(
registry.list().collect::<Vec<_>>(),
@@ -577,12 +523,12 @@ mod tests {
ui: Default::default(),
editor: Default::default(),
syntax: [
("function", ColorU::from_u32(0x100000ff)),
("function.method", ColorU::from_u32(0x200000ff)),
("function.async", ColorU::from_u32(0x300000ff)),
("variable.builtin.self.rust", ColorU::from_u32(0x400000ff)),
("variable.builtin", ColorU::from_u32(0x500000ff)),
("variable", ColorU::from_u32(0x600000ff)),
("function", Color::from_u32(0x100000ff)),
("function.method", Color::from_u32(0x200000ff)),
("function.async", Color::from_u32(0x300000ff)),
("variable.builtin.self.rust", Color::from_u32(0x400000ff)),
("variable.builtin", Color::from_u32(0x500000ff)),
("variable", Color::from_u32(0x600000ff)),
]
.iter()
.map(|e| (e.0.to_string(), e.1, FontProperties::new()))
+8 -8
View File
@@ -9,7 +9,7 @@ use crate::{
};
use futures::lock::Mutex;
use gpui::{
color::ColorF,
color::Color,
elements::{
Align, ChildView, ConstrainedBox, Container, Expanded, Flex, Label, LabelStyle,
ParentElement, UniformList, UniformListState,
@@ -199,7 +199,7 @@ impl ThemeSelector {
settings.ui_font_family,
settings.ui_font_size,
)
.with_default_color(settings.theme.editor.default_text.0)
.with_default_color(settings.theme.editor.default_text)
.boxed(),
)
.with_margin_top(6.0)
@@ -241,9 +241,9 @@ impl ThemeSelector {
settings.ui_font_size,
)
.with_style(&LabelStyle {
default_color: theme.modal_match_text.0,
highlight_color: theme.modal_match_text_highlight.0,
highlight_font_properties: *Properties::new().weight(Weight::BOLD),
color: theme.modal_match_text,
highlight_color: Some(theme.modal_match_text_highlight),
highlight_font_properties: Some(*Properties::new().weight(Weight::BOLD)),
..Default::default()
})
.with_highlights(theme_match.positions.clone())
@@ -251,9 +251,9 @@ impl ThemeSelector {
)
.with_uniform_padding(6.0)
.with_background_color(if index == self.selected_index {
theme.modal_match_background_active.0
theme.modal_match_background_active
} else {
theme.modal_match_background.0
theme.modal_match_background
});
if index == self.selected_index || index < self.matches.len() - 1 {
@@ -288,7 +288,7 @@ impl View for ThemeSelector {
.with_uniform_padding(6.0)
.with_corner_radius(6.0)
.with_background_color(settings.theme.ui.modal_background)
.with_shadow(vec2f(0., 4.), 12., ColorF::new(0.0, 0.0, 0.0, 0.5).to_u8())
.with_shadow(vec2f(0., 4.), 12., Color::new(0, 0, 0, 128))
.boxed(),
)
.with_max_width(600.0)
+7 -7
View File
@@ -1,7 +1,7 @@
use super::{ItemViewHandle, SplitDirection};
use crate::settings::{Settings, UiTheme};
use gpui::{
color::ColorU,
color::Color,
elements::*,
geometry::{rect::RectF, vector::vec2f},
keymap::Binding,
@@ -200,7 +200,7 @@ impl Pane {
MouseEventHandler::new::<Tab, _>(item.id(), cx, |mouse_state| {
let title = item.title(cx);
let mut border = Border::new(1.0, theme.tab_border.0);
let mut border = Border::new(1.0, theme.tab_border);
border.left = ix > 0;
border.right = ix == last_item_ix;
border.bottom = ix != self.active_item;
@@ -215,9 +215,9 @@ impl Pane {
settings.ui_font_size,
)
.with_default_color(if is_active {
theme.tab_text_active.0
theme.tab_text_active
} else {
theme.tab_text.0
theme.tab_text
})
.boxed(),
)
@@ -317,12 +317,12 @@ impl Pane {
};
let icon = if tab_hovered {
let close_color = current_color.unwrap_or(theme.tab_icon_close).0;
let close_color = current_color.unwrap_or(theme.tab_icon_close);
let icon = Svg::new("icons/x.svg").with_color(close_color);
MouseEventHandler::new::<TabCloseButton, _>(item_id, cx, |mouse_state| {
if mouse_state.hovered {
Container::new(icon.with_color(ColorU::white()).boxed())
Container::new(icon.with_color(Color::white()).boxed())
.with_background_color(if mouse_state.clicked {
clicked_color
} else {
@@ -344,7 +344,7 @@ impl Pane {
let square = RectF::new(bounds.origin(), vec2f(diameter, diameter));
cx.scene.push_quad(Quad {
bounds: square,
background: Some(current_color.0),
background: Some(current_color),
border: Default::default(),
corner_radius: diameter / 2.,
});
+3 -7
View File
@@ -1,9 +1,5 @@
use anyhow::{anyhow, Result};
use gpui::{
color::{rgbu, ColorU},
elements::*,
Axis, Border,
};
use gpui::{color::Color, elements::*, Axis, Border};
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct PaneGroup {
@@ -388,6 +384,6 @@ fn border_width() -> f32 {
}
#[inline(always)]
fn border_color() -> ColorU {
rgbu(0xdb, 0xdb, 0xdc)
fn border_color() -> Color {
Color::new(0xdb, 0xdb, 0xdc, 0xff)
}