Skip to content

Commit 9df6fc5

Browse files
authored
* Added --clean flag to custom FBT tool (#133)
* Improved TOTP code rendering
1 parent 7b34241 commit 9df6fc5

File tree

1 file changed

+41
-20
lines changed

1 file changed

+41
-20
lines changed

ui/scenes/generate_token/totp_scene_generate_token.c

+41-20
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@
2121
#define PROGRESS_BAR_MARGIN (3)
2222
#define PROGRESS_BAR_HEIGHT (4)
2323

24+
typedef struct {
25+
uint8_t progress_bar_x;
26+
uint8_t progress_bar_width;
27+
uint8_t code_total_length;
28+
uint8_t code_offset_x;
29+
uint8_t code_offset_x_inc;
30+
uint8_t code_offset_y;
31+
} UiPrecalculatedDimensions;
32+
2433
typedef struct {
2534
uint16_t current_token_index;
2635
char last_code[TOTP_TOKEN_DIGITS_MAX_COUNT + 1];
@@ -30,8 +39,7 @@ typedef struct {
3039
NotificationMessage const** notification_sequence_automation;
3140
FuriMutex* last_code_update_sync;
3241
TotpGenerateCodeWorkerContext* generate_code_worker_context;
33-
uint8_t progress_bar_x;
34-
uint8_t progress_bar_width;
42+
UiPrecalculatedDimensions ui_precalculated_dimensions;
3543
} SceneState;
3644

3745
static const NotificationSequence*
@@ -133,29 +141,40 @@ static void update_totp_params(PluginState* const plugin_state) {
133141

134142
static void draw_totp_code(Canvas* const canvas, const SceneState* const scene_state) {
135143
uint8_t code_length = scene_state->current_token->digits;
144+
uint8_t offset_x = scene_state->ui_precalculated_dimensions.code_offset_x;
136145
uint8_t char_width = modeNine_15ptFontInfo.charInfo[0].width;
137-
uint8_t total_length = code_length * (char_width + modeNine_15ptFontInfo.spacePixels);
138-
uint8_t offset_x = (SCREEN_WIDTH - total_length) >> 1;
139-
uint8_t offset_x_inc = char_width + modeNine_15ptFontInfo.spacePixels;
140-
uint8_t offset_y = SCREEN_HEIGHT_CENTER - (modeNine_15ptFontInfo.height >> 1);
146+
uint8_t offset_x_inc = scene_state->ui_precalculated_dimensions.code_offset_x_inc;
141147
for(uint8_t i = 0; i < code_length; i++) {
142148
char ch = scene_state->last_code[i];
143-
uint8_t char_index = ch - modeNine_15ptFontInfo.startChar;
144-
canvas_draw_xbm(
145-
canvas,
146-
offset_x,
147-
offset_y,
148-
char_width,
149-
modeNine_15ptFontInfo.height,
150-
&modeNine_15ptFontInfo.data[modeNine_15ptFontInfo.charInfo[char_index].offset]);
149+
if(ch >= modeNine_15ptFontInfo.startChar && ch <= modeNine_15ptFontInfo.endChar) {
150+
uint8_t char_index = ch - modeNine_15ptFontInfo.startChar;
151+
canvas_draw_xbm(
152+
canvas,
153+
offset_x,
154+
scene_state->ui_precalculated_dimensions.code_offset_y,
155+
char_width,
156+
modeNine_15ptFontInfo.height,
157+
&modeNine_15ptFontInfo.data[modeNine_15ptFontInfo.charInfo[char_index].offset]);
158+
}
151159

152160
offset_x += offset_x_inc;
153161
}
154162
}
155163

156164
static void on_new_token_code_generated(bool time_left, void* context) {
165+
const PluginState* plugin_state = context;
166+
SceneState* scene_state = plugin_state->current_scene_state;
167+
uint8_t char_width = modeNine_15ptFontInfo.charInfo[0].width;
168+
scene_state->ui_precalculated_dimensions.code_total_length =
169+
scene_state->current_token->digits * (char_width + modeNine_15ptFontInfo.spacePixels);
170+
scene_state->ui_precalculated_dimensions.code_offset_x =
171+
(SCREEN_WIDTH - scene_state->ui_precalculated_dimensions.code_total_length) >> 1;
172+
scene_state->ui_precalculated_dimensions.code_offset_x_inc =
173+
char_width + modeNine_15ptFontInfo.spacePixels;
174+
scene_state->ui_precalculated_dimensions.code_offset_y =
175+
SCREEN_HEIGHT_CENTER - (modeNine_15ptFontInfo.height >> 1);
176+
157177
if(time_left) {
158-
PluginState* plugin_state = context;
159178
notification_message(
160179
plugin_state->notification_app,
161180
get_notification_sequence_new_token(plugin_state, plugin_state->current_scene_state));
@@ -164,10 +183,12 @@ static void on_new_token_code_generated(bool time_left, void* context) {
164183

165184
static void on_code_lifetime_updated_generated(float code_lifetime_percent, void* context) {
166185
SceneState* scene_state = context;
167-
scene_state->progress_bar_width =
186+
scene_state->ui_precalculated_dimensions.progress_bar_width =
168187
(uint8_t)((float)(SCREEN_WIDTH - (PROGRESS_BAR_MARGIN << 1)) * code_lifetime_percent);
169-
scene_state->progress_bar_x =
170-
((SCREEN_WIDTH - (PROGRESS_BAR_MARGIN << 1) - scene_state->progress_bar_width) >> 1) +
188+
scene_state->ui_precalculated_dimensions.progress_bar_x =
189+
((SCREEN_WIDTH - (PROGRESS_BAR_MARGIN << 1) -
190+
scene_state->ui_precalculated_dimensions.progress_bar_width) >>
191+
1) +
171192
PROGRESS_BAR_MARGIN;
172193
}
173194

@@ -301,9 +322,9 @@ void totp_scene_generate_token_render(Canvas* const canvas, PluginState* plugin_
301322

302323
canvas_draw_box(
303324
canvas,
304-
scene_state->progress_bar_x,
325+
scene_state->ui_precalculated_dimensions.progress_bar_x,
305326
SCREEN_HEIGHT - PROGRESS_BAR_MARGIN - PROGRESS_BAR_HEIGHT,
306-
scene_state->progress_bar_width,
327+
scene_state->ui_precalculated_dimensions.progress_bar_width,
307328
PROGRESS_BAR_HEIGHT);
308329

309330
if(plugin_state->tokens_count > 1) {

0 commit comments

Comments
 (0)