mirror of
https://github.com/trezor/trezor-firmware.git
synced 2024-11-13 19:18:56 +00:00
WIP - replace painter by jpeg and bar components
This commit is contained in:
parent
03457b5d45
commit
4b01309216
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");
|
||||
}
|
||||
}
|
98
core/embed/rust/src/ui/component/jpeg.rs
Normal file
98
core/embed/rust/src/ui/component/jpeg.rs
Normal file
@ -0,0 +1,98 @@
|
||||
use crate::{
|
||||
error::Error,
|
||||
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 border;
|
||||
pub mod connect;
|
||||
pub mod empty;
|
||||
pub mod image;
|
||||
#[cfg(all(feature = "jpeg", feature = "micropython"))]
|
||||
pub mod jpeg;
|
||||
pub mod label;
|
||||
pub mod map;
|
||||
pub mod marquee;
|
||||
pub mod maybe;
|
||||
pub mod pad;
|
||||
pub mod paginated;
|
||||
pub mod painter;
|
||||
pub mod placed;
|
||||
pub mod qr_code;
|
||||
pub mod text;
|
||||
pub mod timeout;
|
||||
|
||||
pub use bar::Bar;
|
||||
pub use base::{Child, Component, ComponentExt, Event, EventCtx, Never, Root, TimerToken};
|
||||
pub use border::Border;
|
||||
pub use empty::Empty;
|
||||
#[cfg(all(feature = "jpeg", feature = "micropython"))]
|
||||
pub use jpeg::Jpeg;
|
||||
pub use label::Label;
|
||||
pub use map::MsgMap;
|
||||
pub use marquee::Marquee;
|
||||
pub use maybe::Maybe;
|
||||
pub use pad::Pad;
|
||||
pub use paginated::{PageMsg, Paginate};
|
||||
pub use painter::Painter;
|
||||
pub use placed::{FixedHeightBar, Floating, GridPlaced, Split};
|
||||
pub use qr_code::Qr;
|
||||
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)
|
||||
}
|
@ -1,4 +1,5 @@
|
||||
use crate::{
|
||||
micropython::{buffer::get_buffer, gc::Gc, obj::Obj},
|
||||
strutil::TString,
|
||||
translations::TR,
|
||||
trezorhal::usb::usb_configured,
|
||||
|
@ -7,8 +7,7 @@ use crate::{
|
||||
translations::TR,
|
||||
ui::{
|
||||
component::{
|
||||
base::Never, painter, Child, Component, ComponentExt, Empty, Event, EventCtx, Label,
|
||||
Split,
|
||||
base::Never, Bar, Child, Component, ComponentExt, Empty, Event, EventCtx, Label, Split,
|
||||
},
|
||||
constant,
|
||||
display::loader::{loader_circular_uncompress, LoaderDimensions},
|
||||
@ -45,7 +44,7 @@ impl<U> CoinJoinProgress<U> {
|
||||
let style = theme::label_coinjoin_progress();
|
||||
let label = Label::centered(TR::coinjoin__title_do_not_disconnect.into(), style)
|
||||
.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);
|
||||
CoinJoinProgress::with_background(text, inner, indeterminate)
|
||||
}
|
||||
|
@ -14,8 +14,8 @@ use crate::{
|
||||
base::ComponentExt,
|
||||
connect::Connect,
|
||||
image::BlendedImage,
|
||||
jpeg::{ImageBuffer, Jpeg},
|
||||
paginated::{PageMsg, Paginate},
|
||||
painter,
|
||||
placed::GridPlaced,
|
||||
text::{
|
||||
op::OpTextLayout,
|
||||
@ -196,10 +196,7 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<F> ComponentMsgObj for painter::Painter<F>
|
||||
where
|
||||
F: FnMut(geometry::Rect),
|
||||
{
|
||||
impl ComponentMsgObj for Jpeg {
|
||||
fn msg_try_into_obj(&self, _msg: Self::Msg) -> Result<Obj, Error> {
|
||||
unreachable!()
|
||||
}
|
||||
@ -617,33 +614,25 @@ 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 {
|
||||
let block = move |_args: &[Obj], kwargs: &Map| {
|
||||
let title: TString = 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]
|
||||
// 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 mut jpeg = unwrap!(ImageBuffer::from_object(image));
|
||||
|
||||
let size = match jpeg_info(buffer_func()) {
|
||||
Some(info) => info.0,
|
||||
_ => return Err(value_error!("Invalid image.")),
|
||||
if jpeg.is_empty() {
|
||||
// Incoming data may be empty, meaning we should
|
||||
// display default homescreen image.
|
||||
jpeg = ImageBuffer::from_slice(theme::IMAGE_HOMESCREEN);
|
||||
}
|
||||
|
||||
if jpeg_info(jpeg.data()).is_none() {
|
||||
return Err(value_error!("Invalid image."));
|
||||
};
|
||||
|
||||
let buttons = Button::cancel_confirm_text(None, Some(TR::buttons__change.into()));
|
||||
let obj = LayoutObj::new(Frame::centered(
|
||||
theme::label_title(),
|
||||
title,
|
||||
Dialog::new(painter::jpeg_painter(buffer_func, size, 1), buttons),
|
||||
Dialog::new(Jpeg::new(jpeg, 1), buttons),
|
||||
))?;
|
||||
Ok(obj.into())
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user