Add ability to set global buffer attributes
This commit is contained in:
parent
59a4f8a4c1
commit
525c4efaa5
4 changed files with 65 additions and 5 deletions
|
|
@ -23,6 +23,3 @@ version = "0.10"
|
||||||
#TODO: iced portal
|
#TODO: iced portal
|
||||||
#default-features = false
|
#default-features = false
|
||||||
#features = ["xdg-portal"]
|
#features = ["xdg-portal"]
|
||||||
|
|
||||||
[features]
|
|
||||||
mono = []
|
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,7 @@ use cosmic::{
|
||||||
settings,
|
settings,
|
||||||
widget::{
|
widget::{
|
||||||
button,
|
button,
|
||||||
|
toggler,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
use cosmic_text::{
|
use cosmic_text::{
|
||||||
|
|
@ -64,6 +65,9 @@ pub struct Window {
|
||||||
path_opt: Option<PathBuf>,
|
path_opt: Option<PathBuf>,
|
||||||
buffer: Mutex<TextBuffer<'static>>,
|
buffer: Mutex<TextBuffer<'static>>,
|
||||||
cache: Mutex<SwashCache>,
|
cache: Mutex<SwashCache>,
|
||||||
|
bold: bool,
|
||||||
|
italic: bool,
|
||||||
|
monospaced: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
|
|
@ -71,6 +75,9 @@ pub struct Window {
|
||||||
pub enum Message {
|
pub enum Message {
|
||||||
Open,
|
Open,
|
||||||
Save,
|
Save,
|
||||||
|
Bold(bool),
|
||||||
|
Italic(bool),
|
||||||
|
Monospaced(bool),
|
||||||
MetricsChanged(TextMetrics),
|
MetricsChanged(TextMetrics),
|
||||||
ThemeChanged(&'static str),
|
ThemeChanged(&'static str),
|
||||||
}
|
}
|
||||||
|
|
@ -100,11 +107,10 @@ impl Application for Window {
|
||||||
type Theme = Theme;
|
type Theme = Theme;
|
||||||
|
|
||||||
fn new(_flags: ()) -> (Self, Command<Self::Message>) {
|
fn new(_flags: ()) -> (Self, Command<Self::Message>) {
|
||||||
let attrs = cosmic_text::Attrs::new().monospaced(cfg!(feature = "mono"));
|
|
||||||
let font_size_i = 1; // Body
|
let font_size_i = 1; // Body
|
||||||
let buffer = TextBuffer::new(
|
let buffer = TextBuffer::new(
|
||||||
&FONT_SYSTEM,
|
&FONT_SYSTEM,
|
||||||
attrs,
|
cosmic_text::Attrs::new(),
|
||||||
FONT_SIZES[font_size_i],
|
FONT_SIZES[font_size_i],
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
@ -113,6 +119,9 @@ impl Application for Window {
|
||||||
path_opt: None,
|
path_opt: None,
|
||||||
buffer: Mutex::new(buffer),
|
buffer: Mutex::new(buffer),
|
||||||
cache: Mutex::new(SwashCache::new()),
|
cache: Mutex::new(SwashCache::new()),
|
||||||
|
bold: false,
|
||||||
|
italic: false,
|
||||||
|
monospaced: false,
|
||||||
};
|
};
|
||||||
if let Some(arg) = env::args().nth(1) {
|
if let Some(arg) = env::args().nth(1) {
|
||||||
window.open(PathBuf::from(arg));
|
window.open(PathBuf::from(arg));
|
||||||
|
|
@ -157,6 +166,35 @@ impl Application for Window {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
Message::Bold(bold) => {
|
||||||
|
self.bold = bold;
|
||||||
|
|
||||||
|
let mut buffer = self.buffer.lock().unwrap();
|
||||||
|
let attrs = buffer.attrs().clone().weight(if bold {
|
||||||
|
cosmic_text::Weight::BOLD
|
||||||
|
} else {
|
||||||
|
cosmic_text::Weight::NORMAL
|
||||||
|
});
|
||||||
|
buffer.set_attrs(&FONT_SYSTEM, attrs);
|
||||||
|
},
|
||||||
|
Message::Italic(italic) => {
|
||||||
|
self.italic = italic;
|
||||||
|
|
||||||
|
let mut buffer = self.buffer.lock().unwrap();
|
||||||
|
let attrs = buffer.attrs().clone().style(if italic {
|
||||||
|
cosmic_text::Style::Italic
|
||||||
|
} else {
|
||||||
|
cosmic_text::Style::Normal
|
||||||
|
});
|
||||||
|
buffer.set_attrs(&FONT_SYSTEM, attrs);
|
||||||
|
},
|
||||||
|
Message::Monospaced(monospaced) => {
|
||||||
|
self.monospaced = monospaced;
|
||||||
|
|
||||||
|
let mut buffer = self.buffer.lock().unwrap();
|
||||||
|
let attrs = buffer.attrs().clone().monospaced(monospaced);
|
||||||
|
buffer.set_attrs(&FONT_SYSTEM, attrs);
|
||||||
|
},
|
||||||
Message::MetricsChanged(metrics) => {
|
Message::MetricsChanged(metrics) => {
|
||||||
let mut buffer = self.buffer.lock().unwrap();
|
let mut buffer = self.buffer.lock().unwrap();
|
||||||
buffer.set_metrics(metrics);
|
buffer.set_metrics(metrics);
|
||||||
|
|
@ -196,6 +234,12 @@ impl Application for Window {
|
||||||
button!("Open").on_press(Message::Open),
|
button!("Open").on_press(Message::Open),
|
||||||
button!("Save").on_press(Message::Save),
|
button!("Save").on_press(Message::Save),
|
||||||
horizontal_space(Length::Fill),
|
horizontal_space(Length::Fill),
|
||||||
|
text("Bold:"),
|
||||||
|
toggler(None, self.bold, Message::Bold),
|
||||||
|
text("Italic:"),
|
||||||
|
toggler(None, self.italic, Message::Italic),
|
||||||
|
text("Monospaced:"),
|
||||||
|
toggler(None, self.monospaced, Message::Monospaced),
|
||||||
text("Theme:"),
|
text("Theme:"),
|
||||||
theme_picker,
|
theme_picker,
|
||||||
text("Font Size:"),
|
text("Font Size:"),
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
pub use fontdb::{Family, Stretch, Style, Weight};
|
pub use fontdb::{Family, Stretch, Style, Weight};
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||||
pub struct Attrs<'a> {
|
pub struct Attrs<'a> {
|
||||||
pub family: Family<'a>,
|
pub family: Family<'a>,
|
||||||
pub monospaced: bool,
|
pub monospaced: bool,
|
||||||
|
|
|
||||||
|
|
@ -516,6 +516,24 @@ impl<'a> TextBuffer<'a> {
|
||||||
self.height / self.metrics.line_height
|
self.height / self.metrics.line_height
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn attrs(&self) -> &Attrs<'a> {
|
||||||
|
&self.attrs
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Set attributes
|
||||||
|
pub fn set_attrs(&mut self, font_system: &'a FontSystem<'a>, attrs: Attrs<'a>) {
|
||||||
|
if attrs != self.attrs {
|
||||||
|
self.font_matches = font_system.matches_attrs(&attrs);
|
||||||
|
self.attrs = attrs;
|
||||||
|
|
||||||
|
for line in self.lines.iter_mut() {
|
||||||
|
line.reset();
|
||||||
|
}
|
||||||
|
|
||||||
|
self.shape_until_scroll();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Set text of buffer
|
/// Set text of buffer
|
||||||
pub fn set_text(&mut self, text: &str) {
|
pub fn set_text(&mut self, text: &str) {
|
||||||
self.lines.clear();
|
self.lines.clear();
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue