@@ -15,6 +15,8 @@ pub const Error = error{
1515 GraphicsInitFailed ,
1616 WindowCreationFailed ,
1717 GraphicsRendererFailed ,
18+ FontRequired ,
19+ RootAcceptsPanelsOnly ,
1820};
1921
2022/// Display describes how to draw all visual elements onto the main
@@ -1314,7 +1316,7 @@ pub const Display = struct {
13141316 @tagName (element .type ),
13151317 element .name ,
13161318 });
1317- unreachable ; // add_panel only accepts panels
1319+ return Error . RootAcceptsPanelsOnly ;
13181320 }
13191321 return self .root .add (allocator , self , element );
13201322 }
@@ -2815,7 +2817,7 @@ pub fn setup_checkbox(
28152817 element .background .image = null ;
28162818 element .type .checkbox .translated = "" ;
28172819 element .type .checkbox .elements = .empty ;
2818- element .type .checkbox .font = select_font (self .fonts .items , element .type .checkbox .font_name );
2820+ element .type .checkbox .font = try select_font (self .fonts .items , element .type .checkbox .font_name );
28192821
28202822 if (element .focus == .unspecified )
28212823 element .focus = .can_focus ;
@@ -2869,7 +2871,7 @@ pub fn setup_label(
28692871 element .background .image = null ;
28702872 element .type .label .translated = "" ;
28712873 element .type .label .elements = .empty ;
2872- element .type .label .font = select_font (self .fonts .items , element .type .label .font_name );
2874+ element .type .label .font = try select_font (self .fonts .items , element .type .label .font_name );
28732875
28742876 if (element .focus == .unspecified ) {
28752877 if (element .type .label .on_click .func != null )
@@ -2901,7 +2903,7 @@ pub fn setup_text_input(
29012903 if (element .focus == .unspecified ) {
29022904 element .focus = .can_focus ;
29032905 }
2904- element .type .text_input .font = select_font (self .fonts .items , element .type .text_input .font_name );
2906+ element .type .text_input .font = try select_font (self .fonts .items , element .type .text_input .font_name );
29052907
29062908 if (element .type .text_input .icon_texture_name ) | icon | {
29072909 if (try self .load_texture (allocator , icon )) | texture | {
@@ -2983,7 +2985,7 @@ pub fn setup_sprite(
29832985 trace ("sprite {s} bg {s}" , .{ element .name , element .background .image_name .? });
29842986}
29852987
2986- fn select_font (fonts : []* Font , name : ? []const u8 ) * Font {
2988+ fn select_font (fonts : []* Font , name : ? []const u8 ) error { FontRequired } ! * Font {
29872989 if (name ) | font_name | {
29882990 for (fonts ) | font | {
29892991 if (std .mem .eql (u8 , font .name , font_name )) {
@@ -2994,8 +2996,7 @@ fn select_font(fonts: []*Font, name: ?[]const u8) *Font {
29942996 }
29952997 if (fonts .len > 0 ) return fonts [0 ];
29962998
2997- std .debug .assert (fonts .len > 0 );
2998- unreachable ; // select_font requires at least one font
2999+ return Error .FontRequired ;
29993000}
30003001
30013002pub fn setup_button (
@@ -3013,7 +3014,7 @@ pub fn setup_button(
30133014 element .type .button .background_hover = null ;
30143015 element .type .button .background_disabled = null ;
30153016 element .type .button .text_size = .normal ;
3016- element .type .button .font = select_font (display .fonts .items , element .type .button .font_name );
3017+ element .type .button .font = try select_font (display .fonts .items , element .type .button .font_name );
30173018
30183019 if (element .focus == .unspecified )
30193020 element .focus = .can_focus ;
@@ -3696,6 +3697,7 @@ test "test_init" {
36963697 const allocator = std .testing .allocator ;
36973698 var display = try Display .create (allocator , "test" , "test" , "test" , "./test/repo" , null , "test translation" , 0 );
36983699 defer display .destroy (allocator );
3700+
36993701 var panel = try display .add_panel (allocator , .{
37003702 .rect = .{ .width = 500 , .height = 200 },
37013703 .minimum = .{ .width = 5 , .height = 8 },
0 commit comments