Skip to content

Commit b040db0

Browse files
derskythehedgerskotopes
authored
Gui: Add up and down button drawing functions to GUI elements (#3804)
* feat: Add up and down button drawing functions to GUI elements Two button drawing functions, elements_button_up and elements_button_down, have been added to the GUI elements. These functions allow a button to be drawn at the top left and top right corner of the canvas respectively, with an accompanying string and icon. The underlying layout and design of these buttons is defined within these functions. * feat: Add null checks for Canvas parameter in button functions Added furi_check to ensure the Canvas parameter is not null in elements_button_up and elements_button_down functions. This prevents potential crashes due to dereferencing a null pointer. Co-authored-by: hedger <hedger@users.noreply.github.com> Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
1 parent f353e57 commit b040db0

File tree

4 files changed

+98
-8
lines changed

4 files changed

+98
-8
lines changed

applications/services/gui/elements.c

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,70 @@ void elements_button_right(Canvas* canvas, const char* str) {
185185
canvas_invert_color(canvas);
186186
}
187187

188+
void elements_button_up(Canvas* canvas, const char* str) {
189+
furi_check(canvas);
190+
191+
const Icon* icon = &I_ButtonUp_7x4;
192+
193+
const size_t button_height = 12;
194+
const size_t vertical_offset = 3;
195+
const size_t horizontal_offset = 3;
196+
const size_t string_width = canvas_string_width(canvas, str);
197+
const int32_t icon_h_offset = 3;
198+
const int32_t icon_width_with_offset = icon_get_width(icon) + icon_h_offset;
199+
const int32_t icon_v_offset = icon_get_height(icon) + (int32_t)vertical_offset;
200+
const size_t button_width = string_width + horizontal_offset * 2 + icon_width_with_offset;
201+
202+
const int32_t x = 0;
203+
const int32_t y = 0 + button_height;
204+
205+
int32_t line_x = x + button_width;
206+
int32_t line_y = y - button_height;
207+
208+
canvas_draw_box(canvas, x, line_y, button_width, button_height);
209+
canvas_draw_line(canvas, line_x + 0, line_y, line_x + 0, y - 1);
210+
canvas_draw_line(canvas, line_x + 1, line_y, line_x + 1, y - 2);
211+
canvas_draw_line(canvas, line_x + 2, line_y, line_x + 2, y - 3);
212+
213+
canvas_invert_color(canvas);
214+
canvas_draw_icon(canvas, x + horizontal_offset, y - icon_v_offset, icon);
215+
canvas_draw_str(
216+
canvas, x + horizontal_offset + icon_width_with_offset, y - vertical_offset, str);
217+
canvas_invert_color(canvas);
218+
}
219+
220+
void elements_button_down(Canvas* canvas, const char* str) {
221+
furi_check(canvas);
222+
223+
const Icon* icon = &I_ButtonDown_7x4;
224+
225+
const size_t button_height = 12;
226+
const size_t vertical_offset = 3;
227+
const size_t horizontal_offset = 3;
228+
const size_t string_width = canvas_string_width(canvas, str);
229+
const int32_t icon_h_offset = 3;
230+
const int32_t icon_width_with_offset = icon_get_width(icon) + icon_h_offset;
231+
const int32_t icon_v_offset = icon_get_height(icon) + vertical_offset + 1;
232+
const size_t button_width = string_width + horizontal_offset * 2 + icon_width_with_offset;
233+
234+
const int32_t x = canvas_width(canvas);
235+
const int32_t y = button_height;
236+
237+
int32_t line_x = x - button_width;
238+
int32_t line_y = y - button_height;
239+
240+
canvas_draw_box(canvas, line_x, line_y, button_width, button_height);
241+
canvas_draw_line(canvas, line_x - 1, line_y, line_x - 1, y - 1);
242+
canvas_draw_line(canvas, line_x - 2, line_y, line_x - 2, y - 2);
243+
canvas_draw_line(canvas, line_x - 3, line_y, line_x - 3, y - 3);
244+
245+
canvas_invert_color(canvas);
246+
canvas_draw_str(canvas, x - button_width + horizontal_offset, y - vertical_offset, str);
247+
canvas_draw_icon(
248+
canvas, x - horizontal_offset - icon_get_width(icon), y - icon_v_offset, icon);
249+
canvas_invert_color(canvas);
250+
}
251+
188252
void elements_button_center(Canvas* canvas, const char* str) {
189253
furi_check(canvas);
190254

applications/services/gui/elements.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,28 @@ void elements_button_left(Canvas* canvas, const char* str);
9696
*/
9797
void elements_button_right(Canvas* canvas, const char* str);
9898

99+
/**
100+
* @brief This function draws a button in the top left corner of the canvas with icon and string.
101+
*
102+
* The design and layout of the button is defined within this function.
103+
*
104+
* @param[in] canvas This is a pointer to the @c Canvas structure where the button will be drawn.
105+
* @param[in] str This is a pointer to the character string that will be drawn within the button.
106+
*
107+
*/
108+
void elements_button_up(Canvas* canvas, const char* str);
109+
110+
/**
111+
* @brief This function draws a button in the top right corner of the canvas with icon and string.
112+
*
113+
* The design and layout of the button is defined within this function.
114+
*
115+
* @param[in] canvas This is a pointer to the @c Canvas structure where the button will be drawn.
116+
* @param[in] str This is a pointer to the character string that will be drawn within the button.
117+
*
118+
*/
119+
void elements_button_down(Canvas* canvas, const char* str);
120+
99121
/** Draw button in center
100122
*
101123
* @param canvas Canvas instance

targets/f18/api_symbols.csv

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
entry,status,name,type,params
2-
Version,+,72.1,,
2+
Version,+,72.2,,
33
Header,+,applications/services/bt/bt_service/bt.h,,
44
Header,+,applications/services/bt/bt_service/bt_keys_storage.h,,
55
Header,+,applications/services/cli/cli.h,,
@@ -13,13 +13,13 @@ Header,+,applications/services/gui/icon_i.h,,
1313
Header,+,applications/services/gui/modules/button_menu.h,,
1414
Header,+,applications/services/gui/modules/button_panel.h,,
1515
Header,+,applications/services/gui/modules/byte_input.h,,
16-
Header,+,applications/services/gui/modules/number_input.h,,
1716
Header,+,applications/services/gui/modules/dialog_ex.h,,
1817
Header,+,applications/services/gui/modules/empty_screen.h,,
1918
Header,+,applications/services/gui/modules/file_browser.h,,
2019
Header,+,applications/services/gui/modules/file_browser_worker.h,,
2120
Header,+,applications/services/gui/modules/loading.h,,
2221
Header,+,applications/services/gui/modules/menu.h,,
22+
Header,+,applications/services/gui/modules/number_input.h,,
2323
Header,+,applications/services/gui/modules/popup.h,,
2424
Header,+,applications/services/gui/modules/submenu.h,,
2525
Header,+,applications/services/gui/modules/text_box.h,,
@@ -723,11 +723,6 @@ Function,+,byte_input_free,void,ByteInput*
723723
Function,+,byte_input_get_view,View*,ByteInput*
724724
Function,+,byte_input_set_header_text,void,"ByteInput*, const char*"
725725
Function,+,byte_input_set_result_callback,void,"ByteInput*, ByteInputCallback, ByteChangedCallback, void*, uint8_t*, uint8_t"
726-
Function,+,number_input_alloc,NumberInput*,
727-
Function,+,number_input_free,void,NumberInput*
728-
Function,+,number_input_get_view,View*,NumberInput*
729-
Function,+,number_input_set_header_text,void,"NumberInput*, const char*"
730-
Function,+,number_input_set_result_callback,void,"NumberInput*, NumberInputCallback, void*, int32_t, int32_t, int32_t"
731726
Function,-,bzero,void,"void*, size_t"
732727
Function,+,calloc,void*,"size_t, size_t"
733728
Function,+,canvas_clear,void,Canvas*
@@ -883,8 +878,10 @@ Function,+,elements_bold_rounded_frame,void,"Canvas*, int32_t, int32_t, size_t,
883878
Function,+,elements_bubble,void,"Canvas*, int32_t, int32_t, size_t, size_t"
884879
Function,+,elements_bubble_str,void,"Canvas*, int32_t, int32_t, const char*, Align, Align"
885880
Function,+,elements_button_center,void,"Canvas*, const char*"
881+
Function,+,elements_button_down,void,"Canvas*, const char*"
886882
Function,+,elements_button_left,void,"Canvas*, const char*"
887883
Function,+,elements_button_right,void,"Canvas*, const char*"
884+
Function,+,elements_button_up,void,"Canvas*, const char*"
888885
Function,+,elements_frame,void,"Canvas*, int32_t, int32_t, size_t, size_t"
889886
Function,+,elements_multiline_text,void,"Canvas*, int32_t, int32_t, const char*"
890887
Function,+,elements_multiline_text_aligned,void,"Canvas*, int32_t, int32_t, Align, Align, const char*"
@@ -2197,6 +2194,11 @@ Function,+,notification_internal_message_block,void,"NotificationApp*, const Not
21972194
Function,+,notification_message,void,"NotificationApp*, const NotificationSequence*"
21982195
Function,+,notification_message_block,void,"NotificationApp*, const NotificationSequence*"
21992196
Function,-,nrand48,long,unsigned short[3]
2197+
Function,+,number_input_alloc,NumberInput*,
2198+
Function,+,number_input_free,void,NumberInput*
2199+
Function,+,number_input_get_view,View*,NumberInput*
2200+
Function,+,number_input_set_header_text,void,"NumberInput*, const char*"
2201+
Function,+,number_input_set_result_callback,void,"NumberInput*, NumberInputCallback, void*, int32_t, int32_t, int32_t"
22002202
Function,-,on_exit,int,"void (*)(int, void*), void*"
22012203
Function,+,onewire_host_alloc,OneWireHost*,const GpioPin*
22022204
Function,+,onewire_host_free,void,OneWireHost*

targets/f7/api_symbols.csv

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
entry,status,name,type,params
2-
Version,+,72.1,,
2+
Version,+,72.2,,
33
Header,+,applications/drivers/subghz/cc1101_ext/cc1101_ext_interconnect.h,,
44
Header,+,applications/services/bt/bt_service/bt.h,,
55
Header,+,applications/services/bt/bt_service/bt_keys_storage.h,,
@@ -965,8 +965,10 @@ Function,+,elements_bold_rounded_frame,void,"Canvas*, int32_t, int32_t, size_t,
965965
Function,+,elements_bubble,void,"Canvas*, int32_t, int32_t, size_t, size_t"
966966
Function,+,elements_bubble_str,void,"Canvas*, int32_t, int32_t, const char*, Align, Align"
967967
Function,+,elements_button_center,void,"Canvas*, const char*"
968+
Function,+,elements_button_down,void,"Canvas*, const char*"
968969
Function,+,elements_button_left,void,"Canvas*, const char*"
969970
Function,+,elements_button_right,void,"Canvas*, const char*"
971+
Function,+,elements_button_up,void,"Canvas*, const char*"
970972
Function,+,elements_frame,void,"Canvas*, int32_t, int32_t, size_t, size_t"
971973
Function,+,elements_multiline_text,void,"Canvas*, int32_t, int32_t, const char*"
972974
Function,+,elements_multiline_text_aligned,void,"Canvas*, int32_t, int32_t, Align, Align, const char*"

0 commit comments

Comments
 (0)