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

Add more String views (&'static str, Cow<'static, str>) #144

Merged
merged 1 commit into from
Nov 9, 2023
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
2 changes: 1 addition & 1 deletion examples/hello.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fn app_logic(data: &mut i32) -> impl View<i32> {
*data += 1;
}),
h_stack((
String::from("Buttons: "),
"Buttons: ",
button("decrease", |data| {
println!("clicked decrease");
*data -= 1;
Expand Down
79 changes: 79 additions & 0 deletions src/view/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use std::borrow::Cow;

use crate::view::{Id, ViewMarker};
use crate::widget::ChangeFlags;

Expand All @@ -24,6 +26,83 @@ impl<T, A> View<T, A> for String {

type Element = crate::widget::TextWidget;

fn build(&self, cx: &mut Cx) -> (crate::view::Id, Self::State, Self::Element) {
let (id, element) =
cx.with_new_id(|_| crate::widget::TextWidget::new(Cow::from(self.clone())));
(id, (), element)
}

fn rebuild(
&self,
_cx: &mut Cx,
prev: &Self,
_id: &mut Id,
_state: &mut Self::State,
element: &mut Self::Element,
) -> ChangeFlags {
if prev != self {
element.set_text(Cow::from(self.clone()))
} else {
ChangeFlags::empty()
}
}

fn message(
&self,
_id_path: &[xilem_core::Id],
_state: &mut Self::State,
_message: Box<dyn std::any::Any>,
_app_state: &mut T,
) -> xilem_core::MessageResult<A> {
xilem_core::MessageResult::Nop
}
}

impl ViewMarker for &'static str {}

impl<T, A> View<T, A> for &'static str {
type State = ();

type Element = crate::widget::TextWidget;

fn build(&self, cx: &mut Cx) -> (crate::view::Id, Self::State, Self::Element) {
let (id, element) = cx.with_new_id(|_| crate::widget::TextWidget::new(Cow::from(*self)));
(id, (), element)
}

fn rebuild(
&self,
_cx: &mut Cx,
prev: &Self,
_id: &mut Id,
_state: &mut Self::State,
element: &mut Self::Element,
) -> ChangeFlags {
if prev != self {
element.set_text(Cow::from(*self))
} else {
ChangeFlags::empty()
}
}

fn message(
&self,
_id_path: &[xilem_core::Id],
_state: &mut Self::State,
_message: Box<dyn std::any::Any>,
_app_state: &mut T,
) -> xilem_core::MessageResult<A> {
xilem_core::MessageResult::Nop
}
}

impl ViewMarker for Cow<'static, str> {}

impl<T, A> View<T, A> for Cow<'static, str> {
type State = ();

type Element = crate::widget::TextWidget;

fn build(&self, cx: &mut Cx) -> (crate::view::Id, Self::State, Self::Element) {
let (id, element) = cx.with_new_id(|_| crate::widget::TextWidget::new(self.clone()));
(id, (), element)
Expand Down
7 changes: 4 additions & 3 deletions src/widget/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// limitations under the License.

use parley::{FontContext, Layout};
use std::borrow::Cow;
use vello::{
kurbo::{Affine, Size},
peniko::{Brush, Color},
Expand All @@ -27,16 +28,16 @@ use super::{
};

pub struct TextWidget {
text: String,
text: Cow<'static, str>,
layout: Option<Layout<ParleyBrush>>,
}

impl TextWidget {
pub fn new(text: String) -> TextWidget {
pub fn new(text: Cow<'static, str>) -> TextWidget {
TextWidget { text, layout: None }
}

pub fn set_text(&mut self, text: String) -> ChangeFlags {
pub fn set_text(&mut self, text: Cow<'static, str>) -> ChangeFlags {
self.text = text;
ChangeFlags::LAYOUT | ChangeFlags::PAINT
}
Expand Down