-
Notifications
You must be signed in to change notification settings - Fork 42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Checkbox inputs #125
Checkbox inputs #125
Changes from all commits
8969635
d6cc3e7
0611159
1c0c92d
e0f5041
712b129
92c3d5a
cd6d244
4b8a8f0
6c271ae
3d0ccf1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
//! Drive the renderer from Dioxus | ||
|
||
use dioxus::prelude::*; | ||
|
||
fn main() { | ||
dioxus_blitz::launch(app); | ||
} | ||
|
||
fn app() -> Element { | ||
let mut checkbox_checked = use_signal(|| false); | ||
|
||
rsx! { | ||
div { | ||
class: "container", | ||
style { {CSS} } | ||
form { | ||
div { | ||
input { | ||
type: "checkbox", | ||
id: "check1", | ||
name: "check1", | ||
value: "check1", | ||
checked: "{checkbox_checked}", | ||
oninput: move |ev| { | ||
dbg!(ev); | ||
checkbox_checked.set(!checkbox_checked()); | ||
}, | ||
} | ||
label { | ||
r#for: "check1", | ||
"Checkbox 1 (controlled)" | ||
} | ||
} | ||
div { | ||
label { | ||
input { | ||
type: "checkbox", | ||
name: "check2", | ||
value: "check2", | ||
} | ||
"Checkbox 2 (uncontrolled)" | ||
} | ||
} | ||
} | ||
div { "Checkbox 1 checked: {checkbox_checked}" } | ||
} | ||
} | ||
} | ||
|
||
const CSS: &str = r#" | ||
|
||
.container { | ||
display: flex; | ||
flex-direction: column; | ||
justify-content: center; | ||
align-items: center; | ||
height: 100vh; | ||
width: 100vw; | ||
} | ||
|
||
|
||
form { | ||
margin: 12px 0; | ||
display: block; | ||
} | ||
|
||
form > div { | ||
margin: 8px 0; | ||
} | ||
|
||
label { | ||
display: inline-block; | ||
} | ||
|
||
input { | ||
/* Should be accent-color */ | ||
color: #0000cc; | ||
} | ||
|
||
"#; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,6 +40,7 @@ use style::{ | |
use image::{imageops::FilterType, DynamicImage}; | ||
use parley::layout::PositionedLayoutItem; | ||
use taffy::prelude::Layout; | ||
use vello::kurbo::{BezPath, Cap, Join}; | ||
use vello::{ | ||
kurbo::{Affine, Point, Rect, Shape, Stroke, Vec2}, | ||
peniko::{self, Brush, Color, Fill, Mix}, | ||
|
@@ -362,6 +363,7 @@ impl<'dom> VelloSceneGenerator<'dom> { | |
cx.stroke_devtools(scene); | ||
cx.draw_image(scene); | ||
cx.draw_svg(scene); | ||
cx.draw_input(scene); | ||
|
||
// Render the text in text inputs | ||
if let Some(input_data) = cx.text_input { | ||
|
@@ -441,7 +443,7 @@ impl<'dom> VelloSceneGenerator<'dom> { | |
} | ||
} | ||
|
||
fn element_cx<'w>(&'w self, element: &'w Node, location: Point) -> ElementCx { | ||
fn element_cx<'w>(&'w self, element: &'w Node, location: Point) -> ElementCx<'w> { | ||
let style = element | ||
.stylo_element_data | ||
.borrow() | ||
|
@@ -1091,4 +1093,61 @@ impl ElementCx<'_> { | |
) { | ||
unimplemented!() | ||
} | ||
|
||
fn draw_input(&self, scene: &mut Scene) { | ||
if self.element.local_name() == "input" | ||
&& matches!(self.element.attr(local_name!("type")), Some("checkbox")) | ||
{ | ||
let checked: bool = self | ||
.element | ||
.attr(local_name!("checked")) | ||
.and_then(|c| c.parse().ok()) | ||
.unwrap_or_default(); | ||
|
||
// TODO this should be coming from css accent-color, but I couldn't find how to retrieve it | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Such a change could be submitted to the DioxusLabs fork of Stylo. We are currently using the |
||
let accent_color = self.style.get_inherited_text().color.as_vello(); | ||
|
||
let scale = self | ||
.frame | ||
.outer_rect | ||
.width() | ||
.min(self.frame.outer_rect.height()) | ||
/ 16.0; | ||
|
||
let frame = self.frame.outer_rect.to_rounded_rect(scale * 2.0); | ||
|
||
if checked { | ||
scene.fill(Fill::NonZero, self.transform, accent_color, None, &frame); | ||
|
||
//Tick code derived from masonry | ||
let mut path = BezPath::new(); | ||
path.move_to((2.0, 9.0)); | ||
path.line_to((6.0, 13.0)); | ||
path.line_to((14.0, 2.0)); | ||
|
||
path.apply_affine(Affine::scale(scale)); | ||
|
||
let style = Stroke { | ||
width: 2.0 * scale, | ||
join: Join::Round, | ||
miter_limit: 10.0, | ||
start_cap: Cap::Round, | ||
end_cap: Cap::Round, | ||
dash_pattern: Default::default(), | ||
dash_offset: 0.0, | ||
}; | ||
|
||
scene.stroke(&style, self.transform, Color::WHITE, None, &path); | ||
} else { | ||
scene.fill(Fill::NonZero, self.transform, Color::WHITE, None, &frame); | ||
scene.stroke( | ||
&Stroke::default(), | ||
self.transform, | ||
accent_color, | ||
None, | ||
&frame, | ||
); | ||
} | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
checked
is a boolean attribute, so this should just check for the presence of thechecked
attribute in the array. If it is present thenchecked
istrue
elsefalse
(even the empty string or "false" counts astrue
).See: https://developer.mozilla.org/en-US/docs/Glossary/Boolean/HTML