From a818cc41fdd8606b8c4e8b8c7387a501e218f62b Mon Sep 17 00:00:00 2001 From: Jonathan Johnson Date: Thu, 9 Nov 2023 06:58:58 -0800 Subject: [PATCH] TileMap focus, Style helpers --- examples/style.rs | 7 +++---- src/context.rs | 5 +++++ src/widget.rs | 15 ++++++++++++++- src/widgets/tilemap.rs | 13 +++++++++++++ 4 files changed, 35 insertions(+), 5 deletions(-) diff --git a/examples/style.rs b/examples/style.rs index 809a1e647..f47a0e671 100644 --- a/examples/style.rs +++ b/examples/style.rs @@ -1,18 +1,17 @@ use gooey::styles::components::TextColor; -use gooey::styles::Styles; use gooey::widget::{MakeWidget, Widget}; use gooey::widgets::stack::Stack; use gooey::widgets::{Button, Style}; -use gooey::{styles, Run}; +use gooey::Run; use kludgine::Color; fn main() -> gooey::Result { Stack::rows(Button::new("Green").and(red_text(Button::new("Red")))) - .with_styles(Styles::new().with(&TextColor, Color::GREEN)) + .with(&TextColor, Color::GREEN) .run() } /// Creating reusable style helpers that work with any Widget is straightfoward fn red_text(w: impl Widget) -> Style { - Style::new(styles!(TextColor => Color::RED), w) + w.with(&TextColor, Color::RED) } diff --git a/src/context.rs b/src/context.rs index 266f2bdb1..968500834 100644 --- a/src/context.rs +++ b/src/context.rs @@ -451,6 +451,11 @@ impl<'context, 'window, 'clip, 'gfx, 'pass> GraphicsContext<'context, 'window, ' /// To ensure the correct color is used, include [`HighlightColor`] in the /// styles request. pub fn draw_focus_ring_using(&mut self, styles: &Styles) { + // If this is the root widget, don't draw a focus ring. It's redundant. + if !self.current_node.has_parent() { + return; + } + let visible_rect = Rect::from(self.graphics.region().size - (Px(1), Px(1))); let focus_ring = Shape::stroked_rect( visible_rect, diff --git a/src/widget.rs b/src/widget.rs index b91fbeb08..d38980400 100644 --- a/src/widget.rs +++ b/src/widget.rs @@ -16,7 +16,7 @@ use kludgine::figures::{IntoSigned, IntoUnsigned, Point, Rect, Size}; use crate::context::{AsEventContext, EventContext, GraphicsContext, LayoutContext}; use crate::styles::components::VisualOrder; -use crate::styles::Styles; +use crate::styles::{Component, NamedComponent, Styles}; use crate::tree::Tree; use crate::value::{IntoValue, Value}; use crate::widgets::{Align, Expand, Scroll, Style}; @@ -444,6 +444,13 @@ pub trait MakeWidget: Sized { Style::new(styles, self) } + /// Associates a style component with `self`. + fn with(self, name: &impl NamedComponent, component: impl Into) -> Style { + let mut styles = Styles::new(); + styles.insert(name, component); + Style::new(styles, self) + } + /// Sets the widget that should be focused next. /// /// Gooey automatically determines reverse tab order by using this same @@ -897,6 +904,12 @@ impl ManagedWidget { .and_then(|id| self.tree.widget(id)) } + /// Returns true if this node has a parent. + #[must_use] + pub fn has_parent(&self) -> bool { + self.tree.parent(self.id()).is_some() + } + pub(crate) fn attach_styles(&self, styles: Styles) { self.tree.attach_styles(self.id(), styles); } diff --git a/src/widgets/tilemap.rs b/src/widgets/tilemap.rs index 9d60893ac..b014b8f6b 100644 --- a/src/widgets/tilemap.rs +++ b/src/widgets/tilemap.rs @@ -69,6 +69,7 @@ where self.layers.map(|layers| { tilemap::draw(layers, focus, self.zoom, context.graphics.inner_graphics()); }); + context.draw_focus_ring(); if let Some(tick) = &self.tick { tick.rendered(context); @@ -78,6 +79,18 @@ where } } + fn accept_focus(&mut self, _context: &mut EventContext<'_, '_>) -> bool { + true + } + + fn hit_test( + &mut self, + _location: kludgine::figures::Point, + _context: &mut EventContext<'_, '_>, + ) -> bool { + true + } + fn layout( &mut self, available_space: Size,