Store button styles in Button

This commit is contained in:
Richard Feldman 2022-02-23 08:35:09 -05:00
parent f1b7c7383f
commit 90a25efbd8
No known key found for this signature in database
GPG key ID: 7E4127D1E4241798
4 changed files with 27 additions and 7 deletions

View file

@ -6,4 +6,10 @@ app "hello-gui"
render = render =
# btn = button { onPress : \prev, _ -> Action.none } (text "Hello, button!") # btn = button { onPress : \prev, _ -> Action.none } (text "Hello, button!")
Button (Text "Hello, World!") { left: 300, top: 400, height: 300, width: 400 } div0 = \numerator, denominator -> (numerator / denominator) |> Result.withDefault 0
rgba = \r, g, b, a -> { r: div0 r 255, g: div0 g 255, b: div0 b 255, a }
styles = { bgColor: rgba 100 200 250 1, borderColor: rgba 10 20 30 1, borderWidth : 10, textColor : rgba 220 220 250 1 }
Button (Text "Hello, World!") styles

View file

@ -5,9 +5,11 @@ platform "examples/hello-world"
imports [] imports []
provides [ renderForHost ] provides [ renderForHost ]
Dim : { left : F32, top : F32, width : F32, height : F32 } Rgba : { r : F32, g : F32, b : F32, a : F32 }
Elem : [ Button Elem Dim, Col (List Elem), Row (List Elem), Text Str ] ButtonStyles : { bgColor : Rgba, borderColor : Rgba, borderWidth : F32, textColor : Rgba }
Elem : [ Button Elem ButtonStyles, Col (List Elem), Row (List Elem), Text Str ]
renderForHost : Elem renderForHost : Elem
renderForHost = render renderForHost = render

View file

@ -550,6 +550,7 @@ fn add_drawable(
match elem.tag() { match elem.tag() {
Button => { Button => {
let button = unsafe { &elem.entry().button }; let button = unsafe { &elem.entry().button };
let styles = button.styles;
let child_bounds = add_drawable(&*button.child, bounds, drawables, glyph_brush); let child_bounds = add_drawable(&*button.child, bounds, drawables, glyph_brush);
drawables.push(Drawable { drawables.push(Drawable {
@ -557,9 +558,9 @@ fn add_drawable(
offset: (0.0, 0.0).into(), offset: (0.0, 0.0).into(),
// TODO let buttons specify this // TODO let buttons specify this
content: DrawableContent::FillRect { content: DrawableContent::FillRect {
color: (0.2, 0.2, 0.5, 1.0), color: styles.bg_color,
border_width: 10.0, border_width: styles.border_width,
border_color: (0.2, 0.5, 0.5, 1.0), border_color: styles.border_color,
}, },
}); });

View file

@ -1,3 +1,4 @@
use crate::graphics::colors::RgbaTup;
use crate::graphics::primitives::rect::Rect; use crate::graphics::primitives::rect::Rect;
use core::ffi::c_void; use core::ffi::c_void;
use core::mem::{self, ManuallyDrop}; use core::mem::{self, ManuallyDrop};
@ -70,6 +71,7 @@ impl RocElem {
} }
#[repr(u8)] #[repr(u8)]
#[allow(unused)] // This is actually used, just via a mem::transmute from u8
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub enum RocElemTag { pub enum RocElemTag {
Button = 0, Button = 0,
@ -81,7 +83,16 @@ pub enum RocElemTag {
#[repr(C)] #[repr(C)]
pub struct RocButton { pub struct RocButton {
pub child: ManuallyDrop<RocElem>, pub child: ManuallyDrop<RocElem>,
pub bounds: Rect, pub styles: ButtonStyles,
}
#[repr(C)]
#[derive(Copy, Clone, Debug)]
pub struct ButtonStyles {
pub bg_color: RgbaTup,
pub border_color: RgbaTup,
pub border_width: f32,
pub text_color: RgbaTup,
} }
#[repr(C)] #[repr(C)]