Remove Arc wrapper for text buffer when using iced

This commit is contained in:
Jeremy Soller 2022-10-19 08:05:22 -06:00
parent 021782b92b
commit fa00813c0b
No known key found for this signature in database
GPG key ID: 87F211AF2BE4C2FE
2 changed files with 24 additions and 13 deletions

View file

@ -24,7 +24,7 @@ use cosmic_text::{
use std::{ use std::{
env, env,
fs, fs,
sync::{Arc, Mutex}, sync::Mutex,
}; };
use self::text_box::text_box; use self::text_box::text_box;
@ -88,7 +88,7 @@ fn main() -> cosmic::iced::Result {
pub struct Window { pub struct Window {
theme: Theme, theme: Theme,
buffer: Arc<Mutex<TextBuffer<'static>>>, buffer: Mutex<TextBuffer<'static>>,
} }
#[allow(dead_code)] #[allow(dead_code)]
@ -116,11 +116,11 @@ impl Application for Window {
}; };
let font_size_i = 1; // Body let font_size_i = 1; // Body
let buffer = Arc::new(Mutex::new(TextBuffer::new( let buffer = Mutex::new(TextBuffer::new(
unsafe { FONT_MATCHES.as_ref().unwrap() }, unsafe { FONT_MATCHES.as_ref().unwrap() },
&text, &text,
FONT_SIZES[font_size_i], FONT_SIZES[font_size_i],
))); ));
let window = Window { let window = Window {
theme: Theme::Dark, theme: Theme::Dark,
@ -183,7 +183,7 @@ impl Application for Window {
.align_items(Alignment::Center) .align_items(Alignment::Center)
.spacing(8) .spacing(8)
, ,
text_box(self.buffer.clone()) text_box(&self.buffer)
] ]
.spacing(8) .spacing(8)
.padding(16) .padding(16)

View file

@ -7,18 +7,17 @@ use cosmic::iced_native::{
}, },
keyboard::{Event as KeyEvent, KeyCode}, keyboard::{Event as KeyEvent, KeyCode},
layout::{self, Layout}, layout::{self, Layout},
mouse::{Event as MouseEvent, ScrollDelta}, mouse::{Button, Event as MouseEvent, ScrollDelta},
renderer, renderer,
widget::{self, Widget}, widget::{self, Widget},
}; };
use cosmic_text::{ use cosmic_text::{
TextLineIndex,
TextAction, TextAction,
TextBuffer, TextBuffer,
}; };
use std::{ use std::{
cmp, cmp,
sync::{Arc, Mutex}, sync::Mutex,
time::Instant, time::Instant,
}; };
@ -60,16 +59,16 @@ impl StyleSheet for Theme {
} }
pub struct TextBox<'a> { pub struct TextBox<'a> {
buffer: Arc<Mutex<TextBuffer<'a>>>, buffer: &'a Mutex<TextBuffer<'static>>,
} }
impl<'a> TextBox<'a> { impl<'a> TextBox<'a> {
pub fn new(buffer: Arc<Mutex<TextBuffer<'a>>>) -> Self { pub fn new(buffer: &'a Mutex<TextBuffer<'static>>) -> Self {
Self { buffer } Self { buffer }
} }
} }
pub fn text_box<'a>(buffer: Arc<Mutex<TextBuffer<'a>>>) -> TextBox<'a> { pub fn text_box<'a>(buffer: &'a Mutex<TextBuffer<'static>>) -> TextBox<'a> {
TextBox::new(buffer) TextBox::new(buffer)
} }
@ -182,8 +181,8 @@ where
&mut self, &mut self,
_state: &mut widget::Tree, _state: &mut widget::Tree,
event: Event, event: Event,
_layout: Layout<'_>, layout: Layout<'_>,
_cursor_position: Point, cursor_position: Point,
_renderer: &Renderer, _renderer: &Renderer,
_clipboard: &mut dyn Clipboard, _clipboard: &mut dyn Clipboard,
_shell: &mut Shell<'_, Message>, _shell: &mut Shell<'_, Message>,
@ -236,6 +235,18 @@ where
_ => Status::Ignored, _ => Status::Ignored,
}, },
Event::Mouse(mouse_event) => match mouse_event { Event::Mouse(mouse_event) => match mouse_event {
MouseEvent::ButtonPressed(button) => match button {
Button::Left => if layout.bounds().contains(cursor_position) {
buffer.action(TextAction::Click {
x: (cursor_position.x - layout.bounds().x) as i32,
y: (cursor_position.y - layout.bounds().y) as i32,
});
Status::Captured
} else {
Status::Ignored
},
_ => Status::Ignored,
},
MouseEvent::WheelScrolled { delta } => match delta { MouseEvent::WheelScrolled { delta } => match delta {
ScrollDelta::Lines { x, y } => { ScrollDelta::Lines { x, y } => {
buffer.action(TextAction::Scroll((-y * 6.0) as i32)); buffer.action(TextAction::Scroll((-y * 6.0) as i32));