Skip to content

Commit

Permalink
divider class and some renaming
Browse files Browse the repository at this point in the history
  • Loading branch information
Uriopass committed Dec 23, 2023
1 parent 2bcd54d commit acdb03e
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 54 deletions.
18 changes: 9 additions & 9 deletions assets_gui/src/yakui_gui.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use yakui::shapes::RoundedRectangle;
use yakui::widgets::{List, Pad, StateResponse, TextBox};
use yakui::{
align, center, colored_box_container, column, constrained, pad, row, use_state, Alignment,
Expand All @@ -11,12 +10,11 @@ use engine::wgpu::RenderPass;
use engine::{set_cursor_icon, CursorIcon, Drawable, GfxContext, Mesh, SpriteBatch};
use geom::Matrix4;
use goryak::{
background, button_primary, center_width, checkbox_value, combo_box, dragvalue, icon,
interact_box, is_hovered, labelc, on_background, on_primary_container, on_secondary,
on_secondary_container, on_surface, outline, outline_variant, primary_container, round_rect,
scroll_vertical, secondary, secondary_container, set_theme, stretch_width, surface,
surface_variant, tertiary, use_changed, CountGrid, Draggable, MainAxisAlignItems, RoundRect,
Theme,
background, button_primary, center_width, checkbox_value, combo_box, divider, dragvalue, icon,
interact_box_radius, is_hovered, labelc, on_background, on_secondary, on_secondary_container,
on_surface, outline, outline_variant, round_rect, scroll_vertical, secondary,

Check warning on line 15 in assets_gui/src/yakui_gui.rs

View workflow job for this annotation

GitHub Actions / build

unused import: `outline`

Check warning on line 15 in assets_gui/src/yakui_gui.rs

View workflow job for this annotation

GitHub Actions / build

unused import: `outline`
secondary_container, set_theme, stretch_width, surface, surface_variant, use_changed,
CountGrid, Draggable, MainAxisAlignItems, RoundRect, Theme,
};

use crate::companies::Companies;
Expand Down Expand Up @@ -130,14 +128,16 @@ impl State {
folder: Option<bool>,
on_click: impl FnOnce(),
) {
let r = interact_box(
divider(outline_variant(), 3.0, 1.0);
let r = interact_box_radius(
if selected {
surface_variant()
} else {
surface()
},
surface_variant(),
surface_variant(),
0.0,
|| {
let mut p = Pad::ZERO;
p.left = 4.0;
Expand All @@ -161,7 +161,7 @@ impl State {
});
},
);
if r.just_clicked {
if r.clicked {
on_click();
}
}
Expand Down
18 changes: 0 additions & 18 deletions goryak/src/decoration.rs

This file was deleted.

97 changes: 97 additions & 0 deletions goryak/src/divider.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
use yakui_core::geometry::{Color, Constraints, Rect, Vec2};
use yakui_core::paint::PaintRect;
use yakui_core::widget::{LayoutContext, PaintContext, Widget};
use yakui_core::Response;

pub fn divider(color: Color, height: f32, thickness: f32) -> Response<DividerResponse> {
Divider::new(color, height, thickness).show()
}

/// A horizontal divider line. Will take up the whole width of the parent.
///
/// The line width is determined by the parent's width after the layout phase.
///
/// Responds with [DividerResponse].
#[derive(Debug)]
#[non_exhaustive]
pub struct Divider {
/// The color of the divider.
pub color: Color,
/// The thickness of the divider.
pub thickness: f32,
/// The height of the divider.
/// How much vertical space it takes up.
pub height: f32,
/// The indent of the divider from the left.
pub indent: f32,
/// The indent of the divider from the right.
pub end_indent: f32,
}

impl Divider {
pub fn new(color: Color, height: f32, thickness: f32) -> Self {
Self {
color,
thickness,
height,
indent: 0.0,
end_indent: 0.0,
}
}

pub fn show(self) -> Response<DividerResponse> {
yakui_widgets::util::widget::<DividerWidget>(self)
}
}

#[derive(Debug)]
pub struct DividerWidget {
props: Divider,
}

pub type DividerResponse = ();

impl Widget for DividerWidget {
type Props<'a> = Divider;
type Response = DividerResponse;

fn new() -> Self {
Self {
props: Divider::new(Color::WHITE, 0.0, 0.0),
}
}

fn update(&mut self, props: Self::Props<'_>) -> Self::Response {
self.props = props;
}

fn layout(&self, _ctx: LayoutContext<'_>, input: Constraints) -> Vec2 {
// We say we dont take horizontal space to avoid the divider making
// the parent wider than it should be.
Vec2::new(0.0, self.props.height.clamp(input.min.y, input.max.y))
}

fn paint(&self, ctx: PaintContext<'_>) {
let id = ctx.dom.current();
let Some(parent) = ctx.dom.get(id).unwrap().parent else {
return;
};
let line_width = ctx.layout.get(parent).unwrap().rect.size().x;

let outer_rect = ctx.layout.get(id).unwrap().rect;

let line_pos = outer_rect.pos()
+ Vec2::new(
self.props.indent,
(outer_rect.size().y - self.props.thickness) / 2.0,
);
let line_size = Vec2::new(
line_width - self.props.indent - self.props.end_indent,
self.props.thickness,
);

let mut line_rect = PaintRect::new(Rect::from_pos_size(line_pos, line_size));
line_rect.color = self.props.color;
line_rect.add(ctx.paint);
}
}
53 changes: 32 additions & 21 deletions goryak/src/interact_box.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use yakui_core::paint::PaintRect;
use yakui_core::widget::{EventContext, LayoutContext, PaintContext, Widget};
use yakui_core::Response;
use yakui_widgets::shapes::RoundedRectangle;
use yakui_widgets::widgets::{Pad, PadWidget};

/**
A colored box that can contain children.
Expand All @@ -15,17 +16,19 @@ Responds with [InteractBoxResponse].
pub struct InteractBox {
pub color: Color,
pub hover_color: Color,
pub click_color: Color,
pub active_color: Color,
pub border_radius: f32,
pub padding: Pad,
}

impl InteractBox {
pub fn empty() -> Self {
Self {
color: Color::WHITE,
hover_color: Color::WHITE,
click_color: Color::WHITE,
active_color: Color::WHITE,
border_radius: 0.0,
padding: Pad::ZERO,
}
}

Expand All @@ -41,30 +44,32 @@ impl InteractBox {
pub fn interact_box_radius(
color: Color,
hover_color: Color,
click_color: Color,
active_color: Color,
border_radius: f32,
children: impl FnOnce(),
) -> Response<InteractBoxResponse> {
InteractBox {
color,
hover_color,
click_color,
active_color,
border_radius,
padding: Pad::ZERO,
}
.show_children(children)
}

pub fn interact_box(
color: Color,
hover_color: Color,
click_color: Color,
active_color: Color,
children: impl FnOnce(),
) -> Response<InteractBoxResponse> {
InteractBox {
color,
hover_color,
click_color,
active_color,
border_radius: 0.0,
padding: Pad::ZERO,
}
.show_children(children)
}
Expand All @@ -77,10 +82,10 @@ pub struct InteractBoxWidget {

#[derive(Copy, Clone, Debug, Default)]
pub struct InteractBoxResponse {
pub hovered: bool,
pub mousedown: bool,
pub just_hovered: bool,
pub just_clicked: bool,
pub hovering: bool,
pub mouse_down: bool,
pub mouse_entered: bool,
pub clicked: bool,
}

impl Widget for InteractBoxWidget {
Expand All @@ -97,18 +102,18 @@ impl Widget for InteractBoxWidget {
fn update(&mut self, props: Self::Props<'_>) -> Self::Response {
self.props = props;
let resp = self.resp;
self.resp.just_hovered = false;
self.resp.just_clicked = false;
self.resp.mouse_entered = false;
self.resp.clicked = false;
resp
}

fn paint(&self, mut ctx: PaintContext<'_>) {
let node = ctx.dom.get_current();
let layout_node = ctx.layout.get(ctx.dom.current()).unwrap();

let curcolor = if self.resp.mousedown {
self.props.click_color
} else if self.resp.hovered {
let curcolor = if self.resp.mouse_down {
self.props.active_color
} else if self.resp.hovering {
self.props.hover_color
} else {
self.props.color
Expand All @@ -133,15 +138,21 @@ impl Widget for InteractBoxWidget {
EventInterest::MOUSE_ALL
}

fn layout(&self, ctx: LayoutContext<'_>, constraints: Constraints) -> Vec2 {
let mut p = PadWidget::new();
p.update(self.props.padding);
p.layout(ctx, constraints)
}

fn event(&mut self, _: EventContext<'_>, event: &WidgetEvent) -> EventResponse {
match event {
WidgetEvent::MouseEnter => {
self.resp.just_hovered = true;
self.resp.hovered = true;
self.resp.mouse_entered = true;
self.resp.hovering = true;
EventResponse::Bubble
}
WidgetEvent::MouseLeave => {
self.resp.hovered = false;
self.resp.hovering = false;
EventResponse::Bubble
}
WidgetEvent::MouseButtonChanged {
Expand All @@ -151,10 +162,10 @@ impl Widget for InteractBoxWidget {
..
} => {
if *down && *inside {
self.resp.just_clicked = true;
self.resp.mousedown = true;
self.resp.clicked = true;
self.resp.mouse_down = true;
} else {
self.resp.mousedown = false;
self.resp.mouse_down = false;
}
EventResponse::Bubble
}
Expand Down
4 changes: 2 additions & 2 deletions goryak/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
mod combo_box;
mod count_grid;
mod decoration;
mod divider;
mod dragvalue;
mod hovered;
mod icon;
Expand All @@ -13,7 +13,7 @@ mod util;

pub use combo_box::*;
pub use count_grid::*;
pub use decoration::*;
pub use divider::*;
pub use dragvalue::*;
pub use hovered::*;
pub use icon::icon;
Expand Down
3 changes: 2 additions & 1 deletion goryak/src/roundrect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ impl Widget for RoundRectWidget {
inner_rect.set_pos(inner_rect.pos() + Vec2::splat(thickness));

if self.props.radius > 0.0 {
let mut inner_rect = shapes::RoundedRectangle::new(inner_rect, self.props.radius);
let mut inner_rect =
shapes::RoundedRectangle::new(inner_rect, self.props.radius - thickness);
inner_rect.color = self.props.color;
inner_rect.add(ctx.paint);
} else {
Expand Down
3 changes: 0 additions & 3 deletions goryak/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@ use std::panic::Location;
use yakui_core::geometry::{Color, Constraints, Vec2};
use yakui_core::widget::{LayoutContext, PaintContext, Widget};
use yakui_core::{Response, WidgetId};
use yakui_widgets::font::FontName;
use yakui_widgets::util::widget;
use yakui_widgets::widgets::{Button, ButtonResponse, Text};
use yakui_widgets::{center, constrained, max_width};

use crate::icon::ICON_NAME_MAPPING;
use crate::{on_primary, on_secondary, primary, secondary, Scrollable, ScrollableResponse};

pub fn scroll_vertical(children: impl FnOnce()) -> Response<ScrollableResponse> {
Expand Down

0 comments on commit acdb03e

Please sign in to comment.