Skip to content
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

Use nightly feature arbitrary_self_types #854

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
6 changes: 6 additions & 0 deletions masonry/src/core/widget_mut.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright 2018 the Xilem Authors and the Druid Authors
// SPDX-License-Identifier: Apache-2.0

use std::ops::Receiver;

use crate::core::{FromDynWidget, MutateCtx, Widget};
use crate::kurbo::Affine;

Expand Down Expand Up @@ -28,6 +30,10 @@ pub struct WidgetMut<'a, W: Widget + ?Sized> {
pub widget: &'a mut W,
}

impl<W: Widget + ?Sized> Receiver for WidgetMut<'_, W> {
type Target = W;
}

impl<W: Widget + ?Sized> Drop for WidgetMut<'_, W> {
fn drop(&mut self) {
// If this `WidgetMut` is a reborrow, a parent non-reborrow `WidgetMut`
Expand Down
1 change: 1 addition & 0 deletions masonry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@
reason = "Potentially controversial code style"
)]
#![expect(clippy::single_match, reason = "General policy not decided")]
#![feature(arbitrary_self_types)]

// TODO - Add logo

Expand Down
8 changes: 4 additions & 4 deletions masonry/src/widgets/button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,12 @@ impl Button {
// --- MARK: WIDGETMUT ---
impl Button {
/// Set the text.
pub fn set_text(this: &mut WidgetMut<'_, Self>, new_text: impl Into<ArcStr>) {
Label::set_text(&mut Self::label_mut(this), new_text);
pub fn set_text(self: &mut WidgetMut<'_, Self>, new_text: impl Into<ArcStr>) {
Label::set_text(&mut self.label_mut(), new_text);
}

pub fn label_mut<'t>(this: &'t mut WidgetMut<'_, Self>) -> WidgetMut<'t, Label> {
this.ctx.get_mut(&mut this.widget.label)
pub fn label_mut<'t>(self: &'t mut WidgetMut<'_, Self>) -> WidgetMut<'t, Label> {
self.ctx.get_mut(&mut self.widget.label)
}
}

Expand Down
29 changes: 13 additions & 16 deletions masonry/src/widgets/checkbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,21 @@ impl Checkbox {

// --- MARK: WIDGETMUT ---
impl Checkbox {
pub fn set_checked(this: &mut WidgetMut<'_, Self>, checked: bool) {
this.widget.checked = checked;
pub fn set_checked(self: &mut WidgetMut<'_, Self>, checked: bool) {
self.widget.checked = checked;
// Checked state impacts appearance and accessibility node
this.ctx.request_render();
self.ctx.request_render();
}

/// Set the text.
///
/// We enforce this to be an `ArcStr` to make the allocation explicit.
pub fn set_text(this: &mut WidgetMut<'_, Self>, new_text: ArcStr) {
Label::set_text(&mut Self::label_mut(this), new_text);
pub fn set_text(self: &mut WidgetMut<'_, Self>, new_text: ArcStr) {
Label::set_text(&mut self.label_mut(), new_text);
}

pub fn label_mut<'t>(this: &'t mut WidgetMut<'_, Self>) -> WidgetMut<'t, Label> {
this.ctx.get_mut(&mut this.widget.label)
pub fn label_mut<'t>(self: &'t mut WidgetMut<'_, Self>) -> WidgetMut<'t, Label> {
self.ctx.get_mut(&mut self.widget.label)
}
}

Expand Down Expand Up @@ -288,15 +288,12 @@ mod tests {

harness.edit_root_widget(|mut checkbox| {
let mut checkbox = checkbox.downcast::<Checkbox>();
Checkbox::set_checked(&mut checkbox, true);
Checkbox::set_text(
&mut checkbox,
ArcStr::from("The quick brown fox jumps over the lazy dog"),
);

let mut label = Checkbox::label_mut(&mut checkbox);
Label::set_brush(&mut label, PRIMARY_LIGHT);
Label::insert_style(&mut label, StyleProperty::FontSize(20.0));
checkbox.set_checked(true);
checkbox.set_text("The quick brown fox jumps over the lazy dog".into());

let mut label = checkbox.label_mut();
label.set_brush(PRIMARY_LIGHT);
label.insert_style(StyleProperty::FontSize(20.0));
});

harness.render()
Expand Down
Loading
Loading