Skip to content

Commit 09d0ef8

Browse files
authored
Merge pull request #44 from museun/unadhoc
remove Adhoc trait, they can all be implemented via View
2 parents 303d017 + 35d88a5 commit 09d0ef8

File tree

8 files changed

+138
-161
lines changed

8 files changed

+138
-161
lines changed

src/view/adhoc.rs

Lines changed: 0 additions & 100 deletions
This file was deleted.

src/view/mod.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,6 @@ pub use style::{Elements, Palette, StyleKind};
3030

3131
mod internal_views;
3232

33-
mod adhoc;
34-
pub use adhoc::Adhoc;
35-
3633
mod builder;
3734
pub use builder::{Builder, View, ViewExt};
3835

src/view/ui.rs

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::{
1111
use super::{
1212
filter::{Filter, Filterable},
1313
input::InputState,
14-
internal_views, Adhoc, Builder, LayoutNodes, Palette, Response, State, View, ViewId, ViewNodes,
14+
internal_views, Builder, LayoutNodes, Palette, Response, State, View, ViewId, ViewNodes,
1515
};
1616

1717
impl<'a> Filterable for Ui<'a> {
@@ -48,13 +48,6 @@ impl<'a> Ui<'a> {
4848
}
4949

5050
impl<'a> Ui<'a> {
51-
pub fn adhoc<'v, A>(&self, view: A) -> A::Output
52-
where
53-
A: Adhoc<'v>,
54-
{
55-
view.show(self)
56-
}
57-
5851
pub fn show<'v, B>(&self, args: B) -> Response<<B::View as View>::Response>
5952
where
6053
B: Builder<'v>,
@@ -363,22 +356,22 @@ impl<'a> Ui<'a> {
363356
}
364357

365358
pub fn checkbox(&self, value: &mut bool, label: impl Into<Str>) -> Response<bool> {
366-
self.adhoc(views::checkbox(value, label))
359+
self.show(views::checkbox(value, label))
367360
}
368361

369362
pub fn todo_value(&self, value: &mut bool, label: impl Into<Str>) -> Response<bool> {
370-
self.adhoc(views::todo_value(value, label))
363+
self.show(views::todo_value(value, label))
371364
}
372365

373366
pub fn selected(&self, value: &mut bool, label: impl Into<Str>) -> Response<bool> {
374-
self.adhoc(views::selected(value, label))
367+
self.show(views::selected(value, label))
375368
}
376369

377370
pub fn radio<V>(&self, value: V, existing: &mut V, label: impl Into<Str>) -> Response<bool>
378371
where
379-
V: PartialEq,
372+
V: PartialEq + 'static,
380373
{
381-
self.adhoc(views::radio(value, existing, label))
374+
self.show(views::radio(value, existing, label))
382375
}
383376

384377
pub fn label(&self, data: impl Into<Str>) -> Response {

src/views/checkbox.rs

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
use crate::{
22
renderer::Rgba,
3-
view::{Adhoc, Palette, Response, StyleKind, Ui},
3+
view::{Builder, Palette, StyleKind, Ui, View},
44
Str,
55
};
66

77
use super::label::LabelStyle;
88

99
pub type CheckboxClass = fn(&Palette, bool) -> CheckboxStyle;
1010

11-
#[derive(Debug)]
11+
#[derive(Debug, Copy, Clone)]
1212
pub struct CheckboxStyle {
1313
pub checked: &'static str,
1414
pub unchecked: &'static str,
@@ -55,15 +55,32 @@ impl<'a> Checkbox<'a> {
5555
}
5656
}
5757

58-
impl<'a> Adhoc<'a> for Checkbox<'a> {
59-
// TODO make this an opaque response
60-
type Output = Response<bool>;
58+
impl<'v> Builder<'v> for Checkbox<'v> {
59+
type View = CheckboxView;
60+
}
61+
62+
#[derive(Debug)]
63+
pub struct CheckboxView {
64+
label: Str,
65+
class: StyleKind<CheckboxClass, CheckboxStyle>,
66+
}
67+
68+
impl View for CheckboxView {
69+
type Args<'v> = Checkbox<'v>;
70+
type Response = bool;
71+
72+
fn create(args: Self::Args<'_>) -> Self {
73+
Self {
74+
label: args.label,
75+
class: args.class,
76+
}
77+
}
6178

62-
fn show(self, ui: &Ui) -> Self::Output {
79+
fn update(&mut self, args: Self::Args<'_>, ui: &Ui) -> Self::Response {
6380
let resp = ui
6481
.mouse_area(|ui| {
6582
let style = match self.class {
66-
StyleKind::Deferred(style) => (style)(&ui.palette(), *self.value),
83+
StyleKind::Deferred(style) => (style)(&ui.palette(), *args.value),
6784
StyleKind::Direct(style) => style,
6885
};
6986

@@ -74,19 +91,19 @@ impl<'a> Adhoc<'a> for Checkbox<'a> {
7491
};
7592

7693
ui.horizontal(|ui| {
77-
let marker = if *self.value {
94+
let marker = if *args.value {
7895
style.checked
7996
} else {
8097
style.unchecked
8198
};
8299
ui.label(marker);
83-
ui.show(super::label(self.label).style(LabelStyle { foreground }));
100+
ui.show(super::label(&self.label).style(LabelStyle { foreground }));
84101
});
85102
})
86103
.flatten_left();
87104

88-
*self.value ^= resp.clicked();
89-
resp.map(|c| c.clicked())
105+
*args.value ^= resp.clicked();
106+
resp.clicked()
90107
}
91108
}
92109

src/views/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ pub use button::{button, Button, ButtonClass, ButtonResponse, ButtonStyle};
1717
mod checkbox;
1818
pub use checkbox::{checkbox, Checkbox, CheckboxClass, CheckboxStyle};
1919

20-
mod collapsible;
21-
pub use collapsible::{collapsible, Collapsible, CollapsibleClass, CollapsibleStyle};
20+
// mod collapsible;
21+
// pub use collapsible::{collapsible, Collapsible, CollapsibleClass, CollapsibleStyle};
2222

2323
mod constrain;
2424
pub use constrain::{Constrain, Unconstrained};

src/views/radio.rs

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1+
use std::marker::PhantomData;
2+
13
use crate::{
24
renderer::Rgba,
3-
view::{Adhoc, Palette, Response, StyleKind},
5+
view::{Builder, Palette, StyleKind, Ui, View},
46
Str,
57
};
68

79
use super::label::{label, LabelStyle};
810

911
pub type RadioClass = fn(&Palette, bool) -> RadioStyle;
1012

11-
#[derive(Debug)]
13+
#[derive(Debug, Copy, Clone)]
1214
pub struct RadioStyle {
1315
pub selected: Option<&'static str>,
1416
pub unselected: Option<&'static str>,
@@ -51,24 +53,55 @@ pub struct Radio<'a, V> {
5153
class: StyleKind<RadioClass, RadioStyle>,
5254
}
5355

54-
impl<'v, V> Adhoc<'v> for Radio<'v, V>
56+
impl<'v, V: PartialEq + 'static> Builder<'v> for Radio<'v, V> {
57+
type View = RadioView<V>;
58+
}
59+
60+
pub struct RadioView<V>
5561
where
56-
V: PartialEq,
62+
V: PartialEq + 'static,
5763
{
58-
type Output = Response<bool>;
64+
label: Str,
65+
class: StyleKind<RadioClass, RadioStyle>,
66+
_marker: std::marker::PhantomData<V>,
67+
}
68+
69+
impl<V: PartialEq> std::fmt::Debug for RadioView<V> {
70+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
71+
f.debug_struct("RadioView")
72+
.field("label", &self.label)
73+
.field("class", &self.class)
74+
.finish()
75+
}
76+
}
77+
78+
impl<V> View for RadioView<V>
79+
where
80+
V: PartialEq + 'static,
81+
{
82+
type Args<'v> = Radio<'v, V>;
83+
type Response = bool;
84+
85+
fn create(args: Self::Args<'_>) -> Self {
86+
Self {
87+
label: args.label,
88+
class: args.class,
89+
_marker: PhantomData,
90+
}
91+
}
5992

60-
fn show(self, ui: &crate::view::Ui) -> Self::Output {
93+
fn update(&mut self, args: Self::Args<'_>, ui: &Ui) -> Self::Response {
6194
let resp = ui
6295
.mouse_area(|ui| {
6396
let style = match self.class {
6497
StyleKind::Deferred(style) => {
65-
(style)(&ui.palette(), self.value == *self.existing)
98+
(style)(&ui.palette(), args.value == *args.existing)
6699
}
67100
StyleKind::Direct(style) => style,
68101
};
69102

70103
let hovered = ui.is_hovered();
71-
let fill = match (hovered, self.value == *self.existing) {
104+
let fill = match (hovered, args.value == *args.existing) {
72105
(false, true) => style.selected_background,
73106
(false, false) => style.background,
74107
(true, true) => style
@@ -84,15 +117,16 @@ where
84117
};
85118

86119
ui.background(fill, |ui| {
87-
ui.show(label(self.label).style(LabelStyle { foreground }))
120+
ui.show(label(&self.label).style(LabelStyle { foreground }))
88121
});
89122
})
90123
.flatten_left();
91124

92-
if resp.clicked() {
93-
*self.existing = self.value;
125+
let clicked = resp.clicked();
126+
if clicked {
127+
*args.existing = args.value;
94128
}
95-
resp.map(|c| c.clicked())
129+
clicked
96130
}
97131
}
98132

0 commit comments

Comments
 (0)