Draw most items with GPU, except for line numbers
This commit is contained in:
parent
a16c02d63f
commit
966cc0f332
2 changed files with 136 additions and 111 deletions
20
Cargo.lock
generated
20
Cargo.lock
generated
|
|
@ -1540,8 +1540,8 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "cosmic-text"
|
name = "cosmic-text"
|
||||||
version = "0.14.2"
|
version = "0.15.0"
|
||||||
source = "git+https://github.com/pop-os/cosmic-text.git#cffdea2b334e7830a5fd6f95bf5e1784014442a8"
|
source = "git+https://github.com/pop-os/cosmic-text.git#9339446cfa9b7f0110094a97764dccc09cfa98a2"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bitflags 2.10.0",
|
"bitflags 2.10.0",
|
||||||
"cosmic_undo_2",
|
"cosmic_undo_2",
|
||||||
|
|
@ -1883,7 +1883,7 @@ dependencies = [
|
||||||
"libc",
|
"libc",
|
||||||
"option-ext",
|
"option-ext",
|
||||||
"redox_users 0.5.2",
|
"redox_users 0.5.2",
|
||||||
"windows-sys 0.61.2",
|
"windows-sys 0.59.0",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|
@ -2113,7 +2113,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb"
|
checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"libc",
|
"libc",
|
||||||
"windows-sys 0.61.2",
|
"windows-sys 0.52.0",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|
@ -2679,7 +2679,7 @@ dependencies = [
|
||||||
"gobject-sys",
|
"gobject-sys",
|
||||||
"libc",
|
"libc",
|
||||||
"system-deps 7.0.6",
|
"system-deps 7.0.6",
|
||||||
"windows-sys 0.61.2",
|
"windows-sys 0.52.0",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|
@ -4314,7 +4314,7 @@ version = "1.4.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "d463f34ca3c400fde3a054da0e0b8c6ffa21e4590922f3e18281bb5eeef4cbdc"
|
checksum = "d463f34ca3c400fde3a054da0e0b8c6ffa21e4590922f3e18281bb5eeef4cbdc"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"windows-sys 0.61.2",
|
"windows-sys 0.59.0",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|
@ -5383,7 +5383,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "7d8fae84b431384b68627d0f9b3b1245fcf9f46f6c0e3dc902e9dce64edd1967"
|
checksum = "7d8fae84b431384b68627d0f9b3b1245fcf9f46f6c0e3dc902e9dce64edd1967"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"libc",
|
"libc",
|
||||||
"windows-sys 0.61.2",
|
"windows-sys 0.45.0",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|
@ -6391,7 +6391,7 @@ dependencies = [
|
||||||
"errno",
|
"errno",
|
||||||
"libc",
|
"libc",
|
||||||
"linux-raw-sys 0.11.0",
|
"linux-raw-sys 0.11.0",
|
||||||
"windows-sys 0.61.2",
|
"windows-sys 0.52.0",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|
@ -6976,7 +6976,7 @@ dependencies = [
|
||||||
"getrandom 0.3.4",
|
"getrandom 0.3.4",
|
||||||
"once_cell",
|
"once_cell",
|
||||||
"rustix 1.1.2",
|
"rustix 1.1.2",
|
||||||
"windows-sys 0.61.2",
|
"windows-sys 0.52.0",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|
@ -8021,7 +8021,7 @@ version = "0.1.11"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22"
|
checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"windows-sys 0.61.2",
|
"windows-sys 0.48.0",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|
|
||||||
227
src/text_box.rs
227
src/text_box.rs
|
|
@ -5,18 +5,19 @@ use cosmic::{
|
||||||
cosmic_theme::palette::{WithAlpha, blend::Compose},
|
cosmic_theme::palette::{WithAlpha, blend::Compose},
|
||||||
iced::{
|
iced::{
|
||||||
Color, Element, Length, Padding, Point, Rectangle, Size, Vector,
|
Color, Element, Length, Padding, Point, Rectangle, Size, Vector,
|
||||||
advanced::graphics::text::font_system,
|
advanced::graphics::text::{Raw, font_system},
|
||||||
event::{Event, Status},
|
event::{Event, Status},
|
||||||
keyboard::{Event as KeyEvent, Modifiers},
|
keyboard::{Event as KeyEvent, Modifiers},
|
||||||
mouse::{self, Button, Event as MouseEvent, ScrollDelta},
|
mouse::{self, Button, Event as MouseEvent, ScrollDelta},
|
||||||
},
|
},
|
||||||
iced_core::{
|
iced_core::{
|
||||||
Border, Radians, Shell,
|
Border, Radians, Shell, Transformation,
|
||||||
clipboard::Clipboard,
|
clipboard::Clipboard,
|
||||||
image,
|
image,
|
||||||
keyboard::{Key, key::Named},
|
keyboard::{Key, key::Named},
|
||||||
layout::{self, Layout},
|
layout::{self, Layout},
|
||||||
renderer::{self, Quad, Renderer as _},
|
renderer::{self, Quad, Renderer as _},
|
||||||
|
text::Renderer as _,
|
||||||
widget::{
|
widget::{
|
||||||
self, Id, Widget,
|
self, Id, Widget,
|
||||||
operation::{self, Operation},
|
operation::{self, Operation},
|
||||||
|
|
@ -26,12 +27,13 @@ use cosmic::{
|
||||||
theme::Theme,
|
theme::Theme,
|
||||||
};
|
};
|
||||||
use cosmic_text::{
|
use cosmic_text::{
|
||||||
Action, BorrowedWithFontSystem, Edit, Metrics, Motion, Scroll, Selection, ViEditor,
|
Action, BorrowedWithFontSystem, Edit, Metrics, Motion, Renderer as _, Scroll, Selection,
|
||||||
|
ViEditor,
|
||||||
};
|
};
|
||||||
use std::{
|
use std::{
|
||||||
cell::Cell,
|
cell::Cell,
|
||||||
cmp,
|
cmp,
|
||||||
sync::Mutex,
|
sync::{Arc, Mutex},
|
||||||
time::{Duration, Instant},
|
time::{Duration, Instant},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -137,11 +139,13 @@ where
|
||||||
TextBox::new(editor, metrics)
|
TextBox::new(editor, metrics)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Copy)]
|
||||||
struct Canvas {
|
struct Canvas {
|
||||||
w: i32,
|
w: i32,
|
||||||
h: i32,
|
h: i32,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Copy)]
|
||||||
struct Offset {
|
struct Offset {
|
||||||
x: i32,
|
x: i32,
|
||||||
y: i32,
|
y: i32,
|
||||||
|
|
@ -228,6 +232,30 @@ fn draw_rect(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct CustomRenderer<'a> {
|
||||||
|
renderer: &'a mut Renderer,
|
||||||
|
pos: Point,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> cosmic_text::Renderer for CustomRenderer<'a> {
|
||||||
|
fn rectangle(&mut self, x: i32, y: i32, w: u32, h: u32, color: cosmic_text::Color) {
|
||||||
|
self.renderer.fill_quad(
|
||||||
|
Quad {
|
||||||
|
bounds: Rectangle::new(
|
||||||
|
self.pos + Vector::new(x as f32, y as f32),
|
||||||
|
Size::new(w as f32, h as f32),
|
||||||
|
),
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
Color::from_rgba8(color.r(), color.g(), color.b(), (color.a() as f32) / 255.0),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn glyph(&mut self, _physical_glyph: cosmic_text::PhysicalGlyph, _color: cosmic_text::Color) {
|
||||||
|
// Glyphs will be drawn by iced fill_raw for performance
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<'a, Message> Widget<Message, cosmic::Theme, Renderer> for TextBox<'a, Message>
|
impl<'a, Message> Widget<Message, cosmic::Theme, Renderer> for TextBox<'a, Message>
|
||||||
where
|
where
|
||||||
Message: Clone,
|
Message: Clone,
|
||||||
|
|
@ -341,6 +369,7 @@ where
|
||||||
let cosmic_theme = theme.cosmic();
|
let cosmic_theme = theme.cosmic();
|
||||||
let scrollbar_w = cosmic_theme.spacing.space_xxs as i32;
|
let scrollbar_w = cosmic_theme.spacing.space_xxs as i32;
|
||||||
|
|
||||||
|
let view_position = layout.position() + [self.padding.left, self.padding.top].into();
|
||||||
let view_w = cmp::min(viewport.width as i32, layout.bounds().width as i32)
|
let view_w = cmp::min(viewport.width as i32, layout.bounds().width as i32)
|
||||||
- self.padding.horizontal() as i32
|
- self.padding.horizontal() as i32
|
||||||
- scrollbar_w;
|
- scrollbar_w;
|
||||||
|
|
@ -370,8 +399,8 @@ where
|
||||||
(image, scaled)
|
(image, scaled)
|
||||||
};
|
};
|
||||||
|
|
||||||
let (image_w, scaled_w) = calculate_ideal(view_w);
|
let (image_w, _scaled_w) = calculate_ideal(view_w);
|
||||||
let (image_h, scaled_h) = calculate_ideal(view_h);
|
let (image_h, _scaled_h) = calculate_ideal(view_h);
|
||||||
|
|
||||||
if image_w <= 0 || image_h <= 0 {
|
if image_w <= 0 || image_h <= 0 {
|
||||||
// Zero sized image
|
// Zero sized image
|
||||||
|
|
@ -437,9 +466,13 @@ where
|
||||||
editor.shape_as_needed(font_system.raw(), true);
|
editor.shape_as_needed(font_system.raw(), true);
|
||||||
|
|
||||||
let mut handle_opt = state.handle_opt.lock().unwrap();
|
let mut handle_opt = state.handle_opt.lock().unwrap();
|
||||||
|
let image_canvas = Canvas {
|
||||||
|
w: editor_offset_x,
|
||||||
|
h: image_h,
|
||||||
|
};
|
||||||
if editor.redraw() || handle_opt.is_none() {
|
if editor.redraw() || handle_opt.is_none() {
|
||||||
// Draw to pixel buffer
|
// Draw to pixel buffer
|
||||||
let mut pixels_u8 = vec![0; image_w as usize * image_h as usize * 4];
|
let mut pixels_u8 = vec![0; image_canvas.w as usize * image_canvas.h as usize * 4];
|
||||||
{
|
{
|
||||||
let mut swash_cache = SWASH_CACHE.get().unwrap().lock().unwrap();
|
let mut swash_cache = SWASH_CACHE.get().unwrap().lock().unwrap();
|
||||||
|
|
||||||
|
|
@ -450,6 +483,7 @@ where
|
||||||
)
|
)
|
||||||
};
|
};
|
||||||
|
|
||||||
|
//TODO: draw line numbers using iced functions for performance
|
||||||
if self.line_numbers {
|
if self.line_numbers {
|
||||||
let (gutter, gutter_foreground) = {
|
let (gutter, gutter_foreground) = {
|
||||||
let convert_color = |color: syntect::highlighting::Color| {
|
let convert_color = |color: syntect::highlighting::Color| {
|
||||||
|
|
@ -470,10 +504,7 @@ where
|
||||||
// Ensure fill with gutter color
|
// Ensure fill with gutter color
|
||||||
draw_rect(
|
draw_rect(
|
||||||
pixels,
|
pixels,
|
||||||
Canvas {
|
image_canvas,
|
||||||
w: image_w,
|
|
||||||
h: image_h,
|
|
||||||
},
|
|
||||||
Canvas {
|
Canvas {
|
||||||
w: editor_offset_x,
|
w: editor_offset_x,
|
||||||
h: image_h,
|
h: image_h,
|
||||||
|
|
@ -527,10 +558,7 @@ where
|
||||||
|x, y, color| {
|
|x, y, color| {
|
||||||
draw_rect(
|
draw_rect(
|
||||||
pixels,
|
pixels,
|
||||||
Canvas {
|
image_canvas,
|
||||||
w: image_w,
|
|
||||||
h: image_h,
|
|
||||||
},
|
|
||||||
Canvas { w: 1, h: 1 },
|
Canvas { w: 1, h: 1 },
|
||||||
Offset {
|
Offset {
|
||||||
x: physical_glyph.x + x,
|
x: physical_glyph.x + x,
|
||||||
|
|
@ -546,71 +574,6 @@ where
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if self.highlight_current_line {
|
|
||||||
let line_highlight = {
|
|
||||||
let convert_color = |color: syntect::highlighting::Color| {
|
|
||||||
cosmic_text::Color::rgba(color.r, color.g, color.b, color.a)
|
|
||||||
};
|
|
||||||
let syntax_theme = editor.theme();
|
|
||||||
//TODO: ideal fallback for line highlight color
|
|
||||||
syntax_theme
|
|
||||||
.settings
|
|
||||||
.line_highlight
|
|
||||||
.map_or(editor.background_color(), convert_color)
|
|
||||||
};
|
|
||||||
|
|
||||||
let cursor = editor.cursor();
|
|
||||||
editor.with_buffer(|buffer| {
|
|
||||||
for run in buffer.layout_runs() {
|
|
||||||
if run.line_i != cursor.line {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
draw_rect(
|
|
||||||
pixels,
|
|
||||||
Canvas {
|
|
||||||
w: image_w,
|
|
||||||
h: image_h,
|
|
||||||
},
|
|
||||||
Canvas {
|
|
||||||
w: image_w - editor_offset_x,
|
|
||||||
h: metrics.line_height as i32,
|
|
||||||
},
|
|
||||||
Offset {
|
|
||||||
x: editor_offset_x,
|
|
||||||
y: run.line_top as i32,
|
|
||||||
},
|
|
||||||
line_highlight,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// Draw editor
|
|
||||||
let scroll_x = editor.with_buffer(|buffer| buffer.scroll().horizontal as i32);
|
|
||||||
editor.draw(font_system.raw(), &mut swash_cache, |x, y, w, h, color| {
|
|
||||||
if x < scroll_x {
|
|
||||||
//TODO: modify width?
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
draw_rect(
|
|
||||||
pixels,
|
|
||||||
Canvas {
|
|
||||||
w: image_w,
|
|
||||||
h: image_h,
|
|
||||||
},
|
|
||||||
Canvas {
|
|
||||||
w: w as i32,
|
|
||||||
h: h as i32,
|
|
||||||
},
|
|
||||||
Offset {
|
|
||||||
x: editor_offset_x + x - scroll_x,
|
|
||||||
y,
|
|
||||||
},
|
|
||||||
color,
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
// Calculate scrollbar
|
// Calculate scrollbar
|
||||||
editor.with_buffer(|buffer| {
|
editor.with_buffer(|buffer| {
|
||||||
let mut start_line_opt = None;
|
let mut start_line_opt = None;
|
||||||
|
|
@ -668,32 +631,94 @@ where
|
||||||
|
|
||||||
state.scale_factor.set(scale_factor);
|
state.scale_factor.set(scale_factor);
|
||||||
*handle_opt = Some(image::Handle::from_rgba(
|
*handle_opt = Some(image::Handle::from_rgba(
|
||||||
image_w as u32,
|
image_canvas.w as u32,
|
||||||
image_h as u32,
|
image_canvas.h as u32,
|
||||||
pixels_u8,
|
pixels_u8,
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Draw cached image
|
||||||
let image_position = layout.position() + [self.padding.left, self.padding.top].into();
|
let image_position = layout.position() + [self.padding.left, self.padding.top].into();
|
||||||
if let Some(ref handle) = *handle_opt {
|
|
||||||
let image_size = image::Renderer::measure_image(renderer, handle);
|
// Draw editor UI
|
||||||
let scaled_size = Size::new(scaled_w as f32, scaled_h as f32);
|
renderer.with_translation(Vector::new(view_position.x, view_position.y), |renderer| {
|
||||||
log::debug!(
|
renderer.with_transformation(Transformation::scale(1.0 / scale_factor), |renderer| {
|
||||||
"text_box image {:?} scaled {:?} position {:?}",
|
// Draw cached image (only has line numbers)
|
||||||
image_size,
|
if let Some(ref handle) = *handle_opt {
|
||||||
scaled_size,
|
let image_size = image::Renderer::measure_image(renderer, handle);
|
||||||
image_position
|
image::Renderer::draw_image(
|
||||||
);
|
renderer,
|
||||||
image::Renderer::draw_image(
|
handle.clone(),
|
||||||
renderer,
|
image::FilterMethod::Nearest,
|
||||||
handle.clone(),
|
Rectangle::new(
|
||||||
image::FilterMethod::Nearest,
|
Point::new(0.0, 0.0),
|
||||||
Rectangle::new(image_position, scaled_size),
|
Size::new(image_size.width as f32, image_size.height as f32),
|
||||||
Radians(0.0),
|
),
|
||||||
1.0,
|
Radians(0.0),
|
||||||
[0.0; 4],
|
1.0,
|
||||||
);
|
[0.0; 4],
|
||||||
}
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Calculate editor position
|
||||||
|
let scroll_x = editor.with_buffer(|buffer| buffer.scroll().horizontal);
|
||||||
|
let pos = Point::new(editor_offset_x as f32, 0.0);
|
||||||
|
let size = Size::new((image_w - editor_offset_x) as f32, image_h as f32);
|
||||||
|
|
||||||
|
// Create custom renderer for rectangles
|
||||||
|
let mut custom_renderer = CustomRenderer { renderer, pos };
|
||||||
|
|
||||||
|
// Draw line highlight
|
||||||
|
if self.highlight_current_line {
|
||||||
|
let line_highlight = {
|
||||||
|
let convert_color = |color: syntect::highlighting::Color| {
|
||||||
|
cosmic_text::Color::rgba(color.r, color.g, color.b, color.a)
|
||||||
|
};
|
||||||
|
let syntax_theme = editor.theme();
|
||||||
|
//TODO: ideal fallback for line highlight color
|
||||||
|
syntax_theme
|
||||||
|
.settings
|
||||||
|
.line_highlight
|
||||||
|
.map_or(editor.background_color(), convert_color)
|
||||||
|
};
|
||||||
|
|
||||||
|
let cursor = editor.cursor();
|
||||||
|
editor.with_buffer(|buffer| {
|
||||||
|
for run in buffer.layout_runs() {
|
||||||
|
if run.line_i != cursor.line {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
custom_renderer.rectangle(
|
||||||
|
0,
|
||||||
|
run.line_top as i32,
|
||||||
|
(image_w - editor_offset_x) as u32,
|
||||||
|
metrics.line_height as u32,
|
||||||
|
line_highlight,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Draw editor selection, cursor, etc.
|
||||||
|
editor.render(&mut custom_renderer);
|
||||||
|
|
||||||
|
// Draw editor text
|
||||||
|
match editor.buffer_ref() {
|
||||||
|
cosmic_text::BufferRef::Arc(buffer) => {
|
||||||
|
renderer.fill_raw(Raw {
|
||||||
|
buffer: Arc::downgrade(&buffer),
|
||||||
|
position: pos - Vector::new(scroll_x, 0.0),
|
||||||
|
color: Color::new(1.0, 1.0, 1.0, 1.0),
|
||||||
|
clip_bounds: Rectangle::new(pos, size),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
log::error!("cosmic-text buffer not an Arc");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
});
|
||||||
|
|
||||||
// Draw vertical scrollbar
|
// Draw vertical scrollbar
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue