Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unify Attrs and AttrsOwned #100

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 22 additions & 18 deletions examples/editor-libcosmic/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ fn main() -> cosmic::iced::Result {
pub struct Window {
theme: Theme,
path_opt: Option<PathBuf>,
attrs: Attrs<'static>,
attrs: Attrs,
font_size: FontSize,
#[cfg(not(feature = "vi"))]
editor: Mutex<SyntaxEditor<'static>>,
Expand All @@ -111,7 +111,7 @@ impl Window {
let mut editor = self.editor.lock().unwrap();
let mut font_system = FONT_SYSTEM.lock().unwrap();
let mut editor = editor.borrow_with(&mut font_system);
match editor.load_text(&path, self.attrs) {
match editor.load_text(&path, &self.attrs) {
Ok(()) => {
log::info!("opened '{}'", path.display());
self.path_opt = Some(path);
Expand All @@ -131,8 +131,11 @@ impl Application for Window {
type Theme = Theme;

fn new(_flags: ()) -> (Self, Command<Self::Message>) {
let attrs = cosmic_text::Attrs::new().family(cosmic_text::Family::Monospace);
let attrs = cosmic_text::Attrs::builder()
.family(cosmic_text::Family::Monospace)
.build();

#[cfg_attr(feature = "vi", allow(unused_mut))]
let mut editor = SyntaxEditor::new(
Buffer::new(
&mut FONT_SYSTEM.lock().unwrap(),
Expand All @@ -146,7 +149,7 @@ impl Application for Window {
#[cfg(feature = "vi")]
let mut editor = cosmic_text::ViEditor::new(editor);

update_attrs(&mut editor, attrs);
update_attrs(&mut editor, &attrs);

let mut window = Window {
theme: Theme::dark(),
Expand Down Expand Up @@ -203,34 +206,35 @@ impl Application for Window {
}
}
Message::Bold(bold) => {
self.attrs = self.attrs.weight(if bold {
self.attrs.weight = if bold {
cosmic_text::Weight::BOLD
} else {
cosmic_text::Weight::NORMAL
});
};

let mut editor = self.editor.lock().unwrap();
update_attrs(&mut *editor, self.attrs);
update_attrs(&mut *editor, &self.attrs);
}
Message::Italic(italic) => {
self.attrs = self.attrs.style(if italic {
self.attrs.style = if italic {
cosmic_text::Style::Italic
} else {
cosmic_text::Style::Normal
});
};

let mut editor = self.editor.lock().unwrap();
update_attrs(&mut *editor, self.attrs);
update_attrs(&mut *editor, &self.attrs);
}
Message::Monospaced(monospaced) => {
self.attrs = self.attrs.family(if monospaced {
self.attrs.family_owned = if monospaced {
cosmic_text::Family::Monospace
} else {
cosmic_text::Family::SansSerif
});
}
.into();

let mut editor = self.editor.lock().unwrap();
update_attrs(&mut *editor, self.attrs);
update_attrs(&mut *editor, &self.attrs);
}
Message::FontSizeChanged(font_size) => {
self.font_size = font_size;
Expand Down Expand Up @@ -260,7 +264,7 @@ impl Application for Window {

let Color { r, g, b, a } = self.theme.cosmic().on_bg_color().into();
let as_u8 = |component: f32| (component * 255.0) as u8;
self.attrs = self.attrs.color(cosmic_text::Color::rgba(
self.attrs.color_opt = Some(cosmic_text::Color::rgba(
as_u8(r),
as_u8(g),
as_u8(b),
Expand All @@ -275,7 +279,7 @@ impl Application for Window {
"Dark" | _ => editor.update_theme("base16-eighties.dark"),
};

update_attrs(&mut *editor, self.attrs);
update_attrs(&mut *editor, &self.attrs);
}
}

Expand Down Expand Up @@ -335,7 +339,7 @@ impl Application for Window {
text("Monospaced:"),
toggler(
None,
self.attrs.family == cosmic_text::Family::Monospace,
self.attrs.family_owned == cosmic_text::FamilyOwned::Monospace,
Message::Monospaced
),
text("Theme:"),
Expand Down Expand Up @@ -374,9 +378,9 @@ impl Application for Window {
}
}

fn update_attrs<T: Edit>(editor: &mut T, attrs: Attrs) {
fn update_attrs<T: Edit>(editor: &mut T, attrs: &Attrs) {
editor.buffer_mut().lines.iter_mut().for_each(|line| {
line.set_attrs_list(AttrsList::new(attrs));
line.set_attrs_list(AttrsList::new(attrs.clone()));
});
}

Expand Down
4 changes: 2 additions & 2 deletions examples/editor-orbclient/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ fn main() {

let line_x = 8.0 * display_scale;

#[cfg_attr(feature = "vi", allow(unused_mut))]
let mut editor = SyntaxEditor::new(
Buffer::new(&mut font_system, font_sizes[font_size_i]),
&syntax_system,
Expand All @@ -72,8 +73,7 @@ fn main() {
editor
.buffer_mut()
.set_size(window.width() as f32 - line_x * 2.0, window.height() as f32);

let attrs = Attrs::new().family(Family::Monospace);
let attrs = Attrs::builder().family(Family::Monospace).build();
match editor.load_text(&path, attrs) {
Ok(()) => (),
Err(err) => {
Expand Down
112 changes: 56 additions & 56 deletions examples/rich-text/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// SPDX-License-Identifier: MIT OR Apache-2.0

use cosmic_text::{
Action, Attrs, AttrsList, Buffer, BufferLine, Color, Edit, Editor, Family, FontSystem, Metrics,
Shaping, Style, SwashCache, Weight,
Action, Attrs, AttrsBuilder, AttrsList, Buffer, BufferLine, Color, Edit, Editor, Family,
FontSystem, Metrics, Shaping, Style, SwashCache, Weight,
};
use orbclient::{EventOption, Renderer, Window, WindowFlag};
use std::{
Expand Down Expand Up @@ -47,98 +47,98 @@ fn main() {
.buffer_mut()
.set_size(window.width() as f32, window.height() as f32);

let attrs = Attrs::new();
let serif_attrs = attrs.family(Family::Serif);
let mono_attrs = attrs.family(Family::Monospace);
let comic_attrs = attrs.family(Family::Name("Comic Neue"));
let attrs = Attrs::builder();
let serif_attrs = attrs.clone().family(Family::Serif);
let mono_attrs = attrs.clone().family(Family::Monospace);
let comic_attrs = attrs.clone().family(Family::Name("Comic Neue"));

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

let lines: &[&[(&str, Attrs)]] = &[
let lines: &[&[(&str, AttrsBuilder)]] = &[
&[
("B", attrs.weight(Weight::BOLD)),
("old ", attrs),
("I", attrs.style(Style::Italic)),
("talic ", attrs),
("f", attrs),
("i ", attrs),
("f", attrs.weight(Weight::BOLD)),
("i ", attrs),
("f", attrs.style(Style::Italic)),
("i ", attrs),
("B", attrs.clone().weight(Weight::BOLD)),
("old ", attrs.clone()),
("I", attrs.clone().style(Style::Italic)),
("talic ", attrs.clone()),
("f", attrs.clone()),
("i ", attrs.clone()),
("f", attrs.clone().weight(Weight::BOLD)),
("i ", attrs.clone()),
("f", attrs.clone().style(Style::Italic)),
("i ", attrs.clone()),
],
&[
("Sans-Serif Normal ", attrs),
("Sans-Serif Bold ", attrs.weight(Weight::BOLD)),
("Sans-Serif Italic ", attrs.style(Style::Italic)),
("Sans-Serif Normal ", attrs.clone()),
("Sans-Serif Bold ", attrs.clone().weight(Weight::BOLD)),
("Sans-Serif Italic ", attrs.clone().style(Style::Italic)),
(
"Sans-Serif Bold Italic",
attrs.weight(Weight::BOLD).style(Style::Italic),
attrs.clone().weight(Weight::BOLD).style(Style::Italic),
),
],
&[
("Serif Normal ", serif_attrs),
("Serif Bold ", serif_attrs.weight(Weight::BOLD)),
("Serif Italic ", serif_attrs.style(Style::Italic)),
("Serif Normal ", serif_attrs.clone()),
("Serif Bold ", serif_attrs.clone().weight(Weight::BOLD)),
("Serif Italic ", serif_attrs.clone().style(Style::Italic)),
(
"Serif Bold Italic",
serif_attrs.weight(Weight::BOLD).style(Style::Italic),
),
],
&[
("Mono Normal ", mono_attrs),
("Mono Bold ", mono_attrs.weight(Weight::BOLD)),
("Mono Italic ", mono_attrs.style(Style::Italic)),
("Mono Normal ", mono_attrs.clone()),
("Mono Bold ", mono_attrs.clone().weight(Weight::BOLD)),
("Mono Italic ", mono_attrs.clone().style(Style::Italic)),
(
"Mono Bold Italic",
mono_attrs.weight(Weight::BOLD).style(Style::Italic),
),
],
&[
("Comic Normal ", comic_attrs),
("Comic Bold ", comic_attrs.weight(Weight::BOLD)),
("Comic Italic ", comic_attrs.style(Style::Italic)),
("Comic Normal ", comic_attrs.clone()),
("Comic Bold ", comic_attrs.clone().weight(Weight::BOLD)),
("Comic Italic ", comic_attrs.clone().style(Style::Italic)),
(
"Comic Bold Italic",
comic_attrs.weight(Weight::BOLD).style(Style::Italic),
),
],
&[
("R", attrs.color(Color::rgb(0xFF, 0x00, 0x00))),
("A", attrs.color(Color::rgb(0xFF, 0x7F, 0x00))),
("I", attrs.color(Color::rgb(0xFF, 0xFF, 0x00))),
("N", attrs.color(Color::rgb(0x00, 0xFF, 0x00))),
("B", attrs.color(Color::rgb(0x00, 0x00, 0xFF))),
("O", attrs.color(Color::rgb(0x4B, 0x00, 0x82))),
("W ", attrs.color(Color::rgb(0x94, 0x00, 0xD3))),
("Red ", attrs.color(Color::rgb(0xFF, 0x00, 0x00))),
("Orange ", attrs.color(Color::rgb(0xFF, 0x7F, 0x00))),
("Yellow ", attrs.color(Color::rgb(0xFF, 0xFF, 0x00))),
("Green ", attrs.color(Color::rgb(0x00, 0xFF, 0x00))),
("Blue ", attrs.color(Color::rgb(0x00, 0x00, 0xFF))),
("Indigo ", attrs.color(Color::rgb(0x4B, 0x00, 0x82))),
("Violet ", attrs.color(Color::rgb(0x94, 0x00, 0xD3))),
("U", attrs.color(Color::rgb(0x94, 0x00, 0xD3))),
("N", attrs.color(Color::rgb(0x4B, 0x00, 0x82))),
("I", attrs.color(Color::rgb(0x00, 0x00, 0xFF))),
("C", attrs.color(Color::rgb(0x00, 0xFF, 0x00))),
("O", attrs.color(Color::rgb(0xFF, 0xFF, 0x00))),
("R", attrs.color(Color::rgb(0xFF, 0x7F, 0x00))),
("N", attrs.color(Color::rgb(0xFF, 0x00, 0x00))),
("R", attrs.clone().color(Color::rgb(0xFF, 0x00, 0x00))),
("A", attrs.clone().color(Color::rgb(0xFF, 0x7F, 0x00))),
("I", attrs.clone().color(Color::rgb(0xFF, 0xFF, 0x00))),
("N", attrs.clone().color(Color::rgb(0x00, 0xFF, 0x00))),
("B", attrs.clone().color(Color::rgb(0x00, 0x00, 0xFF))),
("O", attrs.clone().color(Color::rgb(0x4B, 0x00, 0x82))),
("W ", attrs.clone().color(Color::rgb(0x94, 0x00, 0xD3))),
("Red ", attrs.clone().color(Color::rgb(0xFF, 0x00, 0x00))),
("Orange ", attrs.clone().color(Color::rgb(0xFF, 0x7F, 0x00))),
("Yellow ", attrs.clone().color(Color::rgb(0xFF, 0xFF, 0x00))),
("Green ", attrs.clone().color(Color::rgb(0x00, 0xFF, 0x00))),
("Blue ", attrs.clone().color(Color::rgb(0x00, 0x00, 0xFF))),
("Indigo ", attrs.clone().color(Color::rgb(0x4B, 0x00, 0x82))),
("Violet ", attrs.clone().color(Color::rgb(0x94, 0x00, 0xD3))),
("U", attrs.clone().color(Color::rgb(0x94, 0x00, 0xD3))),
("N", attrs.clone().color(Color::rgb(0x4B, 0x00, 0x82))),
("I", attrs.clone().color(Color::rgb(0x00, 0x00, 0xFF))),
("C", attrs.clone().color(Color::rgb(0x00, 0xFF, 0x00))),
("O", attrs.clone().color(Color::rgb(0xFF, 0xFF, 0x00))),
("R", attrs.clone().color(Color::rgb(0xFF, 0x7F, 0x00))),
("N", attrs.clone().color(Color::rgb(0xFF, 0x00, 0x00))),
],
&[(
"生活,삶,जिंदगी 😀 FPS",
attrs.color(Color::rgb(0xFF, 0x00, 0x00)),
attrs.clone().color(Color::rgb(0xFF, 0x00, 0x00)),
)],
];
for &line in lines {
for line in lines {
let mut line_text = String::new();
let mut attrs_list = AttrsList::new(attrs);
for &(text, attrs) in line {
let mut attrs_list = AttrsList::new(attrs.clone().build());
for (text, attrs) in line.iter() {
let start = line_text.len();
line_text.push_str(text);
let end = line_text.len();
attrs_list.add_span(start..end, attrs);
attrs_list.add_span(start..end, attrs.clone().build());
}
editor
.buffer_mut()
Expand Down
Loading
Loading