Skip to content

remove Adhoc trait, they can all be implemented via View #44

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

Merged
merged 1 commit into from
Nov 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 0 additions & 100 deletions src/view/adhoc.rs

This file was deleted.

3 changes: 0 additions & 3 deletions src/view/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@ pub use style::{Elements, Palette, StyleKind};

mod internal_views;

mod adhoc;
pub use adhoc::Adhoc;

mod builder;
pub use builder::{Builder, View, ViewExt};

Expand Down
19 changes: 6 additions & 13 deletions src/view/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::{
use super::{
filter::{Filter, Filterable},
input::InputState,
internal_views, Adhoc, Builder, LayoutNodes, Palette, Response, State, View, ViewId, ViewNodes,
internal_views, Builder, LayoutNodes, Palette, Response, State, View, ViewId, ViewNodes,
};

impl<'a> Filterable for Ui<'a> {
Expand Down Expand Up @@ -48,13 +48,6 @@ impl<'a> Ui<'a> {
}

impl<'a> Ui<'a> {
pub fn adhoc<'v, A>(&self, view: A) -> A::Output
where
A: Adhoc<'v>,
{
view.show(self)
}

pub fn show<'v, B>(&self, args: B) -> Response<<B::View as View>::Response>
where
B: Builder<'v>,
Expand Down Expand Up @@ -363,22 +356,22 @@ impl<'a> Ui<'a> {
}

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

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

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

pub fn radio<V>(&self, value: V, existing: &mut V, label: impl Into<Str>) -> Response<bool>
where
V: PartialEq,
V: PartialEq + 'static,
{
self.adhoc(views::radio(value, existing, label))
self.show(views::radio(value, existing, label))
}

pub fn label(&self, data: impl Into<Str>) -> Response {
Expand Down
39 changes: 28 additions & 11 deletions src/views/checkbox.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use crate::{
renderer::Rgba,
view::{Adhoc, Palette, Response, StyleKind, Ui},
view::{Builder, Palette, StyleKind, Ui, View},
Str,
};

use super::label::LabelStyle;

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

#[derive(Debug)]
#[derive(Debug, Copy, Clone)]
pub struct CheckboxStyle {
pub checked: &'static str,
pub unchecked: &'static str,
Expand Down Expand Up @@ -55,15 +55,32 @@ impl<'a> Checkbox<'a> {
}
}

impl<'a> Adhoc<'a> for Checkbox<'a> {
// TODO make this an opaque response
type Output = Response<bool>;
impl<'v> Builder<'v> for Checkbox<'v> {
type View = CheckboxView;
}

#[derive(Debug)]
pub struct CheckboxView {
label: Str,
class: StyleKind<CheckboxClass, CheckboxStyle>,
}

impl View for CheckboxView {
type Args<'v> = Checkbox<'v>;
type Response = bool;

fn create(args: Self::Args<'_>) -> Self {
Self {
label: args.label,
class: args.class,
}
}

fn show(self, ui: &Ui) -> Self::Output {
fn update(&mut self, args: Self::Args<'_>, ui: &Ui) -> Self::Response {
let resp = ui
.mouse_area(|ui| {
let style = match self.class {
StyleKind::Deferred(style) => (style)(&ui.palette(), *self.value),
StyleKind::Deferred(style) => (style)(&ui.palette(), *args.value),
StyleKind::Direct(style) => style,
};

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

ui.horizontal(|ui| {
let marker = if *self.value {
let marker = if *args.value {
style.checked
} else {
style.unchecked
};
ui.label(marker);
ui.show(super::label(self.label).style(LabelStyle { foreground }));
ui.show(super::label(&self.label).style(LabelStyle { foreground }));
});
})
.flatten_left();

*self.value ^= resp.clicked();
resp.map(|c| c.clicked())
*args.value ^= resp.clicked();
resp.clicked()
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/views/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ pub use button::{button, Button, ButtonClass, ButtonResponse, ButtonStyle};
mod checkbox;
pub use checkbox::{checkbox, Checkbox, CheckboxClass, CheckboxStyle};

mod collapsible;
pub use collapsible::{collapsible, Collapsible, CollapsibleClass, CollapsibleStyle};
// mod collapsible;
// pub use collapsible::{collapsible, Collapsible, CollapsibleClass, CollapsibleStyle};

mod constrain;
pub use constrain::{Constrain, Unconstrained};
Expand Down
58 changes: 46 additions & 12 deletions src/views/radio.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
use std::marker::PhantomData;

use crate::{
renderer::Rgba,
view::{Adhoc, Palette, Response, StyleKind},
view::{Builder, Palette, StyleKind, Ui, View},
Str,
};

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

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

#[derive(Debug)]
#[derive(Debug, Copy, Clone)]
pub struct RadioStyle {
pub selected: Option<&'static str>,
pub unselected: Option<&'static str>,
Expand Down Expand Up @@ -51,24 +53,55 @@ pub struct Radio<'a, V> {
class: StyleKind<RadioClass, RadioStyle>,
}

impl<'v, V> Adhoc<'v> for Radio<'v, V>
impl<'v, V: PartialEq + 'static> Builder<'v> for Radio<'v, V> {
type View = RadioView<V>;
}

pub struct RadioView<V>
where
V: PartialEq,
V: PartialEq + 'static,
{
type Output = Response<bool>;
label: Str,
class: StyleKind<RadioClass, RadioStyle>,
_marker: std::marker::PhantomData<V>,
}

impl<V: PartialEq> std::fmt::Debug for RadioView<V> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("RadioView")
.field("label", &self.label)
.field("class", &self.class)
.finish()
}
}

impl<V> View for RadioView<V>
where
V: PartialEq + 'static,
{
type Args<'v> = Radio<'v, V>;
type Response = bool;

fn create(args: Self::Args<'_>) -> Self {
Self {
label: args.label,
class: args.class,
_marker: PhantomData,
}
}

fn show(self, ui: &crate::view::Ui) -> Self::Output {
fn update(&mut self, args: Self::Args<'_>, ui: &Ui) -> Self::Response {
let resp = ui
.mouse_area(|ui| {
let style = match self.class {
StyleKind::Deferred(style) => {
(style)(&ui.palette(), self.value == *self.existing)
(style)(&ui.palette(), args.value == *args.existing)
}
StyleKind::Direct(style) => style,
};

let hovered = ui.is_hovered();
let fill = match (hovered, self.value == *self.existing) {
let fill = match (hovered, args.value == *args.existing) {
(false, true) => style.selected_background,
(false, false) => style.background,
(true, true) => style
Expand All @@ -84,15 +117,16 @@ where
};

ui.background(fill, |ui| {
ui.show(label(self.label).style(LabelStyle { foreground }))
ui.show(label(&self.label).style(LabelStyle { foreground }))
});
})
.flatten_left();

if resp.clicked() {
*self.existing = self.value;
let clicked = resp.clicked();
if clicked {
*args.existing = args.value;
}
resp.map(|c| c.clicked())
clicked
}
}

Expand Down
Loading