rustphone/src/apps/dial.rs

105 lines
2.4 KiB
Rust

use crate::{
apps::App,
display::Display,
keypad::{Key, KeyEvent, KeyEventType, KeyEvents, KeyInputMode},
state::{ModeState, State},
Context,
};
use arrayvec::ArrayString;
use embedded_graphics::{
mono_font::{
ascii::{FONT_10X20, FONT_6X10, FONT_9X15},
MonoTextStyleBuilder,
},
pixelcolor::BinaryColor,
prelude::*,
primitives::{Line, PrimitiveStyle, PrimitiveStyleBuilder, Rectangle, StrokeAlignment},
text::{Alignment, Text},
};
use embedded_text::{
alignment::*,
style::{HeightMode, TextBoxStyleBuilder},
TextBox,
};
pub struct Dial;
pub struct DialState {
pub line: ArrayString<40>,
pub line_changed: bool,
}
impl App for Dial {
type AppModeState = DialState;
fn on_enter(ctx: &mut Context, mode_state: &mut DialState) {
ctx.state.key_label_left.clear();
ctx.state.key_label_left.push_str("Message");
ctx.state.key_label_enter.clear();
ctx.state.key_label_enter.push_str("Save");
ctx.state.key_label_right.clear();
ctx.state.key_label_right.push_str("Delete");
ctx.key_labels_change = true;
}
fn update(ctx: &mut Context, mode_state: &mut DialState) -> Option<ModeState> {
// TODO move to init
let dial_text_style = MonoTextStyleBuilder::new()
.font(&FONT_10X20)
.text_color(BinaryColor::On)
.background_color(BinaryColor::Off)
.build();
let textbox_style = TextBoxStyleBuilder::new()
.height_mode(HeightMode::FitToText)
.alignment(HorizontalAlignment::Left)
.paragraph_spacing(6)
.build();
for key_event in &ctx.key_events {
if key_event.event_type != KeyEventType::Pressed {
continue;
}
match key_event.key {
Key::HangUp => {
return Some(ModeState::Clock(Default::default()));
}
Key::PickUp => {
// TODO
}
Key::OptionRight => {
if mode_state.line.pop().is_some() {
mode_state.line_changed = true;
} else {
return Some(ModeState::Clock(Default::default()));
}
}
_ => {
if let Some(ch) = key_event.get_char(KeyInputMode::Digit) {
if mode_state.line.try_push(ch).is_err() {
// TODO
} else {
mode_state.line_changed = true;
}
}
}
}
}
if mode_state.line_changed {
let bounds = Rectangle::new(Point::new(0, 16), Size::new(200, 0));
let text_box = TextBox::with_textbox_style(
&mode_state.line,
bounds,
dial_text_style,
textbox_style,
);
text_box.draw(ctx.display.inner_mut()).unwrap();
}
None
}
fn on_leave(ctx: &mut Context, mode_state: &mut DialState) {}
}