Skip to content

Commit

Permalink
fix: ensure rects swallow UI events
Browse files Browse the repository at this point in the history
  • Loading branch information
ten3roberts committed Oct 23, 2024
1 parent 2004103 commit 410ec08
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
16 changes: 10 additions & 6 deletions violet-core/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::{
pub struct Input {}

#[derive(Debug, Clone)]
struct FocusedEntity {
pub struct FocusedEntity {
id: Entity,
sticky: bool,
}
Expand Down Expand Up @@ -46,7 +46,7 @@ impl InputState {
pos: Vec2,
mut filter: impl FnMut(&EntityRef) -> bool,
) -> Option<(Entity, Vec2)> {
let query = (screen_transform(), rect());
let query = (screen_transform(), rect()).filtered(focusable().with());
OrderedDfsIterator::new(&frame.world, frame.world.entity(self.root).unwrap())
.filter_map(|entity| {
if !filter(&entity) {
Expand Down Expand Up @@ -131,7 +131,7 @@ impl InputState {
pub fn on_cursor_move(&mut self, frame: &mut Frame, pos: Vec2) -> bool {
self.pos = pos;

if let Some(entity) = &self.focused(&frame.world) {
if let Some(entity) = &self.focused_entity(&frame.world) {
let transform = entity.get_copy(screen_transform()).unwrap_or_default();
let rect = entity.get_copy(rect()).unwrap_or_default();
if let Ok(mut on_input) = entity.get_mut(on_cursor_move()) {
Expand Down Expand Up @@ -185,7 +185,7 @@ impl InputState {
state: ElementState,
text: Option<SmolStr>,
) -> bool {
if let Some(entity) = &self.focused(frame.world()) {
if let Some(entity) = &self.focused_entity(frame.world()) {
if let Ok(mut on_input) = entity.get_mut(on_keyboard_input()) {
let s = ScopeRef::new(frame, *entity);
on_input(
Expand All @@ -205,12 +205,16 @@ impl InputState {
false
}

fn focused<'a>(&self, world: &'a World) -> Option<EntityRef<'a>> {
pub fn focused(&self) -> Option<&FocusedEntity> {
self.focused.as_ref()
}

fn focused_entity<'a>(&self, world: &'a World) -> Option<EntityRef<'a>> {
self.focused.as_ref().and_then(|v| world.entity(v.id).ok())
}

fn set_focused(&mut self, frame: &Frame, focused: Option<Entity>) {
let cur = self.focused(&frame.world);
let cur = self.focused_entity(&frame.world);

if cur.map(|v| v.id()) == focused {
return;
Expand Down
6 changes: 5 additions & 1 deletion violet-core/src/style/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use palette::{IntoColor, Oklab, Srgba};

use crate::{
components::{color, draw_shape, margin, max_size, maximize, min_size, padding, size},
input::focusable,
shape::shape_rectangle,
unit::Unit,
Edges, Scope,
Expand Down Expand Up @@ -243,7 +244,10 @@ impl Background {

pub fn mount(self, scope: &mut Scope) {
let c = self.color.resolve(&scope.stylesheet());
scope.set(draw_shape(shape_rectangle()), ()).set(color(), c);
scope
.set(draw_shape(shape_rectangle()), ())
.set(color(), c)
.set_default(focusable());
}
}

Expand Down

0 comments on commit 410ec08

Please sign in to comment.