Newer
Older
#![allow(clippy::type_complexity)]
use std::{collections::VecDeque, path::PathBuf, time::Duration};
#[path = "utils.rs"]
pub mod utils;
pub use utils::*;
use bevy::{
asset::HandleId,
input::mouse::{MouseScrollUnit, MouseWheel},
prelude::*,
render::{render_resource::Extent3d, texture::DEFAULT_IMAGE_HANDLE},
ui::FocusPolicy,
window::{PrimaryWindow, WindowScaleFactorChanged},
};
use cosmic_text::{
Action, Attrs, AttrsList, AttrsOwned, Buffer, BufferLine, Cursor, Edit, Editor, FontSystem,
Metrics, Shaping, SwashCache,
};
use image::{imageops::FilterType, GenericImageView};
#[derive(Clone)]
pub enum CosmicText {
OneStyle(String),
MultiStyle(Vec<Vec<(String, cosmic_text::AttrsOwned)>>),
}
/// Enum representing the position of the cosmic text.
#[derive(Clone, Component, Default)]
pub enum CosmicTextPosition {
// we have the space for full words :)
#[default]
Center,
TopLeft,
}
#[derive(Clone, Component)]
pub struct CosmicMetrics {
pub font_size: f32,
pub line_height: f32,
pub scale_factor: f32,
}
impl Default for CosmicMetrics {
fn default() -> Self {
Self {
font_size: 12.,
line_height: 12.,
scale_factor: 1.,
}
}
pub struct CosmicFontSystem(pub FontSystem);
#[derive(Component)]
pub struct ReadOnly; // tag component
#[derive(Component)]
pub struct CosmicUiNode; // tag component
#[derive(Component)]
pub struct CosmicEditor(pub Editor);
impl Default for CosmicEditor {
fn default() -> Self {
Self(Editor::new(Buffer::new_empty(Metrics::new(12., 14.))))
}
}
/// Adds the font system to each editor when added
fn cosmic_editor_builder(
mut added_editors: Query<(&mut CosmicEditor, &CosmicAttrs), Added<CosmicEditor>>,
mut font_system: ResMut<CosmicFontSystem>,
) {
for (mut editor, attrs) in added_editors.iter_mut() {
// keep old text if set
let mut text = get_cosmic_text(editor.0.buffer());
if text.is_empty() {
text = "".into();
}
editor.0.buffer_mut().set_text(
&mut font_system.0,
text.as_str(),
attrs.0.as_attrs(),
Shaping::Advanced,
);
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
.set_size(&mut font_system.0, 100., 100.)
}
}
#[derive(Component)]
pub struct CosmicAttrs(pub AttrsOwned);
impl Default for CosmicAttrs {
fn default() -> Self {
CosmicAttrs(AttrsOwned::new(Attrs::new()))
}
}
#[derive(Component, Default)]
pub struct CosmicBackground(Option<Handle<Image>>);
#[derive(Bundle)]
pub struct CosmicEditUiBundle {
// Bevy UI bits
/// Describes the logical size of the node
pub node: Node,
/// Marker component that signals this node is a button
pub button: Button,
/// Styles which control the layout (size and position) of the node and it's children
/// In some cases these styles also affect how the node drawn/painted.
pub style: Style,
/// Describes whether and how the button has been interacted with by the input
pub interaction: Interaction,
/// Whether this node should block interaction with lower nodes
pub focus_policy: FocusPolicy,
/// The background color, which serves as a "fill" for this node
pub background_color: BackgroundColor,
/// The color of the Node's border
pub border_color: BorderColor,
/// This is used as the cosmic text canvas
pub image: UiImage,
/// The transform of the node
///
/// This field is automatically managed by the UI layout system.
/// To alter the position of the `NodeBundle`, use the properties of the [`Style`] component.
pub transform: Transform,
/// The global transform of the node
///
/// This field is automatically managed by the UI layout system.
/// To alter the position of the `NodeBundle`, use the properties of the [`Style`] component.
pub global_transform: GlobalTransform,
/// Describes the visibility properties of the node
pub visibility: Visibility,
/// Algorithmically-computed indication of whether an entity is visible and should be extracted for rendering
pub computed_visibility: ComputedVisibility,
/// Indicates the depth at which the node should appear in the UI
pub z_index: ZIndex,
// cosmic bits
/// cosmic-text Editor, holds the text buffer + font system
pub editor: CosmicEditor,
/// text positioning enum
pub text_position: CosmicTextPosition,
/// text metrics
pub cosmic_metrics: CosmicMetrics,
/// edit history
pub cosmic_edit_history: CosmicEditHistory,
/// text attributes
pub cosmic_attrs: CosmicAttrs,
/// bg img
pub background_image: CosmicBackground,
}
impl Default for CosmicEditUiBundle {
fn default() -> Self {
Self {
focus_policy: FocusPolicy::Block,
node: Default::default(),
button: Default::default(),
style: Default::default(),
border_color: BorderColor(Color::NONE),
interaction: Default::default(),
background_color: Default::default(),
image: Default::default(),
transform: Default::default(),
global_transform: Default::default(),
visibility: Default::default(),
computed_visibility: Default::default(),
z_index: Default::default(),
editor: Default::default(),
text_position: Default::default(),
cosmic_metrics: Default::default(),
cosmic_edit_history: Default::default(),
cosmic_attrs: Default::default(),
background_image: Default::default(),
}
}
}
#[derive(Bundle)]
pub struct CosmicEditSpriteBundle {
// Bevy Sprite Bits
pub sprite: Sprite,
pub transform: Transform,
pub global_transform: GlobalTransform,
pub texture: Handle<Image>,
/// User indication of whether an entity is visible
pub visibility: Visibility,
/// Algorithmically-computed indication of whether an entity is visible and should be extracted for rendering
pub computed_visibility: ComputedVisibility,
//
pub background_color: BackgroundColor,
// cosmic bits
/// cosmic-text Editor, holds the text buffer + font system
pub editor: CosmicEditor,
/// text positioning enum
pub text_position: CosmicTextPosition,
/// text metrics
pub cosmic_metrics: CosmicMetrics,
/// edit history
pub cosmic_edit_history: CosmicEditHistory,
/// text attributes
pub cosmic_attrs: CosmicAttrs,
/// bg img
pub background_image: CosmicBackground,
}
impl Default for CosmicEditSpriteBundle {
fn default() -> Self {
Self {
sprite: Default::default(),
transform: Default::default(),
global_transform: Default::default(),
texture: DEFAULT_IMAGE_HANDLE.typed(),
visibility: Visibility::Hidden,
computed_visibility: Default::default(),
//
background_color: Default::default(),
//
editor: Default::default(),
text_position: Default::default(),
cosmic_metrics: Default::default(),
cosmic_edit_history: Default::default(),
cosmic_attrs: Default::default(),
background_image: Default::default(),
}
}
}
#[derive(Clone)]
pub struct EditHistoryItem {
pub cursor: Cursor,
pub lines: Vec<Vec<(String, AttrsOwned)>>,
}
pub struct CosmicEditHistory {
pub edits: VecDeque<EditHistoryItem>,
pub current_edit: usize,
}
/// Plugin struct that adds systems and initializes resources related to cosmic edit functionality.
#[derive(Default)]
pub struct CosmicEditPlugin {
pub font_config: CosmicFontConfig,
}
impl Plugin for CosmicEditPlugin {
fn build(&self, app: &mut App) {
let font_system = create_cosmic_font_system(self.font_config.clone());
app.add_systems(PreUpdate, cosmic_editor_builder)
.add_systems(
Update,
(
change_active_editor,
cosmic_edit_bevy_events,
cosmic_edit_set_redraw,
on_scale_factor_change,
cosmic_edit_redraw_buffer_ui
.before(cosmic_edit_set_redraw)
.before(on_scale_factor_change),
cosmic_edit_redraw_buffer.before(on_scale_factor_change),
),
)
.init_resource::<ActiveEditor>()
// .add_asset::<CosmicFont>()
.insert_resource(SwashCacheState {
swash_cache: SwashCache::new(),
})
.insert_resource(CosmicFontSystem(font_system));
}
}
/// Resource struct that keeps track of the currently active editor entity.
#[derive(Resource, Default)]
pub struct ActiveEditor {
pub entity: Option<Entity>,
}
fn change_active_editor(
mut commands: Commands,
mut interaction_query: Query<
(&Interaction, &mut CosmicEditor, Entity),
(Changed<Interaction>, With<CosmicEditor>),
>,
) {
for (interaction, cosmic_edit, entity) in interaction_query.iter_mut() {
if let Interaction::Pressed = interaction {
info!("PRESSED");
commands.insert_resource(ActiveEditor {
entity: Some(entity),
});
info!("Widget text: {}", get_cosmic_text(cosmic_edit.0.buffer()));
}
}
}
/// Resource struct that holds configuration options for cosmic fonts.
pub struct CosmicFontConfig {
pub fonts_dir_path: Option<PathBuf>,
pub font_bytes: Option<Vec<&'static [u8]>>,
pub load_system_fonts: bool, // caution: this can be relatively slow
}
impl Default for CosmicFontConfig {
fn default() -> Self {
Self {
load_system_fonts: true,
font_bytes: None,
fonts_dir_path: None,
}
}
}
#[derive(Resource)]
struct SwashCacheState {
swash_cache: SwashCache,
}
fn create_cosmic_font_system(cosmic_font_config: CosmicFontConfig) -> FontSystem {
let locale = sys_locale::get_locale().unwrap_or_else(|| String::from("en-US"));
let mut db = cosmic_text::fontdb::Database::new();
if let Some(dir_path) = cosmic_font_config.fonts_dir_path.clone() {
db.load_fonts_dir(dir_path);
}
if let Some(custom_font_data) = &cosmic_font_config.font_bytes {
for elem in custom_font_data {
db.load_font_data(elem.to_vec());
}
}
if cosmic_font_config.load_system_fonts {
db.load_system_fonts();
}
cosmic_text::FontSystem::new_with_locale_and_db(locale, db)
}
fn on_scale_factor_change(
mut scale_factor_changed: EventReader<WindowScaleFactorChanged>,
mut cosmic_query: Query<(&mut CosmicEditor, &mut CosmicMetrics)>,
mut font_system: ResMut<CosmicFontSystem>,
) {
if !scale_factor_changed.is_empty() {
let new_scale_factor = scale_factor_changed.iter().last().unwrap().scale_factor as f32;
for (mut editor, metrics) in &mut cosmic_query.iter_mut() {
let font_system = &mut font_system.0;
let metrics =
Metrics::new(metrics.font_size, metrics.line_height).scale(new_scale_factor);
editor.0.buffer_mut().set_metrics(font_system, metrics);
editor.0.buffer_mut().set_redraw(true);
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
}
}
}
pub fn get_node_cursor_pos(
window: &Window,
node_transform: &GlobalTransform,
size: (f32, f32),
is_ui_node: bool,
camera: &Camera,
camera_transform: &GlobalTransform,
) -> Option<(f32, f32)> {
let (x_min, y_min, x_max, y_max) = (
node_transform.affine().translation.x - size.0 / 2.,
node_transform.affine().translation.y - size.1 / 2.,
node_transform.affine().translation.x + size.0 / 2.,
node_transform.affine().translation.y + size.1 / 2.,
);
window.cursor_position().and_then(|pos| {
if is_ui_node {
if x_min < pos.x && pos.x < x_max && y_min < pos.y && pos.y < y_max {
Some((pos.x - x_min, pos.y - y_min))
} else {
None
}
} else {
camera
.viewport_to_world_2d(camera_transform, pos)
.and_then(|pos| {
if x_min < pos.x && pos.x < x_max && y_min < pos.y && pos.y < y_max {
Some((pos.x - x_min, y_max - pos.y))
} else {
None
}
})
}
})
}
// Sets text in a CosmicEdit
pub fn cosmic_edit_set_text(
text: CosmicText,
attrs: AttrsOwned,
editor: &mut Editor,
font_system: &mut FontSystem,
) {
editor.buffer_mut().lines.clear();
match text {
CosmicText::OneStyle(text) => {
println!("ONESTYLE {:?}, {:?}", text, attrs);
editor.buffer_mut().set_text(
font_system,
text.as_str(),
attrs.as_attrs(),
Shaping::Advanced,
);
}
CosmicText::MultiStyle(lines) => {
println!("M-M-M-M-MULTI-STYLE");
for line in lines {
let mut line_text = String::new();
let mut attrs_list = AttrsList::new(attrs.as_attrs());
for (text, attrs) in line.iter() {
let start = line_text.len();
line_text.push_str(text);
let end = line_text.len();
attrs_list.add_span(start..end, attrs.as_attrs());
}
editor.buffer_mut().lines.push(BufferLine::new(
line_text,
attrs_list,
Shaping::Advanced,
));
}
}
}
}
/// Retrieves the cosmic text content from an editor.
///
/// # Arguments
///
/// * `editor` - A reference to the `Editor` instance containing the text content.
///
/// # Returns
///
/// A `String` containing the cosmic text content.
// TODO put this on the CosmicEdit impl
pub fn get_cosmic_text(buffer: &Buffer) -> String {
let mut text = String::new();
let line_count = buffer.lines.len();
for (i, line) in buffer.lines.iter().enumerate() {
text.push_str(line.text());
if i < line_count - 1 {
text.push('\n');
}
}
text
}
/// Returns texts from a MultiStyle buffer
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
pub fn get_text_spans(
buffer: &Buffer,
default_attrs: AttrsOwned,
) -> Vec<Vec<(String, AttrsOwned)>> {
let mut spans = Vec::new();
for line in buffer.lines.iter() {
let mut line_spans = Vec::new();
let line_text = line.text();
let line_attrs = line.attrs_list();
if line_attrs.spans().is_empty() {
line_spans.push((line_text.to_string(), default_attrs.clone()));
} else {
let mut current_pos = 0;
for span in line_attrs.spans() {
let span_range = span.0;
let span_attrs = span.1.clone();
let start_index = span_range.start;
let end_index = span_range.end;
if start_index > current_pos {
// Add the text between the current position and the start of the span
let non_span_text = line_text[current_pos..start_index].to_string();
line_spans.push((non_span_text, default_attrs.clone()));
}
let span_text = line_text[start_index..end_index].to_string();
line_spans.push((span_text.clone(), span_attrs));
current_pos = end_index;
}
if current_pos < line_text.len() {
// Add the remaining text after the last span
let remaining_text = line_text[current_pos..].to_string();
line_spans.push((remaining_text, default_attrs.clone()));
}
}
spans.push(line_spans);
}
spans
}
fn save_edit_history(
editor: &mut Editor,
attrs: &AttrsOwned,
edit_history: &mut CosmicEditHistory,
) {
let current_lines = get_text_spans(editor.buffer(), attrs.clone());
let current_edit = edit_history.current_edit;
let mut new_edits = VecDeque::new();
new_edits.extend(edits.iter().take(current_edit + 1).cloned());
// remove old edits
if new_edits.len() > 1000 {
new_edits.drain(0..100);
}
new_edits.push_back(EditHistoryItem {
lines: current_lines,
});
let len = new_edits.len();
*edit_history = CosmicEditHistory {
edits: new_edits,
current_edit: len - 1,
};
}
pub fn bevy_color_to_cosmic(color: bevy::prelude::Color) -> cosmic_text::Color {
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
cosmic_text::Color::rgba(
(color.r() * 255.) as u8,
(color.g() * 255.) as u8,
(color.b() * 255.) as u8,
(color.a() * 255.) as u8,
)
}
fn get_text_size(buffer: &Buffer) -> (f32, f32) {
let width = buffer.layout_runs().map(|run| run.line_w).reduce(f32::max);
let height = buffer.layout_runs().count() as f32 * buffer.metrics().line_height;
if width.is_none() || height == 0. {
return (1., 1.);
}
(width.unwrap(), height)
}
pub fn get_y_offset(buffer: &Buffer) -> i32 {
let (_, text_height) = get_text_size(buffer);
((buffer.size().1 - text_height) / 2.0) as i32
}
pub fn get_x_offset(buffer: &Buffer) -> i32 {
let (text_width, _) = get_text_size(buffer);
((buffer.size().0 - text_width) / 2.0) as i32
}
#[allow(clippy::too_many_arguments, clippy::type_complexity)]
// the meat of the input management
// why public?
pub fn cosmic_edit_bevy_events(
windows: Query<&Window, With<PrimaryWindow>>,
active_editor: Res<ActiveEditor>,
keys: Res<Input<KeyCode>>,
mut char_evr: EventReader<ReceivedCharacter>,
buttons: Res<Input<MouseButton>>,
mut cosmic_edit_query: Query<
(
&CosmicAttrs,
&CosmicTextPosition,
readonly_query: Query<&ReadOnly>,
style_query: Query<&mut Style>,
sprite_query: Query<&mut Sprite>,
mut font_system: ResMut<CosmicFontSystem>,
mut is_deleting: Local<bool>,
mut scroll_evr: EventReader<MouseWheel>,
mut edits_duration: Local<Option<Duration>>,
mut undoredo_duration: Local<Option<Duration>>,
camera_q: Query<(&Camera, &GlobalTransform)>,
) {
let primary_window = windows.single();
let scale_factor = primary_window.scale_factor() as f32;
let (camera, camera_transform) = camera_q.iter().find(|(c, _)| c.is_active).unwrap();
for (mut editor, mut edit_history, node_transform, attrs, text_position, entity) in
let readonly = readonly_query.get(entity).is_ok();
let (width, height, is_ui_node) = match style_query.get(entity) {
Ok(style) => (
style.width.evaluate(1.).unwrap(),
style.height.evaluate(1.).unwrap(),
true,
),
Err(_) => {
let sprite = sprite_query.get(entity).unwrap();
let size = sprite.custom_size.unwrap();
(size.x, size.y, false)
}
};
let editor = &mut editor.0;
let attrs = &attrs.0;
#[cfg(target_os = "macos")]
let command = keys.any_pressed([KeyCode::SuperLeft, KeyCode::SuperRight]);
#[cfg(not(target_os = "macos"))]
let command = keys.any_pressed([KeyCode::ControlLeft, KeyCode::ControlRight]);
let shift = keys.any_pressed([KeyCode::ShiftLeft, KeyCode::ShiftRight]);
#[cfg(target_os = "macos")]
let option = keys.any_pressed([KeyCode::AltLeft, KeyCode::AltRight]);
// if shift key is pressed
let already_has_selection = editor.select_opt().is_some();
if shift && !already_has_selection {
let cursor = editor.cursor();
editor.set_select_opt(Some(cursor));
}
#[cfg(target_os = "macos")]
let should_jump = command && option;
#[cfg(not(target_os = "macos"))]
let should_jump = command;
if should_jump && keys.just_pressed(KeyCode::Left) {
editor.action(&mut font_system.0, Action::PreviousWord);
if !shift {
editor.set_select_opt(None);
// RETURN
return;
}
if should_jump && keys.just_pressed(KeyCode::Right) {
editor.action(&mut font_system.0, Action::NextWord);
if !shift {
editor.set_select_opt(None);
// RETURN
return;
}
if should_jump && keys.just_pressed(KeyCode::Home) {
editor.action(&mut font_system.0, Action::BufferStart);
// there's a bug with cosmic text where it doesn't update the visual cursor for this action
// TODO: fix upstream
editor.buffer_mut().set_redraw(true);
if !shift {
editor.set_select_opt(None);
// RETURN
return;
}
if should_jump && keys.just_pressed(KeyCode::End) {
editor.action(&mut font_system.0, Action::BufferEnd);
// there's a bug with cosmic text where it doesn't update the visual cursor for this action
// TODO: fix upstream
editor.buffer_mut().set_redraw(true);
if !shift {
editor.set_select_opt(None);
if keys.just_pressed(KeyCode::Left) {
editor.action(&mut font_system.0, Action::Left);
if !shift {
editor.set_select_opt(None);
// RETURN
return;
}
if keys.just_pressed(KeyCode::Right) {
editor.action(&mut font_system.0, Action::Right);
if !shift {
editor.set_select_opt(None);
// RETURN
return;
}
if keys.just_pressed(KeyCode::Up) {
editor.action(&mut font_system.0, Action::Up);
if !shift {
editor.set_select_opt(None);
// RETURN
return;
}
if keys.just_pressed(KeyCode::Down) {
editor.action(&mut font_system.0, Action::Down);
if !shift {
editor.set_select_opt(None);
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
// RETURN
return;
}
if !readonly && keys.just_pressed(KeyCode::Back) {
#[cfg(target_arch = "wasm32")]
editor.editor.action(&mut font_system.0, Action::Backspace);
*is_deleting = true;
}
if !readonly && keys.just_released(KeyCode::Back) {
*is_deleting = false;
}
if !readonly && keys.just_pressed(KeyCode::Delete) {
editor.action(&mut font_system.0, Action::Delete);
}
if keys.just_pressed(KeyCode::Escape) {
editor.action(&mut font_system.0, Action::Escape);
}
if command && keys.just_pressed(KeyCode::A) {
editor.action(&mut font_system.0, Action::BufferEnd);
let current_cursor = editor.cursor();
editor.set_select_opt(Some(Cursor {
line: 0,
index: 0,
affinity: current_cursor.affinity,
color: current_cursor.color,
}));
// RETURN
return;
}
if keys.just_pressed(KeyCode::Home) {
editor.action(&mut font_system.0, Action::Home);
if !shift {
editor.set_select_opt(None);
// RETURN
return;
}
if keys.just_pressed(KeyCode::End) {
editor.action(&mut font_system.0, Action::End);
if !shift {
editor.set_select_opt(None);
// RETURN
return;
}
if keys.just_pressed(KeyCode::PageUp) {
editor.action(&mut font_system.0, Action::PageUp);
if !shift {
editor.set_select_opt(None);
// RETURN
return;
}
if keys.just_pressed(KeyCode::PageDown) {
editor.action(&mut font_system.0, Action::PageDown);
if !shift {
editor.set_select_opt(None);
// RETURN
return;
}
// redo
#[cfg(not(target_os = "windows"))]
let requested_redo = command && shift && keys.just_pressed(KeyCode::Z);
#[cfg(target_os = "windows")]
let requested_redo = command && keys.just_pressed(KeyCode::Y);
if !readonly && requested_redo {
let edits = &edit_history.edits;
if edits.is_empty() {
if edit_history.current_edit == edits.len() - 1 {
let idx = edit_history.current_edit + 1;
if let Some(current_edit) = edits.get(idx) {
editor.buffer_mut().lines.clear();
for line in current_edit.lines.iter() {
let mut line_text = String::new();
let mut attrs_list = AttrsList::new(attrs.as_attrs());
for (text, attrs) in line.iter() {
let start = line_text.len();
line_text.push_str(text);
let end = line_text.len();
attrs_list.add_span(start..end, attrs.as_attrs());
}
editor.buffer_mut().lines.push(BufferLine::new(
line_text,
attrs_list,
Shaping::Advanced,
));
editor.set_cursor(current_edit.cursor);
editor.buffer_mut().set_redraw(true);
edit_history.current_edit += 1;
*undoredo_duration = Some(Duration::from_millis(now_ms as u64));
// RETURN
return;
}
// undo
let requested_undo = command && keys.just_pressed(KeyCode::Z);
if !readonly && requested_undo {
let edits = &edit_history.edits;
if edits.is_empty() {
if edit_history.current_edit <= 1 {
let idx = edit_history.current_edit - 1;
if let Some(current_edit) = edits.get(idx) {
editor.buffer_mut().lines.clear();
for line in current_edit.lines.iter() {
let mut line_text = String::new();
let mut attrs_list = AttrsList::new(attrs.as_attrs());
for (text, attrs) in line.iter() {
let start = line_text.len();
line_text.push_str(text);
let end = line_text.len();
attrs_list.add_span(start..end, attrs.as_attrs());
editor.buffer_mut().lines.push(BufferLine::new(
line_text,
attrs_list,
Shaping::Advanced,
));
editor.set_cursor(current_edit.cursor);
editor.buffer_mut().set_redraw(true);
edit_history.current_edit -= 1;
*undoredo_duration = Some(Duration::from_millis(now_ms as u64));
// RETURN
return;
}
let mut is_clipboard = false;
#[cfg(not(target_arch = "wasm32"))]
{
if let Ok(mut clipboard) = arboard::Clipboard::new() {
if command && keys.just_pressed(KeyCode::C) {
if let Some(text) = editor.copy_selection() {
clipboard.set_text(text).unwrap();
// RETURN
return;
}
if !readonly && command && keys.just_pressed(KeyCode::X) {
if let Some(text) = editor.copy_selection() {
clipboard.set_text(text).unwrap();
editor.delete_selection();
is_clipboard = true;
}
if !readonly && command && keys.just_pressed(KeyCode::V) {
if let Ok(text) = clipboard.get_text() {
for c in text.chars() {
editor.action(&mut font_system.0, Action::Insert(c));
}
let (offset_x, offset_y) = match text_position {
CosmicTextPosition::Center => {
(get_x_offset(editor.buffer()), get_y_offset(editor.buffer()))
}
CosmicTextPosition::TopLeft => (0, 0),
};
let point = |node_cursor_pos: (f32, f32)| {
(
(node_cursor_pos.0 * scale_factor) as i32 - offset_x,
(node_cursor_pos.1 * scale_factor) as i32 - offset_y,
)
};
if buttons.just_pressed(MouseButton::Left) {
if let Some(node_cursor_pos) = get_node_cursor_pos(
primary_window,
node_transform,
// TODO this does not work with percent sizes
// well like I haven't tested it so it might
width, height,
),
is_ui_node,
camera,
camera_transform,
) {
let (x, y) = point(node_cursor_pos);
if shift {
editor.action(&mut font_system.0, Action::Drag { x, y });
} else {
editor.action(&mut font_system.0, Action::Click { x, y });
// RETURN
return;
}
if buttons.pressed(MouseButton::Left) {
if let Some(node_cursor_pos) = get_node_cursor_pos(
primary_window,
node_transform,
(width, height),
is_ui_node,
camera,
camera_transform,
) {
let (x, y) = point(node_cursor_pos);
editor.action(&mut font_system.0, Action::Drag { x, y });
// RETURN
return;
}
for ev in scroll_evr.iter() {
match ev.unit {
MouseScrollUnit::Line => {
editor.action(
&mut font_system.0,
Action::Scroll {
lines: -ev.y as i32,
},
);
}
MouseScrollUnit::Pixel => {
let line_height = editor.buffer().metrics().line_height;
editor.action(
&mut font_system.0,
Action::Scroll {
lines: -(ev.y / line_height) as i32,
},
);
if readonly {
// RETURN
return;
}
let mut is_edit = is_clipboard;
let mut is_return = false;
if keys.just_pressed(KeyCode::Return) {
is_return = true;
is_edit = true;
// to have new line on wasm rather than E
editor.action(&mut font_system.0, Action::Insert('\n'));
}
if !(is_clipboard || is_return) {
for char_ev in char_evr.iter() {
is_edit = true;
if *is_deleting {
editor.action(&mut font_system.0, Action::Backspace);
} else {
editor.action(&mut font_system.0, Action::Insert(char_ev.char));
if !is_edit {
// RETURN
return;
}
if let Some(last_edit_duration) = *edits_duration {
if Duration::from_millis(now_ms as u64) - last_edit_duration
> Duration::from_millis(150)
{
save_edit_history(editor, attrs, &mut edit_history);
*edits_duration = Some(Duration::from_millis(now_ms as u64));
}
} else {
save_edit_history(editor, attrs, &mut edit_history);
*edits_duration = Some(Duration::from_millis(now_ms as u64));
fn cosmic_edit_set_redraw(mut cosmic_edit_query: Query<&mut CosmicEditor, Added<CosmicEditor>>) {
for mut editor in cosmic_edit_query.iter_mut() {