Skip to content

Commit 1fd65e6

Browse files
committed
Extract color overrides from attrs_list into new color_spans field
1 parent 208a45e commit 1fd65e6

File tree

16 files changed

+303
-168
lines changed

16 files changed

+303
-168
lines changed

examples/editor-libcosmic/src/main.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ pub struct Window {
8888
theme: Theme,
8989
path_opt: Option<PathBuf>,
9090
attrs: Attrs,
91+
color: Option<cosmic_text::Color>,
9192
font_size: FontSize,
9293
#[cfg(not(feature = "vi"))]
9394
editor: Mutex<SyntaxEditor<'static>>,
@@ -112,7 +113,7 @@ pub enum Message {
112113
impl Window {
113114
pub fn open(&mut self, path: PathBuf) {
114115
let mut editor = self.editor.lock().unwrap();
115-
match editor.load_text(&path, &self.attrs) {
116+
match editor.load_text(&path, &self.attrs, self.color) {
116117
Ok(()) => {
117118
log::info!("opened '{}'", path.display());
118119
self.path_opt = Some(path);
@@ -154,6 +155,7 @@ impl Application for Window {
154155
font_size: FontSize::Body,
155156
path_opt: None,
156157
attrs,
158+
color: None,
157159
editor: Mutex::new(editor),
158160
};
159161
if let Some(arg) = env::args().nth(1) {
@@ -258,15 +260,12 @@ impl Application for Window {
258260

259261
let Color { r, g, b, a } = self.theme.palette().text;
260262
let as_u8 = |component: f32| (component * 255.0) as u8;
261-
self.attrs.color_opt = Some(cosmic_text::Color::rgba(
263+
self.color = Some(cosmic_text::Color::rgba(
262264
as_u8(r),
263265
as_u8(g),
264266
as_u8(b),
265267
as_u8(a),
266268
));
267-
268-
let mut editor = self.editor.lock().unwrap();
269-
update_attrs(&mut *editor, &self.attrs);
270269
}
271270
}
272271

@@ -349,7 +348,7 @@ impl Application for Window {
349348
]
350349
.align_items(Alignment::Center)
351350
.spacing(8),
352-
text_box(&self.editor)
351+
text_box(&self.editor, self.color)
353352
]
354353
.spacing(8)
355354
.padding(16)

examples/editor-libcosmic/src/text.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use cosmic::{
1111
iced_winit::renderer::BorderRadius,
1212
theme::Theme,
1313
};
14-
use cosmic_text::{Attrs, AttrsList, BufferLine, Metrics, SwashCache};
14+
use cosmic_text::{Attrs, AttrsList, BufferLine, Metrics, Spans, SwashCache};
1515
use std::{cmp, sync::Mutex, time::Instant};
1616

1717
use crate::FONT_SYSTEM;
@@ -50,7 +50,7 @@ impl Text {
5050
let instant = Instant::now();
5151

5252
//TODO: make it possible to set attrs
53-
let mut line = BufferLine::new(string, AttrsList::new(Attrs::new()));
53+
let mut line = BufferLine::new(string, AttrsList::new(Attrs::new()), Spans::default());
5454

5555
//TODO: do we have to immediately shape?
5656
line.shape(&crate::FONT_SYSTEM);
@@ -180,8 +180,8 @@ where
180180
for glyph in layout_line.glyphs.iter() {
181181
let (cache_key, x_int, y_int) = (glyph.cache_key, glyph.x_int, glyph.y_int);
182182

183-
let glyph_color = match glyph.color_opt {
184-
Some(some) => some,
183+
let glyph_color = match self.line.color_spans().get(glyph.start) {
184+
Some(some) => *some,
185185
None => text_color,
186186
};
187187

examples/editor-libcosmic/src/text_box.rs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,15 @@ impl StyleSheet for Theme {
4545

4646
pub struct TextBox<'a, Editor> {
4747
editor: &'a Mutex<Editor>,
48+
color: Option<cosmic_text::Color>,
4849
padding: Padding,
4950
}
5051

5152
impl<'a, Editor> TextBox<'a, Editor> {
52-
pub fn new(editor: &'a Mutex<Editor>) -> Self {
53+
pub fn new(editor: &'a Mutex<Editor>, color: Option<cosmic_text::Color>) -> Self {
5354
Self {
5455
editor,
56+
color,
5557
padding: Padding::new(0),
5658
}
5759
}
@@ -62,8 +64,11 @@ impl<'a, Editor> TextBox<'a, Editor> {
6264
}
6365
}
6466

65-
pub fn text_box<Editor>(editor: &Mutex<Editor>) -> TextBox<Editor> {
66-
TextBox::new(editor)
67+
pub fn text_box<Editor>(
68+
editor: &Mutex<Editor>,
69+
color: Option<cosmic_text::Color>,
70+
) -> TextBox<Editor> {
71+
TextBox::new(editor, color)
6772
}
6873

6974
impl<'a, 'editor, Editor, Message, Renderer> Widget<Message, Renderer> for TextBox<'a, Editor>
@@ -151,12 +156,14 @@ where
151156
);
152157
}
153158

154-
let text_color = cosmic_text::Color::rgba(
155-
cmp::max(0, cmp::min(255, (appearance.text_color.r * 255.0) as i32)) as u8,
156-
cmp::max(0, cmp::min(255, (appearance.text_color.g * 255.0) as i32)) as u8,
157-
cmp::max(0, cmp::min(255, (appearance.text_color.b * 255.0) as i32)) as u8,
158-
cmp::max(0, cmp::min(255, (appearance.text_color.a * 255.0) as i32)) as u8,
159-
);
159+
let text_color = self.color.unwrap_or_else(|| {
160+
cosmic_text::Color::rgba(
161+
cmp::max(0, cmp::min(255, (appearance.text_color.r * 255.0) as i32)) as u8,
162+
cmp::max(0, cmp::min(255, (appearance.text_color.g * 255.0) as i32)) as u8,
163+
cmp::max(0, cmp::min(255, (appearance.text_color.b * 255.0) as i32)) as u8,
164+
cmp::max(0, cmp::min(255, (appearance.text_color.a * 255.0) as i32)) as u8,
165+
)
166+
});
160167

161168
let mut editor = self.editor.lock().unwrap();
162169

examples/editor-orbclient/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ fn main() {
7575
.monospaced(true)
7676
.family(Family::Monospace)
7777
.build();
78-
match editor.load_text(&path, attrs) {
78+
match editor.load_text(&path, attrs, None) {
7979
Ok(()) => (),
8080
Err(err) => {
8181
log::error!("failed to load {:?}: {}", path, err);

examples/rich-text/src/main.rs

Lines changed: 81 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
use cosmic_text::{
44
Action, Attrs, AttrsBuilder, AttrsList, Buffer, BufferLine, Color, Edit, Editor, Family,
5-
FontSystem, Metrics, Style, SwashCache, Weight,
5+
FontSystem, Metrics, Spans, Style, SwashCache, Weight,
66
};
77
use orbclient::{EventOption, Renderer, Window, WindowFlag};
88
use std::{
@@ -52,96 +52,129 @@ fn main() {
5252

5353
editor.buffer_mut().lines.clear();
5454

55-
let lines: &[&[(&str, AttrsBuilder)]] = &[
55+
let lines: &[&[(&str, AttrsBuilder, Option<Color>)]] = &[
5656
&[
57-
("B", attrs.clone().weight(Weight::BOLD)),
58-
("old ", attrs.clone()),
59-
("I", attrs.clone().style(Style::Italic)),
60-
("talic ", attrs.clone()),
61-
("f", attrs.clone()),
62-
("i ", attrs.clone()),
63-
("f", attrs.clone().weight(Weight::BOLD)),
64-
("i ", attrs.clone()),
65-
("f", attrs.clone().style(Style::Italic)),
66-
("i ", attrs.clone()),
57+
("B", attrs.clone().weight(Weight::BOLD), None),
58+
("old ", attrs.clone(), None),
59+
("I", attrs.clone().style(Style::Italic), None),
60+
("talic ", attrs.clone(), None),
61+
("f", attrs.clone(), None),
62+
("i ", attrs.clone(), None),
63+
("f", attrs.clone().weight(Weight::BOLD), None),
64+
("i ", attrs.clone(), None),
65+
("f", attrs.clone().style(Style::Italic), None),
66+
("i ", attrs.clone(), None),
6767
],
6868
&[
69-
("Sans-Serif Normal ", attrs.clone()),
70-
("Sans-Serif Bold ", attrs.clone().weight(Weight::BOLD)),
71-
("Sans-Serif Italic ", attrs.clone().style(Style::Italic)),
69+
("Sans-Serif Normal ", attrs.clone(), None),
70+
("Sans-Serif Bold ", attrs.clone().weight(Weight::BOLD), None),
71+
(
72+
"Sans-Serif Italic ",
73+
attrs.clone().style(Style::Italic),
74+
None,
75+
),
7276
(
7377
"Sans-Serif Bold Italic",
7478
attrs.clone().weight(Weight::BOLD).style(Style::Italic),
79+
None,
7580
),
7681
],
7782
&[
78-
("Serif Normal ", serif_attrs.clone()),
79-
("Serif Bold ", serif_attrs.clone().weight(Weight::BOLD)),
80-
("Serif Italic ", serif_attrs.clone().style(Style::Italic)),
83+
("Serif Normal ", serif_attrs.clone(), None),
84+
(
85+
"Serif Bold ",
86+
serif_attrs.clone().weight(Weight::BOLD),
87+
None,
88+
),
89+
(
90+
"Serif Italic ",
91+
serif_attrs.clone().style(Style::Italic),
92+
None,
93+
),
8194
(
8295
"Serif Bold Italic",
8396
serif_attrs.weight(Weight::BOLD).style(Style::Italic),
97+
None,
8498
),
8599
],
86100
&[
87-
("Mono Normal ", mono_attrs.clone()),
88-
("Mono Bold ", mono_attrs.clone().weight(Weight::BOLD)),
89-
("Mono Italic ", mono_attrs.clone().style(Style::Italic)),
101+
("Mono Normal ", mono_attrs.clone(), None),
102+
("Mono Bold ", mono_attrs.clone().weight(Weight::BOLD), None),
103+
(
104+
"Mono Italic ",
105+
mono_attrs.clone().style(Style::Italic),
106+
None,
107+
),
90108
(
91109
"Mono Bold Italic",
92110
mono_attrs.weight(Weight::BOLD).style(Style::Italic),
111+
None,
93112
),
94113
],
95114
&[
96-
("Comic Normal ", comic_attrs.clone()),
97-
("Comic Bold ", comic_attrs.clone().weight(Weight::BOLD)),
98-
("Comic Italic ", comic_attrs.clone().style(Style::Italic)),
115+
("Comic Normal ", comic_attrs.clone(), None),
116+
(
117+
"Comic Bold ",
118+
comic_attrs.clone().weight(Weight::BOLD),
119+
None,
120+
),
121+
(
122+
"Comic Italic ",
123+
comic_attrs.clone().style(Style::Italic),
124+
None,
125+
),
99126
(
100127
"Comic Bold Italic",
101128
comic_attrs.weight(Weight::BOLD).style(Style::Italic),
129+
None,
102130
),
103131
],
104132
&[
105-
("R", attrs.clone().color(Color::rgb(0xFF, 0x00, 0x00))),
106-
("A", attrs.clone().color(Color::rgb(0xFF, 0x7F, 0x00))),
107-
("I", attrs.clone().color(Color::rgb(0xFF, 0xFF, 0x00))),
108-
("N", attrs.clone().color(Color::rgb(0x00, 0xFF, 0x00))),
109-
("B", attrs.clone().color(Color::rgb(0x00, 0x00, 0xFF))),
110-
("O", attrs.clone().color(Color::rgb(0x4B, 0x00, 0x82))),
111-
("W ", attrs.clone().color(Color::rgb(0x94, 0x00, 0xD3))),
112-
("Red ", attrs.clone().color(Color::rgb(0xFF, 0x00, 0x00))),
113-
("Orange ", attrs.clone().color(Color::rgb(0xFF, 0x7F, 0x00))),
114-
("Yellow ", attrs.clone().color(Color::rgb(0xFF, 0xFF, 0x00))),
115-
("Green ", attrs.clone().color(Color::rgb(0x00, 0xFF, 0x00))),
116-
("Blue ", attrs.clone().color(Color::rgb(0x00, 0x00, 0xFF))),
117-
("Indigo ", attrs.clone().color(Color::rgb(0x4B, 0x00, 0x82))),
118-
("Violet ", attrs.clone().color(Color::rgb(0x94, 0x00, 0xD3))),
119-
("U", attrs.clone().color(Color::rgb(0x94, 0x00, 0xD3))),
120-
("N", attrs.clone().color(Color::rgb(0x4B, 0x00, 0x82))),
121-
("I", attrs.clone().color(Color::rgb(0x00, 0x00, 0xFF))),
122-
("C", attrs.clone().color(Color::rgb(0x00, 0xFF, 0x00))),
123-
("O", attrs.clone().color(Color::rgb(0xFF, 0xFF, 0x00))),
124-
("R", attrs.clone().color(Color::rgb(0xFF, 0x7F, 0x00))),
125-
("N", attrs.clone().color(Color::rgb(0xFF, 0x00, 0x00))),
133+
("R", attrs.clone(), Some(Color::rgb(0xFF, 0x00, 0x00))),
134+
("A", attrs.clone(), Some(Color::rgb(0xFF, 0x7F, 0x00))),
135+
("I", attrs.clone(), Some(Color::rgb(0xFF, 0xFF, 0x00))),
136+
("N", attrs.clone(), Some(Color::rgb(0x00, 0xFF, 0x00))),
137+
("B", attrs.clone(), Some(Color::rgb(0x00, 0x00, 0xFF))),
138+
("O", attrs.clone(), Some(Color::rgb(0x4B, 0x00, 0x82))),
139+
("W ", attrs.clone(), Some(Color::rgb(0x94, 0x00, 0xD3))),
140+
("Red ", attrs.clone(), Some(Color::rgb(0xFF, 0x00, 0x00))),
141+
("Orange ", attrs.clone(), Some(Color::rgb(0xFF, 0x7F, 0x00))),
142+
("Yellow ", attrs.clone(), Some(Color::rgb(0xFF, 0xFF, 0x00))),
143+
("Green ", attrs.clone(), Some(Color::rgb(0x00, 0xFF, 0x00))),
144+
("Blue ", attrs.clone(), Some(Color::rgb(0x00, 0x00, 0xFF))),
145+
("Indigo ", attrs.clone(), Some(Color::rgb(0x4B, 0x00, 0x82))),
146+
("Violet ", attrs.clone(), Some(Color::rgb(0x94, 0x00, 0xD3))),
147+
("U", attrs.clone(), Some(Color::rgb(0x94, 0x00, 0xD3))),
148+
("N", attrs.clone(), Some(Color::rgb(0x4B, 0x00, 0x82))),
149+
("I", attrs.clone(), Some(Color::rgb(0x00, 0x00, 0xFF))),
150+
("C", attrs.clone(), Some(Color::rgb(0x00, 0xFF, 0x00))),
151+
("O", attrs.clone(), Some(Color::rgb(0xFF, 0xFF, 0x00))),
152+
("R", attrs.clone(), Some(Color::rgb(0xFF, 0x7F, 0x00))),
153+
("N", attrs.clone(), Some(Color::rgb(0xFF, 0x00, 0x00))),
126154
],
127155
&[(
128156
"生活,삶,जिंदगी 😀 FPS",
129-
attrs.clone().color(Color::rgb(0xFF, 0x00, 0x00)),
157+
attrs.clone(),
158+
Some(Color::rgb(0xFF, 0x00, 0x00)),
130159
)],
131160
];
132161
for line in lines {
133162
let mut line_text = String::new();
134163
let mut attrs_list = AttrsList::new(attrs.clone().build());
135-
for (text, attrs) in line.iter() {
164+
let mut color_spans = Spans::<Color>::default();
165+
for (text, attrs, color) in line.iter() {
136166
let start = line_text.len();
137167
line_text.push_str(text);
138168
let end = line_text.len();
139169
attrs_list.add_span(start..end, attrs.clone().build());
170+
if let Some(color) = color {
171+
color_spans.add(start..end, *color);
172+
}
140173
}
141174
editor
142175
.buffer_mut()
143176
.lines
144-
.push(BufferLine::new(line_text, attrs_list));
177+
.push(BufferLine::new(line_text, attrs_list, color_spans));
145178
}
146179

147180
let mut swash_cache = SwashCache::new();

examples/terminal/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ fn main() {
2626
let attrs = Attrs::new();
2727

2828
// Add some text!
29-
buffer.set_text(" Hi, Rust! 🦀", attrs);
29+
buffer.set_text(" Hi, Rust! 🦀", attrs, None);
3030

3131
// Perform shaping as desired
3232
buffer.shape_until_scroll();

0 commit comments

Comments
 (0)