Skip to content

Commit

Permalink
TileMap focus, Style helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
ecton committed Nov 9, 2023
1 parent 22fb955 commit a818cc4
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 5 deletions.
7 changes: 3 additions & 4 deletions examples/style.rs
Original file line number Diff line number Diff line change
@@ -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)
}
5 changes: 5 additions & 0 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
15 changes: 14 additions & 1 deletion src/widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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<Component>) -> 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
Expand Down Expand Up @@ -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);
}
Expand Down
13 changes: 13 additions & 0 deletions src/widgets/tilemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -78,6 +79,18 @@ where
}
}

fn accept_focus(&mut self, _context: &mut EventContext<'_, '_>) -> bool {
true
}

fn hit_test(
&mut self,
_location: kludgine::figures::Point<kludgine::figures::units::Px>,
_context: &mut EventContext<'_, '_>,
) -> bool {
true
}

fn layout(
&mut self,
available_space: Size<ConstraintLimit>,
Expand Down

0 comments on commit a818cc4

Please sign in to comment.