Skip to content

Commit

Permalink
chore(core): More style fixes in Rust UI
Browse files Browse the repository at this point in the history
  • Loading branch information
jpochyla committed Sep 22, 2021
1 parent 534f09f commit d4e9eee
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 22 deletions.
1 change: 1 addition & 0 deletions core/embed/rust/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use core::convert::{Infallible, TryInto};

use cstr_core::CStr;

use crate::micropython::{ffi, obj::Obj, qstr::Qstr};
Expand Down
2 changes: 1 addition & 1 deletion core/embed/rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ fn panic(_info: &core::panic::PanicInfo) -> ! {

// TODO: Ideally we would take the file and line info out of
// `PanicInfo::location()`.
trezorhal::common::fatal_error(&empty, &msg, &empty, 0, &empty);
trezorhal::common::fatal_error(empty, msg, empty, 0, empty);
}
2 changes: 1 addition & 1 deletion core/embed/rust/src/protobuf/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use cstr_core::CStr;

use crate::{error::Error, micropython::qstr::Qstr};

/* XXX const version of from_bytes_with_nul_unchecked is nightly-only */
// XXX const version of `from_bytes_with_nul_unchecked` is nightly-only.

pub fn experimental_not_enabled() -> Error {
let msg =
Expand Down
6 changes: 3 additions & 3 deletions core/embed/rust/src/ui/component/model_tt/dialog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use crate::ui::{

use super::button::{Button, ButtonMsg::Clicked};

pub enum DialogMsg<T: Component> {
Content(T::Msg),
pub enum DialogMsg<T> {
Content(T),
LeftClicked,
RightClicked,
}
Expand Down Expand Up @@ -40,7 +40,7 @@ impl<T: Component> Dialog<T> {
}

impl<T: Component> Component for Dialog<T> {
type Msg = DialogMsg<T>;
type Msg = DialogMsg<T::Msg>;

fn event(&mut self, ctx: &mut EventCtx, event: Event) -> Option<Self::Msg> {
if let Some(msg) = self.content.event(ctx, event) {
Expand Down
21 changes: 12 additions & 9 deletions core/embed/rust/src/ui/layout/example.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use core::convert::TryInto;
use core::convert::{TryFrom, TryInto};

use crate::{
error::Error,
micropython::{buffer::Buffer, obj::Obj},
ui::{
component::{
model_tt::{theme, Button, Dialog, DialogMsg, Text},
Child, Component, Never,
Child, Never,
},
display,
},
Expand All @@ -14,16 +15,18 @@ use crate::{

use super::obj::LayoutObj;

impl<T> From<DialogMsg<T>> for Obj
impl<T> TryFrom<DialogMsg<T>> for Obj
where
T: Component,
T::Msg: Into<Obj>,
Obj: TryFrom<T>,
Error: From<<T as TryInto<Obj>>::Error>,
{
fn from(val: DialogMsg<T>) -> Self {
type Error = Error;

fn try_from(val: DialogMsg<T>) -> Result<Self, Self::Error> {
match val {
DialogMsg::Content(c) => c.into(),
DialogMsg::LeftClicked => 1.try_into().unwrap(),
DialogMsg::RightClicked => 2.try_into().unwrap(),
DialogMsg::Content(c) => Ok(c.try_into()?),
DialogMsg::LeftClicked => 1.try_into(),
DialogMsg::RightClicked => 2.try_into(),
}
}
}
Expand Down
19 changes: 11 additions & 8 deletions core/embed/rust/src/ui/layout/obj.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,18 @@ use crate::{
/// wrapping trait that is implemented for all components where `Component::Msg`
/// can be converted to `Obj`.
pub trait ObjComponent {
fn obj_event(&mut self, ctx: &mut EventCtx, event: Event) -> Obj;
fn obj_event(&mut self, ctx: &mut EventCtx, event: Event) -> Result<Obj, Error>;
fn obj_paint(&mut self);
}

impl<T> ObjComponent for Child<T>
where
T: Component,
T::Msg: Into<Obj>,
T::Msg: TryInto<Obj, Error = Error>,
{
fn obj_event(&mut self, ctx: &mut EventCtx, event: Event) -> Obj {
fn obj_event(&mut self, ctx: &mut EventCtx, event: Event) -> Result<Obj, Error> {
self.event(ctx, event)
.map_or_else(Obj::const_none, Into::into)
.map_or_else(|| Ok(Obj::const_none()), TryInto::try_into)
}

fn obj_paint(&mut self) {
Expand Down Expand Up @@ -105,12 +105,15 @@ impl LayoutObj {
fn obj_event(&self, event: Event) -> Result<Obj, Error> {
let inner = &mut *self.inner.borrow_mut();

// Clear the upwards-propagating paint request flag from the last event pass.
inner.event_ctx.clear_paint_requests();

// Send the event down the component tree. Bail out in case of failure.
// SAFETY: `inner.root` is unique because of the `inner.borrow_mut()`.
let msg = unsafe { Gc::as_mut(&mut inner.root) }.obj_event(&mut inner.event_ctx, event);
let msg = unsafe { Gc::as_mut(&mut inner.root) }.obj_event(&mut inner.event_ctx, event)?;

// Clear the upwards-propagating paint request flag, all concerning `Child`
// wrappers should have already marked themselves for painting.
inner.event_ctx.clear_paint_requests();
// All concerning `Child` wrappers should have already marked themselves for
// painting by now, and we're prepared for a paint pass.

// Drain any pending timers into the callback.
while let Some((token, deadline)) = inner.event_ctx.pop_timer() {
Expand Down

0 comments on commit d4e9eee

Please sign in to comment.