Appearance for iced text box

This commit is contained in:
Jeremy Soller 2022-10-18 14:35:16 -06:00
parent 3f01aa1496
commit e62f8b9292
No known key found for this signature in database
GPG key ID: 87F211AF2BE4C2FE
4 changed files with 158 additions and 5882 deletions

View file

@ -1,13 +1,19 @@
use cosmic::{
iced::widget::{
container,
},
iced::{
self,
Alignment,
Application,
Command,
Element,
Theme
Theme,
widget::{
container,
column,
pick_list,
radio,
row,
text,
},
},
settings,
};
@ -18,6 +24,7 @@ use cosmic_text::{
};
use std::{
env,
fmt,
fs,
sync::{Arc, Mutex},
};
@ -32,6 +39,37 @@ lazy_static::lazy_static! {
//TODO: find out how to do this!
static mut FONT_MATCHES: Option<FontMatches<'static>> = None;
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct FontMetrics {
pub font_size: i32,
pub line_height: i32,
}
impl fmt::Display for FontMetrics {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
//TODO: should line height also be shown?
write!(f, "{}", self.font_size)
}
}
impl FontMetrics {
pub const fn new(font_size: i32, line_height: i32) -> Self {
Self {
font_size,
line_height,
}
}
}
static FONT_SIZES: &'static [FontMetrics] = &[
FontMetrics::new(10, 14), // Caption
FontMetrics::new(14, 20), // Body
FontMetrics::new(20, 28), // Title 4
FontMetrics::new(24, 32), // Title 3
FontMetrics::new(28, 36), // Title 2
FontMetrics::new(32, 44), // Title 1
];
fn main() -> cosmic::iced::Result {
env_logger::init();
@ -73,12 +111,16 @@ fn main() -> cosmic::iced::Result {
}
pub struct Window {
theme: Theme,
buffer: Arc<Mutex<TextBuffer<'static>>>,
}
#[allow(dead_code)]
#[derive(Clone, Copy, Debug)]
pub enum Message {}
pub enum Message {
FontSize(FontMetrics),
ThemeChanged(&'static str),
}
impl Application for Window {
type Executor = iced::executor::Default;
@ -87,17 +129,6 @@ impl Application for Window {
type Theme = Theme;
fn new(_flags: ()) -> (Self, Command<Self::Message>) {
let font_sizes = [
(10, 14), // Caption
(14, 20), // Body
(20, 28), // Title 4
(24, 32), // Title 3
(28, 36), // Title 2
(32, 44), // Title 1
];
let font_size_default = 1; // Body
let mut font_size_i = font_size_default;
let text = if let Some(arg) = env::args().nth(1) {
fs::read_to_string(&arg).expect("failed to open file")
} else {
@ -108,33 +139,82 @@ impl Application for Window {
default_text.to_string()
};
let font_size_i = 1; // Body
let buffer = Arc::new(Mutex::new(TextBuffer::new(
unsafe { FONT_MATCHES.as_ref().unwrap() },
&text,
font_sizes[font_size_i].0,
font_sizes[font_size_i].1,
FONT_SIZES[font_size_i].font_size,
FONT_SIZES[font_size_i].line_height,
0,
0
)));
let window = Window {
theme: Theme::Dark,
buffer,
};
(window, Command::none())
}
fn theme(&self) -> Theme {
self.theme
}
fn title(&self) -> String {
let buffer = self.buffer.lock().unwrap();
format!("COSMIC Text - iced - {}", buffer.font_matches().locale)
}
fn update(&mut self, message: Message) -> iced::Command<Self::Message> {
match message {
Message::FontSize(font_metrics) => {
let mut buffer = self.buffer.lock().unwrap();
buffer.set_font_metrics(font_metrics.font_size, font_metrics.line_height);
},
Message::ThemeChanged(theme) => match theme {
"Dark" => self.theme = Theme::Dark,
"Light" => self.theme = Theme::Light,
_ => (),
},
}
Command::none()
}
fn view(&self) -> Element<Message> {
container(
static THEMES: &'static [&'static str] = &["Dark", "Light"];
let theme_picker = pick_list(
THEMES,
Some(match self.theme {
Theme::Dark => THEMES[0],
Theme::Light => THEMES[1],
}),
Message::ThemeChanged
);
let font_size_picker = {
let buffer = self.buffer.lock().unwrap();
pick_list(
FONT_SIZES,
Some(FontMetrics::new(buffer.font_size(), buffer.line_height())),
Message::FontSize
)
};
column![
row![
text("Theme:"),
theme_picker,
text("Font Size:"),
font_size_picker,
]
.align_items(Alignment::Center)
.spacing(8)
,
text_box(self.buffer.clone())
).padding(16).into()
]
.spacing(8)
.padding(16)
.into()
}
}