mirror of
https://github.com/trezor/trezor-firmware.git
synced 2025-07-05 14:22:33 +00:00
WIP - replace painter by jpeg and bar components
This commit is contained in:
parent
472a8bebd2
commit
1be00d543c
62
core/embed/rust/src/ui/component/bar.rs
Normal file
62
core/embed/rust/src/ui/component/bar.rs
Normal file
@ -0,0 +1,62 @@
|
|||||||
|
use crate::ui::{
|
||||||
|
component::{Component, Event, EventCtx, Never},
|
||||||
|
display,
|
||||||
|
display::Color,
|
||||||
|
geometry::Rect,
|
||||||
|
shape,
|
||||||
|
shape::Renderer,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub struct Bar {
|
||||||
|
area: Rect,
|
||||||
|
color: Color,
|
||||||
|
bg_color: Color,
|
||||||
|
radius: i16,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Bar {
|
||||||
|
pub fn new(color: Color, bg_color: Color, radius: i16) -> Self {
|
||||||
|
Self {
|
||||||
|
area: Rect::zero(),
|
||||||
|
color,
|
||||||
|
bg_color,
|
||||||
|
radius,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Component for Bar {
|
||||||
|
type Msg = Never;
|
||||||
|
|
||||||
|
fn place(&mut self, bounds: Rect) -> Rect {
|
||||||
|
self.area = bounds;
|
||||||
|
self.area
|
||||||
|
}
|
||||||
|
|
||||||
|
fn event(&mut self, _ctx: &mut EventCtx, _event: Event) -> Option<Self::Msg> {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|
||||||
|
fn paint(&mut self) {
|
||||||
|
display::rect_fill_rounded(self.area, self.color, self.bg_color, self.radius as u8);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn render<'s>(&'s self, target: &mut impl Renderer<'s>) {
|
||||||
|
shape::Bar::new(self.area)
|
||||||
|
.with_bg(self.color)
|
||||||
|
.with_radius(self.radius)
|
||||||
|
.render(target);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "ui_bounds")]
|
||||||
|
fn bounds(&self, sink: &mut dyn FnMut(Rect)) {
|
||||||
|
sink(self.area)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "ui_debug")]
|
||||||
|
impl crate::trace::Trace for Bar {
|
||||||
|
fn trace(&self, t: &mut dyn crate::trace::Tracer) {
|
||||||
|
t.component("Bar");
|
||||||
|
}
|
||||||
|
}
|
96
core/embed/rust/src/ui/component/jpeg.rs
Normal file
96
core/embed/rust/src/ui/component/jpeg.rs
Normal file
@ -0,0 +1,96 @@
|
|||||||
|
use crate::error::Error;
|
||||||
|
use crate::micropython::{buffer::get_buffer, obj::Obj};
|
||||||
|
|
||||||
|
use crate::ui::{
|
||||||
|
component::{Component, Event, EventCtx, Never},
|
||||||
|
display,
|
||||||
|
geometry::{Alignment2D, Offset, Rect},
|
||||||
|
shape,
|
||||||
|
shape::Renderer,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub enum ImageBuffer {
|
||||||
|
Object { obj: Obj },
|
||||||
|
Slice { data: &'static [u8] },
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ImageBuffer {
|
||||||
|
pub fn from_object(obj: Obj) -> Result<Self, Error> {
|
||||||
|
if !obj.is_bytes() {
|
||||||
|
return Err(Error::TypeError);
|
||||||
|
}
|
||||||
|
Ok(ImageBuffer::Object { obj })
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn from_slice(data: &'static [u8]) -> Self {
|
||||||
|
ImageBuffer::Slice { data }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn is_empty(&self) -> bool {
|
||||||
|
self.data().is_empty()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn data(&self) -> &[u8] {
|
||||||
|
match self {
|
||||||
|
// SAFETY: We expect no existing mutable reference. Resulting reference is
|
||||||
|
// discarded before returning to micropython.
|
||||||
|
ImageBuffer::Object { obj } => unsafe { unwrap!(get_buffer(*obj)) },
|
||||||
|
ImageBuffer::Slice { data } => data,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Jpeg {
|
||||||
|
area: Rect,
|
||||||
|
data: ImageBuffer,
|
||||||
|
scale: u8,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Jpeg {
|
||||||
|
pub fn new(data: ImageBuffer, scale: u8) -> Self {
|
||||||
|
Self {
|
||||||
|
area: Rect::zero(),
|
||||||
|
data,
|
||||||
|
scale,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Component for Jpeg {
|
||||||
|
type Msg = Never;
|
||||||
|
|
||||||
|
fn place(&mut self, bounds: Rect) -> Rect {
|
||||||
|
self.area = bounds;
|
||||||
|
self.area
|
||||||
|
}
|
||||||
|
|
||||||
|
fn event(&mut self, _ctx: &mut EventCtx, _event: Event) -> Option<Self::Msg> {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|
||||||
|
fn paint(&mut self) {
|
||||||
|
if let Some((size, _)) = display::tjpgd::jpeg_info(self.data.data()) {
|
||||||
|
let off = Offset::new(size.x / (2 << self.scale), size.y / (2 << self.scale));
|
||||||
|
display::tjpgd::jpeg(self.data.data(), self.area.center() - off, self.scale);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn render<'s>(&'s self, target: &mut impl Renderer<'s>) {
|
||||||
|
shape::JpegImage::new(self.area.center(), self.data.data())
|
||||||
|
.with_align(Alignment2D::CENTER)
|
||||||
|
.with_scale(self.scale)
|
||||||
|
.render(target);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "ui_bounds")]
|
||||||
|
fn bounds(&self, sink: &mut dyn FnMut(Rect)) {
|
||||||
|
sink(self.area)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "ui_debug")]
|
||||||
|
impl crate::trace::Trace for Jpeg {
|
||||||
|
fn trace(&self, t: &mut dyn crate::trace::Tracer) {
|
||||||
|
t.component("Jpeg");
|
||||||
|
}
|
||||||
|
}
|
@ -1,32 +1,36 @@
|
|||||||
#![forbid(unsafe_code)]
|
//#![forbid(unsafe_code)]
|
||||||
|
|
||||||
|
pub mod bar;
|
||||||
pub mod base;
|
pub mod base;
|
||||||
pub mod border;
|
pub mod border;
|
||||||
pub mod connect;
|
pub mod connect;
|
||||||
pub mod empty;
|
pub mod empty;
|
||||||
pub mod image;
|
pub mod image;
|
||||||
|
#[cfg(all(feature = "jpeg", feature = "micropython"))]
|
||||||
|
pub mod jpeg;
|
||||||
pub mod label;
|
pub mod label;
|
||||||
pub mod map;
|
pub mod map;
|
||||||
pub mod marquee;
|
pub mod marquee;
|
||||||
pub mod maybe;
|
pub mod maybe;
|
||||||
pub mod pad;
|
pub mod pad;
|
||||||
pub mod paginated;
|
pub mod paginated;
|
||||||
pub mod painter;
|
|
||||||
pub mod placed;
|
pub mod placed;
|
||||||
pub mod qr_code;
|
pub mod qr_code;
|
||||||
pub mod text;
|
pub mod text;
|
||||||
pub mod timeout;
|
pub mod timeout;
|
||||||
|
|
||||||
|
pub use bar::Bar;
|
||||||
pub use base::{Child, Component, ComponentExt, Event, EventCtx, Never, Root, TimerToken};
|
pub use base::{Child, Component, ComponentExt, Event, EventCtx, Never, Root, TimerToken};
|
||||||
pub use border::Border;
|
pub use border::Border;
|
||||||
pub use empty::Empty;
|
pub use empty::Empty;
|
||||||
|
#[cfg(all(feature = "jpeg", feature = "micropython"))]
|
||||||
|
pub use jpeg::Jpeg;
|
||||||
pub use label::Label;
|
pub use label::Label;
|
||||||
pub use map::MsgMap;
|
pub use map::MsgMap;
|
||||||
pub use marquee::Marquee;
|
pub use marquee::Marquee;
|
||||||
pub use maybe::Maybe;
|
pub use maybe::Maybe;
|
||||||
pub use pad::Pad;
|
pub use pad::Pad;
|
||||||
pub use paginated::{PageMsg, Paginate};
|
pub use paginated::{PageMsg, Paginate};
|
||||||
pub use painter::Painter;
|
|
||||||
pub use placed::{FixedHeightBar, Floating, GridPlaced, Split};
|
pub use placed::{FixedHeightBar, Floating, GridPlaced, Split};
|
||||||
pub use qr_code::Qr;
|
pub use qr_code::Qr;
|
||||||
pub use text::{
|
pub use text::{
|
||||||
|
@ -1,88 +0,0 @@
|
|||||||
#[cfg(feature = "jpeg")]
|
|
||||||
use crate::ui::geometry::Offset;
|
|
||||||
use crate::ui::{
|
|
||||||
component::{image::Image, Component, Event, EventCtx, Never},
|
|
||||||
display,
|
|
||||||
geometry::{Alignment2D, Rect},
|
|
||||||
shape,
|
|
||||||
shape::Renderer,
|
|
||||||
};
|
|
||||||
|
|
||||||
pub struct Painter<F> {
|
|
||||||
area: Rect,
|
|
||||||
func: F,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<F> Painter<F> {
|
|
||||||
pub fn new(func: F) -> Self {
|
|
||||||
Self {
|
|
||||||
func,
|
|
||||||
area: Rect::zero(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<F> Component for Painter<F>
|
|
||||||
where
|
|
||||||
F: FnMut(Rect),
|
|
||||||
{
|
|
||||||
type Msg = Never;
|
|
||||||
|
|
||||||
fn place(&mut self, bounds: Rect) -> Rect {
|
|
||||||
self.area = bounds;
|
|
||||||
self.area
|
|
||||||
}
|
|
||||||
|
|
||||||
fn event(&mut self, _ctx: &mut EventCtx, _event: Event) -> Option<Self::Msg> {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
|
|
||||||
fn paint(&mut self) {
|
|
||||||
(self.func)(self.area);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn render<'s>(&'s self, target: &mut impl Renderer<'s>) {
|
|
||||||
let area = self.area;
|
|
||||||
shape::Bar::new(area)
|
|
||||||
.with_thickness(1)
|
|
||||||
.with_fg(display::Color::white())
|
|
||||||
.render(target);
|
|
||||||
shape::Text::new(area.top_left(), "Paint").render(target); // !@# replace
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(feature = "ui_bounds")]
|
|
||||||
fn bounds(&self, sink: &mut dyn FnMut(Rect)) {
|
|
||||||
sink(self.area)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(feature = "ui_debug")]
|
|
||||||
impl<F> crate::trace::Trace for Painter<F> {
|
|
||||||
fn trace(&self, t: &mut dyn crate::trace::Tracer) {
|
|
||||||
t.component("Painter");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn image_painter(image: Image) -> Painter<impl FnMut(Rect)> {
|
|
||||||
let f = move |area: Rect| image.draw(area.center(), Alignment2D::CENTER);
|
|
||||||
Painter::new(f)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(feature = "jpeg")]
|
|
||||||
pub fn jpeg_painter<'a>(
|
|
||||||
image: impl Fn() -> &'a [u8],
|
|
||||||
size: Offset,
|
|
||||||
scale: u8,
|
|
||||||
) -> Painter<impl FnMut(Rect)> {
|
|
||||||
let off = Offset::new(size.x / (2 << scale), size.y / (2 << scale));
|
|
||||||
#[cfg(not(feature = "new_rendering"))]
|
|
||||||
let f = move |area: Rect| display::tjpgd::jpeg(image(), area.center() - off, scale);
|
|
||||||
#[cfg(feature = "new_rendering")]
|
|
||||||
let f = move |area: Rect| {};
|
|
||||||
Painter::new(f)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn rect_painter(fg: display::Color, bg: display::Color) -> Painter<impl FnMut(Rect)> {
|
|
||||||
let f = move |area: Rect| display::rect_fill_rounded(area, fg, bg, 2);
|
|
||||||
Painter::new(f)
|
|
||||||
}
|
|
@ -8,8 +8,7 @@ use crate::{
|
|||||||
ui::{
|
ui::{
|
||||||
canvas::algo::PI4,
|
canvas::algo::PI4,
|
||||||
component::{
|
component::{
|
||||||
base::Never, painter, Child, Component, ComponentExt, Empty, Event, EventCtx, Label,
|
base::Never, Bar, Child, Component, ComponentExt, Empty, Event, EventCtx, Label, Split,
|
||||||
Split,
|
|
||||||
},
|
},
|
||||||
constant,
|
constant,
|
||||||
display::loader::{loader_circular_uncompress, LoaderDimensions},
|
display::loader::{loader_circular_uncompress, LoaderDimensions},
|
||||||
@ -55,7 +54,7 @@ where
|
|||||||
style,
|
style,
|
||||||
)
|
)
|
||||||
.vertically_centered();
|
.vertically_centered();
|
||||||
let bg = painter::rect_painter(style.background_color, theme::BG);
|
let bg = Bar::new(style.background_color, theme::BG, 2);
|
||||||
let inner = (bg, label);
|
let inner = (bg, label);
|
||||||
CoinJoinProgress::with_background(text, inner, indeterminate)
|
CoinJoinProgress::with_background(text, inner, indeterminate)
|
||||||
}
|
}
|
||||||
|
@ -21,8 +21,8 @@ use crate::{
|
|||||||
base::ComponentExt,
|
base::ComponentExt,
|
||||||
connect::Connect,
|
connect::Connect,
|
||||||
image::BlendedImage,
|
image::BlendedImage,
|
||||||
|
jpeg::{ImageBuffer, Jpeg},
|
||||||
paginated::{PageMsg, Paginate},
|
paginated::{PageMsg, Paginate},
|
||||||
painter,
|
|
||||||
placed::GridPlaced,
|
placed::GridPlaced,
|
||||||
text::{
|
text::{
|
||||||
op::OpTextLayout,
|
op::OpTextLayout,
|
||||||
@ -211,10 +211,7 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<F> ComponentMsgObj for painter::Painter<F>
|
impl ComponentMsgObj for Jpeg {
|
||||||
where
|
|
||||||
F: FnMut(geometry::Rect),
|
|
||||||
{
|
|
||||||
fn msg_try_into_obj(&self, _msg: Self::Msg) -> Result<Obj, Error> {
|
fn msg_try_into_obj(&self, _msg: Self::Msg) -> Result<Obj, Error> {
|
||||||
unreachable!()
|
unreachable!()
|
||||||
}
|
}
|
||||||
@ -648,26 +645,18 @@ extern "C" fn new_confirm_properties(n_args: usize, args: *const Obj, kwargs: *m
|
|||||||
extern "C" fn new_confirm_homescreen(n_args: usize, args: *const Obj, kwargs: *mut Map) -> Obj {
|
extern "C" fn new_confirm_homescreen(n_args: usize, args: *const Obj, kwargs: *mut Map) -> Obj {
|
||||||
let block = move |_args: &[Obj], kwargs: &Map| {
|
let block = move |_args: &[Obj], kwargs: &Map| {
|
||||||
let title: StrBuffer = kwargs.get(Qstr::MP_QSTR_title)?.try_into()?;
|
let title: StrBuffer = kwargs.get(Qstr::MP_QSTR_title)?.try_into()?;
|
||||||
let data: Obj = kwargs.get(Qstr::MP_QSTR_image)?;
|
let image: Obj = kwargs.get(Qstr::MP_QSTR_image)?;
|
||||||
|
|
||||||
// Layout needs to hold the Obj to play nice with GC. Obj is resolved to &[u8]
|
let mut jpeg = unwrap!(ImageBuffer::from_object(image));
|
||||||
// in every paint pass.
|
|
||||||
let buffer_func = move || {
|
|
||||||
// SAFETY: We expect no existing mutable reference. Resulting reference is
|
|
||||||
// discarded before returning to micropython.
|
|
||||||
let buffer = unsafe { unwrap!(get_buffer(data)) };
|
|
||||||
// Incoming data may be empty, meaning we should display default homescreen
|
|
||||||
// image.
|
|
||||||
if buffer.is_empty() {
|
|
||||||
theme::IMAGE_HOMESCREEN
|
|
||||||
} else {
|
|
||||||
buffer
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
let size = match jpeg_info(buffer_func()) {
|
if jpeg.is_empty() {
|
||||||
Some(info) => info.0,
|
// Incoming data may be empty, meaning we should
|
||||||
_ => return Err(value_error!("Invalid image.")),
|
// display default homescreen image.
|
||||||
|
jpeg = ImageBuffer::from_slice(theme::IMAGE_HOMESCREEN);
|
||||||
|
}
|
||||||
|
|
||||||
|
if let None = jpeg_info(jpeg.data()) {
|
||||||
|
return Err(value_error!("Invalid image."));
|
||||||
};
|
};
|
||||||
|
|
||||||
let tr_change: StrBuffer = TR::buttons__change.try_into()?;
|
let tr_change: StrBuffer = TR::buttons__change.try_into()?;
|
||||||
@ -675,7 +664,7 @@ extern "C" fn new_confirm_homescreen(n_args: usize, args: *const Obj, kwargs: *m
|
|||||||
let obj = LayoutObj::new(Frame::centered(
|
let obj = LayoutObj::new(Frame::centered(
|
||||||
theme::label_title(),
|
theme::label_title(),
|
||||||
title,
|
title,
|
||||||
Dialog::new(painter::jpeg_painter(buffer_func, size, 1), buttons),
|
Dialog::new(Jpeg::new(jpeg, 1), buttons),
|
||||||
))?;
|
))?;
|
||||||
Ok(obj.into())
|
Ok(obj.into())
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user