Use f32 instead of i32 for lengths
This allows users to use logical coordinates instead of physical ones.
This commit is contained in:
parent
f08bea22ed
commit
4320ae6329
15 changed files with 203 additions and 155 deletions
|
|
@ -14,7 +14,7 @@ use cosmic::{
|
|||
use cosmic_text::{
|
||||
Align, Attrs, AttrsList, Buffer, Edit, FontSystem, Metrics, SyntaxEditor, SyntaxSystem, Wrap,
|
||||
};
|
||||
use std::{env, fs, path::PathBuf, sync::Mutex};
|
||||
use std::{env, fmt, fs, path::PathBuf, sync::Mutex};
|
||||
|
||||
use self::text::text;
|
||||
mod text;
|
||||
|
|
@ -27,14 +27,52 @@ lazy_static::lazy_static! {
|
|||
static ref SYNTAX_SYSTEM: SyntaxSystem = SyntaxSystem::new();
|
||||
}
|
||||
|
||||
static FONT_SIZES: &'static [Metrics] = &[
|
||||
Metrics::new(10, 14), // Caption
|
||||
Metrics::new(14, 20), // Body
|
||||
Metrics::new(20, 28), // Title 4
|
||||
Metrics::new(24, 32), // Title 3
|
||||
Metrics::new(28, 36), // Title 2
|
||||
Metrics::new(32, 44), // Title 1
|
||||
];
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum FontSize {
|
||||
Caption,
|
||||
Body,
|
||||
Title4,
|
||||
Title3,
|
||||
Title2,
|
||||
Title1,
|
||||
}
|
||||
|
||||
impl FontSize {
|
||||
pub fn all() -> &'static [Self] {
|
||||
&[
|
||||
Self::Caption,
|
||||
Self::Body,
|
||||
Self::Title4,
|
||||
Self::Title3,
|
||||
Self::Title2,
|
||||
Self::Title1,
|
||||
]
|
||||
}
|
||||
|
||||
pub fn to_metrics(self) -> Metrics {
|
||||
match self {
|
||||
Self::Caption => Metrics::new(10.0, 14.0), // Caption
|
||||
Self::Body => Metrics::new(14.0, 20.0), // Body
|
||||
Self::Title4 => Metrics::new(20.0, 28.0), // Title 4
|
||||
Self::Title3 => Metrics::new(24.0, 32.0), // Title 3
|
||||
Self::Title2 => Metrics::new(28.0, 36.0), // Title 2
|
||||
Self::Title1 => Metrics::new(32.0, 44.0), // Title 1
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for FontSize {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match self {
|
||||
Self::Caption => write!(f, "Caption"),
|
||||
Self::Body => write!(f, "Body"),
|
||||
Self::Title4 => write!(f, "Title 4"),
|
||||
Self::Title3 => write!(f, "Title 3"),
|
||||
Self::Title2 => write!(f, "Title 2"),
|
||||
Self::Title1 => write!(f, "Title 1"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static WRAP_MODE: &'static [Wrap] = &[Wrap::None, Wrap::Glyph, Wrap::Word];
|
||||
|
||||
|
|
@ -50,6 +88,7 @@ pub struct Window {
|
|||
theme: Theme,
|
||||
path_opt: Option<PathBuf>,
|
||||
attrs: Attrs<'static>,
|
||||
font_size: FontSize,
|
||||
#[cfg(not(feature = "vi"))]
|
||||
editor: Mutex<SyntaxEditor<'static>>,
|
||||
#[cfg(feature = "vi")]
|
||||
|
|
@ -64,7 +103,7 @@ pub enum Message {
|
|||
Bold(bool),
|
||||
Italic(bool),
|
||||
Monospaced(bool),
|
||||
MetricsChanged(Metrics),
|
||||
FontSizeChanged(FontSize),
|
||||
WrapChanged(Wrap),
|
||||
AlignmentChanged(Align),
|
||||
ThemeChanged(&'static str),
|
||||
|
|
@ -98,7 +137,7 @@ impl Application for Window {
|
|||
.family(cosmic_text::Family::Monospace);
|
||||
|
||||
let mut editor = SyntaxEditor::new(
|
||||
Buffer::new(&FONT_SYSTEM, FONT_SIZES[1 /* Body */]),
|
||||
Buffer::new(&FONT_SYSTEM, FontSize::Body.to_metrics()),
|
||||
&SYNTAX_SYSTEM,
|
||||
"base16-eighties.dark",
|
||||
)
|
||||
|
|
@ -111,6 +150,7 @@ impl Application for Window {
|
|||
|
||||
let mut window = Window {
|
||||
theme: Theme::Dark,
|
||||
font_size: FontSize::Body,
|
||||
path_opt: None,
|
||||
attrs,
|
||||
editor: Mutex::new(editor),
|
||||
|
|
@ -195,9 +235,11 @@ impl Application for Window {
|
|||
let mut editor = self.editor.lock().unwrap();
|
||||
update_attrs(&mut *editor, self.attrs);
|
||||
}
|
||||
Message::MetricsChanged(metrics) => {
|
||||
Message::FontSizeChanged(font_size) => {
|
||||
self.font_size = font_size;
|
||||
|
||||
let mut editor = self.editor.lock().unwrap();
|
||||
editor.buffer_mut().set_metrics(metrics);
|
||||
editor.buffer_mut().set_metrics(font_size.to_metrics());
|
||||
}
|
||||
Message::WrapChanged(wrap) => {
|
||||
let mut editor = self.editor.lock().unwrap();
|
||||
|
|
@ -245,9 +287,9 @@ impl Application for Window {
|
|||
let font_size_picker = {
|
||||
let editor = self.editor.lock().unwrap();
|
||||
pick_list(
|
||||
FONT_SIZES,
|
||||
Some(editor.buffer().metrics()),
|
||||
Message::MetricsChanged,
|
||||
FontSize::all(),
|
||||
Some(self.font_size),
|
||||
Message::FontSizeChanged,
|
||||
)
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -55,7 +55,7 @@ impl Text {
|
|||
|
||||
let text = Self {
|
||||
line,
|
||||
metrics: Metrics::new(14, 20),
|
||||
metrics: Metrics::new(14.0, 20.0),
|
||||
};
|
||||
|
||||
log::debug!("Text::new in {:?}", instant.elapsed());
|
||||
|
|
@ -99,17 +99,17 @@ where
|
|||
//TODO: can we cache this?
|
||||
let layout_lines = shape.layout(
|
||||
self.metrics.font_size,
|
||||
limits.max().width as i32,
|
||||
limits.max().width,
|
||||
self.line.wrap(),
|
||||
self.line.align(),
|
||||
);
|
||||
|
||||
let mut width = 0;
|
||||
let mut height = 0;
|
||||
let mut width = 0.0f32;
|
||||
let mut height = 0.0f32;
|
||||
|
||||
for layout_line in layout_lines {
|
||||
for glyph in layout_line.glyphs.iter() {
|
||||
width = cmp::max(width, (glyph.x + glyph.w) as i32 + 1);
|
||||
width = width.max((glyph.x + glyph.w) + 1.0);
|
||||
}
|
||||
height += self.metrics.line_height;
|
||||
}
|
||||
|
|
@ -156,8 +156,8 @@ where
|
|||
cmp::max(0, cmp::min(255, (appearance.text_color.a * 255.0) as i32)) as u8,
|
||||
);
|
||||
|
||||
let layout_w = layout.bounds().width as i32;
|
||||
let layout_h = layout.bounds().height as i32;
|
||||
let layout_w = layout.bounds().width;
|
||||
let layout_h = layout.bounds().height;
|
||||
|
||||
let shape = self.line.shape_opt().as_ref().unwrap();
|
||||
|
||||
|
|
@ -185,8 +185,8 @@ where
|
|||
|
||||
cache.with_pixels(cache_key, glyph_color, |pixel_x, pixel_y, color| {
|
||||
let x = x_int + pixel_x;
|
||||
let y = line_y + y_int + pixel_y;
|
||||
draw_pixel(&mut pixels, layout_w, layout_h, x, y, color);
|
||||
let y = line_y as i32 + y_int + pixel_y;
|
||||
draw_pixel(&mut pixels, layout_w as i32, layout_h as i32, x, y, color);
|
||||
});
|
||||
}
|
||||
line_y += self.metrics.line_height;
|
||||
|
|
|
|||
|
|
@ -160,10 +160,8 @@ where
|
|||
|
||||
let mut editor = self.editor.lock().unwrap();
|
||||
|
||||
let view_w = cmp::min(viewport.width as i32, layout.bounds().width as i32)
|
||||
- self.padding.horizontal() as i32;
|
||||
let view_h = cmp::min(viewport.height as i32, layout.bounds().height as i32)
|
||||
- self.padding.vertical() as i32;
|
||||
let view_w = viewport.width.min(layout.bounds().width) - self.padding.horizontal() as f32;
|
||||
let view_h = viewport.height.min(layout.bounds().height) - self.padding.vertical() as f32;
|
||||
editor.buffer_mut().set_size(view_w, view_h);
|
||||
|
||||
editor.shape_as_needed();
|
||||
|
|
@ -203,7 +201,7 @@ where
|
|||
),
|
||||
);
|
||||
} else {
|
||||
text::draw_pixel(&mut pixels, view_w, view_h, x, y, color);
|
||||
text::draw_pixel(&mut pixels, view_w as i32, view_h as i32, x, y, color);
|
||||
}
|
||||
},
|
||||
);
|
||||
|
|
|
|||
|
|
@ -22,11 +22,11 @@ fn main() {
|
|||
let display_scale = match orbclient::get_display_size() {
|
||||
Ok((w, h)) => {
|
||||
log::info!("Display size: {}, {}", w, h);
|
||||
(h as i32 / 1600) + 1
|
||||
(h as f32 / 1600.0) + 1.0
|
||||
}
|
||||
Err(err) => {
|
||||
log::warn!("Failed to get display size: {}", err);
|
||||
1
|
||||
1.0
|
||||
}
|
||||
};
|
||||
|
||||
|
|
@ -45,17 +45,17 @@ fn main() {
|
|||
let syntax_system = SyntaxSystem::new();
|
||||
|
||||
let font_sizes = [
|
||||
Metrics::new(10, 14).scale(display_scale), // Caption
|
||||
Metrics::new(14, 20).scale(display_scale), // Body
|
||||
Metrics::new(20, 28).scale(display_scale), // Title 4
|
||||
Metrics::new(24, 32).scale(display_scale), // Title 3
|
||||
Metrics::new(28, 36).scale(display_scale), // Title 2
|
||||
Metrics::new(32, 44).scale(display_scale), // Title 1
|
||||
Metrics::new(10.0, 14.0).scale(display_scale), // Caption
|
||||
Metrics::new(14.0, 20.0).scale(display_scale), // Body
|
||||
Metrics::new(20.0, 28.0).scale(display_scale), // Title 4
|
||||
Metrics::new(24.0, 32.0).scale(display_scale), // Title 3
|
||||
Metrics::new(28.0, 36.0).scale(display_scale), // Title 2
|
||||
Metrics::new(32.0, 44.0).scale(display_scale), // Title 1
|
||||
];
|
||||
let font_size_default = 1; // Body
|
||||
let mut font_size_i = font_size_default;
|
||||
|
||||
let line_x = 8 * display_scale;
|
||||
let line_x = 8.0 * display_scale;
|
||||
|
||||
let mut editor = SyntaxEditor::new(
|
||||
Buffer::new(&font_system, font_sizes[font_size_i]),
|
||||
|
|
@ -69,7 +69,7 @@ fn main() {
|
|||
|
||||
editor
|
||||
.buffer_mut()
|
||||
.set_size(window.width() as i32 - line_x * 2, window.height() as i32);
|
||||
.set_size(window.width() as f32 - line_x * 2.0, window.height() as f32);
|
||||
|
||||
let attrs = Attrs::new().monospaced(true).family(Family::Monospace);
|
||||
match editor.load_text(&path, attrs) {
|
||||
|
|
@ -95,7 +95,13 @@ fn main() {
|
|||
|
||||
let fg = editor.foreground_color();
|
||||
editor.draw(&mut swash_cache, fg, |x, y, w, h, color| {
|
||||
window.rect(line_x + x, y, w, h, orbclient::Color { data: color.0 })
|
||||
window.rect(
|
||||
line_x as i32 + x,
|
||||
y,
|
||||
w,
|
||||
h,
|
||||
orbclient::Color { data: color.0 },
|
||||
)
|
||||
});
|
||||
|
||||
// Draw scrollbar
|
||||
|
|
@ -177,7 +183,7 @@ fn main() {
|
|||
mouse_y = event.y;
|
||||
if mouse_left {
|
||||
editor.action(Action::Drag {
|
||||
x: mouse_x - line_x,
|
||||
x: mouse_x - line_x as i32,
|
||||
y: mouse_y,
|
||||
});
|
||||
|
||||
|
|
@ -197,7 +203,7 @@ fn main() {
|
|||
mouse_left = event.left;
|
||||
if mouse_left {
|
||||
editor.action(Action::Click {
|
||||
x: mouse_x - line_x,
|
||||
x: mouse_x - line_x as i32,
|
||||
y: mouse_y,
|
||||
});
|
||||
}
|
||||
|
|
@ -207,7 +213,7 @@ fn main() {
|
|||
EventOption::Resize(event) => {
|
||||
editor
|
||||
.buffer_mut()
|
||||
.set_size(event.width as i32 - line_x * 2, event.height as i32);
|
||||
.set_size(event.width as f32 - line_x * 2.0, event.height as f32);
|
||||
}
|
||||
EventOption::Scroll(event) => {
|
||||
editor.action(Action::Scroll {
|
||||
|
|
@ -221,7 +227,7 @@ fn main() {
|
|||
|
||||
if mouse_left && force_drag {
|
||||
editor.action(Action::Drag {
|
||||
x: mouse_x - line_x,
|
||||
x: mouse_x - line_x as i32,
|
||||
y: mouse_y,
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -31,7 +31,7 @@ fn redraw(window: &mut Window, editor: &mut Editor<'_>, swash_cache: &mut SwashC
|
|||
fn main() {
|
||||
env_logger::init();
|
||||
|
||||
let display_scale = 1;
|
||||
let display_scale = 1.0;
|
||||
let font_system = FontSystem::new();
|
||||
|
||||
let mut window = Window::new_flags(
|
||||
|
|
@ -45,17 +45,17 @@ fn main() {
|
|||
.unwrap();
|
||||
|
||||
let font_sizes = [
|
||||
Metrics::new(10, 14).scale(display_scale), // Caption
|
||||
Metrics::new(14, 20).scale(display_scale), // Body
|
||||
Metrics::new(20, 28).scale(display_scale), // Title 4
|
||||
Metrics::new(24, 32).scale(display_scale), // Title 3
|
||||
Metrics::new(28, 36).scale(display_scale), // Title 2
|
||||
Metrics::new(32, 44).scale(display_scale), // Title 1
|
||||
Metrics::new(10.0, 14.0).scale(display_scale), // Caption
|
||||
Metrics::new(14.0, 20.0).scale(display_scale), // Body
|
||||
Metrics::new(20.0, 28.0).scale(display_scale), // Title 4
|
||||
Metrics::new(24.0, 32.0).scale(display_scale), // Title 3
|
||||
Metrics::new(28.0, 36.0).scale(display_scale), // Title 2
|
||||
Metrics::new(32.0, 44.0).scale(display_scale), // Title 1
|
||||
];
|
||||
let font_size_default = 1; // Body
|
||||
|
||||
let mut buffer = Buffer::new(&font_system, font_sizes[font_size_default]);
|
||||
buffer.set_size(window.width() as i32, window.height() as i32);
|
||||
buffer.set_size(window.width() as f32, window.height() as f32);
|
||||
|
||||
let mut editor = Editor::new(buffer);
|
||||
|
||||
|
|
|
|||
|
|
@ -18,11 +18,11 @@ fn main() {
|
|||
let display_scale = match orbclient::get_display_size() {
|
||||
Ok((w, h)) => {
|
||||
log::info!("Display size: {}, {}", w, h);
|
||||
(h as i32 / 1600) + 1
|
||||
(h as f32 / 1600.0) + 1.0
|
||||
}
|
||||
Err(err) => {
|
||||
log::warn!("Failed to get display size: {}", err);
|
||||
1
|
||||
1.0
|
||||
}
|
||||
};
|
||||
|
||||
|
|
@ -38,12 +38,12 @@ fn main() {
|
|||
|
||||
let mut editor = Editor::new(Buffer::new(
|
||||
&font_system,
|
||||
Metrics::new(32, 44).scale(display_scale),
|
||||
Metrics::new(32.0, 44.0).scale(display_scale),
|
||||
));
|
||||
|
||||
editor
|
||||
.buffer_mut()
|
||||
.set_size(window.width() as i32, window.height() as i32);
|
||||
.set_size(window.width() as f32, window.height() as f32);
|
||||
|
||||
let attrs = Attrs::new();
|
||||
let serif_attrs = attrs.family(Family::Serif);
|
||||
|
|
@ -211,7 +211,7 @@ fn main() {
|
|||
EventOption::Resize(resize) => {
|
||||
editor
|
||||
.buffer_mut()
|
||||
.set_size(resize.width as i32, resize.height as i32);
|
||||
.set_size(resize.width as f32, resize.height as f32);
|
||||
}
|
||||
EventOption::Quit(_) => process::exit(0),
|
||||
_ => (),
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@ fn main() {
|
|||
let mut swash_cache = SwashCache::new(&font_system);
|
||||
|
||||
// Text metrics indicate the font size and line height of a buffer
|
||||
let metrics = Metrics::new(14, 20);
|
||||
let metrics = Metrics::new(14.0, 20.0);
|
||||
|
||||
// A Buffer provides shaping and layout for a UTF-8 string, create one per text widget
|
||||
let mut buffer = Buffer::new(&font_system, metrics);
|
||||
|
|
@ -20,7 +20,7 @@ fn main() {
|
|||
// Set a size for the text buffer, in pixels
|
||||
let width = 80u16;
|
||||
let height = 25u16;
|
||||
buffer.set_size(width as i32, height as i32);
|
||||
buffer.set_size(width as f32, height as f32);
|
||||
|
||||
// Attributes indicate what font to choose
|
||||
let attrs = Attrs::new();
|
||||
|
|
@ -39,7 +39,7 @@ fn main() {
|
|||
|
||||
// Clear buffer with black background
|
||||
for _y in 0..height {
|
||||
for _x in 0..buffer.size().0 {
|
||||
for _x in 0..(buffer.size().0 as i32) {
|
||||
print!(
|
||||
"{} {}",
|
||||
color::Bg(color::Rgb(0, 0, 0)),
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue