Skip to content
Snippets Groups Projects
focus.rs 2.2 KiB
Newer Older
Caleb Yates's avatar
Caleb Yates committed
use crate::{prelude::*, widget::WidgetSet};
sam edelsten's avatar
sam edelsten committed
use cosmic_text::{Edit, Editor};

/// System set for focus systems. Runs in `PostUpdate`
#[derive(SystemSet, Debug, Clone, PartialEq, Eq, Hash)]
Caleb Yates's avatar
Caleb Yates committed
pub(crate) struct FocusSet;
pub(crate) struct FocusPlugin;

impl Plugin for FocusPlugin {
    fn build(&self, app: &mut App) {
        app.add_systems(
            PostUpdate,
            (drop_editor_unfocused, add_editor_to_focused)
                .chain()
                .in_set(FocusSet)
                .after(WidgetSet),
        .init_resource::<FocusedWidget>()
        .register_type::<FocusedWidget>();
sam edelsten's avatar
sam edelsten committed
/// Resource struct that keeps track of the currently active editor entity.
Caleb Yates's avatar
Caleb Yates committed
///
/// The focussed entity must have a [`CosmicEditBuffer`], and should have a
/// [`CosmicEditor`] component as well if it can be mutated (i.e. isn't [`Readonly`]).
#[derive(Resource, Reflect, Default, Deref, DerefMut)]
#[reflect(Resource)]
sam edelsten's avatar
sam edelsten committed
pub struct FocusedWidget(pub Option<Entity>);

Caleb Yates's avatar
Caleb Yates committed
/// Adds [`CosmicEditor`] by copying from existing [`CosmicEditBuffer`].
sam edelsten's avatar
sam edelsten committed
pub(crate) fn add_editor_to_focused(
    mut commands: Commands,
    active_editor: Res<FocusedWidget>,
Caleb Yates's avatar
Caleb Yates committed
    q: Query<&CosmicEditBuffer, Without<CosmicEditor>>,
sam edelsten's avatar
sam edelsten committed
) {
    if let Some(e) = active_editor.0 {
        let Ok(b) = q.get(e) else {
            return;
        };
        let mut editor = Editor::new(b.0.clone());
        editor.set_redraw(true);
        commands.entity(e).insert(CosmicEditor::new(editor));
    }
}

Caleb Yates's avatar
Caleb Yates committed
/// Removes [`CosmicEditor`]
sam edelsten's avatar
sam edelsten committed
pub(crate) fn drop_editor_unfocused(
    mut commands: Commands,
    active_editor: Res<FocusedWidget>,
Caleb Yates's avatar
Caleb Yates committed
    mut q: Query<(Entity, &mut CosmicEditBuffer, &CosmicEditor)>,
sam edelsten's avatar
sam edelsten committed
) {
    if active_editor.0.is_none() {
        for (e, mut b, ed) in q.iter_mut() {
            b.lines = ed.with_buffer(|buf| buf.lines.clone());
            b.set_redraw(true);
            commands.entity(e).remove::<CosmicEditor>();
        }
    } else if let Some(focused) = active_editor.0 {
        for (e, mut b, ed) in q.iter_mut() {
            if e != focused {
                b.lines = ed.with_buffer(|buf| buf.lines.clone());
                b.set_redraw(true);
                commands.entity(e).remove::<CosmicEditor>();
            }
        }
    }
}