clean up password modal box

This commit is contained in:
bunnie 2022-01-22 01:41:55 +08:00
parent 2642e6c075
commit 001b07ac74
3 changed files with 146 additions and 52 deletions

View file

@ -48,12 +48,13 @@ impl ActionApi for TextEntry {
auto-closes on enter
*/
if self.is_password {
glyph_height + 2*margin + glyph_height + 2*margin + 8 // 8 pixels extra margin because the emoji glyphs are oversized
glyph_height + 2*margin + glyph_height
} else {
glyph_height + 2*margin
}
}
fn redraw(&self, at_height: i16, modal: &Modal) {
const MAX_CHARS: usize = 33;
let color = if self.is_password {
PixelColor::Light
} else {
@ -69,25 +70,29 @@ impl ActionApi for TextEntry {
));
tv.ellipsis = true;
tv.invert = self.is_password;
tv.style = modal.style;
tv.style = if self.is_password {
GlyphStyle::Monospace
} else {
modal.style
};
tv.margin = Point::new(0, 0);
tv.draw_border = false;
tv.insertion = Some(self.action_payload.0.len() as i32);
tv.text.clear(); // make sure this is blank
let payload_chars = self.action_payload.0.as_str().unwrap().chars().count();
// TODO: condense the "above 20" chars length path a bit -- written out "the dumb way" just to reason out the logic a bit
// TODO: condense the "above MAX_CHARS" chars length path a bit -- written out "the dumb way" just to reason out the logic a bit
match self.visibility {
TextEntryVisibility::Visible => {
log::trace!("action payload: {}", self.action_payload.0.as_str().unwrap());
if payload_chars < 20 {
if payload_chars < MAX_CHARS {
write!(tv.text, "{}", self.action_payload.0.as_str().unwrap()).unwrap();
} else {
write!(tv.text, "...{}", &self.action_payload.0.as_str().unwrap()[payload_chars-18..]).unwrap();
write!(tv.text, "...{}", &self.action_payload.0.as_str().unwrap()[payload_chars-(MAX_CHARS - 3)..]).unwrap();
}
modal.gam.post_textview(&mut tv).expect("couldn't post textview");
},
TextEntryVisibility::Hidden => {
if payload_chars < 20 {
if payload_chars < MAX_CHARS {
for _char in self.action_payload.0.as_str().unwrap().chars() {
tv.text.push('*').expect("text field too long");
}
@ -96,14 +101,14 @@ impl ActionApi for TextEntry {
tv.text.push('.').unwrap();
tv.text.push('.').unwrap();
tv.text.push('.').unwrap();
for _ in 0..18 {
for _ in 0..(MAX_CHARS - 3) {
tv.text.push('*').expect("text field too long");
}
}
modal.gam.post_textview(&mut tv).expect("couldn't post textview");
},
TextEntryVisibility::LastChars => {
if payload_chars < 20 {
if payload_chars < MAX_CHARS {
let hide_to = if self.action_payload.0.as_str().unwrap().chars().count() >= 2 {
self.action_payload.0.as_str().unwrap().chars().count() - 2
} else {
@ -125,8 +130,8 @@ impl ActionApi for TextEntry {
} else {
0
};
for (index, ch) in self.action_payload.0.as_str().unwrap()[payload_chars-18..].chars().enumerate() {
if index + payload_chars-18 < hide_to {
for (index, ch) in self.action_payload.0.as_str().unwrap()[payload_chars-(MAX_CHARS - 3)..].chars().enumerate() {
if index + payload_chars-(MAX_CHARS - 3) < hide_to {
tv.text.push('*').expect("text field too long");
} else {
tv.text.push(ch).expect("text field too long");
@ -138,39 +143,105 @@ impl ActionApi for TextEntry {
}
if self.is_password {
// draw the visibility selection area
// "<👀🤫✴️>" coded explicitly. Pasting unicode into vscode yields extra cruft that we can't parse (e.g. skin tones and color mods).
let prompt = "\u{2b05} \u{1f440}\u{1f576}\u{26d4} \u{27a1}";
let select_index = match self.visibility {
TextEntryVisibility::Visible => 2,
TextEntryVisibility::LastChars => 3,
TextEntryVisibility::Hidden => 4,
};
let spacing = 38; // fixed width spacing for the array
let emoji_width = 36;
// center the prompt nicely, if possible
let left_edge = if modal.canvas_width > prompt.chars().count() as i16 * spacing {
(modal.canvas_width - prompt.chars().count() as i16 * spacing) / 2
if false {
// "<👀🤫✴️>" coded explicitly. Pasting unicode into vscode yields extra cruft that we can't parse (e.g. skin tones and color mods).
let prompt = "\u{2b05} \u{1f440}\u{1f576}\u{26d4} \u{27a1}";
let select_index = match self.visibility {
TextEntryVisibility::Visible => 2,
TextEntryVisibility::LastChars => 3,
TextEntryVisibility::Hidden => 4,
};
let spacing = 38; // fixed width spacing for the array
let emoji_width = 36;
// center the prompt nicely, if possible
let left_edge = if modal.canvas_width > prompt.chars().count() as i16 * spacing {
(modal.canvas_width - prompt.chars().count() as i16 * spacing) / 2
} else {
0
};
for (i, ch) in prompt.chars().enumerate() {
let mut tv = TextView::new(
modal.canvas,
TextBounds::BoundingBox(Rectangle::new(
Point::new(left_edge + i as i16 * spacing, at_height + modal.line_height + modal.margin * 4),
Point::new(left_edge + i as i16 * spacing + emoji_width, at_height + modal.line_height + 34 + modal.margin * 4))
));
tv.style = GlyphStyle::Large;
tv.margin = Point::new(0, 0);
tv.draw_border = false;
if i == select_index {
tv.invert = !self.is_password;
} else {
tv.invert = self.is_password;
}
tv.text.clear();
write!(tv.text, "{}", ch).unwrap();
log::trace!("tv.text: {} : {}/{}", i, tv.text, ch);
modal.gam.post_textview(&mut tv).expect("couldn't post textview");
}
} else {
0
};
for (i, ch) in prompt.chars().enumerate() {
let select_index = match self.visibility {
TextEntryVisibility::Visible => 0,
TextEntryVisibility::LastChars => 1,
TextEntryVisibility::Hidden => 2,
};
let prompt_width = glyph_to_height_hint(GlyphStyle::Monospace) as i16 * 4;
let lr_margin = (modal.canvas_width - prompt_width * 3) / 2;
let left_edge = lr_margin;
let mut tv = TextView::new(
modal.canvas,
TextBounds::BoundingBox(Rectangle::new(
Point::new(left_edge + i as i16 * spacing, at_height + modal.line_height + modal.margin * 4),
Point::new(left_edge + i as i16 * spacing + emoji_width, at_height + modal.line_height + 34 + modal.margin * 4))
));
tv.style = GlyphStyle::Regular;
TextBounds::GrowableFromTl(
Point::new(modal.margin, at_height + glyph_to_height_hint(GlyphStyle::Monospace) as i16 + modal.margin),
lr_margin as u16
));
tv.style = GlyphStyle::Large;
tv.margin = Point::new(0, 0);
tv.invert = self.is_password;
tv.draw_border = false;
if i == select_index {
tv.invert = !self.is_password;
} else {
tv.invert = self.is_password;
}
tv.text.clear();
write!(tv.text, "{}", ch).unwrap();
log::trace!("tv.text: {} : {}/{}", i, tv.text, ch);
write!(tv.text, "\u{2b05}").unwrap();
modal.gam.post_textview(&mut tv).expect("couldn't post textview");
for i in 0..3 {
let mut tv = TextView::new(
modal.canvas,
TextBounds::GrowableFromTl(
Point::new(left_edge + i * prompt_width, at_height + glyph_to_height_hint(GlyphStyle::Monospace) as i16 + modal.margin),
prompt_width as u16)
);
tv.style = GlyphStyle::Monospace;
tv.margin = Point::new(8, 8);
if i == select_index {
tv.invert = !self.is_password;
tv.draw_border = true;
tv.rounded_border = Some(6);
} else {
tv.invert = self.is_password;
tv.draw_border = false;
tv.rounded_border = None;
}
tv.text.clear();
match i {
0 => write!(tv.text, "abcd").unwrap(),
1 => write!(tv.text, "ab**").unwrap(),
_ => write!(tv.text, "****").unwrap(),
}
modal.gam.post_textview(&mut tv).expect("couldn't post textview");
}
let mut tv = TextView::new(
modal.canvas,
TextBounds::GrowableFromTr(
Point::new(modal.canvas_width - modal.margin, at_height + glyph_to_height_hint(GlyphStyle::Monospace) as i16 + modal.margin),
lr_margin as u16
));
tv.style = GlyphStyle::Large;
tv.margin = Point::new(0, 0);
tv.invert = self.is_password;
tv.draw_border = false;
tv.text.clear();
write!(tv.text, "\u{27a1} ").unwrap(); // minor bug - needs a trailing space on the right to make this render. it's an issue in the word wrapper.
modal.gam.post_textview(&mut tv).expect("couldn't post textview");
}
}

View file

@ -201,6 +201,29 @@ pub fn emoji_glyph(ch: char) -> Result<GlyphSprite, usize> {
}
}
pub fn emoji_large_glyph(ch: char) -> Result<GlyphSprite, usize> {
match emoji::CODEPOINTS.binary_search(&(ch as u32)) {
Ok(n) => {
let offset = n << 3;
let end = offset + 8;
match end <= emoji::GLYPHS.len() {
true => Ok(GlyphSprite {
glyph: &emoji::GLYPHS[offset..end],
wide: emoji::MAX_HEIGHT * 2, // yes, use height for wide
high: emoji::MAX_HEIGHT * 2,
kern: DEFAULT_KERN,
ch,
invert: false,
insert: false,
double: true,
}),
false => Err(0),
}
}
_ => Err(1),
}
}
pub fn zh_glyph(ch: char) -> Result<GlyphSprite, usize> {
match zh::CODEPOINTS.binary_search(&(ch as u32)) {
Ok(n) => {

View file

@ -4,35 +4,35 @@ macro_rules! style_wrapper {
($rule: ident, $base_style: ident, $ch: ident) => {
match & $base_style {
GlyphStyle::Small => {
$rule!(small_glyph, $ch)
$rule!(small_glyph, emoji_glyph, $ch)
}
GlyphStyle::Bold => {
$rule!(bold_glyph, $ch)
$rule!(bold_glyph, emoji_glyph, $ch)
}
GlyphStyle::Monospace => {
$rule!(mono_glyph, $ch)
$rule!(mono_glyph, emoji_glyph, $ch)
}
GlyphStyle::Large => {
$rule!(large_glyph, $ch)
$rule!(large_glyph, emoji_large_glyph, $ch)
}
GlyphStyle::ExtraLarge => {
$rule!(extra_large_glyph, $ch)
$rule!(extra_large_glyph, emoji_large_glyph, $ch)
}
// default to regular
_ => {
$rule!(regular_glyph, $ch)
$rule!(regular_glyph, emoji_glyph, $ch)
}
}
}
}
macro_rules! zh_rules {
($base_style: expr, $ch: ident) => {
($base_style: expr, $emoji_style: expr, $ch: ident) => {
match zh_glyph($ch) {
Ok(g) => g,
_ => match $base_style($ch) {
Ok(g) => g,
_ => match emoji_glyph($ch) {
_ => match $emoji_style($ch) {
Ok(g) => g,
_ => match ja_glyph($ch) {
Ok(g) => g,
@ -51,12 +51,12 @@ macro_rules! zh_rules {
}
macro_rules! jp_rules {
($base_style: expr, $ch: ident) => {
($base_style: expr, $emoji_style: expr, $ch: ident) => {
match ja_glyph($ch) {
Ok(g) => g,
_ => match $base_style($ch) {
Ok(g) => g,
_ => match emoji_glyph($ch) {
_ => match $emoji_style($ch) {
Ok(g) => g,
_ => match zh_glyph($ch) {
Ok(g) => g,
@ -75,12 +75,12 @@ macro_rules! jp_rules {
}
macro_rules! kr_rules {
($base_style: expr, $ch: ident) => {
($base_style: expr, $emoji_style: expr, $ch: ident) => {
match kr_glyph($ch) {
Ok(g) => g,
_ => match $base_style($ch) {
Ok(g) => g,
_ => match emoji_glyph($ch) {
_ => match $emoji_style($ch) {
Ok(g) => g,
_ => match ja_glyph($ch) {
Ok(g) => g,
@ -99,7 +99,7 @@ macro_rules! kr_rules {
}
macro_rules! en_audio_rules {
($base_style: expr, $ch: ident) => {
($base_style: expr, $emoji_style: expr, $ch: ident) => {
match $base_style($ch) {
Ok(g) => g,
_ => match $base_style(REPLACEMENT) {
@ -111,10 +111,10 @@ macro_rules! en_audio_rules {
}
macro_rules! english_rules {
($base_style: expr, $ch: ident) => {
($base_style: expr, $emoji_style: expr, $ch: ident) => {
match $base_style($ch) {
Ok(g) => g,
_ => match emoji_glyph($ch) {
_ => match $emoji_style($ch) {
Ok(g) => g,
_ => match ja_glyph($ch) {
Ok(g) => g,